ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / components / cronet / android / chromium_url_request_context.cc
blob1544dde830415d06c7f4e49fe5550f875ff61a0e
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 #include "components/cronet/android/chromium_url_request_context.h"
7 #include <string>
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/json/json_reader.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/metrics/statistics_recorder.h"
15 #include "base/values.h"
16 #include "components/cronet/android/chromium_url_request.h"
17 #include "components/cronet/android/url_request_adapter.h"
18 #include "components/cronet/android/url_request_context_adapter.h"
19 #include "components/cronet/url_request_context_config.h"
20 #include "jni/ChromiumUrlRequestContext_jni.h"
22 using base::android::ConvertUTF8ToJavaString;
23 using base::android::ConvertJavaStringToUTF8;
25 namespace {
27 // Delegate of URLRequestContextAdapter that delivers callbacks to the Java
28 // layer.
29 class JniURLRequestContextAdapterDelegate
30 : public cronet::URLRequestContextAdapter::
31 URLRequestContextAdapterDelegate {
32 public:
33 JniURLRequestContextAdapterDelegate(JNIEnv* env, jobject owner)
34 : owner_(env->NewGlobalRef(owner)) {}
36 void OnContextInitialized(
37 cronet::URLRequestContextAdapter* context_adapter) override {
38 JNIEnv* env = base::android::AttachCurrentThread();
39 cronet::Java_ChromiumUrlRequestContext_initNetworkThread(env, owner_);
40 // TODO(dplotnikov): figure out if we need to detach from the thread.
41 // The documentation says we should detach just before the thread exits.
44 protected:
45 ~JniURLRequestContextAdapterDelegate() override {
46 JNIEnv* env = base::android::AttachCurrentThread();
47 env->DeleteGlobalRef(owner_);
50 private:
51 jobject owner_;
54 } // namespace
56 namespace cronet {
58 // Explicitly register static JNI functions.
59 bool ChromiumUrlRequestContextRegisterJni(JNIEnv* env) {
60 return RegisterNativesImpl(env);
63 // Sets global user-agent to be used for all subsequent requests.
64 static jlong CreateRequestContextAdapter(JNIEnv* env,
65 jobject jcaller,
66 jobject jcontext,
67 jstring juser_agent,
68 jint jlog_level,
69 jstring jconfig) {
70 std::string user_agent = ConvertJavaStringToUTF8(env, juser_agent);
72 std::string config = ConvertJavaStringToUTF8(env, jconfig);
74 scoped_ptr<base::Value> config_value(base::JSONReader::Read(config));
75 if (!config_value || !config_value->IsType(base::Value::TYPE_DICTIONARY)) {
76 DLOG(ERROR) << "Bad JSON: " << config;
77 return 0;
80 scoped_ptr<URLRequestContextConfig> context_config(
81 new URLRequestContextConfig());
82 base::JSONValueConverter<URLRequestContextConfig> converter;
83 if (!converter.Convert(*config_value, context_config.get())) {
84 DLOG(ERROR) << "Bad Config: " << config_value;
85 return 0;
88 // Set application context.
89 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, jcontext);
90 base::android::InitApplicationContext(env, scoped_context);
92 // TODO(mef): MinLogLevel is global, shared by all URLRequestContexts.
93 // Revisit this if each URLRequestContext would need an individual log level.
94 logging::SetMinLogLevel(static_cast<int>(jlog_level));
96 // TODO(dplotnikov): set application context.
97 URLRequestContextAdapter* context_adapter = new URLRequestContextAdapter(
98 new JniURLRequestContextAdapterDelegate(env, jcaller), user_agent);
99 context_adapter->AddRef(); // Hold onto this ref-counted object.
100 context_adapter->Initialize(context_config.Pass());
101 return reinterpret_cast<jlong>(context_adapter);
104 // Releases native objects.
105 static void ReleaseRequestContextAdapter(JNIEnv* env,
106 jobject jcaller,
107 jlong jurl_request_context_adapter) {
108 URLRequestContextAdapter* context_adapter =
109 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter);
110 // TODO(mef): Revisit this from thread safety point of view: Can we delete a
111 // thread while running on that thread?
112 // URLRequestContextAdapter is a ref-counted object, and may have pending
113 // tasks,
114 // so we need to release it instead of deleting here.
115 context_adapter->Release();
118 // Starts recording statistics.
119 static void InitializeStatistics(JNIEnv* env, jobject jcaller) {
120 base::StatisticsRecorder::Initialize();
123 // Gets current statistics with |jfilter| as a substring as JSON text (an empty
124 // |jfilter| will include all registered histograms).
125 static jstring GetStatisticsJSON(JNIEnv* env,
126 jobject jcaller,
127 jstring jfilter) {
128 std::string query = ConvertJavaStringToUTF8(env, jfilter);
129 std::string json = base::StatisticsRecorder::ToJSON(query);
130 return ConvertUTF8ToJavaString(env, json).Release();
133 // Starts recording NetLog into file with |jfilename|.
134 static void StartNetLogToFile(JNIEnv* env,
135 jobject jcaller,
136 jlong jurl_request_context_adapter,
137 jstring jfilename) {
138 URLRequestContextAdapter* context_adapter =
139 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter);
140 std::string filename = ConvertJavaStringToUTF8(env, jfilename);
141 context_adapter->StartNetLogToFile(filename);
144 // Stops recording NetLog.
145 static void StopNetLog(JNIEnv* env,
146 jobject jcaller,
147 jlong jurl_request_context_adapter) {
148 URLRequestContextAdapter* context_adapter =
149 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter);
150 context_adapter->StopNetLog();
153 // Called on application's main Java thread.
154 static void InitRequestContextOnMainThread(JNIEnv* env,
155 jobject jcaller,
156 jlong jurl_request_context_adapter) {
157 URLRequestContextAdapter* context_adapter =
158 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter);
159 context_adapter->InitRequestContextOnMainThread();
162 } // namespace cronet