OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 // This file defines utility functions for sending notifications (calling | |
6 // methods that return void and do not have out params) to the RenderViewHost | |
7 // or one of its delegate interfaces. The notifications are dispatched | |
8 // asynchronously, and only if the specified RenderViewHost still exists. | |
9 | |
10 #ifndef CHROME_BROWSER_RENDERER_HOST_RENDER_VIEW_HOST_NOTIFICATION_TASK_H_ | 5 #ifndef CHROME_BROWSER_RENDERER_HOST_RENDER_VIEW_HOST_NOTIFICATION_TASK_H_ |
11 #define CHROME_BROWSER_RENDERER_HOST_RENDER_VIEW_HOST_NOTIFICATION_TASK_H_ | 6 #define CHROME_BROWSER_RENDERER_HOST_RENDER_VIEW_HOST_NOTIFICATION_TASK_H_ |
12 #pragma once | 7 #pragma once |
13 | 8 |
14 #include "base/callback.h" | 9 // TODO(jam): remove this file when all files have been converted. |
15 #include "base/task.h" | 10 #include "content/browser/renderer_host/render_view_host_notification_task.h" |
16 #include "chrome/browser/browser_thread.h" | |
17 #include "chrome/browser/renderer_host/render_view_host.h" | |
18 #include "chrome/browser/renderer_host/render_view_host_delegate.h" | |
19 | |
20 // ---------------------------------------------------------------------------- | |
21 | |
22 namespace internal { | |
23 | |
24 // The simplest Mapper, used when proxying calls to a RenderViewHost. | |
25 class RenderViewHostIdentityMapper { | |
26 public: | |
27 typedef RenderViewHost MappedType; | |
28 static MappedType* Map(RenderViewHost* rvh) { return rvh; } | |
29 }; | |
30 | |
31 template <typename Method, typename Params, | |
32 typename Mapper = RenderViewHostIdentityMapper> | |
33 class RenderViewHostNotificationTask : public Task { | |
34 public: | |
35 RenderViewHostNotificationTask(int render_process_id, | |
36 int render_view_id, | |
37 Method method, | |
38 const Params& params) | |
39 : render_process_id_(render_process_id), | |
40 render_view_id_(render_view_id), | |
41 unbound_method_(method, params) { | |
42 } | |
43 | |
44 virtual void Run() { | |
45 RenderViewHost* rvh = RenderViewHost::FromID(render_process_id_, | |
46 render_view_id_); | |
47 typename Mapper::MappedType* obj = Mapper::Map(rvh); | |
48 if (obj) | |
49 unbound_method_.Run(obj); | |
50 } | |
51 | |
52 private: | |
53 int render_process_id_; | |
54 int render_view_id_; | |
55 UnboundMethod<typename Mapper::MappedType, Method, Params> unbound_method_; | |
56 | |
57 DISALLOW_COPY_AND_ASSIGN(RenderViewHostNotificationTask); | |
58 }; | |
59 | |
60 // For proxying calls to RenderViewHost | |
61 | |
62 template <typename Method, typename Params> | |
63 inline void CallRenderViewHostHelper(int render_process_id, int render_view_id, | |
64 Method method, const Params& params) { | |
65 BrowserThread::PostTask( | |
66 BrowserThread::UI, FROM_HERE, | |
67 new RenderViewHostNotificationTask<Method, Params>(render_process_id, | |
68 render_view_id, | |
69 method, | |
70 params)); | |
71 } | |
72 | |
73 // For proxying calls to RenderViewHostDelegate::ContentSettings | |
74 | |
75 class RenderViewHostToContentSettingsDelegate { | |
76 public: | |
77 typedef RenderViewHostDelegate::ContentSettings MappedType; | |
78 static MappedType* Map(RenderViewHost* rvh) { | |
79 return rvh ? rvh->delegate()->GetContentSettingsDelegate() : NULL; | |
80 } | |
81 }; | |
82 | |
83 template <typename Method, typename Params> | |
84 inline void CallRenderViewHostContentSettingsDelegateHelper( | |
85 int render_process_id, | |
86 int render_view_id, | |
87 Method method, | |
88 const Params& params) { | |
89 | |
90 BrowserThread::PostTask( | |
91 BrowserThread::UI, FROM_HERE, | |
92 new RenderViewHostNotificationTask< | |
93 Method, Params, RenderViewHostToContentSettingsDelegate>( | |
94 render_process_id, | |
95 render_view_id, | |
96 method, | |
97 params)); | |
98 } | |
99 | |
100 // For proxying calls to RenderViewHostDelegate::RendererManagement | |
101 | |
102 class RenderViewHostToRendererManagementDelegate { | |
103 public: | |
104 typedef RenderViewHostDelegate::RendererManagement MappedType; | |
105 static MappedType* Map(RenderViewHost* rvh) { | |
106 return rvh ? rvh->delegate()->GetRendererManagementDelegate() : NULL; | |
107 } | |
108 }; | |
109 | |
110 template <typename Method, typename Params> | |
111 inline void CallRenderViewHostRendererManagementDelegateHelper( | |
112 int render_process_id, | |
113 int render_view_id, | |
114 Method method, | |
115 const Params& params) { | |
116 BrowserThread::PostTask( | |
117 BrowserThread::UI, FROM_HERE, | |
118 new RenderViewHostNotificationTask< | |
119 Method, Params, RenderViewHostToRendererManagementDelegate>( | |
120 render_process_id, | |
121 render_view_id, | |
122 method, | |
123 params)); | |
124 } | |
125 | |
126 // For proxying calls to RenderViewHostDelegate::SSL | |
127 | |
128 class RenderViewHostToSSLDelegate { | |
129 public: | |
130 typedef RenderViewHostDelegate::SSL MappedType; | |
131 static MappedType* Map(RenderViewHost* rvh) { | |
132 return rvh ? rvh->delegate()->GetSSLDelegate() : NULL; | |
133 } | |
134 }; | |
135 | |
136 template <typename Method, typename Params> | |
137 inline void CallRenderViewHostSSLDelegateHelper( | |
138 int render_process_id, | |
139 int render_view_id, | |
140 Method method, | |
141 const Params& params) { | |
142 BrowserThread::PostTask( | |
143 BrowserThread::UI, FROM_HERE, | |
144 new RenderViewHostNotificationTask< | |
145 Method, Params, RenderViewHostToSSLDelegate>( | |
146 render_process_id, | |
147 render_view_id, | |
148 method, | |
149 params)); | |
150 } | |
151 | |
152 } // namespace internal | |
153 | |
154 // ---------------------------------------------------------------------------- | |
155 // Proxy calls to the specified RenderViewHost. | |
156 | |
157 template <typename Method> | |
158 inline void CallRenderViewHost(int render_process_id, | |
159 int render_view_id, | |
160 Method method) { | |
161 internal::CallRenderViewHostHelper(render_process_id, | |
162 render_view_id, | |
163 method, | |
164 MakeTuple()); | |
165 } | |
166 | |
167 template <typename Method, typename A> | |
168 inline void CallRenderViewHost(int render_process_id, | |
169 int render_view_id, | |
170 Method method, | |
171 const A& a) { | |
172 internal::CallRenderViewHostHelper(render_process_id, | |
173 render_view_id, | |
174 method, | |
175 MakeTuple(a)); | |
176 } | |
177 | |
178 template <typename Method, typename A, typename B> | |
179 inline void CallRenderViewHost(int render_process_id, | |
180 int render_view_id, | |
181 Method method, | |
182 const A& a, | |
183 const B& b) { | |
184 internal::CallRenderViewHostHelper(render_process_id, | |
185 render_view_id, | |
186 method, | |
187 MakeTuple(a, b)); | |
188 } | |
189 | |
190 // ---------------------------------------------------------------------------- | |
191 // Proxy calls to the specified RenderViewHost's ContentSettings delegate. | |
192 | |
193 template <typename Method> | |
194 inline void CallRenderViewHostContentSettingsDelegate(int render_process_id, | |
195 int render_view_id, | |
196 Method method) { | |
197 internal::CallRenderViewHostContentSettingsDelegateHelper(render_process_id, | |
198 render_view_id, | |
199 method, | |
200 MakeTuple()); | |
201 } | |
202 | |
203 template <typename Method, typename A> | |
204 inline void CallRenderViewHostContentSettingsDelegate(int render_process_id, | |
205 int render_view_id, | |
206 Method method, | |
207 const A& a) { | |
208 internal::CallRenderViewHostContentSettingsDelegateHelper(render_process_id, | |
209 render_view_id, | |
210 method, | |
211 MakeTuple(a)); | |
212 } | |
213 | |
214 template <typename Method, typename A, typename B> | |
215 inline void CallRenderViewHostContentSettingsDelegate(int render_process_id, | |
216 int render_view_id, | |
217 Method method, | |
218 const A& a, | |
219 const B& b) { | |
220 internal::CallRenderViewHostContentSettingsDelegateHelper(render_process_id, | |
221 render_view_id, | |
222 method, | |
223 MakeTuple(a, b)); | |
224 } | |
225 | |
226 template <typename Method, typename A, typename B, typename C> | |
227 inline void CallRenderViewHostContentSettingsDelegate(int render_process_id, | |
228 int render_view_id, | |
229 Method method, | |
230 const A& a, | |
231 const B& b, | |
232 const C& c) { | |
233 internal::CallRenderViewHostContentSettingsDelegateHelper(render_process_id, | |
234 render_view_id, | |
235 method, | |
236 MakeTuple(a, b, c)); | |
237 } | |
238 | |
239 template <typename Method, typename A, typename B, typename C, typename D> | |
240 inline void CallRenderViewHostContentSettingsDelegate(int render_process_id, | |
241 int render_view_id, | |
242 Method method, | |
243 const A& a, | |
244 const B& b, | |
245 const C& c, | |
246 const D& d) { | |
247 internal::CallRenderViewHostContentSettingsDelegateHelper( | |
248 render_process_id, | |
249 render_view_id, | |
250 method, | |
251 MakeTuple(a, b, c, d)); | |
252 } | |
253 | |
254 template <typename Method, | |
255 typename A, typename B, typename C, typename D, typename E> | |
256 inline void CallRenderViewHostContentSettingsDelegate(int render_process_id, | |
257 int render_view_id, | |
258 Method method, | |
259 const A& a, | |
260 const B& b, | |
261 const C& c, | |
262 const D& d, | |
263 const E& e) { | |
264 internal::CallRenderViewHostContentSettingsDelegateHelper( | |
265 render_process_id, render_view_id, method, MakeTuple(a, b, c, d, e)); | |
266 } | |
267 | |
268 // ---------------------------------------------------------------------------- | |
269 // Proxy calls to the specified RenderViewHost's RendererManagement delegate. | |
270 | |
271 template <typename Method> | |
272 inline void CallRenderViewHostRendererManagementDelegate(int render_process_id, | |
273 int render_view_id, | |
274 Method method) { | |
275 internal::CallRenderViewHostRendererManagementDelegateHelper( | |
276 render_process_id, | |
277 render_view_id, | |
278 method, | |
279 MakeTuple()); | |
280 } | |
281 | |
282 template <typename Method, typename A> | |
283 inline void CallRenderViewHostRendererManagementDelegate(int render_process_id, | |
284 int render_view_id, | |
285 Method method, | |
286 const A& a) { | |
287 internal::CallRenderViewHostRendererManagementDelegateHelper( | |
288 render_process_id, | |
289 render_view_id, | |
290 method, | |
291 MakeTuple(a)); | |
292 } | |
293 | |
294 template <typename Method, typename A, typename B> | |
295 inline void CallRenderViewHostRendererManagementDelegate(int render_process_id, | |
296 int render_view_id, | |
297 Method method, | |
298 const A& a, | |
299 const B& b) { | |
300 internal::CallRenderViewHostRendererManagementDelegateHelper( | |
301 render_process_id, | |
302 render_view_id, | |
303 method, | |
304 MakeTuple(a, b)); | |
305 } | |
306 | |
307 // ---------------------------------------------------------------------------- | |
308 // Proxy calls to the specified RenderViewHost's SSL delegate. | |
309 | |
310 template <typename Method, typename A> | |
311 inline void CallRenderViewHostSSLDelegate(int render_process_id, | |
312 int render_view_id, | |
313 Method method, | |
314 const A& a) { | |
315 internal::CallRenderViewHostSSLDelegateHelper( | |
316 render_process_id, | |
317 render_view_id, | |
318 method, | |
319 MakeTuple(a)); | |
320 } | |
321 | |
322 template <typename Method, typename A, typename B> | |
323 inline void CallRenderViewHostSSLDelegate(int render_process_id, | |
324 int render_view_id, | |
325 Method method, | |
326 const A& a, | |
327 const B& b) { | |
328 internal::CallRenderViewHostSSLDelegateHelper( | |
329 render_process_id, | |
330 render_view_id, | |
331 method, | |
332 MakeTuple(a, b)); | |
333 } | |
334 | |
335 // ---------------------------------------------------------------------------- | |
336 | 11 |
337 #endif // CHROME_BROWSER_RENDERER_HOST_RENDER_VIEW_HOST_NOTIFICATION_TASK_H_ | 12 #endif // CHROME_BROWSER_RENDERER_HOST_RENDER_VIEW_HOST_NOTIFICATION_TASK_H_ |
OLD | NEW |