1e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// found in the LICENSE file.
4e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
5e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_UNREGISTER_JOB_H_
6e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_UNREGISTER_JOB_H_
7e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
8e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include <vector>
9e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
10e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "base/memory/weak_ptr.h"
11e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "content/browser/service_worker/service_worker_register_job_base.h"
12e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "content/common/service_worker/service_worker_status_code.h"
13e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "url/gurl.h"
14e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
15e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochnamespace content {
16e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
17e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochclass EmbeddedWorkerRegistry;
18c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochclass ServiceWorkerContextCore;
19e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochclass ServiceWorkerJobCoordinator;
20e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochclass ServiceWorkerRegistration;
21e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochclass ServiceWorkerStorage;
22e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
23e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// Handles the unregistration of a Service Worker.
24e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch//
25e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// The unregistration process is primarily cleanup, removing everything that was
26e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// created during the registration process, including the
27e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// ServiceWorkerRegistration itself.
28e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochclass ServiceWorkerUnregisterJob : public ServiceWorkerRegisterJobBase {
29e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch public:
30e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  typedef base::Callback<void(ServiceWorkerStatusCode status)>
31e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      UnregistrationCallback;
32e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
33c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  ServiceWorkerUnregisterJob(base::WeakPtr<ServiceWorkerContextCore> context,
34e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch                             const GURL& pattern);
35e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual ~ServiceWorkerUnregisterJob();
36e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
37e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // Registers a callback to be called when the job completes (whether
38e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // successfully or not). Multiple callbacks may be registered.
39e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  void AddCallback(const UnregistrationCallback& callback);
40e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
41e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // ServiceWorkerRegisterJobBase implementation:
42e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual void Start() OVERRIDE;
436d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual void Abort() OVERRIDE;
44e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual bool Equals(ServiceWorkerRegisterJobBase* job) OVERRIDE;
45e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual RegistrationJobType GetType() OVERRIDE;
46e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
47e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch private:
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void OnRegistrationFound(
49e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      ServiceWorkerStatusCode status,
50e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      const scoped_refptr<ServiceWorkerRegistration>& registration);
51e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  void Complete(ServiceWorkerStatusCode status);
526d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  void CompleteInternal(ServiceWorkerStatusCode status);
536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void ResolvePromise(ServiceWorkerStatusCode status);
54e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
55c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  base::WeakPtr<ServiceWorkerContextCore> context_;
56e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  const GURL pattern_;
57e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  std::vector<UnregistrationCallback> callbacks_;
586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  bool is_promise_resolved_;
59e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  base::WeakPtrFactory<ServiceWorkerUnregisterJob> weak_factory_;
60e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
61e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  DISALLOW_COPY_AND_ASSIGN(ServiceWorkerUnregisterJob);
62e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch};
63e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}  // namespace content
64e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
65e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_UNREGISTER_JOB_H_
66