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.ConditionVariable; | |
| 10 import android.os.Handler; | |
| 11 import android.os.Looper; | |
| 9 import android.os.Process; | 12 import android.os.Process; |
| 10 import android.util.Log; | 13 import android.util.Log; |
| 11 | 14 |
| 12 import org.chromium.base.CalledByNative; | 15 import org.chromium.base.CalledByNative; |
| 13 import org.chromium.base.JNINamespace; | 16 import org.chromium.base.JNINamespace; |
| 14 | 17 |
| 15 import java.util.concurrent.Executor; | 18 import java.util.concurrent.Executor; |
| 16 import java.util.concurrent.atomic.AtomicInteger; | 19 import java.util.concurrent.atomic.AtomicInteger; |
| 17 | 20 |
| 18 /** | 21 /** |
| 19 * UrlRequest context using Chromium HTTP stack implementation. | 22 * UrlRequest context using Chromium HTTP stack implementation. |
| 20 */ | 23 */ |
| 21 @JNINamespace("cronet") | 24 @JNINamespace("cronet") |
| 22 public class CronetUrlRequestContext extends UrlRequestContext { | 25 public class CronetUrlRequestContext extends UrlRequestContext { |
| 23 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. | 26 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. |
| 24 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) | 27 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) | 28 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) |
| 26 static final String LOG_TAG = "ChromiumNetwork"; | 29 static final String LOG_TAG = "ChromiumNetwork"; |
| 27 | 30 |
| 31 /** | |
| 32 * Synchronize access to mUrlRequestContextAdapter and shutdown routine. | |
| 33 */ | |
| 34 private final Object mLock = new Object(); | |
| 28 private long mUrlRequestContextAdapter = 0; | 35 private long mUrlRequestContextAdapter = 0; |
| 29 private Thread mNetworkThread; | 36 private Thread mNetworkThread; |
| 37 private final ConditionVariable mInitCompleted = new ConditionVariable(false ); | |
|
mmenke
2015/01/23 20:41:41
A couple lines in here that could be reformatted t
mef
2015/01/28 21:32:28
Acknowledged.
| |
| 38 | |
| 30 private AtomicInteger mActiveRequestCount = new AtomicInteger(0); | 39 private AtomicInteger mActiveRequestCount = new AtomicInteger(0); |
| 31 | 40 |
| 32 public CronetUrlRequestContext(Context context, | 41 public CronetUrlRequestContext(Context context, |
| 33 UrlRequestContextConfig config) { | 42 UrlRequestContextConfig config) { |
| 43 CronetLibraryLoader.ensureInitialized(context, config); | |
| 34 nativeSetMinLogLevel(getLoggingLevel()); | 44 nativeSetMinLogLevel(getLoggingLevel()); |
| 35 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter( | 45 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter( |
| 36 context, config.toString()); | 46 context, config.toString()); |
| 37 if (mUrlRequestContextAdapter == 0) { | 47 if (mUrlRequestContextAdapter == 0) { |
| 38 throw new NullPointerException("Context Adapter creation failed"); | 48 throw new NullPointerException("Context Adapter creation failed"); |
| 39 } | 49 } |
|
mmenke
2015/01/23 20:41:41
Suggest a blank line before a comment that applies
mef
2015/01/28 21:32:28
Done.
| |
| 50 // Post a task to UI thread to init native Chromium URLRequestContext. | |
| 51 // TODO(xunjieli): This constructor is not supposed to be invoked on | |
| 52 // the main thread. Consider making the following code into a blocking | |
| 53 // API to handle the case where we are already on main thread. | |
|
mmenke
2015/01/23 20:41:41
Should be clearer here about what cases are bad.
mef
2015/01/28 21:32:28
Hrm, are they actually bad?
| |
| 54 Runnable task = new Runnable() { | |
| 55 public void run() { | |
| 56 synchronized (mLock) { | |
| 57 // mUrlRequestContextAdapter is guaranteed to exist until | |
| 58 // initialization on main and network threads completes and | |
| 59 // initNetworkThread is called back on network thread. | |
| 60 nativeInitRequestContextOnMainThread(mUrlRequestContextAdapt er); | |
| 61 mInitCompleted.open(); | |
| 62 } | |
| 63 } | |
| 64 }; | |
| 65 new Handler(Looper.getMainLooper()).post(task); | |
| 40 } | 66 } |
| 41 | 67 |
| 42 @Override | 68 @Override |
| 43 public UrlRequest createRequest(String url, UrlRequestListener listener, | 69 public UrlRequest createRequest(String url, UrlRequestListener listener, |
| 44 Executor executor) { | 70 Executor executor) { |
| 45 if (mUrlRequestContextAdapter == 0) { | 71 synchronized (mLock) { |
| 46 throw new IllegalStateException( | 72 if (mUrlRequestContextAdapter == 0) { |
| 47 "Cannot create requests on shutdown context."); | 73 throw new IllegalStateException( |
| 74 "Context is shutdown."); | |
|
mmenke
2015/01/23 20:41:41
Think this can fit on a single line.
mef
2015/01/28 21:32:28
Done.
| |
| 75 } | |
| 76 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url, | |
| 77 UrlRequest.REQUEST_PRIORITY_MEDIUM, listener, executor); | |
| 48 } | 78 } |
| 49 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url, | |
| 50 UrlRequest.REQUEST_PRIORITY_MEDIUM, listener, executor); | |
| 51 } | 79 } |
| 52 | 80 |
| 53 @Override | 81 @Override |
| 54 public boolean isEnabled() { | 82 public boolean isEnabled() { |
| 55 return Build.VERSION.SDK_INT >= 14; | 83 return Build.VERSION.SDK_INT >= 14; |
| 56 } | 84 } |
| 57 | 85 |
| 58 @Override | 86 @Override |
| 59 public String getVersionString() { | 87 public String getVersionString() { |
| 60 return "Cronet/" + Version.getVersion(); | 88 return "Cronet/" + Version.getVersion(); |
| 61 } | 89 } |
| 62 | 90 |
| 63 @Override | 91 @Override |
| 64 public void shutdown() { | 92 public void shutdown() { |
| 65 if (mActiveRequestCount.get() != 0) { | 93 synchronized (mLock) { |
|
mmenke
2015/01/23 20:41:41
Hrm...Still really not a fan of this. I'll see if
mef
2015/01/28 21:32:28
SG, let's chat tomorrow.
| |
| 66 throw new IllegalStateException( | 94 if (mUrlRequestContextAdapter == 0) { |
| 67 "Cannot shutdown with active requests."); | 95 throw new IllegalStateException( |
| 96 "Context is already shutdown."); | |
| 97 } | |
| 98 if (mActiveRequestCount.get() != 0) { | |
| 99 throw new IllegalStateException( | |
| 100 "Cannot shutdown with active requests."); | |
| 101 } | |
| 102 // Destroying adapter stops the network thread, so it cannot be | |
| 103 // called on network thread. | |
| 104 if (Thread.currentThread() == mNetworkThread) { | |
| 105 throw new IllegalThreadStateException( | |
| 106 "Cannot shutdown from network thread."); | |
| 107 } | |
| 68 } | 108 } |
| 69 // Destroying adapter stops the network thread, so it cannot be called | 109 // Wait for init to complete on main thread (without lock, so main threa d |
| 70 // on network thread. | 110 // could access it). |
| 71 if (Thread.currentThread() == mNetworkThread) { | 111 mInitCompleted.block(); |
| 72 throw new IllegalThreadStateException( | 112 |
| 73 "Cannot shutdown from network thread."); | 113 long urlRequestContextAdapter; |
| 114 synchronized (mLock) { | |
| 115 urlRequestContextAdapter = mUrlRequestContextAdapter; | |
| 116 mUrlRequestContextAdapter = 0; | |
| 74 } | 117 } |
| 75 nativeDestroyRequestContextAdapter(mUrlRequestContextAdapter); | 118 nativeDestroyRequestContextAdapter(urlRequestContextAdapter); |
| 76 mUrlRequestContextAdapter = 0; | |
| 77 } | 119 } |
| 78 | 120 |
| 79 @Override | 121 @Override |
| 80 public void startNetLogToFile(String fileName) { | 122 public void startNetLogToFile(String fileName) { |
| 81 nativeStartNetLogToFile(mUrlRequestContextAdapter, fileName); | 123 synchronized (mLock) { |
| 124 if (mUrlRequestContextAdapter == 0) { | |
| 125 throw new IllegalStateException( | |
| 126 "Context is shutdown."); | |
| 127 } | |
| 128 nativeStartNetLogToFile(mUrlRequestContextAdapter, fileName); | |
| 129 } | |
| 82 } | 130 } |
| 83 | 131 |
| 84 @Override | 132 @Override |
| 85 public void stopNetLog() { | 133 public void stopNetLog() { |
| 86 nativeStopNetLog(mUrlRequestContextAdapter); | 134 synchronized (mLock) { |
| 135 if (mUrlRequestContextAdapter == 0) { | |
| 136 throw new IllegalStateException( | |
| 137 "Context is shutdown."); | |
| 138 } | |
| 139 nativeStopNetLog(mUrlRequestContextAdapter); | |
| 140 } | |
| 87 } | 141 } |
| 88 | 142 |
| 89 /** | 143 /** |
| 90 * Mark request as started to prevent shutdown when there are active | 144 * Mark request as started to prevent shutdown when there are active |
| 91 * requests. | 145 * requests. |
| 92 */ | 146 */ |
| 93 void onRequestStarted(UrlRequest urlRequest) { | 147 void onRequestStarted(UrlRequest urlRequest) { |
| 94 mActiveRequestCount.incrementAndGet(); | 148 mActiveRequestCount.incrementAndGet(); |
| 95 } | 149 } |
| 96 | 150 |
| 97 /** | 151 /** |
| 98 * Mark request as completed to allow shutdown when there are no active | 152 * Mark request as completed to allow shutdown when there are no active |
| 99 * requests. | 153 * requests. |
| 100 */ | 154 */ |
| 101 void onRequestDestroyed(UrlRequest urlRequest) { | 155 void onRequestDestroyed(UrlRequest urlRequest) { |
| 102 mActiveRequestCount.decrementAndGet(); | 156 mActiveRequestCount.decrementAndGet(); |
| 103 } | 157 } |
| 104 | 158 |
| 105 long getUrlRequestContextAdapter() { | 159 long getUrlRequestContextAdapter() { |
| 106 if (mUrlRequestContextAdapter == 0) { | 160 synchronized (mLock) { |
| 107 throw new IllegalStateException("Context Adapter is destroyed."); | 161 if (mUrlRequestContextAdapter == 0) { |
| 162 throw new IllegalStateException("Context is shutdown."); | |
| 163 } | |
| 164 return mUrlRequestContextAdapter; | |
| 108 } | 165 } |
| 109 return mUrlRequestContextAdapter; | |
| 110 } | 166 } |
| 111 | 167 |
| 112 /** | 168 /** |
| 113 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and | 169 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and |
| 114 * {@link #LOG_VERBOSE}. | 170 * {@link #LOG_VERBOSE}. |
| 115 */ | 171 */ |
| 116 private int getLoggingLevel() { | 172 private int getLoggingLevel() { |
| 117 int loggingLevel; | 173 int loggingLevel; |
| 118 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) { | 174 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) { |
| 119 loggingLevel = LOG_VERBOSE; | 175 loggingLevel = LOG_VERBOSE; |
| 120 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { | 176 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { |
| 121 loggingLevel = LOG_DEBUG; | 177 loggingLevel = LOG_DEBUG; |
| 122 } else { | 178 } else { |
| 123 loggingLevel = LOG_NONE; | 179 loggingLevel = LOG_NONE; |
| 124 } | 180 } |
| 125 return loggingLevel; | 181 return loggingLevel; |
| 126 } | 182 } |
| 127 | 183 |
| 128 @SuppressWarnings("unused") | 184 @SuppressWarnings("unused") |
| 129 @CalledByNative | 185 @CalledByNative |
| 130 private void initNetworkThread() { | 186 private void initNetworkThread() { |
| 131 mNetworkThread = Thread.currentThread(); | 187 synchronized (mLock) { |
| 188 mNetworkThread = Thread.currentThread(); | |
| 189 } | |
| 132 Thread.currentThread().setName("ChromiumNet"); | 190 Thread.currentThread().setName("ChromiumNet"); |
| 133 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); | 191 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 134 } | 192 } |
| 135 | 193 |
| 136 // Native methods are implemented in cronet_url_request_context.cc. | 194 // Native methods are implemented in cronet_url_request_context.cc. |
| 137 private native long nativeCreateRequestContextAdapter(Context context, | 195 private native long nativeCreateRequestContextAdapter(Context context, |
| 138 String config); | 196 String config); |
| 139 | 197 |
| 140 private native void nativeDestroyRequestContextAdapter( | 198 private native void nativeDestroyRequestContextAdapter( |
| 141 long urlRequestContextAdapter); | 199 long urlRequestContextAdapter); |
| 142 | 200 |
| 143 private native void nativeStartNetLogToFile( | 201 private native void nativeStartNetLogToFile( |
| 144 long urlRequestContextAdapter, String fileName); | 202 long urlRequestContextAdapter, String fileName); |
| 145 | 203 |
| 146 private native void nativeStopNetLog(long urlRequestContextAdapter); | 204 private native void nativeStopNetLog(long urlRequestContextAdapter); |
| 147 | 205 |
| 148 private native int nativeSetMinLogLevel(int loggingLevel); | 206 private native int nativeSetMinLogLevel(int loggingLevel); |
| 207 | |
| 208 private native void nativeInitRequestContextOnMainThread( | |
| 209 long urlRequestContextAdapter); | |
| 149 } | 210 } |
| OLD | NEW |