Chromium Code Reviews| Index: components/cronet/android/java/src/org/chromium/net/CronetUrlRequestContext.java |
| diff --git a/components/cronet/android/java/src/org/chromium/net/CronetUrlRequestContext.java b/components/cronet/android/java/src/org/chromium/net/CronetUrlRequestContext.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c073e4b2cbecbf8db8cc6aa7afc147956a68b18a |
| --- /dev/null |
| +++ b/components/cronet/android/java/src/org/chromium/net/CronetUrlRequestContext.java |
| @@ -0,0 +1,142 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.net; |
| + |
| +import android.content.Context; |
| +import android.os.Build; |
| +import android.os.Process; |
| +import android.util.Log; |
| + |
| +import org.chromium.base.CalledByNative; |
| +import org.chromium.base.JNINamespace; |
| + |
| +import java.util.concurrent.Executor; |
| +import java.util.concurrent.atomic.AtomicInteger; |
| + |
| +/** |
| + * UrlRequest context using Chromium HTTP stack implementation. |
| + */ |
| +@JNINamespace("cronet") |
| +public class CronetUrlRequestContext extends UrlRequestContext { |
| + private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. |
| + private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) |
| + private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) |
| + static final String LOG_TAG = "ChromiumNetwork"; |
| + |
| + private long mUrlRequestContextAdapter = 0; |
| + private Thread mNetworkThread; |
| + private AtomicInteger mActiveRequestCount = new AtomicInteger(0); |
| + |
| + public CronetUrlRequestContext(Context context, |
| + UrlRequestContextConfig config) { |
| + nativeSetMinLogLevel(getLoggingLevel()); |
|
mmenke
2014/11/06 17:31:29
Does it really make sense to call this whenever we
mef
2014/11/06 22:51:47
Good question. Theoretically log level could chang
|
| + mUrlRequestContextAdapter = nativeCreateRequestContextAdapter( |
| + context, config.toString()); |
| + if (mUrlRequestContextAdapter == 0) { |
| + throw new NullPointerException("Context Adapter creation failed"); |
| + } |
| + } |
| + |
| + @Override |
| + public UrlRequest createRequest(String url, UrlRequestListener listener, |
| + Executor executor) { |
| + if (mUrlRequestContextAdapter == 0) { |
| + throw new IllegalStateException( |
| + "Cannot create requests on shutdown context."); |
| + } |
| + return new CronetUrlRequest(this, mUrlRequestContextAdapter, url, |
| + UrlRequest.REQUEST_PRIORITY_MEDIUM, listener, executor); |
| + } |
| + |
| + @Override |
| + public boolean isEnabled() { |
| + return Build.VERSION.SDK_INT >= 14; |
| + } |
| + |
| + @Override |
| + public String getVersionString() { |
| + return "Cronet/" + Version.getVersion(); |
| + } |
| + |
| + @Override |
| + public void shutdown() { |
| + if (mActiveRequestCount.get() != 0) { |
| + throw new IllegalStateException( |
| + "Cannot shutdown with active requests."); |
| + } |
| + // Destroying adapter stops the network thread, so it cannot be called |
| + // on network thread. |
| + if (Thread.currentThread() == mNetworkThread) { |
| + throw new IllegalThreadStateException( |
| + "Cannot shutdown from network thread."); |
| + } |
| + nativeDestroyRequestContextAdapter(mUrlRequestContextAdapter); |
| + mUrlRequestContextAdapter = 0; |
| + } |
| + |
| + /** |
| + * Mark request as started to prevent shutdown when there are active |
| + * requests. |
| + */ |
| + void onRequestStarted(UrlRequest urlRequest) { |
| + mActiveRequestCount.incrementAndGet(); |
| + } |
| + |
| + /** |
| + * Mark request as completed to allow shutdown when there are no active |
| + * requests. |
| + */ |
| + void onRequestDestroyed(UrlRequest urlRequest) { |
| + mActiveRequestCount.decrementAndGet(); |
| + } |
| + |
| + long getUrlRequestContextAdapter() { |
| + if (mUrlRequestContextAdapter == 0) { |
| + throw new IllegalStateException("Context Adapter is destroyed."); |
| + } |
| + return mUrlRequestContextAdapter; |
| + } |
| + |
| + /** |
| + * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and |
| + * {@link #LOG_VERBOSE}. |
| + */ |
| + private int getLoggingLevel() { |
| + int loggingLevel; |
| + if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) { |
| + loggingLevel = LOG_VERBOSE; |
| + } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { |
| + loggingLevel = LOG_DEBUG; |
| + } else { |
| + loggingLevel = LOG_NONE; |
| + } |
| + return loggingLevel; |
| + } |
| + |
| + @CalledByNative |
|
mmenke
2014/11/06 17:31:29
Why isn't @SuppressWarnings("unused") needed?
mef
2014/11/06 22:51:47
Done.
|
| + private void initNetworkThread() { |
| + mNetworkThread = Thread.currentThread(); |
| + Thread.currentThread().setName("ChromiumNet"); |
| + Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| + } |
| + |
| + // Native methods are implemented in cronet_url_request_context.cc. |
| + private native long nativeCreateRequestContextAdapter(Context context, |
| + String config); |
| + |
| + private native void nativeDestroyRequestContextAdapter( |
| + long urlRequestContextAdapter); |
| + |
| + private native void nativeInitializeStatistics(); |
| + |
| + private native String nativeGetStatisticsJSON(String filter); |
| + |
| + private native void nativeStartNetLogToFile( |
| + long urlRequestContextAdapter, String fileName); |
| + |
| + private native void nativeStopNetLog(long urlRequestContextAdapter); |
| + |
| + private native int nativeSetMinLogLevel(int loggingLevel); |
| +} |