OLD | NEW |
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 HttpUrlRequestFactory, which allows runtime configuration of |
13 * HttpUrlRequestFactory. | 13 * HttpUrlRequestFactory. |
14 */ | 14 */ |
15 public class HttpUrlRequestFactoryConfig { | 15 public class HttpUrlRequestFactoryConfig { |
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 HttpUrlRequestFactoryConfig() { |
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 * Override the name of the native library backing cronet. | |
29 */ | |
30 public HttpUrlRequestFactoryConfig setLibraryName(String libName) { | |
31 return putString(UrlRequestContextConfig.NATIVE_LIBRARY_NAME, libName); | |
32 } | |
33 | |
34 /** | |
35 * Create config from json serialized using @toString. | 28 * Create config from json serialized using @toString. |
36 */ | 29 */ |
37 public HttpUrlRequestFactoryConfig(String json) throws JSONException { | 30 public HttpUrlRequestFactoryConfig(String json) throws JSONException { |
38 mConfig = new JSONObject(json); | 31 mConfig = new JSONObject(json); |
39 } | 32 } |
40 | 33 |
41 /** | 34 /** |
42 * Boolean, use HttpUrlRequest-based implementation if true. All other | 35 * Boolean, use HttpUrlRequest-based implementation if true. All other |
43 * keys are not applicable. | 36 * keys are not applicable. |
44 */ | 37 */ |
45 public HttpUrlRequestFactoryConfig enableLegacyMode(boolean value) { | 38 public HttpUrlRequestFactoryConfig enableLegacyMode(boolean value) { |
46 return putBoolean(UrlRequestContextConfig.ENABLE_LEGACY_MODE, value); | 39 return putBoolean(UrlRequestContextConfig.ENABLE_LEGACY_MODE, value); |
47 } | 40 } |
48 | 41 |
49 boolean legacyMode() { | 42 boolean legacyMode() { |
50 return mConfig.optBoolean(UrlRequestContextConfig.ENABLE_LEGACY_MODE); | 43 return mConfig.optBoolean(UrlRequestContextConfig.ENABLE_LEGACY_MODE); |
51 } | 44 } |
52 | 45 |
| 46 /** |
| 47 * Override the user-agent header for all requests. |
| 48 */ |
| 49 public HttpUrlRequestFactoryConfig setUserAgent(String userAgent) { |
| 50 return putString(UrlRequestContextConfig.USER_AGENT, userAgent); |
| 51 } |
| 52 |
| 53 String userAgent() { |
| 54 return mConfig.optString(UrlRequestContextConfig.USER_AGENT); |
| 55 } |
| 56 |
| 57 /** |
| 58 * Override the name of the native library backing cronet. |
| 59 */ |
| 60 public HttpUrlRequestFactoryConfig setLibraryName(String libName) { |
| 61 return putString(UrlRequestContextConfig.NATIVE_LIBRARY_NAME, libName); |
| 62 } |
| 63 |
53 /** | 64 /** |
54 * Boolean, enable QUIC if true. | 65 * Boolean, enable QUIC if true. |
55 */ | 66 */ |
56 public HttpUrlRequestFactoryConfig enableQUIC(boolean value) { | 67 public HttpUrlRequestFactoryConfig enableQUIC(boolean value) { |
57 return putBoolean(UrlRequestContextConfig.ENABLE_QUIC, value); | 68 return putBoolean(UrlRequestContextConfig.ENABLE_QUIC, value); |
58 } | 69 } |
59 | 70 |
60 /** | 71 /** |
61 * Boolean, enable SPDY if true. | 72 * Boolean, enable SPDY if true. |
62 */ | 73 */ |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 quicHints = new JSONArray(); | 129 quicHints = new JSONArray(); |
119 mConfig.put(UrlRequestContextConfig.QUIC_HINTS, quicHints); | 130 mConfig.put(UrlRequestContextConfig.QUIC_HINTS, quicHints); |
120 } | 131 } |
121 | 132 |
122 JSONObject hint = new JSONObject(); | 133 JSONObject hint = new JSONObject(); |
123 hint.put(UrlRequestContextConfig.QUIC_HINT_HOST, host); | 134 hint.put(UrlRequestContextConfig.QUIC_HINT_HOST, host); |
124 hint.put(UrlRequestContextConfig.QUIC_HINT_PORT, port); | 135 hint.put(UrlRequestContextConfig.QUIC_HINT_PORT, port); |
125 hint.put(UrlRequestContextConfig.QUIC_HINT_ALT_PORT, alternatePort); | 136 hint.put(UrlRequestContextConfig.QUIC_HINT_ALT_PORT, alternatePort); |
126 quicHints.put(hint); | 137 quicHints.put(hint); |
127 } catch (JSONException e) { | 138 } catch (JSONException e) { |
128 ; | 139 // Ignore the exception. |
129 } | 140 } |
130 return this; | 141 return this; |
131 } | 142 } |
132 | 143 |
133 /** | 144 /** |
134 * Get JSON string representation of the config. | 145 * Get JSON string representation of the config. |
135 */ | 146 */ |
136 public String toString() { | 147 public String toString() { |
137 return mConfig.toString(); | 148 return mConfig.toString(); |
138 } | 149 } |
139 | 150 |
140 /** | 151 /** |
141 * Sets a boolean value in the config. Returns a reference to the same | 152 * Sets a boolean value in the config. Returns a reference to the same |
142 * config object, so you can chain put calls together. | 153 * config object, so you can chain put calls together. |
143 */ | 154 */ |
144 private HttpUrlRequestFactoryConfig putBoolean(String key, boolean value) { | 155 private HttpUrlRequestFactoryConfig putBoolean(String key, boolean value) { |
145 try { | 156 try { |
146 mConfig.put(key, value); | 157 mConfig.put(key, value); |
147 } catch (JSONException e) { | 158 } catch (JSONException e) { |
148 ; | 159 // Ignore the exception. |
149 } | 160 } |
150 return this; | 161 return this; |
151 } | 162 } |
152 | 163 |
153 /** | 164 /** |
154 * Sets a long value in the config. Returns a reference to the same | 165 * Sets a long value in the config. Returns a reference to the same |
155 * config object, so you can chain put calls together. | 166 * config object, so you can chain put calls together. |
156 */ | 167 */ |
157 private HttpUrlRequestFactoryConfig putLong(String key, long value) { | 168 private HttpUrlRequestFactoryConfig putLong(String key, long value) { |
158 try { | 169 try { |
159 mConfig.put(key, value); | 170 mConfig.put(key, value); |
160 } catch (JSONException e) { | 171 } catch (JSONException e) { |
161 ; | 172 // Ignore the exception. |
162 } | 173 } |
163 return this; | 174 return this; |
164 } | 175 } |
165 | 176 |
166 /** | 177 /** |
167 * Sets a string value in the config. Returns a reference to the same | 178 * Sets a string value in the config. Returns a reference to the same |
168 * config object, so you can chain put calls together. | 179 * config object, so you can chain put calls together. |
169 */ | 180 */ |
170 private HttpUrlRequestFactoryConfig putString(String key, String value) { | 181 private HttpUrlRequestFactoryConfig putString(String key, String value) { |
171 try { | 182 try { |
172 mConfig.put(key, value); | 183 mConfig.put(key, value); |
173 } catch (JSONException e) { | 184 } catch (JSONException e) { |
174 ; | 185 // Ignore the exception. |
175 } | 186 } |
176 return this; | 187 return this; |
177 } | 188 } |
178 | 189 |
179 private JSONObject mConfig = new JSONObject(); | 190 private JSONObject mConfig = new JSONObject(); |
180 } | 191 } |
OLD | NEW |