OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "components/cronet/android/cronet_url_request_context.h" | |
6 | |
7 #include <string> | |
8 | |
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/cronet_url_request.h" | |
17 #include "components/cronet/android/cronet_url_request_adapter.h" | |
18 #include "components/cronet/android/cronet_url_request_context_adapter.h" | |
19 #include "components/cronet/url_request_context_config.h" | |
20 #include "jni/CronetUrlRequestContext_jni.h" | |
21 | |
22 namespace { | |
mmenke
2014/11/06 17:31:29
nit: Suggest moving this into the cronet namespac
mef
2014/11/06 22:51:46
Done.
| |
23 | |
24 void initJavaNetworkThread( | |
25 const base::android::ScopedJavaGlobalRef<jobject>& jowner) { | |
mmenke
2014/11/06 17:31:28
Need to include the header for ScopedJavaGlobalRef
mef
2014/11/06 22:51:46
Done.
| |
26 JNIEnv* jenv = base::android::AttachCurrentThread(); | |
27 cronet::Java_CronetUrlRequestContext_initNetworkThread(jenv, jowner.obj()); | |
28 } | |
29 | |
30 } // namespace | |
31 | |
32 namespace cronet { | |
33 | |
34 // Explicitly register static JNI functions. | |
35 bool CronetUrlRequestContextRegisterJni(JNIEnv* jenv) { | |
mmenke
2014/11/06 17:31:28
Per comments elsewhere, this is called env pretty
mef
2014/11/06 22:51:46
Done.
| |
36 return RegisterNativesImpl(jenv); | |
37 } | |
38 | |
39 // Sets global user-agent to be used for all subsequent requests. | |
40 static jlong CreateRequestContextAdapter(JNIEnv* jenv, | |
41 jobject jcaller, | |
mmenke
2014/11/06 17:31:28
optional: Should we use something more meaningful
mef
2014/11/06 22:51:46
Acknowledged. I think those are sufficiently gener
| |
42 jobject japp_context, | |
43 jstring jconfig) { | |
44 std::string config_string = | |
45 base::android::ConvertJavaStringToUTF8(jenv, jconfig); | |
46 | |
47 scoped_ptr<base::Value> config_value(base::JSONReader::Read(config_string)); | |
48 if (!config_value || !config_value->IsType(base::Value::TYPE_DICTIONARY)) { | |
49 DLOG(ERROR) << "Bad JSON: " << config_string; | |
50 return 0; | |
51 } | |
52 | |
53 scoped_ptr<URLRequestContextConfig> context_config( | |
54 new URLRequestContextConfig()); | |
55 base::JSONValueConverter<URLRequestContextConfig> converter; | |
mmenke
2014/11/06 17:31:28
Should probably include the JSONValueConverter hea
mef
2014/11/06 22:51:45
Done. Moved conversion from JSON into URLRequestCo
| |
56 if (!converter.Convert(*config_value, context_config.get())) { | |
57 DLOG(ERROR) << "Bad Config: " << config_value; | |
58 return 0; | |
59 } | |
60 | |
61 // Set application context. | |
62 base::android::ScopedJavaLocalRef<jobject> scoped_context(jenv, japp_context); | |
63 base::android::InitApplicationContext(jenv, scoped_context); | |
64 | |
65 base::android::ScopedJavaGlobalRef<jobject> jcaller_ref; | |
66 jcaller_ref.Reset(jenv, jcaller); | |
67 | |
68 CronetURLRequestContextAdapter* context_adapter = | |
69 new CronetURLRequestContextAdapter(); | |
70 base::Closure init_java_network_thread = base::Bind(&initJavaNetworkThread, | |
71 jcaller_ref); | |
72 context_adapter->Initialize(context_config.Pass(), init_java_network_thread); | |
73 | |
74 return reinterpret_cast<jlong>(context_adapter); | |
75 } | |
76 | |
77 // Destroys native objects. | |
78 static void DestroyRequestContextAdapter(JNIEnv* jenv, | |
79 jobject jcaller, | |
80 jlong jurl_request_context_adapter) { | |
81 if (jurl_request_context_adapter == 0) | |
mmenke
2014/11/06 17:31:28
DCHECK(jurl_request_context_adapter);? For thread
mef
2014/11/06 22:51:46
Done.
| |
82 return; | |
83 CronetURLRequestContextAdapter* context_adapter = | |
84 reinterpret_cast<CronetURLRequestContextAdapter*>( | |
85 jurl_request_context_adapter); | |
86 context_adapter->Destroy(); | |
87 } | |
88 | |
89 // Starts recording statistics. | |
90 static void InitializeStatistics(JNIEnv* jenv, jobject jcaller) { | |
91 base::StatisticsRecorder::Initialize(); | |
92 } | |
93 | |
94 // Gets current statistics with |filter| as a substring as JSON text (an empty | |
95 // |filter| will include all registered histograms). | |
96 static jstring GetStatisticsJSON(JNIEnv* jenv, | |
97 jobject jcaller, | |
98 jstring jfilter) { | |
99 std::string query = base::android::ConvertJavaStringToUTF8(jenv, jfilter); | |
100 std::string json = base::StatisticsRecorder::ToJSON(query); | |
101 return base::android::ConvertUTF8ToJavaString(jenv, json).Release(); | |
mmenke
2014/11/06 17:31:28
I don't think this should be a method on the conte
mef
2014/11/06 22:51:46
Done.
| |
102 } | |
103 | |
104 // Starts recording NetLog into file with |fileName|. | |
105 static void StartNetLogToFile(JNIEnv* jenv, | |
106 jobject jcaller, | |
107 jlong jurl_request_context_adapter, | |
108 jstring fileName) { | |
xunjieli
2014/11/06 17:02:36
nit: fileName -> jfile_name.
mef
2014/11/06 22:51:46
Done.
| |
109 if (jurl_request_context_adapter == 0) | |
110 return; | |
111 CronetURLRequestContextAdapter* context_adapter = | |
112 reinterpret_cast<CronetURLRequestContextAdapter*>( | |
113 jurl_request_context_adapter); | |
114 std::string file_name = | |
115 base::android::ConvertJavaStringToUTF8(jenv, fileName); | |
116 context_adapter->StartNetLogToFile(file_name); | |
117 } | |
118 | |
119 // Stops recording NetLog. | |
120 static void StopNetLog(JNIEnv* jenv, | |
121 jobject jcaller, | |
122 jlong jurl_request_context_adapter) { | |
123 if (jurl_request_context_adapter == 0) | |
124 return; | |
125 CronetURLRequestContextAdapter* context_adapter = | |
126 reinterpret_cast<CronetURLRequestContextAdapter*>( | |
127 jurl_request_context_adapter); | |
128 context_adapter->StopNetLog(); | |
129 } | |
130 | |
131 static jint SetMinLogLevel(JNIEnv* env, jobject jcaller, jint jlog_level) { | |
132 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); | |
133 // MinLogLevel is global, shared by all URLRequestContexts. | |
134 logging::SetMinLogLevel(static_cast<int>(jlog_level)); | |
135 return old_log_level; | |
136 } | |
137 | |
138 } // namespace cronet | |
OLD | NEW |