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.ConditionVariable;
9 import android.os.Process;
10 import android.util.Log;
12 import org.chromium.base.CalledByNative;
13 import org.chromium.base.JNINamespace;
16 * Provides context for the native HTTP operations.
18 @JNINamespace("cronet")
19 public class ChromiumUrlRequestContext {
20 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG.
21 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1)
22 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2)
23 static final String LOG_TAG = "ChromiumNetwork";
26 * Native adapter object, owned by ChromiumUrlRequestContext.
28 private long mChromiumUrlRequestContextAdapter;
30 private final ConditionVariable mStarted = new ConditionVariable();
36 protected ChromiumUrlRequestContext(Context context, String userAgent,
38 mChromiumUrlRequestContextAdapter = nativeCreateRequestContextAdapter(
39 context, userAgent, getLoggingLevel(), config);
40 if (mChromiumUrlRequestContextAdapter == 0)
41 throw new NullPointerException("Context Adapter creation failed");
43 // TODO(mef): Revisit the need of block here.
48 * Returns the version of this network stack formatted as N.N.N.N/X where
49 * N.N.N.N is the version of Chromium and X is the revision number.
51 public static String getVersion() {
52 return Version.getVersion();
56 * Initializes statistics recorder.
58 public void initializeStatistics() {
59 nativeInitializeStatistics();
63 * Gets current statistics recorded since |initializeStatistics| with
64 * |filter| as a substring as JSON text (an empty |filter| will include all
65 * registered histograms).
67 public String getStatisticsJSON(String filter) {
68 return nativeGetStatisticsJSON(filter);
72 * Starts NetLog logging to a file named |fileName| in the
73 * application temporary directory. |fileName| must not be empty. Log level
74 * is LOG_ALL_BUT_BYTES. If the file exists it is truncated before starting.
75 * If actively logging the call is ignored.
77 public void startNetLogToFile(String fileName) {
78 nativeStartNetLogToFile(mChromiumUrlRequestContextAdapter, fileName);
82 * Stops NetLog logging and flushes file to disk. If a logging session is
83 * not in progress this call is ignored.
85 public void stopNetLog() {
86 nativeStopNetLog(mChromiumUrlRequestContextAdapter);
90 private void initNetworkThread() {
91 Thread.currentThread().setName("ChromiumNet");
92 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
97 protected void finalize() throws Throwable {
98 nativeReleaseRequestContextAdapter(mChromiumUrlRequestContextAdapter);
102 protected long getChromiumUrlRequestContextAdapter() {
103 return mChromiumUrlRequestContextAdapter;
107 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and
108 * {@link #LOG_VERBOSE}.
110 private int getLoggingLevel() {
112 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) {
113 loggingLevel = LOG_VERBOSE;
114 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) {
115 loggingLevel = LOG_DEBUG;
117 loggingLevel = LOG_NONE;
122 // Returns an instance ChromiumUrlRequestContextAdapter to be stored in
123 // mChromiumUrlRequestContextAdapter.
124 private native long nativeCreateRequestContextAdapter(Context context,
125 String userAgent, int loggingLevel, String config);
127 private native void nativeReleaseRequestContextAdapter(
128 long ChromiumUrlRequestContextAdapter);
130 private native void nativeInitializeStatistics();
132 private native String nativeGetStatisticsJSON(String filter);
134 private native void nativeStartNetLogToFile(
135 long ChromiumUrlRequestContextAdapter, String fileName);
137 private native void nativeStopNetLog(long ChromiumUrlRequestContextAdapter);