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_CHILD_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_H_
6 #define CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_H_
11 #include "base/id_map.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/strings/string16.h"
14 #include "content/child/worker_task_runner.h"
15 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h"
16 #include "third_party/WebKit/public/platform/WebServiceWorkerProvider.h"
17 #include "third_party/WebKit/public/platform/WebServiceWorkerState.h"
31 class ServiceWorkerMessageFilter;
32 struct ServiceWorkerObjectInfo;
33 class ServiceWorkerProviderContext;
34 class ThreadSafeSender;
35 class WebServiceWorkerImpl;
37 // This class manages communication with the browser process about
38 // registration of the service worker, exposed to renderer and worker
39 // scripts through methods like navigator.registerServiceWorker().
40 class ServiceWorkerDispatcher : public WorkerTaskRunner::Observer {
42 explicit ServiceWorkerDispatcher(ThreadSafeSender* thread_safe_sender);
43 virtual ~ServiceWorkerDispatcher();
45 void OnMessageReceived(const IPC::Message& msg);
46 bool Send(IPC::Message* msg);
48 // Corresponds to navigator.serviceWorker.register()
49 void RegisterServiceWorker(
52 const GURL& script_url,
53 blink::WebServiceWorkerProvider::WebServiceWorkerCallbacks* callbacks);
54 // Corresponds to navigator.serviceWorker.unregister()
55 void UnregisterServiceWorker(
58 blink::WebServiceWorkerProvider::WebServiceWorkerCallbacks* callbacks);
60 // Called when a new provider context for a document is created. Usually
61 // this happens when a new document is being loaded, and is called much
62 // earlier than AddScriptClient.
63 // (This is attached only to the document thread's ServiceWorkerDispatcher)
64 void AddProviderContext(ServiceWorkerProviderContext* provider_context);
65 void RemoveProviderContext(ServiceWorkerProviderContext* provider_context);
67 // Called when navigator.serviceWorker is instantiated or detached
68 // for a document whose provider can be identified by |provider_id|.
69 void AddScriptClient(int provider_id,
70 blink::WebServiceWorkerProviderClient* client);
71 void RemoveScriptClient(int provider_id);
73 // |thread_safe_sender| needs to be passed in because if the call leads to
74 // construction it will be needed.
75 static ServiceWorkerDispatcher* GetOrCreateThreadSpecificInstance(
76 ThreadSafeSender* thread_safe_sender);
78 // Unlike GetOrCreateThreadSpecificInstance() this doesn't create a new
79 // instance if thread-local instance doesn't exist.
80 static ServiceWorkerDispatcher* GetThreadSpecificInstance();
83 typedef IDMap<blink::WebServiceWorkerProvider::WebServiceWorkerCallbacks,
84 IDMapOwnPointer> CallbackMap;
85 typedef std::map<int, blink::WebServiceWorkerProviderClient*> ScriptClientMap;
86 typedef std::map<int, ServiceWorkerProviderContext*> ProviderContextMap;
87 typedef std::map<int, WebServiceWorkerImpl*> WorkerObjectMap;
88 typedef std::map<int, ServiceWorkerProviderContext*> WorkerToProviderMap;
90 friend class WebServiceWorkerImpl;
92 // WorkerTaskRunner::Observer implementation.
93 virtual void OnWorkerRunLoopStopped() OVERRIDE;
95 void OnRegistered(int thread_id,
97 const ServiceWorkerObjectInfo& info);
98 void OnUnregistered(int thread_id,
100 void OnRegistrationError(int thread_id,
102 blink::WebServiceWorkerError::ErrorType error_type,
103 const base::string16& message);
104 void OnServiceWorkerStateChanged(int thread_id,
106 blink::WebServiceWorkerState state);
107 void OnSetCurrentServiceWorker(int thread_id,
109 const ServiceWorkerObjectInfo& info);
110 void OnPostMessage(int thread_id,
112 const base::string16& message,
113 const std::vector<int>& sent_message_port_ids,
114 const std::vector<int>& new_routing_ids);
116 // Keeps map from handle_id to ServiceWorker object.
117 void AddServiceWorker(int handle_id, WebServiceWorkerImpl* worker);
118 void RemoveServiceWorker(int handle_id);
120 CallbackMap pending_callbacks_;
121 ScriptClientMap script_clients_;
122 ProviderContextMap provider_contexts_;
123 WorkerObjectMap service_workers_;
125 // A map for ServiceWorkers that are associated to a particular document
126 // (e.g. as .current).
127 WorkerToProviderMap worker_to_provider_;
129 scoped_refptr<ThreadSafeSender> thread_safe_sender_;
131 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcher);
134 } // namespace content
136 #endif // CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_H_