OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/cronet/android/chromium_url_request_context.h" | 5 #include "components/cronet/android/chromium_url_request_context.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 } // namespace | 54 } // namespace |
55 | 55 |
56 namespace cronet { | 56 namespace cronet { |
57 | 57 |
58 // Explicitly register static JNI functions. | 58 // Explicitly register static JNI functions. |
59 bool ChromiumUrlRequestContextRegisterJni(JNIEnv* env) { | 59 bool ChromiumUrlRequestContextRegisterJni(JNIEnv* env) { |
60 return RegisterNativesImpl(env); | 60 return RegisterNativesImpl(env); |
61 } | 61 } |
62 | 62 |
63 // Sets global user-agent to be used for all subsequent requests. | 63 // Sets global user-agent to be used for all subsequent requests. |
64 static jlong CreateRequestContextAdapter(JNIEnv* env, | 64 static jlong CreateRequestContextAdapter( |
65 jobject jcaller, | 65 JNIEnv* env, |
66 jstring juser_agent, | 66 const JavaParamRef<jobject>& jcaller, |
67 jint jlog_level, | 67 const JavaParamRef<jstring>& juser_agent, |
68 jstring jconfig) { | 68 jint jlog_level, |
| 69 const JavaParamRef<jstring>& jconfig) { |
69 std::string user_agent = ConvertJavaStringToUTF8(env, juser_agent); | 70 std::string user_agent = ConvertJavaStringToUTF8(env, juser_agent); |
70 | 71 |
71 std::string config = ConvertJavaStringToUTF8(env, jconfig); | 72 std::string config = ConvertJavaStringToUTF8(env, jconfig); |
72 | 73 |
73 scoped_ptr<base::Value> config_value = base::JSONReader::Read(config); | 74 scoped_ptr<base::Value> config_value = base::JSONReader::Read(config); |
74 if (!config_value || !config_value->IsType(base::Value::TYPE_DICTIONARY)) { | 75 if (!config_value || !config_value->IsType(base::Value::TYPE_DICTIONARY)) { |
75 DLOG(ERROR) << "Bad JSON: " << config; | 76 DLOG(ERROR) << "Bad JSON: " << config; |
76 return 0; | 77 return 0; |
77 } | 78 } |
78 | 79 |
(...skipping 12 matching lines...) Expand all Loading... |
91 // TODO(dplotnikov): set application context. | 92 // TODO(dplotnikov): set application context. |
92 URLRequestContextAdapter* context_adapter = new URLRequestContextAdapter( | 93 URLRequestContextAdapter* context_adapter = new URLRequestContextAdapter( |
93 new JniURLRequestContextAdapterDelegate(env, jcaller), user_agent); | 94 new JniURLRequestContextAdapterDelegate(env, jcaller), user_agent); |
94 context_adapter->AddRef(); // Hold onto this ref-counted object. | 95 context_adapter->AddRef(); // Hold onto this ref-counted object. |
95 context_adapter->Initialize(context_config.Pass()); | 96 context_adapter->Initialize(context_config.Pass()); |
96 return reinterpret_cast<jlong>(context_adapter); | 97 return reinterpret_cast<jlong>(context_adapter); |
97 } | 98 } |
98 | 99 |
99 // Releases native objects. | 100 // Releases native objects. |
100 static void ReleaseRequestContextAdapter(JNIEnv* env, | 101 static void ReleaseRequestContextAdapter(JNIEnv* env, |
101 jobject jcaller, | 102 const JavaParamRef<jobject>& jcaller, |
102 jlong jurl_request_context_adapter) { | 103 jlong jurl_request_context_adapter) { |
103 URLRequestContextAdapter* context_adapter = | 104 URLRequestContextAdapter* context_adapter = |
104 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter); | 105 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter); |
105 // TODO(mef): Revisit this from thread safety point of view: Can we delete a | 106 // TODO(mef): Revisit this from thread safety point of view: Can we delete a |
106 // thread while running on that thread? | 107 // thread while running on that thread? |
107 // URLRequestContextAdapter is a ref-counted object, and may have pending | 108 // URLRequestContextAdapter is a ref-counted object, and may have pending |
108 // tasks, | 109 // tasks, |
109 // so we need to release it instead of deleting here. | 110 // so we need to release it instead of deleting here. |
110 context_adapter->Release(); | 111 context_adapter->Release(); |
111 } | 112 } |
112 | 113 |
113 // Starts recording statistics. | 114 // Starts recording statistics. |
114 static void InitializeStatistics(JNIEnv* env, jobject jcaller) { | 115 static void InitializeStatistics(JNIEnv* env, |
| 116 const JavaParamRef<jobject>& jcaller) { |
115 base::StatisticsRecorder::Initialize(); | 117 base::StatisticsRecorder::Initialize(); |
116 } | 118 } |
117 | 119 |
118 // Gets current statistics with |jfilter| as a substring as JSON text (an empty | 120 // Gets current statistics with |jfilter| as a substring as JSON text (an empty |
119 // |jfilter| will include all registered histograms). | 121 // |jfilter| will include all registered histograms). |
120 static ScopedJavaLocalRef<jstring> GetStatisticsJSON(JNIEnv* env, | 122 static ScopedJavaLocalRef<jstring> GetStatisticsJSON( |
121 jobject jcaller, | 123 JNIEnv* env, |
122 jstring jfilter) { | 124 const JavaParamRef<jobject>& jcaller, |
| 125 const JavaParamRef<jstring>& jfilter) { |
123 std::string query = ConvertJavaStringToUTF8(env, jfilter); | 126 std::string query = ConvertJavaStringToUTF8(env, jfilter); |
124 std::string json = base::StatisticsRecorder::ToJSON(query); | 127 std::string json = base::StatisticsRecorder::ToJSON(query); |
125 return ConvertUTF8ToJavaString(env, json); | 128 return ConvertUTF8ToJavaString(env, json); |
126 } | 129 } |
127 | 130 |
128 // Starts recording NetLog into file with |jfilename|. | 131 // Starts recording NetLog into file with |jfilename|. |
129 static void StartNetLogToFile(JNIEnv* env, | 132 static void StartNetLogToFile(JNIEnv* env, |
130 jobject jcaller, | 133 const JavaParamRef<jobject>& jcaller, |
131 jlong jurl_request_context_adapter, | 134 jlong jurl_request_context_adapter, |
132 jstring jfilename, | 135 const JavaParamRef<jstring>& jfilename, |
133 jboolean jlog_all) { | 136 jboolean jlog_all) { |
134 URLRequestContextAdapter* context_adapter = | 137 URLRequestContextAdapter* context_adapter = |
135 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter); | 138 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter); |
136 std::string filename = ConvertJavaStringToUTF8(env, jfilename); | 139 std::string filename = ConvertJavaStringToUTF8(env, jfilename); |
137 context_adapter->StartNetLogToFile(filename, jlog_all); | 140 context_adapter->StartNetLogToFile(filename, jlog_all); |
138 } | 141 } |
139 | 142 |
140 // Stops recording NetLog. | 143 // Stops recording NetLog. |
141 static void StopNetLog(JNIEnv* env, | 144 static void StopNetLog(JNIEnv* env, |
142 jobject jcaller, | 145 const JavaParamRef<jobject>& jcaller, |
143 jlong jurl_request_context_adapter) { | 146 jlong jurl_request_context_adapter) { |
144 URLRequestContextAdapter* context_adapter = | 147 URLRequestContextAdapter* context_adapter = |
145 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter); | 148 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter); |
146 context_adapter->StopNetLog(); | 149 context_adapter->StopNetLog(); |
147 } | 150 } |
148 | 151 |
149 // Called on application's main Java thread. | 152 // Called on application's main Java thread. |
150 static void InitRequestContextOnMainThread(JNIEnv* env, | 153 static void InitRequestContextOnMainThread(JNIEnv* env, |
151 jobject jcaller, | 154 const JavaParamRef<jobject>& jcaller, |
152 jlong jurl_request_context_adapter) { | 155 jlong jurl_request_context_adapter) { |
153 URLRequestContextAdapter* context_adapter = | 156 URLRequestContextAdapter* context_adapter = |
154 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter); | 157 reinterpret_cast<URLRequestContextAdapter*>(jurl_request_context_adapter); |
155 context_adapter->InitRequestContextOnMainThread(); | 158 context_adapter->InitRequestContextOnMainThread(); |
156 } | 159 } |
157 | 160 |
158 } // namespace cronet | 161 } // namespace cronet |
OLD | NEW |