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/message_loop/message_loop.h" |
14 #include "base/prefs/pref_filter.h" | 15 #include "base/prefs/pref_filter.h" |
| 16 #include "base/prefs/pref_registry_simple.h" |
| 17 #include "base/prefs/pref_service.h" |
| 18 #include "base/prefs/pref_service_factory.h" |
15 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
16 #include "base/values.h" | 20 #include "base/values.h" |
17 #include "components/cronet/url_request_context_config.h" | 21 #include "components/cronet/url_request_context_config.h" |
18 #include "jni/CronetUrlRequestContext_jni.h" | 22 #include "jni/CronetUrlRequestContext_jni.h" |
19 #include "net/base/load_flags.h" | 23 #include "net/base/load_flags.h" |
20 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
21 #include "net/base/network_delegate_impl.h" | 25 #include "net/base/network_delegate_impl.h" |
22 #include "net/http/http_auth_handler_factory.h" | 26 #include "net/http/http_auth_handler_factory.h" |
| 27 #include "net/http/http_server_properties_manager.h" |
23 #include "net/log/write_to_file_net_log_observer.h" | 28 #include "net/log/write_to_file_net_log_observer.h" |
24 #include "net/proxy/proxy_service.h" | 29 #include "net/proxy/proxy_service.h" |
25 #include "net/sdch/sdch_owner.h" | 30 #include "net/sdch/sdch_owner.h" |
26 #include "net/url_request/url_request_context.h" | 31 #include "net/url_request/url_request_context.h" |
27 #include "net/url_request/url_request_context_builder.h" | 32 #include "net/url_request/url_request_context_builder.h" |
28 #include "net/url_request/url_request_interceptor.h" | 33 #include "net/url_request/url_request_interceptor.h" |
29 | 34 |
30 #if defined(DATA_REDUCTION_PROXY_SUPPORT) | 35 #if defined(DATA_REDUCTION_PROXY_SUPPORT) |
31 #include "components/cronet/android/cronet_data_reduction_proxy.h" | 36 #include "components/cronet/android/cronet_data_reduction_proxy.h" |
32 #endif | 37 #endif |
33 | 38 |
34 namespace { | 39 namespace { |
35 | 40 |
| 41 const char kHttpServerProperties[] = "net.http_server_properties"; |
| 42 |
36 class BasicNetworkDelegate : public net::NetworkDelegateImpl { | 43 class BasicNetworkDelegate : public net::NetworkDelegateImpl { |
37 public: | 44 public: |
38 BasicNetworkDelegate() {} | 45 BasicNetworkDelegate() {} |
39 ~BasicNetworkDelegate() override {} | 46 ~BasicNetworkDelegate() override {} |
40 | 47 |
41 private: | 48 private: |
42 // net::NetworkDelegate implementation. | 49 // net::NetworkDelegate implementation. |
43 int OnBeforeURLRequest(net::URLRequest* request, | 50 int OnBeforeURLRequest(net::URLRequest* request, |
44 const net::CompletionCallback& callback, | 51 const net::CompletionCallback& callback, |
45 GURL* new_url) override { | 52 GURL* new_url) override { |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 context_config_(context_config.Pass()), | 128 context_config_(context_config.Pass()), |
122 is_context_initialized_(false), | 129 is_context_initialized_(false), |
123 default_load_flags_(net::LOAD_NORMAL) { | 130 default_load_flags_(net::LOAD_NORMAL) { |
124 base::Thread::Options options; | 131 base::Thread::Options options; |
125 options.message_loop_type = base::MessageLoop::TYPE_IO; | 132 options.message_loop_type = base::MessageLoop::TYPE_IO; |
126 network_thread_->StartWithOptions(options); | 133 network_thread_->StartWithOptions(options); |
127 } | 134 } |
128 | 135 |
129 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { | 136 CronetURLRequestContextAdapter::~CronetURLRequestContextAdapter() { |
130 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); | 137 DCHECK(GetNetworkTaskRunner()->BelongsToCurrentThread()); |
| 138 |
| 139 if (http_server_properties_manager_) |
| 140 http_server_properties_manager_->ShutdownOnPrefThread(); |
| 141 if (pref_service_) |
| 142 pref_service_->CommitPendingWrite(); |
131 StopNetLogOnNetworkThread(); | 143 StopNetLogOnNetworkThread(); |
132 } | 144 } |
133 | 145 |
134 void CronetURLRequestContextAdapter::InitRequestContextOnMainThread( | 146 void CronetURLRequestContextAdapter::InitRequestContextOnMainThread( |
135 JNIEnv* env, | 147 JNIEnv* env, |
136 jobject jcaller) { | 148 jobject jcaller) { |
137 base::android::ScopedJavaGlobalRef<jobject> jcaller_ref; | 149 base::android::ScopedJavaGlobalRef<jobject> jcaller_ref; |
138 jcaller_ref.Reset(env, jcaller); | 150 jcaller_ref.Reset(env, jcaller); |
139 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( | 151 proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( |
140 GetNetworkTaskRunner(), nullptr)); | 152 GetNetworkTaskRunner(), nullptr)); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 interceptors.push_back(data_reduction_proxy_->CreateInterceptor()); | 189 interceptors.push_back(data_reduction_proxy_->CreateInterceptor()); |
178 context_builder.SetInterceptors(interceptors.Pass()); | 190 context_builder.SetInterceptors(interceptors.Pass()); |
179 } | 191 } |
180 #endif // defined(DATA_REDUCTION_PROXY_SUPPORT) | 192 #endif // defined(DATA_REDUCTION_PROXY_SUPPORT) |
181 context_builder.set_network_delegate(network_delegate.release()); | 193 context_builder.set_network_delegate(network_delegate.release()); |
182 context_builder.set_net_log(net_log.release()); | 194 context_builder.set_net_log(net_log.release()); |
183 context_builder.set_proxy_config_service(proxy_config_service_.release()); | 195 context_builder.set_proxy_config_service(proxy_config_service_.release()); |
184 config->ConfigureURLRequestContextBuilder(&context_builder); | 196 config->ConfigureURLRequestContextBuilder(&context_builder); |
185 | 197 |
186 // Set up pref file if storage path is specified. | 198 // Set up pref file if storage path is specified. |
187 // TODO(xunjieli): maybe get rid of the condition on sdch. | 199 if (!config->storage_path.empty()) { |
188 if (!config->storage_path.empty() && config->enable_sdch) { | |
189 base::FilePath filepath(config->storage_path); | 200 base::FilePath filepath(config->storage_path); |
190 filepath = filepath.Append(FILE_PATH_LITERAL("local_prefs.json")); | 201 filepath = filepath.Append(FILE_PATH_LITERAL("local_prefs.json")); |
191 json_pref_store_ = new JsonPrefStore( | 202 json_pref_store_ = new JsonPrefStore( |
192 filepath, GetFileThread()->task_runner(), scoped_ptr<PrefFilter>()); | 203 filepath, GetFileThread()->task_runner(), scoped_ptr<PrefFilter>()); |
193 json_pref_store_->ReadPrefsAsync(nullptr); | |
194 context_builder.SetFileTaskRunner(GetFileThread()->task_runner()); | 204 context_builder.SetFileTaskRunner(GetFileThread()->task_runner()); |
| 205 |
| 206 // Set up HttpServerPropertiesManager. |
| 207 base::PrefServiceFactory factory; |
| 208 factory.set_user_prefs(json_pref_store_); |
| 209 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple()); |
| 210 registry->RegisterDictionaryPref(kHttpServerProperties, |
| 211 new base::DictionaryValue()); |
| 212 pref_service_ = factory.Create(registry.get()).Pass(); |
| 213 |
| 214 scoped_ptr<net::HttpServerPropertiesManager> http_server_properties_manager( |
| 215 new net::HttpServerPropertiesManager(pref_service_.get(), |
| 216 kHttpServerProperties, |
| 217 GetNetworkTaskRunner())); |
| 218 http_server_properties_manager->InitializeOnNetworkThread(); |
| 219 http_server_properties_manager_ = http_server_properties_manager.get(); |
| 220 context_builder.SetHttpServerProperties( |
| 221 http_server_properties_manager.Pass()); |
195 } | 222 } |
196 | 223 |
197 context_.reset(context_builder.Build()); | 224 context_.reset(context_builder.Build()); |
198 | 225 |
199 default_load_flags_ = net::LOAD_DO_NOT_SAVE_COOKIES | | 226 default_load_flags_ = net::LOAD_DO_NOT_SAVE_COOKIES | |
200 net::LOAD_DO_NOT_SEND_COOKIES; | 227 net::LOAD_DO_NOT_SEND_COOKIES; |
201 if (config->load_disable_cache) | 228 if (config->load_disable_cache) |
202 default_load_flags_ |= net::LOAD_DISABLE_CACHE; | 229 default_load_flags_ |= net::LOAD_DISABLE_CACHE; |
203 | 230 |
204 if (config->enable_sdch) { | 231 if (config->enable_sdch) { |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 } | 417 } |
391 | 418 |
392 static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) { | 419 static jint SetMinLogLevel(JNIEnv* env, jclass jcaller, jint jlog_level) { |
393 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); | 420 jint old_log_level = static_cast<jint>(logging::GetMinLogLevel()); |
394 // MinLogLevel is global, shared by all URLRequestContexts. | 421 // MinLogLevel is global, shared by all URLRequestContexts. |
395 logging::SetMinLogLevel(static_cast<int>(jlog_level)); | 422 logging::SetMinLogLevel(static_cast<int>(jlog_level)); |
396 return old_log_level; | 423 return old_log_level; |
397 } | 424 } |
398 | 425 |
399 } // namespace cronet | 426 } // namespace cronet |
OLD | NEW |