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 "base/memory/weak_ptr.h"
16 #include "content/browser/worker_host/worker_document_set.h"
17 #include "content/browser/worker_host/worker_storage_partition.h"
18 #include "content/common/content_export.h"
19 #include "content/public/browser/browser_child_process_host_delegate.h"
20 #include "content/public/browser/browser_child_process_host_iterator.h"
21 #include "content/public/common/process_type.h"
22 #include "content/public/common/resource_type.h"
23 #include "ipc/ipc_sender.h"
24 #include "third_party/WebKit/public/web/WebContentSecurityPolicy.h"
27 struct ResourceHostMsg_Request
;
30 class FileSystemContext
;
31 } // namespace fileapi
34 class URLRequestContext
;
37 namespace webkit_database
{
38 class DatabaseTracker
;
39 } // namespace webkit_database
42 class BrowserChildProcessHostImpl
;
43 class IndexedDBContextImpl
;
44 class ResourceContext
;
45 class SocketStreamDispatcherHost
;
46 class WorkerMessageFilter
;
47 class WorkerServiceImpl
;
49 // The WorkerProcessHost is the interface that represents the browser side of
50 // the browser <-> worker communication channel. There will be one
51 // WorkerProcessHost per worker process. Currently each worker runs in its own
52 // process, but that may change. However, we do assume (by storing a
53 // net::URLRequestContext) that a WorkerProcessHost serves a single
55 class WorkerProcessHost
: public BrowserChildProcessHostDelegate
,
58 // Contains information about each worker instance, needed to forward messages
59 // between the renderer and worker processes.
60 class WorkerInstance
{
62 WorkerInstance(const GURL
& url
,
63 const base::string16
& name
,
64 const base::string16
& content_security_policy
,
65 blink::WebContentSecurityPolicyType security_policy_type
,
68 ResourceContext
* resource_context
,
69 const WorkerStoragePartition
& partition
);
72 // Unique identifier for a worker client.
75 FilterInfo(WorkerMessageFilter
* filter
, int route_id
)
76 : filter_(filter
), route_id_(route_id
), message_port_id_(0) { }
77 WorkerMessageFilter
* filter() const { return filter_
; }
78 int route_id() const { return route_id_
; }
79 int message_port_id() const { return message_port_id_
; }
80 void set_message_port_id(int id
) { message_port_id_
= id
; }
83 WorkerMessageFilter
* filter_
;
88 // APIs to manage the filter list for a given instance.
89 void AddFilter(WorkerMessageFilter
* filter
, int route_id
);
90 void RemoveFilter(WorkerMessageFilter
* filter
, int route_id
);
91 void RemoveFilters(WorkerMessageFilter
* filter
);
92 bool HasFilter(WorkerMessageFilter
* filter
, int route_id
) const;
93 bool FrameIsParent(int render_process_id
, int render_frame_id
) const;
94 int NumFilters() const { return filters_
.size(); }
95 void SetMessagePortID(WorkerMessageFilter
* filter
,
98 // Returns the single filter (must only be one).
99 FilterInfo
GetFilter() const;
101 typedef std::list
<FilterInfo
> FilterList
;
102 const FilterList
& filters() const { return filters_
; }
104 // Checks if this WorkerInstance matches the passed url/name params
105 // (per the comparison algorithm in the WebWorkers spec). This API only
106 // applies to shared workers.
109 const base::string16
& name
,
110 const WorkerStoragePartition
& partition
,
111 ResourceContext
* resource_context
) const;
113 // Shares the passed instance's WorkerDocumentSet with this instance. This
114 // instance's current WorkerDocumentSet is dereferenced (and freed if this
115 // is the only reference) as a result.
116 void ShareDocumentSet(const WorkerInstance
& instance
) {
117 worker_document_set_
= instance
.worker_document_set_
;
121 bool closed() const { return closed_
; }
122 void set_closed(bool closed
) { closed_
= closed
; }
123 const GURL
& url() const { return url_
; }
124 const base::string16
name() const { return name_
; }
125 const base::string16
content_security_policy() const {
126 return content_security_policy_
;
128 blink::WebContentSecurityPolicyType
security_policy_type() const {
129 return security_policy_type_
;
131 int worker_route_id() const { return worker_route_id_
; }
132 int render_frame_id() const { return render_frame_id_
; }
133 WorkerDocumentSet
* worker_document_set() const {
134 return worker_document_set_
.get();
136 ResourceContext
* resource_context() const {
137 return resource_context_
;
139 const WorkerStoragePartition
& partition() const {
142 void set_load_failed(bool failed
) { load_failed_
= failed
; }
143 bool load_failed() { return load_failed_
; }
146 // Set of all filters (clients) associated with this worker.
149 base::string16 name_
;
150 base::string16 content_security_policy_
;
151 blink::WebContentSecurityPolicyType security_policy_type_
;
152 int worker_route_id_
;
153 int render_frame_id_
;
155 scoped_refptr
<WorkerDocumentSet
> worker_document_set_
;
156 ResourceContext
* const resource_context_
;
157 WorkerStoragePartition partition_
;
161 WorkerProcessHost(ResourceContext
* resource_context
,
162 const WorkerStoragePartition
& partition
);
163 virtual ~WorkerProcessHost();
165 // IPC::Sender implementation:
166 virtual bool Send(IPC::Message
* message
) OVERRIDE
;
168 // Starts the process. Returns true iff it succeeded.
169 // |render_process_id| and |render_frame_id| are the renderer process and the
170 // renderer frame responsible for starting this worker.
171 bool Init(int render_process_id
, int render_frame_id
);
173 // Creates a worker object in the process.
174 void CreateWorker(const WorkerInstance
& instance
, bool pause_on_start
);
176 // Returns true iff the given message from a renderer process was forwarded to
178 bool FilterMessage(const IPC::Message
& message
, WorkerMessageFilter
* filter
);
180 void FilterShutdown(WorkerMessageFilter
* filter
);
182 // Shuts down any shared workers that are no longer referenced by active
184 void DocumentDetached(WorkerMessageFilter
* filter
,
185 unsigned long long document_id
);
187 // Terminates the given worker, i.e. based on a UI action.
188 CONTENT_EXPORT
void TerminateWorker(int worker_route_id
);
190 // Callers can reduce the WorkerProcess' priority.
191 void SetBackgrounded(bool backgrounded
);
193 CONTENT_EXPORT
const ChildProcessData
& GetData();
195 typedef std::list
<WorkerInstance
> Instances
;
196 const Instances
& instances() const { return instances_
; }
198 ResourceContext
* resource_context() const {
199 return resource_context_
;
202 bool process_launched() const { return process_launched_
; }
205 friend class WorkerServiceImpl
;
207 Instances
& mutable_instances() { return instances_
; }
210 // BrowserChildProcessHostDelegate implementation:
211 virtual void OnProcessLaunched() OVERRIDE
;
212 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
214 // Creates and adds the message filters.
215 void CreateMessageFilters(int render_process_id
);
217 void OnWorkerContextClosed(int worker_route_id
);
218 void OnWorkerContextDestroyed(int worker_route_id
);
219 void OnWorkerScriptLoaded(int worker_route_id
);
220 void OnWorkerScriptLoadFailed(int worker_route_id
);
221 void OnWorkerConnected(int message_port_id
, int worker_route_id
);
222 void OnAllowDatabase(int worker_route_id
,
224 const base::string16
& name
,
225 const base::string16
& display_name
,
226 unsigned long estimated_size
,
228 void OnRequestFileSystemAccess(int worker_route_id
,
230 IPC::Message
* reply_msg
);
231 void OnRequestFileSystemAccessResponse(scoped_ptr
<IPC::Message
> reply_msg
,
233 void OnAllowIndexedDB(int worker_route_id
,
235 const base::string16
& name
,
237 void OnForceKillWorkerProcess();
239 // Relays a message to the given endpoint. Takes care of parsing the message
240 // if it contains a message port and sending it a valid route id.
241 void RelayMessage(const IPC::Message
& message
,
242 WorkerMessageFilter
* incoming_filter
,
243 WorkerInstance
* instance
);
245 void ShutdownSocketStreamDispatcherHostIfNecessary();
247 virtual bool CanShutdown() OVERRIDE
;
249 // Updates the title shown in the task manager.
252 // Return a vector of all the render process/render frame IDs that use the
254 std::vector
<std::pair
<int, int> > GetRenderFrameIDsForWorker(int route_id
);
256 // Callbacks for ResourceMessageFilter and SocketStreamDispatcherHost.
257 void GetContexts(const ResourceHostMsg_Request
& request
,
258 ResourceContext
** resource_context
,
259 net::URLRequestContext
** request_context
);
260 net::URLRequestContext
* GetRequestContext(ResourceType::Type resource_type
);
262 Instances instances_
;
264 ResourceContext
* const resource_context_
;
265 WorkerStoragePartition partition_
;
267 // A reference to the filter associated with this worker process. We need to
268 // keep this around since we'll use it when forward messages to the worker
270 scoped_refptr
<WorkerMessageFilter
> worker_message_filter_
;
272 scoped_ptr
<BrowserChildProcessHostImpl
> process_
;
273 bool process_launched_
;
275 scoped_refptr
<SocketStreamDispatcherHost
> socket_stream_dispatcher_host_
;
277 base::WeakPtrFactory
<WorkerProcessHost
> weak_factory_
;
279 DISALLOW_COPY_AND_ASSIGN(WorkerProcessHost
);
282 class WorkerProcessHostIterator
283 : public BrowserChildProcessHostTypeIterator
<WorkerProcessHost
> {
285 WorkerProcessHostIterator()
286 : BrowserChildProcessHostTypeIterator
<WorkerProcessHost
>(
287 PROCESS_TYPE_WORKER
) {
291 } // namespace content
293 #endif // CONTENT_BROWSER_WORKER_HOST_WORKER_PROCESS_HOST_H_