Chromium Code Reviews| Index: components/cronet/android/cronet_url_request_context_adapter.cc |
| diff --git a/components/cronet/android/cronet_url_request_context_adapter.cc b/components/cronet/android/cronet_url_request_context_adapter.cc |
| index fcc1ea7bf6b39c9185944a9e8e242a8b90724378..551372a651f069e0884f190c19778d4d010b7b7a 100644 |
| --- a/components/cronet/android/cronet_url_request_context_adapter.cc |
| +++ b/components/cronet/android/cronet_url_request_context_adapter.cc |
| @@ -6,6 +6,7 @@ |
| #include <limits.h> |
| #include <stddef.h> |
| +#include <stdint.h> |
| #include <map> |
| #include <utility> |
| @@ -55,6 +56,10 @@ |
| namespace { |
| const char kHttpServerProperties[] = "net.http_server_properties"; |
| +// Current version of disk cache. |
| +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
|
| +// Version number used when the version of disk cache is unknown. |
| +const uint32_t kDiskCacheVersionUnknown = 0; |
| // Connects the HttpServerPropertiesManager's storage to the prefs. |
| class PrefServiceAdapter |
| @@ -263,6 +268,59 @@ class BasicNetworkDelegate : public net::NetworkDelegateImpl { |
| DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); |
| }; |
| +bool IsCurrentVersion(const base::FilePath& version_filepath) { |
| + if (!base::PathExists(version_filepath)) |
| + return false; |
| + base::File version_file(version_filepath, |
| + base::File::FLAG_OPEN | base::File::FLAG_READ); |
| + uint32_t version = kDiskCacheVersionUnknown; |
| + int bytes_read = |
| + version_file.Read(0, reinterpret_cast<char*>(&version), sizeof(version)); |
| + if (bytes_read != sizeof(version)) { |
| + DLOG(WARNING) << "Cannot read from version file."; |
| + return false; |
| + } |
| + return version == kDiskCacheVersion; |
| +} |
| + |
| +// TODO(xunjieli): Handle failures. |
| +void InitializeCacheDiskCacheDirectory(const base::FilePath& dir) { |
| + // Checks version file and remove old versions. |
| + base::FilePath version_filepath = dir.Append("version"); |
| + if (IsCurrentVersion(version_filepath)) { |
| + // The version is up-to-date, so there is nothing to do. |
| + return; |
| + } |
| + // Delete old directory recursively and create a new directory. |
| + // base::DeleteFile returns true if the directory does not exist, so it is |
| + // fine if there is nothing on disk. |
| + 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.
|
| + DLOG(WARNING) << "Cannot purge directory."; |
| + return; |
| + } |
| + base::File new_version_file(version_filepath, base::File::FLAG_CREATE_ALWAYS | |
| + base::File::FLAG_WRITE); |
| + |
| + if (!new_version_file.IsValid()) { |
| + DLOG(WARNING) << "Cannot create a version file."; |
| + return; |
| + } |
| + |
| + DCHECK(new_version_file.created()); |
| + uint32_t new_version = kDiskCacheVersion; |
| + int bytes_written = new_version_file.Write( |
| + 0, reinterpret_cast<char*>(&new_version), sizeof(new_version)); |
| + if (bytes_written != sizeof(new_version)) { |
| + DLOG(WARNING) << "Cannot write to version file."; |
| + return; |
| + } |
| + base::FilePath prefs_dir = dir.Append(FILE_PATH_LITERAL("prefs")); |
| + if (!base::CreateDirectory(prefs_dir)) { |
| + DLOG(WARNING) << "Cannot create prefs directory"; |
| + return; |
| + } |
| +} |
| + |
| } // namespace |
| namespace cronet { |
| @@ -434,13 +492,18 @@ void CronetURLRequestContextAdapter::InitializeOnNetworkThread( |
| context_builder.set_proxy_service( |
| net::ProxyService::CreateWithoutProxyResolver( |
| std::move(proxy_config_service_), net_log_.get())); |
| + |
| config->ConfigureURLRequestContextBuilder(&context_builder, net_log_.get(), |
| GetFileThread()->task_runner()); |
| // Set up pref file if storage path is specified. |
| if (!config->storage_path.empty()) { |
| - base::FilePath filepath(config->storage_path); |
| - filepath = filepath.Append(FILE_PATH_LITERAL("local_prefs.json")); |
| + base::FilePath storage_path(config->storage_path); |
| + // Make sure disk cache directory has correct version. |
| + 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
|
| + base::FilePath filepath = |
| + 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.
|
| + .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.
|
| json_pref_store_ = new JsonPrefStore( |
| filepath, GetFileThread()->task_runner(), scoped_ptr<PrefFilter>()); |
| context_builder.SetFileTaskRunner(GetFileThread()->task_runner()); |