service_worker_provider_context.h revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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;
27struct ServiceWorkerProviderContextDeleter;
28class ThreadSafeSender;
29
30// An instance of this class holds document-related information (e.g.
31// .current). Created and destructed on the main thread.
32// TODO(kinuko): To support navigator.serviceWorker in dedicated workers
33// this needs to be RefCountedThreadSafe and .current info needs to be
34// handled in a thread-safe manner (e.g. by a lock etc).
35class ServiceWorkerProviderContext
36    : public base::RefCounted<ServiceWorkerProviderContext> {
37 public:
38  explicit ServiceWorkerProviderContext(int provider_id);
39
40  // Called from ServiceWorkerDispatcher.
41  void OnServiceWorkerStateChanged(int handle_id,
42                                   blink::WebServiceWorkerState state);
43  void OnSetWaitingServiceWorker(int provider_id,
44                                 const ServiceWorkerObjectInfo& info);
45  void OnSetCurrentServiceWorker(int provider_id,
46                                 const ServiceWorkerObjectInfo& info);
47
48  int provider_id() const { return provider_id_; }
49
50  ServiceWorkerHandleReference* waiting();
51  // Gets the context's handle reference for .controller.
52  // TODO(dominicc): Rename this to "controller".
53  ServiceWorkerHandleReference* current();
54
55  // Gets the handle ID of the controller, or
56  // kInvalidServiceWorkerHandleId if the provider is not controlled
57  // by a Service Worker.
58  int current_handle_id() const;
59
60  // Gets the handle ID of the waiting Service Worker, or
61  // kInvalidServiceWorkerHandleId if the provider does not have a
62  // waiting Service Worker.
63  int waiting_handle_id() const;
64
65 private:
66  friend class base::RefCounted<ServiceWorkerProviderContext>;
67  ~ServiceWorkerProviderContext();
68
69  const int provider_id_;
70  scoped_refptr<base::MessageLoopProxy> main_thread_loop_proxy_;
71  scoped_refptr<ThreadSafeSender> thread_safe_sender_;
72  scoped_ptr<ServiceWorkerHandleReference> waiting_;
73  scoped_ptr<ServiceWorkerHandleReference> current_;
74
75  DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderContext);
76};
77
78}  // namespace content
79
80#endif  // CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_CONTEXT_H_
81