OLD | NEW |
---|---|
(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 long getCronetUrlRequestContextAdapter() { | |
xunjieli
2014/10/23 14:04:33
nit: is this method private or public?
mef
2014/10/24 03:31:45
Package as it needs to be accessed by CronetUrlReq
| |
57 return mUrlRequestContextAdapter; | |
58 } | |
59 | |
60 /** | |
61 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and | |
62 * {@link #LOG_VERBOSE}. | |
63 */ | |
64 private int getLoggingLevel() { | |
65 int loggingLevel; | |
66 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) { | |
67 loggingLevel = LOG_VERBOSE; | |
68 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { | |
69 loggingLevel = LOG_DEBUG; | |
70 } else { | |
71 loggingLevel = LOG_NONE; | |
72 } | |
73 return loggingLevel; | |
74 } | |
75 | |
76 @CalledByNative | |
77 private void initNetworkThread() { | |
78 mNetworkThread = Thread.currentThread(); | |
79 Thread.currentThread().setName("ChromiumNet"); | |
80 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); | |
81 } | |
82 | |
83 // Native methods are implemented in cronet_url_request_context.cc. | |
84 private native long nativeCreateRequestContextAdapter(Context context, | |
85 int loggingLevel, String config); | |
86 | |
87 private native void nativeReleaseRequestContextAdapter( | |
88 long urlRequestContextAdapter); | |
mmenke
2014/10/23 16:26:01
This is never called.
Seems like we have two opti
mef
2014/10/24 03:31:45
Previous interface did it in finalize, but I guess
mmenke
2014/10/24 03:46:19
My suggestion is go with the exception, and if we
mef
2014/10/24 19:57:55
Sounds good. We can also set a 'shutdown' flag, so
| |
89 | |
90 private native void nativeInitializeStatistics(); | |
91 | |
92 private native String nativeGetStatisticsJSON(String filter); | |
93 | |
94 private native void nativeStartNetLogToFile( | |
95 long urlRequestContextAdapter, String fileName); | |
96 | |
97 private native void nativeStopNetLog(long urlRequestContextAdapter); | |
98 } | |
OLD | NEW |