OLD | NEW |
| (Empty) |
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_PROCESS_HOST_H_ | |
6 #define CONTENT_BROWSER_WORKER_HOST_WORKER_PROCESS_HOST_H_ | |
7 | |
8 #include <list> | |
9 #include <string> | |
10 #include <utility> | |
11 | |
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" | |
25 #include "url/gurl.h" | |
26 | |
27 struct ResourceHostMsg_Request; | |
28 | |
29 namespace fileapi { | |
30 class FileSystemContext; | |
31 } // namespace fileapi | |
32 | |
33 namespace net { | |
34 class URLRequestContext; | |
35 } | |
36 | |
37 namespace webkit_database { | |
38 class DatabaseTracker; | |
39 } // namespace webkit_database | |
40 | |
41 namespace content { | |
42 class BrowserChildProcessHostImpl; | |
43 class IndexedDBContextImpl; | |
44 class ResourceContext; | |
45 class SocketStreamDispatcherHost; | |
46 class WorkerMessageFilter; | |
47 class WorkerServiceImpl; | |
48 | |
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 | |
54 // BrowserContext. | |
55 class WorkerProcessHost : public BrowserChildProcessHostDelegate, | |
56 public IPC::Sender { | |
57 public: | |
58 // Contains information about each worker instance, needed to forward messages | |
59 // between the renderer and worker processes. | |
60 class WorkerInstance { | |
61 public: | |
62 WorkerInstance(const GURL& url, | |
63 const base::string16& name, | |
64 const base::string16& content_security_policy, | |
65 blink::WebContentSecurityPolicyType security_policy_type, | |
66 int worker_route_id, | |
67 int render_frame_id, | |
68 ResourceContext* resource_context, | |
69 const WorkerStoragePartition& partition); | |
70 ~WorkerInstance(); | |
71 | |
72 // Unique identifier for a worker client. | |
73 class FilterInfo { | |
74 public: | |
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; } | |
81 | |
82 private: | |
83 WorkerMessageFilter* filter_; | |
84 int route_id_; | |
85 int message_port_id_; | |
86 }; | |
87 | |
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, | |
96 int route_id, | |
97 int message_port_id); | |
98 // Returns the single filter (must only be one). | |
99 FilterInfo GetFilter() const; | |
100 | |
101 typedef std::list<FilterInfo> FilterList; | |
102 const FilterList& filters() const { return filters_; } | |
103 | |
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. | |
107 bool Matches( | |
108 const GURL& url, | |
109 const base::string16& name, | |
110 const WorkerStoragePartition& partition, | |
111 ResourceContext* resource_context) const; | |
112 | |
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_; | |
118 }; | |
119 | |
120 // Accessors | |
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_; | |
127 } | |
128 blink::WebContentSecurityPolicyType security_policy_type() const { | |
129 return security_policy_type_; | |
130 } | |
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(); | |
135 } | |
136 ResourceContext* resource_context() const { | |
137 return resource_context_; | |
138 } | |
139 const WorkerStoragePartition& partition() const { | |
140 return partition_; | |
141 } | |
142 void set_load_failed(bool failed) { load_failed_ = failed; } | |
143 bool load_failed() { return load_failed_; } | |
144 | |
145 private: | |
146 // Set of all filters (clients) associated with this worker. | |
147 GURL url_; | |
148 bool closed_; | |
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_; | |
154 FilterList filters_; | |
155 scoped_refptr<WorkerDocumentSet> worker_document_set_; | |
156 ResourceContext* const resource_context_; | |
157 WorkerStoragePartition partition_; | |
158 bool load_failed_; | |
159 }; | |
160 | |
161 WorkerProcessHost(ResourceContext* resource_context, | |
162 const WorkerStoragePartition& partition); | |
163 virtual ~WorkerProcessHost(); | |
164 | |
165 // IPC::Sender implementation: | |
166 virtual bool Send(IPC::Message* message) OVERRIDE; | |
167 | |
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); | |
172 | |
173 // Creates a worker object in the process. | |
174 void CreateWorker(const WorkerInstance& instance, bool pause_on_start); | |
175 | |
176 // Returns true iff the given message from a renderer process was forwarded to | |
177 // the worker. | |
178 bool FilterMessage(const IPC::Message& message, WorkerMessageFilter* filter); | |
179 | |
180 void FilterShutdown(WorkerMessageFilter* filter); | |
181 | |
182 // Shuts down any shared workers that are no longer referenced by active | |
183 // documents. | |
184 void DocumentDetached(WorkerMessageFilter* filter, | |
185 unsigned long long document_id); | |
186 | |
187 // Terminates the given worker, i.e. based on a UI action. | |
188 CONTENT_EXPORT void TerminateWorker(int worker_route_id); | |
189 | |
190 // Callers can reduce the WorkerProcess' priority. | |
191 void SetBackgrounded(bool backgrounded); | |
192 | |
193 CONTENT_EXPORT const ChildProcessData& GetData(); | |
194 | |
195 typedef std::list<WorkerInstance> Instances; | |
196 const Instances& instances() const { return instances_; } | |
197 | |
198 ResourceContext* resource_context() const { | |
199 return resource_context_; | |
200 } | |
201 | |
202 bool process_launched() const { return process_launched_; } | |
203 | |
204 protected: | |
205 friend class WorkerServiceImpl; | |
206 | |
207 Instances& mutable_instances() { return instances_; } | |
208 | |
209 private: | |
210 // BrowserChildProcessHostDelegate implementation: | |
211 virtual void OnProcessLaunched() OVERRIDE; | |
212 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | |
213 | |
214 // Creates and adds the message filters. | |
215 void CreateMessageFilters(int render_process_id); | |
216 | |
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, | |
223 const GURL& url, | |
224 const base::string16& name, | |
225 const base::string16& display_name, | |
226 unsigned long estimated_size, | |
227 bool* result); | |
228 void OnRequestFileSystemAccess(int worker_route_id, | |
229 const GURL& url, | |
230 IPC::Message* reply_msg); | |
231 void OnRequestFileSystemAccessResponse(scoped_ptr<IPC::Message> reply_msg, | |
232 bool allowed); | |
233 void OnAllowIndexedDB(int worker_route_id, | |
234 const GURL& url, | |
235 const base::string16& name, | |
236 bool* result); | |
237 void OnForceKillWorkerProcess(); | |
238 | |
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); | |
244 | |
245 void ShutdownSocketStreamDispatcherHostIfNecessary(); | |
246 | |
247 virtual bool CanShutdown() OVERRIDE; | |
248 | |
249 // Updates the title shown in the task manager. | |
250 void UpdateTitle(); | |
251 | |
252 // Return a vector of all the render process/render frame IDs that use the | |
253 // given worker. | |
254 std::vector<std::pair<int, int> > GetRenderFrameIDsForWorker(int route_id); | |
255 | |
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); | |
261 | |
262 Instances instances_; | |
263 | |
264 ResourceContext* const resource_context_; | |
265 WorkerStoragePartition partition_; | |
266 | |
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 | |
269 // process. | |
270 scoped_refptr<WorkerMessageFilter> worker_message_filter_; | |
271 | |
272 scoped_ptr<BrowserChildProcessHostImpl> process_; | |
273 bool process_launched_; | |
274 | |
275 scoped_refptr<SocketStreamDispatcherHost> socket_stream_dispatcher_host_; | |
276 | |
277 base::WeakPtrFactory<WorkerProcessHost> weak_factory_; | |
278 | |
279 DISALLOW_COPY_AND_ASSIGN(WorkerProcessHost); | |
280 }; | |
281 | |
282 class WorkerProcessHostIterator | |
283 : public BrowserChildProcessHostTypeIterator<WorkerProcessHost> { | |
284 public: | |
285 WorkerProcessHostIterator() | |
286 : BrowserChildProcessHostTypeIterator<WorkerProcessHost>( | |
287 PROCESS_TYPE_WORKER) { | |
288 } | |
289 }; | |
290 | |
291 } // namespace content | |
292 | |
293 #endif // CONTENT_BROWSER_WORKER_HOST_WORKER_PROCESS_HOST_H_ | |
OLD | NEW |