Chromium Code Reviews| 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 <limits.h> | 7 #include <limits.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | |
| 9 #include <map> | 10 #include <map> |
| 10 #include <utility> | 11 #include <utility> |
| 11 | 12 |
| 12 #include "base/android/jni_android.h" | 13 #include "base/android/jni_android.h" |
| 13 #include "base/android/jni_array.h" | 14 #include "base/android/jni_array.h" |
| 14 #include "base/android/jni_string.h" | 15 #include "base/android/jni_string.h" |
| 15 #include "base/bind.h" | 16 #include "base/bind.h" |
| 16 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
| 17 #include "base/files/file_util.h" | 18 #include "base/files/file_util.h" |
| 18 #include "base/files/scoped_file.h" | 19 #include "base/files/scoped_file.h" |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 48 #include "net/url_request/url_request_context_builder.h" | 49 #include "net/url_request/url_request_context_builder.h" |
| 49 #include "net/url_request/url_request_interceptor.h" | 50 #include "net/url_request/url_request_interceptor.h" |
| 50 | 51 |
| 51 #if defined(DATA_REDUCTION_PROXY_SUPPORT) | 52 #if defined(DATA_REDUCTION_PROXY_SUPPORT) |
| 52 #include "components/cronet/android/cronet_data_reduction_proxy.h" | 53 #include "components/cronet/android/cronet_data_reduction_proxy.h" |
| 53 #endif | 54 #endif |
| 54 | 55 |
| 55 namespace { | 56 namespace { |
| 56 | 57 |
| 57 const char kHttpServerProperties[] = "net.http_server_properties"; | 58 const char kHttpServerProperties[] = "net.http_server_properties"; |
| 59 // Current version of disk storage. | |
| 60 const int32_t kStorageVersion = 1; | |
| 61 // Version number used when the version of disk storage is unknown. | |
| 62 const uint32_t kStorageVersionUnknown = 0; | |
| 63 // Name of preference directory. | |
| 64 const char kPrefsDirectoryName[] = "prefs"; | |
| 65 // Name of preference file. | |
| 66 const char kPrefsFileName[] = "local_prefs.json"; | |
| 58 | 67 |
| 59 // Connects the HttpServerPropertiesManager's storage to the prefs. | 68 // Connects the HttpServerPropertiesManager's storage to the prefs. |
| 60 class PrefServiceAdapter | 69 class PrefServiceAdapter |
| 61 : public net::HttpServerPropertiesManager::PrefDelegate { | 70 : public net::HttpServerPropertiesManager::PrefDelegate { |
| 62 public: | 71 public: |
| 63 explicit PrefServiceAdapter(PrefService* pref_service) | 72 explicit PrefServiceAdapter(PrefService* pref_service) |
| 64 : pref_service_(pref_service), path_(kHttpServerProperties) { | 73 : pref_service_(pref_service), path_(kHttpServerProperties) { |
| 65 pref_change_registrar_.Init(pref_service_); | 74 pref_change_registrar_.Init(pref_service_); |
| 66 } | 75 } |
| 67 | 76 |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 266 // Helper method that takes a Java string that can be null, in which case it | 275 // Helper method that takes a Java string that can be null, in which case it |
| 267 // will get converted to an empty string. | 276 // will get converted to an empty string. |
| 268 std::string ConvertNullableJavaStringToUTF8(JNIEnv* env, | 277 std::string ConvertNullableJavaStringToUTF8(JNIEnv* env, |
| 269 const JavaParamRef<jstring>& jstr) { | 278 const JavaParamRef<jstring>& jstr) { |
| 270 std::string str; | 279 std::string str; |
| 271 if (!jstr.is_null()) | 280 if (!jstr.is_null()) |
| 272 base::android::ConvertJavaStringToUTF8(env, jstr, &str); | 281 base::android::ConvertJavaStringToUTF8(env, jstr, &str); |
| 273 return str; | 282 return str; |
| 274 } | 283 } |
| 275 | 284 |
| 285 bool IsCurrentVersion(const base::FilePath& version_filepath) { | |
| 286 if (!base::PathExists(version_filepath)) | |
| 287 return false; | |
| 288 base::File version_file(version_filepath, | |
| 289 base::File::FLAG_OPEN | base::File::FLAG_READ); | |
| 290 uint32_t version = kStorageVersionUnknown; | |
| 291 int bytes_read = | |
| 292 version_file.Read(0, reinterpret_cast<char*>(&version), sizeof(version)); | |
| 293 if (bytes_read != sizeof(version)) { | |
| 294 DLOG(WARNING) << "Cannot read from version file."; | |
| 295 return false; | |
| 296 } | |
| 297 return version == kStorageVersion; | |
| 298 } | |
| 299 | |
| 300 // TODO(xunjieli): Handle failures. | |
| 301 void InitializeStorageDirectory(const base::FilePath& dir) { | |
| 302 // Checks version file and clear old storage. | |
| 303 base::FilePath version_filepath = dir.Append("version"); | |
| 304 if (IsCurrentVersion(version_filepath)) { | |
| 305 // The version is up-to-date, so there is nothing to do. | |
| 306 return; | |
| 307 } | |
| 308 // Delete old directory recursively and create a new directory. | |
| 309 // base::DeleteFile returns true if the directory does not exist, so it is | |
| 310 // fine if there is nothing on disk. | |
| 311 if (!(base::DeleteFile(dir, true) && base::CreateDirectory(dir))) { | |
| 312 DLOG(WARNING) << "Cannot purge directory."; | |
| 313 return; | |
| 314 } | |
| 315 base::File new_version_file(version_filepath, base::File::FLAG_CREATE_ALWAYS | | |
| 316 base::File::FLAG_WRITE); | |
| 317 | |
| 318 if (!new_version_file.IsValid()) { | |
| 319 DLOG(WARNING) << "Cannot create a version file."; | |
| 320 return; | |
| 321 } | |
| 322 | |
| 323 DCHECK(new_version_file.created()); | |
| 324 uint32_t new_version = kStorageVersion; | |
| 325 int bytes_written = new_version_file.Write( | |
| 326 0, reinterpret_cast<char*>(&new_version), sizeof(new_version)); | |
| 327 if (bytes_written != sizeof(new_version)) { | |
| 328 DLOG(WARNING) << "Cannot write to version file."; | |
| 329 return; | |
| 330 } | |
| 331 base::FilePath prefs_dir = dir.Append(FILE_PATH_LITERAL(kPrefsDirectoryName)); | |
| 332 if (!base::CreateDirectory(prefs_dir)) { | |
| 333 DLOG(WARNING) << "Cannot create prefs directory"; | |
| 334 return; | |
| 335 } | |
| 336 } | |
| 337 | |
| 276 } // namespace | 338 } // namespace |
| 277 | 339 |
| 278 namespace cronet { | 340 namespace cronet { |
| 279 | 341 |
| 280 // Explicitly register static JNI functions. | 342 // Explicitly register static JNI functions. |
| 281 bool CronetUrlRequestContextAdapterRegisterJni(JNIEnv* env) { | 343 bool CronetUrlRequestContextAdapterRegisterJni(JNIEnv* env) { |
| 282 return RegisterNativesImpl(env); | 344 return RegisterNativesImpl(env); |
| 283 } | 345 } |
| 284 | 346 |
| 285 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter( | 347 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter( |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 437 #endif // defined(DATA_REDUCTION_PROXY_SUPPORT) | 499 #endif // defined(DATA_REDUCTION_PROXY_SUPPORT) |
| 438 context_builder.set_network_delegate(std::move(network_delegate)); | 500 context_builder.set_network_delegate(std::move(network_delegate)); |
| 439 context_builder.set_net_log(net_log_.get()); | 501 context_builder.set_net_log(net_log_.get()); |
| 440 | 502 |
| 441 // Android provides a local HTTP proxy server that handles proxying when a PAC | 503 // Android provides a local HTTP proxy server that handles proxying when a PAC |
| 442 // URL is present. Create a proxy service without a resolver and rely on this | 504 // URL is present. Create a proxy service without a resolver and rely on this |
| 443 // local HTTP proxy. See: crbug.com/432539. | 505 // local HTTP proxy. See: crbug.com/432539. |
| 444 context_builder.set_proxy_service( | 506 context_builder.set_proxy_service( |
| 445 net::ProxyService::CreateWithoutProxyResolver( | 507 net::ProxyService::CreateWithoutProxyResolver( |
| 446 std::move(proxy_config_service_), net_log_.get())); | 508 std::move(proxy_config_service_), net_log_.get())); |
| 509 | |
| 447 config->ConfigureURLRequestContextBuilder(&context_builder, net_log_.get(), | 510 config->ConfigureURLRequestContextBuilder(&context_builder, net_log_.get(), |
| 448 GetFileThread()->task_runner()); | 511 GetFileThread()->task_runner()); |
| 449 | 512 |
| 450 // Set up pref file if storage path is specified. | 513 // Set up pref file if storage path is specified. |
| 451 if (!config->storage_path.empty()) { | 514 if (!config->storage_path.empty()) { |
| 452 base::FilePath filepath(config->storage_path); | 515 base::FilePath storage_path(config->storage_path); |
|
mef
2016/04/01 21:16:03
So, what will happen if 2 engines are racing to in
xunjieli
2016/04/04 15:01:24
That will be bad. That will happen without this ch
mef
2016/04/05 18:00:50
Acknowledged.
| |
| 453 filepath = filepath.Append(FILE_PATH_LITERAL("local_prefs.json")); | 516 // Make sure storage directory has correct version. |
| 517 InitializeStorageDirectory(storage_path); | |
| 518 base::FilePath filepath = | |
| 519 storage_path.Append(FILE_PATH_LITERAL(kPrefsDirectoryName)) | |
| 520 .Append(FILE_PATH_LITERAL(kPrefsFileName)); | |
| 454 json_pref_store_ = new JsonPrefStore( | 521 json_pref_store_ = new JsonPrefStore( |
| 455 filepath, GetFileThread()->task_runner(), scoped_ptr<PrefFilter>()); | 522 filepath, GetFileThread()->task_runner(), scoped_ptr<PrefFilter>()); |
| 456 context_builder.SetFileTaskRunner(GetFileThread()->task_runner()); | 523 context_builder.SetFileTaskRunner(GetFileThread()->task_runner()); |
| 457 | 524 |
| 458 // Set up HttpServerPropertiesManager. | 525 // Set up HttpServerPropertiesManager. |
| 459 PrefServiceFactory factory; | 526 PrefServiceFactory factory; |
| 460 factory.set_user_prefs(json_pref_store_); | 527 factory.set_user_prefs(json_pref_store_); |
| 461 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple()); | 528 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple()); |
| 462 registry->RegisterDictionaryPref(kHttpServerProperties, | 529 registry->RegisterDictionaryPref(kHttpServerProperties, |
| 463 new base::DictionaryValue()); | 530 new base::DictionaryValue()); |
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 809 JNIEnv* env, | 876 JNIEnv* env, |
| 810 const JavaParamRef<jclass>& jcaller) { | 877 const JavaParamRef<jclass>& jcaller) { |
| 811 base::StatisticsRecorder::Initialize(); | 878 base::StatisticsRecorder::Initialize(); |
| 812 std::vector<uint8_t> data; | 879 std::vector<uint8_t> data; |
| 813 if (!HistogramManager::GetInstance()->GetDeltas(&data)) | 880 if (!HistogramManager::GetInstance()->GetDeltas(&data)) |
| 814 return ScopedJavaLocalRef<jbyteArray>(); | 881 return ScopedJavaLocalRef<jbyteArray>(); |
| 815 return base::android::ToJavaByteArray(env, &data[0], data.size()); | 882 return base::android::ToJavaByteArray(env, &data[0], data.size()); |
| 816 } | 883 } |
| 817 | 884 |
| 818 } // namespace cronet | 885 } // namespace cronet |
| OLD | NEW |