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