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.
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_HOST_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_HOST_H_
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"
15 struct EmbeddedWorkerHostMsg_ReportConsoleMessage_Params;
19 class MessagePortMessageFilter;
20 class ServiceWorkerContextCore;
21 class ServiceWorkerContextWrapper;
22 class ServiceWorkerHandle;
23 class ServiceWorkerProviderHost;
24 class ServiceWorkerRegistration;
26 class CONTENT_EXPORT ServiceWorkerDispatcherHost : public BrowserMessageFilter {
28 ServiceWorkerDispatcherHost(
29 int render_process_id,
30 MessagePortMessageFilter* message_port_message_filter);
32 void Init(ServiceWorkerContextWrapper* context_wrapper);
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;
40 // IPC::Sender implementation
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
46 virtual bool Send(IPC::Message* message) OVERRIDE;
48 void RegisterServiceWorkerHandle(scoped_ptr<ServiceWorkerHandle> handle);
50 MessagePortMessageFilter* message_port_message_filter() {
51 return message_port_message_filter_;
55 virtual ~ServiceWorkerDispatcherHost();
58 friend class BrowserThread;
59 friend class base::DeleteHelper<ServiceWorkerDispatcherHost>;
60 friend class TestingServiceWorkerDispatcherHost;
62 // IPC Message handlers
63 void OnRegisterServiceWorker(int thread_id,
67 const GURL& script_url);
68 void OnUnregisterServiceWorker(int thread_id,
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,
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);
98 // Callbacks from ServiceWorkerContextCore
99 void RegistrationComplete(int thread_id,
101 ServiceWorkerStatusCode status,
102 int64 registration_id,
105 void UnregistrationComplete(int thread_id,
107 ServiceWorkerStatusCode status);
109 void SendRegistrationError(int thread_id,
111 ServiceWorkerStatusCode status);
113 int render_process_id_;
114 MessagePortMessageFilter* const message_port_message_filter_;
115 base::WeakPtr<ServiceWorkerContextCore> context_;
117 IDMap<ServiceWorkerHandle, IDMapOwnPointer> handles_;
119 bool channel_ready_; // True after BrowserMessageFilter::channel_ != NULL.
120 ScopedVector<IPC::Message> pending_messages_;
122 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcherHost);
125 } // namespace content
127 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_HOST_H_