service_worker_unregister_job.cc revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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
3// found in the LICENSE file.
4
5#include "content/browser/service_worker/service_worker_unregister_job.h"
6
7#include "content/browser/service_worker/service_worker_context_core.h"
8#include "content/browser/service_worker/service_worker_job_coordinator.h"
9#include "content/browser/service_worker/service_worker_registration.h"
10#include "content/browser/service_worker/service_worker_storage.h"
11
12namespace content {
13
14typedef ServiceWorkerRegisterJobBase::RegistrationJobType RegistrationJobType;
15
16ServiceWorkerUnregisterJob::ServiceWorkerUnregisterJob(
17    base::WeakPtr<ServiceWorkerContextCore> context,
18    const GURL& pattern)
19    : context_(context),
20      pattern_(pattern),
21      weak_factory_(this) {}
22
23ServiceWorkerUnregisterJob::~ServiceWorkerUnregisterJob() {}
24
25void ServiceWorkerUnregisterJob::AddCallback(
26    const UnregistrationCallback& callback) {
27  callbacks_.push_back(callback);
28}
29
30void ServiceWorkerUnregisterJob::Start() {
31  context_->storage()->FindRegistrationForPattern(
32      pattern_,
33      base::Bind(&ServiceWorkerUnregisterJob::DeleteExistingRegistration,
34                 weak_factory_.GetWeakPtr()));
35}
36
37bool ServiceWorkerUnregisterJob::Equals(ServiceWorkerRegisterJobBase* job) {
38  if (job->GetType() != GetType())
39    return false;
40  return static_cast<ServiceWorkerUnregisterJob*>(job)->pattern_ == pattern_;
41}
42
43RegistrationJobType ServiceWorkerUnregisterJob::GetType() {
44  return ServiceWorkerRegisterJobBase::UNREGISTRATION;
45}
46
47void ServiceWorkerUnregisterJob::DeleteExistingRegistration(
48    ServiceWorkerStatusCode status,
49    const scoped_refptr<ServiceWorkerRegistration>& registration) {
50  if (status == SERVICE_WORKER_OK) {
51    DCHECK(registration);
52    // TODO(michaeln): Deactivate the live registration object and
53    // eventually call storage->DeleteVersionResources()
54    // when the version no longer has any controllees.
55    context_->storage()->DeleteRegistration(
56        registration->id(),
57        base::Bind(&ServiceWorkerUnregisterJob::Complete,
58                   weak_factory_.GetWeakPtr()));
59    return;
60  }
61
62  if (status == SERVICE_WORKER_ERROR_NOT_FOUND) {
63    DCHECK(!registration);
64    Complete(SERVICE_WORKER_OK);
65    return;
66  }
67
68  Complete(status);
69}
70
71void ServiceWorkerUnregisterJob::Complete(ServiceWorkerStatusCode status) {
72  for (std::vector<UnregistrationCallback>::iterator it = callbacks_.begin();
73       it != callbacks_.end();
74       ++it) {
75    it->Run(status);
76  }
77  context_->job_coordinator()->FinishJob(pattern_, this);
78}
79
80}  // namespace content
81