diff --git a/brightray/browser/devtools_embedder_message_dispatcher.cc b/brightray/browser/devtools_embedder_message_dispatcher.cc index bb64846320b4..c5b90fccef9c 100644 --- a/brightray/browser/devtools_embedder_message_dispatcher.cc +++ b/brightray/browser/devtools_embedder_message_dispatcher.cc @@ -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 struct StorageTraits { - typedef T StorageType; + using StorageType = T; }; template struct StorageTraits { - typedef T StorageType; + using StorageType = T; }; -template -class Argument { - public: - typedef typename StorageTraits::StorageType ValueType; - - Argument(const base::ListValue& list, int pos) { - valid_ = GetValue(list, pos, value_); +template +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 + void Apply(const H& handler, As... args) { + handler.Run(args...); + } }; -bool ParseAndHandle0(const base::Callback& handler, - const base::ListValue& list) { - handler.Run(); +template +struct ParamTuple { + 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 + void Apply(const H& handler, As... args) { + tail.template Apply(handler, args..., head); + } + + typename StorageTraits::StorageType head; + ParamTuple tail; +}; + +template +bool ParseAndHandle(const base::Callback& handler, + const DispatchCallback& callback, + const base::ListValue& list) { + ParamTuple tuple; + if (!tuple.Parse(list, list.begin())) + return false; + tuple.Apply(handler); return true; } -template -bool ParseAndHandle1(const base::Callback& handler, - const base::ListValue& list) { - if (list.GetSize() != 1) +template +bool ParseAndHandleWithCallback( + const base::Callback& handler, + const DispatchCallback& callback, + const base::ListValue& list) { + ParamTuple tuple; + if (!tuple.Parse(list, list.begin())) return false; - Argument arg1(list, 0); - if (!arg1.valid()) - return false; - handler.Run(arg1.value()); + tuple.Apply(handler, callback); return true; } -template -bool ParseAndHandle2(const base::Callback& handler, - const base::ListValue& list) { - if (list.GetSize() != 2) - return false; - Argument arg1(list, 0); - if (!arg1.valid()) - return false; - Argument arg2(list, 1); - if (!arg2.valid()) - return false; - handler.Run(arg1.value(), arg2.value()); - return true; -} +} // namespace -template -bool ParseAndHandle3(const base::Callback& handler, - const base::ListValue& list) { - if (list.GetSize() != 3) - return false; - Argument arg1(list, 0); - if (!arg1.valid()) - return false; - Argument arg2(list, 1); - if (!arg2.valid()) - return false; - Argument 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 -bool ParseAndHandle3(const base::Callback& handler, - const base::ListValue& list) { - if (list.GetSize() != 3) - return false; - Argument arg1(list, 0); - if (!arg1.valid()) - return false; - Argument arg2(list, 1); - if (!arg2.valid()) - return false; - Argument arg3(list, 2); - if (!arg3.valid()) - return false; - Argument 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 + void RegisterHandler(const std::string& method, + void (Delegate::*handler)(As...), + Delegate* delegate) { + handlers_[method] = base::Bind(&ParseAndHandle, + base::Bind(handler, + base::Unretained(delegate))); + } + + template + void RegisterHandlerWithCallback( + const std::string& method, + void (Delegate::*handler)(const DispatchCallback&, As...), + Delegate* delegate) { + handlers_[method] = base::Bind(&ParseAndHandleWithCallback, + base::Bind(handler, + base::Unretained(delegate))); + } -typedef base::Callback ListValueParser; + private: + using Handler = base::Callback; + using HandlerMap = std::map; + HandlerMap handlers_; +}; -ListValueParser BindToListParser(const base::Callback& handler) { - return base::Bind(&ParseAndHandle0, handler); -} - -template -ListValueParser BindToListParser(const base::Callback& handler) { - return base::Bind(&ParseAndHandle1, handler); -} - -template -ListValueParser BindToListParser(const base::Callback& handler) { - return base::Bind(&ParseAndHandle2, handler); -} - -template -ListValueParser BindToListParser( - const base::Callback& handler) { - return base::Bind(&ParseAndHandle3, handler); -} - -template -ListValueParser BindToListParser( - const base::Callback& handler) { - return base::Bind(&ParseAndHandle3, 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 diff --git a/brightray/browser/devtools_embedder_message_dispatcher.h b/brightray/browser/devtools_embedder_message_dispatcher.h index defe16c1f43a..851a2f490586 100644 --- a/brightray/browser/devtools_embedder_message_dispatcher.h +++ b/brightray/browser/devtools_embedder_message_dispatcher.h @@ -15,6 +15,7 @@ namespace base { class ListValue; +class Value; } namespace brightray { @@ -29,14 +30,18 @@ class DevToolsEmbedderMessageDispatcher { public: class Delegate { public: + using DispatchCallback = base::Callback; + virtual ~Delegate() {} virtual void ActivateWindow() = 0; virtual void CloseWindow() = 0; + virtual void LoadCompleted() = 0; virtual void SetInspectedPageBounds(const gfx::Rect& rect) = 0; virtual void InspectElementCompleted() = 0; - virtual void MoveWindow(int x, int y) = 0; - virtual void SetIsDocked(bool docked) = 0; + virtual void InspectedURLChanged(const std::string& url) = 0; + virtual void SetIsDocked(const DispatchCallback& callback, + bool is_docked) = 0; virtual void OpenInNewTab(const std::string& url) = 0; virtual void SaveToFile(const std::string& url, const std::string& content, @@ -48,29 +53,37 @@ class DevToolsEmbedderMessageDispatcher { virtual void RemoveFileSystem(const std::string& file_system_path) = 0; virtual void UpgradeDraggedFileSystemPermissions( const std::string& file_system_url) = 0; - virtual void IndexPath(int request_id, + virtual void IndexPath(int index_request_id, const std::string& file_system_path) = 0; - virtual void StopIndexing(int request_id) = 0; - virtual void SearchInPath(int request_id, + virtual void StopIndexing(int index_request_id) = 0; + virtual void LoadNetworkResource(const DispatchCallback& callback, + const std::string& url, + const std::string& headers, + int stream_id) = 0; + virtual void SearchInPath(int search_request_id, const std::string& file_system_path, const std::string& query) = 0; + virtual void SetWhitelistedShortcuts(const std::string& message) = 0; virtual void ZoomIn() = 0; virtual void ZoomOut() = 0; virtual void ResetZoom() = 0; + virtual void SetDevicesUpdatesEnabled(bool enabled) = 0; + virtual void SendMessageToBrowser(const std::string& message) = 0; + virtual void RecordActionUMA(const std::string& name, int action) = 0; + virtual void SendJsonRequest(const DispatchCallback& callback, + const std::string& browser_id, + const std::string& url) = 0; }; - explicit DevToolsEmbedderMessageDispatcher(Delegate* delegate); + using DispatchCallback = Delegate::DispatchCallback; - ~DevToolsEmbedderMessageDispatcher(); + virtual ~DevToolsEmbedderMessageDispatcher() {} + virtual bool Dispatch(const DispatchCallback& callback, + const std::string& method, + const base::ListValue* params) = 0; - std::string Dispatch(const std::string& method, base::ListValue* params); - - private: - typedef base::Callback Handler; - void RegisterHandler(const std::string& method, const Handler& handler); - - typedef std::map HandlerMap; - HandlerMap handlers_; + static DevToolsEmbedderMessageDispatcher* CreateForDevToolsFrontend( + Delegate* delegate); }; } // namespace brightray diff --git a/brightray/browser/inspectable_web_contents_impl.cc b/brightray/browser/inspectable_web_contents_impl.cc index 27bf3dbac175..43fc7132db72 100644 --- a/brightray/browser/inspectable_web_contents_impl.cc +++ b/brightray/browser/inspectable_web_contents_impl.cc @@ -12,6 +12,8 @@ #include "browser/inspectable_web_contents_view.h" #include "base/json/json_reader.h" +#include "base/json/json_writer.h" +#include "base/metrics/histogram.h" #include "base/prefs/pref_registry_simple.h" #include "base/prefs/pref_service.h" #include "base/strings/stringprintf.h" @@ -41,6 +43,11 @@ const char kFrontendHostId[] = "id"; const char kFrontendHostMethod[] = "method"; const char kFrontendHostParams[] = "params"; +const char kDevToolsActionTakenHistogram[] = "DevTools.ActionTaken"; +const int kDevToolsActionTakenBoundary = 100; +const char kDevToolsPanelShownHistogram[] = "DevTools.PanelShown"; +const int kDevToolsPanelShownBoundary = 20; + void RectToDictionary(const gfx::Rect& bounds, base::DictionaryValue* dict) { dict->SetInteger("x", bounds.x()); dict->SetInteger("y", bounds.y()); @@ -57,34 +64,6 @@ void DictionaryToRect(const base::DictionaryValue& dict, gfx::Rect* bounds) { *bounds = gfx::Rect(x, y, width, height); } -bool ParseMessage(const std::string& message, - std::string* method, - base::ListValue* params, - int* id) { - scoped_ptr parsed_message(base::JSONReader::Read(message)); - if (!parsed_message) - return false; - - base::DictionaryValue* dict = NULL; - if (!parsed_message->GetAsDictionary(&dict)) - return false; - if (!dict->GetString(kFrontendHostMethod, method)) - return false; - - // "params" is optional. - if (dict->HasKey(kFrontendHostParams)) { - base::ListValue* internal_params; - if (dict->GetList(kFrontendHostParams, &internal_params)) - params->Swap(internal_params); - else - return false; - } - - *id = 0; - dict->GetInteger(kFrontendHostId, id); - return true; -} - double GetZoomLevelForWebContents(content::WebContents* web_contents) { return content::HostZoomMap::GetZoomLevel(web_contents); } @@ -123,7 +102,8 @@ InspectableWebContentsImpl::InspectableWebContentsImpl( content::WebContents* web_contents) : web_contents_(web_contents), can_dock_(true), - delegate_(nullptr) { + delegate_(nullptr), + weak_factory_(this) { auto context = static_cast(web_contents_->GetBrowserContext()); auto bounds_dict = context->prefs()->GetDictionary(kDevToolsBoundsPref); if (bounds_dict) @@ -152,7 +132,7 @@ void InspectableWebContentsImpl::ShowDevTools() { // SetIsDocked is called *BEFORE* ShowDevTools. if (!devtools_web_contents_) { embedder_message_dispatcher_.reset( - new DevToolsEmbedderMessageDispatcher(this)); + DevToolsEmbedderMessageDispatcher::CreateForDevToolsFrontend(this)); content::WebContents::CreateParams create_params(web_contents_->GetBrowserContext()); devtools_web_contents_.reset(content::WebContents::Create(create_params)); @@ -200,6 +180,29 @@ void InspectableWebContentsImpl::Detach() { agent_host_ = nullptr; } +void InspectableWebContentsImpl::CallClientFunction(const std::string& function_name, + const base::Value* arg1, + const base::Value* arg2, + const base::Value* arg3) { + std::string javascript = function_name + "("; + if (arg1) { + std::string json; + base::JSONWriter::Write(arg1, &json); + javascript.append(json); + if (arg2) { + base::JSONWriter::Write(arg2, &json); + javascript.append(", ").append(json); + if (arg3) { + base::JSONWriter::Write(arg3, &json); + javascript.append(", ").append(json); + } + } + } + javascript.append(");"); + devtools_web_contents_->GetMainFrame()->ExecuteJavaScript( + base::UTF8ToUTF16(javascript)); +} + gfx::Rect InspectableWebContentsImpl::GetDevToolsBounds() const { return devtools_bounds_; } @@ -219,6 +222,9 @@ void InspectableWebContentsImpl::CloseWindow() { devtools_web_contents()->DispatchBeforeUnload(false); } +void InspectableWebContentsImpl::LoadCompleted() { +} + void InspectableWebContentsImpl::SetInspectedPageBounds(const gfx::Rect& rect) { DevToolsContentsResizingStrategy strategy(rect); if (contents_resizing_strategy_.Equals(strategy)) @@ -231,11 +237,21 @@ void InspectableWebContentsImpl::SetInspectedPageBounds(const gfx::Rect& rect) { void InspectableWebContentsImpl::InspectElementCompleted() { } -void InspectableWebContentsImpl::MoveWindow(int x, int y) { +void InspectableWebContentsImpl::InspectedURLChanged(const std::string& url) { } -void InspectableWebContentsImpl::SetIsDocked(bool docked) { +void InspectableWebContentsImpl::LoadNetworkResource( + const DispatchCallback& callback, + const std::string& url, + const std::string& headers, + int stream_id) { +} + +void InspectableWebContentsImpl::SetIsDocked(const DispatchCallback& callback, + bool docked) { view_->SetIsDocked(docked); + if (!callback.is_null()) + callback.Run(nullptr); } void InspectableWebContentsImpl::OpenInNewTab(const std::string& url) { @@ -253,12 +269,6 @@ void InspectableWebContentsImpl::AppendToFile( delegate_->DevToolsAppendToFile(url, content); } -void InspectableWebContentsImpl::WebContentsFocused( - content::WebContents* contents) { - if (delegate_) - delegate_->DevToolsFocused(); -} - void InspectableWebContentsImpl::RequestFileSystems() { devtools_web_contents()->GetMainFrame()->ExecuteJavaScript( base::ASCIIToUTF16("DevToolsAPI.fileSystemsLoaded([])")); @@ -292,6 +302,9 @@ void InspectableWebContentsImpl::SearchInPath( const std::string& query) { } +void InspectableWebContentsImpl::SetWhitelistedShortcuts(const std::string& message) { +} + void InspectableWebContentsImpl::ZoomIn() { double level = GetZoomLevelForWebContents(devtools_web_contents()); SetZoomLevelForWebContents(devtools_web_contents(), GetNextZoomLevel(level, false)); @@ -306,26 +319,56 @@ void InspectableWebContentsImpl::ResetZoom() { SetZoomLevelForWebContents(devtools_web_contents(), 0.); } +void InspectableWebContentsImpl::SetDevicesUpdatesEnabled(bool enabled) { +} + +void InspectableWebContentsImpl::SendMessageToBrowser(const std::string& message) { + if (agent_host_.get()) + agent_host_->DispatchProtocolMessage(message); +} + +void InspectableWebContentsImpl::RecordActionUMA(const std::string& name, int action) { + if (name == kDevToolsActionTakenHistogram) + UMA_HISTOGRAM_ENUMERATION(name, action, kDevToolsActionTakenBoundary); + else if (name == kDevToolsPanelShownHistogram) + UMA_HISTOGRAM_ENUMERATION(name, action, kDevToolsPanelShownBoundary); +} + +void InspectableWebContentsImpl::SendJsonRequest(const DispatchCallback& callback, + const std::string& browser_id, + const std::string& url) { + callback.Run(nullptr); +} + void InspectableWebContentsImpl::HandleMessageFromDevToolsFrontend(const std::string& message) { std::string method; - base::ListValue params; - int id; - if (!ParseMessage(message, &method, ¶ms, &id)) { + base::ListValue empty_params; + base::ListValue* params = &empty_params; + + base::DictionaryValue* dict = NULL; + scoped_ptr parsed_message(base::JSONReader::Read(message)); + if (!parsed_message || + !parsed_message->GetAsDictionary(&dict) || + !dict->GetString(kFrontendHostMethod, &method) || + (dict->HasKey(kFrontendHostParams) && + !dict->GetList(kFrontendHostParams, ¶ms))) { LOG(ERROR) << "Invalid message was sent to embedder: " << message; return; } - - std::string error = embedder_message_dispatcher_->Dispatch(method, ¶ms); - if (id) { - std::string ack = base::StringPrintf( - "DevToolsAPI.embedderMessageAck(%d, \"%s\");", id, error.c_str()); - devtools_web_contents()->GetMainFrame()->ExecuteJavaScript(base::UTF8ToUTF16(ack)); - } + int id = 0; + dict->GetInteger(kFrontendHostId, &id); + embedder_message_dispatcher_->Dispatch( + base::Bind(&InspectableWebContentsImpl::SendMessageAck, + weak_factory_.GetWeakPtr(), + id), + method, + params); } void InspectableWebContentsImpl::HandleMessageFromDevToolsFrontendToBackend( const std::string& message) { - agent_host_->DispatchProtocolMessage(message); + if (agent_host_.get()) + agent_host_->DispatchProtocolMessage(message); } void InspectableWebContentsImpl::DispatchProtocolMessage( @@ -356,7 +399,7 @@ void InspectableWebContentsImpl::DidFinishLoad(content::RenderFrameHost* render_ // If the devtools can dock, "SetIsDocked" will be called by devtools itself. if (!can_dock_) - SetIsDocked(false); + SetIsDocked(DispatchCallback(), false); } void InspectableWebContentsImpl::WebContentsDestroyed() { @@ -400,4 +443,17 @@ void InspectableWebContentsImpl::CloseContents(content::WebContents* source) { CloseDevTools(); } +void InspectableWebContentsImpl::WebContentsFocused( + content::WebContents* contents) { + if (delegate_) + delegate_->DevToolsFocused(); +} + +void InspectableWebContentsImpl::SendMessageAck(int request_id, + const base::Value* arg) { + base::FundamentalValue id_value(request_id); + CallClientFunction("DevToolsAPI.embedderMessageAck", + &id_value, arg, nullptr); +} + } // namespace brightray diff --git a/brightray/browser/inspectable_web_contents_impl.h b/brightray/browser/inspectable_web_contents_impl.h index 655f77ad0a08..bde6e1b4aac8 100644 --- a/brightray/browser/inspectable_web_contents_impl.h +++ b/brightray/browser/inspectable_web_contents_impl.h @@ -11,6 +11,7 @@ #include "browser/devtools_contents_resizing_strategy.h" #include "browser/devtools_embedder_message_dispatcher.h" +#include "base/memory/weak_ptr.h" #include "content/public/browser/devtools_agent_host.h" #include "content/public/browser/devtools_frontend_host.h" #include "content/public/browser/web_contents_delegate.h" @@ -51,6 +52,10 @@ class InspectableWebContentsImpl : void AttachTo(const scoped_refptr&) override; void Detach(); + void CallClientFunction(const std::string& function_name, + const base::Value* arg1, + const base::Value* arg2, + const base::Value* arg3); // Return the last position and size of devtools window. gfx::Rect GetDevToolsBounds() const; @@ -71,10 +76,15 @@ class InspectableWebContentsImpl : // DevToolsEmbedderMessageDispacher::Delegate void ActivateWindow() override; void CloseWindow() override; + void LoadCompleted() override; void SetInspectedPageBounds(const gfx::Rect& rect) override; void InspectElementCompleted() override; - void MoveWindow(int x, int y) override; - void SetIsDocked(bool docked) override; + void InspectedURLChanged(const std::string& url) override; + void LoadNetworkResource(const DispatchCallback& callback, + const std::string& url, + const std::string& headers, + int stream_id) override; + void SetIsDocked(const DispatchCallback& callback, bool is_docked) override; void OpenInNewTab(const std::string& url) override; void SaveToFile(const std::string& url, const std::string& content, @@ -86,15 +96,22 @@ class InspectableWebContentsImpl : void RemoveFileSystem(const std::string& file_system_path) override; void UpgradeDraggedFileSystemPermissions( const std::string& file_system_url) override; - void IndexPath(int request_id, + void IndexPath(int index_request_id, const std::string& file_system_path) override; - void StopIndexing(int request_id) override; - void SearchInPath(int request_id, + void StopIndexing(int index_request_id) override; + void SearchInPath(int search_request_id, const std::string& file_system_path, const std::string& query) override; + void SetWhitelistedShortcuts(const std::string& message) override; void ZoomIn() override; void ZoomOut() override; void ResetZoom() override; + void SetDevicesUpdatesEnabled(bool enabled) override; + void SendMessageToBrowser(const std::string& message) override; + void RecordActionUMA(const std::string& name, int action) override; + void SendJsonRequest(const DispatchCallback& callback, + const std::string& browser_id, + const std::string& url) override; // content::DevToolsFrontendHostDelegate: void HandleMessageFromDevToolsFrontend(const std::string& message) override; @@ -133,6 +150,9 @@ class InspectableWebContentsImpl : void CloseContents(content::WebContents* source) override; void WebContentsFocused(content::WebContents* contents) override; + void SendMessageAck(int request_id, + const base::Value* arg1); + scoped_ptr web_contents_; scoped_ptr devtools_web_contents_; scoped_ptr view_; @@ -147,6 +167,8 @@ class InspectableWebContentsImpl : InspectableWebContentsDelegate* delegate_; + base::WeakPtrFactory weak_factory_; + DISALLOW_COPY_AND_ASSIGN(InspectableWebContentsImpl); };