| 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; |
| 11 import android.util.Log; | |
| 12 | 11 |
| 12 import static junit.framework.Assert.assertEquals; |
| 13 import static junit.framework.Assert.assertTrue; | 13 import static junit.framework.Assert.assertTrue; |
| 14 | 14 |
| 15 import org.chromium.base.Log; |
| 15 import org.chromium.base.PathUtils; | 16 import org.chromium.base.PathUtils; |
| 16 import org.chromium.base.annotations.SuppressFBWarnings; | 17 import org.chromium.base.annotations.SuppressFBWarnings; |
| 17 import org.chromium.net.urlconnection.CronetURLStreamHandlerFactory; | 18 import org.chromium.net.urlconnection.CronetURLStreamHandlerFactory; |
| 18 | 19 |
| 19 import java.io.ByteArrayInputStream; | 20 import java.io.ByteArrayInputStream; |
| 20 import java.io.File; | 21 import java.io.File; |
| 21 import java.io.InputStream; | 22 import java.io.InputStream; |
| 22 | 23 |
| 23 import java.nio.channels.Channels; | 24 import java.nio.channels.Channels; |
| 24 import java.nio.channels.ReadableByteChannel; | 25 import java.nio.channels.ReadableByteChannel; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 HttpUrlRequestFactory mRequestFactory; | 66 HttpUrlRequestFactory mRequestFactory; |
| 66 @SuppressFBWarnings("URF_UNREAD_FIELD") | 67 @SuppressFBWarnings("URF_UNREAD_FIELD") |
| 67 HistogramManager mHistogramManager; | 68 HistogramManager mHistogramManager; |
| 68 | 69 |
| 69 String mUrl; | 70 String mUrl; |
| 70 | 71 |
| 71 boolean mLoading = false; | 72 boolean mLoading = false; |
| 72 | 73 |
| 73 int mHttpStatusCode = 0; | 74 int mHttpStatusCode = 0; |
| 74 | 75 |
| 76 // UrlRequestContextConfig used for this activity. |
| 77 private UrlRequestContextConfig mConfig; |
| 78 |
| 75 class TestHttpUrlRequestListener implements HttpUrlRequestListener { | 79 class TestHttpUrlRequestListener implements HttpUrlRequestListener { |
| 76 public TestHttpUrlRequestListener() { | 80 public TestHttpUrlRequestListener() { |
| 77 } | 81 } |
| 78 | 82 |
| 79 @Override | 83 @Override |
| 80 public void onResponseStarted(HttpUrlRequest request) { | 84 public void onResponseStarted(HttpUrlRequest request) { |
| 81 mHttpStatusCode = request.getHttpStatusCode(); | 85 mHttpStatusCode = request.getHttpStatusCode(); |
| 82 } | 86 } |
| 83 | 87 |
| 84 @Override | 88 @Override |
| 85 public void onRequestComplete(HttpUrlRequest request) { | 89 public void onRequestComplete(HttpUrlRequest request) { |
| 86 mLoading = false; | 90 mLoading = false; |
| 87 } | 91 } |
| 88 } | 92 } |
| 89 | 93 |
| 90 @Override | 94 @Override |
| 91 protected void onCreate(final Bundle savedInstanceState) { | 95 protected void onCreate(final Bundle savedInstanceState) { |
| 92 super.onCreate(savedInstanceState); | 96 super.onCreate(savedInstanceState); |
| 93 prepareTestStorage(); | 97 prepareTestStorage(); |
| 94 | 98 |
| 99 // Print out extra arguments passed in starting this activity. |
| 100 Intent intent = getIntent(); |
| 101 Bundle extras = intent.getExtras(); |
| 102 Log.i(TAG, "Cronet extras: " + extras); |
| 103 if (extras != null) { |
| 104 String[] commandLine = extras.getStringArray(COMMAND_LINE_ARGS_KEY); |
| 105 if (commandLine != null) { |
| 106 assertEquals(0, commandLine.length % 2); |
| 107 for (int i = 0; i < commandLine.length / 2; i++) { |
| 108 Log.i(TAG, "Cronet commandLine %s = %s", commandLine[i * 2], |
| 109 commandLine[i * 2 + 1]); |
| 110 } |
| 111 } |
| 112 } |
| 113 |
| 114 // Initializes UrlRequestContextConfig from commandLine args. |
| 115 mConfig = initializeContextConfig(); |
| 116 Log.i(TAG, "Using Config: " + mConfig.toString()); |
| 117 |
| 95 String initString = getCommandLineArg(LIBRARY_INIT_KEY); | 118 String initString = getCommandLineArg(LIBRARY_INIT_KEY); |
| 96 if (LIBRARY_INIT_SKIP.equals(initString)) { | 119 if (LIBRARY_INIT_SKIP.equals(initString)) { |
| 97 return; | 120 return; |
| 98 } | 121 } |
| 99 | 122 |
| 100 if (LIBRARY_INIT_WRAPPER.equals(initString)) { | 123 if (LIBRARY_INIT_WRAPPER.equals(initString)) { |
| 101 mStreamHandlerFactory = | 124 mStreamHandlerFactory = |
| 102 new CronetURLStreamHandlerFactory(this, getContextConfig()); | 125 new CronetURLStreamHandlerFactory(this, mConfig); |
| 103 } | 126 } |
| 104 | 127 |
| 105 mUrlRequestContext = initRequestContext(); | 128 mUrlRequestContext = initRequestContext(); |
| 106 mHistogramManager = HistogramManager.createHistogramManager(); | 129 mHistogramManager = HistogramManager.createHistogramManager(); |
| 107 | 130 |
| 108 if (LIBRARY_INIT_CRONET_ONLY.equals(initString)) { | 131 if (LIBRARY_INIT_CRONET_ONLY.equals(initString)) { |
| 109 return; | 132 return; |
| 110 } | 133 } |
| 111 | 134 |
| 112 mRequestFactory = initRequestFactory(); | 135 mRequestFactory = initRequestFactory(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 136 for (File c : path.listFiles()) { | 159 for (File c : path.listFiles()) { |
| 137 if (!recursiveDelete(c)) { | 160 if (!recursiveDelete(c)) { |
| 138 return false; | 161 return false; |
| 139 } | 162 } |
| 140 } | 163 } |
| 141 } | 164 } |
| 142 return path.delete(); | 165 return path.delete(); |
| 143 } | 166 } |
| 144 | 167 |
| 145 UrlRequestContextConfig getContextConfig() { | 168 UrlRequestContextConfig getContextConfig() { |
| 169 return mConfig; |
| 170 } |
| 171 |
| 172 private UrlRequestContextConfig initializeContextConfig() { |
| 146 UrlRequestContextConfig config = new UrlRequestContextConfig(); | 173 UrlRequestContextConfig config = new UrlRequestContextConfig(); |
| 147 | |
| 148 String cacheString = getCommandLineArg(CACHE_KEY); | |
| 149 if (CACHE_DISK.equals(cacheString)) { | |
| 150 config.setStoragePath(getTestStorage()); | |
| 151 config.enableHttpCache(UrlRequestContextConfig.HttpCache.DISK, 1000
* 1024); | |
| 152 } else if (CACHE_DISK_NO_HTTP.equals(cacheString)) { | |
| 153 config.setStoragePath(getTestStorage()); | |
| 154 config.enableHttpCache(UrlRequestContextConfig.HttpCache.DISK_NO_HTT
P, 1000 * 1024); | |
| 155 } else if (CACHE_IN_MEMORY.equals(cacheString)) { | |
| 156 config.enableHttpCache(UrlRequestContextConfig.HttpCache.IN_MEMORY,
100 * 1024); | |
| 157 } | |
| 158 config.enableSPDY(true).enableQUIC(true); | 174 config.enableSPDY(true).enableQUIC(true); |
| 159 | 175 |
| 160 // Override config if it is passed from the launcher. | 176 // Override config if it is passed from the launcher. |
| 161 String configString = getCommandLineArg(CONFIG_KEY); | 177 String configString = getCommandLineArg(CONFIG_KEY); |
| 162 if (configString != null) { | 178 if (configString != null) { |
| 163 try { | 179 try { |
| 164 Log.i(TAG, "Using Config: " + configString); | |
| 165 config = new UrlRequestContextConfig(configString); | 180 config = new UrlRequestContextConfig(configString); |
| 166 } catch (org.json.JSONException e) { | 181 } catch (org.json.JSONException e) { |
| 167 Log.e(TAG, "Invalid Config.", e); | 182 Log.e(TAG, "Invalid Config.", e); |
| 168 finish(); | 183 finish(); |
| 169 return null; | 184 return null; |
| 170 } | 185 } |
| 171 } | 186 } |
| 172 | 187 |
| 188 String cacheString = getCommandLineArg(CACHE_KEY); |
| 189 if (CACHE_DISK.equals(cacheString)) { |
| 190 config.setStoragePath(getTestStorage()); |
| 191 config.enableHttpCache(UrlRequestContextConfig.HttpCache.DISK, 1000
* 1024); |
| 192 } else if (CACHE_DISK_NO_HTTP.equals(cacheString)) { |
| 193 config.setStoragePath(getTestStorage()); |
| 194 config.enableHttpCache(UrlRequestContextConfig.HttpCache.DISK_NO_HTT
P, 1000 * 1024); |
| 195 } else if (CACHE_IN_MEMORY.equals(cacheString)) { |
| 196 config.enableHttpCache(UrlRequestContextConfig.HttpCache.IN_MEMORY,
100 * 1024); |
| 197 } |
| 198 |
| 173 // Setting this here so it isn't overridden on the command line | 199 // Setting this here so it isn't overridden on the command line |
| 174 config.setLibraryName("cronet_tests"); | 200 config.setLibraryName("cronet_tests"); |
| 175 return config; | 201 return config; |
| 176 } | 202 } |
| 177 | 203 |
| 178 // Helper function to initialize request context. Also used in testing. | 204 // Helper function to initialize request context. Also used in testing. |
| 179 public UrlRequestContext initRequestContext() { | 205 public UrlRequestContext initRequestContext() { |
| 180 return UrlRequestContext.createContext(this, getContextConfig()); | 206 return UrlRequestContext.createContext(this, mConfig); |
| 181 } | 207 } |
| 182 | 208 |
| 183 // Helper function to initialize request factory. Also used in testing. | 209 // Helper function to initialize request factory. Also used in testing. |
| 184 public HttpUrlRequestFactory initRequestFactory() { | 210 public HttpUrlRequestFactory initRequestFactory() { |
| 185 return HttpUrlRequestFactory.createFactory(this, getContextConfig()); | 211 return HttpUrlRequestFactory.createFactory(this, mConfig); |
| 186 } | 212 } |
| 187 | 213 |
| 188 private static String getUrlFromIntent(Intent intent) { | 214 private static String getUrlFromIntent(Intent intent) { |
| 189 return intent != null ? intent.getDataString() : null; | 215 return intent != null ? intent.getDataString() : null; |
| 190 } | 216 } |
| 191 | 217 |
| 192 private String getCommandLineArg(String key) { | 218 private String getCommandLineArg(String key) { |
| 193 Intent intent = getIntent(); | 219 Intent intent = getIntent(); |
| 194 Bundle extras = intent.getExtras(); | 220 Bundle extras = intent.getExtras(); |
| 195 Log.i(TAG, "Cronet extras: " + extras); | |
| 196 if (extras != null) { | 221 if (extras != null) { |
| 197 String[] commandLine = extras.getStringArray(COMMAND_LINE_ARGS_KEY); | 222 String[] commandLine = extras.getStringArray(COMMAND_LINE_ARGS_KEY); |
| 198 if (commandLine != null) { | 223 if (commandLine != null) { |
| 199 for (int i = 0; i < commandLine.length; ++i) { | 224 for (int i = 0; i < commandLine.length; ++i) { |
| 200 Log.i(TAG, | |
| 201 "Cronet commandLine[" + i + "]=" + commandLine[i]); | |
| 202 if (commandLine[i].equals(key)) { | 225 if (commandLine[i].equals(key)) { |
| 203 return commandLine[++i]; | 226 return commandLine[++i]; |
| 204 } | 227 } |
| 205 } | 228 } |
| 206 } | 229 } |
| 207 } | 230 } |
| 208 return null; | 231 return null; |
| 209 } | 232 } |
| 210 | 233 |
| 211 private void applyCommandLineToHttpUrlRequest(HttpUrlRequest request) { | 234 private void applyCommandLineToHttpUrlRequest(HttpUrlRequest request) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 | 283 |
| 261 public void stopNetLog() { | 284 public void stopNetLog() { |
| 262 if (mRequestFactory != null) { | 285 if (mRequestFactory != null) { |
| 263 mRequestFactory.stopNetLog(); | 286 mRequestFactory.stopNetLog(); |
| 264 } | 287 } |
| 265 if (mUrlRequestContext != null) { | 288 if (mUrlRequestContext != null) { |
| 266 mUrlRequestContext.stopNetLog(); | 289 mUrlRequestContext.stopNetLog(); |
| 267 } | 290 } |
| 268 } | 291 } |
| 269 } | 292 } |
| OLD | NEW |