From 368d23ef5b53d9d9aedd6e9b21702a82e1528a7d Mon Sep 17 00:00:00 2001 From: gray Date: Sun, 2 Nov 2014 06:34:22 -0800 Subject: [PATCH 1/4] add open devtools api to webview tag --- atom/browser/api/atom_api_web_contents.cc | 14 +++++++++- atom/browser/api/atom_api_web_contents.h | 32 +++++++++++++++++++++++ atom/renderer/lib/web-view.coffee | 3 ++- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index bcf42553e98..cbe0b5a6402 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -10,6 +10,9 @@ #include "atom/common/native_mate_converters/gurl_converter.h" #include "atom/common/native_mate_converters/string16_converter.h" #include "atom/common/native_mate_converters/value_converter.h" +#include "atom/browser/atom_javascript_dialog_manager.h" +#include "brightray/browser/inspectable_web_contents.h" +#include "brightray/browser/inspectable_web_contents_view.h" #include "base/strings/utf_string_conversions.h" #include "content/public/browser/render_frame_host.h" #include "content/public/browser/render_process_host.h" @@ -37,7 +40,8 @@ WebContents::WebContents(content::WebContents* web_contents) : content::WebContentsObserver(web_contents), guest_instance_id_(-1), guest_opaque_(true), - auto_size_enabled_(false) { + auto_size_enabled_(false) + { } WebContents::WebContents(const mate::Dictionary& options) @@ -58,6 +62,9 @@ WebContents::WebContents(const mate::Dictionary& options) storage_.reset(content::WebContents::Create(params)); storage_->SetDelegate(this); Observe(storage_.get()); + + inspectable_web_contents_.reset(brightray::InspectableWebContents::Create(storage_.get())); +// inspectable_web_contents()->SetDelegate(this); } WebContents::~WebContents() { @@ -368,6 +375,10 @@ void WebContents::ExecuteJavaScript(const base::string16& code) { web_contents()->GetMainFrame()->ExecuteJavaScript(code); } +void WebContents::OpenDevTools() { + inspectable_web_contents()->ShowDevTools(); +} + bool WebContents::SendIPCMessage(const base::string16& channel, const base::ListValue& args) { return Send(new AtomViewMsg_Message(routing_id(), channel, args)); @@ -442,6 +453,7 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder( .SetMethod("setAutoSize", &WebContents::SetAutoSize) .SetMethod("setAllowTransparency", &WebContents::SetAllowTransparency) .SetMethod("isGuest", &WebContents::is_guest) + .SetMethod("openDevTools", &WebContents::OpenDevTools) .Build()); return mate::ObjectTemplateBuilder( diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 679f0c091df..47f8e25f558 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -11,20 +11,32 @@ #include "content/public/browser/browser_plugin_guest_delegate.h" #include "content/public/browser/web_contents_delegate.h" #include "content/public/browser/web_contents_observer.h" +#include "brightray/browser/default_web_contents_delegate.h" +#include "brightray/browser/inspectable_web_contents_delegate.h" +#include "brightray/browser/inspectable_web_contents_impl.h" #include "native_mate/handle.h" namespace mate { class Dictionary; } +namespace brightray +{ + class InspectableWebContents; + class InspectableWebContentsImpl; +} + namespace atom { +class AtomJavaScriptDialogManager; + namespace api { class WebContents : public mate::EventEmitter, public content::BrowserPluginGuestDelegate, public content::WebContentsDelegate, public content::WebContentsObserver { + // public brightray::InspectableWebContentsDelegate { public: // Create from an existing WebContents. static mate::Handle CreateFrom( @@ -57,6 +69,7 @@ class WebContents : public mate::EventEmitter, void SetUserAgent(const std::string& user_agent); void InsertCSS(const std::string& css); void ExecuteJavaScript(const base::string16& code); + void OpenDevTools(); bool SendIPCMessage(const base::string16& channel, const base::ListValue& args); @@ -83,6 +96,18 @@ class WebContents : public mate::EventEmitter, explicit WebContents(const mate::Dictionary& options); ~WebContents(); + brightray::InspectableWebContentsImpl* inspectable_web_contents() const { + return static_cast( + inspectable_web_contents_.get()); + } + + // Devtools +/* void DevToolsSaveToFile(const std::string& url, + const std::string& content, + bool save_as) override; + void DevToolsAppendToFile(const std::string& url, + const std::string& content) override; */ + // mate::Wrappable: virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder( v8::Isolate* isolate) override; @@ -180,6 +205,13 @@ class WebContents : public mate::EventEmitter, // The WebContents that attaches this guest view. content::WebContents* embedder_web_contents_; + scoped_ptr dialog_manager_; + + // Notice that inspectable_web_contents_ must be placed after dialog_manager_, + // so we can make sure inspectable_web_contents_ is destroyed before + // dialog_manager_, otherwise a crash would happen. + scoped_ptr inspectable_web_contents_; + // The size of the container element. gfx::Size element_size_; diff --git a/atom/renderer/lib/web-view.coffee b/atom/renderer/lib/web-view.coffee index 8061f9b049c..e198c2b820f 100644 --- a/atom/renderer/lib/web-view.coffee +++ b/atom/renderer/lib/web-view.coffee @@ -517,7 +517,8 @@ registerWebViewElement = -> "isCrashed" "setUserAgent" "executeJavaScript" - "insertCSS" + "insertCSS", + "openDevTools", "send" ] From f43c227806196a5ff8bf312af76b32515fdd1ccd Mon Sep 17 00:00:00 2001 From: gray Date: Sun, 2 Nov 2014 07:20:10 -0800 Subject: [PATCH 2/4] update and document openDevTools on web view. --- atom/browser/api/atom_api_web_contents.cc | 1 - atom/browser/api/atom_api_web_contents.h | 12 ------------ docs/api/web-view-tag.md | 4 ++++ 3 files changed, 4 insertions(+), 13 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index cbe0b5a6402..16e31b7fe90 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -64,7 +64,6 @@ WebContents::WebContents(const mate::Dictionary& options) Observe(storage_.get()); inspectable_web_contents_.reset(brightray::InspectableWebContents::Create(storage_.get())); -// inspectable_web_contents()->SetDelegate(this); } WebContents::~WebContents() { diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 47f8e25f558..6b9e0b736c5 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -28,15 +28,12 @@ namespace brightray namespace atom { -class AtomJavaScriptDialogManager; - namespace api { class WebContents : public mate::EventEmitter, public content::BrowserPluginGuestDelegate, public content::WebContentsDelegate, public content::WebContentsObserver { - // public brightray::InspectableWebContentsDelegate { public: // Create from an existing WebContents. static mate::Handle CreateFrom( @@ -101,13 +98,6 @@ class WebContents : public mate::EventEmitter, inspectable_web_contents_.get()); } - // Devtools -/* void DevToolsSaveToFile(const std::string& url, - const std::string& content, - bool save_as) override; - void DevToolsAppendToFile(const std::string& url, - const std::string& content) override; */ - // mate::Wrappable: virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder( v8::Isolate* isolate) override; @@ -205,8 +195,6 @@ class WebContents : public mate::EventEmitter, // The WebContents that attaches this guest view. content::WebContents* embedder_web_contents_; - scoped_ptr dialog_manager_; - // Notice that inspectable_web_contents_ must be placed after dialog_manager_, // so we can make sure inspectable_web_contents_ is destroyed before // dialog_manager_, otherwise a crash would happen. diff --git a/docs/api/web-view-tag.md b/docs/api/web-view-tag.md index 10bc067df4b..55c524b0d9e 100644 --- a/docs/api/web-view-tag.md +++ b/docs/api/web-view-tag.md @@ -169,6 +169,10 @@ Injects CSS into guest page. Evaluate `code` in guest page. +### ``.openDevTools() + +Open a devtools instance for the webview's contents. + ### ``.send(channel[, args...]) * `channel` String From 45f4a25ac9be46d67bcb95b7ae6f74ae27cc1135 Mon Sep 17 00:00:00 2001 From: gray Date: Sun, 2 Nov 2014 07:32:33 -0800 Subject: [PATCH 3/4] fix for cpplint --- atom/browser/api/atom_api_web_contents.cc | 6 +++--- atom/browser/api/atom_api_web_contents.h | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 16e31b7fe90..6b5cc85859c 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -40,8 +40,7 @@ WebContents::WebContents(content::WebContents* web_contents) : content::WebContentsObserver(web_contents), guest_instance_id_(-1), guest_opaque_(true), - auto_size_enabled_(false) - { + auto_size_enabled_(false) { } WebContents::WebContents(const mate::Dictionary& options) @@ -63,7 +62,8 @@ WebContents::WebContents(const mate::Dictionary& options) storage_->SetDelegate(this); Observe(storage_.get()); - inspectable_web_contents_.reset(brightray::InspectableWebContents::Create(storage_.get())); + inspectable_web_contents_.reset( + brightray::InspectableWebContents::Create(storage_.get())); } WebContents::~WebContents() { diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 6b9e0b736c5..45fccba6e3e 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -20,8 +20,7 @@ namespace mate { class Dictionary; } -namespace brightray -{ +namespace brightray { class InspectableWebContents; class InspectableWebContentsImpl; } From 6c3f06514782bc56f553a38286fd9c0bf9c1262c Mon Sep 17 00:00:00 2001 From: gray Date: Sun, 2 Nov 2014 11:32:06 -0800 Subject: [PATCH 4/4] force devtools to show as undocked. --- atom/browser/api/atom_api_web_contents.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 6b5cc85859c..9c878adf507 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -376,6 +376,9 @@ void WebContents::ExecuteJavaScript(const base::string16& code) { void WebContents::OpenDevTools() { inspectable_web_contents()->ShowDevTools(); + + // force the inspectable web contents to be undocked when it is opened. + inspectable_web_contents()->GetView()->SetIsDocked(false); } bool WebContents::SendIPCMessage(const base::string16& channel,