Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / devtools / devtools_embedder_message_dispatcher.cc
blobab8c80e7c6506570ef0d486ae30c20c433c3889f
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 using DispatchCallback = DevToolsEmbedderMessageDispatcher::DispatchCallback;
14 bool GetValue(const base::Value* value, std::string* result) {
15 return value->GetAsString(result);
18 bool GetValue(const base::Value* value, int* result) {
19 return value->GetAsInteger(result);
22 bool GetValue(const base::Value* value, bool* result) {
23 return value->GetAsBoolean(result);
26 bool GetValue(const base::Value* value, gfx::Rect* rect) {
27 const base::DictionaryValue* dict;
28 if (!value->GetAsDictionary(&dict))
29 return false;
30 int x = 0;
31 int y = 0;
32 int width = 0;
33 int height = 0;
34 if (!dict->GetInteger("x", &x) ||
35 !dict->GetInteger("y", &y) ||
36 !dict->GetInteger("width", &width) ||
37 !dict->GetInteger("height", &height))
38 return false;
39 rect->SetRect(x, y, width, height);
40 return true;
43 template <typename T>
44 struct StorageTraits {
45 using StorageType = T;
48 template <typename T>
49 struct StorageTraits<const T&> {
50 using StorageType = T;
53 // TODO(dgozman): move back to variadic templates once it compiles everywhere.
54 // See http://crbug.com/491973.
56 bool ParseAndHandle0(const base::Callback<void()>& handler,
57 const DispatchCallback& callback,
58 const base::ListValue& list) {
59 if (list.GetSize() != 0)
60 return false;
61 handler.Run();
62 return true;
65 template<class A1>
66 bool ParseAndHandle1(const base::Callback<void(A1)>& handler,
67 const DispatchCallback& callback,
68 const base::ListValue& list) {
69 if (list.GetSize() != 1)
70 return false;
71 const base::Value* value1;
72 list.Get(0, &value1);
73 typename StorageTraits<A1>::StorageType a1;
74 if (!GetValue(value1, &a1))
75 return false;
76 handler.Run(a1);
77 return true;
80 template<class A1, class A2>
81 bool ParseAndHandle2(const base::Callback<void(A1, A2)>& handler,
82 const DispatchCallback& callback,
83 const base::ListValue& list) {
84 if (list.GetSize() != 2)
85 return false;
86 const base::Value* value1;
87 list.Get(0, &value1);
88 typename StorageTraits<A1>::StorageType a1;
89 if (!GetValue(value1, &a1))
90 return false;
91 const base::Value* value2;
92 list.Get(1, &value2);
93 typename StorageTraits<A2>::StorageType a2;
94 if (!GetValue(value2, &a2))
95 return false;
96 handler.Run(a1, a2);
97 return true;
100 template<class A1, class A2, class A3>
101 bool ParseAndHandle3(const base::Callback<void(A1, A2, A3)>& handler,
102 const DispatchCallback& callback,
103 const base::ListValue& list) {
104 if (list.GetSize() != 3)
105 return false;
106 const base::Value* value1;
107 list.Get(0, &value1);
108 typename StorageTraits<A1>::StorageType a1;
109 if (!GetValue(value1, &a1))
110 return false;
111 const base::Value* value2;
112 list.Get(1, &value2);
113 typename StorageTraits<A2>::StorageType a2;
114 if (!GetValue(value2, &a2))
115 return false;
116 const base::Value* value3;
117 list.Get(2, &value3);
118 typename StorageTraits<A3>::StorageType a3;
119 if (!GetValue(value3, &a3))
120 return false;
121 handler.Run(a1, a2, a3);
122 return true;
125 bool ParseAndHandleWithCallback0(
126 const base::Callback<void(const DispatchCallback&)>& handler,
127 const DispatchCallback& callback,
128 const base::ListValue& list) {
129 if (list.GetSize() != 0)
130 return false;
131 handler.Run(callback);
132 return true;
135 template<class A1>
136 bool ParseAndHandleWithCallback1(
137 const base::Callback<void(const DispatchCallback&, A1)>& handler,
138 const DispatchCallback& callback,
139 const base::ListValue& list) {
140 if (list.GetSize() != 1)
141 return false;
142 const base::Value* value1;
143 list.Get(0, &value1);
144 typename StorageTraits<A1>::StorageType a1;
145 if (!GetValue(value1, &a1))
146 return false;
147 handler.Run(callback, a1);
148 return true;
151 template<class A1, class A2>
152 bool ParseAndHandleWithCallback2(
153 const base::Callback<void(const DispatchCallback&, A1, A2)>& handler,
154 const DispatchCallback& callback,
155 const base::ListValue& list) {
156 if (list.GetSize() != 2)
157 return false;
158 const base::Value* value1;
159 list.Get(0, &value1);
160 typename StorageTraits<A1>::StorageType a1;
161 if (!GetValue(value1, &a1))
162 return false;
163 const base::Value* value2;
164 list.Get(1, &value2);
165 typename StorageTraits<A2>::StorageType a2;
166 if (!GetValue(value2, &a2))
167 return false;
168 handler.Run(callback, a1, a2);
169 return true;
172 template<class A1, class A2, class A3>
173 bool ParseAndHandleWithCallback3(
174 const base::Callback<void(const DispatchCallback&, A1, A2, A3)>& handler,
175 const DispatchCallback& callback,
176 const base::ListValue& list) {
177 if (list.GetSize() != 3)
178 return false;
179 const base::Value* value1;
180 list.Get(0, &value1);
181 typename StorageTraits<A1>::StorageType a1;
182 if (!GetValue(value1, &a1))
183 return false;
184 const base::Value* value2;
185 list.Get(1, &value2);
186 typename StorageTraits<A2>::StorageType a2;
187 if (!GetValue(value2, &a2))
188 return false;
189 const base::Value* value3;
190 list.Get(2, &value3);
191 typename StorageTraits<A3>::StorageType a3;
192 if (!GetValue(value3, &a3))
193 return false;
194 handler.Run(callback, a1, a2, a3);
195 return true;
198 } // namespace
201 * Dispatcher for messages sent from the frontend running in an
202 * isolated renderer (chrome-devtools:// or chrome://inspect) to the embedder
203 * in the browser.
205 * The messages are sent via InspectorFrontendHost.sendMessageToEmbedder or
206 * chrome.send method accordingly.
208 class DispatcherImpl : public DevToolsEmbedderMessageDispatcher {
209 public:
210 ~DispatcherImpl() override {}
212 bool Dispatch(const DispatchCallback& callback,
213 const std::string& method,
214 const base::ListValue* params) override {
215 HandlerMap::iterator it = handlers_.find(method);
216 return it != handlers_.end() && it->second.Run(callback, *params);
219 void RegisterHandler(const std::string& method,
220 void (Delegate::*handler)(),
221 Delegate* delegate) {
222 handlers_[method] = base::Bind(&ParseAndHandle0,
223 base::Bind(handler,
224 base::Unretained(delegate)));
227 template<class A1>
228 void RegisterHandler(const std::string& method,
229 void (Delegate::*handler)(A1),
230 Delegate* delegate) {
231 handlers_[method] = base::Bind(&ParseAndHandle1<A1>,
232 base::Bind(handler,
233 base::Unretained(delegate)));
236 template<class A1, class A2>
237 void RegisterHandler(const std::string& method,
238 void (Delegate::*handler)(A1, A2),
239 Delegate* delegate) {
240 handlers_[method] = base::Bind(&ParseAndHandle2<A1, A2>,
241 base::Bind(handler,
242 base::Unretained(delegate)));
245 template<class A1, class A2, class A3>
246 void RegisterHandler(const std::string& method,
247 void (Delegate::*handler)(A1, A2, A3),
248 Delegate* delegate) {
249 handlers_[method] = base::Bind(&ParseAndHandle3<A1, A2, A3>,
250 base::Bind(handler,
251 base::Unretained(delegate)));
254 void RegisterHandlerWithCallback(
255 const std::string& method,
256 void (Delegate::*handler)(const DispatchCallback&),
257 Delegate* delegate) {
258 handlers_[method] = base::Bind(&ParseAndHandleWithCallback0,
259 base::Bind(handler,
260 base::Unretained(delegate)));
263 template<class A1>
264 void RegisterHandlerWithCallback(
265 const std::string& method,
266 void (Delegate::*handler)(const DispatchCallback&, A1),
267 Delegate* delegate) {
268 handlers_[method] = base::Bind(&ParseAndHandleWithCallback1<A1>,
269 base::Bind(handler,
270 base::Unretained(delegate)));
273 template<class A1, class A2>
274 void RegisterHandlerWithCallback(
275 const std::string& method,
276 void (Delegate::*handler)(const DispatchCallback&, A1, A2),
277 Delegate* delegate) {
278 handlers_[method] = base::Bind(&ParseAndHandleWithCallback2<A1, A2>,
279 base::Bind(handler,
280 base::Unretained(delegate)));
283 template<class A1, class A2, class A3>
284 void RegisterHandlerWithCallback(
285 const std::string& method,
286 void (Delegate::*handler)(const DispatchCallback&, A1, A2, A3),
287 Delegate* delegate) {
288 handlers_[method] = base::Bind(&ParseAndHandleWithCallback3<A1, A2, A3>,
289 base::Bind(handler,
290 base::Unretained(delegate)));
293 private:
294 using Handler = base::Callback<bool(const DispatchCallback&,
295 const base::ListValue&)>;
296 using HandlerMap = std::map<std::string, Handler>;
297 HandlerMap handlers_;
300 // static
301 DevToolsEmbedderMessageDispatcher*
302 DevToolsEmbedderMessageDispatcher::CreateForDevToolsFrontend(
303 Delegate* delegate) {
304 DispatcherImpl* d = new DispatcherImpl();
306 d->RegisterHandler("bringToFront", &Delegate::ActivateWindow, delegate);
307 d->RegisterHandler("closeWindow", &Delegate::CloseWindow, delegate);
308 d->RegisterHandler("loadCompleted", &Delegate::LoadCompleted, delegate);
309 d->RegisterHandler("setInspectedPageBounds",
310 &Delegate::SetInspectedPageBounds, delegate);
311 d->RegisterHandler("inspectElementCompleted",
312 &Delegate::InspectElementCompleted, delegate);
313 d->RegisterHandler("inspectedURLChanged",
314 &Delegate::InspectedURLChanged, delegate);
315 d->RegisterHandlerWithCallback("setIsDocked",
316 &Delegate::SetIsDocked, delegate);
317 d->RegisterHandler("openInNewTab", &Delegate::OpenInNewTab, delegate);
318 d->RegisterHandler("save", &Delegate::SaveToFile, delegate);
319 d->RegisterHandler("append", &Delegate::AppendToFile, delegate);
320 d->RegisterHandler("requestFileSystems",
321 &Delegate::RequestFileSystems, delegate);
322 d->RegisterHandler("addFileSystem", &Delegate::AddFileSystem, delegate);
323 d->RegisterHandler("removeFileSystem", &Delegate::RemoveFileSystem, delegate);
324 d->RegisterHandler("upgradeDraggedFileSystemPermissions",
325 &Delegate::UpgradeDraggedFileSystemPermissions, delegate);
326 d->RegisterHandler("indexPath", &Delegate::IndexPath, delegate);
327 d->RegisterHandlerWithCallback("loadNetworkResource",
328 &Delegate::LoadNetworkResource, delegate);
329 d->RegisterHandler("stopIndexing", &Delegate::StopIndexing, delegate);
330 d->RegisterHandler("searchInPath", &Delegate::SearchInPath, delegate);
331 d->RegisterHandler("setWhitelistedShortcuts",
332 &Delegate::SetWhitelistedShortcuts, delegate);
333 d->RegisterHandler("zoomIn", &Delegate::ZoomIn, delegate);
334 d->RegisterHandler("zoomOut", &Delegate::ZoomOut, delegate);
335 d->RegisterHandler("resetZoom", &Delegate::ResetZoom, delegate);
336 d->RegisterHandler("setDevicesUpdatesEnabled",
337 &Delegate::SetDevicesUpdatesEnabled, delegate);
338 d->RegisterHandler("sendMessageToBrowser",
339 &Delegate::SendMessageToBrowser, delegate);
340 d->RegisterHandler("recordEnumeratedHistogram",
341 &Delegate::RecordEnumeratedHistogram, delegate);
342 d->RegisterHandlerWithCallback("sendJsonRequest",
343 &Delegate::SendJsonRequest, delegate);
344 d->RegisterHandlerWithCallback("getPreferences",
345 &Delegate::GetPreferences, delegate);
346 d->RegisterHandler("setPreference",
347 &Delegate::SetPreference, delegate);
348 d->RegisterHandler("removePreference",
349 &Delegate::RemovePreference, delegate);
350 d->RegisterHandler("clearPreferences",
351 &Delegate::ClearPreferences, delegate);
352 return d;