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_PROCESS_HOST_H_
6 #define CONTENT_BROWSER_WORKER_HOST_WORKER_PROCESS_HOST_H_
12 #include "base/basictypes.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "content/browser/worker_host/worker_document_set.h"
16 #include "content/browser/worker_host/worker_storage_partition.h"
17 #include "content/common/content_export.h"
18 #include "content/public/browser/browser_child_process_host_delegate.h"
19 #include "content/public/browser/browser_child_process_host_iterator.h"
20 #include "content/public/common/process_type.h"
21 #include "ipc/ipc_sender.h"
22 #include "third_party/WebKit/public/web/WebContentSecurityPolicy.h"
24 #include "webkit/common/resource_type.h"
26 struct ResourceHostMsg_Request
;
29 class FileSystemContext
;
30 } // namespace fileapi
33 class URLRequestContext
;
36 namespace webkit_database
{
37 class DatabaseTracker
;
38 } // namespace webkit_database
41 class BrowserChildProcessHostImpl
;
42 class IndexedDBContextImpl
;
43 class ResourceContext
;
44 class SocketStreamDispatcherHost
;
45 class WorkerServiceImpl
;
47 // The WorkerProcessHost is the interface that represents the browser side of
48 // the browser <-> worker communication channel. There will be one
49 // WorkerProcessHost per worker process. Currently each worker runs in its own
50 // process, but that may change. However, we do assume (by storing a
51 // net::URLRequestContext) that a WorkerProcessHost serves a single
53 class WorkerProcessHost
: public BrowserChildProcessHostDelegate
,
56 // Contains information about each worker instance, needed to forward messages
57 // between the renderer and worker processes.
58 class WorkerInstance
{
60 WorkerInstance(const GURL
& url
,
61 const base::string16
& name
,
62 const base::string16
& content_security_policy
,
63 blink::WebContentSecurityPolicyType security_policy_type
,
65 int parent_process_id
,
66 int64 main_resource_appcache_id
,
67 ResourceContext
* resource_context
,
68 const WorkerStoragePartition
& partition
);
69 // Used for pending instances. Rest of the parameters are ignored.
70 WorkerInstance(const GURL
& url
,
72 const base::string16
& name
,
73 ResourceContext
* resource_context
,
74 const WorkerStoragePartition
& partition
);
77 // Unique identifier for a worker client.
78 typedef std::pair
<WorkerMessageFilter
*, int> FilterInfo
;
80 // APIs to manage the filter list for a given instance.
81 void AddFilter(WorkerMessageFilter
* filter
, int route_id
);
82 void RemoveFilter(WorkerMessageFilter
* filter
, int route_id
);
83 void RemoveFilters(WorkerMessageFilter
* filter
);
84 bool HasFilter(WorkerMessageFilter
* filter
, int route_id
) const;
85 bool FrameIsParent(int render_process_id
, int render_frame_id
) const;
86 int NumFilters() const { return filters_
.size(); }
87 // Returns the single filter (must only be one).
88 FilterInfo
GetFilter() const;
90 typedef std::list
<FilterInfo
> FilterList
;
91 const FilterList
& filters() const { return filters_
; }
93 // Checks if this WorkerInstance matches the passed url/name params
94 // (per the comparison algorithm in the WebWorkers spec). This API only
95 // applies to shared workers.
98 const base::string16
& name
,
99 const WorkerStoragePartition
& partition
,
100 ResourceContext
* resource_context
) const;
102 // Shares the passed instance's WorkerDocumentSet with this instance. This
103 // instance's current WorkerDocumentSet is dereferenced (and freed if this
104 // is the only reference) as a result.
105 void ShareDocumentSet(const WorkerInstance
& instance
) {
106 worker_document_set_
= instance
.worker_document_set_
;
110 bool closed() const { return closed_
; }
111 void set_closed(bool closed
) { closed_
= closed
; }
112 const GURL
& url() const { return url_
; }
113 const base::string16
name() const { return name_
; }
114 const base::string16
content_security_policy() const {
115 return content_security_policy_
;
117 blink::WebContentSecurityPolicyType
security_policy_type() const {
118 return security_policy_type_
;
120 int worker_route_id() const { return worker_route_id_
; }
121 int parent_process_id() const { return parent_process_id_
; }
122 int64
main_resource_appcache_id() const {
123 return main_resource_appcache_id_
;
125 WorkerDocumentSet
* worker_document_set() const {
126 return worker_document_set_
.get();
128 ResourceContext
* resource_context() const {
129 return resource_context_
;
131 const WorkerStoragePartition
& partition() const {
136 // Set of all filters (clients) associated with this worker.
139 base::string16 name_
;
140 base::string16 content_security_policy_
;
141 blink::WebContentSecurityPolicyType security_policy_type_
;
142 int worker_route_id_
;
143 int parent_process_id_
;
144 int64 main_resource_appcache_id_
;
146 scoped_refptr
<WorkerDocumentSet
> worker_document_set_
;
147 ResourceContext
* const resource_context_
;
148 WorkerStoragePartition partition_
;
151 WorkerProcessHost(ResourceContext
* resource_context
,
152 const WorkerStoragePartition
& partition
);
153 virtual ~WorkerProcessHost();
155 // IPC::Sender implementation:
156 virtual bool Send(IPC::Message
* message
) OVERRIDE
;
158 // Starts the process. Returns true iff it succeeded.
159 // |render_process_id| is the renderer process responsible for starting this
161 bool Init(int render_process_id
);
163 // Creates a worker object in the process.
164 void CreateWorker(const WorkerInstance
& instance
);
166 // Returns true iff the given message from a renderer process was forwarded to
168 bool FilterMessage(const IPC::Message
& message
, WorkerMessageFilter
* filter
);
170 void FilterShutdown(WorkerMessageFilter
* filter
);
172 // Shuts down any shared workers that are no longer referenced by active
174 void DocumentDetached(WorkerMessageFilter
* filter
,
175 unsigned long long document_id
);
177 // Terminates the given worker, i.e. based on a UI action.
178 CONTENT_EXPORT
void TerminateWorker(int worker_route_id
);
180 // Callers can reduce the WorkerProcess' priority.
181 void SetBackgrounded(bool backgrounded
);
183 CONTENT_EXPORT
const ChildProcessData
& GetData();
185 typedef std::list
<WorkerInstance
> Instances
;
186 const Instances
& instances() const { return instances_
; }
188 ResourceContext
* resource_context() const {
189 return resource_context_
;
192 bool process_launched() const { return process_launched_
; }
195 friend class WorkerServiceImpl
;
197 Instances
& mutable_instances() { return instances_
; }
200 // BrowserChildProcessHostDelegate implementation:
201 virtual void OnProcessLaunched() OVERRIDE
;
202 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
204 // Creates and adds the message filters.
205 void CreateMessageFilters(int render_process_id
);
207 void OnWorkerContextClosed(int worker_route_id
);
208 void OnAllowDatabase(int worker_route_id
,
210 const base::string16
& name
,
211 const base::string16
& display_name
,
212 unsigned long estimated_size
,
214 void OnAllowFileSystem(int worker_route_id
,
217 void OnAllowIndexedDB(int worker_route_id
,
219 const base::string16
& name
,
221 void OnForceKillWorkerProcess();
223 // Relays a message to the given endpoint. Takes care of parsing the message
224 // if it contains a message port and sending it a valid route id.
225 void RelayMessage(const IPC::Message
& message
,
226 WorkerMessageFilter
* filter
,
229 void ShutdownSocketStreamDispatcherHostIfNecessary();
231 virtual bool CanShutdown() OVERRIDE
;
233 // Updates the title shown in the task manager.
236 // Return a vector of all the render process/render frame IDs that use the
238 std::vector
<std::pair
<int, int> > GetRenderFrameIDsForWorker(int route_id
);
240 // Callbacks for ResourceMessageFilter and SocketStreamDispatcherHost.
241 void GetContexts(const ResourceHostMsg_Request
& request
,
242 ResourceContext
** resource_context
,
243 net::URLRequestContext
** request_context
);
244 net::URLRequestContext
* GetRequestContext(ResourceType::Type resource_type
);
246 Instances instances_
;
248 ResourceContext
* const resource_context_
;
249 WorkerStoragePartition partition_
;
251 // A reference to the filter associated with this worker process. We need to
252 // keep this around since we'll use it when forward messages to the worker
254 scoped_refptr
<WorkerMessageFilter
> worker_message_filter_
;
256 scoped_ptr
<BrowserChildProcessHostImpl
> process_
;
257 bool process_launched_
;
259 scoped_refptr
<SocketStreamDispatcherHost
> socket_stream_dispatcher_host_
;
261 DISALLOW_COPY_AND_ASSIGN(WorkerProcessHost
);
264 class WorkerProcessHostIterator
265 : public BrowserChildProcessHostTypeIterator
<WorkerProcessHost
> {
267 WorkerProcessHostIterator()
268 : BrowserChildProcessHostTypeIterator
<WorkerProcessHost
>(
269 PROCESS_TYPE_WORKER
) {
273 } // namespace content
275 #endif // CONTENT_BROWSER_WORKER_HOST_WORKER_PROCESS_HOST_H_