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/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/files/scoped_file.h" | 11 #include "base/files/scoped_file.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
14 #include "base/prefs/pref_filter.h" | 14 #include "base/prefs/pref_filter.h" |
15 #include "base/prefs/pref_registry_simple.h" | |
16 #include "base/prefs/pref_service.h" | |
17 #include "base/prefs/pref_service_factory.h" | |
15 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
16 #include "base/values.h" | 19 #include "base/values.h" |
17 #include "components/cronet/url_request_context_config.h" | 20 #include "components/cronet/url_request_context_config.h" |
18 #include "jni/CronetUrlRequestContext_jni.h" | 21 #include "jni/CronetUrlRequestContext_jni.h" |
19 #include "net/base/load_flags.h" | 22 #include "net/base/load_flags.h" |
20 #include "net/base/net_errors.h" | 23 #include "net/base/net_errors.h" |
21 #include "net/base/network_delegate_impl.h" | 24 #include "net/base/network_delegate_impl.h" |
22 #include "net/http/http_auth_handler_factory.h" | 25 #include "net/http/http_auth_handler_factory.h" |
26 #include "net/http/http_server_properties_manager.h" | |
23 #include "net/log/write_to_file_net_log_observer.h" | 27 #include "net/log/write_to_file_net_log_observer.h" |
24 #include "net/proxy/proxy_service.h" | 28 #include "net/proxy/proxy_service.h" |
25 #include "net/sdch/sdch_owner.h" | 29 #include "net/sdch/sdch_owner.h" |
26 #include "net/url_request/url_request_context.h" | 30 #include "net/url_request/url_request_context.h" |
27 #include "net/url_request/url_request_context_builder.h" | 31 #include "net/url_request/url_request_context_builder.h" |
28 #include "net/url_request/url_request_interceptor.h" | 32 #include "net/url_request/url_request_interceptor.h" |
29 | 33 |
30 #if defined(DATA_REDUCTION_PROXY_SUPPORT) | 34 #if defined(DATA_REDUCTION_PROXY_SUPPORT) |
31 #include "components/cronet/android/cronet_data_reduction_proxy.h" | 35 #include "components/cronet/android/cronet_data_reduction_proxy.h" |
32 #endif | 36 #endif |
33 | 37 |
34 namespace { | 38 namespace { |
35 | 39 |
40 const char kHttpServerProperties[] = "net.http_server_properties"; | |
41 | |
36 class BasicNetworkDelegate : public net::NetworkDelegateImpl { | 42 class BasicNetworkDelegate : public net::NetworkDelegateImpl { |
37 public: | 43 public: |
38 BasicNetworkDelegate() {} | 44 BasicNetworkDelegate() {} |
39 ~BasicNetworkDelegate() override {} | 45 ~BasicNetworkDelegate() override {} |
40 | 46 |
41 private: | 47 private: |
42 // net::NetworkDelegate implementation. | 48 // net::NetworkDelegate implementation. |
43 int OnBeforeURLRequest(net::URLRequest* request, | 49 int OnBeforeURLRequest(net::URLRequest* request, |
44 const net::CompletionCallback& callback, | 50 const net::CompletionCallback& callback, |
45 GURL* new_url) override { | 51 GURL* new_url) override { |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 interceptors.push_back(data_reduction_proxy_->CreateInterceptor()); | 187 interceptors.push_back(data_reduction_proxy_->CreateInterceptor()); |
182 context_builder.SetInterceptors(interceptors.Pass()); | 188 context_builder.SetInterceptors(interceptors.Pass()); |
183 } | 189 } |
184 #endif // defined(DATA_REDUCTION_PROXY_SUPPORT) | 190 #endif // defined(DATA_REDUCTION_PROXY_SUPPORT) |
185 context_builder.set_network_delegate(network_delegate.release()); | 191 context_builder.set_network_delegate(network_delegate.release()); |
186 context_builder.set_net_log(net_log.release()); | 192 context_builder.set_net_log(net_log.release()); |
187 context_builder.set_proxy_config_service(proxy_config_service_.release()); | 193 context_builder.set_proxy_config_service(proxy_config_service_.release()); |
188 config->ConfigureURLRequestContextBuilder(&context_builder); | 194 config->ConfigureURLRequestContextBuilder(&context_builder); |
189 | 195 |
190 // Set up pref file if storage path is specified. | 196 // Set up pref file if storage path is specified. |
191 // TODO(xunjieli): maybe get rid of the condition on sdch. | 197 if (!config->storage_path.empty()) { |
192 if (!config->storage_path.empty() && config->enable_sdch) { | |
193 base::FilePath filepath(config->storage_path); | 198 base::FilePath filepath(config->storage_path); |
194 filepath = filepath.Append(FILE_PATH_LITERAL("local_prefs.json")); | 199 filepath = filepath.Append(FILE_PATH_LITERAL("local_prefs.json")); |
195 json_pref_store_ = new JsonPrefStore( | 200 json_pref_store_ = new JsonPrefStore( |
196 filepath, GetFileThread()->task_runner(), scoped_ptr<PrefFilter>()); | 201 filepath, GetFileThread()->task_runner(), scoped_ptr<PrefFilter>()); |
197 json_pref_store_->ReadPrefsAsync(nullptr); | |
198 context_builder.SetFileTaskRunner(GetFileThread()->task_runner()); | 202 context_builder.SetFileTaskRunner(GetFileThread()->task_runner()); |
203 | |
204 // Set up HttpServerPropertiesManager. | |
205 base::PrefServiceFactory factory; | |
206 factory.set_user_prefs(json_pref_store_); | |
207 scoped_refptr<PrefRegistrySimple> registry = new PrefRegistrySimple; | |
208 registry->RegisterDictionaryPref(kHttpServerProperties, | |
209 new base::DictionaryValue()); | |
210 pref_service_ = factory.Create(registry.get()).Pass(); | |
pauljensen
2015/06/15 15:57:42
I don't know much about PrefService. Cronet could
mmenke
2015/06/15 16:36:27
Each context must have its own cache directory.
I
xunjieli
2015/06/15 18:41:36
Just for the record. Talked to Paul offline, and w
| |
211 | |
212 http_server_properties_manager_.reset(new net::HttpServerPropertiesManager( | |
213 pref_service_.get(), kHttpServerProperties, | |
214 base::MessageLoopProxy::current())); | |
215 http_server_properties_manager_->InitializeOnNetworkThread(); | |
216 context_builder.set_http_server_properties( | |
217 http_server_properties_manager_->GetWeakPtr()); | |
199 } | 218 } |
200 | 219 |
201 context_.reset(context_builder.Build()); | 220 context_.reset(context_builder.Build()); |
202 | 221 |
203 default_load_flags_ = net::LOAD_DO_NOT_SAVE_COOKIES | | 222 default_load_flags_ = net::LOAD_DO_NOT_SAVE_COOKIES | |
204 net::LOAD_DO_NOT_SEND_COOKIES; | 223 net::LOAD_DO_NOT_SEND_COOKIES; |
205 if (config->load_disable_cache) | 224 if (config->load_disable_cache) |
206 default_load_flags_ |= net::LOAD_DISABLE_CACHE; | 225 default_load_flags_ |= net::LOAD_DISABLE_CACHE; |
207 | 226 |
208 if (config->enable_sdch) { | 227 if (config->enable_sdch) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
270 tasks_waiting_for_context_.front().Run(); | 289 tasks_waiting_for_context_.front().Run(); |
271 tasks_waiting_for_context_.pop(); | 290 tasks_waiting_for_context_.pop(); |
272 } | 291 } |
273 } | 292 } |
274 | 293 |
275 void CronetURLRequestContextAdapter::Destroy(JNIEnv* env, jobject jcaller) { | 294 void CronetURLRequestContextAdapter::Destroy(JNIEnv* env, jobject jcaller) { |
276 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); | 295 DCHECK(!GetNetworkTaskRunner()->BelongsToCurrentThread()); |
277 // Stick network_thread_ in a local, as |this| may be destroyed from the | 296 // Stick network_thread_ in a local, as |this| may be destroyed from the |
278 // network thread before delete network_thread is called. | 297 // network thread before delete network_thread is called. |
279 base::Thread* network_thread = network_thread_; | 298 base::Thread* network_thread = network_thread_; |
280 GetNetworkTaskRunner()->DeleteSoon(FROM_HERE, this); | 299 PostTaskToNetworkThread( |
300 FROM_HERE, | |
301 base::Bind(&CronetURLRequestContextAdapter::DestroyOnNetworkThread, | |
302 base::Unretained(this))); | |
281 // Deleting thread stops it after all tasks are completed. | 303 // Deleting thread stops it after all tasks are completed. |
282 delete network_thread; | 304 delete network_thread; |
283 } | 305 } |
284 | 306 |
307 void CronetURLRequestContextAdapter::DestroyOnNetworkThread() { | |
pauljensen
2015/06/15 15:57:42
Why is this function needed? Can this be moved to
xunjieli
2015/06/15 18:41:36
Done. Oops. Didn't realize that we can do that.
| |
308 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | |
309 if (http_server_properties_manager_) | |
310 http_server_properties_manager_->ShutdownOnPrefThread(); | |
311 delete this; | |
312 } | |
313 | |
285 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() { | 314 net::URLRequestContext* CronetURLRequestContextAdapter::GetURLRequestContext() { |
286 if (!context_) { | 315 if (!context_) { |
287 LOG(ERROR) << "URLRequestContext is not set up"; | 316 LOG(ERROR) << "URLRequestContext is not set up"; |
288 } | 317 } |
289 return context_.get(); | 318 return context_.get(); |
290 } | 319 } |
291 | 320 |
292 void CronetURLRequestContextAdapter::PostTaskToNetworkThread( | 321 void CronetURLRequestContextAdapter::PostTaskToNetworkThread( |
293 const tracked_objects::Location& posted_from, | 322 const tracked_objects::Location& posted_from, |
294 const base::Closure& callback) { | 323 const base::Closure& callback) { |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
394 } | 423 } |
395 | 424 |
396 static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) { | 425 static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) { |
397 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); | 426 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); |
398 // MinLogLevel is global, shared by all URLRequestContexts. | 427 // MinLogLevel is global, shared by all URLRequestContexts. |
399 logging::SetMinLogLevel(static_cast<int>(jlog_level)); | 428 logging::SetMinLogLevel(static_cast<int>(jlog_level)); |
400 return old_log_level; | 429 return old_log_level; |
401 } | 430 } |
402 | 431 |
403 } // namespace cronet | 432 } // namespace cronet |
OLD | NEW |