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.ConditionVariable; | |
10 import android.os.Handler; | |
11 import android.os.Looper; | |
9 import android.os.Process; | 12 import android.os.Process; |
10 import android.util.Log; | 13 import android.util.Log; |
11 | 14 |
12 import org.chromium.base.CalledByNative; | 15 import org.chromium.base.CalledByNative; |
13 import org.chromium.base.JNINamespace; | 16 import org.chromium.base.JNINamespace; |
14 | 17 |
15 import java.util.concurrent.Executor; | 18 import java.util.concurrent.Executor; |
16 import java.util.concurrent.atomic.AtomicInteger; | 19 import java.util.concurrent.atomic.AtomicInteger; |
17 | 20 |
18 /** | 21 /** |
19 * UrlRequest context using Chromium HTTP stack implementation. | 22 * UrlRequest context using Chromium HTTP stack implementation. |
20 */ | 23 */ |
21 @JNINamespace("cronet") | 24 @JNINamespace("cronet") |
22 public class CronetUrlRequestContext extends UrlRequestContext { | 25 public class CronetUrlRequestContext extends UrlRequestContext { |
23 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. | 26 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. |
24 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) | 27 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) | 28 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) |
26 static final String LOG_TAG = "ChromiumNetwork"; | 29 static final String LOG_TAG = "ChromiumNetwork"; |
27 | 30 |
31 /** | |
32 * Synchronize access to mUrlRequestContextAdapter and shutdown routine. | |
33 */ | |
34 private final Object mLock = new Object(); | |
28 private long mUrlRequestContextAdapter = 0; | 35 private long mUrlRequestContextAdapter = 0; |
29 private Thread mNetworkThread; | 36 private Thread mNetworkThread; |
37 private final ConditionVariable mInitCompleted = new ConditionVariable(false ); | |
38 | |
30 private AtomicInteger mActiveRequestCount = new AtomicInteger(0); | 39 private AtomicInteger mActiveRequestCount = new AtomicInteger(0); |
31 | 40 |
32 public CronetUrlRequestContext(Context context, | 41 public CronetUrlRequestContext(Context context, |
33 UrlRequestContextConfig config) { | 42 UrlRequestContextConfig config) { |
43 CronetLibraryLoader.ensureInitialized(context, config); | |
34 nativeSetMinLogLevel(getLoggingLevel()); | 44 nativeSetMinLogLevel(getLoggingLevel()); |
35 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter( | 45 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter( |
36 context, config.toString()); | 46 context, config.toString()); |
37 if (mUrlRequestContextAdapter == 0) { | 47 if (mUrlRequestContextAdapter == 0) { |
38 throw new NullPointerException("Context Adapter creation failed"); | 48 throw new NullPointerException("Context Adapter creation failed"); |
39 } | 49 } |
50 | |
51 // Post a task to UI thread to init native Chromium URLRequestContext. | |
52 // TODO(xunjieli): This constructor is not supposed to be invoked on | |
53 // the main thread. Consider making the following code into a blocking | |
54 // API to handle the case where we are already on main thread. | |
mmenke
2015/02/02 19:30:26
Random comment: Because this is a non-blocking AP
mef
2015/02/03 01:28:57
Done.
| |
55 Runnable task = new Runnable() { | |
56 public void run() { | |
57 synchronized (mLock) { | |
58 // mUrlRequestContextAdapter is guaranteed to exist until | |
59 // initialization on main and network threads completes and | |
60 // initNetworkThread is called back on network thread. | |
61 nativeInitRequestContextOnMainThread(mUrlRequestContextAdapt er); | |
62 mInitCompleted.open(); | |
63 } | |
64 } | |
65 }; | |
66 new Handler(Looper.getMainLooper()).post(task); | |
40 } | 67 } |
41 | 68 |
42 @Override | 69 @Override |
43 public UrlRequest createRequest(String url, UrlRequestListener listener, | 70 public UrlRequest createRequest(String url, UrlRequestListener listener, |
44 Executor executor) { | 71 Executor executor) { |
45 if (mUrlRequestContextAdapter == 0) { | 72 synchronized (mLock) { |
46 throw new IllegalStateException( | 73 if (mUrlRequestContextAdapter == 0) { |
47 "Cannot create requests on shutdown context."); | 74 throw new IllegalStateException("Context is shutdown."); |
75 } | |
76 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url, | |
77 UrlRequest.REQUEST_PRIORITY_MEDIUM, listener, executor); | |
48 } | 78 } |
49 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url, | |
50 UrlRequest.REQUEST_PRIORITY_MEDIUM, listener, executor); | |
51 } | 79 } |
52 | 80 |
53 @Override | 81 @Override |
54 public boolean isEnabled() { | 82 public boolean isEnabled() { |
55 return Build.VERSION.SDK_INT >= 14; | 83 return Build.VERSION.SDK_INT >= 14; |
56 } | 84 } |
57 | 85 |
58 @Override | 86 @Override |
59 public String getVersionString() { | 87 public String getVersionString() { |
60 return "Cronet/" + Version.getVersion(); | 88 return "Cronet/" + Version.getVersion(); |
61 } | 89 } |
62 | 90 |
63 @Override | 91 @Override |
64 public void shutdown() { | 92 public void shutdown() { |
65 if (mActiveRequestCount.get() != 0) { | 93 synchronized (mLock) { |
66 throw new IllegalStateException( | 94 if (mUrlRequestContextAdapter == 0) { |
67 "Cannot shutdown with active requests."); | 95 throw new IllegalStateException( |
96 "Context is already shutdown."); | |
97 } | |
98 if (mActiveRequestCount.get() != 0) { | |
99 throw new IllegalStateException( | |
100 "Cannot shutdown with active requests."); | |
101 } | |
102 // Destroying adapter stops the network thread, so it cannot be | |
103 // called on network thread. | |
104 if (Thread.currentThread() == mNetworkThread) { | |
105 throw new IllegalThreadStateException( | |
106 "Cannot shutdown from network thread."); | |
107 } | |
68 } | 108 } |
69 // Destroying adapter stops the network thread, so it cannot be called | 109 // Wait for init to complete on main thread (without lock, so main threa d |
70 // on network thread. | 110 // could access it). |
71 if (Thread.currentThread() == mNetworkThread) { | 111 mInitCompleted.block(); |
mmenke
2015/02/02 19:30:26
This doesn't work if shutdown is called on two thr
mef
2015/02/03 01:28:57
Done.
| |
72 throw new IllegalThreadStateException( | 112 |
73 "Cannot shutdown from network thread."); | 113 long urlRequestContextAdapter; |
114 synchronized (mLock) { | |
115 urlRequestContextAdapter = mUrlRequestContextAdapter; | |
116 mUrlRequestContextAdapter = 0; | |
74 } | 117 } |
75 nativeDestroyRequestContextAdapter(mUrlRequestContextAdapter); | 118 nativeDestroyRequestContextAdapter(urlRequestContextAdapter); |
mmenke
2015/02/02 19:30:26
Can we just move this into the synchronized block,
mef
2015/02/03 01:28:57
Unfortunately nativeDestroyRequestContextAdapter u
mef
2015/02/03 14:29:03
Done. The deadlock was due to synchronized (mLock)
| |
76 mUrlRequestContextAdapter = 0; | |
77 } | 119 } |
78 | 120 |
79 @Override | 121 @Override |
80 public void startNetLogToFile(String fileName) { | 122 public void startNetLogToFile(String fileName) { |
81 nativeStartNetLogToFile(mUrlRequestContextAdapter, fileName); | 123 synchronized (mLock) { |
124 if (mUrlRequestContextAdapter == 0) { | |
125 throw new IllegalStateException( | |
126 "Context is shutdown."); | |
127 } | |
128 nativeStartNetLogToFile(mUrlRequestContextAdapter, fileName); | |
129 } | |
82 } | 130 } |
83 | 131 |
84 @Override | 132 @Override |
85 public void stopNetLog() { | 133 public void stopNetLog() { |
86 nativeStopNetLog(mUrlRequestContextAdapter); | 134 synchronized (mLock) { |
135 if (mUrlRequestContextAdapter == 0) { | |
136 throw new IllegalStateException( | |
137 "Context is shutdown."); | |
138 } | |
139 nativeStopNetLog(mUrlRequestContextAdapter); | |
140 } | |
87 } | 141 } |
88 | 142 |
89 /** | 143 /** |
90 * Mark request as started to prevent shutdown when there are active | 144 * Mark request as started to prevent shutdown when there are active |
91 * requests. | 145 * requests. |
92 */ | 146 */ |
93 void onRequestStarted(UrlRequest urlRequest) { | 147 void onRequestStarted(UrlRequest urlRequest) { |
94 mActiveRequestCount.incrementAndGet(); | 148 mActiveRequestCount.incrementAndGet(); |
95 } | 149 } |
96 | 150 |
97 /** | 151 /** |
98 * Mark request as completed to allow shutdown when there are no active | 152 * Mark request as completed to allow shutdown when there are no active |
99 * requests. | 153 * requests. |
100 */ | 154 */ |
101 void onRequestDestroyed(UrlRequest urlRequest) { | 155 void onRequestDestroyed(UrlRequest urlRequest) { |
102 mActiveRequestCount.decrementAndGet(); | 156 mActiveRequestCount.decrementAndGet(); |
103 } | 157 } |
104 | 158 |
105 long getUrlRequestContextAdapter() { | 159 long getUrlRequestContextAdapter() { |
106 if (mUrlRequestContextAdapter == 0) { | 160 synchronized (mLock) { |
107 throw new IllegalStateException("Context Adapter is destroyed."); | 161 if (mUrlRequestContextAdapter == 0) { |
162 throw new IllegalStateException("Context is shutdown."); | |
163 } | |
164 return mUrlRequestContextAdapter; | |
108 } | 165 } |
109 return mUrlRequestContextAdapter; | |
110 } | 166 } |
111 | 167 |
112 /** | 168 /** |
113 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and | 169 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and |
114 * {@link #LOG_VERBOSE}. | 170 * {@link #LOG_VERBOSE}. |
115 */ | 171 */ |
116 private int getLoggingLevel() { | 172 private int getLoggingLevel() { |
117 int loggingLevel; | 173 int loggingLevel; |
118 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) { | 174 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) { |
119 loggingLevel = LOG_VERBOSE; | 175 loggingLevel = LOG_VERBOSE; |
120 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { | 176 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { |
121 loggingLevel = LOG_DEBUG; | 177 loggingLevel = LOG_DEBUG; |
122 } else { | 178 } else { |
123 loggingLevel = LOG_NONE; | 179 loggingLevel = LOG_NONE; |
124 } | 180 } |
125 return loggingLevel; | 181 return loggingLevel; |
126 } | 182 } |
127 | 183 |
128 @SuppressWarnings("unused") | 184 @SuppressWarnings("unused") |
129 @CalledByNative | 185 @CalledByNative |
130 private void initNetworkThread() { | 186 private void initNetworkThread() { |
131 mNetworkThread = Thread.currentThread(); | 187 synchronized (mLock) { |
188 mNetworkThread = Thread.currentThread(); | |
189 } | |
132 Thread.currentThread().setName("ChromiumNet"); | 190 Thread.currentThread().setName("ChromiumNet"); |
133 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); | 191 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
134 } | 192 } |
135 | 193 |
136 // Native methods are implemented in cronet_url_request_context.cc. | 194 // Native methods are implemented in cronet_url_request_context.cc. |
137 private native long nativeCreateRequestContextAdapter(Context context, | 195 private native long nativeCreateRequestContextAdapter(Context context, |
138 String config); | 196 String config); |
139 | 197 |
140 private native void nativeDestroyRequestContextAdapter( | 198 private native void nativeDestroyRequestContextAdapter( |
141 long urlRequestContextAdapter); | 199 long urlRequestContextAdapter); |
142 | 200 |
143 private native void nativeStartNetLogToFile( | 201 private native void nativeStartNetLogToFile( |
144 long urlRequestContextAdapter, String fileName); | 202 long urlRequestContextAdapter, String fileName); |
145 | 203 |
146 private native void nativeStopNetLog(long urlRequestContextAdapter); | 204 private native void nativeStopNetLog(long urlRequestContextAdapter); |
147 | 205 |
148 private native int nativeSetMinLogLevel(int loggingLevel); | 206 private native int nativeSetMinLogLevel(int loggingLevel); |
207 | |
208 private native void nativeInitRequestContextOnMainThread( | |
209 long urlRequestContextAdapter); | |
149 } | 210 } |
OLD | NEW |