electron/brightray/browser/devtools_embedder_message_dispatcher.cc

209 lines
7.2 KiB
C++
Raw Normal View History

// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE-CHROMIUM file.
#include "browser/devtools_embedder_message_dispatcher.h"
#include "base/bind.h"
#include "base/values.h"
namespace brightray {
namespace {
2015-06-05 03:03:47 +00:00
using DispatchCallback = DevToolsEmbedderMessageDispatcher::DispatchCallback;
2016-09-06 08:22:52 +00:00
bool GetValue(const base::Value& value, std::string* result) {
return value.GetAsString(result);
}
2016-09-06 08:22:52 +00:00
bool GetValue(const base::Value& value, int* result) {
return value.GetAsInteger(result);
}
2016-09-06 08:22:52 +00:00
bool GetValue(const base::Value& value, bool* result) {
return value.GetAsBoolean(result);
}
2016-09-06 08:22:52 +00:00
bool GetValue(const base::Value& value, gfx::Rect* rect) {
const base::DictionaryValue* dict;
2016-09-06 08:22:52 +00:00
if (!value.GetAsDictionary(&dict))
return false;
int x = 0;
int y = 0;
int width = 0;
int height = 0;
if (!dict->GetInteger("x", &x) ||
!dict->GetInteger("y", &y) ||
!dict->GetInteger("width", &width) ||
!dict->GetInteger("height", &height))
return false;
2015-06-05 03:03:47 +00:00
rect->SetRect(x, y, width, height);
return true;
}
template <typename T>
struct StorageTraits {
2015-06-05 03:03:47 +00:00
using StorageType = T;
};
template <typename T>
struct StorageTraits<const T&> {
2015-06-05 03:03:47 +00:00
using StorageType = T;
};
2015-06-05 03:03:47 +00:00
template <typename... Ts>
struct ParamTuple {
bool Parse(const base::ListValue& list,
const base::ListValue::const_iterator& it) {
return it == list.end();
}
2015-06-05 03:03:47 +00:00
template <typename H, typename... As>
void Apply(const H& handler, As... args) {
handler.Run(args...);
}
};
2015-06-05 03:03:47 +00:00
template <typename T, typename... Ts>
struct ParamTuple<T, Ts...> {
bool Parse(const base::ListValue& list,
const base::ListValue::const_iterator& it) {
2016-09-06 08:22:52 +00:00
return it != list.end() && GetValue(**it, &head) &&
tail.Parse(list, it + 1);
2015-06-05 03:03:47 +00:00
}
2015-06-05 03:03:47 +00:00
template <typename H, typename... As>
void Apply(const H& handler, As... args) {
tail.template Apply<H, As..., T>(handler, args..., head);
}
2015-06-05 03:03:47 +00:00
typename StorageTraits<T>::StorageType head;
ParamTuple<Ts...> tail;
};
2015-06-05 03:03:47 +00:00
template<typename... As>
bool ParseAndHandle(const base::Callback<void(As...)>& handler,
const DispatchCallback& callback,
const base::ListValue& list) {
ParamTuple<As...> tuple;
if (!tuple.Parse(list, list.begin()))
return false;
2015-06-05 03:03:47 +00:00
tuple.Apply(handler);
return true;
}
2015-06-05 03:03:47 +00:00
template<typename... As>
bool ParseAndHandleWithCallback(
const base::Callback<void(const DispatchCallback&, As...)>& handler,
const DispatchCallback& callback,
const base::ListValue& list) {
ParamTuple<As...> tuple;
if (!tuple.Parse(list, list.begin()))
return false;
2015-06-05 03:03:47 +00:00
tuple.Apply(handler, callback);
return true;
}
2015-06-05 04:07:27 +00:00
} // namespace
2015-06-05 03:03:47 +00:00
/**
* Dispatcher for messages sent from the frontend running in an
* isolated renderer (chrome-devtools:// or chrome://inspect) to the embedder
* in the browser.
*
* The messages are sent via InspectorFrontendHost.sendMessageToEmbedder or
* chrome.send method accordingly.
*/
class DispatcherImpl : public DevToolsEmbedderMessageDispatcher {
public:
~DispatcherImpl() override {}
2015-06-05 03:03:47 +00:00
bool Dispatch(const DispatchCallback& callback,
const std::string& method,
const base::ListValue* params) override {
2016-07-10 11:12:33 +00:00
auto it = handlers_.find(method);
2015-06-05 03:03:47 +00:00
return it != handlers_.end() && it->second.Run(callback, *params);
}
2015-06-05 03:03:47 +00:00
template<typename... As>
void RegisterHandler(const std::string& method,
void (Delegate::*handler)(As...),
Delegate* delegate) {
handlers_[method] = base::Bind(&ParseAndHandle<As...>,
base::Bind(handler,
base::Unretained(delegate)));
}
2015-06-05 03:03:47 +00:00
template<typename... As>
void RegisterHandlerWithCallback(
const std::string& method,
void (Delegate::*handler)(const DispatchCallback&, As...),
Delegate* delegate) {
handlers_[method] = base::Bind(&ParseAndHandleWithCallback<As...>,
base::Bind(handler,
base::Unretained(delegate)));
}
2015-06-05 03:03:47 +00:00
private:
using Handler = base::Callback<bool(const DispatchCallback&,
const base::ListValue&)>;
using HandlerMap = std::map<std::string, Handler>;
HandlerMap handlers_;
};
2015-06-05 03:03:47 +00:00
// static
DevToolsEmbedderMessageDispatcher*
DevToolsEmbedderMessageDispatcher::CreateForDevToolsFrontend(
Delegate* delegate) {
2016-07-10 11:12:33 +00:00
auto* d = new DispatcherImpl();
2015-06-05 03:03:47 +00:00
d->RegisterHandler("bringToFront", &Delegate::ActivateWindow, delegate);
d->RegisterHandler("closeWindow", &Delegate::CloseWindow, delegate);
d->RegisterHandler("loadCompleted", &Delegate::LoadCompleted, delegate);
d->RegisterHandler("setInspectedPageBounds",
&Delegate::SetInspectedPageBounds, delegate);
d->RegisterHandler("inspectElementCompleted",
&Delegate::InspectElementCompleted, delegate);
d->RegisterHandler("inspectedURLChanged",
&Delegate::InspectedURLChanged, delegate);
d->RegisterHandlerWithCallback("setIsDocked",
&Delegate::SetIsDocked, delegate);
d->RegisterHandler("openInNewTab", &Delegate::OpenInNewTab, delegate);
d->RegisterHandler("save", &Delegate::SaveToFile, delegate);
d->RegisterHandler("append", &Delegate::AppendToFile, delegate);
d->RegisterHandler("requestFileSystems",
&Delegate::RequestFileSystems, delegate);
d->RegisterHandler("addFileSystem", &Delegate::AddFileSystem, delegate);
d->RegisterHandler("removeFileSystem", &Delegate::RemoveFileSystem, delegate);
d->RegisterHandler("upgradeDraggedFileSystemPermissions",
&Delegate::UpgradeDraggedFileSystemPermissions, delegate);
d->RegisterHandler("indexPath", &Delegate::IndexPath, delegate);
d->RegisterHandlerWithCallback("loadNetworkResource",
&Delegate::LoadNetworkResource, delegate);
d->RegisterHandler("stopIndexing", &Delegate::StopIndexing, delegate);
d->RegisterHandler("searchInPath", &Delegate::SearchInPath, delegate);
d->RegisterHandler("setWhitelistedShortcuts",
&Delegate::SetWhitelistedShortcuts, delegate);
d->RegisterHandler("zoomIn", &Delegate::ZoomIn, delegate);
d->RegisterHandler("zoomOut", &Delegate::ZoomOut, delegate);
d->RegisterHandler("resetZoom", &Delegate::ResetZoom, delegate);
d->RegisterHandler("setDevicesUpdatesEnabled",
&Delegate::SetDevicesUpdatesEnabled, delegate);
2016-03-08 14:28:28 +00:00
d->RegisterHandler("dispatchProtocolMessage",
&Delegate::DispatchProtocolMessageFromDevToolsFrontend,
delegate);
2015-06-05 03:03:47 +00:00
d->RegisterHandler("recordActionUMA", &Delegate::RecordActionUMA, delegate);
d->RegisterHandlerWithCallback("sendJsonRequest",
&Delegate::SendJsonRequest, delegate);
d->RegisterHandlerWithCallback("getPreferences",
&Delegate::GetPreferences, delegate);
d->RegisterHandler("setPreference", &Delegate::SetPreference, delegate);
d->RegisterHandler("removePreference", &Delegate::RemovePreference, delegate);
d->RegisterHandler("clearPreferences", &Delegate::ClearPreferences, delegate);
2015-06-05 03:03:47 +00:00
return d;
}
} // namespace brightray