From e3aaaf2643869f3aac727d61849808f035c215a6 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 4 Mar 2014 16:12:02 +0800 Subject: [PATCH 1/5] Add InspectableWebContentsDelegate. --- brightray/brightray.gyp | 1 + brightray/browser/inspectable_web_contents.h | 4 +++ .../inspectable_web_contents_delegate.h | 26 +++++++++++++++++++ .../browser/inspectable_web_contents_impl.cc | 14 ++++++++-- .../browser/inspectable_web_contents_impl.h | 7 +++++ 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 brightray/browser/inspectable_web_contents_delegate.h diff --git a/brightray/brightray.gyp b/brightray/brightray.gyp index 9504a57786f..532011bc955 100644 --- a/brightray/brightray.gyp +++ b/brightray/brightray.gyp @@ -43,6 +43,7 @@ 'browser/download_manager_delegate.h', 'browser/inspectable_web_contents.cc', 'browser/inspectable_web_contents.h', + 'browser/inspectable_web_contents_delegate.h', 'browser/inspectable_web_contents_impl.cc', 'browser/inspectable_web_contents_impl.h', 'browser/inspectable_web_contents_view.h', diff --git a/brightray/browser/inspectable_web_contents.h b/brightray/browser/inspectable_web_contents.h index 49a50bc5079..b436c391ce3 100644 --- a/brightray/browser/inspectable_web_contents.h +++ b/brightray/browser/inspectable_web_contents.h @@ -5,6 +5,7 @@ namespace brightray { +class InspectableWebContentsDelegate; class InspectableWebContentsView; class InspectableWebContents { @@ -23,6 +24,9 @@ class InspectableWebContents { virtual void ShowDevTools() = 0; virtual bool IsDevToolsViewShowing() = 0; + + // The delegate manages its own life. + virtual void SetDelegate(InspectableWebContentsDelegate* delegate) = 0; }; } // namespace brightray diff --git a/brightray/browser/inspectable_web_contents_delegate.h b/brightray/browser/inspectable_web_contents_delegate.h new file mode 100644 index 00000000000..7ef79f60e0e --- /dev/null +++ b/brightray/browser/inspectable_web_contents_delegate.h @@ -0,0 +1,26 @@ +#ifndef BRIGHTRAY_INSPECTABLE_WEB_CONTENTS_DELEGATE_H_ +#define BRIGHTRAY_INSPECTABLE_WEB_CONTENTS_DELEGATE_H_ + +#include + +namespace brightray { + +class InspectableWebContentsDelegate { + public: + virtual ~InspectableWebContentsDelegate() {} + + // Called when the devtools is going to change the dock side, returning true + // to override the default behavior. + // Receiver should set |succeed| to |false| if it failed to handle this. + virtual bool DevToolsSetDockSide(const std::string& side, bool* succeed) { + return false; + } + + // Called when the devtools is going to be showed, returning true to override + // the default behavior. + virtual bool DevToolsShow(const std::string& side) { return false; } +}; + +} // namespace brightray + +#endif // BRIGHTRAY_INSPECTABLE_WEB_CONTENTS_DELEGATE_H_ diff --git a/brightray/browser/inspectable_web_contents_impl.cc b/brightray/browser/inspectable_web_contents_impl.cc index e972e568735..cf73a26443d 100644 --- a/brightray/browser/inspectable_web_contents_impl.cc +++ b/brightray/browser/inspectable_web_contents_impl.cc @@ -8,6 +8,7 @@ #include "browser/browser_client.h" #include "browser/browser_context.h" #include "browser/browser_main_parts.h" +#include "browser/inspectable_web_contents_delegate.h" #include "browser/inspectable_web_contents_view.h" #include "base/prefs/pref_registry_simple.h" @@ -40,7 +41,8 @@ void InspectableWebContentsImpl::RegisterPrefs(PrefRegistrySimple* registry) { InspectableWebContentsImpl::InspectableWebContentsImpl( content::WebContents* web_contents) - : web_contents_(web_contents) { + : web_contents_(web_contents), + delegate_(nullptr) { auto context = static_cast( web_contents_->GetBrowserContext()); dock_side_ = context->prefs()->GetString(kDockSidePref); @@ -92,6 +94,9 @@ void InspectableWebContentsImpl::ShowDevTools() { std::string()); } + if (delegate_ && delegate_->DevToolsShow(dock_side_)) + return; + view_->SetDockSide(dock_side_); view_->ShowDevTools(); } @@ -120,8 +125,13 @@ void InspectableWebContentsImpl::MoveWindow(int x, int y) { } void InspectableWebContentsImpl::SetDockSide(const std::string& side) { - if (!view_->SetDockSide(side)) + bool succeed = true; + if (delegate_ && delegate_->DevToolsSetDockSide(side, &succeed)) { + if (!succeed) // delegate failed to set dock side. + return; + } else if (!view_->SetDockSide(side)) { return; + } dock_side_ = side; diff --git a/brightray/browser/inspectable_web_contents_impl.h b/brightray/browser/inspectable_web_contents_impl.h index 53023204dd1..f48f0a47bcb 100644 --- a/brightray/browser/inspectable_web_contents_impl.h +++ b/brightray/browser/inspectable_web_contents_impl.h @@ -23,6 +23,7 @@ class DevToolsClientHost; namespace brightray { +class InspectableWebContentsDelegate; class InspectableWebContentsView; class InspectableWebContentsImpl : @@ -43,6 +44,10 @@ class InspectableWebContentsImpl : virtual void ShowDevTools() OVERRIDE; virtual bool IsDevToolsViewShowing() OVERRIDE; + virtual void SetDelegate(InspectableWebContentsDelegate* delegate) { + delegate_ = delegate; + } + content::WebContents* devtools_web_contents() { return devtools_web_contents_.get(); } @@ -101,6 +106,8 @@ class InspectableWebContentsImpl : scoped_ptr embedder_message_dispatcher_; + InspectableWebContentsDelegate* delegate_; + DISALLOW_COPY_AND_ASSIGN(InspectableWebContentsImpl); }; From 5768ff698107b4a63fd275dcea19a5c0268c385a Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 4 Mar 2014 19:47:33 +0800 Subject: [PATCH 2/5] Enable delegate to change the dock side. --- brightray/browser/inspectable_web_contents_delegate.h | 3 ++- brightray/browser/inspectable_web_contents_impl.cc | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/brightray/browser/inspectable_web_contents_delegate.h b/brightray/browser/inspectable_web_contents_delegate.h index 7ef79f60e0e..0b2f3f6a2b2 100644 --- a/brightray/browser/inspectable_web_contents_delegate.h +++ b/brightray/browser/inspectable_web_contents_delegate.h @@ -18,7 +18,8 @@ class InspectableWebContentsDelegate { // Called when the devtools is going to be showed, returning true to override // the default behavior. - virtual bool DevToolsShow(const std::string& side) { return false; } + // Receiver is given the chance to change the |dock_side|. + virtual bool DevToolsShow(std::string* dock_side) { return false; } }; } // namespace brightray diff --git a/brightray/browser/inspectable_web_contents_impl.cc b/brightray/browser/inspectable_web_contents_impl.cc index cf73a26443d..c4a8f75ef4b 100644 --- a/brightray/browser/inspectable_web_contents_impl.cc +++ b/brightray/browser/inspectable_web_contents_impl.cc @@ -94,7 +94,7 @@ void InspectableWebContentsImpl::ShowDevTools() { std::string()); } - if (delegate_ && delegate_->DevToolsShow(dock_side_)) + if (delegate_ && delegate_->DevToolsShow(&dock_side_)) return; view_->SetDockSide(dock_side_); From d2ff5ad798b75547aeaef0ce52463e97656192a0 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 20 Mar 2014 09:26:21 +0800 Subject: [PATCH 3/5] Provide a way to close devtools completely. --- brightray/browser/inspectable_web_contents.h | 2 ++ brightray/browser/inspectable_web_contents_impl.cc | 12 +++++++++--- brightray/browser/inspectable_web_contents_impl.h | 1 + brightray/browser/inspectable_web_contents_view.h | 1 + 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/brightray/browser/inspectable_web_contents.h b/brightray/browser/inspectable_web_contents.h index b436c391ce3..313e4ebea2c 100644 --- a/brightray/browser/inspectable_web_contents.h +++ b/brightray/browser/inspectable_web_contents.h @@ -23,6 +23,8 @@ class InspectableWebContents { virtual content::WebContents* GetWebContents() const = 0; virtual void ShowDevTools() = 0; + // Close the DevTools completely instead of just hide it. + virtual void CloseDevTools() = 0; virtual bool IsDevToolsViewShowing() = 0; // The delegate manages its own life. diff --git a/brightray/browser/inspectable_web_contents_impl.cc b/brightray/browser/inspectable_web_contents_impl.cc index c4a8f75ef4b..b22b4a287fb 100644 --- a/brightray/browser/inspectable_web_contents_impl.cc +++ b/brightray/browser/inspectable_web_contents_impl.cc @@ -101,6 +101,14 @@ void InspectableWebContentsImpl::ShowDevTools() { view_->ShowDevTools(); } +void InspectableWebContentsImpl::CloseDevTools() { + if (IsDevToolsViewShowing()) { + view_->CloseDevTools(); + devtools_web_contents_.reset(); + web_contents_->GetView()->Focus(); + } +} + bool InspectableWebContentsImpl::IsDevToolsViewShowing() { return devtools_web_contents_ && view_->IsDevToolsViewShowing(); } @@ -116,9 +124,7 @@ void InspectableWebContentsImpl::ActivateWindow() { } void InspectableWebContentsImpl::CloseWindow() { - view_->CloseDevTools(); - devtools_web_contents_.reset(); - web_contents_->GetView()->Focus(); + CloseDevTools(); } void InspectableWebContentsImpl::MoveWindow(int x, int y) { diff --git a/brightray/browser/inspectable_web_contents_impl.h b/brightray/browser/inspectable_web_contents_impl.h index f48f0a47bcb..8581b24ec47 100644 --- a/brightray/browser/inspectable_web_contents_impl.h +++ b/brightray/browser/inspectable_web_contents_impl.h @@ -42,6 +42,7 @@ class InspectableWebContentsImpl : virtual content::WebContents* GetWebContents() const OVERRIDE; virtual void ShowDevTools() OVERRIDE; + virtual void CloseDevTools() OVERRIDE; virtual bool IsDevToolsViewShowing() OVERRIDE; virtual void SetDelegate(InspectableWebContentsDelegate* delegate) { diff --git a/brightray/browser/inspectable_web_contents_view.h b/brightray/browser/inspectable_web_contents_view.h index 87274805f8e..18fd192a67e 100644 --- a/brightray/browser/inspectable_web_contents_view.h +++ b/brightray/browser/inspectable_web_contents_view.h @@ -12,6 +12,7 @@ class InspectableWebContentsView { virtual gfx::NativeView GetNativeView() const = 0; virtual void ShowDevTools() = 0; + // Hide the DevTools view. virtual void CloseDevTools() = 0; virtual bool IsDevToolsViewShowing() = 0; virtual bool SetDockSide(const std::string& side) = 0; From 54060ed53d9bd664b0586e61609a25a467e4879a Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sat, 5 Apr 2014 00:10:09 +0800 Subject: [PATCH 4/5] Enable delegate to override SaveToFile and AppendToFile. --- brightray/browser/inspectable_web_contents_delegate.h | 6 ++++++ brightray/browser/inspectable_web_contents_impl.cc | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/brightray/browser/inspectable_web_contents_delegate.h b/brightray/browser/inspectable_web_contents_delegate.h index 0b2f3f6a2b2..d86285789e3 100644 --- a/brightray/browser/inspectable_web_contents_delegate.h +++ b/brightray/browser/inspectable_web_contents_delegate.h @@ -20,6 +20,12 @@ class InspectableWebContentsDelegate { // the default behavior. // Receiver is given the chance to change the |dock_side|. virtual bool DevToolsShow(std::string* dock_side) { return false; } + + // Requested by WebContents of devtools. + virtual void DevToolsSaveToFile( + const std::string& url, const std::string& content, bool save_as) {} + virtual void DevToolsAppendToFile( + const std::string& url, const std::string& content) {} }; } // namespace brightray diff --git a/brightray/browser/inspectable_web_contents_impl.cc b/brightray/browser/inspectable_web_contents_impl.cc index b22b4a287fb..1ccb4e1054d 100644 --- a/brightray/browser/inspectable_web_contents_impl.cc +++ b/brightray/browser/inspectable_web_contents_impl.cc @@ -153,10 +153,14 @@ void InspectableWebContentsImpl::OpenInNewTab(const std::string& url) { void InspectableWebContentsImpl::SaveToFile( const std::string& url, const std::string& content, bool save_as) { + if (delegate_) + delegate_->DevToolsSaveToFile(url, content, save_as); } void InspectableWebContentsImpl::AppendToFile( const std::string& url, const std::string& content) { + if (delegate_) + delegate_->DevToolsAppendToFile(url, content); } void InspectableWebContentsImpl::RequestFileSystems() { From 5e0dd5c687dabb135ede4fe1b1bd77c2121287bb Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 23 Apr 2014 11:20:12 +0800 Subject: [PATCH 5/5] Remove from notification center when web contents is destroyed. --- brightray/browser/inspectable_web_contents_view_mac.h | 1 + brightray/browser/inspectable_web_contents_view_mac.mm | 4 ++++ brightray/browser/mac/bry_inspectable_web_contents_view.h | 1 + brightray/browser/mac/bry_inspectable_web_contents_view.mm | 4 ++++ 4 files changed, 10 insertions(+) diff --git a/brightray/browser/inspectable_web_contents_view_mac.h b/brightray/browser/inspectable_web_contents_view_mac.h index 86cc7822178..927a91a362d 100644 --- a/brightray/browser/inspectable_web_contents_view_mac.h +++ b/brightray/browser/inspectable_web_contents_view_mac.h @@ -15,6 +15,7 @@ class InspectableWebContentsViewMac : public InspectableWebContentsView { public: explicit InspectableWebContentsViewMac( InspectableWebContentsImpl* inspectable_web_contents_impl); + virtual ~InspectableWebContentsViewMac(); virtual gfx::NativeView GetNativeView() const OVERRIDE; virtual void ShowDevTools() OVERRIDE; diff --git a/brightray/browser/inspectable_web_contents_view_mac.mm b/brightray/browser/inspectable_web_contents_view_mac.mm index 95ed74f2dbd..54ad9f19404 100644 --- a/brightray/browser/inspectable_web_contents_view_mac.mm +++ b/brightray/browser/inspectable_web_contents_view_mac.mm @@ -17,6 +17,10 @@ InspectableWebContentsViewMac::InspectableWebContentsViewMac(InspectableWebConte view_([[BRYInspectableWebContentsView alloc] initWithInspectableWebContentsViewMac:this]) { } +InspectableWebContentsViewMac::~InspectableWebContentsViewMac() { + [view_ removeFromNotificationCenter]; +} + gfx::NativeView InspectableWebContentsViewMac::GetNativeView() const { return view_.get(); } diff --git a/brightray/browser/mac/bry_inspectable_web_contents_view.h b/brightray/browser/mac/bry_inspectable_web_contents_view.h index 49eff626321..5c17e6c0165 100644 --- a/brightray/browser/mac/bry_inspectable_web_contents_view.h +++ b/brightray/browser/mac/bry_inspectable_web_contents_view.h @@ -8,6 +8,7 @@ BRYInspectableWebContentsViewPrivate *_private; } +- (void)removeFromNotificationCenter; - (IBAction)showDevTools:(id)sender; @end diff --git a/brightray/browser/mac/bry_inspectable_web_contents_view.mm b/brightray/browser/mac/bry_inspectable_web_contents_view.mm index f193f7ef765..edc455c2fd7 100644 --- a/brightray/browser/mac/bry_inspectable_web_contents_view.mm +++ b/brightray/browser/mac/bry_inspectable_web_contents_view.mm @@ -76,6 +76,10 @@ void SetActive(content::WebContents* web_contents, bool active) { [super dealloc]; } +- (void)removeFromNotificationCenter { + [NSNotificationCenter.defaultCenter removeObserver:self]; +} + - (IBAction)showDevTools:(id)sender { _private->inspectableWebContentsView->inspectable_web_contents()->ShowDevTools(); }