Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / cronet / android / java / src / org / chromium / net / ChromiumUrlRequestContext.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.Handler;
9 import android.os.Looper;
10 import android.os.Process;
11 import android.util.Log;
12
13 import org.chromium.base.CalledByNative;
14 import org.chromium.base.JNINamespace;
15
16 /**
17  * Provides context for the native HTTP operations.
18  */
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";
25
26     /**
27      * Native adapter object, owned by ChromiumUrlRequestContext.
28      */
29     private long mChromiumUrlRequestContextAdapter;
30
31     /**
32      * Constructor.
33      */
34     protected ChromiumUrlRequestContext(final Context context, String userAgent,
35             String config) {
36         mChromiumUrlRequestContextAdapter = nativeCreateRequestContextAdapter(
37                 context, userAgent, getLoggingLevel(), config);
38         if (mChromiumUrlRequestContextAdapter == 0) {
39             throw new NullPointerException("Context Adapter creation failed");
40         }
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() {
46             public void run() {
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);
55             }
56         };
57         new Handler(Looper.getMainLooper()).post(task);
58     }
59
60     /**
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.
63      */
64     public static String getVersion() {
65         return Version.getVersion();
66     }
67
68     /**
69      * Initializes statistics recorder.
70      */
71     public void initializeStatistics() {
72         nativeInitializeStatistics();
73     }
74
75     /**
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).
79      */
80     public String getStatisticsJSON(String filter) {
81         return nativeGetStatisticsJSON(filter);
82     }
83
84     /**
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.
89      */
90     public void startNetLogToFile(String fileName) {
91         nativeStartNetLogToFile(mChromiumUrlRequestContextAdapter, fileName);
92     }
93
94     /**
95      * Stops NetLog logging and flushes file to disk. If a logging session is
96      * not in progress this call is ignored.
97      */
98     public void stopNetLog() {
99         nativeStopNetLog(mChromiumUrlRequestContextAdapter);
100     }
101
102     @CalledByNative
103     private void initNetworkThread() {
104         Thread.currentThread().setName("ChromiumNet");
105         Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
106     }
107
108     @Override
109     protected void finalize() throws Throwable {
110         nativeReleaseRequestContextAdapter(mChromiumUrlRequestContextAdapter);
111         super.finalize();
112     }
113
114     protected long getChromiumUrlRequestContextAdapter() {
115         return mChromiumUrlRequestContextAdapter;
116     }
117
118     /**
119      * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and
120      *         {@link #LOG_VERBOSE}.
121      */
122     private int getLoggingLevel() {
123         int loggingLevel;
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;
128         } else {
129             loggingLevel = LOG_NONE;
130         }
131         return loggingLevel;
132     }
133
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);
138
139     private native void nativeReleaseRequestContextAdapter(
140             long chromiumUrlRequestContextAdapter);
141
142     private native void nativeInitializeStatistics();
143
144     private native String nativeGetStatisticsJSON(String filter);
145
146     private native void nativeStartNetLogToFile(
147             long chromiumUrlRequestContextAdapter, String fileName);
148
149     private native void nativeStopNetLog(long chromiumUrlRequestContextAdapter);
150
151     private native void nativeInitRequestContextOnMainThread(
152             long chromiumUrlRequestContextAdapter);
153 }