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

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 more comments. Created 6 years, 2 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
(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
17 /**
18 * UrlRequest context using Chromium HTTP stack implementation.
19 */
20 @JNINamespace("cronet")
21 public class CronetUrlRequestContext extends UrlRequestContext {
22 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG.
23 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1)
24 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2)
25 static final String LOG_TAG = "ChromiumNetwork";
26
27 private long mUrlRequestContextAdapter = 0;
28 private Thread mNetworkThread;
29
30 public CronetUrlRequestContext(Context context,
31 UrlRequestContextConfig config) {
32 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter(
33 context, getLoggingLevel(), config.toString());
34 if (mUrlRequestContextAdapter == 0) {
35 throw new NullPointerException("Context Adapter creation failed");
36 }
37 }
38
39 @Override
40 public UrlRequest createRequest(String url, UrlRequestListener listener,
41 Executor executor) {
42 return new CronetUrlRequest(this, url,
43 UrlRequest.REQUEST_PRIORITY_MEDIUM, listener, executor);
44 }
45
46 @Override
47 public boolean isEnabled() {
48 return Build.VERSION.SDK_INT >= 14;
49 }
50
51 @Override
52 public String getName() {
53 return "Cronet/" + Version.getVersion();
54 }
55
56 @Override
57 public void shutdown() {
58 nativeReleaseRequestContextAdapter(mUrlRequestContextAdapter);
59 }
60
61 long getCronetUrlRequestContextAdapter() {
62 return mUrlRequestContextAdapter;
63 }
64
65 /**
66 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and
67 * {@link #LOG_VERBOSE}.
68 */
69 private int getLoggingLevel() {
70 int loggingLevel;
71 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) {
72 loggingLevel = LOG_VERBOSE;
73 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) {
74 loggingLevel = LOG_DEBUG;
75 } else {
76 loggingLevel = LOG_NONE;
77 }
78 return loggingLevel;
79 }
80
81 @CalledByNative
82 private void initNetworkThread() {
83 mNetworkThread = Thread.currentThread();
84 Thread.currentThread().setName("ChromiumNet");
85 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
86 }
87
88 // Native methods are implemented in cronet_url_request_context.cc.
89 private native long nativeCreateRequestContextAdapter(Context context,
90 int loggingLevel, String config);
91
92 private native void nativeReleaseRequestContextAdapter(
93 long urlRequestContextAdapter);
94
95 private native void nativeInitializeStatistics();
96
97 private native String nativeGetStatisticsJSON(String filter);
98
99 private native void nativeStartNetLogToFile(
100 long urlRequestContextAdapter, String fileName);
101
102 private native void nativeStopNetLog(long urlRequestContextAdapter);
103 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698