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.
5 #ifndef CONTENT_BROWSER_WORKER_HOST_WORKER_SERVICE_H_
6 #define CONTENT_BROWSER_WORKER_HOST_WORKER_SERVICE_H_
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"
19 struct ViewHostMsg_CreateWorker_Params
;
22 class ResourceContext
;
23 class WorkerServiceObserver
;
24 class WorkerStoragePartition
;
25 class WorkerPrioritySetter
;
27 class CONTENT_EXPORT WorkerServiceImpl
28 : public NON_EXPORTED_BASE(WorkerService
) {
30 // Returns the WorkerServiceImpl singleton.
31 static WorkerServiceImpl
* GetInstance();
33 // Releases the priority setter to avoid memory leak error.
34 void PerformTeardownForTesting();
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
;
42 // These methods correspond to worker related IPCs.
43 void CreateWorker(const ViewHostMsg_CreateWorker_Params
& params
,
45 WorkerMessageFilter
* filter
,
46 ResourceContext
* resource_context
,
47 const WorkerStoragePartition
& worker_partition
);
48 void LookupSharedWorker(const ViewHostMsg_CreateWorker_Params
& params
,
50 WorkerMessageFilter
* filter
,
51 ResourceContext
* resource_context
,
52 const WorkerStoragePartition
& worker_partition
,
55 void ForwardToWorker(const IPC::Message
& message
,
56 WorkerMessageFilter
* filter
);
57 void DocumentDetached(unsigned long long document_id
,
58 WorkerMessageFilter
* filter
);
60 void OnWorkerMessageFilterClosing(WorkerMessageFilter
* filter
);
62 int next_worker_route_id() { return ++next_worker_route_id_
; }
64 // Given a worker's process id, return the IDs of the renderer process and
65 // render frame that created it. For shared workers, this returns the first
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_frame_id
) const;
72 const WorkerProcessHost::WorkerInstance
* FindWorkerInstance(
73 int worker_process_id
);
75 void NotifyWorkerDestroyed(
76 WorkerProcessHost
* process
,
79 void NotifyWorkerProcessCreated();
81 // Used when we run each worker in a separate process.
82 static const int kMaxWorkersWhenSeparate
;
83 static const int kMaxWorkersPerFrameWhenSeparate
;
86 friend struct DefaultSingletonTraits
<WorkerServiceImpl
>;
89 virtual ~WorkerServiceImpl();
91 // Given a WorkerInstance, create an associated worker process.
92 bool CreateWorkerFromInstance(WorkerProcessHost::WorkerInstance instance
);
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
);
99 // Checks if the frame associated with the passed RenderFrame 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 FrameCanCreateWorkerProcess(
103 int render_process_id
, int render_frame_id
, bool* hit_total_worker_limit
);
105 // Tries to see if any of the queued workers can be created.
106 void TryStartingQueuedWorker();
108 // APIs for manipulating our set of pending shared worker instances.
109 WorkerProcessHost::WorkerInstance
* CreatePendingInstance(
111 const base::string16
& name
,
112 ResourceContext
* resource_context
,
113 const WorkerStoragePartition
& worker_partition
);
114 WorkerProcessHost::WorkerInstance
* FindPendingInstance(
116 const base::string16
& name
,
117 const WorkerStoragePartition
& worker_partition
,
118 ResourceContext
* resource_context
);
119 void RemovePendingInstances(
121 const base::string16
& name
,
122 const WorkerStoragePartition
& worker_partition
,
123 ResourceContext
* resource_context
);
125 WorkerProcessHost::WorkerInstance
* FindSharedWorkerInstance(
127 const base::string16
& name
,
128 const WorkerStoragePartition
& worker_partition
,
129 ResourceContext
* resource_context
);
131 scoped_refptr
<WorkerPrioritySetter
> priority_setter_
;
133 int next_worker_route_id_
;
135 WorkerProcessHost::Instances queued_workers_
;
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_
;
142 ObserverList
<WorkerServiceObserver
> observers_
;
144 DISALLOW_COPY_AND_ASSIGN(WorkerServiceImpl
);
147 } // namespace content
149 #endif // CONTENT_BROWSER_WORKER_HOST_WORKER_SERVICE_H_