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_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_HOST_H_
      6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_HOST_H_
      7 
      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"
     13 
     14 class GURL;
     15 struct EmbeddedWorkerHostMsg_ReportConsoleMessage_Params;
     16 
     17 namespace content {
     18 
     19 class MessagePortMessageFilter;
     20 class ServiceWorkerContextCore;
     21 class ServiceWorkerContextWrapper;
     22 class ServiceWorkerHandle;
     23 class ServiceWorkerProviderHost;
     24 class ServiceWorkerRegistration;
     25 
     26 class CONTENT_EXPORT ServiceWorkerDispatcherHost : public BrowserMessageFilter {
     27  public:
     28   ServiceWorkerDispatcherHost(
     29       int render_process_id,
     30       MessagePortMessageFilter* message_port_message_filter);
     31 
     32   void Init(ServiceWorkerContextWrapper* context_wrapper);
     33 
     34   // BrowserMessageFilter implementation
     35   virtual void OnFilterAdded(IPC::Sender* sender) OVERRIDE;
     36   virtual void OnDestruct() const OVERRIDE;
     37   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     38 
     39   // IPC::Sender implementation
     40 
     41   // Send() queues the message until the underlying sender is ready.  This
     42   // class assumes that Send() can only fail after that when the renderer
     43   // process has terminated, at which point the whole instance will eventually
     44   // be destroyed.
     45   virtual bool Send(IPC::Message* message) OVERRIDE;
     46 
     47   void RegisterServiceWorkerHandle(scoped_ptr<ServiceWorkerHandle> handle);
     48 
     49   MessagePortMessageFilter* message_port_message_filter() {
     50     return message_port_message_filter_;
     51   }
     52 
     53  protected:
     54   virtual ~ServiceWorkerDispatcherHost();
     55 
     56  private:
     57   friend class BrowserThread;
     58   friend class base::DeleteHelper<ServiceWorkerDispatcherHost>;
     59   friend class TestingServiceWorkerDispatcherHost;
     60 
     61   // IPC Message handlers
     62   void OnRegisterServiceWorker(int thread_id,
     63                                int request_id,
     64                                int provider_id,
     65                                const GURL& pattern,
     66                                const GURL& script_url);
     67   void OnUnregisterServiceWorker(int thread_id,
     68                                  int request_id,
     69                                  int provider_id,
     70                                  const GURL& pattern);
     71   void OnProviderCreated(int provider_id);
     72   void OnProviderDestroyed(int provider_id);
     73   void OnSetHostedVersionId(int provider_id, int64 version_id);
     74   void OnWorkerScriptLoaded(int embedded_worker_id);
     75   void OnWorkerScriptLoadFailed(int embedded_worker_id);
     76   void OnWorkerStarted(int thread_id,
     77                        int embedded_worker_id);
     78   void OnWorkerStopped(int embedded_worker_id);
     79   void OnReportException(int embedded_worker_id,
     80                          const base::string16& error_message,
     81                          int line_number,
     82                          int column_number,
     83                          const GURL& source_url);
     84   void OnReportConsoleMessage(
     85       int embedded_worker_id,
     86       const EmbeddedWorkerHostMsg_ReportConsoleMessage_Params& params);
     87   void OnPostMessage(int handle_id,
     88                      const base::string16& message,
     89                      const std::vector<int>& sent_message_port_ids);
     90   void OnIncrementServiceWorkerRefCount(int handle_id);
     91   void OnDecrementServiceWorkerRefCount(int handle_id);
     92   void OnPostMessageToWorker(int handle_id,
     93                              const base::string16& message,
     94                              const std::vector<int>& sent_message_port_ids);
     95   void OnServiceWorkerObjectDestroyed(int handle_id);
     96 
     97   // Callbacks from ServiceWorkerContextCore
     98   void RegistrationComplete(int thread_id,
     99                             int request_id,
    100                             ServiceWorkerStatusCode status,
    101                             int64 registration_id,
    102                             int64 version_id);
    103 
    104   void UnregistrationComplete(int thread_id,
    105                               int request_id,
    106                               ServiceWorkerStatusCode status);
    107 
    108   void SendRegistrationError(int thread_id,
    109                              int request_id,
    110                              ServiceWorkerStatusCode status);
    111 
    112   ServiceWorkerContextCore* GetContext();
    113 
    114   int render_process_id_;
    115   MessagePortMessageFilter* const message_port_message_filter_;
    116   scoped_refptr<ServiceWorkerContextWrapper> context_wrapper_;
    117 
    118   IDMap<ServiceWorkerHandle, IDMapOwnPointer> handles_;
    119 
    120   bool channel_ready_;  // True after BrowserMessageFilter::sender_ != NULL.
    121   ScopedVector<IPC::Message> pending_messages_;
    122 
    123   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcherHost);
    124 };
    125 
    126 }  // namespace content
    127 
    128 #endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_HOST_H_
    129