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.
5 package org.chromium.net;
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;
13 import org.chromium.base.CalledByNative;
14 import org.chromium.base.JNINamespace;
17 * Provides context for the native HTTP operations.
19 @JNINamespace("cronet")
20 public class ChromiumUrlRequestContext {
21 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG.
22 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1)
23 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2)
24 static final String LOG_TAG = "ChromiumNetwork";
27 * Native adapter object, owned by ChromiumUrlRequestContext.
29 private long mChromiumUrlRequestContextAdapter;
34 protected ChromiumUrlRequestContext(final Context context, String userAgent,
36 mChromiumUrlRequestContextAdapter = nativeCreateRequestContextAdapter(
37 context, userAgent, getLoggingLevel(), config);
38 if (mChromiumUrlRequestContextAdapter == 0) {
39 throw new NullPointerException("Context Adapter creation failed");
41 // Post a task to UI thread to init native Chromium URLRequestContext.
42 // TODO(xunjieli): This constructor is not supposed to be invoked on
43 // the main thread. Consider making the following code into a blocking
44 // API to handle the case where we are already on main thread.
45 Runnable task = new Runnable() {
47 NetworkChangeNotifier.init(context);
48 // Registers to always receive network notifications. Note that
49 // this call is fine for Cronet because Cronet embedders do not
50 // have API access to create network change observers. Existing
51 // observers in the net stack do not perform expensive work.
52 NetworkChangeNotifier.registerToReceiveNotificationsAlways();
53 nativeInitRequestContextOnMainThread(
54 mChromiumUrlRequestContextAdapter);
57 new Handler(Looper.getMainLooper()).post(task);
61 * Returns the version of this network stack formatted as N.N.N.N/X where
62 * N.N.N.N is the version of Chromium and X is the revision number.
64 public static String getVersion() {
65 return Version.getVersion();
69 * Initializes statistics recorder.
71 public void initializeStatistics() {
72 nativeInitializeStatistics();
76 * Gets current statistics recorded since |initializeStatistics| with
77 * |filter| as a substring as JSON text (an empty |filter| will include all
78 * registered histograms).
80 public String getStatisticsJSON(String filter) {
81 return nativeGetStatisticsJSON(filter);
85 * Starts NetLog logging to a file named |fileName| in the
86 * application temporary directory. |fileName| must not be empty. Log level
87 * is LOG_ALL_BUT_BYTES. If the file exists it is truncated before starting.
88 * If actively logging the call is ignored.
90 public void startNetLogToFile(String fileName) {
91 nativeStartNetLogToFile(mChromiumUrlRequestContextAdapter, fileName);
95 * Stops NetLog logging and flushes file to disk. If a logging session is
96 * not in progress this call is ignored.
98 public void stopNetLog() {
99 nativeStopNetLog(mChromiumUrlRequestContextAdapter);
103 private void initNetworkThread() {
104 Thread.currentThread().setName("ChromiumNet");
105 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
109 protected void finalize() throws Throwable {
110 nativeReleaseRequestContextAdapter(mChromiumUrlRequestContextAdapter);
114 protected long getChromiumUrlRequestContextAdapter() {
115 return mChromiumUrlRequestContextAdapter;
119 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and
120 * {@link #LOG_VERBOSE}.
122 private int getLoggingLevel() {
124 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) {
125 loggingLevel = LOG_VERBOSE;
126 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) {
127 loggingLevel = LOG_DEBUG;
129 loggingLevel = LOG_NONE;
134 // Returns an instance ChromiumUrlRequestContextAdapter to be stored in
135 // mChromiumUrlRequestContextAdapter.
136 private native long nativeCreateRequestContextAdapter(Context context,
137 String userAgent, int loggingLevel, String config);
139 private native void nativeReleaseRequestContextAdapter(
140 long chromiumUrlRequestContextAdapter);
142 private native void nativeInitializeStatistics();
144 private native String nativeGetStatisticsJSON(String filter);
146 private native void nativeStartNetLogToFile(
147 long chromiumUrlRequestContextAdapter, String fileName);
149 private native void nativeStopNetLog(long chromiumUrlRequestContextAdapter);
151 private native void nativeInitRequestContextOnMainThread(
152 long chromiumUrlRequestContextAdapter);