| 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 |
| 129 mCronetEngine = initCronetEngine(); |
| 130 |
| 128 if (LIBRARY_INIT_WRAPPER.equals(initString)) { | 131 if (LIBRARY_INIT_WRAPPER.equals(initString)) { |
| 129 mStreamHandlerFactory = | 132 mStreamHandlerFactory = new CronetURLStreamHandlerFactory(mCronetEng
ine); |
| 130 new CronetURLStreamHandlerFactory(this, mConfig); | |
| 131 } | 133 } |
| 132 | 134 |
| 133 mUrlRequestContext = initRequestContext(); | |
| 134 mHistogramManager = HistogramManager.createHistogramManager(); | 135 mHistogramManager = HistogramManager.createHistogramManager(); |
| 135 | 136 |
| 136 if (LIBRARY_INIT_CRONET_ONLY.equals(initString)) { | 137 if (LIBRARY_INIT_CRONET_ONLY.equals(initString)) { |
| 137 return; | 138 return; |
| 138 } | 139 } |
| 139 | 140 |
| 140 mRequestFactory = initRequestFactory(); | 141 mRequestFactory = initRequestFactory(); |
| 141 String appUrl = getUrlFromIntent(getIntent()); | 142 String appUrl = getUrlFromIntent(getIntent()); |
| 142 if (appUrl != null) { | 143 if (appUrl != null) { |
| 143 startWithURL(appUrl); | 144 startWithURL(appUrl); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 164 if (path.isDirectory()) { | 165 if (path.isDirectory()) { |
| 165 for (File c : path.listFiles()) { | 166 for (File c : path.listFiles()) { |
| 166 if (!recursiveDelete(c)) { | 167 if (!recursiveDelete(c)) { |
| 167 return false; | 168 return false; |
| 168 } | 169 } |
| 169 } | 170 } |
| 170 } | 171 } |
| 171 return path.delete(); | 172 return path.delete(); |
| 172 } | 173 } |
| 173 | 174 |
| 174 UrlRequestContextConfig getContextConfig() { | 175 CronetEngine.Builder getCronetEngineBuilder() { |
| 175 return mConfig; | 176 return mCronetEngineBuilder; |
| 176 } | 177 } |
| 177 | 178 |
| 178 private UrlRequestContextConfig initializeContextConfig() { | 179 private CronetEngine.Builder initializeCronetEngineBuilder() { |
| 179 UrlRequestContextConfig config = new UrlRequestContextConfig(); | 180 return createCronetEngineBuilder(this); |
| 180 config.enableHTTP2(true).enableQUIC(true); | 181 } |
| 182 |
| 183 CronetEngine.Builder createCronetEngineBuilder(Context context) { |
| 184 CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(cont
ext); |
| 185 cronetEngineBuilder.enableHTTP2(true).enableQUIC(true); |
| 181 | 186 |
| 182 // Override config if it is passed from the launcher. | 187 // Override config if it is passed from the launcher. |
| 183 String configString = getCommandLineArg(CONFIG_KEY); | 188 String configString = getCommandLineArg(CONFIG_KEY); |
| 184 if (configString != null) { | 189 if (configString != null) { |
| 185 try { | 190 try { |
| 186 config = new UrlRequestContextConfig(configString); | 191 cronetEngineBuilder = new CronetEngine.Builder(this, configStrin
g); |
| 187 } catch (org.json.JSONException e) { | 192 } catch (org.json.JSONException e) { |
| 188 Log.e(TAG, "Invalid Config.", e); | 193 Log.e(TAG, "Invalid Config.", e); |
| 189 finish(); | 194 finish(); |
| 190 return null; | 195 return null; |
| 191 } | 196 } |
| 192 } | 197 } |
| 193 | 198 |
| 194 String cacheString = getCommandLineArg(CACHE_KEY); | 199 String cacheString = getCommandLineArg(CACHE_KEY); |
| 195 if (CACHE_DISK.equals(cacheString)) { | 200 if (CACHE_DISK.equals(cacheString)) { |
| 196 config.setStoragePath(getTestStorage()); | 201 cronetEngineBuilder.setStoragePath(getTestStorage()); |
| 197 config.enableHttpCache(UrlRequestContextConfig.HTTP_CACHE_DISK, 1000
* 1024); | 202 cronetEngineBuilder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_
DISK, 1000 * 1024); |
| 198 } else if (CACHE_DISK_NO_HTTP.equals(cacheString)) { | 203 } else if (CACHE_DISK_NO_HTTP.equals(cacheString)) { |
| 199 config.setStoragePath(getTestStorage()); | 204 cronetEngineBuilder.setStoragePath(getTestStorage()); |
| 200 config.enableHttpCache(UrlRequestContextConfig.HTTP_CACHE_DISK_NO_HT
TP, 1000 * 1024); | 205 cronetEngineBuilder.enableHttpCache( |
| 206 CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP, 1000 * 1024); |
| 201 } else if (CACHE_IN_MEMORY.equals(cacheString)) { | 207 } else if (CACHE_IN_MEMORY.equals(cacheString)) { |
| 202 config.enableHttpCache(UrlRequestContextConfig.HTTP_CACHE_IN_MEMORY,
100 * 1024); | 208 cronetEngineBuilder.enableHttpCache( |
| 209 CronetEngine.Builder.HTTP_CACHE_IN_MEMORY, 100 * 1024); |
| 203 } | 210 } |
| 204 | 211 |
| 205 String sdchString = getCommandLineArg(SDCH_KEY); | 212 String sdchString = getCommandLineArg(SDCH_KEY); |
| 206 if (SDCH_ENABLE.equals(sdchString)) { | 213 if (SDCH_ENABLE.equals(sdchString)) { |
| 207 config.enableSDCH(true); | 214 cronetEngineBuilder.enableSDCH(true); |
| 208 } | 215 } |
| 209 | 216 |
| 210 // Setting this here so it isn't overridden on the command line | 217 // Setting this here so it isn't overridden on the command line |
| 211 config.setLibraryName("cronet_tests"); | 218 cronetEngineBuilder.setLibraryName("cronet_tests"); |
| 212 return config; | 219 return cronetEngineBuilder; |
| 213 } | 220 } |
| 214 | 221 |
| 215 // Helper function to initialize request context. Also used in testing. | 222 // Helper function to initialize Cronet engine. Also used in testing. |
| 216 public UrlRequestContext initRequestContext() { | 223 public CronetEngine initCronetEngine() { |
| 217 return UrlRequestContext.createContext(this, mConfig); | 224 return mCronetEngineBuilder.build(); |
| 218 } | 225 } |
| 219 | 226 |
| 220 // Helper function to initialize request factory. Also used in testing. | 227 // Helper function to initialize request factory. Also used in testing. |
| 221 public HttpUrlRequestFactory initRequestFactory() { | 228 public HttpUrlRequestFactory initRequestFactory() { |
| 222 return HttpUrlRequestFactory.createFactory(this, mConfig); | 229 return HttpUrlRequestFactory.createFactory(this, mCronetEngineBuilder); |
| 223 } | 230 } |
| 224 | 231 |
| 225 private static String getUrlFromIntent(Intent intent) { | 232 private static String getUrlFromIntent(Intent intent) { |
| 226 return intent != null ? intent.getDataString() : null; | 233 return intent != null ? intent.getDataString() : null; |
| 227 } | 234 } |
| 228 | 235 |
| 229 private String getCommandLineArg(String key) { | 236 private String getCommandLineArg(String key) { |
| 230 Intent intent = getIntent(); | 237 Intent intent = getIntent(); |
| 231 Bundle extras = intent.getExtras(); | 238 Bundle extras = intent.getExtras(); |
| 232 if (extras != null) { | 239 if (extras != null) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 public int getHttpStatusCode() { | 285 public int getHttpStatusCode() { |
| 279 return mHttpStatusCode; | 286 return mHttpStatusCode; |
| 280 } | 287 } |
| 281 | 288 |
| 282 public void startNetLog() { | 289 public void startNetLog() { |
| 283 if (mRequestFactory != null) { | 290 if (mRequestFactory != null) { |
| 284 mRequestFactory.startNetLogToFile(Environment.getExternalStorageDire
ctory().getPath() | 291 mRequestFactory.startNetLogToFile(Environment.getExternalStorageDire
ctory().getPath() |
| 285 + "/cronet_sample_netlog_old_api.json", | 292 + "/cronet_sample_netlog_old_api.json", |
| 286 false); | 293 false); |
| 287 } | 294 } |
| 288 if (mUrlRequestContext != null) { | 295 if (mCronetEngine != null) { |
| 289 mUrlRequestContext.startNetLogToFile(Environment.getExternalStorageD
irectory().getPath() | 296 mCronetEngine.startNetLogToFile(Environment.getExternalStorageDirect
ory().getPath() |
| 290 + "/cronet_sample_netlog_new_api.json", | 297 + "/cronet_sample_netlog_new_api.json", |
| 291 false); | 298 false); |
| 292 } | 299 } |
| 293 } | 300 } |
| 294 | 301 |
| 295 public void stopNetLog() { | 302 public void stopNetLog() { |
| 296 if (mRequestFactory != null) { | 303 if (mRequestFactory != null) { |
| 297 mRequestFactory.stopNetLog(); | 304 mRequestFactory.stopNetLog(); |
| 298 } | 305 } |
| 299 if (mUrlRequestContext != null) { | 306 if (mCronetEngine != null) { |
| 300 mUrlRequestContext.stopNetLog(); | 307 mCronetEngine.stopNetLog(); |
| 301 } | 308 } |
| 302 } | 309 } |
| 303 } | 310 } |
| OLD | NEW |