Revert of Refactor the avatar button/icon class (patchset #14 id:320001 of https...
[chromium-blink-merge.git] / components / cronet / android / chromium_url_request_context.cc
blob1bfcf2f1502d96cf4b5fb7ae73a3ad9b5e91d710
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 jstring juser_agent,
67 jint jlog_level,
68 jstring jconfig) {
69 std::string user_agent = ConvertJavaStringToUTF8(env, juser_agent);
71 std::string config = ConvertJavaStringToUTF8(env, jconfig);
73 scoped_ptr<base::Value> config_value(base::JSONReader::Read(config));
74 if (!config_value || !config_value->IsType(base::Value::TYPE_DICTIONARY)) {
75 DLOG(ERROR) << "Bad JSON: " << config;
76 return 0;
79 scoped_ptr<URLRequestContextConfig> context_config(
80 new URLRequestContextConfig());
81 base::JSONValueConverter<URLRequestContextConfig> converter;
82 if (!converter.Convert(*config_value, context_config.get())) {
83 DLOG(ERROR) << "Bad Config: " << config_value;
84 return 0;
87 // TODO(mef): MinLogLevel is global, shared by all URLRequestContexts.
88 // Revisit this if each URLRequestContext would need an individual log level.
89 logging::SetMinLogLevel(static_cast<int>(jlog_level));
91 // TODO(dplotnikov): set application context.
92 URLRequestContextAdapter* context_adapter = new URLRequestContextAdapter(
93 new JniURLRequestContextAdapterDelegate(env, jcaller), user_agent);
94 context_adapter->AddRef(); // Hold onto this ref-counted object.
95 context_adapter->Initialize(context_config.Pass());
96 return reinterpret_cast<jlong>(context_adapter);
99 // Releases native objects.
100 static void ReleaseRequestContextAdapter(JNIEnv* env,
101 jobject jcaller,
102 jlong jurl_request_context_adapter) {
103 URLRequestContextAdapter* context_adapter =
104 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter);
105 // TODO(mef): Revisit this from thread safety point of view: Can we delete a
106 // thread while running on that thread?
107 // URLRequestContextAdapter is a ref-counted object, and may have pending
108 // tasks,
109 // so we need to release it instead of deleting here.
110 context_adapter->Release();
113 // Starts recording statistics.
114 static void InitializeStatistics(JNIEnv* env, jobject jcaller) {
115 base::StatisticsRecorder::Initialize();
118 // Gets current statistics with |jfilter| as a substring as JSON text (an empty
119 // |jfilter| will include all registered histograms).
120 static jstring GetStatisticsJSON(JNIEnv* env,
121 jobject jcaller,
122 jstring jfilter) {
123 std::string query = ConvertJavaStringToUTF8(env, jfilter);
124 std::string json = base::StatisticsRecorder::ToJSON(query);
125 return ConvertUTF8ToJavaString(env, json).Release();
128 // Starts recording NetLog into file with |jfilename|.
129 static void StartNetLogToFile(JNIEnv* env,
130 jobject jcaller,
131 jlong jurl_request_context_adapter,
132 jstring jfilename,
133 jboolean jlog_all) {
134 URLRequestContextAdapter* context_adapter =
135 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter);
136 std::string filename = ConvertJavaStringToUTF8(env, jfilename);
137 context_adapter->StartNetLogToFile(filename, jlog_all);
140 // Stops recording NetLog.
141 static void StopNetLog(JNIEnv* env,
142 jobject jcaller,
143 jlong jurl_request_context_adapter) {
144 URLRequestContextAdapter* context_adapter =
145 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter);
146 context_adapter->StopNetLog();
149 // Called on application's main Java thread.
150 static void InitRequestContextOnMainThread(JNIEnv* env,
151 jobject jcaller,
152 jlong jurl_request_context_adapter) {
153 URLRequestContextAdapter* context_adapter =
154 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter);
155 context_adapter->InitRequestContextOnMainThread();
158 } // namespace cronet