Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(526)

Side by Side Diff: components/cronet/android/cronet_url_request_context_adapter.cc

Issue 1839283002: [Cronet] Purge storage directory if version does not match (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: self review Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 #include "net/url_request/url_request_context_builder.h" 51 #include "net/url_request/url_request_context_builder.h"
51 #include "net/url_request/url_request_interceptor.h" 52 #include "net/url_request/url_request_interceptor.h"
52 53
53 #if defined(DATA_REDUCTION_PROXY_SUPPORT) 54 #if defined(DATA_REDUCTION_PROXY_SUPPORT)
54 #include "components/cronet/android/cronet_data_reduction_proxy.h" 55 #include "components/cronet/android/cronet_data_reduction_proxy.h"
55 #endif 56 #endif
56 57
57 namespace { 58 namespace {
58 59
59 const char kHttpServerProperties[] = "net.http_server_properties"; 60 const char kHttpServerProperties[] = "net.http_server_properties";
61 // Current version of disk storage.
62 const int32_t kStorageVersion = 1;
63 // Version number used when the version of disk storage is unknown.
64 const uint32_t kStorageVersionUnknown = 0;
65 // Name of preference directory.
66 const char kPrefsDirectoryName[] = "prefs";
67 // Name of preference file.
68 const char kPrefsFileName[] = "local_prefs.json";
60 69
61 // Connects the HttpServerPropertiesManager's storage to the prefs. 70 // Connects the HttpServerPropertiesManager's storage to the prefs.
62 class PrefServiceAdapter 71 class PrefServiceAdapter
63 : public net::HttpServerPropertiesManager::PrefDelegate { 72 : public net::HttpServerPropertiesManager::PrefDelegate {
64 public: 73 public:
65 explicit PrefServiceAdapter(PrefService* pref_service) 74 explicit PrefServiceAdapter(PrefService* pref_service)
66 : pref_service_(pref_service), path_(kHttpServerProperties) { 75 : pref_service_(pref_service), path_(kHttpServerProperties) {
67 pref_change_registrar_.Init(pref_service_); 76 pref_change_registrar_.Init(pref_service_);
68 } 77 }
69 78
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 // Helper method that takes a Java string that can be null, in which case it 279 // Helper method that takes a Java string that can be null, in which case it
271 // will get converted to an empty string. 280 // will get converted to an empty string.
272 std::string ConvertNullableJavaStringToUTF8(JNIEnv* env, 281 std::string ConvertNullableJavaStringToUTF8(JNIEnv* env,
273 const JavaParamRef<jstring>& jstr) { 282 const JavaParamRef<jstring>& jstr) {
274 std::string str; 283 std::string str;
275 if (!jstr.is_null()) 284 if (!jstr.is_null())
276 base::android::ConvertJavaStringToUTF8(env, jstr, &str); 285 base::android::ConvertJavaStringToUTF8(env, jstr, &str);
277 return str; 286 return str;
278 } 287 }
279 288
289 bool IsCurrentVersion(const base::FilePath& version_filepath) {
290 if (!base::PathExists(version_filepath))
291 return false;
292 base::File version_file(version_filepath,
293 base::File::FLAG_OPEN | base::File::FLAG_READ);
294 uint32_t version = kStorageVersionUnknown;
295 int bytes_read =
296 version_file.Read(0, reinterpret_cast<char*>(&version), sizeof(version));
297 if (bytes_read != sizeof(version)) {
298 DLOG(WARNING) << "Cannot read from version file.";
299 return false;
300 }
301 return version == kStorageVersion;
302 }
303
304 // TODO(xunjieli): Handle failures.
305 void InitializeStorageDirectory(const base::FilePath& dir) {
306 // Checks version file and clear old storage.
307 base::FilePath version_filepath = dir.Append("version");
308 if (IsCurrentVersion(version_filepath)) {
309 // The version is up-to-date, so there is nothing to do.
310 return;
311 }
312 // Delete old directory recursively and create a new directory.
313 // base::DeleteFile returns true if the directory does not exist, so it is
314 // fine if there is nothing on disk.
315 if (!(base::DeleteFile(dir, true) && base::CreateDirectory(dir))) {
316 DLOG(WARNING) << "Cannot purge directory.";
317 return;
318 }
319 base::File new_version_file(version_filepath, base::File::FLAG_CREATE_ALWAYS |
320 base::File::FLAG_WRITE);
321
322 if (!new_version_file.IsValid()) {
323 DLOG(WARNING) << "Cannot create a version file.";
324 return;
325 }
326
327 DCHECK(new_version_file.created());
328 uint32_t new_version = kStorageVersion;
329 int bytes_written = new_version_file.Write(
330 0, reinterpret_cast<char*>(&new_version), sizeof(new_version));
331 if (bytes_written != sizeof(new_version)) {
332 DLOG(WARNING) << "Cannot write to version file.";
333 return;
334 }
335 base::FilePath prefs_dir = dir.Append(FILE_PATH_LITERAL(kPrefsDirectoryName));
336 if (!base::CreateDirectory(prefs_dir)) {
337 DLOG(WARNING) << "Cannot create prefs directory";
338 return;
339 }
340 }
341
280 } // namespace 342 } // namespace
281 343
282 namespace cronet { 344 namespace cronet {
283 345
284 // Explicitly register static JNI functions. 346 // Explicitly register static JNI functions.
285 bool CronetUrlRequestContextAdapterRegisterJni(JNIEnv* env) { 347 bool CronetUrlRequestContextAdapterRegisterJni(JNIEnv* env) {
286 return RegisterNativesImpl(env); 348 return RegisterNativesImpl(env);
287 } 349 }
288 350
289 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter( 351 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter(
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 #endif // defined(DATA_REDUCTION_PROXY_SUPPORT) 503 #endif // defined(DATA_REDUCTION_PROXY_SUPPORT)
442 context_builder.set_network_delegate(std::move(network_delegate)); 504 context_builder.set_network_delegate(std::move(network_delegate));
443 context_builder.set_net_log(net_log_.get()); 505 context_builder.set_net_log(net_log_.get());
444 506
445 // Android provides a local HTTP proxy server that handles proxying when a PAC 507 // Android provides a local HTTP proxy server that handles proxying when a PAC
446 // URL is present. Create a proxy service without a resolver and rely on this 508 // URL is present. Create a proxy service without a resolver and rely on this
447 // local HTTP proxy. See: crbug.com/432539. 509 // local HTTP proxy. See: crbug.com/432539.
448 context_builder.set_proxy_service( 510 context_builder.set_proxy_service(
449 net::ProxyService::CreateWithoutProxyResolver( 511 net::ProxyService::CreateWithoutProxyResolver(
450 std::move(proxy_config_service_), net_log_.get())); 512 std::move(proxy_config_service_), net_log_.get()));
513
451 config->ConfigureURLRequestContextBuilder(&context_builder, net_log_.get(), 514 config->ConfigureURLRequestContextBuilder(&context_builder, net_log_.get(),
452 GetFileThread()->task_runner()); 515 GetFileThread()->task_runner());
453 516
454 // Set up pref file if storage path is specified. 517 // Set up pref file if storage path is specified.
455 if (!config->storage_path.empty()) { 518 if (!config->storage_path.empty()) {
456 base::FilePath filepath(config->storage_path); 519 base::FilePath storage_path(config->storage_path);
457 filepath = filepath.Append(FILE_PATH_LITERAL("local_prefs.json")); 520 // Make sure storage directory has correct version.
521 InitializeStorageDirectory(storage_path);
522 base::FilePath filepath =
523 storage_path.Append(FILE_PATH_LITERAL(kPrefsDirectoryName))
524 .Append(FILE_PATH_LITERAL(kPrefsFileName));
458 json_pref_store_ = new JsonPrefStore( 525 json_pref_store_ = new JsonPrefStore(
459 filepath, GetFileThread()->task_runner(), scoped_ptr<PrefFilter>()); 526 filepath, GetFileThread()->task_runner(), scoped_ptr<PrefFilter>());
460 context_builder.SetFileTaskRunner(GetFileThread()->task_runner()); 527 context_builder.SetFileTaskRunner(GetFileThread()->task_runner());
461 528
462 // Set up HttpServerPropertiesManager. 529 // Set up HttpServerPropertiesManager.
463 PrefServiceFactory factory; 530 PrefServiceFactory factory;
464 factory.set_user_prefs(json_pref_store_); 531 factory.set_user_prefs(json_pref_store_);
465 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple()); 532 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple());
466 registry->RegisterDictionaryPref(kHttpServerProperties, 533 registry->RegisterDictionaryPref(kHttpServerProperties,
467 new base::DictionaryValue()); 534 new base::DictionaryValue());
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after
814 JNIEnv* env, 881 JNIEnv* env,
815 const JavaParamRef<jclass>& jcaller) { 882 const JavaParamRef<jclass>& jcaller) {
816 base::StatisticsRecorder::Initialize(); 883 base::StatisticsRecorder::Initialize();
817 std::vector<uint8_t> data; 884 std::vector<uint8_t> data;
818 if (!HistogramManager::GetInstance()->GetDeltas(&data)) 885 if (!HistogramManager::GetInstance()->GetDeltas(&data))
819 return ScopedJavaLocalRef<jbyteArray>(); 886 return ScopedJavaLocalRef<jbyteArray>();
820 return base::android::ToJavaByteArray(env, &data[0], data.size()); 887 return base::android::ToJavaByteArray(env, &data[0], data.size());
821 } 888 }
822 889
823 } // namespace cronet 890 } // namespace cronet
OLDNEW
« no previous file with comments | « components/cronet/android/BUILD.gn ('k') | components/cronet/android/test/javatests/src/org/chromium/net/DiskStorageTest.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698