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 * 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 /** | |
xunjieli
2014/10/23 14:04:33
nit: +1 indent.
mef
2014/10/24 03:31:45
Done.
| |
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 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 |