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

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

Issue 586143002: Initial implementation of Cronet Async API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync Created 6 years, 1 month 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
(Empty)
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
3 // found in the LICENSE file.
4
5 package org.chromium.net;
6
7 import android.content.Context;
8 import android.os.Build;
9 import android.os.Process;
10 import android.util.Log;
11
12 import org.chromium.base.CalledByNative;
13 import org.chromium.base.JNINamespace;
14
15 import java.util.concurrent.Executor;
16 import java.util.concurrent.atomic.AtomicInteger;
17
18 /**
19 * UrlRequest context using Chromium HTTP stack implementation.
20 */
21 @JNINamespace("cronet")
22 public class CronetUrlRequestContext extends UrlRequestContext {
23 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG.
24 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)
26 static final String LOG_TAG = "ChromiumNetwork";
27
28 private long mUrlRequestContextAdapter = 0;
29 private Thread mNetworkThread;
30 private AtomicInteger mActiveRequestCount = new AtomicInteger(0);
31
32 public CronetUrlRequestContext(Context context,
33 UrlRequestContextConfig config) {
34 nativeSetMinLogLevel(getLoggingLevel());
35 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter(
36 context, config.toString());
37 if (mUrlRequestContextAdapter == 0) {
38 throw new NullPointerException("Context Adapter creation failed");
39 }
40 }
41
42 @Override
43 public UrlRequest createRequest(String url, UrlRequestListener listener,
44 Executor executor) {
45 if (mUrlRequestContextAdapter == 0) {
46 throw new IllegalStateException(
47 "Cannot create requests on shutdown context.");
48 }
49 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url,
50 UrlRequest.REQUEST_PRIORITY_MEDIUM, listener, executor);
51 }
52
53 @Override
54 public boolean isEnabled() {
55 return Build.VERSION.SDK_INT >= 14;
56 }
57
58 @Override
59 public String getVersionString() {
60 return "Cronet/" + Version.getVersion();
61 }
62
63 @Override
64 public void shutdown() {
65 if (mActiveRequestCount.get() != 0) {
66 throw new IllegalStateException(
67 "Cannot shutdown with active requests.");
68 }
69 // Destroying adapter stops the network thread, so it cannot be called
70 // on network thread.
71 if (Thread.currentThread() == mNetworkThread) {
72 throw new IllegalThreadStateException(
73 "Cannot shutdown from network thread.");
74 }
75 nativeDestroyRequestContextAdapter(mUrlRequestContextAdapter);
76 mUrlRequestContextAdapter = 0;
77 }
78
79 /**
80 * Mark request as started to prevent shutdown when there are active
81 * requests.
82 */
83 void onRequestStarted(UrlRequest urlRequest) {
84 mActiveRequestCount.incrementAndGet();
85 }
86
87 /**
88 * Mark request as completed to allow shutdown when there are no active
89 * requests.
90 */
91 void onRequestDestroyed(UrlRequest urlRequest) {
92 mActiveRequestCount.decrementAndGet();
93 }
94
95 long getUrlRequestContextAdapter() {
96 if (mUrlRequestContextAdapter == 0) {
97 throw new IllegalStateException("Context Adapter is destroyed.");
98 }
99 return mUrlRequestContextAdapter;
100 }
101
102 /**
103 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and
104 * {@link #LOG_VERBOSE}.
105 */
106 private int getLoggingLevel() {
107 int loggingLevel;
108 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) {
109 loggingLevel = LOG_VERBOSE;
110 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) {
111 loggingLevel = LOG_DEBUG;
112 } else {
113 loggingLevel = LOG_NONE;
114 }
115 return loggingLevel;
116 }
117
118 @SuppressWarnings("unused")
119 @CalledByNative
120 private void initNetworkThread() {
121 mNetworkThread = Thread.currentThread();
122 Thread.currentThread().setName("ChromiumNet");
123 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
124 }
125
126 // Native methods are implemented in cronet_url_request_context.cc.
127 private native long nativeCreateRequestContextAdapter(Context context,
128 String config);
129
130 private native void nativeDestroyRequestContextAdapter(
131 long urlRequestContextAdapter);
132
133 private native void nativeStartNetLogToFile(
134 long urlRequestContextAdapter, String fileName);
135
136 private native void nativeStopNetLog(long urlRequestContextAdapter);
137
138 private native int nativeSetMinLogLevel(int loggingLevel);
139 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698