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_RENDERER_SERVICE_WORKER_EMBEDDED_WORKER_CONTEXT_CLIENT_H_
      6 #define CONTENT_RENDERER_SERVICE_WORKER_EMBEDDED_WORKER_CONTEXT_CLIENT_H_
      7 
      8 #include "base/memory/ref_counted.h"
      9 #include "base/memory/weak_ptr.h"
     10 #include "base/strings/string16.h"
     11 #include "content/common/service_worker/service_worker_types.h"
     12 #include "ipc/ipc_listener.h"
     13 #include "third_party/WebKit/public/platform/WebServiceWorkerClientsInfo.h"
     14 #include "third_party/WebKit/public/platform/WebServiceWorkerEventResult.h"
     15 #include "third_party/WebKit/public/platform/WebURL.h"
     16 #include "third_party/WebKit/public/web/WebServiceWorkerContextClient.h"
     17 #include "url/gurl.h"
     18 
     19 namespace base {
     20 class MessageLoopProxy;
     21 class TaskRunner;
     22 }
     23 
     24 namespace blink {
     25 class WebDataSource;
     26 }
     27 
     28 namespace content {
     29 
     30 class ServiceWorkerScriptContext;
     31 class ThreadSafeSender;
     32 
     33 // This class provides access to/from an embedded worker's WorkerGlobalScope.
     34 // All methods other than the constructor (it's created on the main thread)
     35 // and createServiceWorkerNetworkProvider (also called on the main thread)
     36 // are called on the worker thread.
     37 //
     38 // TODO(kinuko): Currently EW/SW separation is made a little hazily.
     39 // This should implement WebEmbeddedWorkerContextClient
     40 // or sort of it (which doesn't exist yet) rather than
     41 // WebServiceWorkerContextClient if we want to separate them more cleanly,
     42 // or ServiceWorkerScriptContext should be merged into this class
     43 // if we consider EW == SW script context.
     44 class EmbeddedWorkerContextClient
     45     : public blink::WebServiceWorkerContextClient {
     46  public:
     47   // Returns a thread-specific client instance.  This does NOT create a
     48   // new instance.
     49   static EmbeddedWorkerContextClient* ThreadSpecificInstance();
     50 
     51   EmbeddedWorkerContextClient(int embedded_worker_id,
     52                               int64 service_worker_version_id,
     53                               const GURL& service_worker_scope,
     54                               const GURL& script_url,
     55                               int worker_devtools_agent_route_id);
     56 
     57   virtual ~EmbeddedWorkerContextClient();
     58 
     59   bool OnMessageReceived(const IPC::Message& msg);
     60 
     61   void Send(IPC::Message* message);
     62 
     63   // WebServiceWorkerContextClient overrides, some of them are just dispatched
     64   // on to script_context_.
     65   virtual blink::WebURL scope() const;
     66   virtual blink::WebServiceWorkerCacheStorage* cacheStorage();
     67   virtual void didPauseAfterDownload();
     68   virtual void getClients(blink::WebServiceWorkerClientsCallbacks*);
     69   virtual void workerReadyForInspection();
     70   virtual void workerContextFailedToStart();
     71   virtual void workerContextStarted(blink::WebServiceWorkerContextProxy* proxy);
     72   virtual void willDestroyWorkerContext();
     73   virtual void workerContextDestroyed();
     74   virtual void reportException(const blink::WebString& error_message,
     75                                int line_number,
     76                                int column_number,
     77                                const blink::WebString& source_url);
     78   virtual void reportConsoleMessage(int source,
     79                                     int level,
     80                                     const blink::WebString& message,
     81                                     int line_number,
     82                                     const blink::WebString& source_url);
     83   virtual void dispatchDevToolsMessage(const blink::WebString&);
     84   virtual void saveDevToolsAgentState(const blink::WebString&);
     85   virtual void didHandleActivateEvent(int request_id,
     86                                       blink::WebServiceWorkerEventResult);
     87   virtual void didHandleInstallEvent(int request_id,
     88                                      blink::WebServiceWorkerEventResult result);
     89   virtual void didHandleFetchEvent(int request_id);
     90   virtual void didHandleFetchEvent(
     91       int request_id,
     92       const blink::WebServiceWorkerResponse& response);
     93   virtual void didHandleSyncEvent(int request_id);
     94   virtual blink::WebServiceWorkerNetworkProvider*
     95       createServiceWorkerNetworkProvider(blink::WebDataSource* data_source);
     96   virtual void postMessageToClient(
     97       int client_id,
     98       const blink::WebString& message,
     99       blink::WebMessagePortChannelArray* channels);
    100 
    101   // TODO: Implement DevTools related method overrides.
    102 
    103   int embedded_worker_id() const { return embedded_worker_id_; }
    104   base::MessageLoopProxy* main_thread_proxy() const {
    105     return main_thread_proxy_.get();
    106   }
    107   ThreadSafeSender* thread_safe_sender() { return sender_.get(); }
    108 
    109  private:
    110   void OnMessageToWorker(int thread_id,
    111                          int embedded_worker_id,
    112                          const IPC::Message& message);
    113   void SendWorkerStarted();
    114 
    115   const int embedded_worker_id_;
    116   const int64 service_worker_version_id_;
    117   const GURL service_worker_scope_;
    118   const GURL script_url_;
    119   const int worker_devtools_agent_route_id_;
    120   scoped_refptr<ThreadSafeSender> sender_;
    121   scoped_refptr<base::MessageLoopProxy> main_thread_proxy_;
    122   scoped_refptr<base::TaskRunner> worker_task_runner_;
    123 
    124   scoped_ptr<ServiceWorkerScriptContext> script_context_;
    125 
    126   base::WeakPtrFactory<EmbeddedWorkerContextClient> weak_factory_;
    127 
    128   DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerContextClient);
    129 };
    130 
    131 }  // namespace content
    132 
    133 #endif  // CONTENT_RENDERER_SERVICE_WORKER_EMBEDDED_WORKER_CONTEXT_CLIENT_H_
    134