Home | History | Annotate | Download | only in worker_host
      1 // Copyright (c) 2012 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_WORKER_HOST_WORKER_SERVICE_H_
      6 #define CONTENT_BROWSER_WORKER_HOST_WORKER_SERVICE_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/compiler_specific.h"
     10 #include "base/memory/singleton.h"
     11 #include "base/observer_list.h"
     12 #include "base/threading/non_thread_safe.h"
     13 #include "content/browser/worker_host/worker_process_host.h"
     14 #include "content/public/browser/notification_observer.h"
     15 #include "content/public/browser/notification_registrar.h"
     16 #include "content/public/browser/worker_service.h"
     17 
     18 class GURL;
     19 struct ViewHostMsg_CreateWorker_Params;
     20 
     21 namespace content {
     22 class ResourceContext;
     23 class WorkerServiceObserver;
     24 class WorkerStoragePartition;
     25 class WorkerPrioritySetter;
     26 
     27 class CONTENT_EXPORT WorkerServiceImpl
     28     : public NON_EXPORTED_BASE(WorkerService) {
     29  public:
     30   // Returns the WorkerServiceImpl singleton.
     31   static WorkerServiceImpl* GetInstance();
     32 
     33   // Releases the priority setter to avoid memory leak error.
     34   void PerformTeardownForTesting();
     35 
     36   // WorkerService implementation:
     37   virtual bool TerminateWorker(int process_id, int route_id) OVERRIDE;
     38   virtual std::vector<WorkerInfo> GetWorkers() OVERRIDE;
     39   virtual void AddObserver(WorkerServiceObserver* observer) OVERRIDE;
     40   virtual void RemoveObserver(WorkerServiceObserver* observer) OVERRIDE;
     41 
     42   // These methods correspond to worker related IPCs.
     43   void CreateWorker(const ViewHostMsg_CreateWorker_Params& params,
     44                     int route_id,
     45                     WorkerMessageFilter* filter,
     46                     ResourceContext* resource_context,
     47                     const WorkerStoragePartition& worker_partition);
     48   void LookupSharedWorker(const ViewHostMsg_CreateWorker_Params& params,
     49                           int route_id,
     50                           WorkerMessageFilter* filter,
     51                           ResourceContext* resource_context,
     52                           const WorkerStoragePartition& worker_partition,
     53                           bool* exists,
     54                           bool* url_error);
     55   void ForwardToWorker(const IPC::Message& message,
     56                        WorkerMessageFilter* filter);
     57   void DocumentDetached(unsigned long long document_id,
     58                         WorkerMessageFilter* filter);
     59 
     60   void OnWorkerMessageFilterClosing(WorkerMessageFilter* filter);
     61 
     62   int next_worker_route_id() { return ++next_worker_route_id_; }
     63 
     64   // Given a worker's process id, return the IDs of the renderer process and
     65   // render view that created it.  For shared workers, this returns the first
     66   // parent.
     67   // TODO(dimich): This code assumes there is 1 worker per worker process, which
     68   // is how it is today until V8 can run in separate threads.
     69   bool GetRendererForWorker(int worker_process_id,
     70                             int* render_process_id,
     71                             int* render_view_id) const;
     72   const WorkerProcessHost::WorkerInstance* FindWorkerInstance(
     73       int worker_process_id);
     74 
     75   void NotifyWorkerDestroyed(
     76       WorkerProcessHost* process,
     77       int worker_route_id);
     78 
     79   void NotifyWorkerProcessCreated();
     80 
     81   // Used when we run each worker in a separate process.
     82   static const int kMaxWorkersWhenSeparate;
     83   static const int kMaxWorkersPerTabWhenSeparate;
     84 
     85  private:
     86   friend struct DefaultSingletonTraits<WorkerServiceImpl>;
     87 
     88   WorkerServiceImpl();
     89   virtual ~WorkerServiceImpl();
     90 
     91   // Given a WorkerInstance, create an associated worker process.
     92   bool CreateWorkerFromInstance(WorkerProcessHost::WorkerInstance instance);
     93 
     94   // Checks if we can create a worker process based on the process limit when
     95   // we're using a strategy of one process per core.
     96   bool CanCreateWorkerProcess(
     97       const WorkerProcessHost::WorkerInstance& instance);
     98 
     99   // Checks if the tab associated with the passed RenderView can create a
    100   // worker process based on the process limit when we're using a strategy of
    101   // one worker per process.
    102   bool TabCanCreateWorkerProcess(
    103       int render_process_id, int render_route_id, bool* hit_total_worker_limit);
    104 
    105   // Tries to see if any of the queued workers can be created.
    106   void TryStartingQueuedWorker();
    107 
    108   // APIs for manipulating our set of pending shared worker instances.
    109   WorkerProcessHost::WorkerInstance* CreatePendingInstance(
    110       const GURL& url,
    111       const string16& name,
    112       ResourceContext* resource_context,
    113       const WorkerStoragePartition& worker_partition);
    114   WorkerProcessHost::WorkerInstance* FindPendingInstance(
    115       const GURL& url,
    116       const string16& name,
    117       const WorkerStoragePartition& worker_partition,
    118       ResourceContext* resource_context);
    119   void RemovePendingInstances(
    120       const GURL& url,
    121       const string16& name,
    122       const WorkerStoragePartition& worker_partition,
    123       ResourceContext* resource_context);
    124 
    125   WorkerProcessHost::WorkerInstance* FindSharedWorkerInstance(
    126       const GURL& url,
    127       const string16& name,
    128       const WorkerStoragePartition& worker_partition,
    129       ResourceContext* resource_context);
    130 
    131   scoped_refptr<WorkerPrioritySetter> priority_setter_;
    132 
    133   int next_worker_route_id_;
    134 
    135   WorkerProcessHost::Instances queued_workers_;
    136 
    137   // These are shared workers that have been looked up, but not created yet.
    138   // We need to keep a list of these to synchronously detect shared worker
    139   // URL mismatches when two pages launch shared workers simultaneously.
    140   WorkerProcessHost::Instances pending_shared_workers_;
    141 
    142   ObserverList<WorkerServiceObserver> observers_;
    143 
    144   DISALLOW_COPY_AND_ASSIGN(WorkerServiceImpl);
    145 };
    146 
    147 }  // namespace content
    148 
    149 #endif  // CONTENT_BROWSER_WORKER_HOST_WORKER_SERVICE_H_
    150