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 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 * Override the user-agent header for all requests. |
| 36 */ |
| 37 public UrlRequestContextConfig setUserAgent(String userAgent) { |
| 38 return putString(UrlRequestContextConfigList.USER_AGENT, userAgent); |
| 39 } |
| 40 |
| 41 String userAgent() { |
| 42 return mConfig.optString(UrlRequestContextConfigList.USER_AGENT); |
| 43 } |
| 44 |
| 45 /** |
| 46 * String, path to directory for HTTP Cache and Cookie Storage. |
| 47 */ |
| 48 public UrlRequestContextConfig setStoragePath(String value) { |
| 49 return putString(UrlRequestContextConfigList.STORAGE_PATH, value); |
| 50 } |
| 51 |
| 52 /** |
| 53 * Boolean, use HttpUrlConnection-based implementation if true. All other |
| 54 * keys are not applicable. |
| 55 */ |
| 56 public UrlRequestContextConfig enableLegacyMode(boolean value) { |
| 57 return putBoolean(UrlRequestContextConfigList.ENABLE_LEGACY_MODE, |
| 58 value); |
| 59 } |
| 60 |
| 61 boolean legacyMode() { |
| 62 return mConfig.optBoolean( |
| 63 UrlRequestContextConfigList.ENABLE_LEGACY_MODE); |
| 64 } |
| 65 |
| 66 /** |
28 * Override the name of the native library backing cronet. | 67 * Override the name of the native library backing cronet. |
29 */ | 68 */ |
30 public HttpUrlRequestFactoryConfig setLibraryName(String libName) { | 69 public UrlRequestContextConfig setLibraryName(String libName) { |
31 return putString(UrlRequestContextConfig.NATIVE_LIBRARY_NAME, libName); | 70 return putString(UrlRequestContextConfigList.NATIVE_LIBRARY_NAME, |
| 71 libName); |
32 } | 72 } |
33 | 73 |
34 /** | 74 String libraryName() { |
35 * Create config from json serialized using @toString. | 75 return mConfig.optString( |
36 */ | 76 UrlRequestContextConfigList.NATIVE_LIBRARY_NAME, "cronet"); |
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 } | 77 } |
52 | 78 |
53 /** | 79 /** |
54 * Boolean, enable QUIC if true. | 80 * Boolean, enable QUIC if true. |
55 */ | 81 */ |
56 public HttpUrlRequestFactoryConfig enableQUIC(boolean value) { | 82 public UrlRequestContextConfig enableQUIC(boolean value) { |
57 return putBoolean(UrlRequestContextConfig.ENABLE_QUIC, value); | 83 return putBoolean(UrlRequestContextConfigList.ENABLE_QUIC, value); |
58 } | 84 } |
59 | 85 |
60 /** | 86 /** |
61 * Boolean, enable SPDY if true. | 87 * Boolean, enable SPDY if true. |
62 */ | 88 */ |
63 public HttpUrlRequestFactoryConfig enableSPDY(boolean value) { | 89 public UrlRequestContextConfig enableSPDY(boolean value) { |
64 return putBoolean(UrlRequestContextConfig.ENABLE_SPDY, value); | 90 return putBoolean(UrlRequestContextConfigList.ENABLE_SPDY, value); |
65 } | |
66 | |
67 String libraryName() { | |
68 return mConfig.optString(UrlRequestContextConfig.NATIVE_LIBRARY_NAME, | |
69 "cronet"); | |
70 } | 91 } |
71 | 92 |
72 /** | 93 /** |
73 * Enumeration, Disable or Enable Disk or Memory Cache and specify its | 94 * Enumeration, Disable or Enable Disk or Memory Cache and specify its |
74 * maximum size in bytes. | 95 * maximum size in bytes. |
75 */ | 96 */ |
76 public enum HttpCache { DISABLED, IN_MEMORY, DISK }; | 97 public enum HttpCache { DISABLED, IN_MEMORY, DISK }; |
77 public HttpUrlRequestFactoryConfig enableHttpCache(HttpCache value, | 98 public UrlRequestContextConfig enableHttpCache(HttpCache value, |
78 long maxSize) { | 99 long maxSize) { |
79 switch(value) { | 100 switch(value) { |
80 case DISABLED: | 101 case DISABLED: |
81 return putString(UrlRequestContextConfig.HTTP_CACHE, | 102 return putString(UrlRequestContextConfigList.HTTP_CACHE, |
82 UrlRequestContextConfig.HTTP_CACHE_DISABLED); | 103 UrlRequestContextConfigList.HTTP_CACHE_DISABLED); |
83 case DISK: | 104 case DISK: |
84 putLong(UrlRequestContextConfig.HTTP_CACHE_MAX_SIZE, maxSize); | 105 putLong(UrlRequestContextConfigList.HTTP_CACHE_MAX_SIZE, |
85 return putString(UrlRequestContextConfig.HTTP_CACHE, | 106 maxSize); |
86 UrlRequestContextConfig.HTTP_CACHE_DISK); | 107 return putString(UrlRequestContextConfigList.HTTP_CACHE, |
| 108 UrlRequestContextConfigList.HTTP_CACHE_DISK); |
87 case IN_MEMORY: | 109 case IN_MEMORY: |
88 putLong(UrlRequestContextConfig.HTTP_CACHE_MAX_SIZE, maxSize); | 110 putLong(UrlRequestContextConfigList.HTTP_CACHE_MAX_SIZE, |
89 return putString(UrlRequestContextConfig.HTTP_CACHE, | 111 maxSize); |
90 UrlRequestContextConfig.HTTP_CACHE_MEMORY); | 112 return putString(UrlRequestContextConfigList.HTTP_CACHE, |
| 113 UrlRequestContextConfigList.HTTP_CACHE_MEMORY); |
91 } | 114 } |
92 return this; | 115 return this; |
93 } | 116 } |
94 | 117 |
95 /** | 118 /** |
96 * String, path to directory for HTTP Cache and Cookie Storage. | |
97 */ | |
98 public HttpUrlRequestFactoryConfig setStoragePath(String value) { | |
99 return putString(UrlRequestContextConfig.STORAGE_PATH, value); | |
100 } | |
101 | |
102 /** | |
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 if (host.contains("/")) { | 130 if (host.contains("/")) { |
115 throw new IllegalArgumentException("Illegal QUIC Hint Host: " + | 131 throw new IllegalArgumentException("Illegal QUIC Hint Host: " |
116 host); | 132 + host); |
117 } | 133 } |
118 try { | 134 try { |
119 JSONArray quicHints = mConfig.optJSONArray( | 135 JSONArray quicHints = mConfig.optJSONArray( |
120 UrlRequestContextConfig.QUIC_HINTS); | 136 UrlRequestContextConfigList.QUIC_HINTS); |
121 if (quicHints == null) { | 137 if (quicHints == null) { |
122 quicHints = new JSONArray(); | 138 quicHints = new JSONArray(); |
123 mConfig.put(UrlRequestContextConfig.QUIC_HINTS, quicHints); | 139 mConfig.put(UrlRequestContextConfigList.QUIC_HINTS, quicHints); |
124 } | 140 } |
125 | 141 |
126 JSONObject hint = new JSONObject(); | 142 JSONObject hint = new JSONObject(); |
127 hint.put(UrlRequestContextConfig.QUIC_HINT_HOST, host); | 143 hint.put(UrlRequestContextConfigList.QUIC_HINT_HOST, host); |
128 hint.put(UrlRequestContextConfig.QUIC_HINT_PORT, port); | 144 hint.put(UrlRequestContextConfigList.QUIC_HINT_PORT, port); |
129 hint.put(UrlRequestContextConfig.QUIC_HINT_ALT_PORT, alternatePort); | 145 hint.put(UrlRequestContextConfigList.QUIC_HINT_ALT_PORT, |
| 146 alternatePort); |
130 quicHints.put(hint); | 147 quicHints.put(hint); |
131 } catch (JSONException e) { | 148 } catch (JSONException e) { |
132 // Intentionally do nothing. | 149 // Intentionally do nothing. |
133 } | 150 } |
134 return this; | 151 return this; |
135 } | 152 } |
136 | 153 |
137 /** | 154 /** |
138 * Get JSON string representation of the config. | 155 * Get JSON string representation of the config. |
139 */ | 156 */ |
140 @Override | 157 @Override |
141 public String toString() { | 158 public String toString() { |
142 return mConfig.toString(); | 159 return mConfig.toString(); |
143 } | 160 } |
144 | 161 |
145 /** | 162 /** |
146 * Sets a boolean value in the config. Returns a reference to the same | 163 * Sets a boolean value in the config. Returns a reference to the same |
147 * config object, so you can chain put calls together. | 164 * config object, so you can chain put calls together. |
148 */ | 165 */ |
149 private HttpUrlRequestFactoryConfig putBoolean(String key, boolean value) { | 166 private UrlRequestContextConfig putBoolean(String key, boolean value) { |
150 try { | 167 try { |
151 mConfig.put(key, value); | 168 mConfig.put(key, value); |
152 } catch (JSONException e) { | 169 } catch (JSONException e) { |
153 // Intentionally do nothing. | 170 // Intentionally do nothing. |
154 } | 171 } |
155 return this; | 172 return this; |
156 } | 173 } |
157 | 174 |
158 /** | 175 /** |
159 * Sets a long value in the config. Returns a reference to the same | 176 * Sets a long value in the config. Returns a reference to the same |
160 * config object, so you can chain put calls together. | 177 * config object, so you can chain put calls together. |
161 */ | 178 */ |
162 private HttpUrlRequestFactoryConfig putLong(String key, long value) { | 179 private UrlRequestContextConfig putLong(String key, long value) { |
163 try { | 180 try { |
164 mConfig.put(key, value); | 181 mConfig.put(key, value); |
165 } catch (JSONException e) { | 182 } catch (JSONException e) { |
166 // Intentionally do nothing. | 183 // Intentionally do nothing. |
167 } | 184 } |
168 return this; | 185 return this; |
169 } | 186 } |
170 | 187 |
171 /** | 188 /** |
172 * Sets a string value in the config. Returns a reference to the same | 189 * Sets a string value in the config. Returns a reference to the same |
173 * config object, so you can chain put calls together. | 190 * config object, so you can chain put calls together. |
174 */ | 191 */ |
175 private HttpUrlRequestFactoryConfig putString(String key, String value) { | 192 private UrlRequestContextConfig putString(String key, String value) { |
176 try { | 193 try { |
177 mConfig.put(key, value); | 194 mConfig.put(key, value); |
178 } catch (JSONException e) { | 195 } catch (JSONException e) { |
179 // Intentionally do nothing. | 196 // Intentionally do nothing. |
180 } | 197 } |
181 return this; | 198 return this; |
182 } | 199 } |
183 | 200 |
184 private JSONObject mConfig = new JSONObject(); | 201 private JSONObject mConfig = new JSONObject(); |
185 } | 202 } |
OLD | NEW |