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 WorkerMessageFilter
;
46 class WorkerServiceImpl
;
48 // The WorkerProcessHost is the interface that represents the browser side of
49 // the browser <-> worker communication channel. There will be one
50 // WorkerProcessHost per worker process. Currently each worker runs in its own
51 // process, but that may change. However, we do assume (by storing a
52 // net::URLRequestContext) that a WorkerProcessHost serves a single
54 class WorkerProcessHost
: public BrowserChildProcessHostDelegate
,
57 // Contains information about each worker instance, needed to forward messages
58 // between the renderer and worker processes.
59 class WorkerInstance
{
61 WorkerInstance(const GURL
& url
,
62 const base::string16
& name
,
63 const base::string16
& content_security_policy
,
64 blink::WebContentSecurityPolicyType security_policy_type
,
67 ResourceContext
* resource_context
,
68 const WorkerStoragePartition
& partition
);
71 // Unique identifier for a worker client.
74 FilterInfo(WorkerMessageFilter
* filter
, int route_id
)
75 : filter_(filter
), route_id_(route_id
), message_port_id_(0) { }
76 WorkerMessageFilter
* filter() const { return filter_
; }
77 int route_id() const { return route_id_
; }
78 int message_port_id() const { return message_port_id_
; }
79 void set_message_port_id(int id
) { message_port_id_
= id
; }
82 WorkerMessageFilter
* filter_
;
87 // APIs to manage the filter list for a given instance.
88 void AddFilter(WorkerMessageFilter
* filter
, int route_id
);
89 void RemoveFilter(WorkerMessageFilter
* filter
, int route_id
);
90 void RemoveFilters(WorkerMessageFilter
* filter
);
91 bool HasFilter(WorkerMessageFilter
* filter
, int route_id
) const;
92 bool FrameIsParent(int render_process_id
, int render_frame_id
) const;
93 int NumFilters() const { return filters_
.size(); }
94 void SetMessagePortID(WorkerMessageFilter
* filter
,
97 // Returns the single filter (must only be one).
98 FilterInfo
GetFilter() const;
100 typedef std::list
<FilterInfo
> FilterList
;
101 const FilterList
& filters() const { return filters_
; }
103 // Checks if this WorkerInstance matches the passed url/name params
104 // (per the comparison algorithm in the WebWorkers spec). This API only
105 // applies to shared workers.
108 const base::string16
& name
,
109 const WorkerStoragePartition
& partition
,
110 ResourceContext
* resource_context
) const;
112 // Shares the passed instance's WorkerDocumentSet with this instance. This
113 // instance's current WorkerDocumentSet is dereferenced (and freed if this
114 // is the only reference) as a result.
115 void ShareDocumentSet(const WorkerInstance
& instance
) {
116 worker_document_set_
= instance
.worker_document_set_
;
120 bool closed() const { return closed_
; }
121 void set_closed(bool closed
) { closed_
= closed
; }
122 const GURL
& url() const { return url_
; }
123 const base::string16
name() const { return name_
; }
124 const base::string16
content_security_policy() const {
125 return content_security_policy_
;
127 blink::WebContentSecurityPolicyType
security_policy_type() const {
128 return security_policy_type_
;
130 int worker_route_id() const { return worker_route_id_
; }
131 int render_frame_id() const { return render_frame_id_
; }
132 WorkerDocumentSet
* worker_document_set() const {
133 return worker_document_set_
.get();
135 ResourceContext
* resource_context() const {
136 return resource_context_
;
138 const WorkerStoragePartition
& partition() const {
141 void set_load_failed(bool failed
) { load_failed_
= failed
; }
142 bool load_failed() { return load_failed_
; }
145 // Set of all filters (clients) associated with this worker.
148 base::string16 name_
;
149 base::string16 content_security_policy_
;
150 blink::WebContentSecurityPolicyType security_policy_type_
;
151 int worker_route_id_
;
152 int render_frame_id_
;
154 scoped_refptr
<WorkerDocumentSet
> worker_document_set_
;
155 ResourceContext
* const resource_context_
;
156 WorkerStoragePartition partition_
;
160 WorkerProcessHost(ResourceContext
* resource_context
,
161 const WorkerStoragePartition
& partition
);
162 virtual ~WorkerProcessHost();
164 // IPC::Sender implementation:
165 virtual bool Send(IPC::Message
* message
) OVERRIDE
;
167 // Starts the process. Returns true iff it succeeded.
168 // |render_process_id| and |render_frame_id| are the renderer process and the
169 // renderer frame responsible for starting this worker.
170 bool Init(int render_process_id
, int render_frame_id
);
172 // Creates a worker object in the process.
173 void CreateWorker(const WorkerInstance
& instance
, bool pause_on_start
);
175 // Returns true iff the given message from a renderer process was forwarded to
177 bool FilterMessage(const IPC::Message
& message
, WorkerMessageFilter
* filter
);
179 void FilterShutdown(WorkerMessageFilter
* filter
);
181 // Shuts down any shared workers that are no longer referenced by active
183 void DocumentDetached(WorkerMessageFilter
* filter
,
184 unsigned long long document_id
);
186 // Terminates the given worker, i.e. based on a UI action.
187 CONTENT_EXPORT
void TerminateWorker(int worker_route_id
);
189 // Callers can reduce the WorkerProcess' priority.
190 void SetBackgrounded(bool backgrounded
);
192 CONTENT_EXPORT
const ChildProcessData
& GetData();
194 typedef std::list
<WorkerInstance
> Instances
;
195 const Instances
& instances() const { return instances_
; }
197 ResourceContext
* resource_context() const {
198 return resource_context_
;
201 bool process_launched() const { return process_launched_
; }
204 friend class WorkerServiceImpl
;
206 Instances
& mutable_instances() { return instances_
; }
209 // BrowserChildProcessHostDelegate implementation:
210 virtual void OnProcessLaunched() OVERRIDE
;
211 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
213 // Creates and adds the message filters.
214 void CreateMessageFilters(int render_process_id
);
216 void OnWorkerContextClosed(int worker_route_id
);
217 void OnWorkerContextDestroyed(int worker_route_id
);
218 void OnWorkerScriptLoaded(int worker_route_id
);
219 void OnWorkerScriptLoadFailed(int worker_route_id
);
220 void OnWorkerConnected(int message_port_id
, int worker_route_id
);
221 void OnAllowDatabase(int worker_route_id
,
223 const base::string16
& name
,
224 const base::string16
& display_name
,
225 unsigned long estimated_size
,
227 void OnRequestFileSystemAccessSync(int worker_route_id
,
230 void OnAllowIndexedDB(int worker_route_id
,
232 const base::string16
& name
,
234 void OnForceKillWorkerProcess();
236 // Relays a message to the given endpoint. Takes care of parsing the message
237 // if it contains a message port and sending it a valid route id.
238 void RelayMessage(const IPC::Message
& message
,
239 WorkerMessageFilter
* incoming_filter
,
240 WorkerInstance
* instance
);
242 void ShutdownSocketStreamDispatcherHostIfNecessary();
244 virtual bool CanShutdown() OVERRIDE
;
246 // Updates the title shown in the task manager.
249 // Return a vector of all the render process/render frame IDs that use the
251 std::vector
<std::pair
<int, int> > GetRenderFrameIDsForWorker(int route_id
);
253 // Callbacks for ResourceMessageFilter and SocketStreamDispatcherHost.
254 void GetContexts(const ResourceHostMsg_Request
& request
,
255 ResourceContext
** resource_context
,
256 net::URLRequestContext
** request_context
);
257 net::URLRequestContext
* GetRequestContext(ResourceType::Type resource_type
);
259 Instances instances_
;
261 ResourceContext
* const resource_context_
;
262 WorkerStoragePartition partition_
;
264 // A reference to the filter associated with this worker process. We need to
265 // keep this around since we'll use it when forward messages to the worker
267 scoped_refptr
<WorkerMessageFilter
> worker_message_filter_
;
269 scoped_ptr
<BrowserChildProcessHostImpl
> process_
;
270 bool process_launched_
;
272 scoped_refptr
<SocketStreamDispatcherHost
> socket_stream_dispatcher_host_
;
274 DISALLOW_COPY_AND_ASSIGN(WorkerProcessHost
);
277 class WorkerProcessHostIterator
278 : public BrowserChildProcessHostTypeIterator
<WorkerProcessHost
> {
280 WorkerProcessHostIterator()
281 : BrowserChildProcessHostTypeIterator
<WorkerProcessHost
>(
282 PROCESS_TYPE_WORKER
) {
286 } // namespace content
288 #endif // CONTENT_BROWSER_WORKER_HOST_WORKER_PROCESS_HOST_H_