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 <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 DCHECK(!document_url.has_ref()); | 122 DCHECK(!document_url.has_ref()); |
| 123 if (!LazyInitialize(base::Bind( | 123 if (!LazyInitialize(base::Bind( |
| 124 &ServiceWorkerStorage::FindRegistrationForDocument, | 124 &ServiceWorkerStorage::FindRegistrationForDocument, |
| 125 weak_factory_.GetWeakPtr(), document_url, callback))) { | 125 weak_factory_.GetWeakPtr(), document_url, callback))) { |
| 126 if (state_ != INITIALIZING || !context_) { | 126 if (state_ != INITIALIZING || !context_) { |
| 127 CompleteFindNow(scoped_refptr<ServiceWorkerRegistration>(), | 127 CompleteFindNow(scoped_refptr<ServiceWorkerRegistration>(), |
| 128 SERVICE_WORKER_ERROR_FAILED, callback); | 128 SERVICE_WORKER_ERROR_FAILED, callback); |
| 129 } | 129 } |
| 130 return; | 130 return; |
| 131 } | 131 } |
| 132 DCHECK_EQ(INITIALIZED, state_); | 132 DCHECK(IsReady()) << state_; |
| 133 | 133 |
| 134 // See if there are any stored registrations for the origin. | 134 // See if there are any stored registrations for the origin. |
| 135 if (!ContainsKey(registered_origins_, document_url.GetOrigin())) { | 135 if (!ContainsKey(registered_origins_, document_url.GetOrigin())) { |
| 136 // Look for something currently being installed. | 136 // Look for something currently being installed. |
| 137 scoped_refptr<ServiceWorkerRegistration> installing_registration = | 137 scoped_refptr<ServiceWorkerRegistration> installing_registration = |
| 138 FindInstallingRegistrationForDocument(document_url); | 138 FindInstallingRegistrationForDocument(document_url); |
| 139 CompleteFindNow( | 139 CompleteFindNow( |
| 140 installing_registration, | 140 installing_registration, |
| 141 installing_registration ? | 141 installing_registration ? |
| 142 SERVICE_WORKER_OK : SERVICE_WORKER_ERROR_NOT_FOUND, | 142 SERVICE_WORKER_OK : SERVICE_WORKER_ERROR_NOT_FOUND, |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 160 const FindRegistrationCallback& callback) { | 160 const FindRegistrationCallback& callback) { |
| 161 if (!LazyInitialize(base::Bind( | 161 if (!LazyInitialize(base::Bind( |
| 162 &ServiceWorkerStorage::FindRegistrationForPattern, | 162 &ServiceWorkerStorage::FindRegistrationForPattern, |
| 163 weak_factory_.GetWeakPtr(), scope, callback))) { | 163 weak_factory_.GetWeakPtr(), scope, callback))) { |
| 164 if (state_ != INITIALIZING || !context_) { | 164 if (state_ != INITIALIZING || !context_) { |
| 165 CompleteFindSoon(FROM_HERE, scoped_refptr<ServiceWorkerRegistration>(), | 165 CompleteFindSoon(FROM_HERE, scoped_refptr<ServiceWorkerRegistration>(), |
| 166 SERVICE_WORKER_ERROR_FAILED, callback); | 166 SERVICE_WORKER_ERROR_FAILED, callback); |
| 167 } | 167 } |
| 168 return; | 168 return; |
| 169 } | 169 } |
| 170 DCHECK_EQ(INITIALIZED, state_); | 170 DCHECK(IsReady()) << state_; |
| 171 | 171 |
| 172 // See if there are any stored registrations for the origin. | 172 // See if there are any stored registrations for the origin. |
| 173 if (!ContainsKey(registered_origins_, scope.GetOrigin())) { | 173 if (!ContainsKey(registered_origins_, scope.GetOrigin())) { |
| 174 // Look for something currently being installed. | 174 // Look for something currently being installed. |
| 175 scoped_refptr<ServiceWorkerRegistration> installing_registration = | 175 scoped_refptr<ServiceWorkerRegistration> installing_registration = |
| 176 FindInstallingRegistrationForPattern(scope); | 176 FindInstallingRegistrationForPattern(scope); |
| 177 CompleteFindSoon( | 177 CompleteFindSoon( |
| 178 FROM_HERE, installing_registration, | 178 FROM_HERE, installing_registration, |
| 179 installing_registration ? | 179 installing_registration ? |
| 180 SERVICE_WORKER_OK : SERVICE_WORKER_ERROR_NOT_FOUND, | 180 SERVICE_WORKER_OK : SERVICE_WORKER_ERROR_NOT_FOUND, |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 199 const FindRegistrationCallback& callback) { | 199 const FindRegistrationCallback& callback) { |
| 200 if (!LazyInitialize(base::Bind( | 200 if (!LazyInitialize(base::Bind( |
| 201 &ServiceWorkerStorage::FindRegistrationForId, | 201 &ServiceWorkerStorage::FindRegistrationForId, |
| 202 weak_factory_.GetWeakPtr(), registration_id, origin, callback))) { | 202 weak_factory_.GetWeakPtr(), registration_id, origin, callback))) { |
| 203 if (state_ != INITIALIZING || !context_) { | 203 if (state_ != INITIALIZING || !context_) { |
| 204 CompleteFindNow(scoped_refptr<ServiceWorkerRegistration>(), | 204 CompleteFindNow(scoped_refptr<ServiceWorkerRegistration>(), |
| 205 SERVICE_WORKER_ERROR_FAILED, callback); | 205 SERVICE_WORKER_ERROR_FAILED, callback); |
| 206 } | 206 } |
| 207 return; | 207 return; |
| 208 } | 208 } |
| 209 DCHECK_EQ(INITIALIZED, state_); | 209 DCHECK(IsReady()) << state_; |
| 210 | 210 |
| 211 // See if there are any stored registrations for the origin. | 211 // See if there are any stored registrations for the origin. |
| 212 if (!ContainsKey(registered_origins_, origin)) { | 212 if (!ContainsKey(registered_origins_, origin)) { |
| 213 // Look for something currently being installed. | 213 // Look for something currently being installed. |
| 214 scoped_refptr<ServiceWorkerRegistration> installing_registration = | 214 scoped_refptr<ServiceWorkerRegistration> installing_registration = |
| 215 FindInstallingRegistrationForId(registration_id); | 215 FindInstallingRegistrationForId(registration_id); |
| 216 CompleteFindNow( | 216 CompleteFindNow( |
| 217 installing_registration, | 217 installing_registration, |
| 218 installing_registration ? | 218 installing_registration ? |
| 219 SERVICE_WORKER_OK : SERVICE_WORKER_ERROR_NOT_FOUND, | 219 SERVICE_WORKER_OK : SERVICE_WORKER_ERROR_NOT_FOUND, |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 242 const GetAllRegistrationInfosCallback& callback) { | 242 const GetAllRegistrationInfosCallback& callback) { |
| 243 if (!LazyInitialize(base::Bind( | 243 if (!LazyInitialize(base::Bind( |
| 244 &ServiceWorkerStorage::GetAllRegistrations, | 244 &ServiceWorkerStorage::GetAllRegistrations, |
| 245 weak_factory_.GetWeakPtr(), callback))) { | 245 weak_factory_.GetWeakPtr(), callback))) { |
| 246 if (state_ != INITIALIZING || !context_) { | 246 if (state_ != INITIALIZING || !context_) { |
| 247 RunSoon(FROM_HERE, base::Bind( | 247 RunSoon(FROM_HERE, base::Bind( |
| 248 callback, std::vector<ServiceWorkerRegistrationInfo>())); | 248 callback, std::vector<ServiceWorkerRegistrationInfo>())); |
| 249 } | 249 } |
| 250 return; | 250 return; |
| 251 } | 251 } |
| 252 DCHECK_EQ(INITIALIZED, state_); | 252 DCHECK(IsReady()) << state_; |
| 253 | 253 |
| 254 RegistrationList* registrations = new RegistrationList; | 254 RegistrationList* registrations = new RegistrationList; |
| 255 PostTaskAndReplyWithResult( | 255 PostTaskAndReplyWithResult( |
| 256 database_task_runner_, | 256 database_task_runner_, |
| 257 FROM_HERE, | 257 FROM_HERE, |
| 258 base::Bind(&ServiceWorkerDatabase::GetAllRegistrations, | 258 base::Bind(&ServiceWorkerDatabase::GetAllRegistrations, |
| 259 base::Unretained(database_.get()), | 259 base::Unretained(database_.get()), |
| 260 base::Unretained(registrations)), | 260 base::Unretained(registrations)), |
| 261 base::Bind(&ServiceWorkerStorage::DidGetAllRegistrations, | 261 base::Bind(&ServiceWorkerStorage::DidGetAllRegistrations, |
| 262 weak_factory_.GetWeakPtr(), | 262 weak_factory_.GetWeakPtr(), |
| 263 callback, | 263 callback, |
| 264 base::Owned(registrations))); | 264 base::Owned(registrations))); |
| 265 } | 265 } |
| 266 | 266 |
| 267 void ServiceWorkerStorage::StoreRegistration( | 267 void ServiceWorkerStorage::StoreRegistration( |
| 268 ServiceWorkerRegistration* registration, | 268 ServiceWorkerRegistration* registration, |
| 269 ServiceWorkerVersion* version, | 269 ServiceWorkerVersion* version, |
| 270 const StatusCallback& callback) { | 270 const StatusCallback& callback) { |
| 271 DCHECK(registration); | 271 DCHECK(registration); |
| 272 DCHECK(version); | 272 DCHECK(version); |
| 273 | 273 |
| 274 DCHECK(state_ == INITIALIZED || state_ == DISABLED); | 274 DCHECK(IsReady() || IsDisabled()) << state_; |
| 275 if (state_ != INITIALIZED || !context_) { | 275 if (IsDisabled() || !context_) { |
| 276 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); | 276 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); |
| 277 return; | 277 return; |
| 278 } | 278 } |
| 279 | 279 |
| 280 ServiceWorkerDatabase::RegistrationData data; | 280 ServiceWorkerDatabase::RegistrationData data; |
| 281 data.registration_id = registration->id(); | 281 data.registration_id = registration->id(); |
| 282 data.scope = registration->pattern(); | 282 data.scope = registration->pattern(); |
| 283 data.script = registration->script_url(); | 283 data.script = registration->script_url(); |
| 284 data.has_fetch_handler = true; | 284 data.has_fetch_handler = true; |
| 285 data.version_id = version->version_id(); | 285 data.version_id = version->version_id(); |
| 286 data.last_update_check = base::Time::Now(); | 286 data.last_update_check = base::Time::Now(); |
| 287 data.is_active = false; // initially stored in the waiting state | 287 data.is_active = false; // initially stored in the waiting state |
| 288 | 288 |
| 289 ResourceList resources; | 289 ResourceList resources; |
| 290 version->script_cache_map()->GetResources(&resources); | 290 version->script_cache_map()->GetResources(&resources); |
| 291 | 291 |
| 292 if (state_ == HAS_STALE_RESOURCES) | |
| 293 DeleteStaleResources(); | |
| 294 | |
| 292 database_task_runner_->PostTask( | 295 database_task_runner_->PostTask( |
| 293 FROM_HERE, | 296 FROM_HERE, |
| 294 base::Bind(&WriteRegistrationInDB, | 297 base::Bind(&WriteRegistrationInDB, |
| 295 database_.get(), | 298 database_.get(), |
| 296 base::MessageLoopProxy::current(), | 299 base::MessageLoopProxy::current(), |
| 297 data, resources, | 300 data, resources, |
| 298 base::Bind(&ServiceWorkerStorage::DidStoreRegistration, | 301 base::Bind(&ServiceWorkerStorage::DidStoreRegistration, |
| 299 weak_factory_.GetWeakPtr(), | 302 weak_factory_.GetWeakPtr(), |
| 300 callback))); | 303 callback))); |
| 301 } | 304 } |
| 302 | 305 |
| 303 void ServiceWorkerStorage::UpdateToActiveState( | 306 void ServiceWorkerStorage::UpdateToActiveState( |
| 304 ServiceWorkerRegistration* registration, | 307 ServiceWorkerRegistration* registration, |
| 305 const StatusCallback& callback) { | 308 const StatusCallback& callback) { |
| 306 DCHECK(registration); | 309 DCHECK(registration); |
| 307 | 310 |
| 308 DCHECK(state_ == INITIALIZED || state_ == DISABLED); | 311 DCHECK(IsReady() || IsDisabled()) << state_; |
| 309 if (state_ != INITIALIZED || !context_) { | 312 if (IsDisabled() || !context_) { |
| 310 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); | 313 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); |
| 311 return; | 314 return; |
| 312 } | 315 } |
| 313 | 316 |
| 314 PostTaskAndReplyWithResult( | 317 PostTaskAndReplyWithResult( |
| 315 database_task_runner_, | 318 database_task_runner_, |
| 316 FROM_HERE, | 319 FROM_HERE, |
| 317 base::Bind(&ServiceWorkerDatabase::UpdateVersionToActive, | 320 base::Bind(&ServiceWorkerDatabase::UpdateVersionToActive, |
| 318 base::Unretained(database_.get()), | 321 base::Unretained(database_.get()), |
| 319 registration->id(), | 322 registration->id(), |
| 320 registration->script_url().GetOrigin()), | 323 registration->script_url().GetOrigin()), |
| 321 base::Bind(&ServiceWorkerStorage::DidUpdateToActiveState, | 324 base::Bind(&ServiceWorkerStorage::DidUpdateToActiveState, |
| 322 weak_factory_.GetWeakPtr(), | 325 weak_factory_.GetWeakPtr(), |
| 323 callback)); | 326 callback)); |
| 324 } | 327 } |
| 325 | 328 |
| 326 void ServiceWorkerStorage::DeleteRegistration( | 329 void ServiceWorkerStorage::DeleteRegistration( |
| 327 int64 registration_id, | 330 int64 registration_id, |
| 328 const GURL& origin, | 331 const GURL& origin, |
| 329 const StatusCallback& callback) { | 332 const StatusCallback& callback) { |
| 330 DCHECK(state_ == INITIALIZED || state_ == DISABLED); | 333 DCHECK(IsReady() || IsDisabled()); |
| 331 if (state_ != INITIALIZED || !context_) { | 334 if (IsDisabled() || !context_) { |
| 332 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); | 335 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED)); |
| 333 return; | 336 return; |
| 334 } | 337 } |
| 335 | 338 |
| 339 if (state_ == HAS_STALE_RESOURCES) | |
| 340 DeleteStaleResources(); | |
| 341 | |
| 336 database_task_runner_->PostTask( | 342 database_task_runner_->PostTask( |
| 337 FROM_HERE, | 343 FROM_HERE, |
| 338 base::Bind(&DeleteRegistrationFromDB, | 344 base::Bind(&DeleteRegistrationFromDB, |
| 339 database_.get(), | 345 database_.get(), |
| 340 base::MessageLoopProxy::current(), | 346 base::MessageLoopProxy::current(), |
| 341 registration_id, origin, | 347 registration_id, origin, |
| 342 base::Bind(&ServiceWorkerStorage::DidDeleteRegistration, | 348 base::Bind(&ServiceWorkerStorage::DidDeleteRegistration, |
| 343 weak_factory_.GetWeakPtr(), origin, callback))); | 349 weak_factory_.GetWeakPtr(), origin, callback))); |
| 344 | 350 |
| 345 // TODO(michaeln): Either its instance should also be | 351 // TODO(michaeln): Either its instance should also be |
| 346 // removed from liveregistrations map or the live object | 352 // removed from liveregistrations map or the live object |
| 347 // should marked as deleted in some way and not 'findable' | 353 // should marked as deleted in some way and not 'findable' |
| 348 // thereafter. | 354 // thereafter. |
| 349 } | 355 } |
| 350 | 356 |
| 351 scoped_ptr<ServiceWorkerResponseReader> | |
| 352 ServiceWorkerStorage::CreateResponseReader(int64 response_id) { | |
| 353 return make_scoped_ptr( | |
| 354 new ServiceWorkerResponseReader(response_id, disk_cache())); | |
| 355 } | |
| 356 | |
| 357 scoped_ptr<ServiceWorkerResponseWriter> | 357 scoped_ptr<ServiceWorkerResponseWriter> |
| 358 ServiceWorkerStorage::CreateResponseWriter(int64 response_id) { | 358 ServiceWorkerStorage::CreateResponseWriter(int64 response_id) { |
| 359 return make_scoped_ptr( | 359 return make_scoped_ptr( |
| 360 new ServiceWorkerResponseWriter(response_id, disk_cache())); | 360 new ServiceWorkerResponseWriter(response_id, disk_cache())); |
| 361 } | 361 } |
| 362 | 362 |
| 363 void ServiceWorkerStorage::StoreUncommittedReponseId(int64 id) { | 363 void ServiceWorkerStorage::StoreUncommittedResponseId(int64 id) { |
| 364 DCHECK_NE(kInvalidServiceWorkerResponseId, id); | 364 DCHECK_NE(kInvalidServiceWorkerResponseId, id); |
| 365 DCHECK(IsReady()) << state_; | |
| 366 | |
| 367 if (state_ == HAS_STALE_RESOURCES) | |
| 368 DeleteStaleResources(); | |
| 369 | |
| 365 database_task_runner_->PostTask( | 370 database_task_runner_->PostTask( |
| 366 FROM_HERE, | 371 FROM_HERE, |
| 367 base::Bind(base::IgnoreResult( | 372 base::Bind(base::IgnoreResult( |
| 368 &ServiceWorkerDatabase::WriteUncommittedResourceIds), | 373 &ServiceWorkerDatabase::WriteUncommittedResourceIds), |
| 369 base::Unretained(database_.get()), | 374 base::Unretained(database_.get()), |
| 370 std::set<int64>(&id, &id + 1))); | 375 std::set<int64>(&id, &id + 1))); |
| 371 } | 376 } |
| 372 | 377 |
| 373 void ServiceWorkerStorage::DoomUncommittedResponse(int64 id) { | 378 void ServiceWorkerStorage::DoomUncommittedResponse(int64 id) { |
| 374 DCHECK_NE(kInvalidServiceWorkerResponseId, id); | 379 DCHECK_NE(kInvalidServiceWorkerResponseId, id); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 390 FROM_HERE, | 395 FROM_HERE, |
| 391 base::Bind(&ServiceWorkerDatabase::DestroyDatabase, | 396 base::Bind(&ServiceWorkerDatabase::DestroyDatabase, |
| 392 base::Unretained(database_.get())), | 397 base::Unretained(database_.get())), |
| 393 base::Bind(&ServiceWorkerStorage::DidDeleteDatabase, | 398 base::Bind(&ServiceWorkerStorage::DidDeleteDatabase, |
| 394 weak_factory_.GetWeakPtr(), callback)); | 399 weak_factory_.GetWeakPtr(), callback)); |
| 395 } | 400 } |
| 396 | 401 |
| 397 int64 ServiceWorkerStorage::NewRegistrationId() { | 402 int64 ServiceWorkerStorage::NewRegistrationId() { |
| 398 if (state_ == DISABLED) | 403 if (state_ == DISABLED) |
| 399 return kInvalidServiceWorkerRegistrationId; | 404 return kInvalidServiceWorkerRegistrationId; |
| 400 DCHECK_EQ(INITIALIZED, state_); | 405 DCHECK(IsReady()) << state_; |
| 401 return next_registration_id_++; | 406 return next_registration_id_++; |
| 402 } | 407 } |
| 403 | 408 |
| 404 int64 ServiceWorkerStorage::NewVersionId() { | 409 int64 ServiceWorkerStorage::NewVersionId() { |
| 405 if (state_ == DISABLED) | 410 if (state_ == DISABLED) |
| 406 return kInvalidServiceWorkerVersionId; | 411 return kInvalidServiceWorkerVersionId; |
| 407 DCHECK_EQ(INITIALIZED, state_); | 412 DCHECK(IsReady()) << state_; |
| 408 return next_version_id_++; | 413 return next_version_id_++; |
| 409 } | 414 } |
| 410 | 415 |
| 411 int64 ServiceWorkerStorage::NewResourceId() { | 416 int64 ServiceWorkerStorage::NewResourceId() { |
| 412 if (state_ == DISABLED) | 417 if (state_ == DISABLED) |
| 413 return kInvalidServiceWorkerResourceId; | 418 return kInvalidServiceWorkerResourceId; |
| 414 DCHECK_EQ(INITIALIZED, state_); | 419 DCHECK(IsReady()) << state_; |
| 415 return next_resource_id_++; | 420 return next_resource_id_++; |
| 416 } | 421 } |
| 417 | 422 |
| 418 void ServiceWorkerStorage::NotifyInstallingRegistration( | 423 void ServiceWorkerStorage::NotifyInstallingRegistration( |
| 419 ServiceWorkerRegistration* registration) { | 424 ServiceWorkerRegistration* registration) { |
| 420 installing_registrations_[registration->id()] = registration; | 425 installing_registrations_[registration->id()] = registration; |
| 421 } | 426 } |
| 422 | 427 |
| 423 void ServiceWorkerStorage::NotifyDoneInstallingRegistration( | 428 void ServiceWorkerStorage::NotifyDoneInstallingRegistration( |
| 424 ServiceWorkerRegistration* registration, | 429 ServiceWorkerRegistration* registration, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 479 return base::FilePath(); | 484 return base::FilePath(); |
| 480 return path_.Append(kServiceWorkerDirectory).Append(kDatabaseName); | 485 return path_.Append(kServiceWorkerDirectory).Append(kDatabaseName); |
| 481 } | 486 } |
| 482 | 487 |
| 483 base::FilePath ServiceWorkerStorage::GetDiskCachePath() { | 488 base::FilePath ServiceWorkerStorage::GetDiskCachePath() { |
| 484 if (path_.empty()) | 489 if (path_.empty()) |
| 485 return base::FilePath(); | 490 return base::FilePath(); |
| 486 return path_.Append(kServiceWorkerDirectory).Append(kDiskCacheName); | 491 return path_.Append(kServiceWorkerDirectory).Append(kDiskCacheName); |
| 487 } | 492 } |
| 488 | 493 |
| 494 bool ServiceWorkerStorage::IsReady() { | |
| 495 return state_ == HAS_STALE_RESOURCES || state_ == INITIALIZED; | |
| 496 } | |
| 497 | |
| 489 bool ServiceWorkerStorage::LazyInitialize(const base::Closure& callback) { | 498 bool ServiceWorkerStorage::LazyInitialize(const base::Closure& callback) { |
| 490 if (!context_) | 499 if (!context_) |
| 491 return false; | 500 return false; |
| 492 | 501 |
| 493 switch (state_) { | 502 switch (state_) { |
| 503 case HAS_STALE_RESOURCES: | |
| 504 return true; | |
| 494 case INITIALIZED: | 505 case INITIALIZED: |
| 495 return true; | 506 return true; |
| 496 case DISABLED: | 507 case DISABLED: |
| 497 return false; | 508 return false; |
| 498 case INITIALIZING: | 509 case INITIALIZING: |
| 499 pending_tasks_.push_back(callback); | 510 pending_tasks_.push_back(callback); |
| 500 return false; | 511 return false; |
| 501 case UNINITIALIZED: | 512 case UNINITIALIZED: |
| 502 pending_tasks_.push_back(callback); | 513 pending_tasks_.push_back(callback); |
| 503 // Fall-through. | 514 // Fall-through. |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 518 InitialData* data, | 529 InitialData* data, |
| 519 ServiceWorkerDatabase::Status status) { | 530 ServiceWorkerDatabase::Status status) { |
| 520 DCHECK(data); | 531 DCHECK(data); |
| 521 DCHECK_EQ(INITIALIZING, state_); | 532 DCHECK_EQ(INITIALIZING, state_); |
| 522 | 533 |
| 523 if (status == ServiceWorkerDatabase::STATUS_OK) { | 534 if (status == ServiceWorkerDatabase::STATUS_OK) { |
| 524 next_registration_id_ = data->next_registration_id; | 535 next_registration_id_ = data->next_registration_id; |
| 525 next_version_id_ = data->next_version_id; | 536 next_version_id_ = data->next_version_id; |
| 526 next_resource_id_ = data->next_resource_id; | 537 next_resource_id_ = data->next_resource_id; |
| 527 registered_origins_.swap(data->origins); | 538 registered_origins_.swap(data->origins); |
| 528 state_ = INITIALIZED; | 539 state_ = HAS_STALE_RESOURCES; |
|
michaeln
2014/07/09 02:36:11
we don't really know if we have stale resources ye
falken
2014/07/09 04:31:14
correct, not the best name
| |
| 529 StartPurgingResources( | |
| 530 std::vector<int64>(data->purgeable_resource_ids.begin(), | |
| 531 data->purgeable_resource_ids.end())); | |
| 532 } else { | 540 } else { |
| 533 // TODO(nhiroki): Stringify |status| using StatusToString() defined in | 541 // TODO(nhiroki): Stringify |status| using StatusToString() defined in |
| 534 // service_worker_database.cc. | 542 // service_worker_database.cc. |
| 535 DVLOG(2) << "Failed to initialize: " << status; | 543 DVLOG(2) << "Failed to initialize: " << status; |
| 536 ScheduleDeleteAndStartOver(); | 544 ScheduleDeleteAndStartOver(); |
| 537 } | 545 } |
| 538 | 546 |
| 539 for (std::vector<base::Closure>::const_iterator it = pending_tasks_.begin(); | 547 for (std::vector<base::Closure>::const_iterator it = pending_tasks_.begin(); |
| 540 it != pending_tasks_.end(); ++it) { | 548 it != pending_tasks_.end(); ++it) { |
| 541 RunSoon(FROM_HERE, *it); | 549 RunSoon(FROM_HERE, *it); |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 856 if (version && version->HasControllee()) { | 864 if (version && version->HasControllee()) { |
| 857 deleted_version_resource_ids_[version_id] = resources; | 865 deleted_version_resource_ids_[version_id] = resources; |
| 858 version->AddListener(this); | 866 version->AddListener(this); |
| 859 } else { | 867 } else { |
| 860 StartPurgingResources(resources); | 868 StartPurgingResources(resources); |
| 861 } | 869 } |
| 862 } | 870 } |
| 863 | 871 |
| 864 void ServiceWorkerStorage::StartPurgingResources( | 872 void ServiceWorkerStorage::StartPurgingResources( |
| 865 const std::vector<int64>& ids) { | 873 const std::vector<int64>& ids) { |
| 874 DCHECK_EQ(INITIALIZED, state_); | |
| 866 for (size_t i = 0; i < ids.size(); ++i) | 875 for (size_t i = 0; i < ids.size(); ++i) |
| 867 purgeable_resource_ids_.push_back(ids[i]); | 876 purgeable_resource_ids_.push_back(ids[i]); |
| 868 ContinuePurgingResources(); | 877 ContinuePurgingResources(); |
| 869 } | 878 } |
| 870 | 879 |
| 871 void ServiceWorkerStorage::StartPurgingResources( | 880 void ServiceWorkerStorage::StartPurgingResources( |
| 872 const ResourceList& resources) { | 881 const ResourceList& resources) { |
| 873 for (size_t i = 0; i < resources.size(); ++i) | 882 for (size_t i = 0; i < resources.size(); ++i) |
| 874 purgeable_resource_ids_.push_back(resources[i].resource_id); | 883 purgeable_resource_ids_.push_back(resources[i].resource_id); |
| 875 ContinuePurgingResources(); | 884 ContinuePurgingResources(); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 905 database_task_runner_->PostTask( | 914 database_task_runner_->PostTask( |
| 906 FROM_HERE, | 915 FROM_HERE, |
| 907 base::Bind(base::IgnoreResult( | 916 base::Bind(base::IgnoreResult( |
| 908 &ServiceWorkerDatabase::ClearPurgeableResourceIds), | 917 &ServiceWorkerDatabase::ClearPurgeableResourceIds), |
| 909 base::Unretained(database_.get()), | 918 base::Unretained(database_.get()), |
| 910 std::set<int64>(&id, &id + 1))); | 919 std::set<int64>(&id, &id + 1))); |
| 911 | 920 |
| 912 ContinuePurgingResources(); | 921 ContinuePurgingResources(); |
| 913 } | 922 } |
| 914 | 923 |
| 924 void ServiceWorkerStorage::DeleteStaleResources() { | |
| 925 DCHECK_EQ(HAS_STALE_RESOURCES, state_); | |
| 926 state_ = INITIALIZED; | |
| 927 database_task_runner_->PostTask( | |
| 928 FROM_HERE, | |
| 929 base::Bind(&ServiceWorkerStorage::CollectStaleResourcesFromDB, | |
| 930 database_.get(), | |
| 931 base::MessageLoopProxy::current(), | |
| 932 base::Bind(&ServiceWorkerStorage::DidCollectStaleResources, | |
| 933 weak_factory_.GetWeakPtr()))); | |
| 934 } | |
| 935 | |
| 936 void ServiceWorkerStorage::DidCollectStaleResources( | |
| 937 const std::vector<int64>& stale_resource_ids, | |
| 938 ServiceWorkerDatabase::Status status) { | |
| 939 DCHECK_EQ(ServiceWorkerDatabase::STATUS_OK, status); | |
| 940 if (status != ServiceWorkerDatabase::STATUS_OK) | |
| 941 return; | |
| 942 StartPurgingResources(stale_resource_ids); | |
| 943 } | |
| 944 | |
| 945 void ServiceWorkerStorage::CollectStaleResourcesFromDB( | |
| 946 ServiceWorkerDatabase* database, | |
| 947 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | |
| 948 const GetResourcesCallback& callback) { | |
| 949 std::set<int64> ids; | |
| 950 ServiceWorkerDatabase::Status status = | |
| 951 database->GetUncommittedResourceIds(&ids); | |
| 952 if (status != ServiceWorkerDatabase::STATUS_OK) { | |
| 953 original_task_runner->PostTask( | |
| 954 FROM_HERE, | |
| 955 base::Bind( | |
| 956 callback, std::vector<int64>(ids.begin(), ids.end()), status)); | |
| 957 return; | |
| 958 } | |
| 959 | |
| 960 status = database->PurgeUncommittedResourceIds(ids); | |
| 961 if (status != ServiceWorkerDatabase::STATUS_OK) { | |
| 962 original_task_runner->PostTask( | |
| 963 FROM_HERE, | |
| 964 base::Bind( | |
| 965 callback, std::vector<int64>(ids.begin(), ids.end()), status)); | |
| 966 return; | |
| 967 } | |
| 968 | |
| 969 ids.clear(); | |
| 970 status = database->GetPurgeableResourceIds(&ids); | |
| 971 original_task_runner->PostTask( | |
| 972 FROM_HERE, | |
| 973 base::Bind(callback, std::vector<int64>(ids.begin(), ids.end()), status)); | |
| 974 } | |
| 975 | |
| 976 scoped_ptr<ServiceWorkerResponseReader> | |
| 977 ServiceWorkerStorage::CreateResponseReader(int64 response_id) { | |
|
michaeln
2014/07/09 02:36:11
i think this accidentally got move far away from i
falken
2014/07/09 04:31:14
oops yes, done
| |
| 978 return make_scoped_ptr( | |
| 979 new ServiceWorkerResponseReader(response_id, disk_cache())); | |
| 980 } | |
| 981 | |
| 915 void ServiceWorkerStorage::ReadInitialDataFromDB( | 982 void ServiceWorkerStorage::ReadInitialDataFromDB( |
| 916 ServiceWorkerDatabase* database, | 983 ServiceWorkerDatabase* database, |
| 917 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 984 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 918 const InitializeCallback& callback) { | 985 const InitializeCallback& callback) { |
| 919 DCHECK(database); | 986 DCHECK(database); |
| 920 scoped_ptr<ServiceWorkerStorage::InitialData> data( | 987 scoped_ptr<ServiceWorkerStorage::InitialData> data( |
| 921 new ServiceWorkerStorage::InitialData()); | 988 new ServiceWorkerStorage::InitialData()); |
| 922 | 989 |
| 923 ServiceWorkerDatabase::Status status = | 990 ServiceWorkerDatabase::Status status = |
| 924 database->GetNextAvailableIds(&data->next_registration_id, | 991 database->GetNextAvailableIds(&data->next_registration_id, |
| 925 &data->next_version_id, | 992 &data->next_version_id, |
| 926 &data->next_resource_id); | 993 &data->next_resource_id); |
| 927 if (status != ServiceWorkerDatabase::STATUS_OK) { | 994 if (status != ServiceWorkerDatabase::STATUS_OK) { |
| 928 original_task_runner->PostTask( | 995 original_task_runner->PostTask( |
| 929 FROM_HERE, base::Bind(callback, base::Owned(data.release()), status)); | 996 FROM_HERE, base::Bind(callback, base::Owned(data.release()), status)); |
| 930 return; | 997 return; |
| 931 } | 998 } |
| 932 | 999 |
| 933 status = database->GetOriginsWithRegistrations(&data->origins); | 1000 status = database->GetOriginsWithRegistrations(&data->origins); |
| 934 if (status != ServiceWorkerDatabase::STATUS_OK) { | |
| 935 original_task_runner->PostTask( | |
| 936 FROM_HERE, base::Bind(callback, base::Owned(data.release()), status)); | |
| 937 return; | |
| 938 } | |
| 939 | |
| 940 // TODO: Also purge uncommitted resources. | |
| 941 status = database->GetPurgeableResourceIds(&data->purgeable_resource_ids); | |
| 942 original_task_runner->PostTask( | 1001 original_task_runner->PostTask( |
| 943 FROM_HERE, base::Bind(callback, base::Owned(data.release()), status)); | 1002 FROM_HERE, base::Bind(callback, base::Owned(data.release()), status)); |
| 944 } | 1003 } |
| 945 | 1004 |
| 946 void ServiceWorkerStorage::DeleteRegistrationFromDB( | 1005 void ServiceWorkerStorage::DeleteRegistrationFromDB( |
| 947 ServiceWorkerDatabase* database, | 1006 ServiceWorkerDatabase* database, |
| 948 scoped_refptr<base::SequencedTaskRunner> original_task_runner, | 1007 scoped_refptr<base::SequencedTaskRunner> original_task_runner, |
| 949 int64 registration_id, | 1008 int64 registration_id, |
| 950 const GURL& origin, | 1009 const GURL& origin, |
| 951 const DeleteRegistrationCallback& callback) { | 1010 const DeleteRegistrationCallback& callback) { |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1141 // Give up the corruption recovery until the browser restarts. | 1200 // Give up the corruption recovery until the browser restarts. |
| 1142 LOG(ERROR) << "Failed to delete the diskcache."; | 1201 LOG(ERROR) << "Failed to delete the diskcache."; |
| 1143 callback.Run(SERVICE_WORKER_ERROR_FAILED); | 1202 callback.Run(SERVICE_WORKER_ERROR_FAILED); |
| 1144 return; | 1203 return; |
| 1145 } | 1204 } |
| 1146 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; | 1205 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; |
| 1147 callback.Run(SERVICE_WORKER_OK); | 1206 callback.Run(SERVICE_WORKER_OK); |
| 1148 } | 1207 } |
| 1149 | 1208 |
| 1150 } // namespace content | 1209 } // namespace content |
| OLD | NEW |