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("setWindowBounds",
162 BindToListParser(base::Bind(&Delegate::SetWindowBounds
,
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("openInNewTab",
171 BindToListParser(base::Bind(&Delegate::OpenInNewTab
,
172 base::Unretained(delegate
))));
173 RegisterHandler("save",
174 BindToListParser(base::Bind(&Delegate::SaveToFile
,
175 base::Unretained(delegate
))));
176 RegisterHandler("append",
177 BindToListParser(base::Bind(&Delegate::AppendToFile
,
178 base::Unretained(delegate
))));
179 RegisterHandler("requestFileSystems",
180 BindToListParser(base::Bind(&Delegate::RequestFileSystems
,
181 base::Unretained(delegate
))));
182 RegisterHandler("addFileSystem",
183 BindToListParser(base::Bind(&Delegate::AddFileSystem
,
184 base::Unretained(delegate
))));
185 RegisterHandler("removeFileSystem",
186 BindToListParser(base::Bind(&Delegate::RemoveFileSystem
,
187 base::Unretained(delegate
))));
188 RegisterHandler("indexPath",
189 BindToListParser(base::Bind(&Delegate::IndexPath
,
190 base::Unretained(delegate
))));
191 RegisterHandler("stopIndexing",
192 BindToListParser(base::Bind(&Delegate::StopIndexing
,
193 base::Unretained(delegate
))));
194 RegisterHandler("searchInPath",
195 BindToListParser(base::Bind(&Delegate::SearchInPath
,
196 base::Unretained(delegate
))));
199 DevToolsEmbedderMessageDispatcher::~DevToolsEmbedderMessageDispatcher() {}
201 std::string
DevToolsEmbedderMessageDispatcher::Dispatch(
202 const std::string
& method
, base::ListValue
* params
) {
203 HandlerMap::iterator it
= handlers_
.find(method
);
204 if (it
== handlers_
.end())
205 return "Unsupported frontend host method: " + method
;
207 if (!it
->second
.Run(*params
))
208 return "Invalid frontend host message parameters: " + method
;
212 void DevToolsEmbedderMessageDispatcher::RegisterHandler(
213 const std::string
& method
, const Handler
& handler
) {
214 handlers_
[method
] = handler
;