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

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
« 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 <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
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 cache.
60 const int32_t kDiskCacheVersion = 1;
mef 2016/04/01 18:41:46 how would we know when to change it?
xunjieli 2016/04/01 20:20:04 If we moved things around, for instance, right now
61 // Version number used when the version of disk cache is unknown.
62 const uint32_t kDiskCacheVersionUnknown = 0;
58 63
59 // Connects the HttpServerPropertiesManager's storage to the prefs. 64 // Connects the HttpServerPropertiesManager's storage to the prefs.
60 class PrefServiceAdapter 65 class PrefServiceAdapter
61 : public net::HttpServerPropertiesManager::PrefDelegate { 66 : public net::HttpServerPropertiesManager::PrefDelegate {
62 public: 67 public:
63 explicit PrefServiceAdapter(PrefService* pref_service) 68 explicit PrefServiceAdapter(PrefService* pref_service)
64 : pref_service_(pref_service), path_(kHttpServerProperties) { 69 : pref_service_(pref_service), path_(kHttpServerProperties) {
65 pref_change_registrar_.Init(pref_service_); 70 pref_change_registrar_.Init(pref_service_);
66 } 71 }
67 72
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 } 261 }
257 262
258 bool OnCanAccessFile(const net::URLRequest& request, 263 bool OnCanAccessFile(const net::URLRequest& request,
259 const base::FilePath& path) const override { 264 const base::FilePath& path) const override {
260 return false; 265 return false;
261 } 266 }
262 267
263 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); 268 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate);
264 }; 269 };
265 270
271 bool IsCurrentVersion(const base::FilePath& version_filepath) {
272 if (!base::PathExists(version_filepath))
273 return false;
274 base::File version_file(version_filepath,
275 base::File::FLAG_OPEN | base::File::FLAG_READ);
276 uint32_t version = kDiskCacheVersionUnknown;
277 int bytes_read =
278 version_file.Read(0, reinterpret_cast<char*>(&version), sizeof(version));
279 if (bytes_read != sizeof(version)) {
280 DLOG(WARNING) << "Cannot read from version file.";
281 return false;
282 }
283 return version == kDiskCacheVersion;
284 }
285
286 // TODO(xunjieli): Handle failures.
287 void InitializeCacheDiskCacheDirectory(const base::FilePath& dir) {
288 // Checks version file and remove old versions.
289 base::FilePath version_filepath = dir.Append("version");
290 if (IsCurrentVersion(version_filepath)) {
291 // The version is up-to-date, so there is nothing to do.
292 return;
293 }
294 // Delete old directory recursively and create a new directory.
295 // base::DeleteFile returns true if the directory does not exist, so it is
296 // fine if there is nothing on disk.
297 if (!(base::DeleteFile(dir, true) && base::CreateDirectory(dir))) {
mef 2016/04/01 18:41:46 I think it makes sense to have disk cache director
xunjieli 2016/04/01 20:20:04 Done.
298 DLOG(WARNING) << "Cannot purge directory.";
299 return;
300 }
301 base::File new_version_file(version_filepath, base::File::FLAG_CREATE_ALWAYS |
302 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 uint32_t new_version = kDiskCacheVersion;
311 int bytes_written = new_version_file.Write(
312 0, reinterpret_cast<char*>(&new_version), sizeof(new_version));
313 if (bytes_written != sizeof(new_version)) {
314 DLOG(WARNING) << "Cannot write to version file.";
315 return;
316 }
317 base::FilePath prefs_dir = dir.Append(FILE_PATH_LITERAL("prefs"));
318 if (!base::CreateDirectory(prefs_dir)) {
319 DLOG(WARNING) << "Cannot create prefs directory";
320 return;
321 }
322 }
323
266 } // namespace 324 } // namespace
267 325
268 namespace cronet { 326 namespace cronet {
269 327
270 // Explicitly register static JNI functions. 328 // Explicitly register static JNI functions.
271 bool CronetUrlRequestContextAdapterRegisterJni(JNIEnv* env) { 329 bool CronetUrlRequestContextAdapterRegisterJni(JNIEnv* env) {
272 return RegisterNativesImpl(env); 330 return RegisterNativesImpl(env);
273 } 331 }
274 332
275 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter( 333 CronetURLRequestContextAdapter::CronetURLRequestContextAdapter(
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 #endif // defined(DATA_REDUCTION_PROXY_SUPPORT) 485 #endif // defined(DATA_REDUCTION_PROXY_SUPPORT)
428 context_builder.set_network_delegate(std::move(network_delegate)); 486 context_builder.set_network_delegate(std::move(network_delegate));
429 context_builder.set_net_log(net_log_.get()); 487 context_builder.set_net_log(net_log_.get());
430 488
431 // Android provides a local HTTP proxy server that handles proxying when a PAC 489 // 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 490 // URL is present. Create a proxy service without a resolver and rely on this
433 // local HTTP proxy. See: crbug.com/432539. 491 // local HTTP proxy. See: crbug.com/432539.
434 context_builder.set_proxy_service( 492 context_builder.set_proxy_service(
435 net::ProxyService::CreateWithoutProxyResolver( 493 net::ProxyService::CreateWithoutProxyResolver(
436 std::move(proxy_config_service_), net_log_.get())); 494 std::move(proxy_config_service_), net_log_.get()));
495
437 config->ConfigureURLRequestContextBuilder(&context_builder, net_log_.get(), 496 config->ConfigureURLRequestContextBuilder(&context_builder, net_log_.get(),
438 GetFileThread()->task_runner()); 497 GetFileThread()->task_runner());
439 498
440 // Set up pref file if storage path is specified. 499 // Set up pref file if storage path is specified.
441 if (!config->storage_path.empty()) { 500 if (!config->storage_path.empty()) {
442 base::FilePath filepath(config->storage_path); 501 base::FilePath storage_path(config->storage_path);
443 filepath = filepath.Append(FILE_PATH_LITERAL("local_prefs.json")); 502 // Make sure disk cache directory has correct version.
503 InitializeCacheDiskCacheDirectory(storage_path);
mef 2016/04/01 18:41:46 so, what do we do if this fails?
xunjieli 2016/04/01 20:20:04 I believe we will just fail silently. Nothing will
504 base::FilePath filepath =
505 storage_path.Append(FILE_PATH_LITERAL("prefs"))
mef 2016/04/01 18:41:46 can we have a constant for "prefs" directory name?
xunjieli 2016/04/01 20:20:04 Done.
506 .Append(FILE_PATH_LITERAL("local_prefs.json"));
mef 2016/04/01 18:41:46 can we have a constant for "prefs" file name?
xunjieli 2016/04/01 20:20:04 Done.
444 json_pref_store_ = new JsonPrefStore( 507 json_pref_store_ = new JsonPrefStore(
445 filepath, GetFileThread()->task_runner(), scoped_ptr<PrefFilter>()); 508 filepath, GetFileThread()->task_runner(), scoped_ptr<PrefFilter>());
446 context_builder.SetFileTaskRunner(GetFileThread()->task_runner()); 509 context_builder.SetFileTaskRunner(GetFileThread()->task_runner());
447 510
448 // Set up HttpServerPropertiesManager. 511 // Set up HttpServerPropertiesManager.
449 PrefServiceFactory factory; 512 PrefServiceFactory factory;
450 factory.set_user_prefs(json_pref_store_); 513 factory.set_user_prefs(json_pref_store_);
451 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple()); 514 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple());
452 registry->RegisterDictionaryPref(kHttpServerProperties, 515 registry->RegisterDictionaryPref(kHttpServerProperties,
453 new base::DictionaryValue()); 516 new base::DictionaryValue());
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after
800 JNIEnv* env, 863 JNIEnv* env,
801 const JavaParamRef<jclass>& jcaller) { 864 const JavaParamRef<jclass>& jcaller) {
802 base::StatisticsRecorder::Initialize(); 865 base::StatisticsRecorder::Initialize();
803 std::vector<uint8_t> data; 866 std::vector<uint8_t> data;
804 if (!HistogramManager::GetInstance()->GetDeltas(&data)) 867 if (!HistogramManager::GetInstance()->GetDeltas(&data))
805 return ScopedJavaLocalRef<jbyteArray>(); 868 return ScopedJavaLocalRef<jbyteArray>();
806 return base::android::ToJavaByteArray(env, &data[0], data.size()); 869 return base::android::ToJavaByteArray(env, &data[0], data.size());
807 } 870 }
808 871
809 } // namespace cronet 872 } // 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