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

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 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter(
35 context, getLoggingLevel(), config.toString());
36 if (mUrlRequestContextAdapter == 0) {
37 throw new NullPointerException("Context Adapter creation failed");
38 }
39 }
40
41 @Override
42 public UrlRequest createRequest(String url, UrlRequestListener listener,
43 Executor executor) {
44 if (mUrlRequestContextAdapter == 0) {
45 throw new IllegalStateException(
46 "Cannot create requests on shutdown context.");
47 }
48 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url,
49 UrlRequest.REQUEST_PRIORITY_MEDIUM, listener, executor);
50 }
51
52 @Override
53 public boolean isEnabled() {
54 return Build.VERSION.SDK_INT >= 14;
55 }
56
57 @Override
58 public String getName() {
59 return "Cronet/" + Version.getVersion();
60 }
61
62 @Override
63 public void shutdown() {
64 if (mActiveRequestCount.get() != 0) {
65 throw new IllegalStateException(
66 "Cannot shutdown with active requests.");
67 }
68
69 nativeDestroyRequestContextAdapter(mUrlRequestContextAdapter);
70 mUrlRequestContextAdapter = 0;
71 }
72
73 /**
74 * Mark request as started to prevent shutdown when there are active
75 * requests.
76 */
77 void onRequestStarted(UrlRequest urlRequest) {
78 mActiveRequestCount.incrementAndGet();
79 }
80
81 /**
82 * Mark request as completed to allow shutdown when there are no active
83 * requests.
84 */
85 void onRequestDestroyed(UrlRequest urlRequest) {
86 mActiveRequestCount.decrementAndGet();
87 }
88
89 long getUrlRequestContextAdapter() {
90 if (mUrlRequestContextAdapter == 0) {
91 throw new IllegalStateException("Context Adapter is destroyed.");
92 }
93 return mUrlRequestContextAdapter;
94 }
95
96 /**
97 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and
98 * {@link #LOG_VERBOSE}.
99 */
100 private int getLoggingLevel() {
101 int loggingLevel;
102 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) {
103 loggingLevel = LOG_VERBOSE;
104 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) {
105 loggingLevel = LOG_DEBUG;
106 } else {
107 loggingLevel = LOG_NONE;
108 }
109 return loggingLevel;
110 }
111
112 @CalledByNative
113 private void initNetworkThread() {
114 mNetworkThread = Thread.currentThread();
115 Thread.currentThread().setName("ChromiumNet");
116 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
117 }
118
119 // Native methods are implemented in cronet_url_request_context.cc.
120 private native long nativeCreateRequestContextAdapter(Context context,
121 int loggingLevel, String config);
122
123 private native void nativeDestroyRequestContextAdapter(
124 long urlRequestContextAdapter);
125
126 private native void nativeInitializeStatistics();
127
128 private native String nativeGetStatisticsJSON(String filter);
129
130 private native void nativeStartNetLogToFile(
131 long urlRequestContextAdapter, String fileName);
132
133 private native void nativeStopNetLog(long urlRequestContextAdapter);
134 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698