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(); |
| 35 private final ConditionVariable mInitCompleted = new ConditionVariable(false
); |
| 36 private final AtomicInteger mActiveRequestCount = new AtomicInteger(0); |
| 37 |
28 private long mUrlRequestContextAdapter = 0; | 38 private long mUrlRequestContextAdapter = 0; |
29 private Thread mNetworkThread; | 39 private Thread mNetworkThread; |
30 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."); |
| 49 } |
| 50 |
| 51 // Init native Chromium URLRequestContext on main UI thread. |
| 52 Runnable task = new Runnable() { |
| 53 @Override |
| 54 public void run() { |
| 55 synchronized (mLock) { |
| 56 // mUrlRequestContextAdapter is guaranteed to exist until |
| 57 // initialization on main and network threads completes and |
| 58 // initNetworkThread is called back on network thread. |
| 59 nativeInitRequestContextOnMainThread(mUrlRequestContextAdapt
er); |
| 60 } |
| 61 } |
| 62 }; |
| 63 // Run task immediately or post it to the UI thread. |
| 64 if (Looper.getMainLooper() == Looper.myLooper()) { |
| 65 task.run(); |
| 66 } else { |
| 67 new Handler(Looper.getMainLooper()).post(task); |
39 } | 68 } |
40 } | 69 } |
41 | 70 |
42 @Override | 71 @Override |
43 public UrlRequest createRequest(String url, UrlRequestListener listener, | 72 public UrlRequest createRequest(String url, UrlRequestListener listener, |
44 Executor executor) { | 73 Executor executor) { |
45 if (mUrlRequestContextAdapter == 0) { | 74 synchronized (mLock) { |
46 throw new IllegalStateException( | 75 checkHaveAdapter(); |
47 "Cannot create requests on shutdown context."); | 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 checkHaveAdapter(); |
67 "Cannot shutdown with active requests."); | 95 if (mActiveRequestCount.get() != 0) { |
| 96 throw new IllegalStateException( |
| 97 "Cannot shutdown with active requests."); |
| 98 } |
| 99 // Destroying adapter stops the network thread, so it cannot be |
| 100 // called on network thread. |
| 101 if (Thread.currentThread() == mNetworkThread) { |
| 102 throw new IllegalThreadStateException( |
| 103 "Cannot shutdown from network thread."); |
| 104 } |
68 } | 105 } |
69 // Destroying adapter stops the network thread, so it cannot be called | 106 // Wait for init to complete on main and network thread (without lock, |
70 // on network thread. | 107 // so other thread could access it). |
71 if (Thread.currentThread() == mNetworkThread) { | 108 mInitCompleted.block(); |
72 throw new IllegalThreadStateException( | 109 |
73 "Cannot shutdown from network thread."); | 110 synchronized (mLock) { |
| 111 // It is possible that adapter is already destroyed on another threa
d. |
| 112 if (!haveRequestContextAdapter()) { |
| 113 return; |
| 114 } |
| 115 nativeDestroyRequestContextAdapter(mUrlRequestContextAdapter); |
| 116 mUrlRequestContextAdapter = 0; |
74 } | 117 } |
75 nativeDestroyRequestContextAdapter(mUrlRequestContextAdapter); | |
76 mUrlRequestContextAdapter = 0; | |
77 } | 118 } |
78 | 119 |
79 @Override | 120 @Override |
80 public void startNetLogToFile(String fileName) { | 121 public void startNetLogToFile(String fileName) { |
81 nativeStartNetLogToFile(mUrlRequestContextAdapter, fileName); | 122 synchronized (mLock) { |
| 123 checkHaveAdapter(); |
| 124 nativeStartNetLogToFile(mUrlRequestContextAdapter, fileName); |
| 125 } |
82 } | 126 } |
83 | 127 |
84 @Override | 128 @Override |
85 public void stopNetLog() { | 129 public void stopNetLog() { |
86 nativeStopNetLog(mUrlRequestContextAdapter); | 130 synchronized (mLock) { |
| 131 checkHaveAdapter(); |
| 132 nativeStopNetLog(mUrlRequestContextAdapter); |
| 133 } |
87 } | 134 } |
88 | 135 |
89 /** | 136 /** |
90 * Mark request as started to prevent shutdown when there are active | 137 * Mark request as started to prevent shutdown when there are active |
91 * requests. | 138 * requests. |
92 */ | 139 */ |
93 void onRequestStarted(UrlRequest urlRequest) { | 140 void onRequestStarted(UrlRequest urlRequest) { |
94 mActiveRequestCount.incrementAndGet(); | 141 mActiveRequestCount.incrementAndGet(); |
95 } | 142 } |
96 | 143 |
97 /** | 144 /** |
98 * Mark request as completed to allow shutdown when there are no active | 145 * Mark request as completed to allow shutdown when there are no active |
99 * requests. | 146 * requests. |
100 */ | 147 */ |
101 void onRequestDestroyed(UrlRequest urlRequest) { | 148 void onRequestDestroyed(UrlRequest urlRequest) { |
102 mActiveRequestCount.decrementAndGet(); | 149 mActiveRequestCount.decrementAndGet(); |
103 } | 150 } |
104 | 151 |
105 long getUrlRequestContextAdapter() { | 152 long getUrlRequestContextAdapter() { |
106 if (mUrlRequestContextAdapter == 0) { | 153 synchronized (mLock) { |
107 throw new IllegalStateException("Context Adapter is destroyed."); | 154 checkHaveAdapter(); |
| 155 return mUrlRequestContextAdapter; |
108 } | 156 } |
109 return mUrlRequestContextAdapter; | 157 } |
| 158 |
| 159 private void checkHaveAdapter() throws IllegalStateException { |
| 160 if (!haveRequestContextAdapter()) { |
| 161 throw new IllegalStateException("Context is shut down."); |
| 162 } |
| 163 } |
| 164 |
| 165 private boolean haveRequestContextAdapter() { |
| 166 return mUrlRequestContextAdapter != 0; |
110 } | 167 } |
111 | 168 |
112 /** | 169 /** |
113 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and | 170 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and |
114 * {@link #LOG_VERBOSE}. | 171 * {@link #LOG_VERBOSE}. |
115 */ | 172 */ |
116 private int getLoggingLevel() { | 173 private int getLoggingLevel() { |
117 int loggingLevel; | 174 int loggingLevel; |
118 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) { | 175 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) { |
119 loggingLevel = LOG_VERBOSE; | 176 loggingLevel = LOG_VERBOSE; |
120 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { | 177 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { |
121 loggingLevel = LOG_DEBUG; | 178 loggingLevel = LOG_DEBUG; |
122 } else { | 179 } else { |
123 loggingLevel = LOG_NONE; | 180 loggingLevel = LOG_NONE; |
124 } | 181 } |
125 return loggingLevel; | 182 return loggingLevel; |
126 } | 183 } |
127 | 184 |
128 @SuppressWarnings("unused") | 185 @SuppressWarnings("unused") |
129 @CalledByNative | 186 @CalledByNative |
130 private void initNetworkThread() { | 187 private void initNetworkThread() { |
131 mNetworkThread = Thread.currentThread(); | 188 synchronized (mLock) { |
| 189 mNetworkThread = Thread.currentThread(); |
| 190 mInitCompleted.open(); |
| 191 } |
132 Thread.currentThread().setName("ChromiumNet"); | 192 Thread.currentThread().setName("ChromiumNet"); |
133 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); | 193 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
134 } | 194 } |
135 | 195 |
136 // Native methods are implemented in cronet_url_request_context.cc. | 196 // Native methods are implemented in cronet_url_request_context.cc. |
137 private native long nativeCreateRequestContextAdapter(Context context, | 197 private native long nativeCreateRequestContextAdapter(Context context, |
138 String config); | 198 String config); |
139 | 199 |
140 private native void nativeDestroyRequestContextAdapter( | 200 private native void nativeDestroyRequestContextAdapter( |
141 long urlRequestContextAdapter); | 201 long urlRequestContextAdapter); |
142 | 202 |
143 private native void nativeStartNetLogToFile( | 203 private native void nativeStartNetLogToFile( |
144 long urlRequestContextAdapter, String fileName); | 204 long urlRequestContextAdapter, String fileName); |
145 | 205 |
146 private native void nativeStopNetLog(long urlRequestContextAdapter); | 206 private native void nativeStopNetLog(long urlRequestContextAdapter); |
147 | 207 |
148 private native int nativeSetMinLogLevel(int loggingLevel); | 208 private native int nativeSetMinLogLevel(int loggingLevel); |
| 209 |
| 210 private native void nativeInitRequestContextOnMainThread( |
| 211 long urlRequestContextAdapter); |
149 } | 212 } |
OLD | NEW |