Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / child / service_worker / service_worker_dispatcher.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_CHILD_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_H_
6 #define CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_H_
7
8 #include <map>
9 #include <vector>
10
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"
18
19 class GURL;
20
21 namespace blink {
22 class WebURL;
23 }
24
25 namespace IPC {
26 class Message;
27 }
28
29 namespace content {
30
31 class ServiceWorkerMessageFilter;
32 struct ServiceWorkerObjectInfo;
33 class ServiceWorkerProviderContext;
34 class ThreadSafeSender;
35 class WebServiceWorkerImpl;
36
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 {
41  public:
42   explicit ServiceWorkerDispatcher(ThreadSafeSender* thread_safe_sender);
43   virtual ~ServiceWorkerDispatcher();
44
45   void OnMessageReceived(const IPC::Message& msg);
46   bool Send(IPC::Message* msg);
47
48   // Corresponds to navigator.serviceWorker.register()
49   void RegisterServiceWorker(
50       int provider_id,
51       const GURL& pattern,
52       const GURL& script_url,
53       blink::WebServiceWorkerProvider::WebServiceWorkerCallbacks* callbacks);
54   // Corresponds to navigator.serviceWorker.unregister()
55   void UnregisterServiceWorker(
56       int provider_id,
57       const GURL& pattern,
58       blink::WebServiceWorkerProvider::WebServiceWorkerCallbacks* callbacks);
59
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);
66
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);
72
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);
77
78   // Unlike GetOrCreateThreadSpecificInstance() this doesn't create a new
79   // instance if thread-local instance doesn't exist.
80   static ServiceWorkerDispatcher* GetThreadSpecificInstance();
81
82  private:
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;
89
90   friend class WebServiceWorkerImpl;
91
92   // WorkerTaskRunner::Observer implementation.
93   virtual void OnWorkerRunLoopStopped() OVERRIDE;
94
95   void OnRegistered(int thread_id,
96                     int request_id,
97                     const ServiceWorkerObjectInfo& info);
98   void OnUnregistered(int thread_id,
99                       int request_id);
100   void OnRegistrationError(int thread_id,
101                            int request_id,
102                            blink::WebServiceWorkerError::ErrorType error_type,
103                            const base::string16& message);
104   void OnServiceWorkerStateChanged(int thread_id,
105                                    int handle_id,
106                                    blink::WebServiceWorkerState state);
107   void OnSetCurrentServiceWorker(int thread_id,
108                                  int provider_id,
109                                  const ServiceWorkerObjectInfo& info);
110   void OnPostMessage(int thread_id,
111                      int provider_id,
112                      const base::string16& message,
113                      const std::vector<int>& sent_message_port_ids,
114                      const std::vector<int>& new_routing_ids);
115
116   // Keeps map from handle_id to ServiceWorker object.
117   void AddServiceWorker(int handle_id, WebServiceWorkerImpl* worker);
118   void RemoveServiceWorker(int handle_id);
119
120   CallbackMap pending_callbacks_;
121   ScriptClientMap script_clients_;
122   ProviderContextMap provider_contexts_;
123   WorkerObjectMap service_workers_;
124
125   // A map for ServiceWorkers that are associated to a particular document
126   // (e.g. as .current).
127   WorkerToProviderMap worker_to_provider_;
128
129   scoped_refptr<ThreadSafeSender> thread_safe_sender_;
130
131   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcher);
132 };
133
134 }  // namespace content
135
136 #endif  // CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_H_