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
,
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
;
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
;
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
,
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
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
,
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
,
131 jlong jurl_request_context_adapter
,
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
,
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
,
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