Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / chrome / browser / devtools / devtools_embedder_message_dispatcher.cc
blob490c258a96cc87e1e0f0ebeb4de3300ff96c6ddb
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 if (it == list.end())
69 return false;
70 if (!GetValue(*it, &head))
71 return false;
72 return tail.Parse(list, it + 1);
75 template <typename H, typename... As>
76 void Apply(const H& handler, As... args) {
77 tail.template Apply<H, As..., T>(handler, args..., head);
80 typename StorageTraits<T>::StorageType head;
81 ParamTuple<Ts...> tail;
84 template<typename... As>
85 bool ParseAndHandle(const base::Callback<void(As...)>& handler,
86 const base::ListValue& list) {
87 ParamTuple<As...> tuple;
88 if (!tuple.Parse(list, list.begin()))
89 return false;
90 tuple.Apply(handler);
91 return true;
94 } // namespace
96 /**
97 * Dispatcher for messages sent from the frontend running in an
98 * isolated renderer (chrome-devtools:// or chrome://inspect) to the embedder
99 * in the browser.
101 * The messages are sent via InspectorFrontendHost.sendMessageToEmbedder or
102 * chrome.send method accordingly.
104 class DispatcherImpl : public DevToolsEmbedderMessageDispatcher {
105 public:
106 ~DispatcherImpl() override {}
108 bool Dispatch(const std::string& method,
109 const base::ListValue* params,
110 std::string* error) override {
111 HandlerMap::iterator it = handlers_.find(method);
112 if (it == handlers_.end())
113 return false;
115 if (it->second.Run(*params))
116 return true;
118 if (error)
119 *error = "Invalid frontend host message parameters: " + method;
120 return false;
123 typedef base::Callback<bool(const base::ListValue&)> Handler;
124 void RegisterHandler(const std::string& method, const Handler& handler) {
125 handlers_[method] = handler;
128 template<typename T, typename... As>
129 void RegisterHandler(const std::string& method,
130 void (T::*handler)(As...), T* delegate) {
131 handlers_[method] = base::Bind(&ParseAndHandle<As...>,
132 base::Bind(handler,
133 base::Unretained(delegate)));
136 private:
137 typedef std::map<std::string, Handler> HandlerMap;
138 HandlerMap handlers_;
142 DevToolsEmbedderMessageDispatcher*
143 DevToolsEmbedderMessageDispatcher::createForDevToolsFrontend(
144 Delegate* delegate) {
145 DispatcherImpl* d = new DispatcherImpl();
147 d->RegisterHandler("bringToFront", &Delegate::ActivateWindow, delegate);
148 d->RegisterHandler("closeWindow", &Delegate::CloseWindow, delegate);
149 d->RegisterHandler("loadCompleted", &Delegate::LoadCompleted, delegate);
150 d->RegisterHandler("setInspectedPageBounds",
151 &Delegate::SetInspectedPageBounds, delegate);
152 d->RegisterHandler("inspectElementCompleted",
153 &Delegate::InspectElementCompleted, delegate);
154 d->RegisterHandler("inspectedURLChanged",
155 &Delegate::InspectedURLChanged, delegate);
156 d->RegisterHandler("setIsDocked", &Delegate::SetIsDocked, delegate);
157 d->RegisterHandler("openInNewTab", &Delegate::OpenInNewTab, delegate);
158 d->RegisterHandler("save", &Delegate::SaveToFile, delegate);
159 d->RegisterHandler("append", &Delegate::AppendToFile, delegate);
160 d->RegisterHandler("requestFileSystems",
161 &Delegate::RequestFileSystems, delegate);
162 d->RegisterHandler("addFileSystem", &Delegate::AddFileSystem, delegate);
163 d->RegisterHandler("removeFileSystem", &Delegate::RemoveFileSystem, delegate);
164 d->RegisterHandler("upgradeDraggedFileSystemPermissions",
165 &Delegate::UpgradeDraggedFileSystemPermissions, delegate);
166 d->RegisterHandler("indexPath", &Delegate::IndexPath, delegate);
167 d->RegisterHandler("stopIndexing", &Delegate::StopIndexing, delegate);
168 d->RegisterHandler("searchInPath", &Delegate::SearchInPath, delegate);
169 d->RegisterHandler("setWhitelistedShortcuts",
170 &Delegate::SetWhitelistedShortcuts, delegate);
171 d->RegisterHandler("zoomIn", &Delegate::ZoomIn, delegate);
172 d->RegisterHandler("zoomOut", &Delegate::ZoomOut, delegate);
173 d->RegisterHandler("resetZoom", &Delegate::ResetZoom, delegate);
174 d->RegisterHandler("openUrlOnRemoteDeviceAndInspect",
175 &Delegate::OpenUrlOnRemoteDeviceAndInspect, delegate);
176 d->RegisterHandler("setDeviceCountUpdatesEnabled",
177 &Delegate::SetDeviceCountUpdatesEnabled, delegate);
178 d->RegisterHandler("setDevicesUpdatesEnabled",
179 &Delegate::SetDevicesUpdatesEnabled, delegate);
180 d->RegisterHandler("sendMessageToBrowser",
181 &Delegate::SendMessageToBrowser, delegate);
182 d->RegisterHandler("recordActionUMA",
183 &Delegate::RecordActionUMA, delegate);
184 return d;