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.content.Context; | 7 import android.content.Context; |
| 8 import android.os.Build; | 8 import android.os.Build; |
| 9 import android.os.Handler; | |
| 10 import android.os.Looper; | |
| 9 import android.os.Process; | 11 import android.os.Process; |
| 10 import android.util.Log; | 12 import android.util.Log; |
| 11 | 13 |
| 12 import org.chromium.base.CalledByNative; | 14 import org.chromium.base.CalledByNative; |
| 13 import org.chromium.base.JNINamespace; | 15 import org.chromium.base.JNINamespace; |
| 14 | 16 |
| 15 import java.util.concurrent.Executor; | 17 import java.util.concurrent.Executor; |
| 16 import java.util.concurrent.atomic.AtomicInteger; | 18 import java.util.concurrent.atomic.AtomicInteger; |
| 17 | 19 |
| 18 /** | 20 /** |
| 19 * UrlRequest context using Chromium HTTP stack implementation. | 21 * UrlRequest context using Chromium HTTP stack implementation. |
| 20 */ | 22 */ |
| 21 @JNINamespace("cronet") | 23 @JNINamespace("cronet") |
| 22 public class CronetUrlRequestContext extends UrlRequestContext { | 24 public class CronetUrlRequestContext extends UrlRequestContext { |
| 23 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. | 25 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. |
| 24 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) | 26 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) |
| 25 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) | 27 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) |
| 26 static final String LOG_TAG = "ChromiumNetwork"; | 28 static final String LOG_TAG = "ChromiumNetwork"; |
| 27 | 29 |
| 30 /** | |
| 31 * Synchronize access to mUrlRequestContextAdapter and shutdown routine. | |
| 32 */ | |
| 33 private final Object mLock = new Object(); | |
| 34 | |
| 28 private long mUrlRequestContextAdapter = 0; | 35 private long mUrlRequestContextAdapter = 0; |
| 29 private Thread mNetworkThread; | 36 private Thread mNetworkThread; |
| 30 private AtomicInteger mActiveRequestCount = new AtomicInteger(0); | 37 private AtomicInteger mActiveRequestCount = new AtomicInteger(0); |
| 31 | 38 |
| 32 public CronetUrlRequestContext(Context context, | 39 public CronetUrlRequestContext(Context context, |
| 33 UrlRequestContextConfig config) { | 40 UrlRequestContextConfig config) { |
| 41 CronetLibraryLoader.ensureInitialized(context, config); | |
| 34 nativeSetMinLogLevel(getLoggingLevel()); | 42 nativeSetMinLogLevel(getLoggingLevel()); |
| 43 final String configString = config.toString(); | |
|
xunjieli
2015/01/12 15:12:34
nit: could you make config.toString() inline?
mef
2015/01/12 17:19:56
Done.
| |
| 35 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter( | 44 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter( |
| 36 context, config.toString()); | 45 context, configString); |
| 37 if (mUrlRequestContextAdapter == 0) { | 46 if (mUrlRequestContextAdapter == 0) { |
| 38 throw new NullPointerException("Context Adapter creation failed"); | 47 throw new NullPointerException("Context Adapter creation failed"); |
| 39 } | 48 } |
| 49 // Post a task to UI thread to init native Chromium URLRequestContext. | |
| 50 // TODO(xunjieli): This constructor is not supposed to be invoked on | |
| 51 // the main thread. Consider making the following code into a blocking | |
| 52 // API to handle the case where we are already on main thread. | |
| 53 Runnable task = new Runnable() { | |
| 54 public void run() { | |
| 55 // mUrlRequestContextAdapter is guaranteed to exist until | |
| 56 // initialization on main and network threads completes and | |
| 57 // initNetworkThread is called back on network thread. | |
| 58 nativeInitRequestContextOnMainThread(mUrlRequestContextAdapter); | |
| 59 } | |
| 60 }; | |
| 61 new Handler(Looper.getMainLooper()).post(task); | |
| 40 } | 62 } |
| 41 | 63 |
| 42 @Override | 64 @Override |
| 43 public UrlRequest createRequest(String url, UrlRequestListener listener, | 65 public UrlRequest createRequest(String url, UrlRequestListener listener, |
| 44 Executor executor) { | 66 Executor executor) { |
| 45 if (mUrlRequestContextAdapter == 0) { | 67 if (mUrlRequestContextAdapter == 0) { |
| 46 throw new IllegalStateException( | 68 throw new IllegalStateException( |
| 47 "Cannot create requests on shutdown context."); | 69 "Cannot create requests on shutdown context."); |
| 48 } | 70 } |
| 49 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url, | 71 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url, |
| 50 UrlRequest.REQUEST_PRIORITY_MEDIUM, listener, executor); | 72 UrlRequest.REQUEST_PRIORITY_MEDIUM, listener, executor); |
| 51 } | 73 } |
| 52 | 74 |
| 53 @Override | 75 @Override |
| 54 public boolean isEnabled() { | 76 public boolean isEnabled() { |
| 55 return Build.VERSION.SDK_INT >= 14; | 77 return Build.VERSION.SDK_INT >= 14; |
| 56 } | 78 } |
| 57 | 79 |
| 58 @Override | 80 @Override |
| 59 public String getVersionString() { | 81 public String getVersionString() { |
| 60 return "Cronet/" + Version.getVersion(); | 82 return "Cronet/" + Version.getVersion(); |
| 61 } | 83 } |
| 62 | 84 |
| 63 @Override | 85 @Override |
| 64 public void shutdown() { | 86 public void shutdown() { |
| 65 if (mActiveRequestCount.get() != 0) { | 87 synchronized (mLock) { |
| 66 throw new IllegalStateException( | 88 if (mNetworkThread == null) { |
| 67 "Cannot shutdown with active requests."); | 89 throw new IllegalStateException( |
| 90 "Context is not fully initialized."); | |
| 91 } | |
| 92 if (mUrlRequestContextAdapter == 0) { | |
| 93 throw new IllegalStateException( | |
| 94 "Context is already shutdown."); | |
| 95 } | |
| 96 if (mActiveRequestCount.get() != 0) { | |
| 97 throw new IllegalStateException( | |
| 98 "Cannot shutdown with active requests."); | |
| 99 } | |
| 100 // Destroying adapter stops the network thread, so it cannot be | |
| 101 // called on network thread. | |
| 102 if (Thread.currentThread() == mNetworkThread) { | |
| 103 throw new IllegalThreadStateException( | |
| 104 "Cannot shutdown from network thread."); | |
| 105 } | |
| 106 nativeDestroyRequestContextAdapter(mUrlRequestContextAdapter); | |
| 107 mUrlRequestContextAdapter = 0; | |
| 68 } | 108 } |
| 69 // Destroying adapter stops the network thread, so it cannot be called | |
| 70 // on network thread. | |
| 71 if (Thread.currentThread() == mNetworkThread) { | |
| 72 throw new IllegalThreadStateException( | |
| 73 "Cannot shutdown from network thread."); | |
| 74 } | |
| 75 nativeDestroyRequestContextAdapter(mUrlRequestContextAdapter); | |
| 76 mUrlRequestContextAdapter = 0; | |
| 77 } | 109 } |
| 78 | 110 |
| 79 @Override | 111 @Override |
| 80 public void startNetLogToFile(String fileName) { | 112 public void startNetLogToFile(String fileName) { |
| 81 nativeStartNetLogToFile(mUrlRequestContextAdapter, fileName); | 113 nativeStartNetLogToFile(mUrlRequestContextAdapter, fileName); |
| 82 } | 114 } |
| 83 | 115 |
| 84 @Override | 116 @Override |
| 85 public void stopNetLog() { | 117 public void stopNetLog() { |
| 86 nativeStopNetLog(mUrlRequestContextAdapter); | 118 nativeStopNetLog(mUrlRequestContextAdapter); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 139 | 171 |
| 140 private native void nativeDestroyRequestContextAdapter( | 172 private native void nativeDestroyRequestContextAdapter( |
| 141 long urlRequestContextAdapter); | 173 long urlRequestContextAdapter); |
| 142 | 174 |
| 143 private native void nativeStartNetLogToFile( | 175 private native void nativeStartNetLogToFile( |
| 144 long urlRequestContextAdapter, String fileName); | 176 long urlRequestContextAdapter, String fileName); |
| 145 | 177 |
| 146 private native void nativeStopNetLog(long urlRequestContextAdapter); | 178 private native void nativeStopNetLog(long urlRequestContextAdapter); |
| 147 | 179 |
| 148 private native int nativeSetMinLogLevel(int loggingLevel); | 180 private native int nativeSetMinLogLevel(int loggingLevel); |
| 181 | |
| 182 private native void nativeInitRequestContextOnMainThread( | |
| 183 long urlRequestContextAdapter); | |
| 149 } | 184 } |
| OLD | NEW |