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
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_HOST_H_
6#define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_HOST_H_
7
8#include <vector>
9
10#include "base/id_map.h"
11#include "base/memory/weak_ptr.h"
12#include "base/strings/string16.h"
13#include "content/browser/service_worker/service_worker_registration_status.h"
14#include "content/public/browser/browser_message_filter.h"
15
16class GURL;
17struct EmbeddedWorkerHostMsg_ReportConsoleMessage_Params;
18
19namespace content {
20
21class MessagePortMessageFilter;
22class ServiceWorkerContextCore;
23class ServiceWorkerContextWrapper;
24class ServiceWorkerHandle;
25class ServiceWorkerProviderHost;
26class ServiceWorkerRegistration;
27class ServiceWorkerRegistrationHandle;
28struct ServiceWorkerRegistrationObjectInfo;
29struct ServiceWorkerVersionAttributes;
30
31class CONTENT_EXPORT ServiceWorkerDispatcherHost : public BrowserMessageFilter {
32 public:
33  ServiceWorkerDispatcherHost(
34      int render_process_id,
35      MessagePortMessageFilter* message_port_message_filter);
36
37  void Init(ServiceWorkerContextWrapper* context_wrapper);
38
39  // BrowserMessageFilter implementation
40  virtual void OnFilterAdded(IPC::Sender* sender) OVERRIDE;
41  virtual void OnDestruct() const OVERRIDE;
42  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
43
44  // IPC::Sender implementation
45
46  // Send() queues the message until the underlying sender is ready.  This
47  // class assumes that Send() can only fail after that when the renderer
48  // process has terminated, at which point the whole instance will eventually
49  // be destroyed.
50  virtual bool Send(IPC::Message* message) OVERRIDE;
51
52  // Returns the existing registration handle whose reference count is
53  // incremented or newly created one if it doesn't exist.
54  ServiceWorkerRegistrationHandle* GetOrCreateRegistrationHandle(
55      int provider_id,
56      ServiceWorkerRegistration* registration);
57
58  void RegisterServiceWorkerHandle(scoped_ptr<ServiceWorkerHandle> handle);
59  void RegisterServiceWorkerRegistrationHandle(
60      scoped_ptr<ServiceWorkerRegistrationHandle> handle);
61
62  MessagePortMessageFilter* message_port_message_filter() {
63    return message_port_message_filter_;
64  }
65
66 protected:
67  virtual ~ServiceWorkerDispatcherHost();
68
69 private:
70  friend class BrowserThread;
71  friend class base::DeleteHelper<ServiceWorkerDispatcherHost>;
72  friend class TestingServiceWorkerDispatcherHost;
73
74  // IPC Message handlers
75  void OnRegisterServiceWorker(int thread_id,
76                               int request_id,
77                               int provider_id,
78                               const GURL& pattern,
79                               const GURL& script_url);
80  void OnUnregisterServiceWorker(int thread_id,
81                                 int request_id,
82                                 int provider_id,
83                                 const GURL& pattern);
84  void OnGetRegistration(int thread_id,
85                         int request_id,
86                         int provider_id,
87                         const GURL& document_url);
88  void OnProviderCreated(int provider_id);
89  void OnProviderDestroyed(int provider_id);
90  void OnSetHostedVersionId(int provider_id, int64 version_id);
91  void OnWorkerReadyForInspection(int embedded_worker_id);
92  void OnWorkerScriptLoaded(int embedded_worker_id, int thread_id);
93  void OnWorkerScriptLoadFailed(int embedded_worker_id);
94  void OnWorkerStarted(int embedded_worker_id);
95  void OnWorkerStopped(int embedded_worker_id);
96  void OnPausedAfterDownload(int embedded_worker_id);
97  void OnReportException(int embedded_worker_id,
98                         const base::string16& error_message,
99                         int line_number,
100                         int column_number,
101                         const GURL& source_url);
102  void OnReportConsoleMessage(
103      int embedded_worker_id,
104      const EmbeddedWorkerHostMsg_ReportConsoleMessage_Params& params);
105  void OnPostMessage(int handle_id,
106                     const base::string16& message,
107                     const std::vector<int>& sent_message_port_ids);
108  void OnIncrementServiceWorkerRefCount(int handle_id);
109  void OnDecrementServiceWorkerRefCount(int handle_id);
110  void OnIncrementRegistrationRefCount(int registration_handle_id);
111  void OnDecrementRegistrationRefCount(int registration_handle_id);
112  void OnPostMessageToWorker(int handle_id,
113                             const base::string16& message,
114                             const std::vector<int>& sent_message_port_ids);
115  void OnServiceWorkerObjectDestroyed(int handle_id);
116
117  ServiceWorkerRegistrationHandle* FindRegistrationHandle(
118      int provider_id,
119      int64 registration_id);
120
121  void GetRegistrationObjectInfoAndVersionAttributes(
122      int provider_id,
123      ServiceWorkerRegistration* registration,
124      ServiceWorkerRegistrationObjectInfo* info,
125      ServiceWorkerVersionAttributes* attrs);
126
127  // Callbacks from ServiceWorkerContextCore
128  void RegistrationComplete(int thread_id,
129                            int provider_id,
130                            int request_id,
131                            ServiceWorkerStatusCode status,
132                            int64 registration_id,
133                            int64 version_id);
134
135  void UnregistrationComplete(int thread_id,
136                              int request_id,
137                              ServiceWorkerStatusCode status);
138
139  void GetRegistrationComplete(
140      int thread_id,
141      int provider_id,
142      int request_id,
143      ServiceWorkerStatusCode status,
144      const scoped_refptr<ServiceWorkerRegistration>& registration);
145
146  void SendRegistrationError(int thread_id,
147                             int request_id,
148                             ServiceWorkerStatusCode status);
149
150  void SendUnregistrationError(int thread_id,
151                               int request_id,
152                               ServiceWorkerStatusCode status);
153
154  void SendGetRegistrationError(int thread_id,
155                                int request_id,
156                                ServiceWorkerStatusCode status);
157
158  ServiceWorkerContextCore* GetContext();
159
160  int render_process_id_;
161  MessagePortMessageFilter* const message_port_message_filter_;
162  scoped_refptr<ServiceWorkerContextWrapper> context_wrapper_;
163
164  IDMap<ServiceWorkerHandle, IDMapOwnPointer> handles_;
165  IDMap<ServiceWorkerRegistrationHandle, IDMapOwnPointer> registration_handles_;
166
167  bool channel_ready_;  // True after BrowserMessageFilter::sender_ != NULL.
168  ScopedVector<IPC::Message> pending_messages_;
169
170  DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcherHost);
171};
172
173}  // namespace content
174
175#endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_HOST_H_
176