Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / cronet / android / java / src / org / chromium / net / UrlRequestContext.java
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.ConditionVariable;
9 import android.os.Process;
10
11 import org.chromium.base.CalledByNative;
12 import org.chromium.base.JNINamespace;
13
14 /**
15  * Provides context for the native HTTP operations.
16  */
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;
22
23     /**
24      * Native peer object, owned by UrlRequestContext.
25      */
26     private long mUrlRequestContextPeer;
27
28     private final ConditionVariable mStarted = new ConditionVariable();
29
30     /**
31      * Constructor.
32      *
33      * @param loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and
34      *            {@link #LOG_VERBOSE}.
35      */
36     protected UrlRequestContext(Context context, String userAgent,
37             int loggingLevel) {
38         mUrlRequestContextPeer = nativeCreateRequestContextPeer(context,
39                 userAgent, loggingLevel);
40         // TODO(mef): Revisit the need of block here.
41         mStarted.block(2000);
42     }
43
44     /**
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.
47      */
48     public static String getVersion() {
49         return nativeGetVersion();
50     }
51
52     /**
53      * Initializes statistics recorder.
54      */
55     public void initializeStatistics() {
56         nativeInitializeStatistics();
57     }
58
59     /**
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).
63      */
64     public String getStatisticsJSON(String filter) {
65         return nativeGetStatisticsJSON(filter);
66     }
67
68     /**
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.
73      */
74     public void startNetLogToFile(String fileName) {
75         nativeStartNetLogToFile(mUrlRequestContextPeer, fileName);
76     }
77
78     /**
79      * Stops NetLog logging and flushes file to disk. If a logging session is
80      * not in progress this call is ignored.
81      */
82     public void stopNetLog() {
83         nativeStopNetLog(mUrlRequestContextPeer);
84     }
85
86     @CalledByNative
87     private void initNetworkThread() {
88         Thread.currentThread().setName("ChromiumNet");
89         Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
90         mStarted.open();
91     }
92
93     @Override
94     protected void finalize() throws Throwable {
95         nativeReleaseRequestContextPeer(mUrlRequestContextPeer);
96         super.finalize();
97     }
98
99     protected long getUrlRequestContextPeer() {
100         return mUrlRequestContextPeer;
101     }
102
103     private static native String nativeGetVersion();
104
105     // Returns an instance URLRequestContextPeer to be stored in
106     // mUrlRequestContextPeer.
107     private native long nativeCreateRequestContextPeer(Context context,
108             String userAgent, int loggingLevel);
109
110     private native void nativeReleaseRequestContextPeer(
111             long urlRequestContextPeer);
112
113     private native void nativeInitializeStatistics();
114
115     private native String nativeGetStatisticsJSON(String filter);
116
117     private native void nativeStartNetLogToFile(long urlRequestContextPeer,
118             String fileName);
119
120     private native void nativeStopNetLog(long urlRequestContextPeer);
121 }