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 android.app.Activity; | 7 import android.app.Activity; |
| 8 import android.content.Context; |
8 import android.content.Intent; | 9 import android.content.Intent; |
9 import android.os.Bundle; | 10 import android.os.Bundle; |
10 import android.os.Environment; | 11 import android.os.Environment; |
11 | 12 |
12 import static junit.framework.Assert.assertEquals; | 13 import static junit.framework.Assert.assertEquals; |
13 import static junit.framework.Assert.assertTrue; | 14 import static junit.framework.Assert.assertTrue; |
14 | 15 |
15 import org.chromium.base.Log; | 16 import org.chromium.base.Log; |
16 import org.chromium.base.PathUtils; | 17 import org.chromium.base.PathUtils; |
17 import org.chromium.base.annotations.SuppressFBWarnings; | 18 import org.chromium.base.annotations.SuppressFBWarnings; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 * Initializes Cronet Async API only. | 61 * Initializes Cronet Async API only. |
61 */ | 62 */ |
62 public static final String LIBRARY_INIT_CRONET_ONLY = "cronetOnly"; | 63 public static final String LIBRARY_INIT_CRONET_ONLY = "cronetOnly"; |
63 | 64 |
64 /** | 65 /** |
65 * Initializes Cronet HttpURLConnection Wrapper API. | 66 * Initializes Cronet HttpURLConnection Wrapper API. |
66 */ | 67 */ |
67 public static final String LIBRARY_INIT_WRAPPER = "wrapperOnly"; | 68 public static final String LIBRARY_INIT_WRAPPER = "wrapperOnly"; |
68 | 69 |
69 public CronetURLStreamHandlerFactory mStreamHandlerFactory; | 70 public CronetURLStreamHandlerFactory mStreamHandlerFactory; |
70 public UrlRequestContext mUrlRequestContext; | 71 public CronetEngine mCronetEngine; |
71 HttpUrlRequestFactory mRequestFactory; | 72 HttpUrlRequestFactory mRequestFactory; |
72 @SuppressFBWarnings("URF_UNREAD_FIELD") | 73 @SuppressFBWarnings("URF_UNREAD_FIELD") |
73 HistogramManager mHistogramManager; | 74 HistogramManager mHistogramManager; |
74 | 75 |
75 String mUrl; | 76 String mUrl; |
76 | 77 |
77 boolean mLoading = false; | 78 boolean mLoading = false; |
78 | 79 |
79 int mHttpStatusCode = 0; | 80 int mHttpStatusCode = 0; |
80 | 81 |
81 // UrlRequestContextConfig used for this activity. | 82 // CronetEngine.Builder used for this activity. |
82 private UrlRequestContextConfig mConfig; | 83 private CronetEngine.Builder mCronetEngineBuilder; |
83 | 84 |
84 class TestHttpUrlRequestListener implements HttpUrlRequestListener { | 85 class TestHttpUrlRequestListener implements HttpUrlRequestListener { |
85 public TestHttpUrlRequestListener() { | 86 public TestHttpUrlRequestListener() { |
86 } | 87 } |
87 | 88 |
88 @Override | 89 @Override |
89 public void onResponseStarted(HttpUrlRequest request) { | 90 public void onResponseStarted(HttpUrlRequest request) { |
90 mHttpStatusCode = request.getHttpStatusCode(); | 91 mHttpStatusCode = request.getHttpStatusCode(); |
91 } | 92 } |
92 | 93 |
(...skipping 16 matching lines...) Expand all Loading... |
109 String[] commandLine = extras.getStringArray(COMMAND_LINE_ARGS_KEY); | 110 String[] commandLine = extras.getStringArray(COMMAND_LINE_ARGS_KEY); |
110 if (commandLine != null) { | 111 if (commandLine != null) { |
111 assertEquals(0, commandLine.length % 2); | 112 assertEquals(0, commandLine.length % 2); |
112 for (int i = 0; i < commandLine.length / 2; i++) { | 113 for (int i = 0; i < commandLine.length / 2; i++) { |
113 Log.i(TAG, "Cronet commandLine %s = %s", commandLine[i * 2], | 114 Log.i(TAG, "Cronet commandLine %s = %s", commandLine[i * 2], |
114 commandLine[i * 2 + 1]); | 115 commandLine[i * 2 + 1]); |
115 } | 116 } |
116 } | 117 } |
117 } | 118 } |
118 | 119 |
119 // Initializes UrlRequestContextConfig from commandLine args. | 120 // Initializes CronetEngine.Builder from commandLine args. |
120 mConfig = initializeContextConfig(); | 121 mCronetEngineBuilder = initializeCronetEngineBuilder(); |
121 Log.i(TAG, "Using Config: " + mConfig.toString()); | 122 Log.i(TAG, "Using Config: " + mCronetEngineBuilder.toString()); |
122 | 123 |
123 String initString = getCommandLineArg(LIBRARY_INIT_KEY); | 124 String initString = getCommandLineArg(LIBRARY_INIT_KEY); |
124 if (LIBRARY_INIT_SKIP.equals(initString)) { | 125 if (LIBRARY_INIT_SKIP.equals(initString)) { |
125 return; | 126 return; |
126 } | 127 } |
127 | 128 |
128 if (LIBRARY_INIT_WRAPPER.equals(initString)) { | 129 if (LIBRARY_INIT_WRAPPER.equals(initString)) { |
129 mStreamHandlerFactory = | 130 mStreamHandlerFactory = new CronetURLStreamHandlerFactory(mCronetEng
ineBuilder); |
130 new CronetURLStreamHandlerFactory(this, mConfig); | |
131 } | 131 } |
132 | 132 |
133 mUrlRequestContext = initRequestContext(); | 133 mCronetEngine = initCronetEngine(); |
134 mHistogramManager = HistogramManager.createHistogramManager(); | 134 mHistogramManager = HistogramManager.createHistogramManager(); |
135 | 135 |
136 if (LIBRARY_INIT_CRONET_ONLY.equals(initString)) { | 136 if (LIBRARY_INIT_CRONET_ONLY.equals(initString)) { |
137 return; | 137 return; |
138 } | 138 } |
139 | 139 |
140 mRequestFactory = initRequestFactory(); | 140 mRequestFactory = initRequestFactory(); |
141 String appUrl = getUrlFromIntent(getIntent()); | 141 String appUrl = getUrlFromIntent(getIntent()); |
142 if (appUrl != null) { | 142 if (appUrl != null) { |
143 startWithURL(appUrl); | 143 startWithURL(appUrl); |
(...skipping 20 matching lines...) Expand all Loading... |
164 if (path.isDirectory()) { | 164 if (path.isDirectory()) { |
165 for (File c : path.listFiles()) { | 165 for (File c : path.listFiles()) { |
166 if (!recursiveDelete(c)) { | 166 if (!recursiveDelete(c)) { |
167 return false; | 167 return false; |
168 } | 168 } |
169 } | 169 } |
170 } | 170 } |
171 return path.delete(); | 171 return path.delete(); |
172 } | 172 } |
173 | 173 |
174 UrlRequestContextConfig getContextConfig() { | 174 CronetEngine.Builder getCronetEngineBuilder() { |
175 return mConfig; | 175 return mCronetEngineBuilder; |
176 } | 176 } |
177 | 177 |
178 private UrlRequestContextConfig initializeContextConfig() { | 178 private CronetEngine.Builder initializeCronetEngineBuilder() { |
179 UrlRequestContextConfig config = new UrlRequestContextConfig(); | 179 return createCronetEngineBuilder(this); |
180 config.enableHTTP2(true).enableQUIC(true); | 180 } |
| 181 |
| 182 CronetEngine.Builder createCronetEngineBuilder(Context context) { |
| 183 CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(cont
ext); |
| 184 cronetEngineBuilder.enableHTTP2(true).enableQUIC(true); |
181 | 185 |
182 // Override config if it is passed from the launcher. | 186 // Override config if it is passed from the launcher. |
183 String configString = getCommandLineArg(CONFIG_KEY); | 187 String configString = getCommandLineArg(CONFIG_KEY); |
184 if (configString != null) { | 188 if (configString != null) { |
185 try { | 189 try { |
186 config = new UrlRequestContextConfig(configString); | 190 cronetEngineBuilder = new CronetEngine.Builder(this, configStrin
g); |
187 } catch (org.json.JSONException e) { | 191 } catch (org.json.JSONException e) { |
188 Log.e(TAG, "Invalid Config.", e); | 192 Log.e(TAG, "Invalid Config.", e); |
189 finish(); | 193 finish(); |
190 return null; | 194 return null; |
191 } | 195 } |
192 } | 196 } |
193 | 197 |
194 String cacheString = getCommandLineArg(CACHE_KEY); | 198 String cacheString = getCommandLineArg(CACHE_KEY); |
195 if (CACHE_DISK.equals(cacheString)) { | 199 if (CACHE_DISK.equals(cacheString)) { |
196 config.setStoragePath(getTestStorage()); | 200 cronetEngineBuilder.setStoragePath(getTestStorage()); |
197 config.enableHttpCache(UrlRequestContextConfig.HTTP_CACHE_DISK, 1000
* 1024); | 201 cronetEngineBuilder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_
DISK, 1000 * 1024); |
198 } else if (CACHE_DISK_NO_HTTP.equals(cacheString)) { | 202 } else if (CACHE_DISK_NO_HTTP.equals(cacheString)) { |
199 config.setStoragePath(getTestStorage()); | 203 cronetEngineBuilder.setStoragePath(getTestStorage()); |
200 config.enableHttpCache(UrlRequestContextConfig.HTTP_CACHE_DISK_NO_HT
TP, 1000 * 1024); | 204 cronetEngineBuilder.enableHttpCache( |
| 205 CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP, 1000 * 1024); |
201 } else if (CACHE_IN_MEMORY.equals(cacheString)) { | 206 } else if (CACHE_IN_MEMORY.equals(cacheString)) { |
202 config.enableHttpCache(UrlRequestContextConfig.HTTP_CACHE_IN_MEMORY,
100 * 1024); | 207 cronetEngineBuilder.enableHttpCache( |
| 208 CronetEngine.Builder.HTTP_CACHE_IN_MEMORY, 100 * 1024); |
203 } | 209 } |
204 | 210 |
205 String sdchString = getCommandLineArg(SDCH_KEY); | 211 String sdchString = getCommandLineArg(SDCH_KEY); |
206 if (SDCH_ENABLE.equals(sdchString)) { | 212 if (SDCH_ENABLE.equals(sdchString)) { |
207 config.enableSDCH(true); | 213 cronetEngineBuilder.enableSDCH(true); |
208 } | 214 } |
209 | 215 |
210 // Setting this here so it isn't overridden on the command line | 216 // Setting this here so it isn't overridden on the command line |
211 config.setLibraryName("cronet_tests"); | 217 cronetEngineBuilder.setLibraryName("cronet_tests"); |
212 return config; | 218 return cronetEngineBuilder; |
213 } | 219 } |
214 | 220 |
215 // Helper function to initialize request context. Also used in testing. | 221 // Helper function to initialize Cronet engine. Also used in testing. |
216 public UrlRequestContext initRequestContext() { | 222 public CronetEngine initCronetEngine() { |
217 return UrlRequestContext.createContext(this, mConfig); | 223 return mCronetEngineBuilder.build(); |
218 } | 224 } |
219 | 225 |
220 // Helper function to initialize request factory. Also used in testing. | 226 // Helper function to initialize request factory. Also used in testing. |
221 public HttpUrlRequestFactory initRequestFactory() { | 227 public HttpUrlRequestFactory initRequestFactory() { |
222 return HttpUrlRequestFactory.createFactory(this, mConfig); | 228 return HttpUrlRequestFactory.createFactory(mCronetEngineBuilder); |
223 } | 229 } |
224 | 230 |
225 private static String getUrlFromIntent(Intent intent) { | 231 private static String getUrlFromIntent(Intent intent) { |
226 return intent != null ? intent.getDataString() : null; | 232 return intent != null ? intent.getDataString() : null; |
227 } | 233 } |
228 | 234 |
229 private String getCommandLineArg(String key) { | 235 private String getCommandLineArg(String key) { |
230 Intent intent = getIntent(); | 236 Intent intent = getIntent(); |
231 Bundle extras = intent.getExtras(); | 237 Bundle extras = intent.getExtras(); |
232 if (extras != null) { | 238 if (extras != null) { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 public int getHttpStatusCode() { | 284 public int getHttpStatusCode() { |
279 return mHttpStatusCode; | 285 return mHttpStatusCode; |
280 } | 286 } |
281 | 287 |
282 public void startNetLog() { | 288 public void startNetLog() { |
283 if (mRequestFactory != null) { | 289 if (mRequestFactory != null) { |
284 mRequestFactory.startNetLogToFile(Environment.getExternalStorageDire
ctory().getPath() | 290 mRequestFactory.startNetLogToFile(Environment.getExternalStorageDire
ctory().getPath() |
285 + "/cronet_sample_netlog_old_api.json", | 291 + "/cronet_sample_netlog_old_api.json", |
286 false); | 292 false); |
287 } | 293 } |
288 if (mUrlRequestContext != null) { | 294 if (mCronetEngine != null) { |
289 mUrlRequestContext.startNetLogToFile(Environment.getExternalStorageD
irectory().getPath() | 295 mCronetEngine.startNetLogToFile(Environment.getExternalStorageDirect
ory().getPath() |
290 + "/cronet_sample_netlog_new_api.json", | 296 + "/cronet_sample_netlog_new_api.json", |
291 false); | 297 false); |
292 } | 298 } |
293 } | 299 } |
294 | 300 |
295 public void stopNetLog() { | 301 public void stopNetLog() { |
296 if (mRequestFactory != null) { | 302 if (mRequestFactory != null) { |
297 mRequestFactory.stopNetLog(); | 303 mRequestFactory.stopNetLog(); |
298 } | 304 } |
299 if (mUrlRequestContext != null) { | 305 if (mCronetEngine != null) { |
300 mUrlRequestContext.stopNetLog(); | 306 mCronetEngine.stopNetLog(); |
301 } | 307 } |
302 } | 308 } |
303 } | 309 } |
OLD | NEW |