Update to new devtools messages
This commit is contained in:
parent
3f2c2e3484
commit
d48a80bdfa
4 changed files with 300 additions and 260 deletions
|
@ -11,21 +11,23 @@ namespace brightray {
|
|||
|
||||
namespace {
|
||||
|
||||
bool GetValue(const base::ListValue& list, int pos, std::string& value) {
|
||||
return list.GetString(pos, &value);
|
||||
using DispatchCallback = DevToolsEmbedderMessageDispatcher::DispatchCallback;
|
||||
|
||||
bool GetValue(const base::Value* value, std::string* result) {
|
||||
return value->GetAsString(result);
|
||||
}
|
||||
|
||||
bool GetValue(const base::ListValue& list, int pos, int& value) {
|
||||
return list.GetInteger(pos, &value);
|
||||
bool GetValue(const base::Value* value, int* result) {
|
||||
return value->GetAsInteger(result);
|
||||
}
|
||||
|
||||
bool GetValue(const base::ListValue& list, int pos, bool& value) {
|
||||
return list.GetBoolean(pos, &value);
|
||||
bool GetValue(const base::Value* value, bool* result) {
|
||||
return value->GetAsBoolean(result);
|
||||
}
|
||||
|
||||
bool GetValue(const base::ListValue& list, int pos, gfx::Rect& rect) {
|
||||
bool GetValue(const base::Value* value, gfx::Rect* rect) {
|
||||
const base::DictionaryValue* dict;
|
||||
if (!list.GetDictionary(pos, &dict))
|
||||
if (!value->GetAsDictionary(&dict))
|
||||
return false;
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
|
@ -36,217 +38,164 @@ bool GetValue(const base::ListValue& list, int pos, gfx::Rect& rect) {
|
|||
!dict->GetInteger("width", &width) ||
|
||||
!dict->GetInteger("height", &height))
|
||||
return false;
|
||||
rect.SetRect(x, y, width, height);
|
||||
rect->SetRect(x, y, width, height);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct StorageTraits {
|
||||
typedef T StorageType;
|
||||
using StorageType = T;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct StorageTraits<const T&> {
|
||||
typedef T StorageType;
|
||||
using StorageType = T;
|
||||
};
|
||||
|
||||
template <class A>
|
||||
class Argument {
|
||||
public:
|
||||
typedef typename StorageTraits<A>::StorageType ValueType;
|
||||
|
||||
Argument(const base::ListValue& list, int pos) {
|
||||
valid_ = GetValue(list, pos, value_);
|
||||
template <typename... Ts>
|
||||
struct ParamTuple {
|
||||
bool Parse(const base::ListValue& list,
|
||||
const base::ListValue::const_iterator& it) {
|
||||
return it == list.end();
|
||||
}
|
||||
|
||||
ValueType value() const { return value_; }
|
||||
bool valid() const { return valid_; }
|
||||
|
||||
private:
|
||||
ValueType value_;
|
||||
bool valid_;
|
||||
template <typename H, typename... As>
|
||||
void Apply(const H& handler, As... args) {
|
||||
handler.Run(args...);
|
||||
}
|
||||
};
|
||||
|
||||
bool ParseAndHandle0(const base::Callback<void(void)>& handler,
|
||||
const base::ListValue& list) {
|
||||
handler.Run();
|
||||
template <typename T, typename... Ts>
|
||||
struct ParamTuple<T, Ts...> {
|
||||
bool Parse(const base::ListValue& list,
|
||||
const base::ListValue::const_iterator& it) {
|
||||
return it != list.end() && GetValue(*it, &head) && tail.Parse(list, it + 1);
|
||||
}
|
||||
|
||||
template <typename H, typename... As>
|
||||
void Apply(const H& handler, As... args) {
|
||||
tail.template Apply<H, As..., T>(handler, args..., head);
|
||||
}
|
||||
|
||||
typename StorageTraits<T>::StorageType head;
|
||||
ParamTuple<Ts...> tail;
|
||||
};
|
||||
|
||||
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;
|
||||
tuple.Apply(handler);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <class A1>
|
||||
bool ParseAndHandle1(const base::Callback<void(A1)>& handler,
|
||||
const base::ListValue& list) {
|
||||
if (list.GetSize() != 1)
|
||||
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;
|
||||
Argument<A1> arg1(list, 0);
|
||||
if (!arg1.valid())
|
||||
return false;
|
||||
handler.Run(arg1.value());
|
||||
tuple.Apply(handler, callback);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <class A1, class A2>
|
||||
bool ParseAndHandle2(const base::Callback<void(A1, A2)>& handler,
|
||||
const base::ListValue& list) {
|
||||
if (list.GetSize() != 2)
|
||||
return false;
|
||||
Argument<A1> arg1(list, 0);
|
||||
if (!arg1.valid())
|
||||
return false;
|
||||
Argument<A2> arg2(list, 1);
|
||||
if (!arg2.valid())
|
||||
return false;
|
||||
handler.Run(arg1.value(), arg2.value());
|
||||
return true;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
template <class A1, class A2, class A3>
|
||||
bool ParseAndHandle3(const base::Callback<void(A1, A2, A3)>& handler,
|
||||
const base::ListValue& list) {
|
||||
if (list.GetSize() != 3)
|
||||
return false;
|
||||
Argument<A1> arg1(list, 0);
|
||||
if (!arg1.valid())
|
||||
return false;
|
||||
Argument<A2> arg2(list, 1);
|
||||
if (!arg2.valid())
|
||||
return false;
|
||||
Argument<A3> arg3(list, 2);
|
||||
if (!arg3.valid())
|
||||
return false;
|
||||
handler.Run(arg1.value(), arg2.value(), arg3.value());
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* 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 {}
|
||||
|
||||
template <class A1, class A2, class A3, class A4>
|
||||
bool ParseAndHandle3(const base::Callback<void(A1, A2, A3, A4)>& handler,
|
||||
const base::ListValue& list) {
|
||||
if (list.GetSize() != 3)
|
||||
return false;
|
||||
Argument<A1> arg1(list, 0);
|
||||
if (!arg1.valid())
|
||||
return false;
|
||||
Argument<A2> arg2(list, 1);
|
||||
if (!arg2.valid())
|
||||
return false;
|
||||
Argument<A3> arg3(list, 2);
|
||||
if (!arg3.valid())
|
||||
return false;
|
||||
Argument<A4> arg4(list, 3);
|
||||
if (!arg4.valid())
|
||||
return false;
|
||||
handler.Run(arg1.value(), arg2.value(), arg3.value(), arg4.value());
|
||||
return true;
|
||||
}
|
||||
bool Dispatch(const DispatchCallback& callback,
|
||||
const std::string& method,
|
||||
const base::ListValue* params) override {
|
||||
HandlerMap::iterator it = handlers_.find(method);
|
||||
return it != handlers_.end() && it->second.Run(callback, *params);
|
||||
}
|
||||
|
||||
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)));
|
||||
}
|
||||
|
||||
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)));
|
||||
}
|
||||
|
||||
|
||||
typedef base::Callback<bool (const base::ListValue&)> ListValueParser;
|
||||
private:
|
||||
using Handler = base::Callback<bool(const DispatchCallback&,
|
||||
const base::ListValue&)>;
|
||||
using HandlerMap = std::map<std::string, Handler>;
|
||||
HandlerMap handlers_;
|
||||
};
|
||||
|
||||
ListValueParser BindToListParser(const base::Callback<void()>& handler) {
|
||||
return base::Bind(&ParseAndHandle0, handler);
|
||||
}
|
||||
|
||||
template <class A1>
|
||||
ListValueParser BindToListParser(const base::Callback<void(A1)>& handler) {
|
||||
return base::Bind(&ParseAndHandle1<A1>, handler);
|
||||
}
|
||||
|
||||
template <class A1, class A2>
|
||||
ListValueParser BindToListParser(const base::Callback<void(A1, A2)>& handler) {
|
||||
return base::Bind(&ParseAndHandle2<A1, A2>, handler);
|
||||
}
|
||||
|
||||
template <class A1, class A2, class A3>
|
||||
ListValueParser BindToListParser(
|
||||
const base::Callback<void(A1, A2, A3)>& handler) {
|
||||
return base::Bind(&ParseAndHandle3<A1, A2, A3>, handler);
|
||||
}
|
||||
|
||||
template <class A1, class A2, class A3, class A4>
|
||||
ListValueParser BindToListParser(
|
||||
const base::Callback<void(A1, A2, A3, A4)>& handler) {
|
||||
return base::Bind(&ParseAndHandle3<A1, A2, A3, A4>, handler);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
DevToolsEmbedderMessageDispatcher::DevToolsEmbedderMessageDispatcher(
|
||||
// static
|
||||
DevToolsEmbedderMessageDispatcher*
|
||||
DevToolsEmbedderMessageDispatcher::CreateForDevToolsFrontend(
|
||||
Delegate* delegate) {
|
||||
RegisterHandler("bringToFront",
|
||||
BindToListParser(base::Bind(&Delegate::ActivateWindow,
|
||||
base::Unretained(delegate))));
|
||||
RegisterHandler("closeWindow",
|
||||
BindToListParser(base::Bind(&Delegate::CloseWindow,
|
||||
base::Unretained(delegate))));
|
||||
RegisterHandler("setInspectedPageBounds",
|
||||
BindToListParser(base::Bind(&Delegate::SetInspectedPageBounds,
|
||||
base::Unretained(delegate))));
|
||||
RegisterHandler("inspectElementCompleted",
|
||||
BindToListParser(base::Bind(&Delegate::InspectElementCompleted,
|
||||
base::Unretained(delegate))));
|
||||
RegisterHandler("moveWindowBy",
|
||||
BindToListParser(base::Bind(&Delegate::MoveWindow,
|
||||
base::Unretained(delegate))));
|
||||
RegisterHandler("setIsDocked",
|
||||
BindToListParser(base::Bind(&Delegate::SetIsDocked,
|
||||
base::Unretained(delegate))));
|
||||
RegisterHandler("openInNewTab",
|
||||
BindToListParser(base::Bind(&Delegate::OpenInNewTab,
|
||||
base::Unretained(delegate))));
|
||||
RegisterHandler("save",
|
||||
BindToListParser(base::Bind(&Delegate::SaveToFile,
|
||||
base::Unretained(delegate))));
|
||||
RegisterHandler("append",
|
||||
BindToListParser(base::Bind(&Delegate::AppendToFile,
|
||||
base::Unretained(delegate))));
|
||||
RegisterHandler("requestFileSystems",
|
||||
BindToListParser(base::Bind(&Delegate::RequestFileSystems,
|
||||
base::Unretained(delegate))));
|
||||
RegisterHandler("addFileSystem",
|
||||
BindToListParser(base::Bind(&Delegate::AddFileSystem,
|
||||
base::Unretained(delegate))));
|
||||
RegisterHandler("removeFileSystem",
|
||||
BindToListParser(base::Bind(&Delegate::RemoveFileSystem,
|
||||
base::Unretained(delegate))));
|
||||
RegisterHandler("upgradeDraggedFileSystemPermissions",
|
||||
BindToListParser(base::Bind(&Delegate::UpgradeDraggedFileSystemPermissions,
|
||||
base::Unretained(delegate))));
|
||||
RegisterHandler("indexPath",
|
||||
BindToListParser(base::Bind(&Delegate::IndexPath,
|
||||
base::Unretained(delegate))));
|
||||
RegisterHandler("stopIndexing",
|
||||
BindToListParser(base::Bind(&Delegate::StopIndexing,
|
||||
base::Unretained(delegate))));
|
||||
RegisterHandler("searchInPath",
|
||||
BindToListParser(base::Bind(&Delegate::SearchInPath,
|
||||
base::Unretained(delegate))));
|
||||
RegisterHandler("zoomIn",
|
||||
BindToListParser(base::Bind(&Delegate::ZoomIn,
|
||||
base::Unretained(delegate))));
|
||||
RegisterHandler("zoomOut",
|
||||
BindToListParser(base::Bind(&Delegate::ZoomOut,
|
||||
base::Unretained(delegate))));
|
||||
RegisterHandler("resetZoom",
|
||||
BindToListParser(base::Bind(&Delegate::ResetZoom,
|
||||
base::Unretained(delegate))));
|
||||
}
|
||||
DispatcherImpl* d = new DispatcherImpl();
|
||||
|
||||
DevToolsEmbedderMessageDispatcher::~DevToolsEmbedderMessageDispatcher() {}
|
||||
|
||||
std::string DevToolsEmbedderMessageDispatcher::Dispatch(
|
||||
const std::string& method, base::ListValue* params) {
|
||||
HandlerMap::iterator it = handlers_.find(method);
|
||||
if (it == handlers_.end())
|
||||
return "Unsupported frontend host method: " + method;
|
||||
|
||||
if (!it->second.Run(*params))
|
||||
return "Invalid frontend host message parameters: " + method;
|
||||
return "";
|
||||
}
|
||||
|
||||
void DevToolsEmbedderMessageDispatcher::RegisterHandler(
|
||||
const std::string& method, const Handler& handler) {
|
||||
handlers_[method] = handler;
|
||||
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);
|
||||
d->RegisterHandler("sendMessageToBrowser",
|
||||
&Delegate::SendMessageToBrowser, delegate);
|
||||
d->RegisterHandler("recordActionUMA", &Delegate::RecordActionUMA, delegate);
|
||||
d->RegisterHandlerWithCallback("sendJsonRequest",
|
||||
&Delegate::SendJsonRequest, delegate);
|
||||
return d;
|
||||
}
|
||||
|
||||
} // namespace brightray
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue