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;
11 import org.chromium.base.CalledByNative;
12 import org.chromium.base.JNINamespace;
15 * Provides context for the native HTTP operations.
17 @JNINamespace("cronet")
18 public class UrlRequestContext {
19 protected static final int LOG_NONE = 0;
20 protected static final int LOG_DEBUG = 1;
21 protected static final int LOG_VERBOSE = 2;
24 * Native peer object, owned by UrlRequestContext.
26 private long mUrlRequestContextPeer;
28 private final ConditionVariable mStarted = new ConditionVariable();
33 * @param loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and
34 * {@link #LOG_VERBOSE}.
36 protected UrlRequestContext(Context context, String userAgent,
38 mUrlRequestContextPeer = nativeCreateRequestContextPeer(context,
39 userAgent, loggingLevel);
40 // TODO(mef): Revisit the need of block here.
45 * Returns the version of this network stack formatted as N.N.N.N/X where
46 * N.N.N.N is the version of Chromium and X is the version of the JNI layer.
48 public static String getVersion() {
49 return nativeGetVersion();
53 * Initializes statistics recorder.
55 public void initializeStatistics() {
56 nativeInitializeStatistics();
60 * Gets current statistics recorded since |initializeStatistics| with
61 * |filter| as a substring as JSON text (an empty |filter| will include all
62 * registered histograms).
64 public String getStatisticsJSON(String filter) {
65 return nativeGetStatisticsJSON(filter);
69 * Starts NetLog logging to a file named |fileName| in the
70 * application temporary directory. |fileName| must not be empty. Log level
71 * is LOG_ALL_BUT_BYTES. If the file exists it is truncated before starting.
72 * If actively logging the call is ignored.
74 public void startNetLogToFile(String fileName) {
75 nativeStartNetLogToFile(mUrlRequestContextPeer, fileName);
79 * Stops NetLog logging and flushes file to disk. If a logging session is
80 * not in progress this call is ignored.
82 public void stopNetLog() {
83 nativeStopNetLog(mUrlRequestContextPeer);
87 private void initNetworkThread() {
88 Thread.currentThread().setName("ChromiumNet");
89 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
94 protected void finalize() throws Throwable {
95 nativeReleaseRequestContextPeer(mUrlRequestContextPeer);
99 protected long getUrlRequestContextPeer() {
100 return mUrlRequestContextPeer;
103 private static native String nativeGetVersion();
105 // Returns an instance URLRequestContextPeer to be stored in
106 // mUrlRequestContextPeer.
107 private native long nativeCreateRequestContextPeer(Context context,
108 String userAgent, int loggingLevel);
110 private native void nativeReleaseRequestContextPeer(
111 long urlRequestContextPeer);
113 private native void nativeInitializeStatistics();
115 private native String nativeGetStatisticsJSON(String filter);
117 private native void nativeStartNetLogToFile(long urlRequestContextPeer,
120 private native void nativeStopNetLog(long urlRequestContextPeer);