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

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: 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
« no previous file with comments | « no previous file | components/cronet/android/test/javatests/src/org/chromium/net/DiskCacheTest.java » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <map> 9 #include <map>
10 #include <utility> 10 #include <utility>
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 #include "net/url_request/url_request_context_builder.h" 48 #include "net/url_request/url_request_context_builder.h"
49 #include "net/url_request/url_request_interceptor.h" 49 #include "net/url_request/url_request_interceptor.h"
50 50
51 #if defined(DATA_REDUCTION_PROXY_SUPPORT) 51 #if defined(DATA_REDUCTION_PROXY_SUPPORT)
52 #include "components/cronet/android/cronet_data_reduction_proxy.h" 52 #include "components/cronet/android/cronet_data_reduction_proxy.h"
53 #endif 53 #endif
54 54
55 namespace { 55 namespace {
56 56
57 const char kHttpServerProperties[] = "net.http_server_properties"; 57 const char kHttpServerProperties[] = "net.http_server_properties";
58 // Current version of disk cache.
59 const int32_t kDiskCacheVersion = 1;
60
61 struct VersionHeader {
62 VersionHeader() { std::memset(this, 0, sizeof(*this)); };
mmenke 2016/03/31 15:39:07 Think we should just just direct initialize versio
xunjieli 2016/03/31 18:45:58 Done.
63 uint32_t version;
mmenke 2016/03/31 15:39:07 include <stdint.h>
xunjieli 2016/03/31 18:45:58 Done.
64 };
58 65
59 // Connects the HttpServerPropertiesManager's storage to the prefs. 66 // Connects the HttpServerPropertiesManager's storage to the prefs.
60 class PrefServiceAdapter 67 class PrefServiceAdapter
61 : public net::HttpServerPropertiesManager::PrefDelegate { 68 : public net::HttpServerPropertiesManager::PrefDelegate {
62 public: 69 public:
63 explicit PrefServiceAdapter(PrefService* pref_service) 70 explicit PrefServiceAdapter(PrefService* pref_service)
64 : pref_service_(pref_service), path_(kHttpServerProperties) { 71 : pref_service_(pref_service), path_(kHttpServerProperties) {
65 pref_change_registrar_.Init(pref_service_); 72 pref_change_registrar_.Init(pref_service_);
66 } 73 }
67 74
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 } 263 }
257 264
258 bool OnCanAccessFile(const net::URLRequest& request, 265 bool OnCanAccessFile(const net::URLRequest& request,
259 const base::FilePath& path) const override { 266 const base::FilePath& path) const override {
260 return false; 267 return false;
261 } 268 }
262 269
263 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); 270 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate);
264 }; 271 };
265 272
273 bool IsCurrentVersion(const base::FilePath& version_filepath) {
274 if (!base::PathExists(version_filepath))
275 return false;
276 base::File version_file(version_filepath,
277 base::File::FLAG_OPEN | base::File::FLAG_READ);
278 VersionHeader version;
279 int bytes_read =
280 version_file.Read(0, reinterpret_cast<char*>(&version), sizeof(version));
281 if (bytes_read != sizeof(version)) {
282 DLOG(WARNING) << "Cannot read from version file.";
283 return false;
284 }
285 return version.version == kDiskCacheVersion;
286 }
287
288 // TODO(xunjieli): Handle failures.
289 void InitializeCacheDiskCacheDirectory(const base::FilePath& dir) {
290 // Checks version file and remove old versions.
291 base::FilePath version_filepath = dir.Append("version");
292 if (IsCurrentVersion(version_filepath)) {
293 // The version is up-to-date, so there is nothing to do.
294 return;
295 }
296 if (!(base::DeleteFile(dir, true) && base::CreateDirectory(dir))) {
mmenke 2016/03/31 15:39:07 Think it's worth a comment that DeleteFile returns
xunjieli 2016/03/31 18:45:58 Done.
297 DLOG(WARNING) << "Cannot purge directory.";
298 return;
299 }
300 base::FilePath new_version = dir.Append("version");
mmenke 2016/03/31 15:39:07 This is the same as version_filepath. Just using
xunjieli 2016/03/31 18:45:58 Done.
301 base::File new_version_file(
302 new_version, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
303
304 if (!new_version_file.IsValid()) {
305 DLOG(WARNING) << "Cannot create a version file.";
306 return;
307 }
308
309 DCHECK(new_version_file.created());
310 VersionHeader version;
311 version.version = kDiskCacheVersion;
312 int bytes_written = new_version_file.Write(
313 0, reinterpret_cast<char*>(&version), sizeof(version));
314 if (bytes_written != sizeof(version)) {
315 DLOG(WARNING) << "Cannot write to version file.";
316 return;
317 }
318 base::FilePath prefs_dir = dir.Append(FILE_PATH_LITERAL("prefs"));
319 if (!base::CreateDirectory(prefs_dir)) {
320 DLOG(WARNING) << "Cannot create prefs directory";
321 return;
322 }
323 }
324
266 } // namespace 325 } // namespace
267 326
268 namespace cronet { 327 namespace cronet {
269 328
270 // Explicitly register static JNI functions. 329 // Explicitly register static JNI functions.
271 bool CronetUrlRequestContextAdapterRegisterJni(JNIEnv* env) { 330 bool CronetUrlRequestContextAdapterRegisterJni(JNIEnv* env) {
272 return RegisterNativesImpl(env); 331 return RegisterNativesImpl(env);
273 } 332 }
274 333
275 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter( 334 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter(
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 #endif // defined(DATA_REDUCTION_PROXY_SUPPORT) 486 #endif // defined(DATA_REDUCTION_PROXY_SUPPORT)
428 context_builder.set_network_delegate(std::move(network_delegate)); 487 context_builder.set_network_delegate(std::move(network_delegate));
429 context_builder.set_net_log(net_log_.get()); 488 context_builder.set_net_log(net_log_.get());
430 489
431 // Android provides a local HTTP proxy server that handles proxying when a PAC 490 // Android provides a local HTTP proxy server that handles proxying when a PAC
432 // URL is present. Create a proxy service without a resolver and rely on this 491 // URL is present. Create a proxy service without a resolver and rely on this
433 // local HTTP proxy. See: crbug.com/432539. 492 // local HTTP proxy. See: crbug.com/432539.
434 context_builder.set_proxy_service( 493 context_builder.set_proxy_service(
435 net::ProxyService::CreateWithoutProxyResolver( 494 net::ProxyService::CreateWithoutProxyResolver(
436 std::move(proxy_config_service_), net_log_.get())); 495 std::move(proxy_config_service_), net_log_.get()));
496
437 config->ConfigureURLRequestContextBuilder(&context_builder, net_log_.get(), 497 config->ConfigureURLRequestContextBuilder(&context_builder, net_log_.get(),
438 GetFileThread()->task_runner()); 498 GetFileThread()->task_runner());
439 499
440 // Set up pref file if storage path is specified. 500 // Set up pref file if storage path is specified.
441 if (!config->storage_path.empty()) { 501 if (!config->storage_path.empty()) {
442 base::FilePath filepath(config->storage_path); 502 base::FilePath storage_path(config->storage_path);
443 filepath = filepath.Append(FILE_PATH_LITERAL("local_prefs.json")); 503 // Make sure disk cache directory has correct version.
504 InitializeCacheDiskCacheDirectory(storage_path);
mmenke 2016/03/31 15:39:07 We're doing file IO on the network thread. This d
xunjieli 2016/03/31 18:45:58 No checks are triggered when I run the tests local
505 base::FilePath filepath =
506 storage_path.Append(FILE_PATH_LITERAL("prefs"))
507 .Append(FILE_PATH_LITERAL("local_prefs.json"));
444 json_pref_store_ = new JsonPrefStore( 508 json_pref_store_ = new JsonPrefStore(
445 filepath, GetFileThread()->task_runner(), scoped_ptr<PrefFilter>()); 509 filepath, GetFileThread()->task_runner(), scoped_ptr<PrefFilter>());
446 context_builder.SetFileTaskRunner(GetFileThread()->task_runner()); 510 context_builder.SetFileTaskRunner(GetFileThread()->task_runner());
447 511
448 // Set up HttpServerPropertiesManager. 512 // Set up HttpServerPropertiesManager.
449 PrefServiceFactory factory; 513 PrefServiceFactory factory;
450 factory.set_user_prefs(json_pref_store_); 514 factory.set_user_prefs(json_pref_store_);
451 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple()); 515 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple());
452 registry->RegisterDictionaryPref(kHttpServerProperties, 516 registry->RegisterDictionaryPref(kHttpServerProperties,
453 new base::DictionaryValue()); 517 new base::DictionaryValue());
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after
800 JNIEnv* env, 864 JNIEnv* env,
801 const JavaParamRef<jclass>& jcaller) { 865 const JavaParamRef<jclass>& jcaller) {
802 base::StatisticsRecorder::Initialize(); 866 base::StatisticsRecorder::Initialize();
803 std::vector<uint8_t> data; 867 std::vector<uint8_t> data;
804 if (!HistogramManager::GetInstance()->GetDeltas(&data)) 868 if (!HistogramManager::GetInstance()->GetDeltas(&data))
805 return ScopedJavaLocalRef<jbyteArray>(); 869 return ScopedJavaLocalRef<jbyteArray>();
806 return base::android::ToJavaByteArray(env, &data[0], data.size()); 870 return base::android::ToJavaByteArray(env, &data[0], data.size());
807 } 871 }
808 872
809 } // namespace cronet 873 } // namespace cronet
OLDNEW
« no previous file with comments | « no previous file | components/cronet/android/test/javatests/src/org/chromium/net/DiskCacheTest.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698