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::ListValue
& list
, int pos
, std::string
& value
) {
13 return list
.GetString(pos
, &value
);
16 bool GetValue(const base::ListValue
& list
, int pos
, int& value
) {
17 return list
.GetInteger(pos
, &value
);
20 bool GetValue(const base::ListValue
& list
, int pos
, bool& value
) {
21 return list
.GetBoolean(pos
, &value
);
25 struct StorageTraits
{
26 typedef T StorageType
;
30 struct StorageTraits
<const T
&> {
31 typedef T StorageType
;
37 typedef typename StorageTraits
<A
>::StorageType ValueType
;
39 Argument(const base::ListValue
& list
, int pos
) {
40 valid_
= GetValue(list
, pos
, value_
);
43 ValueType
value() const { return value_
; }
44 bool valid() const { return valid_
; }
51 bool ParseAndHandle0(const base::Callback
<void(void)>& handler
,
52 const base::ListValue
& list
) {
58 bool ParseAndHandle1(const base::Callback
<void(A1
)>& handler
,
59 const base::ListValue
& list
) {
60 if (list
.GetSize() != 1)
62 Argument
<A1
> arg1(list
, 0);
65 handler
.Run(arg1
.value());
69 template <class A1
, class A2
>
70 bool ParseAndHandle2(const base::Callback
<void(A1
, A2
)>& handler
,
71 const base::ListValue
& list
) {
72 if (list
.GetSize() != 2)
74 Argument
<A1
> arg1(list
, 0);
77 Argument
<A2
> arg2(list
, 1);
80 handler
.Run(arg1
.value(), arg2
.value());
84 template <class A1
, class A2
, class A3
>
85 bool ParseAndHandle3(const base::Callback
<void(A1
, A2
, A3
)>& handler
,
86 const base::ListValue
& list
) {
87 if (list
.GetSize() != 3)
89 Argument
<A1
> arg1(list
, 0);
92 Argument
<A2
> arg2(list
, 1);
95 Argument
<A3
> arg3(list
, 2);
98 handler
.Run(arg1
.value(), arg2
.value(), arg3
.value());
102 template <class A1
, class A2
, class A3
, class A4
>
103 bool ParseAndHandle3(const base::Callback
<void(A1
, A2
, A3
, A4
)>& handler
,
104 const base::ListValue
& list
) {
105 if (list
.GetSize() != 4)
107 Argument
<A1
> arg1(list
, 0);
110 Argument
<A2
> arg2(list
, 1);
113 Argument
<A3
> arg3(list
, 2);
116 Argument
<A4
> arg4(list
, 3);
119 handler
.Run(arg1
.value(), arg2
.value(), arg3
.value(), arg4
.value());
123 typedef base::Callback
<bool(const base::ListValue
&)> ListValueParser
;
125 ListValueParser
BindToListParser(const base::Callback
<void()>& handler
) {
126 return base::Bind(&ParseAndHandle0
, handler
);
130 ListValueParser
BindToListParser(const base::Callback
<void(A1
)>& handler
) {
131 return base::Bind(&ParseAndHandle1
<A1
>, handler
);
134 template <class A1
, class A2
>
135 ListValueParser
BindToListParser(const base::Callback
<void(A1
,A2
)>& handler
) {
136 return base::Bind(&ParseAndHandle2
<A1
, A2
>, handler
);
139 template <class A1
, class A2
, class A3
>
140 ListValueParser
BindToListParser(
141 const base::Callback
<void(A1
,A2
,A3
)>& handler
) {
142 return base::Bind(&ParseAndHandle3
<A1
, A2
, A3
>, handler
);
145 template <class A1
, class A2
, class A3
, class A4
>
146 ListValueParser
BindToListParser(
147 const base::Callback
<void(A1
,A2
,A3
,A4
)>& handler
) {
148 return base::Bind(&ParseAndHandle3
<A1
, A2
, A3
, A4
>, handler
);
153 DevToolsEmbedderMessageDispatcher::DevToolsEmbedderMessageDispatcher(
154 Delegate
* delegate
) {
155 RegisterHandler("bringToFront",
156 BindToListParser(base::Bind(&Delegate::ActivateWindow
,
157 base::Unretained(delegate
))));
158 RegisterHandler("closeWindow",
159 BindToListParser(base::Bind(&Delegate::CloseWindow
,
160 base::Unretained(delegate
))));
161 RegisterHandler("setContentsInsets",
162 BindToListParser(base::Bind(&Delegate::SetContentsInsets
,
163 base::Unretained(delegate
))));
164 RegisterHandler("moveWindowBy",
165 BindToListParser(base::Bind(&Delegate::MoveWindow
,
166 base::Unretained(delegate
))));
167 RegisterHandler("requestSetDockSide",
168 BindToListParser(base::Bind(&Delegate::SetDockSide
,
169 base::Unretained(delegate
))));
170 RegisterHandler("setIsDocked",
171 BindToListParser(base::Bind(&Delegate::SetIsDocked
,
172 base::Unretained(delegate
))));
173 RegisterHandler("openInNewTab",
174 BindToListParser(base::Bind(&Delegate::OpenInNewTab
,
175 base::Unretained(delegate
))));
176 RegisterHandler("save",
177 BindToListParser(base::Bind(&Delegate::SaveToFile
,
178 base::Unretained(delegate
))));
179 RegisterHandler("append",
180 BindToListParser(base::Bind(&Delegate::AppendToFile
,
181 base::Unretained(delegate
))));
182 RegisterHandler("requestFileSystems",
183 BindToListParser(base::Bind(&Delegate::RequestFileSystems
,
184 base::Unretained(delegate
))));
185 RegisterHandler("addFileSystem",
186 BindToListParser(base::Bind(&Delegate::AddFileSystem
,
187 base::Unretained(delegate
))));
188 RegisterHandler("removeFileSystem",
189 BindToListParser(base::Bind(&Delegate::RemoveFileSystem
,
190 base::Unretained(delegate
))));
191 RegisterHandler("upgradeDraggedFileSystemPermissions",
193 base::Bind(&Delegate::UpgradeDraggedFileSystemPermissions
,
194 base::Unretained(delegate
))));
195 RegisterHandler("indexPath",
196 BindToListParser(base::Bind(&Delegate::IndexPath
,
197 base::Unretained(delegate
))));
198 RegisterHandler("stopIndexing",
199 BindToListParser(base::Bind(&Delegate::StopIndexing
,
200 base::Unretained(delegate
))));
201 RegisterHandler("searchInPath",
202 BindToListParser(base::Bind(&Delegate::SearchInPath
,
203 base::Unretained(delegate
))));
206 DevToolsEmbedderMessageDispatcher::~DevToolsEmbedderMessageDispatcher() {}
208 std::string
DevToolsEmbedderMessageDispatcher::Dispatch(
209 const std::string
& method
, base::ListValue
* params
) {
210 HandlerMap::iterator it
= handlers_
.find(method
);
211 if (it
== handlers_
.end())
212 return "Unsupported frontend host method: " + method
;
214 if (!it
->second
.Run(*params
))
215 return "Invalid frontend host message parameters: " + method
;
219 void DevToolsEmbedderMessageDispatcher::RegisterHandler(
220 const std::string
& method
, const Handler
& handler
) {
221 handlers_
[method
] = handler
;