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/cronet_url_request_context_adapter.h" | 5 #include "components/cronet/android/cronet_url_request_context_adapter.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | |
10 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/prefs/pref_registry_simple.h" | |
14 #include "base/prefs/pref_service.h" | |
15 #include "base/prefs/pref_service_factory.h" | |
12 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
13 #include "base/values.h" | 17 #include "base/values.h" |
18 #include "components/cronet/android/cronet_pref_store.h" | |
14 #include "components/cronet/url_request_context_config.h" | 19 #include "components/cronet/url_request_context_config.h" |
20 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_io_d ata.h" | |
21 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_pref s.h" | |
22 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_serv ice.h" | |
23 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_sett ings.h" | |
24 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_stat istics_prefs.h" | |
25 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param s.h" | |
15 #include "jni/CronetUrlRequestContext_jni.h" | 26 #include "jni/CronetUrlRequestContext_jni.h" |
16 #include "net/base/load_flags.h" | 27 #include "net/base/load_flags.h" |
17 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
18 #include "net/base/net_log_logger.h" | 29 #include "net/base/net_log_logger.h" |
19 #include "net/base/network_delegate_impl.h" | 30 #include "net/base/network_delegate_impl.h" |
20 #include "net/http/http_auth_handler_factory.h" | 31 #include "net/http/http_auth_handler_factory.h" |
21 #include "net/proxy/proxy_config_service_fixed.h" | 32 #include "net/proxy/proxy_config_service_fixed.h" |
22 #include "net/proxy/proxy_service.h" | 33 #include "net/proxy/proxy_service.h" |
23 #include "net/url_request/url_request_context.h" | 34 #include "net/url_request/url_request_context.h" |
24 #include "net/url_request/url_request_context_builder.h" | 35 #include "net/url_request/url_request_context_builder.h" |
36 #include "net/url_request/url_request_context_getter.h" | |
37 | |
25 | 38 |
26 namespace { | 39 namespace { |
27 | 40 |
41 // Shows notifications which correspond to PersistentPrefStore's reading errors. | |
42 void HandleReadError(PersistentPrefStore::PrefReadError error) { | |
43 } | |
44 | |
28 class BasicNetworkDelegate : public net::NetworkDelegateImpl { | 45 class BasicNetworkDelegate : public net::NetworkDelegateImpl { |
29 public: | 46 public: |
30 BasicNetworkDelegate() {} | 47 BasicNetworkDelegate() {} |
31 ~BasicNetworkDelegate() override {} | 48 ~BasicNetworkDelegate() override {} |
32 | 49 |
33 private: | 50 private: |
34 // net::NetworkDelegate implementation. | 51 // net::NetworkDelegate implementation. |
35 int OnBeforeURLRequest(net::URLRequest* request, | 52 int OnBeforeURLRequest(net::URLRequest* request, |
36 const net::CompletionCallback& callback, | 53 const net::CompletionCallback& callback, |
37 GURL* new_url) override { | 54 GURL* new_url) override { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
97 | 114 |
98 bool OnCanThrottleRequest(const net::URLRequest& request) const override { | 115 bool OnCanThrottleRequest(const net::URLRequest& request) const override { |
99 return false; | 116 return false; |
100 } | 117 } |
101 | 118 |
102 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); | 119 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); |
103 }; | 120 }; |
104 | 121 |
105 } // namespace | 122 } // namespace |
106 | 123 |
124 namespace data_reduction_proxy { | |
125 class DataReductionProxyConfigurator; | |
126 class DataReductionProxyStatisticsPrefs; | |
127 } | |
128 | |
107 namespace cronet { | 129 namespace cronet { |
108 | 130 |
109 // Explicitly register static JNI functions. | 131 // Explicitly register static JNI functions. |
110 bool CronetUrlRequestContextAdapterRegisterJni(JNIEnv* env) { | 132 bool CronetUrlRequestContextAdapterRegisterJni(JNIEnv* env) { |
111 return RegisterNativesImpl(env); | 133 return RegisterNativesImpl(env); |
112 } | 134 } |
113 | 135 |
114 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter( | 136 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter( |
115 scoped_ptr<URLRequestContextConfig> context_config) | 137 scoped_ptr<URLRequestContextConfig> context_config) |
116 : network_thread_(new base::Thread("network")), | 138 : network_thread_(new base::Thread("network")), |
117 context_config_(context_config.Pass()), | 139 context_config_(context_config.Pass()), |
118 is_context_initialized_(false), | 140 is_context_initialized_(false), |
119 default_load_flags_(net::LOAD_NORMAL) { | 141 default_load_flags_(net::LOAD_NORMAL) { |
120 base::Thread::Options options; | 142 base::Thread::Options options; |
121 options.message_loop_type = base::MessageLoop::TYPE_IO; | 143 options.message_loop_type = base::MessageLoop::TYPE_IO; |
122 network_thread_->StartWithOptions(options); | 144 network_thread_->StartWithOptions(options); |
123 } | 145 } |
124 | 146 |
125 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { | 147 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { |
126 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 148 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
127 StopNetLogOnNetworkThread(); | 149 StopNetLogOnNetworkThread(); |
128 } | 150 } |
129 | 151 |
130 void CronetURLRequestContextAdapter::InitRequestContextOnMainThread( | 152 void CronetURLRequestContextAdapter::InitRequestContextOnMainThread( |
131 JNIEnv* env, | 153 JNIEnv* env, |
132 jobject jcaller) { | 154 jobject jcaller) { |
133 base::android::ScopedJavaGlobalRef<jobject> jcaller_ref; | 155 base::android::ScopedJavaGlobalRef<jobject> jcaller_ref; |
134 jcaller_ref.Reset(env, jcaller); | 156 jcaller_ref.Reset(env, jcaller); |
135 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( | 157 |
136 GetNetworkTaskRunner(), nullptr)); | 158 PrefRegistrySimple* pref_registry = new PrefRegistrySimple(); |
159 data_reduction_proxy::RegisterSimpleProfilePrefs(pref_registry); | |
160 base::PrefServiceFactory pref_service_factory; | |
161 pref_service_factory.set_user_prefs( | |
162 make_scoped_refptr(new CronetPrefStore())); | |
163 pref_service_factory.set_read_error_callback(base::Bind(&HandleReadError)); | |
164 prefs_ = pref_service_factory.Create(pref_registry).Pass(); | |
mmenke
2015/03/10 15:26:55
This doesn't persist anything to disk?
bengr
2015/03/10 23:43:31
Right.
| |
165 | |
166 ui_task_runner_ = base::MessageLoop::current()->message_loop_proxy(); | |
mmenke
2015/03/10 15:26:55
Why do we need to do stuff on the main thread? Ca
bengr
2015/03/10 23:43:31
Done.
| |
167 base::CommandLine::Init(0, nullptr); | |
168 scoped_ptr<net::NetLog> net_log(new net::NetLog); | |
169 DCHECK(!data_reduction_proxy_io_data_); | |
170 data_reduction_proxy_io_data_.reset( | |
171 new data_reduction_proxy::DataReductionProxyIOData( | |
172 data_reduction_proxy::Client::CRONET_ANDROID, | |
mmenke
2015/03/10 15:26:55
Should we really use one client name for anything
bengr
2015/03/10 23:43:31
For now. Separately, we're working on a a client m
| |
173 data_reduction_proxy::DataReductionProxyParams::kAllowed | | |
174 data_reduction_proxy::DataReductionProxyParams::kFallbackAllowed, | |
175 net_log.get(), | |
176 GetNetworkTaskRunner(), | |
177 ui_task_runner_, | |
178 false)); | |
179 data_reduction_proxy_settings_.reset( | |
180 new data_reduction_proxy::DataReductionProxySettings()); | |
mmenke
2015/03/10 15:26:55
I'd like to have some way for external embedders t
bengr
2015/03/10 23:43:31
I added a quick and dirty approach.
| |
181 | |
137 GetNetworkTaskRunner()->PostTask( | 182 GetNetworkTaskRunner()->PostTask( |
138 FROM_HERE, | 183 FROM_HERE, |
139 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, | 184 base::Bind(&CronetURLRequestContextAdapter::InitializeOnNetworkThread, |
140 base::Unretained(this), Passed(&context_config_), | 185 base::Unretained(this), Passed(&context_config_), |
141 jcaller_ref)); | 186 Passed(&net_log), jcaller_ref)); |
142 } | 187 } |
143 | 188 |
144 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( | 189 void CronetURLRequestContextAdapter::InitializeOnNetworkThread( |
145 scoped_ptr<URLRequestContextConfig> config, | 190 scoped_ptr<URLRequestContextConfig> config, |
191 scoped_ptr<net::NetLog> net_log, | |
146 const base::android::ScopedJavaGlobalRef<jobject>& | 192 const base::android::ScopedJavaGlobalRef<jobject>& |
147 jcronet_url_request_context) { | 193 jcronet_url_request_context) { |
148 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 194 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
149 DCHECK(!is_context_initialized_); | 195 DCHECK(!is_context_initialized_); |
196 | |
197 DCHECK(data_reduction_proxy_io_data_); | |
198 DCHECK(data_reduction_proxy_io_data_->request_options()); | |
199 data_reduction_proxy_io_data_->request_options()->SetKeyOnIO( | |
200 config->data_saver_key); | |
150 // TODO(mmenke): Add method to have the builder enable SPDY. | 201 // TODO(mmenke): Add method to have the builder enable SPDY. |
151 net::URLRequestContextBuilder context_builder; | 202 net::URLRequestContextBuilder context_builder; |
152 context_builder.set_network_delegate(new BasicNetworkDelegate()); | 203 context_builder.set_network_delegate( |
153 context_builder.set_proxy_config_service( | 204 data_reduction_proxy_io_data_->CreateNetworkDelegate( |
154 new net::ProxyConfigServiceFixed(net::ProxyConfig())); | 205 make_scoped_ptr(new BasicNetworkDelegate()).Pass(), |
206 false /* No UMA is produced to track bypasses*/ ).release()); | |
207 context_builder.set_proxy_service( | |
208 net::ProxyService::CreateWithoutProxyResolver( | |
209 net::ProxyService::CreateSystemProxyConfigService( | |
210 GetNetworkTaskRunner(), nullptr), | |
211 net_log.get())); | |
155 config->ConfigureURLRequestContextBuilder(&context_builder); | 212 config->ConfigureURLRequestContextBuilder(&context_builder); |
156 | 213 context_builder.set_interceptor( |
214 data_reduction_proxy_io_data_->CreateInterceptor()); | |
215 context_builder.set_net_log(net_log.release()); | |
157 context_.reset(context_builder.Build()); | 216 context_.reset(context_builder.Build()); |
158 | 217 |
159 default_load_flags_ = net::LOAD_DO_NOT_SAVE_COOKIES | | 218 default_load_flags_ = net::LOAD_DO_NOT_SAVE_COOKIES | |
160 net::LOAD_DO_NOT_SEND_COOKIES; | 219 net::LOAD_DO_NOT_SEND_COOKIES; |
161 if (config->load_disable_cache) | 220 if (config->load_disable_cache) |
162 default_load_flags_ |= net::LOAD_DISABLE_CACHE; | 221 default_load_flags_ |= net::LOAD_DISABLE_CACHE; |
163 | 222 |
164 // Currently (circa M39) enabling QUIC requires setting probability threshold. | 223 // Currently (circa M39) enabling QUIC requires setting probability threshold. |
165 if (config->enable_quic) { | 224 if (config->enable_quic) { |
166 context_->http_server_properties() | 225 context_->http_server_properties() |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
207 | 266 |
208 JNIEnv* env = base::android::AttachCurrentThread(); | 267 JNIEnv* env = base::android::AttachCurrentThread(); |
209 Java_CronetUrlRequestContext_initNetworkThread( | 268 Java_CronetUrlRequestContext_initNetworkThread( |
210 env, jcronet_url_request_context.obj()); | 269 env, jcronet_url_request_context.obj()); |
211 | 270 |
212 is_context_initialized_ = true; | 271 is_context_initialized_ = true; |
213 while (!tasks_waiting_for_context_.empty()) { | 272 while (!tasks_waiting_for_context_.empty()) { |
214 tasks_waiting_for_context_.front().Run(); | 273 tasks_waiting_for_context_.front().Run(); |
215 tasks_waiting_for_context_.pop(); | 274 tasks_waiting_for_context_.pop(); |
216 } | 275 } |
276 ui_task_runner_->PostTask( | |
277 FROM_HERE, | |
278 base::Bind( | |
279 &CronetURLRequestContextAdapter:: | |
280 InitializeDataReductionProxyOnMainThread, | |
281 base::Unretained(this), config->enable_data_saver)); | |
282 } | |
283 | |
284 void | |
285 CronetURLRequestContextAdapter::InitializeDataReductionProxyOnMainThread( | |
286 bool enable) { | |
287 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
288 url_request_context_getter_ = | |
289 new net::TrivialURLRequestContextGetter( | |
290 GetURLRequestContext(), GetNetworkTaskRunner()); | |
291 base::TimeDelta commit_delay = base::TimeDelta(); | |
292 scoped_ptr<data_reduction_proxy::DataReductionProxyStatisticsPrefs> | |
293 statistics_prefs = make_scoped_ptr( | |
294 new data_reduction_proxy::DataReductionProxyStatisticsPrefs( | |
295 prefs_.get(), ui_task_runner_, commit_delay)); | |
296 scoped_ptr<data_reduction_proxy::DataReductionProxyService> | |
297 data_reduction_proxy_service( | |
298 new data_reduction_proxy::DataReductionProxyService( | |
299 statistics_prefs.Pass(), | |
300 data_reduction_proxy_settings_.get(), | |
301 url_request_context_getter_.get())); | |
302 data_reduction_proxy_io_data_->SetDataReductionProxyService( | |
303 data_reduction_proxy_service->GetWeakPtr()); | |
304 data_reduction_proxy_settings_->InitDataReductionProxySettings( | |
305 prefs_.get(), | |
306 data_reduction_proxy_io_data_.get(), | |
307 data_reduction_proxy_service.Pass()); | |
308 | |
309 data_reduction_proxy_settings_->SetDataReductionProxyEnabled(enable); | |
217 } | 310 } |
218 | 311 |
219 void CronetURLRequestContextAdapter::Destroy(JNIEnv* env, jobject jcaller) { | 312 void CronetURLRequestContextAdapter::Destroy(JNIEnv* env, jobject jcaller) { |
220 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); | 313 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); |
314 ui_task_runner_->PostTask( | |
315 FROM_HERE, | |
316 base::Bind( | |
317 &CronetURLRequestContextAdapter::DestroyOnMainThread, | |
318 base::Unretained(this))); | |
319 } | |
320 | |
321 void CronetURLRequestContextAdapter::DestroyOnMainThread() { | |
322 data_reduction_proxy_settings_.reset(nullptr); | |
323 data_reduction_proxy_io_data_->ShutdownOnUIThread(); | |
324 prefs_.reset(nullptr); | |
221 // Stick network_thread_ in a local, as |this| may be destroyed from the | 325 // Stick network_thread_ in a local, as |this| may be destroyed from the |
222 // network thread before delete network_thread is called. | 326 // network thread before delete network_thread is called. |
223 base::Thread* network_thread = network_thread_; | 327 base::Thread* network_thread = network_thread_; |
224 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this); | 328 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this); |
225 // Deleting thread stops it after all tasks are completed. | 329 // Deleting thread stops it after all tasks are completed. |
226 delete network_thread; | 330 delete network_thread; |
227 } | 331 } |
228 | 332 |
229 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() { | 333 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() { |
230 if (!context_) { | 334 if (!context_) { |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
324 } | 428 } |
325 | 429 |
326 static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) { | 430 static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) { |
327 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); | 431 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); |
328 // MinLogLevel is global, shared by all URLRequestContexts. | 432 // MinLogLevel is global, shared by all URLRequestContexts. |
329 logging::SetMinLogLevel(static_cast<int>(jlog_level)); | 433 logging::SetMinLogLevel(static_cast<int>(jlog_level)); |
330 return old_log_level; | 434 return old_log_level; |
331 } | 435 } |
332 | 436 |
333 } // namespace cronet | 437 } // namespace cronet |
OLD | NEW |