Home | History | Annotate | Download | only in shared_worker
      1 // Copyright 2014 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_SHARED_WORKER_SHARED_WORKER_HOST_H_
      6 #define CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_HOST_H_
      7 
      8 #include <list>
      9 #include <vector>
     10 
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/memory/weak_ptr.h"
     13 #include "base/strings/string16.h"
     14 #include "base/time/time.h"
     15 #include "content/browser/shared_worker/shared_worker_message_filter.h"
     16 #include "content/browser/shared_worker/worker_document_set.h"
     17 
     18 class GURL;
     19 
     20 namespace IPC {
     21 class Message;
     22 }
     23 
     24 namespace content {
     25 class SharedWorkerMessageFilter;
     26 class SharedWorkerInstance;
     27 
     28 // The SharedWorkerHost is the interface that represents the browser side of
     29 // the browser <-> worker communication channel.
     30 class SharedWorkerHost {
     31  public:
     32   SharedWorkerHost(SharedWorkerInstance* instance,
     33                    SharedWorkerMessageFilter* filter,
     34                    int worker_route_id);
     35   ~SharedWorkerHost();
     36 
     37   // Sends |message| to the SharedWorker.
     38   bool Send(IPC::Message* message);
     39 
     40   // Starts the SharedWorker in the renderer process which is associated with
     41   // |filter_|.
     42   void Start(bool pause_on_start);
     43 
     44   // Returns true iff the given message from a renderer process was forwarded to
     45   // the worker.
     46   bool FilterMessage(const IPC::Message& message,
     47                      SharedWorkerMessageFilter* filter);
     48 
     49   // Handles the shutdown of the filter. If the worker has no other client,
     50   // sends TerminateWorkerContext message to shut it down.
     51   void FilterShutdown(SharedWorkerMessageFilter* filter);
     52 
     53   // Shuts down any shared workers that are no longer referenced by active
     54   // documents.
     55   void DocumentDetached(SharedWorkerMessageFilter* filter,
     56                         unsigned long long document_id);
     57 
     58   void WorkerContextClosed();
     59   void WorkerReadyForInspection();
     60   void WorkerScriptLoaded();
     61   void WorkerScriptLoadFailed();
     62   void WorkerConnected(int message_port_id);
     63   void WorkerContextDestroyed();
     64   void AllowDatabase(const GURL& url,
     65                      const base::string16& name,
     66                      const base::string16& display_name,
     67                      unsigned long estimated_size,
     68                      bool* result);
     69   void AllowFileSystem(const GURL& url, scoped_ptr<IPC::Message> reply_msg);
     70   void AllowIndexedDB(const GURL& url,
     71                       const base::string16& name,
     72                       bool* result);
     73 
     74   // Terminates the given worker, i.e. based on a UI action.
     75   void TerminateWorker();
     76 
     77   void AddFilter(SharedWorkerMessageFilter* filter, int route_id);
     78 
     79   SharedWorkerInstance* instance() { return instance_.get(); }
     80   WorkerDocumentSet* worker_document_set() const {
     81     return worker_document_set_.get();
     82   }
     83   SharedWorkerMessageFilter* container_render_filter() const {
     84     return container_render_filter_;
     85   }
     86   int process_id() const { return worker_process_id_; }
     87   int worker_route_id() const { return worker_route_id_; }
     88   bool load_failed() const { return load_failed_; }
     89   bool closed() const { return closed_; }
     90 
     91  private:
     92   // Unique identifier for a worker client.
     93   class FilterInfo {
     94    public:
     95     FilterInfo(SharedWorkerMessageFilter* filter, int route_id)
     96         : filter_(filter), route_id_(route_id), message_port_id_(0) {}
     97     SharedWorkerMessageFilter* filter() const { return filter_; }
     98     int route_id() const { return route_id_; }
     99     int message_port_id() const { return message_port_id_; }
    100     void set_message_port_id(int id) { message_port_id_ = id; }
    101 
    102    private:
    103     SharedWorkerMessageFilter* filter_;
    104     int route_id_;
    105     int message_port_id_;
    106   };
    107 
    108   typedef std::list<FilterInfo> FilterList;
    109 
    110   // Relays |message| to the SharedWorker. Takes care of parsing the message if
    111   // it contains a message port and sending it a valid route id.
    112   void RelayMessage(const IPC::Message& message,
    113                     SharedWorkerMessageFilter* incoming_filter);
    114 
    115   // Return a vector of all the render process/render frame IDs.
    116   std::vector<std::pair<int, int> > GetRenderFrameIDsForWorker();
    117 
    118   void RemoveFilters(SharedWorkerMessageFilter* filter);
    119   bool HasFilter(SharedWorkerMessageFilter* filter, int route_id) const;
    120   void SetMessagePortID(SharedWorkerMessageFilter* filter,
    121                         int route_id,
    122                         int message_port_id);
    123   void AllowFileSystemResponse(scoped_ptr<IPC::Message> reply_msg,
    124                                bool allowed);
    125   scoped_ptr<SharedWorkerInstance> instance_;
    126   scoped_refptr<WorkerDocumentSet> worker_document_set_;
    127   FilterList filters_;
    128   SharedWorkerMessageFilter* container_render_filter_;
    129   int worker_process_id_;
    130   int worker_route_id_;
    131   bool load_failed_;
    132   bool closed_;
    133   const base::TimeTicks creation_time_;
    134 
    135   base::WeakPtrFactory<SharedWorkerHost> weak_factory_;
    136 
    137   DISALLOW_COPY_AND_ASSIGN(SharedWorkerHost);
    138 };
    139 }  // namespace content
    140 
    141 #endif  // CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_HOST_H_
    142