| 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; | |
| 8 import android.os.Build; | 7 import android.os.Build; |
| 9 import android.os.ConditionVariable; | 8 import android.os.ConditionVariable; |
| 10 import android.os.Handler; | 9 import android.os.Handler; |
| 11 import android.os.Looper; | 10 import android.os.Looper; |
| 12 import android.os.Process; | 11 import android.os.Process; |
| 13 import android.util.Log; | 12 import android.util.Log; |
| 14 | 13 |
| 15 import org.chromium.base.VisibleForTesting; | 14 import org.chromium.base.VisibleForTesting; |
| 16 import org.chromium.base.annotations.CalledByNative; | 15 import org.chromium.base.annotations.CalledByNative; |
| 17 import org.chromium.base.annotations.JNINamespace; | 16 import org.chromium.base.annotations.JNINamespace; |
| 18 import org.chromium.base.annotations.NativeClassQualifiedName; | 17 import org.chromium.base.annotations.NativeClassQualifiedName; |
| 19 import org.chromium.base.annotations.UsedByReflection; | 18 import org.chromium.base.annotations.UsedByReflection; |
| 20 | 19 |
| 21 import java.util.concurrent.Executor; | 20 import java.util.concurrent.Executor; |
| 22 import java.util.concurrent.atomic.AtomicInteger; | 21 import java.util.concurrent.atomic.AtomicInteger; |
| 23 | 22 |
| 24 /** | 23 /** |
| 25 * UrlRequestContext using Chromium HTTP stack implementation. | 24 * CronetEngine using Chromium HTTP stack implementation. |
| 26 */ | 25 */ |
| 27 @JNINamespace("cronet") | 26 @JNINamespace("cronet") |
| 28 @UsedByReflection("UrlRequestContext.java") | 27 @UsedByReflection("CronetEngine.java") |
| 29 class CronetUrlRequestContext extends UrlRequestContext { | 28 class CronetUrlRequestContext extends CronetEngine { |
| 30 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. | 29 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. |
| 31 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) | 30 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) |
| 32 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) | 31 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) |
| 33 static final String LOG_TAG = "ChromiumNetwork"; | 32 static final String LOG_TAG = "ChromiumNetwork"; |
| 34 | 33 |
| 35 /** | 34 /** |
| 36 * Synchronize access to mUrlRequestContextAdapter and shutdown routine. | 35 * Synchronize access to mUrlRequestContextAdapter and shutdown routine. |
| 37 */ | 36 */ |
| 38 private final Object mLock = new Object(); | 37 private final Object mLock = new Object(); |
| 39 private final ConditionVariable mInitCompleted = new ConditionVariable(false
); | 38 private final ConditionVariable mInitCompleted = new ConditionVariable(false
); |
| 40 private final AtomicInteger mActiveRequestCount = new AtomicInteger(0); | 39 private final AtomicInteger mActiveRequestCount = new AtomicInteger(0); |
| 41 | 40 |
| 42 private long mUrlRequestContextAdapter = 0; | 41 private long mUrlRequestContextAdapter = 0; |
| 43 private Thread mNetworkThread; | 42 private Thread mNetworkThread; |
| 44 | 43 |
| 45 @UsedByReflection("UrlRequestContext.java") | 44 @UsedByReflection("CronetEngine.java") |
| 46 public CronetUrlRequestContext(Context context, | 45 public CronetUrlRequestContext(CronetEngine.Builder builder) { |
| 47 UrlRequestContextConfig config) { | 46 CronetLibraryLoader.ensureInitialized(builder.getContext(), builder); |
| 48 CronetLibraryLoader.ensureInitialized(context, config); | |
| 49 nativeSetMinLogLevel(getLoggingLevel()); | 47 nativeSetMinLogLevel(getLoggingLevel()); |
| 50 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter(config.toS
tring()); | 48 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter(builder.to
String()); |
| 51 if (mUrlRequestContextAdapter == 0) { | 49 if (mUrlRequestContextAdapter == 0) { |
| 52 throw new NullPointerException("Context Adapter creation failed."); | 50 throw new NullPointerException("Context Adapter creation failed."); |
| 53 } | 51 } |
| 54 | 52 |
| 55 // Init native Chromium URLRequestContext on main UI thread. | 53 // Init native Chromium URLRequestContext on main UI thread. |
| 56 Runnable task = new Runnable() { | 54 Runnable task = new Runnable() { |
| 57 @Override | 55 @Override |
| 58 public void run() { | 56 public void run() { |
| 59 synchronized (mLock) { | 57 synchronized (mLock) { |
| 60 // mUrlRequestContextAdapter is guaranteed to exist until | 58 // mUrlRequestContextAdapter is guaranteed to exist until |
| (...skipping 10 matching lines...) Expand all Loading... |
| 71 new Handler(Looper.getMainLooper()).post(task); | 69 new Handler(Looper.getMainLooper()).post(task); |
| 72 } | 70 } |
| 73 } | 71 } |
| 74 | 72 |
| 75 @Override | 73 @Override |
| 76 public UrlRequest createRequest(String url, UrlRequestListener listener, | 74 public UrlRequest createRequest(String url, UrlRequestListener listener, |
| 77 Executor executor) { | 75 Executor executor) { |
| 78 synchronized (mLock) { | 76 synchronized (mLock) { |
| 79 checkHaveAdapter(); | 77 checkHaveAdapter(); |
| 80 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url, | 78 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url, |
| 81 UrlRequest.REQUEST_PRIORITY_MEDIUM, listener, executor); | 79 UrlRequest.Builder.REQUEST_PRIORITY_MEDIUM, listener, execut
or); |
| 82 } | 80 } |
| 83 } | 81 } |
| 84 | 82 |
| 85 @Override | 83 @Override |
| 86 public UrlRequest createRequest(String url, UrlRequestListener listener, | 84 public UrlRequest createRequest(String url, UrlRequestListener listener, |
| 87 Executor executor, int priority) { | 85 Executor executor, int priority) { |
| 88 synchronized (mLock) { | 86 synchronized (mLock) { |
| 89 checkHaveAdapter(); | 87 checkHaveAdapter(); |
| 90 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url, | 88 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url, |
| 91 priority, listener, executor); | 89 priority, listener, executor); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 @VisibleForTesting | 165 @VisibleForTesting |
| 168 long getUrlRequestContextAdapter() { | 166 long getUrlRequestContextAdapter() { |
| 169 synchronized (mLock) { | 167 synchronized (mLock) { |
| 170 checkHaveAdapter(); | 168 checkHaveAdapter(); |
| 171 return mUrlRequestContextAdapter; | 169 return mUrlRequestContextAdapter; |
| 172 } | 170 } |
| 173 } | 171 } |
| 174 | 172 |
| 175 private void checkHaveAdapter() throws IllegalStateException { | 173 private void checkHaveAdapter() throws IllegalStateException { |
| 176 if (!haveRequestContextAdapter()) { | 174 if (!haveRequestContextAdapter()) { |
| 177 throw new IllegalStateException("Context is shut down."); | 175 throw new IllegalStateException("Engine is shut down."); |
| 178 } | 176 } |
| 179 } | 177 } |
| 180 | 178 |
| 181 private boolean haveRequestContextAdapter() { | 179 private boolean haveRequestContextAdapter() { |
| 182 return mUrlRequestContextAdapter != 0; | 180 return mUrlRequestContextAdapter != 0; |
| 183 } | 181 } |
| 184 | 182 |
| 185 /** | 183 /** |
| 186 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and | 184 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and |
| 187 * {@link #LOG_VERBOSE}. | 185 * {@link #LOG_VERBOSE}. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 218 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 221 private native void nativeStartNetLogToFile(long nativePtr, | 219 private native void nativeStartNetLogToFile(long nativePtr, |
| 222 String fileName, boolean logAll); | 220 String fileName, boolean logAll); |
| 223 | 221 |
| 224 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 222 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 225 private native void nativeStopNetLog(long nativePtr); | 223 private native void nativeStopNetLog(long nativePtr); |
| 226 | 224 |
| 227 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 225 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 228 private native void nativeInitRequestContextOnMainThread(long nativePtr); | 226 private native void nativeInitRequestContextOnMainThread(long nativePtr); |
| 229 } | 227 } |
| OLD | NEW |