Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: components/cronet/android/java/src/org/chromium/net/UrlRequestContextConfig.java

Issue 586143002: Initial implementation of Cronet Async API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Renamed UrlRequestFactory into UrlRequestContext, addressed some comments. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.net; 5 package org.chromium.net;
6 6
7 import org.json.JSONArray; 7 import org.json.JSONArray;
8 import org.json.JSONException; 8 import org.json.JSONException;
9 import org.json.JSONObject; 9 import org.json.JSONObject;
10 10
11 /** 11 /**
12 * A config for HttpUrlRequestFactory, which allows runtime configuration of 12 * A config for UrlRequestContext, which allows runtime configuration of
13 * HttpUrlRequestFactory. 13 * UrlRequestContext.
14 */ 14 */
15 public class HttpUrlRequestFactoryConfig { 15 public class UrlRequestContextConfig {
16 16
17 /** 17 /**
18 * Default config enables SPDY, QUIC, in memory http cache. 18 * Default config enables SPDY, QUIC, in memory http cache.
19 */ 19 */
20 public HttpUrlRequestFactoryConfig() { 20 public UrlRequestContextConfig() {
21 enableLegacyMode(false); 21 enableLegacyMode(false);
22 enableQUIC(false); 22 enableQUIC(false);
23 enableSPDY(true); 23 enableSPDY(true);
24 enableHttpCache(HttpCache.IN_MEMORY, 100 * 1024); 24 enableHttpCache(HttpCache.IN_MEMORY, 100 * 1024);
25 } 25 }
26 26
27 /** 27 /**
28 * Create config from json serialized using @toString.
29 */
30 public UrlRequestContextConfig(String json) throws JSONException {
31 mConfig = new JSONObject(json);
32 }
33
34 /**
35 * Boolean, use HttpUrlConnection-based implementation if true. All other
36 * keys are not applicable.
37 */
38 public UrlRequestContextConfig enableLegacyMode(boolean value) {
39 return putBoolean(UrlRequestContextConfigList.ENABLE_LEGACY_MODE,
40 value);
41 }
42
43 boolean legacyMode() {
44 return mConfig.optBoolean(
45 UrlRequestContextConfigList.ENABLE_LEGACY_MODE);
46 }
47
48 /**
49 * Override the user-agent header for all requests.
50 */
51 public UrlRequestContextConfig setUserAgent(String userAgent) {
52 return putString(UrlRequestContextConfigList.USER_AGENT, userAgent);
53 }
54
55 String userAgent() {
56 return mConfig.optString(UrlRequestContextConfigList.USER_AGENT);
57 }
58
59 /**
28 * Override the name of the native library backing cronet. 60 * Override the name of the native library backing cronet.
29 */ 61 */
30 public HttpUrlRequestFactoryConfig setLibraryName(String libName) { 62 public UrlRequestContextConfig setLibraryName(String libName) {
31 return putString(UrlRequestContextConfig.NATIVE_LIBRARY_NAME, libName); 63 return putString(UrlRequestContextConfigList.NATIVE_LIBRARY_NAME,
32 } 64 libName);
33
34 /**
35 * Create config from json serialized using @toString.
36 */
37 public HttpUrlRequestFactoryConfig(String json) throws JSONException {
38 mConfig = new JSONObject(json);
39 }
40
41 /**
42 * Boolean, use HttpUrlRequest-based implementation if true. All other
43 * keys are not applicable.
44 */
45 public HttpUrlRequestFactoryConfig enableLegacyMode(boolean value) {
46 return putBoolean(UrlRequestContextConfig.ENABLE_LEGACY_MODE, value);
47 }
48
49 boolean legacyMode() {
50 return mConfig.optBoolean(UrlRequestContextConfig.ENABLE_LEGACY_MODE);
51 } 65 }
52 66
53 /** 67 /**
54 * Boolean, enable QUIC if true. 68 * Boolean, enable QUIC if true.
55 */ 69 */
56 public HttpUrlRequestFactoryConfig enableQUIC(boolean value) { 70 public UrlRequestContextConfig enableQUIC(boolean value) {
57 return putBoolean(UrlRequestContextConfig.ENABLE_QUIC, value); 71 return putBoolean(UrlRequestContextConfigList.ENABLE_QUIC, value);
58 } 72 }
59 73
60 /** 74 /**
61 * Boolean, enable SPDY if true. 75 * Boolean, enable SPDY if true.
62 */ 76 */
63 public HttpUrlRequestFactoryConfig enableSPDY(boolean value) { 77 public UrlRequestContextConfig enableSPDY(boolean value) {
64 return putBoolean(UrlRequestContextConfig.ENABLE_SPDY, value); 78 return putBoolean(UrlRequestContextConfigList.ENABLE_SPDY, value);
65 } 79 }
66 80
67 String libraryName() { 81 String libraryName() {
68 return mConfig.optString(UrlRequestContextConfig.NATIVE_LIBRARY_NAME, 82 return mConfig.optString(
69 "cronet"); 83 UrlRequestContextConfigList.NATIVE_LIBRARY_NAME, "cronet");
70 } 84 }
71 85
72 /** 86 /**
73 * Enumeration, Disable or Enable Disk or Memory Cache and specify its 87 * Enumeration, Disable or Enable Disk or Memory Cache and specify its
74 * maximum size in bytes. 88 * maximum size in bytes.
75 */ 89 */
76 public enum HttpCache { DISABLED, IN_MEMORY, DISK }; 90 public enum HttpCache { DISABLED, IN_MEMORY, DISK };
77 public HttpUrlRequestFactoryConfig enableHttpCache(HttpCache value, 91 public UrlRequestContextConfig enableHttpCache(HttpCache value,
78 long maxSize) { 92 long maxSize) {
79 switch(value) { 93 switch(value) {
80 case DISABLED: 94 case DISABLED:
81 return putString(UrlRequestContextConfig.HTTP_CACHE, 95 return putString(UrlRequestContextConfigList.HTTP_CACHE,
82 UrlRequestContextConfig.HTTP_CACHE_DISABLED); 96 UrlRequestContextConfigList.HTTP_CACHE_DISABLED);
83 case DISK: 97 case DISK:
84 putLong(UrlRequestContextConfig.HTTP_CACHE_MAX_SIZE, maxSize); 98 putLong(UrlRequestContextConfigList.HTTP_CACHE_MAX_SIZE,
85 return putString(UrlRequestContextConfig.HTTP_CACHE, 99 maxSize);
86 UrlRequestContextConfig.HTTP_CACHE_DISK); 100 return putString(UrlRequestContextConfigList.HTTP_CACHE,
101 UrlRequestContextConfigList.HTTP_CACHE_DISK);
87 case IN_MEMORY: 102 case IN_MEMORY:
88 putLong(UrlRequestContextConfig.HTTP_CACHE_MAX_SIZE, maxSize); 103 putLong(UrlRequestContextConfigList.HTTP_CACHE_MAX_SIZE,
89 return putString(UrlRequestContextConfig.HTTP_CACHE, 104 maxSize);
90 UrlRequestContextConfig.HTTP_CACHE_MEMORY); 105 return putString(UrlRequestContextConfigList.HTTP_CACHE,
106 UrlRequestContextConfigList.HTTP_CACHE_MEMORY);
91 } 107 }
92 return this; 108 return this;
93 } 109 }
94 110
95 /** 111 /**
96 * String, path to directory for HTTP Cache and Cookie Storage. 112 * String, path to directory for HTTP Cache and Cookie Storage.
97 */ 113 */
98 public HttpUrlRequestFactoryConfig setStoragePath(String value) { 114 public UrlRequestContextConfig setStoragePath(String value) {
99 return putString(UrlRequestContextConfig.STORAGE_PATH, value); 115 return putString(UrlRequestContextConfigList.STORAGE_PATH, value);
100 } 116 }
101 117
102 /** 118 /**
103 * Explicitly mark |host| as supporting QUIC. 119 * Explicitly mark |host| as supporting QUIC.
104 * Note that enableHttpCache(DISK) is needed to take advantage of 0-RTT 120 * Note that enableHttpCache(DISK) is needed to take advantage of 0-RTT
105 * connection establishment between sessions. 121 * connection establishment between sessions.
106 * 122 *
107 * @param host of the server that supports QUIC. 123 * @param host of the server that supports QUIC.
108 * @param port of the server that supports QUIC. 124 * @param port of the server that supports QUIC.
109 * @param alternatePort to use for QUIC. 125 * @param alternatePort to use for QUIC.
110 */ 126 */
111 public HttpUrlRequestFactoryConfig addQuicHint(String host, 127 public UrlRequestContextConfig addQuicHint(String host,
112 int port, 128 int port,
113 int alternatePort) { 129 int alternatePort) {
114 try { 130 try {
115 JSONArray quicHints = mConfig.optJSONArray( 131 JSONArray quicHints = mConfig.optJSONArray(
116 UrlRequestContextConfig.QUIC_HINTS); 132 UrlRequestContextConfigList.QUIC_HINTS);
117 if (quicHints == null) { 133 if (quicHints == null) {
118 quicHints = new JSONArray(); 134 quicHints = new JSONArray();
119 mConfig.put(UrlRequestContextConfig.QUIC_HINTS, quicHints); 135 mConfig.put(UrlRequestContextConfigList.QUIC_HINTS, quicHints);
120 } 136 }
121 137
122 JSONObject hint = new JSONObject(); 138 JSONObject hint = new JSONObject();
123 hint.put(UrlRequestContextConfig.QUIC_HINT_HOST, host); 139 hint.put(UrlRequestContextConfigList.QUIC_HINT_HOST, host);
124 hint.put(UrlRequestContextConfig.QUIC_HINT_PORT, port); 140 hint.put(UrlRequestContextConfigList.QUIC_HINT_PORT, port);
125 hint.put(UrlRequestContextConfig.QUIC_HINT_ALT_PORT, alternatePort); 141 hint.put(UrlRequestContextConfigList.QUIC_HINT_ALT_PORT,
142 alternatePort);
126 quicHints.put(hint); 143 quicHints.put(hint);
127 } catch (JSONException e) { 144 } catch (JSONException e) {
128 // Intentionally do nothing. 145 // Intentionally do nothing.
129 } 146 }
130 return this; 147 return this;
131 } 148 }
132 149
133 /** 150 /**
134 * Get JSON string representation of the config. 151 * Get JSON string representation of the config.
135 */ 152 */
136 @Override 153 @Override
137 public String toString() { 154 public String toString() {
138 return mConfig.toString(); 155 return mConfig.toString();
139 } 156 }
140 157
141 /** 158 /**
142 * Sets a boolean value in the config. Returns a reference to the same 159 * Sets a boolean value in the config. Returns a reference to the same
143 * config object, so you can chain put calls together. 160 * config object, so you can chain put calls together.
144 */ 161 */
145 private HttpUrlRequestFactoryConfig putBoolean(String key, boolean value) { 162 private UrlRequestContextConfig putBoolean(String key, boolean value) {
146 try { 163 try {
147 mConfig.put(key, value); 164 mConfig.put(key, value);
148 } catch (JSONException e) { 165 } catch (JSONException e) {
149 // Intentionally do nothing. 166 // Intentionally do nothing.
150 } 167 }
151 return this; 168 return this;
152 } 169 }
153 170
154 /** 171 /**
155 * Sets a long value in the config. Returns a reference to the same 172 * Sets a long value in the config. Returns a reference to the same
156 * config object, so you can chain put calls together. 173 * config object, so you can chain put calls together.
157 */ 174 */
158 private HttpUrlRequestFactoryConfig putLong(String key, long value) { 175 private UrlRequestContextConfig putLong(String key, long value) {
159 try { 176 try {
160 mConfig.put(key, value); 177 mConfig.put(key, value);
161 } catch (JSONException e) { 178 } catch (JSONException e) {
162 // Intentionally do nothing. 179 // Intentionally do nothing.
163 } 180 }
164 return this; 181 return this;
165 } 182 }
166 183
167 /** 184 /**
168 * Sets a string value in the config. Returns a reference to the same 185 * Sets a string value in the config. Returns a reference to the same
169 * config object, so you can chain put calls together. 186 * config object, so you can chain put calls together.
170 */ 187 */
171 private HttpUrlRequestFactoryConfig putString(String key, String value) { 188 private UrlRequestContextConfig putString(String key, String value) {
172 try { 189 try {
173 mConfig.put(key, value); 190 mConfig.put(key, value);
174 } catch (JSONException e) { 191 } catch (JSONException e) {
175 // Intentionally do nothing. 192 // Intentionally do nothing.
176 } 193 }
177 return this; 194 return this;
178 } 195 }
179 196
180 private JSONObject mConfig = new JSONObject(); 197 private JSONObject mConfig = new JSONObject();
181 } 198 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698