ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / devtools / devtools_embedder_message_dispatcher.cc
blob73187c9d271709a0067485bfdbadac81cc03fe7c
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::Value* value, std::string* result) {
13 return value->GetAsString(result);
16 bool GetValue(const base::Value* value, int* result) {
17 return value->GetAsInteger(result);
20 bool GetValue(const base::Value* value, bool* result) {
21 return value->GetAsBoolean(result);
24 bool GetValue(const base::Value* value, gfx::Rect* rect) {
25 const base::DictionaryValue* dict;
26 if (!value->GetAsDictionary(&dict))
27 return false;
28 int x = 0;
29 int y = 0;
30 int width = 0;
31 int height = 0;
32 if (!dict->GetInteger("x", &x) ||
33 !dict->GetInteger("y", &y) ||
34 !dict->GetInteger("width", &width) ||
35 !dict->GetInteger("height", &height))
36 return false;
37 rect->SetRect(x, y, width, height);
38 return true;
41 template <typename T>
42 struct StorageTraits {
43 using StorageType = T;
46 template <typename T>
47 struct StorageTraits<const T&> {
48 using StorageType = T;
51 template <typename... Ts>
52 struct ParamTuple {
53 bool Parse(const base::ListValue& list,
54 const base::ListValue::const_iterator& it) {
55 return it == list.end();
58 template <typename H, typename... As>
59 void Apply(const H& handler, As... args) {
60 handler.Run(args...);
64 template <typename T, typename... Ts>
65 struct ParamTuple<T, Ts...> {
66 bool Parse(const base::ListValue& list,
67 const base::ListValue::const_iterator& it) {
68 return it != list.end() && GetValue(*it, &head) && tail.Parse(list, it + 1);
71 template <typename H, typename... As>
72 void Apply(const H& handler, As... args) {
73 tail.template Apply<H, As..., T>(handler, args..., head);
76 typename StorageTraits<T>::StorageType head;
77 ParamTuple<Ts...> tail;
80 template<typename... As>
81 bool ParseAndHandle(const base::Callback<void(int, As...)>& handler,
82 int request_id,
83 const base::ListValue& list) {
84 ParamTuple<As...> tuple;
85 if (!tuple.Parse(list, list.begin()))
86 return false;
87 tuple.Apply(handler, request_id);
88 return true;
91 } // namespace
93 /**
94 * Dispatcher for messages sent from the frontend running in an
95 * isolated renderer (chrome-devtools:// or chrome://inspect) to the embedder
96 * in the browser.
98 * The messages are sent via InspectorFrontendHost.sendMessageToEmbedder or
99 * chrome.send method accordingly.
101 class DispatcherImpl : public DevToolsEmbedderMessageDispatcher {
102 public:
103 ~DispatcherImpl() override {}
105 bool Dispatch(int request_id,
106 const std::string& method,
107 const base::ListValue* params) override {
108 HandlerMap::iterator it = handlers_.find(method);
109 return it != handlers_.end() && it->second.Run(request_id, *params);
112 template<typename T, typename... As>
113 void RegisterHandler(const std::string& method,
114 void (T::*handler)(int, As...), T* delegate) {
115 handlers_[method] = base::Bind(&ParseAndHandle<As...>,
116 base::Bind(handler,
117 base::Unretained(delegate)));
120 private:
121 using Handler = base::Callback<bool(int, const base::ListValue&)>;
122 using HandlerMap = std::map<std::string, Handler>;
123 HandlerMap handlers_;
126 // static
127 DevToolsEmbedderMessageDispatcher*
128 DevToolsEmbedderMessageDispatcher::CreateForDevToolsFrontend(
129 Delegate* delegate) {
130 DispatcherImpl* d = new DispatcherImpl();
132 d->RegisterHandler("bringToFront", &Delegate::ActivateWindow, delegate);
133 d->RegisterHandler("closeWindow", &Delegate::CloseWindow, delegate);
134 d->RegisterHandler("loadCompleted", &Delegate::LoadCompleted, delegate);
135 d->RegisterHandler("setInspectedPageBounds",
136 &Delegate::SetInspectedPageBounds, delegate);
137 d->RegisterHandler("inspectElementCompleted",
138 &Delegate::InspectElementCompleted, delegate);
139 d->RegisterHandler("inspectedURLChanged",
140 &Delegate::InspectedURLChanged, delegate);
141 d->RegisterHandler("setIsDocked", &Delegate::SetIsDocked, delegate);
142 d->RegisterHandler("openInNewTab", &Delegate::OpenInNewTab, delegate);
143 d->RegisterHandler("save", &Delegate::SaveToFile, delegate);
144 d->RegisterHandler("append", &Delegate::AppendToFile, delegate);
145 d->RegisterHandler("requestFileSystems",
146 &Delegate::RequestFileSystems, delegate);
147 d->RegisterHandler("addFileSystem", &Delegate::AddFileSystem, delegate);
148 d->RegisterHandler("removeFileSystem", &Delegate::RemoveFileSystem, delegate);
149 d->RegisterHandler("upgradeDraggedFileSystemPermissions",
150 &Delegate::UpgradeDraggedFileSystemPermissions, delegate);
151 d->RegisterHandler("indexPath", &Delegate::IndexPath, delegate);
152 d->RegisterHandler("loadNetworkResource",
153 &Delegate::LoadNetworkResource, delegate);
154 d->RegisterHandler("stopIndexing", &Delegate::StopIndexing, delegate);
155 d->RegisterHandler("searchInPath", &Delegate::SearchInPath, delegate);
156 d->RegisterHandler("setWhitelistedShortcuts",
157 &Delegate::SetWhitelistedShortcuts, delegate);
158 d->RegisterHandler("zoomIn", &Delegate::ZoomIn, delegate);
159 d->RegisterHandler("zoomOut", &Delegate::ZoomOut, delegate);
160 d->RegisterHandler("resetZoom", &Delegate::ResetZoom, delegate);
161 d->RegisterHandler("openUrlOnRemoteDeviceAndInspect",
162 &Delegate::OpenUrlOnRemoteDeviceAndInspect, delegate);
163 d->RegisterHandler("setDeviceCountUpdatesEnabled",
164 &Delegate::SetDeviceCountUpdatesEnabled, delegate);
165 d->RegisterHandler("setDevicesUpdatesEnabled",
166 &Delegate::SetDevicesUpdatesEnabled, delegate);
167 d->RegisterHandler("sendMessageToBrowser",
168 &Delegate::SendMessageToBrowser, delegate);
169 d->RegisterHandler("recordActionUMA", &Delegate::RecordActionUMA, delegate);
170 return d;