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

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: Address comments, moar scoped pointers. 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 getName() {
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
70 nativeDestroyRequestContextAdapter(mUrlRequestContextAdapter);
71 mUrlRequestContextAdapter = 0;
72 }
73
74 /**
75 * Mark request as started to prevent shutdown when there are active
76 * requests.
77 */
78 void onRequestStarted(UrlRequest urlRequest) {
79 mActiveRequestCount.incrementAndGet();
80 }
81
82 /**
83 * Mark request as completed to allow shutdown when there are no active
84 * requests.
85 */
86 void onRequestDestroyed(UrlRequest urlRequest) {
87 mActiveRequestCount.decrementAndGet();
88 }
89
90 long getUrlRequestContextAdapter() {
91 if (mUrlRequestContextAdapter == 0) {
92 throw new IllegalStateException("Context Adapter is destroyed.");
93 }
94 return mUrlRequestContextAdapter;
95 }
96
97 /**
98 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and
99 * {@link #LOG_VERBOSE}.
100 */
101 private int getLoggingLevel() {
102 int loggingLevel;
103 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) {
104 loggingLevel = LOG_VERBOSE;
105 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) {
106 loggingLevel = LOG_DEBUG;
107 } else {
108 loggingLevel = LOG_NONE;
109 }
110 return loggingLevel;
111 }
112
113 @CalledByNative
114 private void initNetworkThread() {
115 mNetworkThread = Thread.currentThread();
116 Thread.currentThread().setName("ChromiumNet");
117 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
118 }
119
120 // Native methods are implemented in cronet_url_request_context.cc.
121 private native long nativeCreateRequestContextAdapter(Context context,
122 String config);
123
124 private native void nativeDestroyRequestContextAdapter(
125 long urlRequestContextAdapter);
126
127 private native void nativeInitializeStatistics();
128
129 private native String nativeGetStatisticsJSON(String filter);
130
131 private native void nativeStartNetLogToFile(
132 long urlRequestContextAdapter, String fileName);
133
134 private native void nativeStopNetLog(long urlRequestContextAdapter);
135
136 private native int nativeSetMinLogLevel(int loggingLevel);
137 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698