Popular sites on the NTP: re-download popular suggestions once per Chrome run
[chromium-blink-merge.git] / chrome / browser / devtools / devtools_embedder_message_dispatcher.cc
blob2a45c02480becef839c5b2d971ac718db43a1863
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 template <typename... Ts>
54 struct ParamTuple {
55 bool Parse(const base::ListValue& list,
56 const base::ListValue::const_iterator& it) {
57 return it == list.end();
60 template <typename H, typename... As>
61 void Apply(const H& handler, As... args) {
62 handler.Run(args...);
66 template <typename T, typename... Ts>
67 struct ParamTuple<T, Ts...> {
68 bool Parse(const base::ListValue& list,
69 const base::ListValue::const_iterator& it) {
70 return it != list.end() && GetValue(*it, &head) && tail.Parse(list, it + 1);
73 template <typename H, typename... As>
74 void Apply(const H& handler, As... args) {
75 tail.template Apply<H, As..., T>(handler, args..., head);
78 typename StorageTraits<T>::StorageType head;
79 ParamTuple<Ts...> tail;
82 template<typename... As>
83 bool ParseAndHandle(const base::Callback<void(As...)>& handler,
84 const DispatchCallback& callback,
85 const base::ListValue& list) {
86 ParamTuple<As...> tuple;
87 if (!tuple.Parse(list, list.begin()))
88 return false;
89 tuple.Apply(handler);
90 return true;
93 template<typename... As>
94 bool ParseAndHandleWithCallback(
95 const base::Callback<void(const DispatchCallback&, As...)>& handler,
96 const DispatchCallback& callback,
97 const base::ListValue& list) {
98 ParamTuple<As...> tuple;
99 if (!tuple.Parse(list, list.begin()))
100 return false;
101 tuple.Apply(handler, callback);
102 return true;
105 } // namespace
108 * Dispatcher for messages sent from the frontend running in an
109 * isolated renderer (chrome-devtools:// or chrome://inspect) to the embedder
110 * in the browser.
112 * The messages are sent via InspectorFrontendHost.sendMessageToEmbedder or
113 * chrome.send method accordingly.
115 class DispatcherImpl : public DevToolsEmbedderMessageDispatcher {
116 public:
117 ~DispatcherImpl() override {}
119 bool Dispatch(const DispatchCallback& callback,
120 const std::string& method,
121 const base::ListValue* params) override {
122 HandlerMap::iterator it = handlers_.find(method);
123 return it != handlers_.end() && it->second.Run(callback, *params);
126 template<typename... As>
127 void RegisterHandler(const std::string& method,
128 void (Delegate::*handler)(As...),
129 Delegate* delegate) {
130 handlers_[method] = base::Bind(&ParseAndHandle<As...>,
131 base::Bind(handler,
132 base::Unretained(delegate)));
135 template<typename... As>
136 void RegisterHandlerWithCallback(
137 const std::string& method,
138 void (Delegate::*handler)(const DispatchCallback&, As...),
139 Delegate* delegate) {
140 handlers_[method] = base::Bind(&ParseAndHandleWithCallback<As...>,
141 base::Bind(handler,
142 base::Unretained(delegate)));
146 private:
147 using Handler = base::Callback<bool(const DispatchCallback&,
148 const base::ListValue&)>;
149 using HandlerMap = std::map<std::string, Handler>;
150 HandlerMap handlers_;
153 // static
154 DevToolsEmbedderMessageDispatcher*
155 DevToolsEmbedderMessageDispatcher::CreateForDevToolsFrontend(
156 Delegate* delegate) {
157 DispatcherImpl* d = new DispatcherImpl();
159 d->RegisterHandler("bringToFront", &Delegate::ActivateWindow, delegate);
160 d->RegisterHandler("closeWindow", &Delegate::CloseWindow, delegate);
161 d->RegisterHandler("loadCompleted", &Delegate::LoadCompleted, delegate);
162 d->RegisterHandler("setInspectedPageBounds",
163 &Delegate::SetInspectedPageBounds, delegate);
164 d->RegisterHandler("inspectElementCompleted",
165 &Delegate::InspectElementCompleted, delegate);
166 d->RegisterHandler("inspectedURLChanged",
167 &Delegate::InspectedURLChanged, delegate);
168 d->RegisterHandlerWithCallback("setIsDocked",
169 &Delegate::SetIsDocked, delegate);
170 d->RegisterHandler("openInNewTab", &Delegate::OpenInNewTab, delegate);
171 d->RegisterHandler("save", &Delegate::SaveToFile, delegate);
172 d->RegisterHandler("append", &Delegate::AppendToFile, delegate);
173 d->RegisterHandler("requestFileSystems",
174 &Delegate::RequestFileSystems, delegate);
175 d->RegisterHandler("addFileSystem", &Delegate::AddFileSystem, delegate);
176 d->RegisterHandler("removeFileSystem", &Delegate::RemoveFileSystem, delegate);
177 d->RegisterHandler("upgradeDraggedFileSystemPermissions",
178 &Delegate::UpgradeDraggedFileSystemPermissions, delegate);
179 d->RegisterHandler("indexPath", &Delegate::IndexPath, delegate);
180 d->RegisterHandlerWithCallback("loadNetworkResource",
181 &Delegate::LoadNetworkResource, delegate);
182 d->RegisterHandler("stopIndexing", &Delegate::StopIndexing, delegate);
183 d->RegisterHandler("searchInPath", &Delegate::SearchInPath, delegate);
184 d->RegisterHandler("setWhitelistedShortcuts",
185 &Delegate::SetWhitelistedShortcuts, delegate);
186 d->RegisterHandler("zoomIn", &Delegate::ZoomIn, delegate);
187 d->RegisterHandler("zoomOut", &Delegate::ZoomOut, delegate);
188 d->RegisterHandler("resetZoom", &Delegate::ResetZoom, delegate);
189 d->RegisterHandler("setDevicesDiscoveryConfig",
190 &Delegate::SetDevicesDiscoveryConfig, delegate);
191 d->RegisterHandler("setDevicesUpdatesEnabled",
192 &Delegate::SetDevicesUpdatesEnabled, delegate);
193 d->RegisterHandler("performActionOnRemotePage",
194 &Delegate::PerformActionOnRemotePage, delegate);
195 d->RegisterHandler("sendMessageToBrowser",
196 &Delegate::SendMessageToBrowser, delegate);
197 d->RegisterHandler("recordEnumeratedHistogram",
198 &Delegate::RecordEnumeratedHistogram, delegate);
199 d->RegisterHandlerWithCallback("sendJsonRequest",
200 &Delegate::SendJsonRequest, delegate);
201 d->RegisterHandler("sendFrontendAPINotification",
202 &Delegate::SendFrontendAPINotification, delegate);
203 d->RegisterHandlerWithCallback("getPreferences",
204 &Delegate::GetPreferences, delegate);
205 d->RegisterHandler("setPreference",
206 &Delegate::SetPreference, delegate);
207 d->RegisterHandler("removePreference",
208 &Delegate::RemovePreference, delegate);
209 d->RegisterHandler("clearPreferences",
210 &Delegate::ClearPreferences, delegate);
211 return d;