Rename desktop_cursor_loader_updater_aurax11.
[chromium-blink-merge.git] / chrome / browser / devtools / devtools_embedder_message_dispatcher.cc
blob1b1567ef295898ff8aff14affd65795354e6ea94
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"
7 #include "base/bind.h"
8 #include "base/values.h"
10 namespace {
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);
24 template <typename T>
25 struct StorageTraits {
26 typedef T StorageType;
29 template <typename T>
30 struct StorageTraits<const T&> {
31 typedef T StorageType;
34 template <class A>
35 class Argument {
36 public:
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_; }
46 private:
47 ValueType value_;
48 bool valid_;
51 bool ParseAndHandle0(const base::Callback<void(void)>& handler,
52 const base::ListValue& list) {
53 handler.Run();
54 return true;
57 template <class A1>
58 bool ParseAndHandle1(const base::Callback<void(A1)>& handler,
59 const base::ListValue& list) {
60 if (list.GetSize() != 1)
61 return false;
62 Argument<A1> arg1(list, 0);
63 if (!arg1.valid())
64 return false;
65 handler.Run(arg1.value());
66 return true;
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)
73 return false;
74 Argument<A1> arg1(list, 0);
75 if (!arg1.valid())
76 return false;
77 Argument<A2> arg2(list, 1);
78 if (!arg2.valid())
79 return false;
80 handler.Run(arg1.value(), arg2.value());
81 return true;
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)
88 return false;
89 Argument<A1> arg1(list, 0);
90 if (!arg1.valid())
91 return false;
92 Argument<A2> arg2(list, 1);
93 if (!arg2.valid())
94 return false;
95 Argument<A3> arg3(list, 2);
96 if (!arg3.valid())
97 return false;
98 handler.Run(arg1.value(), arg2.value(), arg3.value());
99 return true;
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)
106 return false;
107 Argument<A1> arg1(list, 0);
108 if (!arg1.valid())
109 return false;
110 Argument<A2> arg2(list, 1);
111 if (!arg2.valid())
112 return false;
113 Argument<A3> arg3(list, 2);
114 if (!arg3.valid())
115 return false;
116 Argument<A4> arg4(list, 3);
117 if (!arg4.valid())
118 return false;
119 handler.Run(arg1.value(), arg2.value(), arg3.value(), arg4.value());
120 return true;
123 typedef base::Callback<bool(const base::ListValue&)> ListValueParser;
125 ListValueParser BindToListParser(const base::Callback<void()>& handler) {
126 return base::Bind(&ParseAndHandle0, handler);
129 template <class A1>
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);
151 } // namespace
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;
209 return "";
212 void DevToolsEmbedderMessageDispatcher::RegisterHandler(
213 const std::string& method, const Handler& handler) {
214 handlers_[method] = handler;