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 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter( |
| 35 context, getLoggingLevel(), config.toString()); |
| 36 if (mUrlRequestContextAdapter == 0) { |
| 37 throw new NullPointerException("Context Adapter creation failed"); |
| 38 } |
| 39 } |
| 40 |
| 41 @Override |
| 42 public UrlRequest createRequest(String url, UrlRequestListener listener, |
| 43 Executor executor) { |
| 44 if (mUrlRequestContextAdapter == 0) { |
| 45 throw new IllegalStateException( |
| 46 "Cannot create requests on shutdown context."); |
| 47 } |
| 48 mActiveRequestCount.incrementAndGet(); |
| 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 getName() { |
| 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 |
| 70 nativeDestroyRequestContextAdapter(mUrlRequestContextAdapter); |
| 71 mUrlRequestContextAdapter = 0; |
| 72 } |
| 73 |
| 74 /** |
| 75 * Mark request as completed to allow shutdown when there are no active requ
ests. |
| 76 */ |
| 77 void onRequestDestroyed(UrlRequest urlRequest) { |
| 78 mActiveRequestCount.decrementAndGet(); |
| 79 } |
| 80 |
| 81 /** |
| 82 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and |
| 83 * {@link #LOG_VERBOSE}. |
| 84 */ |
| 85 private int getLoggingLevel() { |
| 86 int loggingLevel; |
| 87 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) { |
| 88 loggingLevel = LOG_VERBOSE; |
| 89 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { |
| 90 loggingLevel = LOG_DEBUG; |
| 91 } else { |
| 92 loggingLevel = LOG_NONE; |
| 93 } |
| 94 return loggingLevel; |
| 95 } |
| 96 |
| 97 @CalledByNative |
| 98 private void initNetworkThread() { |
| 99 mNetworkThread = Thread.currentThread(); |
| 100 Thread.currentThread().setName("ChromiumNet"); |
| 101 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 102 } |
| 103 |
| 104 // Native methods are implemented in cronet_url_request_context.cc. |
| 105 private native long nativeCreateRequestContextAdapter(Context context, |
| 106 int loggingLevel, String config); |
| 107 |
| 108 private native void nativeDestroyRequestContextAdapter( |
| 109 long urlRequestContextAdapter); |
| 110 |
| 111 private native void nativeInitializeStatistics(); |
| 112 |
| 113 private native String nativeGetStatisticsJSON(String filter); |
| 114 |
| 115 private native void nativeStartNetLogToFile( |
| 116 long urlRequestContextAdapter, String fileName); |
| 117 |
| 118 private native void nativeStopNetLog(long urlRequestContextAdapter); |
| 119 } |
OLD | NEW |