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"
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
;
27 // Delegate of URLRequestContextAdapter that delivers callbacks to the Java
29 class JniURLRequestContextAdapterDelegate
30 : public cronet::URLRequestContextAdapter::
31 URLRequestContextAdapterDelegate
{
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.
45 ~JniURLRequestContextAdapterDelegate() override
{
46 JNIEnv
* env
= base::android::AttachCurrentThread();
47 env
->DeleteGlobalRef(owner_
);
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
,
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
;
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
;
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
,
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
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
,
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
,
136 jlong jurl_request_context_adapter
,
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
,
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
,
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