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

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: Added testInitAndShutdownOnMainThread. 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();
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;
pauljensen 2015/02/04 14:33:20 nit: part of me feels like this variable would be
mmenke 2015/02/04 16:18:25 We do need the long to actually talk to the adapte
pauljensen 2015/02/04 16:22:11 Oops, I didn't see the use of it to talk to the ad
mef 2015/02/04 17:35:01 Done.
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");
39 } 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);
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 if (mUrlRequestContextAdapter == 0) {
47 "Cannot create requests on shutdown context."); 76 throw new IllegalStateException("Context is shut down.");
77 }
78 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url,
79 UrlRequest.REQUEST_PRIORITY_MEDIUM, listener, executor);
48 } 80 }
49 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url,
50 UrlRequest.REQUEST_PRIORITY_MEDIUM, listener, executor);
51 } 81 }
52 82
53 @Override 83 @Override
54 public boolean isEnabled() { 84 public boolean isEnabled() {
55 return Build.VERSION.SDK_INT >= 14; 85 return Build.VERSION.SDK_INT >= 14;
56 } 86 }
57 87
58 @Override 88 @Override
59 public String getVersionString() { 89 public String getVersionString() {
60 return "Cronet/" + Version.getVersion(); 90 return "Cronet/" + Version.getVersion();
61 } 91 }
62 92
63 @Override 93 @Override
64 public void shutdown() { 94 public void shutdown() {
65 if (mActiveRequestCount.get() != 0) { 95 synchronized (mLock) {
66 throw new IllegalStateException( 96 if (mUrlRequestContextAdapter == 0) {
67 "Cannot shutdown with active requests."); 97 throw new IllegalStateException(
98 "Context is already shutdown.");
99 }
100 if (mActiveRequestCount.get() != 0) {
101 throw new IllegalStateException(
102 "Cannot shutdown with active requests.");
103 }
104 // Destroying adapter stops the network thread, so it cannot be
105 // called on network thread.
106 if (Thread.currentThread() == mNetworkThread) {
107 throw new IllegalThreadStateException(
108 "Cannot shutdown from network thread.");
109 }
68 } 110 }
69 // Destroying adapter stops the network thread, so it cannot be called 111 // Wait for init to complete on main and network thread (without lock,
70 // on network thread. 112 // so other thread could access it).
71 if (Thread.currentThread() == mNetworkThread) { 113 mInitCompleted.block();
72 throw new IllegalThreadStateException( 114
73 "Cannot shutdown from network thread."); 115 synchronized (mLock) {
116 // It is possible that adapter is already destroyed on another threa d.
117 if (mUrlRequestContextAdapter == 0) {
118 return;
119 }
120 nativeDestroyRequestContextAdapter(mUrlRequestContextAdapter);
121 mUrlRequestContextAdapter = 0;
74 } 122 }
75 nativeDestroyRequestContextAdapter(mUrlRequestContextAdapter);
76 mUrlRequestContextAdapter = 0;
77 } 123 }
78 124
79 @Override 125 @Override
80 public void startNetLogToFile(String fileName) { 126 public void startNetLogToFile(String fileName) {
81 nativeStartNetLogToFile(mUrlRequestContextAdapter, fileName); 127 synchronized (mLock) {
128 if (mUrlRequestContextAdapter == 0) {
129 throw new IllegalStateException(
130 "Context is shut down.");
131 }
132 nativeStartNetLogToFile(mUrlRequestContextAdapter, fileName);
133 }
82 } 134 }
83 135
84 @Override 136 @Override
85 public void stopNetLog() { 137 public void stopNetLog() {
86 nativeStopNetLog(mUrlRequestContextAdapter); 138 synchronized (mLock) {
139 if (mUrlRequestContextAdapter == 0) {
140 throw new IllegalStateException(
141 "Context is shut down.");
142 }
143 nativeStopNetLog(mUrlRequestContextAdapter);
144 }
87 } 145 }
88 146
89 /** 147 /**
90 * Mark request as started to prevent shutdown when there are active 148 * Mark request as started to prevent shutdown when there are active
91 * requests. 149 * requests.
92 */ 150 */
93 void onRequestStarted(UrlRequest urlRequest) { 151 void onRequestStarted(UrlRequest urlRequest) {
94 mActiveRequestCount.incrementAndGet(); 152 mActiveRequestCount.incrementAndGet();
95 } 153 }
96 154
97 /** 155 /**
98 * Mark request as completed to allow shutdown when there are no active 156 * Mark request as completed to allow shutdown when there are no active
99 * requests. 157 * requests.
100 */ 158 */
101 void onRequestDestroyed(UrlRequest urlRequest) { 159 void onRequestDestroyed(UrlRequest urlRequest) {
102 mActiveRequestCount.decrementAndGet(); 160 mActiveRequestCount.decrementAndGet();
103 } 161 }
104 162
105 long getUrlRequestContextAdapter() { 163 long getUrlRequestContextAdapter() {
106 if (mUrlRequestContextAdapter == 0) { 164 synchronized (mLock) {
107 throw new IllegalStateException("Context Adapter is destroyed."); 165 if (mUrlRequestContextAdapter == 0) {
166 throw new IllegalStateException("Context is shut down.");
167 }
168 return mUrlRequestContextAdapter;
108 } 169 }
109 return mUrlRequestContextAdapter;
110 } 170 }
111 171
112 /** 172 /**
113 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and 173 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and
114 * {@link #LOG_VERBOSE}. 174 * {@link #LOG_VERBOSE}.
115 */ 175 */
116 private int getLoggingLevel() { 176 private int getLoggingLevel() {
117 int loggingLevel; 177 int loggingLevel;
118 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) { 178 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) {
119 loggingLevel = LOG_VERBOSE; 179 loggingLevel = LOG_VERBOSE;
120 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { 180 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) {
121 loggingLevel = LOG_DEBUG; 181 loggingLevel = LOG_DEBUG;
122 } else { 182 } else {
123 loggingLevel = LOG_NONE; 183 loggingLevel = LOG_NONE;
124 } 184 }
125 return loggingLevel; 185 return loggingLevel;
126 } 186 }
127 187
128 @SuppressWarnings("unused") 188 @SuppressWarnings("unused")
129 @CalledByNative 189 @CalledByNative
130 private void initNetworkThread() { 190 private void initNetworkThread() {
131 mNetworkThread = Thread.currentThread(); 191 synchronized (mLock) {
192 mNetworkThread = Thread.currentThread();
193 mInitCompleted.open();
194 }
132 Thread.currentThread().setName("ChromiumNet"); 195 Thread.currentThread().setName("ChromiumNet");
133 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); 196 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
134 } 197 }
135 198
136 // Native methods are implemented in cronet_url_request_context.cc. 199 // Native methods are implemented in cronet_url_request_context.cc.
137 private native long nativeCreateRequestContextAdapter(Context context, 200 private native long nativeCreateRequestContextAdapter(Context context,
138 String config); 201 String config);
139 202
140 private native void nativeDestroyRequestContextAdapter( 203 private native void nativeDestroyRequestContextAdapter(
141 long urlRequestContextAdapter); 204 long urlRequestContextAdapter);
142 205
143 private native void nativeStartNetLogToFile( 206 private native void nativeStartNetLogToFile(
144 long urlRequestContextAdapter, String fileName); 207 long urlRequestContextAdapter, String fileName);
145 208
146 private native void nativeStopNetLog(long urlRequestContextAdapter); 209 private native void nativeStopNetLog(long urlRequestContextAdapter);
147 210
148 private native int nativeSetMinLogLevel(int loggingLevel); 211 private native int nativeSetMinLogLevel(int loggingLevel);
212
213 private native void nativeInitRequestContextOnMainThread(
214 long urlRequestContextAdapter);
149 } 215 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698