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

Side by Side Diff: content/browser/service_worker/service_worker_storage.cc

Issue 1221643014: Service Worker: Migrate to version_uuid and surface ServiceWorker.id. (Chromium 2/3) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 5 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 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/guid.h"
9 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
10 #include "base/sequenced_task_runner.h" 11 #include "base/sequenced_task_runner.h"
11 #include "base/single_thread_task_runner.h" 12 #include "base/single_thread_task_runner.h"
12 #include "base/task_runner_util.h" 13 #include "base/task_runner_util.h"
13 #include "base/thread_task_runner_handle.h" 14 #include "base/thread_task_runner_handle.h"
14 #include "base/trace_event/trace_event.h" 15 #include "base/trace_event/trace_event.h"
15 #include "content/browser/service_worker/service_worker_context_core.h" 16 #include "content/browser/service_worker/service_worker_context_core.h"
16 #include "content/browser/service_worker/service_worker_disk_cache.h" 17 #include "content/browser/service_worker/service_worker_disk_cache.h"
17 #include "content/browser/service_worker/service_worker_disk_cache_migrator.h" 18 #include "content/browser/service_worker/service_worker_disk_cache_migrator.h"
18 #include "content/browser/service_worker/service_worker_info.h" 19 #include "content/browser/service_worker/service_worker_info.h"
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 return; 201 return;
201 } 202 }
202 203
203 ReadSomeData(); 204 ReadSomeData();
204 } 205 }
205 206
206 } // namespace 207 } // namespace
207 208
208 ServiceWorkerStorage::InitialData::InitialData() 209 ServiceWorkerStorage::InitialData::InitialData()
209 : next_registration_id(kInvalidServiceWorkerRegistrationId), 210 : next_registration_id(kInvalidServiceWorkerRegistrationId),
210 next_version_id(kInvalidServiceWorkerVersionId),
211 next_resource_id(kInvalidServiceWorkerResourceId), 211 next_resource_id(kInvalidServiceWorkerResourceId),
212 disk_cache_migration_needed(false), 212 disk_cache_migration_needed(false),
213 old_disk_cache_deletion_needed(false) { 213 old_disk_cache_deletion_needed(false) {
214 } 214 }
215 215
216 ServiceWorkerStorage::InitialData::~InitialData() { 216 ServiceWorkerStorage::InitialData::~InitialData() {
217 } 217 }
218 218
219 ServiceWorkerStorage:: 219 ServiceWorkerStorage::
220 DidDeleteRegistrationParams::DidDeleteRegistrationParams() 220 DidDeleteRegistrationParams::DidDeleteRegistrationParams()
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 if (IsDisabled() || !context_) { 524 if (IsDisabled() || !context_) {
525 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); 525 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED));
526 return; 526 return;
527 } 527 }
528 528
529 ServiceWorkerDatabase::RegistrationData data; 529 ServiceWorkerDatabase::RegistrationData data;
530 data.registration_id = registration->id(); 530 data.registration_id = registration->id();
531 data.scope = registration->pattern(); 531 data.scope = registration->pattern();
532 data.script = version->script_url(); 532 data.script = version->script_url();
533 data.has_fetch_handler = true; 533 data.has_fetch_handler = true;
534 data.version_id = version->version_id(); 534 data.version_uuid = version->version_uuid();
535 data.last_update_check = registration->last_update_check(); 535 data.last_update_check = registration->last_update_check();
536 data.is_active = (version == registration->active_version()); 536 data.is_active = (version == registration->active_version());
537 537
538 ResourceList resources; 538 ResourceList resources;
539 version->script_cache_map()->GetResources(&resources); 539 version->script_cache_map()->GetResources(&resources);
540 540
541 if (resources.empty()) { 541 if (resources.empty()) {
542 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); 542 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED));
543 return; 543 return;
544 } 544 }
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
831 weak_factory_.GetWeakPtr(), callback)); 831 weak_factory_.GetWeakPtr(), callback));
832 } 832 }
833 833
834 int64 ServiceWorkerStorage::NewRegistrationId() { 834 int64 ServiceWorkerStorage::NewRegistrationId() {
835 if (state_ == DISABLED) 835 if (state_ == DISABLED)
836 return kInvalidServiceWorkerRegistrationId; 836 return kInvalidServiceWorkerRegistrationId;
837 DCHECK_EQ(INITIALIZED, state_); 837 DCHECK_EQ(INITIALIZED, state_);
838 return next_registration_id_++; 838 return next_registration_id_++;
839 } 839 }
840 840
841 int64 ServiceWorkerStorage::NewVersionId() { 841 std::string ServiceWorkerStorage::NewVersionId() {
842 if (state_ == DISABLED) 842 if (state_ == DISABLED)
843 return kInvalidServiceWorkerVersionId; 843 return std::string();
844 DCHECK_EQ(INITIALIZED, state_); 844 DCHECK_EQ(INITIALIZED, state_);
845 return next_version_id_++; 845 return base::GenerateGUID();
846 } 846 }
847 847
848 int64 ServiceWorkerStorage::NewResourceId() { 848 int64 ServiceWorkerStorage::NewResourceId() {
849 if (state_ == DISABLED) 849 if (state_ == DISABLED)
850 return kInvalidServiceWorkerResourceId; 850 return kInvalidServiceWorkerResourceId;
851 DCHECK_EQ(INITIALIZED, state_); 851 DCHECK_EQ(INITIALIZED, state_);
852 return next_resource_id_++; 852 return next_resource_id_++;
853 } 853 }
854 854
855 void ServiceWorkerStorage::NotifyInstallingRegistration( 855 void ServiceWorkerStorage::NotifyInstallingRegistration(
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
910 } 910 }
911 911
912 ServiceWorkerStorage::ServiceWorkerStorage( 912 ServiceWorkerStorage::ServiceWorkerStorage(
913 const base::FilePath& path, 913 const base::FilePath& path,
914 base::WeakPtr<ServiceWorkerContextCore> context, 914 base::WeakPtr<ServiceWorkerContextCore> context,
915 scoped_ptr<ServiceWorkerDatabaseTaskManager> database_task_manager, 915 scoped_ptr<ServiceWorkerDatabaseTaskManager> database_task_manager,
916 const scoped_refptr<base::SingleThreadTaskRunner>& disk_cache_thread, 916 const scoped_refptr<base::SingleThreadTaskRunner>& disk_cache_thread,
917 storage::QuotaManagerProxy* quota_manager_proxy, 917 storage::QuotaManagerProxy* quota_manager_proxy,
918 storage::SpecialStoragePolicy* special_storage_policy) 918 storage::SpecialStoragePolicy* special_storage_policy)
919 : next_registration_id_(kInvalidServiceWorkerRegistrationId), 919 : next_registration_id_(kInvalidServiceWorkerRegistrationId),
920 next_version_id_(kInvalidServiceWorkerVersionId),
921 next_resource_id_(kInvalidServiceWorkerResourceId), 920 next_resource_id_(kInvalidServiceWorkerResourceId),
922 state_(UNINITIALIZED), 921 state_(UNINITIALIZED),
923 path_(path), 922 path_(path),
924 context_(context), 923 context_(context),
925 database_task_manager_(database_task_manager.Pass()), 924 database_task_manager_(database_task_manager.Pass()),
926 disk_cache_thread_(disk_cache_thread), 925 disk_cache_thread_(disk_cache_thread),
927 quota_manager_proxy_(quota_manager_proxy), 926 quota_manager_proxy_(quota_manager_proxy),
928 special_storage_policy_(special_storage_policy), 927 special_storage_policy_(special_storage_policy),
929 disk_cache_migration_needed_(false), 928 disk_cache_migration_needed_(false),
930 old_disk_cache_deletion_needed_(false), 929 old_disk_cache_deletion_needed_(false),
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
984 } 983 }
985 984
986 void ServiceWorkerStorage::DidReadInitialData( 985 void ServiceWorkerStorage::DidReadInitialData(
987 InitialData* data, 986 InitialData* data,
988 ServiceWorkerDatabase::Status status) { 987 ServiceWorkerDatabase::Status status) {
989 DCHECK(data); 988 DCHECK(data);
990 DCHECK_EQ(INITIALIZING, state_); 989 DCHECK_EQ(INITIALIZING, state_);
991 990
992 if (status == ServiceWorkerDatabase::STATUS_OK) { 991 if (status == ServiceWorkerDatabase::STATUS_OK) {
993 next_registration_id_ = data->next_registration_id; 992 next_registration_id_ = data->next_registration_id;
994 next_version_id_ = data->next_version_id;
995 next_resource_id_ = data->next_resource_id; 993 next_resource_id_ = data->next_resource_id;
996 registered_origins_.swap(data->origins); 994 registered_origins_.swap(data->origins);
997 disk_cache_migration_needed_ = data->disk_cache_migration_needed; 995 disk_cache_migration_needed_ = data->disk_cache_migration_needed;
998 old_disk_cache_deletion_needed_ = data->old_disk_cache_deletion_needed; 996 old_disk_cache_deletion_needed_ = data->old_disk_cache_deletion_needed;
999 state_ = INITIALIZED; 997 state_ = INITIALIZED;
1000 } else { 998 } else {
1001 DVLOG(2) << "Failed to initialize: " 999 DVLOG(2) << "Failed to initialize: "
1002 << ServiceWorkerDatabase::StatusToString(status); 1000 << ServiceWorkerDatabase::StatusToString(status);
1003 ScheduleDeleteAndStartOver(); 1001 ScheduleDeleteAndStartOver();
1004 } 1002 }
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
1176 infos.push_back(registration->GetInfo()); 1174 infos.push_back(registration->GetInfo());
1177 continue; 1175 continue;
1178 } 1176 }
1179 1177
1180 ServiceWorkerRegistrationInfo info; 1178 ServiceWorkerRegistrationInfo info;
1181 info.pattern = registration_data.scope; 1179 info.pattern = registration_data.scope;
1182 info.registration_id = registration_data.registration_id; 1180 info.registration_id = registration_data.registration_id;
1183 info.stored_version_size_bytes = 1181 info.stored_version_size_bytes =
1184 registration_data.resources_total_size_bytes; 1182 registration_data.resources_total_size_bytes;
1185 if (ServiceWorkerVersion* version = 1183 if (ServiceWorkerVersion* version =
1186 context_->GetLiveVersion(registration_data.version_id)) { 1184 context_->GetLiveVersion(registration_data.version_uuid)) {
1187 if (registration_data.is_active) 1185 if (registration_data.is_active)
1188 info.active_version = version->GetInfo(); 1186 info.active_version = version->GetInfo();
1189 else 1187 else
1190 info.waiting_version = version->GetInfo(); 1188 info.waiting_version = version->GetInfo();
1191 infos.push_back(info); 1189 infos.push_back(info);
1192 continue; 1190 continue;
1193 } 1191 }
1194 1192
1195 if (registration_data.is_active) { 1193 if (registration_data.is_active) {
1196 info.active_version.status = ServiceWorkerVersion::ACTIVATED; 1194 info.active_version.status = ServiceWorkerVersion::ACTIVATED;
1197 info.active_version.script_url = registration_data.script; 1195 info.active_version.script_url = registration_data.script;
1198 info.active_version.version_id = registration_data.version_id; 1196 info.active_version.version_uuid = registration_data.version_uuid;
1199 info.active_version.registration_id = registration_data.registration_id; 1197 info.active_version.registration_id = registration_data.registration_id;
1200 } else { 1198 } else {
1201 info.waiting_version.status = ServiceWorkerVersion::INSTALLED; 1199 info.waiting_version.status = ServiceWorkerVersion::INSTALLED;
1202 info.waiting_version.script_url = registration_data.script; 1200 info.waiting_version.script_url = registration_data.script;
1203 info.waiting_version.version_id = registration_data.version_id; 1201 info.waiting_version.version_uuid = registration_data.version_uuid;
1204 info.waiting_version.registration_id = registration_data.registration_id; 1202 info.waiting_version.registration_id = registration_data.registration_id;
1205 } 1203 }
1206 infos.push_back(info); 1204 infos.push_back(info);
1207 } 1205 }
1208 1206
1209 // Add unstored registrations that are being installed. 1207 // Add unstored registrations that are being installed.
1210 for (const auto& registration : installing_registrations_) { 1208 for (const auto& registration : installing_registrations_) {
1211 if ((!origin_filter.is_valid() || 1209 if ((!origin_filter.is_valid() ||
1212 registration.second->pattern().GetOrigin() == origin_filter) && 1210 registration.second->pattern().GetOrigin() == origin_filter) &&
1213 pushed_registrations.insert(registration.first).second) { 1211 pushed_registrations.insert(registration.first).second) {
(...skipping 27 matching lines...) Expand all
1241 quota_manager_proxy_->NotifyStorageModified( 1239 quota_manager_proxy_->NotifyStorageModified(
1242 storage::QuotaClient::kServiceWorker, 1240 storage::QuotaClient::kServiceWorker,
1243 origin, 1241 origin,
1244 storage::StorageType::kStorageTypeTemporary, 1242 storage::StorageType::kStorageTypeTemporary,
1245 new_version.resources_total_size_bytes - 1243 new_version.resources_total_size_bytes -
1246 deleted_version.resources_total_size_bytes); 1244 deleted_version.resources_total_size_bytes);
1247 } 1245 }
1248 1246
1249 callback.Run(SERVICE_WORKER_OK); 1247 callback.Run(SERVICE_WORKER_OK);
1250 1248
1251 if (!context_ || !context_->GetLiveVersion(deleted_version.version_id)) 1249 if (!context_ || !context_->GetLiveVersion(deleted_version.version_uuid))
1252 StartPurgingResources(newly_purgeable_resources); 1250 StartPurgingResources(newly_purgeable_resources);
1253 } 1251 }
1254 1252
1255 void ServiceWorkerStorage::DidUpdateToActiveState( 1253 void ServiceWorkerStorage::DidUpdateToActiveState(
1256 const StatusCallback& callback, 1254 const StatusCallback& callback,
1257 ServiceWorkerDatabase::Status status) { 1255 ServiceWorkerDatabase::Status status) {
1258 if (status != ServiceWorkerDatabase::STATUS_OK && 1256 if (status != ServiceWorkerDatabase::STATUS_OK &&
1259 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { 1257 status != ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) {
1260 ScheduleDeleteAndStartOver(); 1258 ScheduleDeleteAndStartOver();
1261 } 1259 }
(...skipping 17 matching lines...) Expand all
1279 quota_manager_proxy_->NotifyStorageModified( 1277 quota_manager_proxy_->NotifyStorageModified(
1280 storage::QuotaClient::kServiceWorker, 1278 storage::QuotaClient::kServiceWorker,
1281 params.origin, 1279 params.origin,
1282 storage::StorageType::kStorageTypeTemporary, 1280 storage::StorageType::kStorageTypeTemporary,
1283 -deleted_version.resources_total_size_bytes); 1281 -deleted_version.resources_total_size_bytes);
1284 } 1282 }
1285 if (origin_is_deletable) 1283 if (origin_is_deletable)
1286 registered_origins_.erase(params.origin); 1284 registered_origins_.erase(params.origin);
1287 params.callback.Run(SERVICE_WORKER_OK); 1285 params.callback.Run(SERVICE_WORKER_OK);
1288 1286
1289 if (!context_ || !context_->GetLiveVersion(deleted_version.version_id)) 1287 if (!context_ || !context_->GetLiveVersion(deleted_version.version_uuid))
1290 StartPurgingResources(newly_purgeable_resources); 1288 StartPurgingResources(newly_purgeable_resources);
1291 } 1289 }
1292 1290
1293 void ServiceWorkerStorage::DidStoreUserData( 1291 void ServiceWorkerStorage::DidStoreUserData(
1294 const StatusCallback& callback, 1292 const StatusCallback& callback,
1295 ServiceWorkerDatabase::Status status) { 1293 ServiceWorkerDatabase::Status status) {
1296 // |status| can be NOT_FOUND when the associated registration did not exist in 1294 // |status| can be NOT_FOUND when the associated registration did not exist in
1297 // the database. In the case, we don't have to schedule the corruption 1295 // the database. In the case, we don't have to schedule the corruption
1298 // recovery. 1296 // recovery.
1299 if (status != ServiceWorkerDatabase::STATUS_OK && 1297 if (status != ServiceWorkerDatabase::STATUS_OK &&
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1342 1340
1343 registration = new ServiceWorkerRegistration( 1341 registration = new ServiceWorkerRegistration(
1344 data.scope, data.registration_id, context_); 1342 data.scope, data.registration_id, context_);
1345 registration->set_resources_total_size_bytes(data.resources_total_size_bytes); 1343 registration->set_resources_total_size_bytes(data.resources_total_size_bytes);
1346 registration->set_last_update_check(data.last_update_check); 1344 registration->set_last_update_check(data.last_update_check);
1347 if (pending_deletions_.find(data.registration_id) != 1345 if (pending_deletions_.find(data.registration_id) !=
1348 pending_deletions_.end()) { 1346 pending_deletions_.end()) {
1349 registration->set_is_deleted(true); 1347 registration->set_is_deleted(true);
1350 } 1348 }
1351 scoped_refptr<ServiceWorkerVersion> version = 1349 scoped_refptr<ServiceWorkerVersion> version =
1352 context_->GetLiveVersion(data.version_id); 1350 context_->GetLiveVersion(data.version_uuid);
1353 if (!version.get()) { 1351 if (!version.get()) {
1354 version = new ServiceWorkerVersion( 1352 version = new ServiceWorkerVersion(registration.get(), data.script,
1355 registration.get(), data.script, data.version_id, context_); 1353 data.version_uuid, context_);
1356 version->SetStatus(data.is_active ? 1354 version->SetStatus(data.is_active ?
1357 ServiceWorkerVersion::ACTIVATED : ServiceWorkerVersion::INSTALLED); 1355 ServiceWorkerVersion::ACTIVATED : ServiceWorkerVersion::INSTALLED);
1358 version->script_cache_map()->SetResources(resources); 1356 version->script_cache_map()->SetResources(resources);
1359 } 1357 }
1360 1358
1361 if (version->status() == ServiceWorkerVersion::ACTIVATED) 1359 if (version->status() == ServiceWorkerVersion::ACTIVATED)
1362 registration->SetActiveVersion(version); 1360 registration->SetActiveVersion(version);
1363 else if (version->status() == ServiceWorkerVersion::INSTALLED) 1361 else if (version->status() == ServiceWorkerVersion::INSTALLED)
1364 registration->SetWaitingVersion(version); 1362 registration->SetWaitingVersion(version);
1365 else 1363 else
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after
1644 void ServiceWorkerStorage::ReadInitialDataFromDB( 1642 void ServiceWorkerStorage::ReadInitialDataFromDB(
1645 ServiceWorkerDatabase* database, 1643 ServiceWorkerDatabase* database,
1646 scoped_refptr<base::SequencedTaskRunner> original_task_runner, 1644 scoped_refptr<base::SequencedTaskRunner> original_task_runner,
1647 const InitializeCallback& callback) { 1645 const InitializeCallback& callback) {
1648 DCHECK(database); 1646 DCHECK(database);
1649 scoped_ptr<ServiceWorkerStorage::InitialData> data( 1647 scoped_ptr<ServiceWorkerStorage::InitialData> data(
1650 new ServiceWorkerStorage::InitialData()); 1648 new ServiceWorkerStorage::InitialData());
1651 1649
1652 ServiceWorkerDatabase::Status status = 1650 ServiceWorkerDatabase::Status status =
1653 database->GetNextAvailableIds(&data->next_registration_id, 1651 database->GetNextAvailableIds(&data->next_registration_id,
1654 &data->next_version_id,
1655 &data->next_resource_id); 1652 &data->next_resource_id);
1656 if (status != ServiceWorkerDatabase::STATUS_OK) { 1653 if (status != ServiceWorkerDatabase::STATUS_OK) {
1657 original_task_runner->PostTask( 1654 original_task_runner->PostTask(
1658 FROM_HERE, base::Bind(callback, base::Owned(data.release()), status)); 1655 FROM_HERE, base::Bind(callback, base::Owned(data.release()), status));
1659 return; 1656 return;
1660 } 1657 }
1661 1658
1662 status = 1659 status =
1663 database->IsDiskCacheMigrationNeeded(&data->disk_cache_migration_needed); 1660 database->IsDiskCacheMigrationNeeded(&data->disk_cache_migration_needed);
1664 if (status != ServiceWorkerDatabase::STATUS_OK) { 1661 if (status != ServiceWorkerDatabase::STATUS_OK) {
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after
1943 callback.Run(SERVICE_WORKER_ERROR_FAILED); 1940 callback.Run(SERVICE_WORKER_ERROR_FAILED);
1944 return; 1941 return;
1945 } 1942 }
1946 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; 1943 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully.";
1947 ServiceWorkerMetrics::RecordDeleteAndStartOverResult( 1944 ServiceWorkerMetrics::RecordDeleteAndStartOverResult(
1948 ServiceWorkerMetrics::DELETE_OK); 1945 ServiceWorkerMetrics::DELETE_OK);
1949 callback.Run(SERVICE_WORKER_OK); 1946 callback.Run(SERVICE_WORKER_OK);
1950 } 1947 }
1951 1948
1952 } // namespace content 1949 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698