Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / net / 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.AccessedByNative;
12 import org.chromium.base.CalledByNative;
13
14 /**
15  * Provides context for the native HTTP operations.
16  */
17 public class UrlRequestContext {
18     protected static final int LOG_NONE = 0;
19
20     protected static final int LOG_DEBUG = 1;
21
22     protected static final int LOG_VERBOSE = 2;
23
24     /**
25      * This field is accessed exclusively from the native layer.
26      */
27     @AccessedByNative
28     private long mRequestContext;
29
30     private final ConditionVariable mStarted = new ConditionVariable();
31
32     /**
33      * Constructor.
34      *
35      * @param loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and
36      *            {@link #LOG_VERBOSE}.
37      */
38     protected UrlRequestContext(Context context, String userAgent,
39             int loggingLevel) {
40         nativeInitialize(context, userAgent, loggingLevel);
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 native String getVersion();
49
50     @CalledByNative
51     private void initNetworkThread() {
52         Thread.currentThread().setName("ChromiumNet");
53         Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
54         mStarted.open();
55     }
56
57     @Override
58     protected void finalize() throws Throwable {
59         nativeFinalize();
60         super.finalize();
61     }
62
63     private native void nativeInitialize(Context context, String userAgent,
64             int loggingLevel);
65
66     private native void nativeFinalize();
67 }