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(
66 const JavaParamRef
<jobject
>& jcaller
,
67 const JavaParamRef
<jstring
>& juser_agent
,
69 const JavaParamRef
<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
;
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 // TODO(mef): MinLogLevel is global, shared by all URLRequestContexts.
89 // Revisit this if each URLRequestContext would need an individual log level.
90 logging::SetMinLogLevel(static_cast<int>(jlog_level
));
92 // TODO(dplotnikov): set application context.
93 URLRequestContextAdapter
* context_adapter
= new URLRequestContextAdapter(
94 new JniURLRequestContextAdapterDelegate(env
, jcaller
), user_agent
);
95 context_adapter
->AddRef(); // Hold onto this ref-counted object.
96 context_adapter
->Initialize(context_config
.Pass());
97 return reinterpret_cast<jlong
>(context_adapter
);
100 // Releases native objects.
101 static void ReleaseRequestContextAdapter(JNIEnv
* env
,
102 const JavaParamRef
<jobject
>& jcaller
,
103 jlong jurl_request_context_adapter
) {
104 URLRequestContextAdapter
* context_adapter
=
105 reinterpret_cast<URLRequestContextAdapter
*>(jurl_request_context_adapter
);
106 // TODO(mef): Revisit this from thread safety point of view: Can we delete a
107 // thread while running on that thread?
108 // URLRequestContextAdapter is a ref-counted object, and may have pending
110 // so we need to release it instead of deleting here.
111 context_adapter
->Release();
114 // Starts recording statistics.
115 static void InitializeStatistics(JNIEnv
* env
,
116 const JavaParamRef
<jobject
>& jcaller
) {
117 base::StatisticsRecorder::Initialize();
120 // Gets current statistics with |jfilter| as a substring as JSON text (an empty
121 // |jfilter| will include all registered histograms).
122 static ScopedJavaLocalRef
<jstring
> GetStatisticsJSON(
124 const JavaParamRef
<jobject
>& jcaller
,
125 const JavaParamRef
<jstring
>& jfilter
) {
126 std::string query
= ConvertJavaStringToUTF8(env
, jfilter
);
127 std::string json
= base::StatisticsRecorder::ToJSON(query
);
128 return ConvertUTF8ToJavaString(env
, json
);
131 // Starts recording NetLog into file with |jfilename|.
132 static void StartNetLogToFile(JNIEnv
* env
,
133 const JavaParamRef
<jobject
>& jcaller
,
134 jlong jurl_request_context_adapter
,
135 const JavaParamRef
<jstring
>& jfilename
,
137 URLRequestContextAdapter
* context_adapter
=
138 reinterpret_cast<URLRequestContextAdapter
*>(jurl_request_context_adapter
);
139 std::string filename
= ConvertJavaStringToUTF8(env
, jfilename
);
140 context_adapter
->StartNetLogToFile(filename
, jlog_all
);
143 // Stops recording NetLog.
144 static void StopNetLog(JNIEnv
* env
,
145 const JavaParamRef
<jobject
>& jcaller
,
146 jlong jurl_request_context_adapter
) {
147 URLRequestContextAdapter
* context_adapter
=
148 reinterpret_cast<URLRequestContextAdapter
*>(jurl_request_context_adapter
);
149 context_adapter
->StopNetLog();
152 // Called on application's main Java thread.
153 static void InitRequestContextOnMainThread(JNIEnv
* env
,
154 const JavaParamRef
<jobject
>& jcaller
,
155 jlong jurl_request_context_adapter
) {
156 URLRequestContextAdapter
* context_adapter
=
157 reinterpret_cast<URLRequestContextAdapter
*>(jurl_request_context_adapter
);
158 context_adapter
->InitRequestContextOnMainThread();
161 } // namespace cronet