Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / devtools / devtools_embedder_message_dispatcher.cc
blob1d676177ac8036f13c254099f38bc7d2b99e517f
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("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",
192 BindToListParser(
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;
216 return "";
219 void DevToolsEmbedderMessageDispatcher::RegisterHandler(
220 const std::string& method, const Handler& handler) {
221 handlers_[method] = handler;