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#ifndef CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_CONTEXT_H_
6#define CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_CONTEXT_H_
7
8#include <set>
9#include <vector>
10
11#include "base/memory/ref_counted.h"
12#include "base/sequenced_task_runner_helpers.h"
13#include "base/synchronization/lock.h"
14#include "content/common/service_worker/service_worker_types.h"
15
16namespace base {
17class MessageLoopProxy;
18}
19
20namespace IPC {
21class Message;
22}
23
24namespace content {
25
26class ServiceWorkerHandleReference;
27class ServiceWorkerRegistrationHandleReference;
28struct ServiceWorkerProviderContextDeleter;
29class ThreadSafeSender;
30
31// An instance of this class holds document-related information (e.g.
32// .controller). Created and destructed on the main thread.
33// TODO(kinuko): To support navigator.serviceWorker in dedicated workers
34// this needs to be RefCountedThreadSafe and .controller info needs to be
35// handled in a thread-safe manner (e.g. by a lock etc).
36class ServiceWorkerProviderContext
37    : public base::RefCounted<ServiceWorkerProviderContext> {
38 public:
39  explicit ServiceWorkerProviderContext(int provider_id);
40
41  // Called from ServiceWorkerDispatcher.
42  void OnAssociateRegistration(const ServiceWorkerRegistrationObjectInfo& info,
43                               const ServiceWorkerVersionAttributes& attrs);
44  void OnDisassociateRegistration();
45  void OnServiceWorkerStateChanged(int handle_id,
46                                   blink::WebServiceWorkerState state);
47  void OnSetInstallingServiceWorker(int registration_handle_id,
48                                    const ServiceWorkerObjectInfo& info);
49  void OnSetWaitingServiceWorker(int registration_handle_id,
50                                 const ServiceWorkerObjectInfo& info);
51  void OnSetActiveServiceWorker(int registration_handle_id,
52                                const ServiceWorkerObjectInfo& info);
53  void OnSetControllerServiceWorker(int registration_handle_id,
54                                    const ServiceWorkerObjectInfo& info);
55
56  int provider_id() const { return provider_id_; }
57
58  ServiceWorkerHandleReference* installing();
59  ServiceWorkerHandleReference* waiting();
60  ServiceWorkerHandleReference* active();
61  ServiceWorkerHandleReference* controller();
62  ServiceWorkerRegistrationHandleReference* registration();
63
64  ServiceWorkerVersionAttributes GetVersionAttributes();
65
66  // Gets the handle ID of the installing Service Worker, or
67  // kInvalidServiceWorkerHandleId if the provider does not have a
68  // installing Service Worker.
69  int installing_handle_id() const;
70
71  // Gets the handle ID of the waiting Service Worker, or
72  // kInvalidServiceWorkerHandleId if the provider does not have a
73  // waiting Service Worker.
74  int waiting_handle_id() const;
75
76  // Gets the handle ID of the active Service Worker, or
77  // kInvalidServiceWorkerHandleId if the provider does not have an active
78  // Service Worker.
79  int active_handle_id() const;
80
81  // Gets the handle ID of the controller Service Worker, or
82  // kInvalidServiceWorkerHandleId if the provider is not controlled
83  // by a Service Worker.
84  int controller_handle_id() const;
85
86  // Gets the handle ID of the associated registration, or
87  // kInvalidRegistrationHandleId if the provider is not associated with any
88  // registration.
89  int registration_handle_id() const;
90
91 private:
92  friend class base::RefCounted<ServiceWorkerProviderContext>;
93  ~ServiceWorkerProviderContext();
94
95  bool IsAssociatedWithRegistration(int registration_handle_id) const;
96
97  const int provider_id_;
98  scoped_refptr<base::MessageLoopProxy> main_thread_loop_proxy_;
99  scoped_refptr<ThreadSafeSender> thread_safe_sender_;
100  scoped_ptr<ServiceWorkerHandleReference> installing_;
101  scoped_ptr<ServiceWorkerHandleReference> waiting_;
102  scoped_ptr<ServiceWorkerHandleReference> active_;
103  scoped_ptr<ServiceWorkerHandleReference> controller_;
104  scoped_ptr<ServiceWorkerRegistrationHandleReference> registration_;
105
106  DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderContext);
107};
108
109}  // namespace content
110
111#endif  // CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_CONTEXT_H_
112