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