Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.net; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.os.Build; | |
| 9 import android.os.Process; | |
| 10 import android.util.Log; | |
| 11 | |
| 12 import org.chromium.base.CalledByNative; | |
| 13 import org.chromium.base.JNINamespace; | |
| 14 | |
| 15 import java.util.concurrent.Executor; | |
| 16 import java.util.concurrent.atomic.AtomicInteger; | |
| 17 | |
| 18 /** | |
| 19 * UrlRequest context using Chromium HTTP stack implementation. | |
| 20 */ | |
| 21 @JNINamespace("cronet") | |
| 22 public class CronetUrlRequestContext extends UrlRequestContext { | |
| 23 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. | |
| 24 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) | |
| 26 static final String LOG_TAG = "ChromiumNetwork"; | |
| 27 | |
| 28 private long mUrlRequestContextAdapter = 0; | |
| 29 private Thread mNetworkThread; | |
| 30 private AtomicInteger mActiveRequestCount = new AtomicInteger(0); | |
| 31 | |
| 32 public CronetUrlRequestContext(Context context, | |
| 33 UrlRequestContextConfig config) { | |
| 34 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
| |
| 35 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter( | |
| 36 context, config.toString()); | |
| 37 if (mUrlRequestContextAdapter == 0) { | |
| 38 throw new NullPointerException("Context Adapter creation failed"); | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 @Override | |
| 43 public UrlRequest createRequest(String url, UrlRequestListener listener, | |
| 44 Executor executor) { | |
| 45 if (mUrlRequestContextAdapter == 0) { | |
| 46 throw new IllegalStateException( | |
| 47 "Cannot create requests on shutdown context."); | |
| 48 } | |
| 49 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url, | |
| 50 UrlRequest.REQUEST_PRIORITY_MEDIUM, listener, executor); | |
| 51 } | |
| 52 | |
| 53 @Override | |
| 54 public boolean isEnabled() { | |
| 55 return Build.VERSION.SDK_INT >= 14; | |
| 56 } | |
| 57 | |
| 58 @Override | |
| 59 public String getVersionString() { | |
| 60 return "Cronet/" + Version.getVersion(); | |
| 61 } | |
| 62 | |
| 63 @Override | |
| 64 public void shutdown() { | |
| 65 if (mActiveRequestCount.get() != 0) { | |
| 66 throw new IllegalStateException( | |
| 67 "Cannot shutdown with active requests."); | |
| 68 } | |
| 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 } | |
| 78 | |
| 79 /** | |
| 80 * Mark request as started to prevent shutdown when there are active | |
| 81 * requests. | |
| 82 */ | |
| 83 void onRequestStarted(UrlRequest urlRequest) { | |
| 84 mActiveRequestCount.incrementAndGet(); | |
| 85 } | |
| 86 | |
| 87 /** | |
| 88 * Mark request as completed to allow shutdown when there are no active | |
| 89 * requests. | |
| 90 */ | |
| 91 void onRequestDestroyed(UrlRequest urlRequest) { | |
| 92 mActiveRequestCount.decrementAndGet(); | |
| 93 } | |
| 94 | |
| 95 long getUrlRequestContextAdapter() { | |
| 96 if (mUrlRequestContextAdapter == 0) { | |
| 97 throw new IllegalStateException("Context Adapter is destroyed."); | |
| 98 } | |
| 99 return mUrlRequestContextAdapter; | |
| 100 } | |
| 101 | |
| 102 /** | |
| 103 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and | |
| 104 * {@link #LOG_VERBOSE}. | |
| 105 */ | |
| 106 private int getLoggingLevel() { | |
| 107 int loggingLevel; | |
| 108 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) { | |
| 109 loggingLevel = LOG_VERBOSE; | |
| 110 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { | |
| 111 loggingLevel = LOG_DEBUG; | |
| 112 } else { | |
| 113 loggingLevel = LOG_NONE; | |
| 114 } | |
| 115 return loggingLevel; | |
| 116 } | |
| 117 | |
| 118 @CalledByNative | |
|
mmenke
2014/11/06 17:31:29
Why isn't @SuppressWarnings("unused") needed?
mef
2014/11/06 22:51:47
Done.
| |
| 119 private void initNetworkThread() { | |
| 120 mNetworkThread = Thread.currentThread(); | |
| 121 Thread.currentThread().setName("ChromiumNet"); | |
| 122 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); | |
| 123 } | |
| 124 | |
| 125 // Native methods are implemented in cronet_url_request_context.cc. | |
| 126 private native long nativeCreateRequestContextAdapter(Context context, | |
| 127 String config); | |
| 128 | |
| 129 private native void nativeDestroyRequestContextAdapter( | |
| 130 long urlRequestContextAdapter); | |
| 131 | |
| 132 private native void nativeInitializeStatistics(); | |
| 133 | |
| 134 private native String nativeGetStatisticsJSON(String filter); | |
| 135 | |
| 136 private native void nativeStartNetLogToFile( | |
| 137 long urlRequestContextAdapter, String fileName); | |
| 138 | |
| 139 private native void nativeStopNetLog(long urlRequestContextAdapter); | |
| 140 | |
| 141 private native int nativeSetMinLogLevel(int loggingLevel); | |
| 142 } | |
| OLD | NEW |