Chromium Code Reviews| 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; | |
| 8 import android.content.Context; | 7 import android.content.Context; |
| 9 import android.content.Intent; | 8 import android.os.ConditionVariable; |
| 10 import android.os.Bundle; | |
| 11 import android.os.Environment; | 9 import android.os.Environment; |
| 12 | 10 |
| 13 import static junit.framework.Assert.assertEquals; | 11 import static junit.framework.Assert.assertEquals; |
| 14 import static junit.framework.Assert.assertTrue; | 12 import static junit.framework.Assert.assertTrue; |
| 13 import static junit.framework.Assert.fail; | |
| 15 | 14 |
| 16 import org.chromium.base.Log; | 15 import org.chromium.base.Log; |
| 17 import org.chromium.base.PathUtils; | 16 import org.chromium.base.PathUtils; |
| 18 import org.chromium.base.annotations.SuppressFBWarnings; | 17 import org.chromium.base.annotations.SuppressFBWarnings; |
| 19 import org.chromium.net.urlconnection.CronetURLStreamHandlerFactory; | 18 import org.chromium.net.urlconnection.CronetURLStreamHandlerFactory; |
| 20 | 19 |
| 21 import java.io.ByteArrayInputStream; | 20 import java.io.ByteArrayInputStream; |
| 22 import java.io.File; | 21 import java.io.File; |
| 23 import java.io.InputStream; | 22 import java.io.InputStream; |
| 24 | 23 |
| 25 import java.nio.channels.Channels; | 24 import java.nio.channels.Channels; |
| 26 import java.nio.channels.ReadableByteChannel; | 25 import java.nio.channels.ReadableByteChannel; |
| 27 import java.util.HashMap; | 26 import java.util.HashMap; |
| 28 | 27 |
| 29 /** | 28 /** |
| 30 * Activity for managing the Cronet Test. | 29 * Activity for managing the Cronet Test. |
|
xunjieli
2015/10/22 18:41:24
nit: Outdated comment containing the word "activit
pauljensen
2015/10/23 11:35:50
Done.
| |
| 31 */ | 30 */ |
| 32 @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") | 31 @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") |
| 33 public class CronetTestActivity extends Activity { | 32 public class CronetTestFramework { |
| 34 private static final String TAG = "CronetTestActivity"; | 33 private static final String TAG = "CronetTestFramework"; |
| 35 | 34 |
| 36 public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs"; | 35 public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs"; |
| 37 public static final String POST_DATA_KEY = "postData"; | 36 public static final String POST_DATA_KEY = "postData"; |
| 38 public static final String CONFIG_KEY = "config"; | 37 public static final String CONFIG_KEY = "config"; |
| 39 public static final String CACHE_KEY = "cache"; | 38 public static final String CACHE_KEY = "cache"; |
| 40 public static final String SDCH_KEY = "sdch"; | 39 public static final String SDCH_KEY = "sdch"; |
| 41 | 40 |
| 42 public static final String LIBRARY_INIT_KEY = "libraryInit"; | 41 public static final String LIBRARY_INIT_KEY = "libraryInit"; |
| 43 /** | 42 /** |
| 44 * Skips library initialization. | 43 * Skips library initialization. |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 60 /** | 59 /** |
| 61 * Initializes Cronet Async API only. | 60 * Initializes Cronet Async API only. |
| 62 */ | 61 */ |
| 63 public static final String LIBRARY_INIT_CRONET_ONLY = "cronetOnly"; | 62 public static final String LIBRARY_INIT_CRONET_ONLY = "cronetOnly"; |
| 64 | 63 |
| 65 /** | 64 /** |
| 66 * Initializes Cronet HttpURLConnection Wrapper API. | 65 * Initializes Cronet HttpURLConnection Wrapper API. |
| 67 */ | 66 */ |
| 68 public static final String LIBRARY_INIT_WRAPPER = "wrapperOnly"; | 67 public static final String LIBRARY_INIT_WRAPPER = "wrapperOnly"; |
| 69 | 68 |
| 69 private final String[] mCommandLine; | |
| 70 private final Context mContext; | |
|
xunjieli
2015/10/22 18:41:24
nit: Private fields should go after public ones.
pauljensen
2015/10/23 11:35:50
Done.
| |
| 71 | |
| 70 public CronetURLStreamHandlerFactory mStreamHandlerFactory; | 72 public CronetURLStreamHandlerFactory mStreamHandlerFactory; |
| 71 public CronetEngine mCronetEngine; | 73 public CronetEngine mCronetEngine; |
| 72 HttpUrlRequestFactory mRequestFactory; | 74 HttpUrlRequestFactory mRequestFactory; |
| 73 @SuppressFBWarnings("URF_UNREAD_FIELD") | 75 @SuppressFBWarnings("URF_UNREAD_FIELD") HistogramManager mHistogramManager; |
| 74 HistogramManager mHistogramManager; | |
| 75 | 76 |
| 76 String mUrl; | 77 private String mUrl; |
| 77 | 78 private boolean mLoading = false; |
| 78 boolean mLoading = false; | 79 private int mHttpStatusCode = 0; |
| 79 | |
| 80 int mHttpStatusCode = 0; | |
| 81 | 80 |
| 82 // CronetEngine.Builder used for this activity. | 81 // CronetEngine.Builder used for this activity. |
| 83 private CronetEngine.Builder mCronetEngineBuilder; | 82 private CronetEngine.Builder mCronetEngineBuilder; |
| 84 | 83 |
| 85 class TestHttpUrlRequestListener implements HttpUrlRequestListener { | 84 private class TestHttpUrlRequestListener implements HttpUrlRequestListener { |
| 86 public TestHttpUrlRequestListener() { | 85 private final ConditionVariable mComplete = new ConditionVariable(); |
| 87 } | 86 |
| 87 public TestHttpUrlRequestListener() {} | |
| 88 | 88 |
| 89 @Override | 89 @Override |
| 90 public void onResponseStarted(HttpUrlRequest request) { | 90 public void onResponseStarted(HttpUrlRequest request) { |
| 91 mHttpStatusCode = request.getHttpStatusCode(); | 91 mHttpStatusCode = request.getHttpStatusCode(); |
| 92 } | 92 } |
| 93 | 93 |
| 94 @Override | 94 @Override |
| 95 public void onRequestComplete(HttpUrlRequest request) { | 95 public void onRequestComplete(HttpUrlRequest request) { |
| 96 mLoading = false; | 96 mLoading = false; |
| 97 mComplete.open(); | |
| 98 } | |
| 99 | |
| 100 public void blockForComplete() { | |
| 101 mComplete.block(); | |
| 97 } | 102 } |
| 98 } | 103 } |
| 99 | 104 |
| 100 @Override | 105 public CronetTestFramework(String appUrl, String[] commandLine, Context cont ext) { |
| 101 protected void onCreate(final Bundle savedInstanceState) { | 106 mCommandLine = commandLine; |
| 102 super.onCreate(savedInstanceState); | 107 mContext = context; |
| 103 prepareTestStorage(); | 108 prepareTestStorage(); |
| 104 | 109 |
| 105 // Print out extra arguments passed in starting this activity. | 110 // Print out extra arguments passed in starting this activity. |
| 106 Intent intent = getIntent(); | 111 if (commandLine != null) { |
| 107 Bundle extras = intent.getExtras(); | 112 assertEquals(0, commandLine.length % 2); |
| 108 Log.i(TAG, "Cronet extras: " + extras); | 113 for (int i = 0; i < commandLine.length / 2; i++) { |
| 109 if (extras != null) { | 114 Log.i(TAG, "Cronet commandLine %s = %s", commandLine[i * 2], |
| 110 String[] commandLine = extras.getStringArray(COMMAND_LINE_ARGS_KEY); | 115 commandLine[i * 2 + 1]); |
| 111 if (commandLine != null) { | |
| 112 assertEquals(0, commandLine.length % 2); | |
| 113 for (int i = 0; i < commandLine.length / 2; i++) { | |
| 114 Log.i(TAG, "Cronet commandLine %s = %s", commandLine[i * 2], | |
| 115 commandLine[i * 2 + 1]); | |
| 116 } | |
| 117 } | 116 } |
| 118 } | 117 } |
| 119 | 118 |
| 120 // Initializes CronetEngine.Builder from commandLine args. | 119 // Initializes CronetEngine.Builder from commandLine args. |
| 121 mCronetEngineBuilder = initializeCronetEngineBuilder(); | 120 mCronetEngineBuilder = initializeCronetEngineBuilder(); |
| 122 Log.i(TAG, "Using Config: " + mCronetEngineBuilder.toString()); | 121 Log.i(TAG, "Using Config: " + mCronetEngineBuilder.toString()); |
| 123 | 122 |
| 124 String initString = getCommandLineArg(LIBRARY_INIT_KEY); | 123 String initString = getCommandLineArg(LIBRARY_INIT_KEY); |
| 125 if (LIBRARY_INIT_SKIP.equals(initString)) { | 124 if (LIBRARY_INIT_SKIP.equals(initString)) { |
| 126 return; | 125 return; |
| 127 } | 126 } |
| 128 | 127 |
| 129 mCronetEngine = initCronetEngine(); | 128 mCronetEngine = initCronetEngine(); |
| 130 | 129 |
| 131 if (LIBRARY_INIT_WRAPPER.equals(initString)) { | 130 if (LIBRARY_INIT_WRAPPER.equals(initString)) { |
| 132 mStreamHandlerFactory = new CronetURLStreamHandlerFactory(mCronetEng ine); | 131 mStreamHandlerFactory = new CronetURLStreamHandlerFactory(mCronetEng ine); |
| 133 } | 132 } |
| 134 | 133 |
| 135 mHistogramManager = HistogramManager.createHistogramManager(); | 134 mHistogramManager = HistogramManager.createHistogramManager(); |
| 136 | 135 |
| 137 if (LIBRARY_INIT_CRONET_ONLY.equals(initString)) { | 136 if (LIBRARY_INIT_CRONET_ONLY.equals(initString)) { |
| 138 return; | 137 return; |
| 139 } | 138 } |
| 140 | 139 |
| 141 mRequestFactory = initRequestFactory(); | 140 mRequestFactory = initRequestFactory(); |
| 142 String appUrl = getUrlFromIntent(getIntent()); | |
| 143 if (appUrl != null) { | 141 if (appUrl != null) { |
| 144 startWithURL(appUrl); | 142 startWithURL(appUrl); |
| 145 } | 143 } |
| 146 } | 144 } |
| 147 | 145 |
| 148 /** | 146 /** |
| 149 * Prepares the path for the test storage (http cache, QUIC server info). | 147 * Prepares the path for the test storage (http cache, QUIC server info). |
| 150 */ | 148 */ |
| 151 private void prepareTestStorage() { | 149 private void prepareTestStorage() { |
| 152 File storage = new File(getTestStorage()); | 150 File storage = new File(getTestStorage()); |
| 153 if (storage.exists()) { | 151 if (storage.exists()) { |
| 154 assertTrue(recursiveDelete(storage)); | 152 assertTrue(recursiveDelete(storage)); |
| 155 } | 153 } |
| 156 assertTrue(storage.mkdir()); | 154 assertTrue(storage.mkdir()); |
| 157 } | 155 } |
| 158 | 156 |
| 159 String getTestStorage() { | 157 String getTestStorage() { |
| 160 return PathUtils.getDataDirectory(getApplicationContext()) + "/test_stor age"; | 158 return PathUtils.getDataDirectory(mContext) + "/test_storage"; |
| 161 } | 159 } |
| 162 | 160 |
| 163 @SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE") | 161 @SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE") |
| 164 private boolean recursiveDelete(File path) { | 162 private boolean recursiveDelete(File path) { |
| 165 if (path.isDirectory()) { | 163 if (path.isDirectory()) { |
| 166 for (File c : path.listFiles()) { | 164 for (File c : path.listFiles()) { |
| 167 if (!recursiveDelete(c)) { | 165 if (!recursiveDelete(c)) { |
| 168 return false; | 166 return false; |
| 169 } | 167 } |
| 170 } | 168 } |
| 171 } | 169 } |
| 172 return path.delete(); | 170 return path.delete(); |
| 173 } | 171 } |
| 174 | 172 |
| 175 CronetEngine.Builder getCronetEngineBuilder() { | 173 CronetEngine.Builder getCronetEngineBuilder() { |
| 176 return mCronetEngineBuilder; | 174 return mCronetEngineBuilder; |
| 177 } | 175 } |
| 178 | 176 |
| 179 private CronetEngine.Builder initializeCronetEngineBuilder() { | 177 private CronetEngine.Builder initializeCronetEngineBuilder() { |
| 180 return createCronetEngineBuilder(this); | 178 return createCronetEngineBuilder(mContext); |
| 181 } | 179 } |
| 182 | 180 |
| 183 CronetEngine.Builder createCronetEngineBuilder(Context context) { | 181 CronetEngine.Builder createCronetEngineBuilder(Context context) { |
| 184 CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(cont ext); | 182 CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(cont ext); |
| 185 cronetEngineBuilder.enableHTTP2(true).enableQUIC(true); | 183 cronetEngineBuilder.enableHTTP2(true).enableQUIC(true); |
| 186 | 184 |
| 187 // Override config if it is passed from the launcher. | 185 // Override config if it is passed from the launcher. |
| 188 String configString = getCommandLineArg(CONFIG_KEY); | 186 String configString = getCommandLineArg(CONFIG_KEY); |
| 189 if (configString != null) { | 187 if (configString != null) { |
| 190 try { | 188 try { |
| 191 cronetEngineBuilder = new CronetEngine.Builder(this, configStrin g); | 189 cronetEngineBuilder = new CronetEngine.Builder(mContext, configS tring); |
| 192 } catch (org.json.JSONException e) { | 190 } catch (org.json.JSONException e) { |
| 193 Log.e(TAG, "Invalid Config.", e); | 191 fail("Invalid Config." + e); |
| 194 finish(); | |
| 195 return null; | 192 return null; |
| 196 } | 193 } |
| 197 } | 194 } |
| 198 | 195 |
| 199 String cacheString = getCommandLineArg(CACHE_KEY); | 196 String cacheString = getCommandLineArg(CACHE_KEY); |
| 200 if (CACHE_DISK.equals(cacheString)) { | 197 if (CACHE_DISK.equals(cacheString)) { |
| 201 cronetEngineBuilder.setStoragePath(getTestStorage()); | 198 cronetEngineBuilder.setStoragePath(getTestStorage()); |
| 202 cronetEngineBuilder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_ DISK, 1000 * 1024); | 199 cronetEngineBuilder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_ DISK, 1000 * 1024); |
| 203 } else if (CACHE_DISK_NO_HTTP.equals(cacheString)) { | 200 } else if (CACHE_DISK_NO_HTTP.equals(cacheString)) { |
| 204 cronetEngineBuilder.setStoragePath(getTestStorage()); | 201 cronetEngineBuilder.setStoragePath(getTestStorage()); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 219 return cronetEngineBuilder; | 216 return cronetEngineBuilder; |
| 220 } | 217 } |
| 221 | 218 |
| 222 // Helper function to initialize Cronet engine. Also used in testing. | 219 // Helper function to initialize Cronet engine. Also used in testing. |
| 223 public CronetEngine initCronetEngine() { | 220 public CronetEngine initCronetEngine() { |
| 224 return mCronetEngineBuilder.build(); | 221 return mCronetEngineBuilder.build(); |
| 225 } | 222 } |
| 226 | 223 |
| 227 // Helper function to initialize request factory. Also used in testing. | 224 // Helper function to initialize request factory. Also used in testing. |
| 228 public HttpUrlRequestFactory initRequestFactory() { | 225 public HttpUrlRequestFactory initRequestFactory() { |
| 229 return HttpUrlRequestFactory.createFactory(this, mCronetEngineBuilder); | 226 return HttpUrlRequestFactory.createFactory(mContext, mCronetEngineBuilde r); |
| 230 } | |
| 231 | |
| 232 private static String getUrlFromIntent(Intent intent) { | |
| 233 return intent != null ? intent.getDataString() : null; | |
| 234 } | 227 } |
| 235 | 228 |
| 236 private String getCommandLineArg(String key) { | 229 private String getCommandLineArg(String key) { |
| 237 Intent intent = getIntent(); | 230 if (mCommandLine != null) { |
| 238 Bundle extras = intent.getExtras(); | 231 for (int i = 0; i < mCommandLine.length; ++i) { |
| 239 if (extras != null) { | 232 if (mCommandLine[i].equals(key)) { |
| 240 String[] commandLine = extras.getStringArray(COMMAND_LINE_ARGS_KEY); | 233 return mCommandLine[++i]; |
| 241 if (commandLine != null) { | |
| 242 for (int i = 0; i < commandLine.length; ++i) { | |
| 243 if (commandLine[i].equals(key)) { | |
| 244 return commandLine[++i]; | |
| 245 } | |
| 246 } | 234 } |
| 247 } | 235 } |
| 248 } | 236 } |
| 249 return null; | 237 return null; |
| 250 } | 238 } |
| 251 | 239 |
| 252 private void applyCommandLineToHttpUrlRequest(HttpUrlRequest request) { | 240 private void applyCommandLineToHttpUrlRequest(HttpUrlRequest request) { |
| 253 String postData = getCommandLineArg(POST_DATA_KEY); | 241 String postData = getCommandLineArg(POST_DATA_KEY); |
| 254 if (postData != null) { | 242 if (postData != null) { |
| 255 InputStream dataStream = new ByteArrayInputStream( | 243 InputStream dataStream = new ByteArrayInputStream(postData.getBytes( )); |
| 256 postData.getBytes()); | |
| 257 ReadableByteChannel dataChannel = Channels.newChannel(dataStream); | 244 ReadableByteChannel dataChannel = Channels.newChannel(dataStream); |
| 258 request.setUploadChannel("text/plain", dataChannel, | 245 request.setUploadChannel("text/plain", dataChannel, postData.length( )); |
| 259 postData.length()); | |
| 260 request.setHttpMethod("POST"); | 246 request.setHttpMethod("POST"); |
| 261 } | 247 } |
| 262 } | 248 } |
| 263 | 249 |
| 264 public void startWithURL(String url) { | 250 public void startWithURL(String url) { |
| 265 Log.i(TAG, "Cronet started: " + url); | 251 Log.i(TAG, "Cronet started: " + url); |
| 266 mUrl = url; | 252 mUrl = url; |
| 267 mLoading = true; | 253 mLoading = true; |
| 268 | 254 |
| 269 HashMap<String, String> headers = new HashMap<String, String>(); | 255 HashMap<String, String> headers = new HashMap<String, String>(); |
| 270 HttpUrlRequestListener listener = new TestHttpUrlRequestListener(); | 256 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); |
| 271 HttpUrlRequest request = mRequestFactory.createRequest( | 257 HttpUrlRequest request = mRequestFactory.createRequest( |
| 272 url, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); | 258 url, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); |
| 273 applyCommandLineToHttpUrlRequest(request); | 259 applyCommandLineToHttpUrlRequest(request); |
| 274 request.start(); | 260 request.start(); |
| 261 listener.blockForComplete(); | |
| 275 } | 262 } |
| 276 | 263 |
| 277 public String getUrl() { | 264 public String getUrl() { |
| 278 return mUrl; | 265 return mUrl; |
| 279 } | 266 } |
| 280 | 267 |
| 281 public boolean isLoading() { | 268 public boolean isLoading() { |
| 282 return mLoading; | 269 return mLoading; |
| 283 } | 270 } |
| 284 | 271 |
| 285 public int getHttpStatusCode() { | 272 public int getHttpStatusCode() { |
| 286 return mHttpStatusCode; | 273 return mHttpStatusCode; |
| 287 } | 274 } |
| 288 | 275 |
| 289 public void startNetLog() { | 276 public void startNetLog() { |
| 290 if (mRequestFactory != null) { | 277 if (mRequestFactory != null) { |
| 291 mRequestFactory.startNetLogToFile(Environment.getExternalStorageDire ctory().getPath() | 278 mRequestFactory.startNetLogToFile(Environment.getExternalStorageDire ctory().getPath() |
| 292 + "/cronet_sample_netlog_old_api.json", | 279 + "/cronet_sample_netlog_old_api.json", |
| 293 false); | 280 false); |
| 294 } | 281 } |
| 295 if (mCronetEngine != null) { | 282 if (mCronetEngine != null) { |
| 296 mCronetEngine.startNetLogToFile(Environment.getExternalStorageDirect ory().getPath() | 283 mCronetEngine.startNetLogToFile(Environment.getExternalStorageDirect ory().getPath() |
| 297 + "/cronet_sample_netlog_new_api.json", | 284 + "/cronet_sample_netlog_new_api.json", |
| 298 false); | 285 false); |
| 299 } | 286 } |
| 300 } | 287 } |
| 301 | 288 |
| 302 public void stopNetLog() { | 289 public void stopNetLog() { |
| 303 if (mRequestFactory != null) { | 290 if (mRequestFactory != null) { |
| 304 mRequestFactory.stopNetLog(); | 291 mRequestFactory.stopNetLog(); |
| 305 } | 292 } |
| 306 if (mCronetEngine != null) { | 293 if (mCronetEngine != null) { |
| 307 mCronetEngine.stopNetLog(); | 294 mCronetEngine.stopNetLog(); |
| 308 } | 295 } |
| 309 } | 296 } |
| 310 } | 297 } |
| OLD | NEW |