1 // Copyright 2013 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 #include "chrome/browser/devtools/devtools_embedder_message_dispatcher.h"
8 #include "base/values.h"
12 using DispatchCallback
= DevToolsEmbedderMessageDispatcher::DispatchCallback
;
14 bool GetValue(const base::Value
* value
, std::string
* result
) {
15 return value
->GetAsString(result
);
18 bool GetValue(const base::Value
* value
, int* result
) {
19 return value
->GetAsInteger(result
);
22 bool GetValue(const base::Value
* value
, bool* result
) {
23 return value
->GetAsBoolean(result
);
26 bool GetValue(const base::Value
* value
, gfx::Rect
* rect
) {
27 const base::DictionaryValue
* dict
;
28 if (!value
->GetAsDictionary(&dict
))
34 if (!dict
->GetInteger("x", &x
) ||
35 !dict
->GetInteger("y", &y
) ||
36 !dict
->GetInteger("width", &width
) ||
37 !dict
->GetInteger("height", &height
))
39 rect
->SetRect(x
, y
, width
, height
);
44 struct StorageTraits
{
45 using StorageType
= T
;
49 struct StorageTraits
<const T
&> {
50 using StorageType
= T
;
53 // TODO(dgozman): move back to variadic templates once it compiles everywhere.
54 // See http://crbug.com/491973.
56 bool ParseAndHandle0(const base::Callback
<void()>& handler
,
57 const DispatchCallback
& callback
,
58 const base::ListValue
& list
) {
59 if (list
.GetSize() != 0)
66 bool ParseAndHandle1(const base::Callback
<void(A1
)>& handler
,
67 const DispatchCallback
& callback
,
68 const base::ListValue
& list
) {
69 if (list
.GetSize() != 1)
71 const base::Value
* value1
;
73 typename StorageTraits
<A1
>::StorageType a1
;
74 if (!GetValue(value1
, &a1
))
80 template<class A1
, class A2
>
81 bool ParseAndHandle2(const base::Callback
<void(A1
, A2
)>& handler
,
82 const DispatchCallback
& callback
,
83 const base::ListValue
& list
) {
84 if (list
.GetSize() != 2)
86 const base::Value
* value1
;
88 typename StorageTraits
<A1
>::StorageType a1
;
89 if (!GetValue(value1
, &a1
))
91 const base::Value
* value2
;
93 typename StorageTraits
<A2
>::StorageType a2
;
94 if (!GetValue(value2
, &a2
))
100 template<class A1
, class A2
, class A3
>
101 bool ParseAndHandle3(const base::Callback
<void(A1
, A2
, A3
)>& handler
,
102 const DispatchCallback
& callback
,
103 const base::ListValue
& list
) {
104 if (list
.GetSize() != 3)
106 const base::Value
* value1
;
107 list
.Get(0, &value1
);
108 typename StorageTraits
<A1
>::StorageType a1
;
109 if (!GetValue(value1
, &a1
))
111 const base::Value
* value2
;
112 list
.Get(1, &value2
);
113 typename StorageTraits
<A2
>::StorageType a2
;
114 if (!GetValue(value2
, &a2
))
116 const base::Value
* value3
;
117 list
.Get(2, &value3
);
118 typename StorageTraits
<A3
>::StorageType a3
;
119 if (!GetValue(value3
, &a3
))
121 handler
.Run(a1
, a2
, a3
);
125 bool ParseAndHandleWithCallback0(
126 const base::Callback
<void(const DispatchCallback
&)>& handler
,
127 const DispatchCallback
& callback
,
128 const base::ListValue
& list
) {
129 if (list
.GetSize() != 0)
131 handler
.Run(callback
);
136 bool ParseAndHandleWithCallback1(
137 const base::Callback
<void(const DispatchCallback
&, A1
)>& handler
,
138 const DispatchCallback
& callback
,
139 const base::ListValue
& list
) {
140 if (list
.GetSize() != 1)
142 const base::Value
* value1
;
143 list
.Get(0, &value1
);
144 typename StorageTraits
<A1
>::StorageType a1
;
145 if (!GetValue(value1
, &a1
))
147 handler
.Run(callback
, a1
);
151 template<class A1
, class A2
>
152 bool ParseAndHandleWithCallback2(
153 const base::Callback
<void(const DispatchCallback
&, A1
, A2
)>& handler
,
154 const DispatchCallback
& callback
,
155 const base::ListValue
& list
) {
156 if (list
.GetSize() != 2)
158 const base::Value
* value1
;
159 list
.Get(0, &value1
);
160 typename StorageTraits
<A1
>::StorageType a1
;
161 if (!GetValue(value1
, &a1
))
163 const base::Value
* value2
;
164 list
.Get(1, &value2
);
165 typename StorageTraits
<A2
>::StorageType a2
;
166 if (!GetValue(value2
, &a2
))
168 handler
.Run(callback
, a1
, a2
);
172 template<class A1
, class A2
, class A3
>
173 bool ParseAndHandleWithCallback3(
174 const base::Callback
<void(const DispatchCallback
&, A1
, A2
, A3
)>& handler
,
175 const DispatchCallback
& callback
,
176 const base::ListValue
& list
) {
177 if (list
.GetSize() != 3)
179 const base::Value
* value1
;
180 list
.Get(0, &value1
);
181 typename StorageTraits
<A1
>::StorageType a1
;
182 if (!GetValue(value1
, &a1
))
184 const base::Value
* value2
;
185 list
.Get(1, &value2
);
186 typename StorageTraits
<A2
>::StorageType a2
;
187 if (!GetValue(value2
, &a2
))
189 const base::Value
* value3
;
190 list
.Get(2, &value3
);
191 typename StorageTraits
<A3
>::StorageType a3
;
192 if (!GetValue(value3
, &a3
))
194 handler
.Run(callback
, a1
, a2
, a3
);
201 * Dispatcher for messages sent from the frontend running in an
202 * isolated renderer (chrome-devtools:// or chrome://inspect) to the embedder
205 * The messages are sent via InspectorFrontendHost.sendMessageToEmbedder or
206 * chrome.send method accordingly.
208 class DispatcherImpl
: public DevToolsEmbedderMessageDispatcher
{
210 ~DispatcherImpl() override
{}
212 bool Dispatch(const DispatchCallback
& callback
,
213 const std::string
& method
,
214 const base::ListValue
* params
) override
{
215 HandlerMap::iterator it
= handlers_
.find(method
);
216 return it
!= handlers_
.end() && it
->second
.Run(callback
, *params
);
219 void RegisterHandler(const std::string
& method
,
220 void (Delegate::*handler
)(),
221 Delegate
* delegate
) {
222 handlers_
[method
] = base::Bind(&ParseAndHandle0
,
224 base::Unretained(delegate
)));
228 void RegisterHandler(const std::string
& method
,
229 void (Delegate::*handler
)(A1
),
230 Delegate
* delegate
) {
231 handlers_
[method
] = base::Bind(&ParseAndHandle1
<A1
>,
233 base::Unretained(delegate
)));
236 template<class A1
, class A2
>
237 void RegisterHandler(const std::string
& method
,
238 void (Delegate::*handler
)(A1
, A2
),
239 Delegate
* delegate
) {
240 handlers_
[method
] = base::Bind(&ParseAndHandle2
<A1
, A2
>,
242 base::Unretained(delegate
)));
245 template<class A1
, class A2
, class A3
>
246 void RegisterHandler(const std::string
& method
,
247 void (Delegate::*handler
)(A1
, A2
, A3
),
248 Delegate
* delegate
) {
249 handlers_
[method
] = base::Bind(&ParseAndHandle3
<A1
, A2
, A3
>,
251 base::Unretained(delegate
)));
254 void RegisterHandlerWithCallback(
255 const std::string
& method
,
256 void (Delegate::*handler
)(const DispatchCallback
&),
257 Delegate
* delegate
) {
258 handlers_
[method
] = base::Bind(&ParseAndHandleWithCallback0
,
260 base::Unretained(delegate
)));
264 void RegisterHandlerWithCallback(
265 const std::string
& method
,
266 void (Delegate::*handler
)(const DispatchCallback
&, A1
),
267 Delegate
* delegate
) {
268 handlers_
[method
] = base::Bind(&ParseAndHandleWithCallback1
<A1
>,
270 base::Unretained(delegate
)));
273 template<class A1
, class A2
>
274 void RegisterHandlerWithCallback(
275 const std::string
& method
,
276 void (Delegate::*handler
)(const DispatchCallback
&, A1
, A2
),
277 Delegate
* delegate
) {
278 handlers_
[method
] = base::Bind(&ParseAndHandleWithCallback2
<A1
, A2
>,
280 base::Unretained(delegate
)));
283 template<class A1
, class A2
, class A3
>
284 void RegisterHandlerWithCallback(
285 const std::string
& method
,
286 void (Delegate::*handler
)(const DispatchCallback
&, A1
, A2
, A3
),
287 Delegate
* delegate
) {
288 handlers_
[method
] = base::Bind(&ParseAndHandleWithCallback3
<A1
, A2
, A3
>,
290 base::Unretained(delegate
)));
294 using Handler
= base::Callback
<bool(const DispatchCallback
&,
295 const base::ListValue
&)>;
296 using HandlerMap
= std::map
<std::string
, Handler
>;
297 HandlerMap handlers_
;
301 DevToolsEmbedderMessageDispatcher
*
302 DevToolsEmbedderMessageDispatcher::CreateForDevToolsFrontend(
303 Delegate
* delegate
) {
304 DispatcherImpl
* d
= new DispatcherImpl();
306 d
->RegisterHandler("bringToFront", &Delegate::ActivateWindow
, delegate
);
307 d
->RegisterHandler("closeWindow", &Delegate::CloseWindow
, delegate
);
308 d
->RegisterHandler("loadCompleted", &Delegate::LoadCompleted
, delegate
);
309 d
->RegisterHandler("setInspectedPageBounds",
310 &Delegate::SetInspectedPageBounds
, delegate
);
311 d
->RegisterHandler("inspectElementCompleted",
312 &Delegate::InspectElementCompleted
, delegate
);
313 d
->RegisterHandler("inspectedURLChanged",
314 &Delegate::InspectedURLChanged
, delegate
);
315 d
->RegisterHandlerWithCallback("setIsDocked",
316 &Delegate::SetIsDocked
, delegate
);
317 d
->RegisterHandler("openInNewTab", &Delegate::OpenInNewTab
, delegate
);
318 d
->RegisterHandler("save", &Delegate::SaveToFile
, delegate
);
319 d
->RegisterHandler("append", &Delegate::AppendToFile
, delegate
);
320 d
->RegisterHandler("requestFileSystems",
321 &Delegate::RequestFileSystems
, delegate
);
322 d
->RegisterHandler("addFileSystem", &Delegate::AddFileSystem
, delegate
);
323 d
->RegisterHandler("removeFileSystem", &Delegate::RemoveFileSystem
, delegate
);
324 d
->RegisterHandler("upgradeDraggedFileSystemPermissions",
325 &Delegate::UpgradeDraggedFileSystemPermissions
, delegate
);
326 d
->RegisterHandler("indexPath", &Delegate::IndexPath
, delegate
);
327 d
->RegisterHandlerWithCallback("loadNetworkResource",
328 &Delegate::LoadNetworkResource
, delegate
);
329 d
->RegisterHandler("stopIndexing", &Delegate::StopIndexing
, delegate
);
330 d
->RegisterHandler("searchInPath", &Delegate::SearchInPath
, delegate
);
331 d
->RegisterHandler("setWhitelistedShortcuts",
332 &Delegate::SetWhitelistedShortcuts
, delegate
);
333 d
->RegisterHandler("zoomIn", &Delegate::ZoomIn
, delegate
);
334 d
->RegisterHandler("zoomOut", &Delegate::ZoomOut
, delegate
);
335 d
->RegisterHandler("resetZoom", &Delegate::ResetZoom
, delegate
);
336 d
->RegisterHandler("setDevicesUpdatesEnabled",
337 &Delegate::SetDevicesUpdatesEnabled
, delegate
);
338 d
->RegisterHandler("sendMessageToBrowser",
339 &Delegate::SendMessageToBrowser
, delegate
);
340 d
->RegisterHandler("recordEnumeratedHistogram",
341 &Delegate::RecordEnumeratedHistogram
, delegate
);
342 d
->RegisterHandlerWithCallback("sendJsonRequest",
343 &Delegate::SendJsonRequest
, delegate
);
344 d
->RegisterHandlerWithCallback("getPreferences",
345 &Delegate::GetPreferences
, delegate
);
346 d
->RegisterHandler("setPreference",
347 &Delegate::SetPreference
, delegate
);
348 d
->RegisterHandler("removePreference",
349 &Delegate::RemovePreference
, delegate
);
350 d
->RegisterHandler("clearPreferences",
351 &Delegate::ClearPreferences
, delegate
);