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.loadAndInitialize(context, config); | |
xunjieli
2015/01/05 14:48:03
Maybe rename "loadAndInitialize" to sth like "init
mef
2015/01/05 16:59:21
Done. ensureInitialized seems to be commonly used
| |
34 nativeSetMinLogLevel(getLoggingLevel()); | 37 nativeSetMinLogLevel(getLoggingLevel()); |
35 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter( | 38 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter( |
36 context, config.toString()); | 39 context, config.toString()); |
37 if (mUrlRequestContextAdapter == 0) { | 40 if (mUrlRequestContextAdapter == 0) { |
38 throw new NullPointerException("Context Adapter creation failed"); | 41 throw new NullPointerException("Context Adapter creation failed"); |
39 } | 42 } |
43 // Post a task to UI thread to init native Chromium URLRequestContext. | |
44 // TODO(xunjieli): This constructor is not supposed to be invoked on | |
45 // the main thread. Consider making the following code into a blocking | |
46 // API to handle the case where we are already on main thread. | |
47 Runnable task = new Runnable() { | |
48 public void run() { | |
49 nativeInitRequestContextOnMainThread( | |
50 mUrlRequestContextAdapter); | |
51 } | |
52 }; | |
53 new Handler(Looper.getMainLooper()).post(task); | |
40 } | 54 } |
41 | 55 |
42 @Override | 56 @Override |
43 public UrlRequest createRequest(String url, UrlRequestListener listener, | 57 public UrlRequest createRequest(String url, UrlRequestListener listener, |
44 Executor executor) { | 58 Executor executor) { |
45 if (mUrlRequestContextAdapter == 0) { | 59 if (mUrlRequestContextAdapter == 0) { |
46 throw new IllegalStateException( | 60 throw new IllegalStateException( |
47 "Cannot create requests on shutdown context."); | 61 "Cannot create requests on shutdown context."); |
48 } | 62 } |
49 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url, | 63 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url, |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 | 153 |
140 private native void nativeDestroyRequestContextAdapter( | 154 private native void nativeDestroyRequestContextAdapter( |
141 long urlRequestContextAdapter); | 155 long urlRequestContextAdapter); |
142 | 156 |
143 private native void nativeStartNetLogToFile( | 157 private native void nativeStartNetLogToFile( |
144 long urlRequestContextAdapter, String fileName); | 158 long urlRequestContextAdapter, String fileName); |
145 | 159 |
146 private native void nativeStopNetLog(long urlRequestContextAdapter); | 160 private native void nativeStopNetLog(long urlRequestContextAdapter); |
147 | 161 |
148 private native int nativeSetMinLogLevel(int loggingLevel); | 162 private native int nativeSetMinLogLevel(int loggingLevel); |
163 | |
164 private native void nativeInitRequestContextOnMainThread( | |
165 long urlRequestContextAdapter); | |
149 } | 166 } |
OLD | NEW |