Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: chrome/browser/worker_host/worker_process_host.h

Issue 6055002: Create a message filter for message port messages. This allows a nice cleanu... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_WORKER_HOST_WORKER_PROCESS_HOST_H_ 5 #ifndef CHROME_BROWSER_WORKER_HOST_WORKER_PROCESS_HOST_H_
6 #define CHROME_BROWSER_WORKER_HOST_WORKER_PROCESS_HOST_H_ 6 #define CHROME_BROWSER_WORKER_HOST_WORKER_PROCESS_HOST_H_
7 #pragma once 7 #pragma once
8 8
9 #include <list> 9 #include <list>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/file_path.h" 12 #include "base/file_path.h"
14 #include "chrome/browser/browser_child_process_host.h" 13 #include "chrome/browser/browser_child_process_host.h"
15 #include "chrome/browser/net/chrome_url_request_context.h" 14 #include "chrome/browser/net/chrome_url_request_context.h"
16 #include "chrome/browser/worker_host/worker_document_set.h" 15 #include "chrome/browser/worker_host/worker_document_set.h"
17 #include "googleurl/src/gurl.h" 16 #include "googleurl/src/gurl.h"
18 17
19 class ChromeURLRequestContext; 18 class URLRequestContextGetter;
20 class ChromeURLRequestContextGetter;
21 namespace webkit_database {
22 class DatabaseTracker;
23 } // namespace webkit_database
24
25 struct ViewHostMsg_CreateWorker_Params;
26 19
27 // The WorkerProcessHost is the interface that represents the browser side of 20 // The WorkerProcessHost is the interface that represents the browser side of
28 // the browser <-> worker communication channel. There will be one 21 // the browser <-> worker communication channel. There will be one
29 // WorkerProcessHost per worker process. Currently each worker runs in its own 22 // WorkerProcessHost per worker process. Currently each worker runs in its own
30 // process, but that may change. However, we do assume [by storing a 23 // process, but that may change. However, we do assume [by storing a
31 // ChromeURLRequestContext] that a WorkerProcessHost serves a single Profile. 24 // URLRequestContext] that a WorkerProcessHost serves a single Profile.
32 class WorkerProcessHost : public BrowserChildProcessHost { 25 class WorkerProcessHost : public BrowserChildProcessHost {
33 public: 26 public:
34 27
35 // Contains information about each worker instance, needed to forward messages 28 // Contains information about each worker instance, needed to forward messages
36 // between the renderer and worker processes. 29 // between the renderer and worker processes.
37 class WorkerInstance { 30 class WorkerInstance {
38 public: 31 public:
39 WorkerInstance(const GURL& url, 32 WorkerInstance(const GURL& url,
40 bool shared, 33 bool shared,
41 bool off_the_record, 34 bool off_the_record,
42 const string16& name, 35 const string16& name,
43 int worker_route_id, 36 int worker_route_id,
44 int parent_process_id, 37 int parent_process_id,
45 int parent_appcache_host_id, 38 int parent_appcache_host_id,
46 int64 main_resource_appcache_id, 39 int64 main_resource_appcache_id,
47 ChromeURLRequestContext* request_context); 40 URLRequestContextGetter* request_context);
48 ~WorkerInstance(); 41 ~WorkerInstance();
49 42
50 // Unique identifier for a worker client. 43 // Unique identifier for a worker client.
51 typedef std::pair<IPC::Message::Sender*, int> SenderInfo; 44 typedef std::pair<WorkerMessageFilter*, int> FilterInfo;
52 45
53 // APIs to manage the sender list for a given instance. 46 // APIs to manage the filter list for a given instance.
54 void AddSender(IPC::Message::Sender* sender, int sender_route_id); 47 void AddFilter(WorkerMessageFilter* filter, int route_id);
55 void RemoveSender(IPC::Message::Sender* sender, int sender_route_id); 48 void RemoveFilter(WorkerMessageFilter* filter, int route_id);
56 void RemoveSenders(IPC::Message::Sender* sender); 49 void RemoveFilters(WorkerMessageFilter* filter);
57 bool HasSender(IPC::Message::Sender* sender, int sender_route_id) const; 50 bool HasFilter(WorkerMessageFilter* filter, int route_id) const;
58 bool RendererIsParent(int renderer_id, int render_view_route_id) const; 51 bool RendererIsParent(int render_process_id, int render_view_id) const;
59 int NumSenders() const { return senders_.size(); } 52 int NumFilters() const { return filters_.size(); }
60 // Returns the single sender (must only be one). 53 // Returns the single filter (must only be one).
61 SenderInfo GetSender() const; 54 FilterInfo GetFilter() const;
62 55
63 typedef std::list<SenderInfo> SenderList; 56 typedef std::list<FilterInfo> FilterList;
64 const SenderList& senders() const { return senders_; } 57 const FilterList& filters() const { return filters_; }
65 58
66 // Checks if this WorkerInstance matches the passed url/name params 59 // Checks if this WorkerInstance matches the passed url/name params
67 // (per the comparison algorithm in the WebWorkers spec). This API only 60 // (per the comparison algorithm in the WebWorkers spec). This API only
68 // applies to shared workers. 61 // applies to shared workers.
69 bool Matches( 62 bool Matches(
70 const GURL& url, const string16& name, bool off_the_record) const; 63 const GURL& url, const string16& name, bool off_the_record) const;
71 64
72 // Shares the passed instance's WorkerDocumentSet with this instance. This 65 // Shares the passed instance's WorkerDocumentSet with this instance. This
73 // instance's current WorkerDocumentSet is dereferenced (and freed if this 66 // instance's current WorkerDocumentSet is dereferenced (and freed if this
74 // is the only reference) as a result. 67 // is the only reference) as a result.
(...skipping 10 matching lines...) Expand all
85 const string16 name() const { return name_; } 78 const string16 name() const { return name_; }
86 int worker_route_id() const { return worker_route_id_; } 79 int worker_route_id() const { return worker_route_id_; }
87 int parent_process_id() const { return parent_process_id_; } 80 int parent_process_id() const { return parent_process_id_; }
88 int parent_appcache_host_id() const { return parent_appcache_host_id_; } 81 int parent_appcache_host_id() const { return parent_appcache_host_id_; }
89 int64 main_resource_appcache_id() const { 82 int64 main_resource_appcache_id() const {
90 return main_resource_appcache_id_; 83 return main_resource_appcache_id_;
91 } 84 }
92 WorkerDocumentSet* worker_document_set() const { 85 WorkerDocumentSet* worker_document_set() const {
93 return worker_document_set_; 86 return worker_document_set_;
94 } 87 }
95 ChromeURLRequestContext* request_context() const { 88 URLRequestContextGetter* request_context() const {
96 return request_context_; 89 return request_context_;
97 } 90 }
98 91
99 private: 92 private:
100 // Set of all senders (clients) associated with this worker. 93 // Set of all filters (clients) associated with this worker.
101 GURL url_; 94 GURL url_;
102 bool shared_; 95 bool shared_;
103 bool off_the_record_; 96 bool off_the_record_;
104 bool closed_; 97 bool closed_;
105 string16 name_; 98 string16 name_;
106 int worker_route_id_; 99 int worker_route_id_;
107 int parent_process_id_; 100 int parent_process_id_;
108 int parent_appcache_host_id_; 101 int parent_appcache_host_id_;
109 int64 main_resource_appcache_id_; 102 int64 main_resource_appcache_id_;
110 scoped_refptr<ChromeURLRequestContext> request_context_; 103 scoped_refptr<URLRequestContextGetter> request_context_;
111 SenderList senders_; 104 FilterList filters_;
112 scoped_refptr<WorkerDocumentSet> worker_document_set_; 105 scoped_refptr<WorkerDocumentSet> worker_document_set_;
113 }; 106 };
114 107
115 WorkerProcessHost( 108 WorkerProcessHost(
116 ResourceDispatcherHost* resource_dispatcher_host, 109 ResourceDispatcherHost* resource_dispatcher_host,
117 ChromeURLRequestContext* request_context); 110 URLRequestContextGetter* request_context);
118 ~WorkerProcessHost(); 111 ~WorkerProcessHost();
119 112
120 // Starts the process. Returns true iff it succeeded. 113 // Starts the process. Returns true iff it succeeded.
121 bool Init(); 114 // |render_process_id| is the renderer process responsible for starting this
115 // worker.
116 bool Init(int render_process_id);
122 117
123 // Creates a worker object in the process. 118 // Creates a worker object in the process.
124 void CreateWorker(const WorkerInstance& instance); 119 void CreateWorker(const WorkerInstance& instance);
125 120
126 // Returns true iff the given message from a renderer process was forwarded to 121 // Returns true iff the given message from a renderer process was forwarded to
127 // the worker. 122 // the worker.
128 bool FilterMessage(const IPC::Message& message, IPC::Message::Sender* sender); 123 bool FilterMessage(const IPC::Message& message, WorkerMessageFilter* filter);
129 124
130 void SenderShutdown(IPC::Message::Sender* sender); 125 void FilterShutdown(WorkerMessageFilter* filter);
131 126
132 // Shuts down any shared workers that are no longer referenced by active 127 // Shuts down any shared workers that are no longer referenced by active
133 // documents. 128 // documents.
134 void DocumentDetached(IPC::Message::Sender* sender, 129 void DocumentDetached(WorkerMessageFilter* filter,
135 unsigned long long document_id); 130 unsigned long long document_id);
136 131
137 ChromeURLRequestContext* request_context() const { 132 URLRequestContextGetter* request_context() const {
138 return request_context_; 133 return request_context_;
139 } 134 }
140 135
141 protected: 136 protected:
142 friend class WorkerService; 137 friend class WorkerService;
143 138
144 typedef std::list<WorkerInstance> Instances; 139 typedef std::list<WorkerInstance> Instances;
145 const Instances& instances() const { return instances_; } 140 const Instances& instances() const { return instances_; }
146 Instances& mutable_instances() { return instances_; } 141 Instances& mutable_instances() { return instances_; }
147 142
148 private: 143 private:
144 // Called when the process has been launched successfully.
145 virtual void OnProcessLaunched();
146
147 // Creates and adds the message filters.
148 void CreateMessageFilters(int render_process_id);
149
149 // IPC::Channel::Listener implementation: 150 // IPC::Channel::Listener implementation:
150 // Called when a message arrives from the worker process. 151 // Called when a message arrives from the worker process.
151 virtual void OnMessageReceived(const IPC::Message& message); 152 virtual void OnMessageReceived(const IPC::Message& message);
152 153
153 // Creates and adds the message filters.
154 void CreateMessageFilters();
155
156 // Called when the process has been launched successfully.
157 virtual void OnProcessLaunched();
158
159 // Called when the app invokes close() from within worker context.
160 void OnWorkerContextClosed(int worker_route_id); 154 void OnWorkerContextClosed(int worker_route_id);
161 155 void OnAllowDatabase(int worker_route_id,
162 // Called if a worker tries to connect to a shared worker. 156 const GURL& url,
163 void OnLookupSharedWorker(const ViewHostMsg_CreateWorker_Params& params, 157 const string16& name,
164 bool* exists, 158 const string16& display_name,
165 int* route_id, 159 unsigned long estimated_size,
166 bool* url_error); 160 bool* result);
167
168 // Given a Sender, returns the callback that generates a new routing id.
169 static CallbackWithReturnValue<int>::Type* GetNextRouteIdCallback(
170 IPC::Message::Sender* sender);
171 161
172 // Relays a message to the given endpoint. Takes care of parsing the message 162 // Relays a message to the given endpoint. Takes care of parsing the message
173 // if it contains a message port and sending it a valid route id. 163 // if it contains a message port and sending it a valid route id.
174 static void RelayMessage(const IPC::Message& message, 164 static void RelayMessage(const IPC::Message& message,
175 IPC::Message::Sender* sender, 165 WorkerMessageFilter* filter,
176 int route_id, 166 int route_id);
177 CallbackWithReturnValue<int>::Type* next_route_id);
178 167
179 virtual bool CanShutdown(); 168 virtual bool CanShutdown();
180 169
181 // Updates the title shown in the task manager. 170 // Updates the title shown in the task manager.
182 void UpdateTitle(); 171 void UpdateTitle();
183 172
184 void OnCreateWorker(const ViewHostMsg_CreateWorker_Params& params, 173 ChromeURLRequestContext* GetChromeURLRequestContext();
185 int* route_id);
186 void OnCancelCreateDedicatedWorker(int route_id);
187 void OnForwardToWorker(const IPC::Message& message);
188
189 // Checks the content settings whether access to web databases is enabled and
190 // relays the WebDatabaseAccessed message to all documents attached to a
191 // worker.
192 void OnAllowDatabase(const GURL& url,
193 const string16& name,
194 const string16& display_name,
195 unsigned long estimated_size,
196 IPC::Message* reply_msg);
197 174
198 Instances instances_; 175 Instances instances_;
199 176
200 scoped_refptr<ChromeURLRequestContext> request_context_; 177 scoped_refptr<URLRequestContextGetter> request_context_;
201 178
202 // A callback to create a routing id for the associated worker process. 179 // A reference to the filter associated with this worker process. We need to
203 scoped_ptr<CallbackWithReturnValue<int>::Type> next_route_id_callback_; 180 // keep this around since we'll use it when forward messages to the worker
181 // process.
182 scoped_refptr<WorkerMessageFilter> worker_message_filter_;
204 183
205 DISALLOW_COPY_AND_ASSIGN(WorkerProcessHost); 184 DISALLOW_COPY_AND_ASSIGN(WorkerProcessHost);
206 }; 185 };
207 186
208 #endif // CHROME_BROWSER_WORKER_HOST_WORKER_PROCESS_HOST_H_ 187 #endif // CHROME_BROWSER_WORKER_HOST_WORKER_PROCESS_HOST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698