Upstream version 7.36.149.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 "base/id_map.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/strings/string16.h"
11 #include "content/browser/service_worker/service_worker_registration_status.h"
12 #include "content/public/browser/browser_message_filter.h"
13
14 class GURL;
15 struct EmbeddedWorkerHostMsg_ReportConsoleMessage_Params;
16
17 namespace content {
18
19 class MessagePortMessageFilter;
20 class ServiceWorkerContextCore;
21 class ServiceWorkerContextWrapper;
22 class ServiceWorkerHandle;
23 class ServiceWorkerProviderHost;
24 class ServiceWorkerRegistration;
25
26 class CONTENT_EXPORT ServiceWorkerDispatcherHost : public BrowserMessageFilter {
27  public:
28   ServiceWorkerDispatcherHost(
29       int render_process_id,
30       MessagePortMessageFilter* message_port_message_filter);
31
32   void Init(ServiceWorkerContextWrapper* context_wrapper);
33
34   // BrowserMessageFilter implementation
35   virtual void OnFilterAdded(IPC::Channel* channel) OVERRIDE;
36   virtual void OnDestruct() const OVERRIDE;
37   virtual bool OnMessageReceived(const IPC::Message& message,
38                                  bool* message_was_ok) OVERRIDE;
39
40   // IPC::Sender implementation
41
42   // Send() queues the message until the underlying channel is ready.  This
43   // class assumes that Send() can only fail after that when the renderer
44   // process has terminated, at which point the whole instance will eventually
45   // be destroyed.
46   virtual bool Send(IPC::Message* message) OVERRIDE;
47
48   void RegisterServiceWorkerHandle(scoped_ptr<ServiceWorkerHandle> handle);
49
50   MessagePortMessageFilter* message_port_message_filter() {
51     return message_port_message_filter_;
52   }
53
54  protected:
55   virtual ~ServiceWorkerDispatcherHost();
56
57  private:
58   friend class BrowserThread;
59   friend class base::DeleteHelper<ServiceWorkerDispatcherHost>;
60   friend class TestingServiceWorkerDispatcherHost;
61
62   // IPC Message handlers
63   void OnRegisterServiceWorker(int thread_id,
64                                int request_id,
65                                int provider_id,
66                                const GURL& pattern,
67                                const GURL& script_url);
68   void OnUnregisterServiceWorker(int thread_id,
69                                  int request_id,
70                                  int provider_id,
71                                  const GURL& pattern);
72   void OnProviderCreated(int provider_id);
73   void OnProviderDestroyed(int provider_id);
74   void OnSetHostedVersionId(int provider_id, int64 version_id);
75   void OnWorkerScriptLoaded(int embedded_worker_id);
76   void OnWorkerScriptLoadFailed(int embedded_worker_id);
77   void OnWorkerStarted(int thread_id,
78                        int embedded_worker_id);
79   void OnWorkerStopped(int embedded_worker_id);
80   void OnReportException(int embedded_worker_id,
81                          const base::string16& error_message,
82                          int line_number,
83                          int column_number,
84                          const GURL& source_url);
85   void OnReportConsoleMessage(
86       int embedded_worker_id,
87       const EmbeddedWorkerHostMsg_ReportConsoleMessage_Params& params);
88   void OnPostMessage(int handle_id,
89                      const base::string16& message,
90                      const std::vector<int>& sent_message_port_ids);
91   void OnIncrementServiceWorkerRefCount(int handle_id);
92   void OnDecrementServiceWorkerRefCount(int handle_id);
93   void OnPostMessageToWorker(int handle_id,
94                              const base::string16& message,
95                              const std::vector<int>& sent_message_port_ids);
96   void OnServiceWorkerObjectDestroyed(int handle_id);
97
98   // Callbacks from ServiceWorkerContextCore
99   void RegistrationComplete(int thread_id,
100                             int request_id,
101                             ServiceWorkerStatusCode status,
102                             int64 registration_id,
103                             int64 version_id);
104
105   void UnregistrationComplete(int thread_id,
106                               int request_id,
107                               ServiceWorkerStatusCode status);
108
109   void SendRegistrationError(int thread_id,
110                              int request_id,
111                              ServiceWorkerStatusCode status);
112
113   int render_process_id_;
114   MessagePortMessageFilter* const message_port_message_filter_;
115   base::WeakPtr<ServiceWorkerContextCore> context_;
116
117   IDMap<ServiceWorkerHandle, IDMapOwnPointer> handles_;
118
119   bool channel_ready_;  // True after BrowserMessageFilter::channel_ != NULL.
120   ScopedVector<IPC::Message> pending_messages_;
121
122   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcherHost);
123 };
124
125 }  // namespace content
126
127 #endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_HOST_H_