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 bool GetValue(const base::Value
* value
, std::string
* result
) {
13 return value
->GetAsString(result
);
16 bool GetValue(const base::Value
* value
, int* result
) {
17 return value
->GetAsInteger(result
);
20 bool GetValue(const base::Value
* value
, bool* result
) {
21 return value
->GetAsBoolean(result
);
24 bool GetValue(const base::Value
* value
, gfx::Rect
* rect
) {
25 const base::DictionaryValue
* dict
;
26 if (!value
->GetAsDictionary(&dict
))
32 if (!dict
->GetInteger("x", &x
) ||
33 !dict
->GetInteger("y", &y
) ||
34 !dict
->GetInteger("width", &width
) ||
35 !dict
->GetInteger("height", &height
))
37 rect
->SetRect(x
, y
, width
, height
);
42 struct StorageTraits
{
43 using StorageType
= T
;
47 struct StorageTraits
<const T
&> {
48 using StorageType
= T
;
51 template <typename
... Ts
>
53 bool Parse(const base::ListValue
& list
,
54 const base::ListValue::const_iterator
& it
) {
55 return it
== list
.end();
58 template <typename H
, typename
... As
>
59 void Apply(const H
& handler
, As
... args
) {
64 template <typename T
, typename
... Ts
>
65 struct ParamTuple
<T
, Ts
...> {
66 bool Parse(const base::ListValue
& list
,
67 const base::ListValue::const_iterator
& it
) {
70 if (!GetValue(*it
, &head
))
72 return tail
.Parse(list
, it
+ 1);
75 template <typename H
, typename
... As
>
76 void Apply(const H
& handler
, As
... args
) {
77 tail
.template Apply
<H
, As
..., T
>(handler
, args
..., head
);
80 typename StorageTraits
<T
>::StorageType head
;
81 ParamTuple
<Ts
...> tail
;
84 template<typename
... As
>
85 bool ParseAndHandle(const base::Callback
<void(As
...)>& handler
,
86 const base::ListValue
& list
) {
87 ParamTuple
<As
...> tuple
;
88 if (!tuple
.Parse(list
, list
.begin()))
97 * Dispatcher for messages sent from the frontend running in an
98 * isolated renderer (chrome-devtools:// or chrome://inspect) to the embedder
101 * The messages are sent via InspectorFrontendHost.sendMessageToEmbedder or
102 * chrome.send method accordingly.
104 class DispatcherImpl
: public DevToolsEmbedderMessageDispatcher
{
106 ~DispatcherImpl() override
{}
108 bool Dispatch(const std::string
& method
,
109 const base::ListValue
* params
,
110 std::string
* error
) override
{
111 HandlerMap::iterator it
= handlers_
.find(method
);
112 if (it
== handlers_
.end())
115 if (it
->second
.Run(*params
))
119 *error
= "Invalid frontend host message parameters: " + method
;
123 typedef base::Callback
<bool(const base::ListValue
&)> Handler
;
124 void RegisterHandler(const std::string
& method
, const Handler
& handler
) {
125 handlers_
[method
] = handler
;
128 template<typename T
, typename
... As
>
129 void RegisterHandler(const std::string
& method
,
130 void (T::*handler
)(As
...), T
* delegate
) {
131 handlers_
[method
] = base::Bind(&ParseAndHandle
<As
...>,
133 base::Unretained(delegate
)));
137 typedef std::map
<std::string
, Handler
> HandlerMap
;
138 HandlerMap handlers_
;
142 DevToolsEmbedderMessageDispatcher
*
143 DevToolsEmbedderMessageDispatcher::createForDevToolsFrontend(
144 Delegate
* delegate
) {
145 DispatcherImpl
* d
= new DispatcherImpl();
147 d
->RegisterHandler("bringToFront", &Delegate::ActivateWindow
, delegate
);
148 d
->RegisterHandler("closeWindow", &Delegate::CloseWindow
, delegate
);
149 d
->RegisterHandler("loadCompleted", &Delegate::LoadCompleted
, delegate
);
150 d
->RegisterHandler("setInspectedPageBounds",
151 &Delegate::SetInspectedPageBounds
, delegate
);
152 d
->RegisterHandler("inspectElementCompleted",
153 &Delegate::InspectElementCompleted
, delegate
);
154 d
->RegisterHandler("inspectedURLChanged",
155 &Delegate::InspectedURLChanged
, delegate
);
156 d
->RegisterHandler("setIsDocked", &Delegate::SetIsDocked
, delegate
);
157 d
->RegisterHandler("openInNewTab", &Delegate::OpenInNewTab
, delegate
);
158 d
->RegisterHandler("save", &Delegate::SaveToFile
, delegate
);
159 d
->RegisterHandler("append", &Delegate::AppendToFile
, delegate
);
160 d
->RegisterHandler("requestFileSystems",
161 &Delegate::RequestFileSystems
, delegate
);
162 d
->RegisterHandler("addFileSystem", &Delegate::AddFileSystem
, delegate
);
163 d
->RegisterHandler("removeFileSystem", &Delegate::RemoveFileSystem
, delegate
);
164 d
->RegisterHandler("upgradeDraggedFileSystemPermissions",
165 &Delegate::UpgradeDraggedFileSystemPermissions
, delegate
);
166 d
->RegisterHandler("indexPath", &Delegate::IndexPath
, delegate
);
167 d
->RegisterHandler("stopIndexing", &Delegate::StopIndexing
, delegate
);
168 d
->RegisterHandler("searchInPath", &Delegate::SearchInPath
, delegate
);
169 d
->RegisterHandler("setWhitelistedShortcuts",
170 &Delegate::SetWhitelistedShortcuts
, delegate
);
171 d
->RegisterHandler("zoomIn", &Delegate::ZoomIn
, delegate
);
172 d
->RegisterHandler("zoomOut", &Delegate::ZoomOut
, delegate
);
173 d
->RegisterHandler("resetZoom", &Delegate::ResetZoom
, delegate
);
174 d
->RegisterHandler("openUrlOnRemoteDeviceAndInspect",
175 &Delegate::OpenUrlOnRemoteDeviceAndInspect
, delegate
);
176 d
->RegisterHandler("setDeviceCountUpdatesEnabled",
177 &Delegate::SetDeviceCountUpdatesEnabled
, delegate
);
178 d
->RegisterHandler("setDevicesUpdatesEnabled",
179 &Delegate::SetDevicesUpdatesEnabled
, delegate
);
180 d
->RegisterHandler("sendMessageToBrowser",
181 &Delegate::SendMessageToBrowser
, delegate
);
182 d
->RegisterHandler("recordActionUMA",
183 &Delegate::RecordActionUMA
, delegate
);