Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: components/cronet/android/java/src/org/chromium/net/CronetUrlRequestContext.java

Issue 726013002: [Cronet] Hook up library loader, system proxy and network change notifier to async api. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix the deadlock. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 );
xunjieli 2015/02/03 18:37:59 nit: group final fields together?
mef 2015/02/03 20:08:55 Done.
38
30 private AtomicInteger mActiveRequestCount = new AtomicInteger(0); 39 private AtomicInteger mActiveRequestCount = new AtomicInteger(0);
xunjieli 2015/02/03 18:37:59 can this be final?
mef 2015/02/03 20:08:55 Done.
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 // Init native Chromium URLRequestContext on main UI thread.
52 Runnable task = new Runnable() {
53 public void run() {
xunjieli 2015/02/03 18:37:59 nit: add @Override?
mef 2015/02/03 20:08:55 Done.
54 synchronized (mLock) {
55 // mUrlRequestContextAdapter is guaranteed to exist until
56 // initialization on main and network threads completes and
57 // initNetworkThread is called back on network thread.
58 nativeInitRequestContextOnMainThread(mUrlRequestContextAdapt er);
59 }
60 }
61 };
62 // Run task immediately or post it to the UI thread.
63 if (Looper.getMainLooper() == Looper.myLooper()) {
64 task.run();
65 } else {
66 new Handler(Looper.getMainLooper()).post(task);
67 }
40 } 68 }
41 69
42 @Override 70 @Override
43 public UrlRequest createRequest(String url, UrlRequestListener listener, 71 public UrlRequest createRequest(String url, UrlRequestListener listener,
44 Executor executor) { 72 Executor executor) {
45 if (mUrlRequestContextAdapter == 0) { 73 synchronized (mLock) {
46 throw new IllegalStateException( 74 if (mUrlRequestContextAdapter == 0) {
47 "Cannot create requests on shutdown context."); 75 throw new IllegalStateException("Context is shut down.");
76 }
77 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url,
78 UrlRequest.REQUEST_PRIORITY_MEDIUM, listener, executor);
48 } 79 }
49 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url,
50 UrlRequest.REQUEST_PRIORITY_MEDIUM, listener, executor);
51 } 80 }
52 81
53 @Override 82 @Override
54 public boolean isEnabled() { 83 public boolean isEnabled() {
55 return Build.VERSION.SDK_INT >= 14; 84 return Build.VERSION.SDK_INT >= 14;
56 } 85 }
57 86
58 @Override 87 @Override
59 public String getVersionString() { 88 public String getVersionString() {
60 return "Cronet/" + Version.getVersion(); 89 return "Cronet/" + Version.getVersion();
61 } 90 }
62 91
63 @Override 92 @Override
64 public void shutdown() { 93 public void shutdown() {
65 if (mActiveRequestCount.get() != 0) { 94 synchronized (mLock) {
66 throw new IllegalStateException( 95 if (mUrlRequestContextAdapter == 0) {
67 "Cannot shutdown with active requests."); 96 throw new IllegalStateException(
97 "Context is already shutdown.");
98 }
99 if (mActiveRequestCount.get() != 0) {
100 throw new IllegalStateException(
101 "Cannot shutdown with active requests.");
102 }
103 // Destroying adapter stops the network thread, so it cannot be
104 // called on network thread.
105 if (Thread.currentThread() == mNetworkThread) {
106 throw new IllegalThreadStateException(
107 "Cannot shutdown from network thread.");
108 }
68 } 109 }
69 // Destroying adapter stops the network thread, so it cannot be called 110 // Wait for init to complete on main and network thread (without lock,
70 // on network thread. 111 // so other thread could access it).
71 if (Thread.currentThread() == mNetworkThread) { 112 mInitCompleted.block();
72 throw new IllegalThreadStateException( 113
73 "Cannot shutdown from network thread."); 114 synchronized (mLock) {
115 // It is possible that adapter is already destroyed on another threa d.
116 if (mUrlRequestContextAdapter == 0) {
117 return;
118 }
119 nativeDestroyRequestContextAdapter(mUrlRequestContextAdapter);
120 mUrlRequestContextAdapter = 0;
74 } 121 }
75 nativeDestroyRequestContextAdapter(mUrlRequestContextAdapter);
76 mUrlRequestContextAdapter = 0;
77 } 122 }
78 123
79 @Override 124 @Override
80 public void startNetLogToFile(String fileName) { 125 public void startNetLogToFile(String fileName) {
81 nativeStartNetLogToFile(mUrlRequestContextAdapter, fileName); 126 synchronized (mLock) {
127 if (mUrlRequestContextAdapter == 0) {
128 throw new IllegalStateException(
129 "Context is shut down.");
130 }
131 nativeStartNetLogToFile(mUrlRequestContextAdapter, fileName);
132 }
82 } 133 }
83 134
84 @Override 135 @Override
85 public void stopNetLog() { 136 public void stopNetLog() {
86 nativeStopNetLog(mUrlRequestContextAdapter); 137 synchronized (mLock) {
138 if (mUrlRequestContextAdapter == 0) {
139 throw new IllegalStateException(
140 "Context is shut down.");
141 }
142 nativeStopNetLog(mUrlRequestContextAdapter);
143 }
87 } 144 }
88 145
89 /** 146 /**
90 * Mark request as started to prevent shutdown when there are active 147 * Mark request as started to prevent shutdown when there are active
91 * requests. 148 * requests.
92 */ 149 */
93 void onRequestStarted(UrlRequest urlRequest) { 150 void onRequestStarted(UrlRequest urlRequest) {
94 mActiveRequestCount.incrementAndGet(); 151 mActiveRequestCount.incrementAndGet();
95 } 152 }
96 153
97 /** 154 /**
98 * Mark request as completed to allow shutdown when there are no active 155 * Mark request as completed to allow shutdown when there are no active
99 * requests. 156 * requests.
100 */ 157 */
101 void onRequestDestroyed(UrlRequest urlRequest) { 158 void onRequestDestroyed(UrlRequest urlRequest) {
102 mActiveRequestCount.decrementAndGet(); 159 mActiveRequestCount.decrementAndGet();
103 } 160 }
104 161
105 long getUrlRequestContextAdapter() { 162 long getUrlRequestContextAdapter() {
106 if (mUrlRequestContextAdapter == 0) { 163 synchronized (mLock) {
107 throw new IllegalStateException("Context Adapter is destroyed."); 164 if (mUrlRequestContextAdapter == 0) {
165 throw new IllegalStateException("Context is shut down.");
166 }
167 return mUrlRequestContextAdapter;
108 } 168 }
109 return mUrlRequestContextAdapter;
110 } 169 }
111 170
112 /** 171 /**
113 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and 172 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and
114 * {@link #LOG_VERBOSE}. 173 * {@link #LOG_VERBOSE}.
115 */ 174 */
116 private int getLoggingLevel() { 175 private int getLoggingLevel() {
117 int loggingLevel; 176 int loggingLevel;
118 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) { 177 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) {
119 loggingLevel = LOG_VERBOSE; 178 loggingLevel = LOG_VERBOSE;
120 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { 179 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) {
121 loggingLevel = LOG_DEBUG; 180 loggingLevel = LOG_DEBUG;
122 } else { 181 } else {
123 loggingLevel = LOG_NONE; 182 loggingLevel = LOG_NONE;
124 } 183 }
125 return loggingLevel; 184 return loggingLevel;
126 } 185 }
127 186
128 @SuppressWarnings("unused") 187 @SuppressWarnings("unused")
129 @CalledByNative 188 @CalledByNative
130 private void initNetworkThread() { 189 private void initNetworkThread() {
131 mNetworkThread = Thread.currentThread(); 190 synchronized (mLock) {
191 mNetworkThread = Thread.currentThread();
192 mInitCompleted.open();
193 }
132 Thread.currentThread().setName("ChromiumNet"); 194 Thread.currentThread().setName("ChromiumNet");
133 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); 195 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
134 } 196 }
135 197
136 // Native methods are implemented in cronet_url_request_context.cc. 198 // Native methods are implemented in cronet_url_request_context.cc.
137 private native long nativeCreateRequestContextAdapter(Context context, 199 private native long nativeCreateRequestContextAdapter(Context context,
138 String config); 200 String config);
139 201
140 private native void nativeDestroyRequestContextAdapter( 202 private native void nativeDestroyRequestContextAdapter(
141 long urlRequestContextAdapter); 203 long urlRequestContextAdapter);
142 204
143 private native void nativeStartNetLogToFile( 205 private native void nativeStartNetLogToFile(
144 long urlRequestContextAdapter, String fileName); 206 long urlRequestContextAdapter, String fileName);
145 207
146 private native void nativeStopNetLog(long urlRequestContextAdapter); 208 private native void nativeStopNetLog(long urlRequestContextAdapter);
147 209
148 private native int nativeSetMinLogLevel(int loggingLevel); 210 private native int nativeSetMinLogLevel(int loggingLevel);
211
212 private native void nativeInitRequestContextOnMainThread(
213 long urlRequestContextAdapter);
149 } 214 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698