Home | History | Annotate | Download | only in service_worker
      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   // If an existing WebServiceWorkerImpl exists for the Service
     74   // Worker, it is returned; otherwise a WebServiceWorkerImpl is
     75   // created and its ownership is transferred to the caller. If
     76   // |adopt_handle| is true, a ServiceWorkerHandleReference will be
     77   // adopted for the specified Service Worker.
     78   //
     79   // TODO(dominicc): The lifetime of WebServiceWorkerImpl is too tricky; this
     80   // method can return an existing WebServiceWorkerImpl, in which case
     81   // it is owned by a WebCore::ServiceWorker and the lifetime is not
     82   // being transferred to the owner; or it can create a
     83   // WebServiceWorkerImpl, in which case ownership is transferred to
     84   // the caller who must bounce it to a method that will associate it
     85   // with a WebCore::ServiceWorker.
     86   WebServiceWorkerImpl* GetServiceWorker(const ServiceWorkerObjectInfo&,
     87                                          bool adopt_handle);
     88 
     89   // |thread_safe_sender| needs to be passed in because if the call leads to
     90   // construction it will be needed.
     91   static ServiceWorkerDispatcher* GetOrCreateThreadSpecificInstance(
     92       ThreadSafeSender* thread_safe_sender);
     93 
     94   // Unlike GetOrCreateThreadSpecificInstance() this doesn't create a new
     95   // instance if thread-local instance doesn't exist.
     96   static ServiceWorkerDispatcher* GetThreadSpecificInstance();
     97 
     98  private:
     99   typedef IDMap<blink::WebServiceWorkerProvider::WebServiceWorkerCallbacks,
    100       IDMapOwnPointer> CallbackMap;
    101   typedef std::map<int, blink::WebServiceWorkerProviderClient*> ScriptClientMap;
    102   typedef std::map<int, ServiceWorkerProviderContext*> ProviderContextMap;
    103   typedef std::map<int, WebServiceWorkerImpl*> WorkerObjectMap;
    104   typedef std::map<int, ServiceWorkerProviderContext*> WorkerToProviderMap;
    105 
    106   friend class WebServiceWorkerImpl;
    107 
    108   // WorkerTaskRunner::Observer implementation.
    109   virtual void OnWorkerRunLoopStopped() OVERRIDE;
    110 
    111   void OnRegistered(int thread_id,
    112                     int request_id,
    113                     const ServiceWorkerObjectInfo& info);
    114   void OnUnregistered(int thread_id,
    115                       int request_id);
    116   void OnRegistrationError(int thread_id,
    117                            int request_id,
    118                            blink::WebServiceWorkerError::ErrorType error_type,
    119                            const base::string16& message);
    120   void OnServiceWorkerStateChanged(int thread_id,
    121                                    int handle_id,
    122                                    blink::WebServiceWorkerState state);
    123   void OnSetWaitingServiceWorker(int thread_id,
    124                                  int provider_id,
    125                                  const ServiceWorkerObjectInfo& info);
    126   void OnSetCurrentServiceWorker(int thread_id,
    127                                  int provider_id,
    128                                  const ServiceWorkerObjectInfo& info);
    129   void OnPostMessage(int thread_id,
    130                      int provider_id,
    131                      const base::string16& message,
    132                      const std::vector<int>& sent_message_port_ids,
    133                      const std::vector<int>& new_routing_ids);
    134 
    135   // Keeps map from handle_id to ServiceWorker object.
    136   void AddServiceWorker(int handle_id, WebServiceWorkerImpl* worker);
    137   void RemoveServiceWorker(int handle_id);
    138 
    139   CallbackMap pending_callbacks_;
    140   ScriptClientMap script_clients_;
    141   ProviderContextMap provider_contexts_;
    142   WorkerObjectMap service_workers_;
    143 
    144   // A map for ServiceWorkers that are associated to a particular document
    145   // (e.g. as .current).
    146   WorkerToProviderMap worker_to_provider_;
    147 
    148   scoped_refptr<ThreadSafeSender> thread_safe_sender_;
    149 
    150   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcher);
    151 };
    152 
    153 }  // namespace content
    154 
    155 #endif  // CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_H_
    156