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.impl; | |
6 | |
7 import android.content.Context; | |
8 import android.os.Handler; | |
9 import android.os.Looper; | |
10 import android.os.Process; | |
11 import android.util.Log; | |
12 | |
13 import org.chromium.base.VisibleForTesting; | |
14 import org.chromium.base.annotations.CalledByNative; | |
15 import org.chromium.base.annotations.JNINamespace; | |
16 import org.chromium.net.CronetEngine; | |
17 | |
18 /** | |
19 * Provides context for the native HTTP operations. | |
20 * @deprecated Use {@link CronetEngine} instead. | |
21 */ | |
22 @JNINamespace("cronet") | |
23 @Deprecated | |
24 public class ChromiumUrlRequestContext { | |
25 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. | |
26 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) | |
27 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) | |
28 static final String LOG_TAG = "ChromiumNetwork"; | |
29 | |
30 /** | |
31 * Native adapter object, owned by ChromiumUrlRequestContext. | |
32 */ | |
33 private long mChromiumUrlRequestContextAdapter; | |
34 | |
35 /** | |
36 * Constructor. | |
37 */ | |
38 protected ChromiumUrlRequestContext( | |
39 final Context context, String userAgent, CronetEngine.Builder config
) { | |
40 CronetLibraryLoader.ensureInitialized(context, config); | |
41 mChromiumUrlRequestContextAdapter = nativeCreateRequestContextAdapter(us
erAgent, | |
42 getLoggingLevel(), | |
43 CronetUrlRequestContext.createNativeUrlRequestContextConfig(cont
ext, config)); | |
44 if (mChromiumUrlRequestContextAdapter == 0) { | |
45 throw new NullPointerException("Context Adapter creation failed"); | |
46 } | |
47 // Post a task to UI thread to init native Chromium URLRequestContext. | |
48 // TODO(xunjieli): This constructor is not supposed to be invoked on | |
49 // the main thread. Consider making the following code into a blocking | |
50 // API to handle the case where we are already on main thread. | |
51 Runnable task = new Runnable() { | |
52 public void run() { | |
53 nativeInitRequestContextOnMainThread(mChromiumUrlRequestContextA
dapter); | |
54 } | |
55 }; | |
56 new Handler(Looper.getMainLooper()).post(task); | |
57 } | |
58 | |
59 /** | |
60 * Returns the version of this network stack formatted as N.N.N.N/X where | |
61 * N.N.N.N is the version of Chromium and X is the revision number. | |
62 */ | |
63 public static String getVersion() { | |
64 return ImplVersion.getVersion(); | |
65 } | |
66 | |
67 /** | |
68 * Initializes statistics recorder. | |
69 */ | |
70 public void initializeStatistics() { | |
71 nativeInitializeStatistics(); | |
72 } | |
73 | |
74 /** | |
75 * Gets current statistics recorded since |initializeStatistics| with | |
76 * |filter| as a substring as JSON text (an empty |filter| will include all | |
77 * registered histograms). | |
78 */ | |
79 public String getStatisticsJSON(String filter) { | |
80 return nativeGetStatisticsJSON(filter); | |
81 } | |
82 | |
83 /** | |
84 * Starts NetLog logging to a file. The NetLog capture mode is either | |
85 * NetLogCaptureMode::Default() or NetLogCaptureMode::IncludeSocketBytes(). | |
86 * The IncludeSocketBytes() mode includes basic events, user cookies, | |
87 * credentials and all transferred bytes in the log. | |
88 * @param fileName The complete file path. It must not be empty. If file | |
89 * exists, it is truncated before starting. If actively logging, | |
90 * this method is ignored. | |
91 * @param logAll {@code true} to use the | |
92 * NetLogCaptureMode::IncludeSocketBytes() logging level. If | |
93 * false, NetLogCaptureMode::Default() is used instead. | |
94 */ | |
95 public void startNetLogToFile(String fileName, boolean logAll) { | |
96 nativeStartNetLogToFile(mChromiumUrlRequestContextAdapter, fileName, log
All); | |
97 } | |
98 | |
99 /** | |
100 * Stops NetLog logging and flushes file to disk. If a logging session is | |
101 * not in progress, this call is ignored. | |
102 */ | |
103 public void stopNetLog() { | |
104 nativeStopNetLog(mChromiumUrlRequestContextAdapter); | |
105 } | |
106 | |
107 @CalledByNative | |
108 private void initNetworkThread() { | |
109 Thread.currentThread().setName("ChromiumNet"); | |
110 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); | |
111 } | |
112 | |
113 @Override | |
114 protected void finalize() throws Throwable { | |
115 if (mChromiumUrlRequestContextAdapter != 0) { | |
116 nativeReleaseRequestContextAdapter(mChromiumUrlRequestContextAdapter
); | |
117 } | |
118 super.finalize(); | |
119 } | |
120 | |
121 @VisibleForTesting | |
122 public long getUrlRequestContextAdapter() { | |
123 return mChromiumUrlRequestContextAdapter; | |
124 } | |
125 | |
126 /** | |
127 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and | |
128 * {@link #LOG_VERBOSE}. | |
129 */ | |
130 private int getLoggingLevel() { | |
131 int loggingLevel; | |
132 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) { | |
133 loggingLevel = LOG_VERBOSE; | |
134 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { | |
135 loggingLevel = LOG_DEBUG; | |
136 } else { | |
137 loggingLevel = LOG_NONE; | |
138 } | |
139 return loggingLevel; | |
140 } | |
141 | |
142 // Returns an instance ChromiumUrlRequestContextAdapter to be stored in | |
143 // mChromiumUrlRequestContextAdapter. | |
144 private native long nativeCreateRequestContextAdapter( | |
145 String userAgent, int loggingLevel, long config); | |
146 | |
147 private native void nativeReleaseRequestContextAdapter(long chromiumUrlReque
stContextAdapter); | |
148 | |
149 private native void nativeInitializeStatistics(); | |
150 | |
151 private native String nativeGetStatisticsJSON(String filter); | |
152 | |
153 private native void nativeStartNetLogToFile( | |
154 long chromiumUrlRequestContextAdapter, String fileName, boolean logA
ll); | |
155 | |
156 private native void nativeStopNetLog(long chromiumUrlRequestContextAdapter); | |
157 | |
158 private native void nativeInitRequestContextOnMainThread(long chromiumUrlReq
uestContextAdapter); | |
159 } | |
OLD | NEW |