Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/browser/service_worker/service_worker_storage.h" | 5 #include "content/browser/service_worker/service_worker_storage.h" |
| 6 | 6 |
| 7 #include "base/bind_helpers.h" | 7 #include "base/bind_helpers.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/sequenced_task_runner.h" | 10 #include "base/sequenced_task_runner.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/task_runner_util.h" | 12 #include "base/task_runner_util.h" |
| 13 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
| 14 #include "base/trace_event/trace_event.h" | 14 #include "base/trace_event/trace_event.h" |
| 15 #include "content/browser/service_worker/service_worker_context_core.h" | 15 #include "content/browser/service_worker/service_worker_context_core.h" |
| 16 #include "content/browser/service_worker/service_worker_disk_cache.h" | 16 #include "content/browser/service_worker/service_worker_disk_cache.h" |
| 17 #include "content/browser/service_worker/service_worker_disk_cache_migrator.h" | |
| 17 #include "content/browser/service_worker/service_worker_info.h" | 18 #include "content/browser/service_worker/service_worker_info.h" |
| 18 #include "content/browser/service_worker/service_worker_metrics.h" | |
| 19 #include "content/browser/service_worker/service_worker_registration.h" | 19 #include "content/browser/service_worker/service_worker_registration.h" |
| 20 #include "content/browser/service_worker/service_worker_utils.h" | 20 #include "content/browser/service_worker/service_worker_utils.h" |
| 21 #include "content/browser/service_worker/service_worker_version.h" | 21 #include "content/browser/service_worker/service_worker_version.h" |
| 22 #include "content/common/service_worker/service_worker_types.h" | 22 #include "content/common/service_worker/service_worker_types.h" |
| 23 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 24 #include "net/base/completion_callback.h" | 24 #include "net/base/completion_callback.h" |
| 25 #include "net/base/io_buffer.h" | 25 #include "net/base/io_buffer.h" |
| 26 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
| 27 #include "storage/browser/quota/quota_manager_proxy.h" | 27 #include "storage/browser/quota/quota_manager_proxy.h" |
| 28 #include "storage/browser/quota/special_storage_policy.h" | 28 #include "storage/browser/quota/special_storage_policy.h" |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 50 | 50 |
| 51 void CompleteFindSoon( | 51 void CompleteFindSoon( |
| 52 const tracked_objects::Location& from_here, | 52 const tracked_objects::Location& from_here, |
| 53 const scoped_refptr<ServiceWorkerRegistration>& registration, | 53 const scoped_refptr<ServiceWorkerRegistration>& registration, |
| 54 ServiceWorkerStatusCode status, | 54 ServiceWorkerStatusCode status, |
| 55 const ServiceWorkerStorage::FindRegistrationCallback& callback) { | 55 const ServiceWorkerStorage::FindRegistrationCallback& callback) { |
| 56 RunSoon(from_here, | 56 RunSoon(from_here, |
| 57 base::Bind(&CompleteFindNow, registration, status, callback)); | 57 base::Bind(&CompleteFindNow, registration, status, callback)); |
| 58 } | 58 } |
| 59 | 59 |
| 60 const base::FilePath::CharType kDatabaseName[] = | 60 const char kDatabaseName[] = "Database"; |
| 61 FILE_PATH_LITERAL("Database"); | 61 const char kDiskCacheName[] = "ScriptCache"; |
| 62 const base::FilePath::CharType kDiskCacheName[] = | 62 const char kOldDiskCacheName[] = "Cache"; |
| 63 FILE_PATH_LITERAL("Cache"); | 63 |
| 64 // Version 2 means that the diskcache was migrated from BlockFile backend to | |
| 65 // Simple backend (http://crbug.com/487482). | |
|
michaeln
2015/06/11 00:52:38
How are we handling android? It already uses the s
nhiroki
2015/06/11 21:04:50
Done in the separate CL:
https://codereview.chromi
| |
| 66 const int64 kCurrentDiskCacheVersion = 2; | |
| 64 | 67 |
| 65 const int kMaxMemDiskCacheSize = 10 * 1024 * 1024; | 68 const int kMaxMemDiskCacheSize = 10 * 1024 * 1024; |
| 66 const int kMaxDiskCacheSize = 250 * 1024 * 1024; | 69 const int kMaxDiskCacheSize = 250 * 1024 * 1024; |
| 67 | 70 |
| 68 ServiceWorkerStatusCode DatabaseStatusToStatusCode( | 71 ServiceWorkerStatusCode DatabaseStatusToStatusCode( |
| 69 ServiceWorkerDatabase::Status status) { | 72 ServiceWorkerDatabase::Status status) { |
| 70 switch (status) { | 73 switch (status) { |
| 71 case ServiceWorkerDatabase::STATUS_OK: | 74 case ServiceWorkerDatabase::STATUS_OK: |
| 72 return SERVICE_WORKER_OK; | 75 return SERVICE_WORKER_OK; |
| 73 case ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND: | 76 case ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND: |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 : registration_id(kInvalidServiceWorkerRegistrationId) { | 221 : registration_id(kInvalidServiceWorkerRegistrationId) { |
| 219 } | 222 } |
| 220 | 223 |
| 221 ServiceWorkerStorage:: | 224 ServiceWorkerStorage:: |
| 222 DidDeleteRegistrationParams::~DidDeleteRegistrationParams() { | 225 DidDeleteRegistrationParams::~DidDeleteRegistrationParams() { |
| 223 } | 226 } |
| 224 | 227 |
| 225 ServiceWorkerStorage::~ServiceWorkerStorage() { | 228 ServiceWorkerStorage::~ServiceWorkerStorage() { |
| 226 ClearSessionOnlyOrigins(); | 229 ClearSessionOnlyOrigins(); |
| 227 weak_factory_.InvalidateWeakPtrs(); | 230 weak_factory_.InvalidateWeakPtrs(); |
| 231 if (disk_cache_migrator_) { | |
| 232 // The migrator will be aborted during the migration. To sweep out partial | |
| 233 // results before the next attempt, register the diskcache directory as a | |
| 234 // purgeable file. | |
| 235 database_task_manager_->GetTaskRunner()->PostTask( | |
| 236 FROM_HERE, | |
| 237 base::Bind( | |
| 238 base::IgnoreResult(&ServiceWorkerDatabase::WritePurgeableFile), | |
| 239 base::Unretained(database_.get()), kDiskCacheName)); | |
| 240 } | |
| 228 database_task_manager_->GetTaskRunner()->DeleteSoon(FROM_HERE, | 241 database_task_manager_->GetTaskRunner()->DeleteSoon(FROM_HERE, |
| 229 database_.release()); | 242 database_.release()); |
| 230 } | 243 } |
| 231 | 244 |
| 232 // static | 245 // static |
| 233 scoped_ptr<ServiceWorkerStorage> ServiceWorkerStorage::Create( | 246 scoped_ptr<ServiceWorkerStorage> ServiceWorkerStorage::Create( |
| 234 const base::FilePath& path, | 247 const base::FilePath& path, |
| 235 base::WeakPtr<ServiceWorkerContextCore> context, | 248 base::WeakPtr<ServiceWorkerContextCore> context, |
| 236 scoped_ptr<ServiceWorkerDatabaseTaskManager> database_task_manager, | 249 scoped_ptr<ServiceWorkerDatabaseTaskManager> database_task_manager, |
| 237 const scoped_refptr<base::SingleThreadTaskRunner>& disk_cache_thread, | 250 const scoped_refptr<base::SingleThreadTaskRunner>& disk_cache_thread, |
| (...skipping 684 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 922 : next_registration_id_(kInvalidServiceWorkerRegistrationId), | 935 : next_registration_id_(kInvalidServiceWorkerRegistrationId), |
| 923 next_version_id_(kInvalidServiceWorkerVersionId), | 936 next_version_id_(kInvalidServiceWorkerVersionId), |
| 924 next_resource_id_(kInvalidServiceWorkerResourceId), | 937 next_resource_id_(kInvalidServiceWorkerResourceId), |
| 925 state_(UNINITIALIZED), | 938 state_(UNINITIALIZED), |
| 926 path_(path), | 939 path_(path), |
| 927 context_(context), | 940 context_(context), |
| 928 database_task_manager_(database_task_manager.Pass()), | 941 database_task_manager_(database_task_manager.Pass()), |
| 929 disk_cache_thread_(disk_cache_thread), | 942 disk_cache_thread_(disk_cache_thread), |
| 930 quota_manager_proxy_(quota_manager_proxy), | 943 quota_manager_proxy_(quota_manager_proxy), |
| 931 special_storage_policy_(special_storage_policy), | 944 special_storage_policy_(special_storage_policy), |
| 945 current_disk_cache_version_(-1), | |
| 932 is_purge_pending_(false), | 946 is_purge_pending_(false), |
| 933 has_checked_for_stale_resources_(false), | 947 has_checked_for_stale_resources_(false), |
| 934 weak_factory_(this) { | 948 weak_factory_(this) { |
| 935 database_.reset(new ServiceWorkerDatabase(GetDatabasePath())); | 949 database_.reset(new ServiceWorkerDatabase(GetDatabasePath())); |
| 936 } | 950 } |
| 937 | 951 |
| 938 base::FilePath ServiceWorkerStorage::GetDatabasePath() { | 952 base::FilePath ServiceWorkerStorage::GetDatabasePath() { |
| 939 if (path_.empty()) | 953 if (path_.empty()) |
| 940 return base::FilePath(); | 954 return base::FilePath(); |
| 941 return path_.Append(ServiceWorkerContextCore::kServiceWorkerDirectory) | 955 return path_.Append(ServiceWorkerContextCore::kServiceWorkerDirectory) |
| 942 .Append(kDatabaseName); | 956 .AppendASCII(kDatabaseName); |
| 943 } | 957 } |
| 944 | 958 |
| 945 base::FilePath ServiceWorkerStorage::GetDiskCachePath() { | 959 base::FilePath ServiceWorkerStorage::GetDiskCachePath() { |
| 946 if (path_.empty()) | 960 if (path_.empty()) |
| 947 return base::FilePath(); | 961 return base::FilePath(); |
| 948 return path_.Append(ServiceWorkerContextCore::kServiceWorkerDirectory) | 962 return path_.Append(ServiceWorkerContextCore::kServiceWorkerDirectory) |
| 949 .Append(kDiskCacheName); | 963 .AppendASCII(kDiskCacheName); |
| 964 } | |
| 965 | |
| 966 base::FilePath ServiceWorkerStorage::GetOldDiskCachePath() { | |
| 967 if (path_.empty()) | |
| 968 return base::FilePath(); | |
| 969 return path_.Append(ServiceWorkerContextCore::kServiceWorkerDirectory) | |
| 970 .AppendASCII(kOldDiskCacheName); | |
| 950 } | 971 } |
| 951 | 972 |
| 952 bool ServiceWorkerStorage::LazyInitialize(const base::Closure& callback) { | 973 bool ServiceWorkerStorage::LazyInitialize(const base::Closure& callback) { |
| 953 if (!context_) | 974 if (!context_) |
| 954 return false; | 975 return false; |
| 955 | 976 |
| 956 switch (state_) { | 977 switch (state_) { |
| 957 case INITIALIZED: | 978 case INITIALIZED: |
| 958 return true; | 979 return true; |
| 959 case DISABLED: | 980 case DISABLED: |
| 960 return false; | 981 return false; |
| 961 case INITIALIZING: | 982 case INITIALIZING: |
| 962 pending_tasks_.push_back(callback); | 983 pending_tasks_.push_back(callback); |
| 963 return false; | 984 return false; |
| 964 case UNINITIALIZED: | 985 case UNINITIALIZED: |
| 965 pending_tasks_.push_back(callback); | 986 pending_tasks_.push_back(callback); |
| 966 // Fall-through. | 987 // Fall-through. |
| 967 } | 988 } |
| 968 | 989 |
| 969 state_ = INITIALIZING; | 990 state_ = INITIALIZING; |
| 970 database_task_manager_->GetTaskRunner()->PostTask( | 991 database_task_manager_->GetTaskRunner()->PostTask( |
| 971 FROM_HERE, | 992 FROM_HERE, |
| 972 base::Bind(&ReadInitialDataFromDB, | 993 base::Bind(&ReadInitialDataFromDB, database_.get(), |
| 973 database_.get(), | |
| 974 base::ThreadTaskRunnerHandle::Get(), | 994 base::ThreadTaskRunnerHandle::Get(), |
| 975 base::Bind(&ServiceWorkerStorage::DidReadInitialData, | 995 base::Bind(&ServiceWorkerStorage::DidReadInitialData, |
| 976 weak_factory_.GetWeakPtr()))); | 996 weak_factory_.GetWeakPtr()))); |
| 977 return false; | 997 return false; |
| 978 } | 998 } |
| 979 | 999 |
| 980 void ServiceWorkerStorage::DidReadInitialData( | 1000 void ServiceWorkerStorage::DidReadInitialData( |
| 981 InitialData* data, | 1001 InitialData* data, |
| 1002 int64 current_disk_cache_version, | |
| 1003 const std::vector<std::string>& purgeable_files, | |
| 982 ServiceWorkerDatabase::Status status) { | 1004 ServiceWorkerDatabase::Status status) { |
| 983 DCHECK(data); | 1005 DCHECK(data); |
| 984 DCHECK_EQ(INITIALIZING, state_); | 1006 DCHECK_EQ(INITIALIZING, state_); |
| 985 | 1007 |
| 986 if (status == ServiceWorkerDatabase::STATUS_OK) { | 1008 if (status != ServiceWorkerDatabase::STATUS_OK) { |
| 987 next_registration_id_ = data->next_registration_id; | |
| 988 next_version_id_ = data->next_version_id; | |
| 989 next_resource_id_ = data->next_resource_id; | |
| 990 registered_origins_.swap(data->origins); | |
| 991 state_ = INITIALIZED; | |
| 992 } else { | |
| 993 DVLOG(2) << "Failed to initialize: " | 1009 DVLOG(2) << "Failed to initialize: " |
| 994 << ServiceWorkerDatabase::StatusToString(status); | 1010 << ServiceWorkerDatabase::StatusToString(status); |
| 995 ScheduleDeleteAndStartOver(); | 1011 ScheduleDeleteAndStartOver(); |
| 1012 | |
| 1013 // These tasks should run before DeleteAndStartOver is complete, and should | |
| 1014 // fail due to the disabled storage. | |
| 1015 for (const base::Closure& pending_task : pending_tasks_) | |
| 1016 RunSoon(FROM_HERE, pending_task); | |
| 1017 pending_tasks_.clear(); | |
| 1018 return; | |
| 996 } | 1019 } |
| 997 | 1020 |
| 998 for (std::vector<base::Closure>::const_iterator it = pending_tasks_.begin(); | 1021 next_registration_id_ = data->next_registration_id; |
| 999 it != pending_tasks_.end(); ++it) { | 1022 next_version_id_ = data->next_version_id; |
| 1000 RunSoon(FROM_HERE, *it); | 1023 next_resource_id_ = data->next_resource_id; |
| 1024 registered_origins_.swap(data->origins); | |
| 1025 state_ = INITIALIZED; | |
| 1026 current_disk_cache_version_ = current_disk_cache_version; | |
| 1027 | |
| 1028 for (const base::Closure& pending_task : pending_tasks_) | |
| 1029 RunSoon(FROM_HERE, pending_task); | |
| 1030 pending_tasks_.clear(); | |
| 1031 | |
| 1032 if (path_.empty()) { | |
| 1033 // On-memory storage should not have to purge files. | |
| 1034 DCHECK(purgeable_files.empty()); | |
| 1035 return; | |
| 1001 } | 1036 } |
| 1002 pending_tasks_.clear(); | 1037 |
| 1038 const base::FilePath service_worker_directory = | |
| 1039 path_.Append(ServiceWorkerContextCore::kServiceWorkerDirectory); | |
| 1040 for (const std::string& filename : purgeable_files) { | |
|
michaeln
2015/06/11 00:52:38
Can you defer the start of purging with BrowserThr
nhiroki
2015/06/11 21:04:50
Done. Purging is deferred until an initial diskcac
| |
| 1041 PostTaskAndReplyWithResult( | |
| 1042 disk_cache_thread_.get(), FROM_HERE, | |
| 1043 base::Bind(&base::DeleteFile, | |
| 1044 service_worker_directory.AppendASCII(filename), true), | |
|
michaeln
2015/06/11 00:52:38
Having a general'ish purpose file deletion mechani
nhiroki
2015/06/11 21:04:50
(This code is gone)
| |
| 1045 base::Bind(&ServiceWorkerStorage::DidPurgeFile, | |
| 1046 weak_factory_.GetWeakPtr())); | |
| 1047 } | |
| 1003 } | 1048 } |
| 1004 | 1049 |
| 1005 void ServiceWorkerStorage::DidFindRegistrationForDocument( | 1050 void ServiceWorkerStorage::DidFindRegistrationForDocument( |
| 1006 const GURL& document_url, | 1051 const GURL& document_url, |
| 1007 const FindRegistrationCallback& callback, | 1052 const FindRegistrationCallback& callback, |
| 1008 int64 callback_id, | 1053 int64 callback_id, |
| 1009 const ServiceWorkerDatabase::RegistrationData& data, | 1054 const ServiceWorkerDatabase::RegistrationData& data, |
| 1010 const ResourceList& resources, | 1055 const ResourceList& resources, |
| 1011 ServiceWorkerDatabase::Status status) { | 1056 ServiceWorkerDatabase::Status status) { |
| 1012 if (status == ServiceWorkerDatabase::STATUS_OK) { | 1057 if (status == ServiceWorkerDatabase::STATUS_OK) { |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1098 void ServiceWorkerStorage::ReturnFoundRegistration( | 1143 void ServiceWorkerStorage::ReturnFoundRegistration( |
| 1099 const FindRegistrationCallback& callback, | 1144 const FindRegistrationCallback& callback, |
| 1100 const ServiceWorkerDatabase::RegistrationData& data, | 1145 const ServiceWorkerDatabase::RegistrationData& data, |
| 1101 const ResourceList& resources) { | 1146 const ResourceList& resources) { |
| 1102 DCHECK(!resources.empty()); | 1147 DCHECK(!resources.empty()); |
| 1103 scoped_refptr<ServiceWorkerRegistration> registration = | 1148 scoped_refptr<ServiceWorkerRegistration> registration = |
| 1104 GetOrCreateRegistration(data, resources); | 1149 GetOrCreateRegistration(data, resources); |
| 1105 CompleteFindNow(registration, SERVICE_WORKER_OK, callback); | 1150 CompleteFindNow(registration, SERVICE_WORKER_OK, callback); |
| 1106 } | 1151 } |
| 1107 | 1152 |
| 1153 void ServiceWorkerStorage::DidWriteDatabase( | |
| 1154 ServiceWorkerDatabase::Status status) { | |
| 1155 if (status != ServiceWorkerDatabase::STATUS_OK) | |
| 1156 ScheduleDeleteAndStartOver(); | |
| 1157 } | |
| 1158 | |
| 1108 void ServiceWorkerStorage::DidGetRegistrations( | 1159 void ServiceWorkerStorage::DidGetRegistrations( |
| 1109 const GetRegistrationsInfosCallback& callback, | 1160 const GetRegistrationsInfosCallback& callback, |
| 1110 RegistrationList* registrations, | 1161 RegistrationList* registrations, |
| 1111 const GURL& origin_filter, | 1162 const GURL& origin_filter, |
| 1112 ServiceWorkerDatabase::Status status) { | 1163 ServiceWorkerDatabase::Status status) { |
| 1113 DCHECK(registrations); | 1164 DCHECK(registrations); |
| 1114 if (status != ServiceWorkerDatabase::STATUS_OK && | 1165 if (status != ServiceWorkerDatabase::STATUS_OK && |
| 1115 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { | 1166 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { |
| 1116 ScheduleDeleteAndStartOver(); | 1167 ScheduleDeleteAndStartOver(); |
| 1117 callback.Run(std::vector<ServiceWorkerRegistrationInfo>()); | 1168 callback.Run(std::vector<ServiceWorkerRegistrationInfo>()); |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1361 ServiceWorkerStorage::FindInstallingRegistrationForId( | 1412 ServiceWorkerStorage::FindInstallingRegistrationForId( |
| 1362 int64 registration_id) { | 1413 int64 registration_id) { |
| 1363 RegistrationRefsById::const_iterator found = | 1414 RegistrationRefsById::const_iterator found = |
| 1364 installing_registrations_.find(registration_id); | 1415 installing_registrations_.find(registration_id); |
| 1365 if (found == installing_registrations_.end()) | 1416 if (found == installing_registrations_.end()) |
| 1366 return NULL; | 1417 return NULL; |
| 1367 return found->second.get(); | 1418 return found->second.get(); |
| 1368 } | 1419 } |
| 1369 | 1420 |
| 1370 ServiceWorkerDiskCache* ServiceWorkerStorage::disk_cache() { | 1421 ServiceWorkerDiskCache* ServiceWorkerStorage::disk_cache() { |
| 1422 DCHECK_EQ(INITIALIZED, state_); | |
| 1371 if (disk_cache_) | 1423 if (disk_cache_) |
| 1372 return disk_cache_.get(); | 1424 return disk_cache_.get(); |
| 1373 | 1425 |
| 1374 disk_cache_ = ServiceWorkerDiskCache::CreateWithBlockFileBackend(); | 1426 disk_cache_ = ServiceWorkerDiskCache::CreateWithSimpleBackend(); |
|
michaeln
2015/06/11 00:52:38
Ooops, does this cause the blockfile backend to be
nhiroki
2015/06/11 21:04:50
Fixed in the separate CL:
https://codereview.chrom
| |
| 1375 | 1427 |
| 1376 base::FilePath path = GetDiskCachePath(); | 1428 if (GetDiskCachePath().empty()) { |
| 1377 if (path.empty()) { | |
| 1378 int rv = disk_cache_->InitWithMemBackend(kMaxMemDiskCacheSize, | 1429 int rv = disk_cache_->InitWithMemBackend(kMaxMemDiskCacheSize, |
| 1379 net::CompletionCallback()); | 1430 net::CompletionCallback()); |
| 1380 DCHECK_EQ(net::OK, rv); | 1431 DCHECK_EQ(net::OK, rv); |
| 1381 return disk_cache_.get(); | 1432 return disk_cache_.get(); |
| 1382 } | 1433 } |
| 1383 | 1434 |
| 1435 if (current_disk_cache_version_ < kCurrentDiskCacheVersion) { | |
| 1436 // Defer the start of initialization until the migration is complete. | |
| 1437 disk_cache_->set_is_waiting_to_initialize(true); | |
| 1438 MigrateDiskCache(); | |
| 1439 return disk_cache_.get(); | |
| 1440 } | |
| 1441 DCHECK_EQ(kCurrentDiskCacheVersion, current_disk_cache_version_); | |
| 1442 | |
| 1443 // DiskCache has already been migrated. | |
| 1444 ServiceWorkerMetrics::RecordDiskCacheMigrationResult( | |
| 1445 ServiceWorkerMetrics::MIGRATION_NOT_NECESSARY); | |
| 1446 InitializeDiskCache(); | |
| 1447 | |
| 1448 return disk_cache_.get(); | |
| 1449 } | |
| 1450 | |
| 1451 void ServiceWorkerStorage::MigrateDiskCache() { | |
| 1452 DCHECK(!disk_cache_migrator_); | |
| 1453 disk_cache_migrator_.reset(new ServiceWorkerDiskCacheMigrator( | |
| 1454 GetOldDiskCachePath(), GetDiskCachePath(), kMaxDiskCacheSize, | |
| 1455 disk_cache_thread_)); | |
| 1456 disk_cache_migrator_->Start(base::Bind( | |
| 1457 &ServiceWorkerStorage::DidMigrateDiskCache, weak_factory_.GetWeakPtr())); | |
| 1458 } | |
| 1459 | |
| 1460 void ServiceWorkerStorage::DidMigrateDiskCache(ServiceWorkerStatusCode status) { | |
| 1461 disk_cache_migrator_.reset(); | |
| 1462 | |
| 1463 // Add the old diskcache directory to the purgeable file list. | |
| 1464 PostTaskAndReplyWithResult( | |
| 1465 database_task_manager_->GetTaskRunner(), FROM_HERE, | |
| 1466 base::Bind(&ServiceWorkerDatabase::WritePurgeableFile, | |
| 1467 base::Unretained(database_.get()), kOldDiskCacheName), | |
| 1468 base::Bind(&ServiceWorkerStorage::DidWriteDatabase, | |
| 1469 weak_factory_.GetWeakPtr())); | |
| 1470 | |
| 1471 // Delete the old DiskCache directory. | |
| 1472 PostTaskAndReplyWithResult( | |
| 1473 disk_cache_thread_.get(), FROM_HERE, | |
| 1474 base::Bind(&base::DeleteFile, GetOldDiskCachePath(), true), | |
| 1475 base::Bind(&ServiceWorkerStorage::DidPurgeFile, | |
| 1476 weak_factory_.GetWeakPtr())); | |
| 1477 | |
| 1478 if (status != SERVICE_WORKER_OK) { | |
| 1479 LOG(ERROR) << "Failed to migrate the diskcache: " | |
| 1480 << ServiceWorkerStatusToString(status); | |
| 1481 ServiceWorkerMetrics::RecordDiskCacheMigrationResult( | |
| 1482 ServiceWorkerMetrics::MIGRATION_ERROR_FAILED); | |
| 1483 | |
| 1484 // Give up the migration and recreate the whole storage. | |
| 1485 ScheduleDeleteAndStartOver(); | |
| 1486 return; | |
| 1487 } | |
| 1488 | |
| 1489 ServiceWorkerMetrics::RecordDiskCacheMigrationResult( | |
| 1490 ServiceWorkerMetrics::MIGRATION_OK); | |
| 1491 | |
| 1492 // Update the disk cache version. | |
|
michaeln
2015/06/11 00:52:38
I'm wondering about the order of the operations in
nhiroki
2015/06/11 21:04:50
Revised these operations. The new version...
(1)
| |
| 1493 current_disk_cache_version_ = kCurrentDiskCacheVersion; | |
| 1494 PostTaskAndReplyWithResult( | |
| 1495 database_task_manager_->GetTaskRunner(), FROM_HERE, | |
| 1496 base::Bind(&ServiceWorkerDatabase::WriteDiskCacheVersion, | |
| 1497 base::Unretained(database_.get()), kCurrentDiskCacheVersion), | |
| 1498 base::Bind(&ServiceWorkerStorage::DidWriteDatabase, | |
| 1499 weak_factory_.GetWeakPtr())); | |
| 1500 | |
| 1501 InitializeDiskCache(); | |
| 1502 } | |
| 1503 | |
| 1504 void ServiceWorkerStorage::DidPurgeFile(bool deleted) { | |
| 1505 if (!deleted) { | |
| 1506 LOG(ERROR) << "Failed to delete the file."; | |
| 1507 // Retry to delete when the browser restarts. | |
| 1508 return; | |
| 1509 } | |
| 1510 | |
| 1511 // Remove the file from the purgeable file list. | |
| 1512 PostTaskAndReplyWithResult( | |
| 1513 database_task_manager_->GetTaskRunner(), FROM_HERE, | |
| 1514 base::Bind(&ServiceWorkerDatabase::ClearPurgeableFile, | |
| 1515 base::Unretained(database_.get()), kOldDiskCacheName), | |
| 1516 base::Bind(&ServiceWorkerStorage::DidWriteDatabase, | |
| 1517 weak_factory_.GetWeakPtr())); | |
| 1518 } | |
| 1519 | |
| 1520 void ServiceWorkerStorage::InitializeDiskCache() { | |
| 1521 disk_cache_->set_is_waiting_to_initialize(false); | |
| 1384 int rv = disk_cache_->InitWithDiskBackend( | 1522 int rv = disk_cache_->InitWithDiskBackend( |
| 1385 path, | 1523 GetDiskCachePath(), kMaxDiskCacheSize, false, disk_cache_thread_, |
| 1386 kMaxDiskCacheSize, | |
| 1387 false, | |
| 1388 disk_cache_thread_, | |
| 1389 base::Bind(&ServiceWorkerStorage::OnDiskCacheInitialized, | 1524 base::Bind(&ServiceWorkerStorage::OnDiskCacheInitialized, |
| 1390 weak_factory_.GetWeakPtr())); | 1525 weak_factory_.GetWeakPtr())); |
| 1391 if (rv != net::ERR_IO_PENDING) | 1526 if (rv != net::ERR_IO_PENDING) |
| 1392 OnDiskCacheInitialized(rv); | 1527 OnDiskCacheInitialized(rv); |
| 1393 | |
| 1394 return disk_cache_.get(); | |
| 1395 } | 1528 } |
| 1396 | 1529 |
| 1397 void ServiceWorkerStorage::OnDiskCacheInitialized(int rv) { | 1530 void ServiceWorkerStorage::OnDiskCacheInitialized(int rv) { |
| 1398 if (rv != net::OK) { | 1531 if (rv != net::OK) { |
| 1399 LOG(ERROR) << "Failed to open the serviceworker diskcache: " | 1532 LOG(ERROR) << "Failed to open the serviceworker diskcache: " |
| 1400 << net::ErrorToString(rv); | 1533 << net::ErrorToString(rv); |
| 1401 ScheduleDeleteAndStartOver(); | 1534 ScheduleDeleteAndStartOver(); |
| 1402 } | 1535 } |
| 1403 ServiceWorkerMetrics::CountInitDiskCacheResult(rv == net::OK); | 1536 ServiceWorkerMetrics::CountInitDiskCacheResult(rv == net::OK); |
| 1404 } | 1537 } |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1531 base::Bind(callback, std::vector<int64>(ids.begin(), ids.end()), status)); | 1664 base::Bind(callback, std::vector<int64>(ids.begin(), ids.end()), status)); |
| 1532 } | 1665 } |
| 1533 | 1666 |
| 1534 void ServiceWorkerStorage::ReadInitialDataFromDB( | 1667 void ServiceWorkerStorage::ReadInitialDataFromDB( |
| 1535 ServiceWorkerDatabase* database, | 1668 ServiceWorkerDatabase* database, |
| 1536 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 1669 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 1537 const InitializeCallback& callback) { | 1670 const InitializeCallback& callback) { |
| 1538 DCHECK(database); | 1671 DCHECK(database); |
| 1539 scoped_ptr<ServiceWorkerStorage::InitialData> data( | 1672 scoped_ptr<ServiceWorkerStorage::InitialData> data( |
| 1540 new ServiceWorkerStorage::InitialData()); | 1673 new ServiceWorkerStorage::InitialData()); |
| 1674 int64 disk_cache_version = -1; | |
| 1541 | 1675 |
| 1542 ServiceWorkerDatabase::Status status = | 1676 ServiceWorkerDatabase::Status status = |
| 1543 database->GetNextAvailableIds(&data->next_registration_id, | 1677 database->GetNextAvailableIds(&data->next_registration_id, |
| 1544 &data->next_version_id, | 1678 &data->next_version_id, |
| 1545 &data->next_resource_id); | 1679 &data->next_resource_id); |
| 1546 if (status != ServiceWorkerDatabase::STATUS_OK) { | 1680 if (status != ServiceWorkerDatabase::STATUS_OK) { |
| 1547 original_task_runner->PostTask( | 1681 original_task_runner->PostTask( |
| 1548 FROM_HERE, base::Bind(callback, base::Owned(data.release()), status)); | 1682 FROM_HERE, |
| 1683 base::Bind(callback, base::Owned(data.release()), disk_cache_version, | |
| 1684 std::vector<std::string>(), status)); | |
| 1685 return; | |
| 1686 } | |
| 1687 | |
| 1688 status = database->ReadDiskCacheVersion(&disk_cache_version); | |
| 1689 if (status != ServiceWorkerDatabase::STATUS_OK) { | |
| 1690 original_task_runner->PostTask( | |
| 1691 FROM_HERE, | |
| 1692 base::Bind(callback, base::Owned(data.release()), disk_cache_version, | |
| 1693 std::vector<std::string>(), status)); | |
| 1549 return; | 1694 return; |
| 1550 } | 1695 } |
| 1551 | 1696 |
| 1552 status = database->GetOriginsWithRegistrations(&data->origins); | 1697 status = database->GetOriginsWithRegistrations(&data->origins); |
| 1698 if (status != ServiceWorkerDatabase::STATUS_OK) { | |
| 1699 original_task_runner->PostTask( | |
| 1700 FROM_HERE, | |
| 1701 base::Bind(callback, base::Owned(data.release()), disk_cache_version, | |
| 1702 std::vector<std::string>(), status)); | |
| 1703 return; | |
| 1704 } | |
| 1705 | |
| 1706 std::vector<std::string> purgeable_files; | |
| 1707 status = database->GetPurgeableFiles(&purgeable_files); | |
| 1553 original_task_runner->PostTask( | 1708 original_task_runner->PostTask( |
| 1554 FROM_HERE, base::Bind(callback, base::Owned(data.release()), status)); | 1709 FROM_HERE, base::Bind(callback, base::Owned(data.release()), |
| 1710 disk_cache_version, purgeable_files, status)); | |
| 1555 } | 1711 } |
| 1556 | 1712 |
| 1557 void ServiceWorkerStorage::DeleteRegistrationFromDB( | 1713 void ServiceWorkerStorage::DeleteRegistrationFromDB( |
| 1558 ServiceWorkerDatabase* database, | 1714 ServiceWorkerDatabase* database, |
| 1559 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 1715 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 1560 int64 registration_id, | 1716 int64 registration_id, |
| 1561 const GURL& origin, | 1717 const GURL& origin, |
| 1562 const DeleteRegistrationCallback& callback) { | 1718 const DeleteRegistrationCallback& callback) { |
| 1563 DCHECK(database); | 1719 DCHECK(database); |
| 1564 | 1720 |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1811 callback.Run(SERVICE_WORKER_ERROR_FAILED); | 1967 callback.Run(SERVICE_WORKER_ERROR_FAILED); |
| 1812 return; | 1968 return; |
| 1813 } | 1969 } |
| 1814 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; | 1970 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; |
| 1815 ServiceWorkerMetrics::RecordDeleteAndStartOverResult( | 1971 ServiceWorkerMetrics::RecordDeleteAndStartOverResult( |
| 1816 ServiceWorkerMetrics::DELETE_OK); | 1972 ServiceWorkerMetrics::DELETE_OK); |
| 1817 callback.Run(SERVICE_WORKER_OK); | 1973 callback.Run(SERVICE_WORKER_OK); |
| 1818 } | 1974 } |
| 1819 | 1975 |
| 1820 } // namespace content | 1976 } // namespace content |
| OLD | NEW |