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.Handler; |
| 10 import android.os.Looper; |
9 import android.os.Process; | 11 import android.os.Process; |
10 import android.util.Log; | 12 import android.util.Log; |
11 | 13 |
12 import org.chromium.base.CalledByNative; | 14 import org.chromium.base.CalledByNative; |
13 import org.chromium.base.JNINamespace; | 15 import org.chromium.base.JNINamespace; |
14 | 16 |
15 import java.util.concurrent.Executor; | 17 import java.util.concurrent.Executor; |
16 import java.util.concurrent.atomic.AtomicInteger; | 18 import java.util.concurrent.atomic.AtomicInteger; |
17 | 19 |
18 /** | 20 /** |
19 * UrlRequest context using Chromium HTTP stack implementation. | 21 * UrlRequest context using Chromium HTTP stack implementation. |
20 */ | 22 */ |
21 @JNINamespace("cronet") | 23 @JNINamespace("cronet") |
22 public class CronetUrlRequestContext extends UrlRequestContext { | 24 public class CronetUrlRequestContext extends UrlRequestContext { |
23 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. | 25 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. |
24 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) | 26 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) | 27 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) |
26 static final String LOG_TAG = "ChromiumNetwork"; | 28 static final String LOG_TAG = "ChromiumNetwork"; |
27 | 29 |
28 private long mUrlRequestContextAdapter = 0; | 30 private long mUrlRequestContextAdapter = 0; |
29 private Thread mNetworkThread; | 31 private Thread mNetworkThread; |
30 private AtomicInteger mActiveRequestCount = new AtomicInteger(0); | 32 private AtomicInteger mActiveRequestCount = new AtomicInteger(0); |
31 | 33 |
32 public CronetUrlRequestContext(Context context, | 34 public CronetUrlRequestContext(Context context, |
33 UrlRequestContextConfig config) { | 35 UrlRequestContextConfig config) { |
| 36 CronetLibraryLoader.ensureInitialized(context, config); |
34 nativeSetMinLogLevel(getLoggingLevel()); | 37 nativeSetMinLogLevel(getLoggingLevel()); |
| 38 final String configString = config.toString(); |
35 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter( | 39 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter( |
36 context, config.toString()); | 40 context, configString); |
37 if (mUrlRequestContextAdapter == 0) { | 41 if (mUrlRequestContextAdapter == 0) { |
38 throw new NullPointerException("Context Adapter creation failed"); | 42 throw new NullPointerException("Context Adapter creation failed"); |
39 } | 43 } |
| 44 // Post a task to UI thread to init native Chromium URLRequestContext. |
| 45 // TODO(xunjieli): This constructor is not supposed to be invoked on |
| 46 // the main thread. Consider making the following code into a blocking |
| 47 // API to handle the case where we are already on main thread. |
| 48 Runnable task = new Runnable() { |
| 49 public void run() { |
| 50 nativeInitRequestContextOnMainThread( |
| 51 mUrlRequestContextAdapter, configString); |
| 52 } |
| 53 }; |
| 54 new Handler(Looper.getMainLooper()).post(task); |
40 } | 55 } |
41 | 56 |
42 @Override | 57 @Override |
43 public UrlRequest createRequest(String url, UrlRequestListener listener, | 58 public UrlRequest createRequest(String url, UrlRequestListener listener, |
44 Executor executor) { | 59 Executor executor) { |
45 if (mUrlRequestContextAdapter == 0) { | 60 if (mUrlRequestContextAdapter == 0) { |
46 throw new IllegalStateException( | 61 throw new IllegalStateException( |
47 "Cannot create requests on shutdown context."); | 62 "Cannot create requests on shutdown context."); |
48 } | 63 } |
49 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url, | 64 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url, |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 | 154 |
140 private native void nativeDestroyRequestContextAdapter( | 155 private native void nativeDestroyRequestContextAdapter( |
141 long urlRequestContextAdapter); | 156 long urlRequestContextAdapter); |
142 | 157 |
143 private native void nativeStartNetLogToFile( | 158 private native void nativeStartNetLogToFile( |
144 long urlRequestContextAdapter, String fileName); | 159 long urlRequestContextAdapter, String fileName); |
145 | 160 |
146 private native void nativeStopNetLog(long urlRequestContextAdapter); | 161 private native void nativeStopNetLog(long urlRequestContextAdapter); |
147 | 162 |
148 private native int nativeSetMinLogLevel(int loggingLevel); | 163 private native int nativeSetMinLogLevel(int loggingLevel); |
| 164 |
| 165 private native void nativeInitRequestContextOnMainThread( |
| 166 long urlRequestContextAdapter, String config); |
149 } | 167 } |
OLD | NEW |