Merge pull request #52 from brightray/expose-message-dispatcher

Add InspectableWebContentsDelegate
This commit is contained in:
Cheng Zhao 2014-05-15 17:24:53 +08:00
commit 0a75ddb49f
10 changed files with 84 additions and 5 deletions

View file

@ -43,6 +43,7 @@
'browser/download_manager_delegate.h', 'browser/download_manager_delegate.h',
'browser/inspectable_web_contents.cc', 'browser/inspectable_web_contents.cc',
'browser/inspectable_web_contents.h', 'browser/inspectable_web_contents.h',
'browser/inspectable_web_contents_delegate.h',
'browser/inspectable_web_contents_impl.cc', 'browser/inspectable_web_contents_impl.cc',
'browser/inspectable_web_contents_impl.h', 'browser/inspectable_web_contents_impl.h',
'browser/inspectable_web_contents_view.h', 'browser/inspectable_web_contents_view.h',

View file

@ -5,6 +5,7 @@
namespace brightray { namespace brightray {
class InspectableWebContentsDelegate;
class InspectableWebContentsView; class InspectableWebContentsView;
class InspectableWebContents { class InspectableWebContents {
@ -22,7 +23,12 @@ class InspectableWebContents {
virtual content::WebContents* GetWebContents() const = 0; virtual content::WebContents* GetWebContents() const = 0;
virtual void ShowDevTools() = 0; virtual void ShowDevTools() = 0;
// Close the DevTools completely instead of just hide it.
virtual void CloseDevTools() = 0;
virtual bool IsDevToolsViewShowing() = 0; virtual bool IsDevToolsViewShowing() = 0;
// The delegate manages its own life.
virtual void SetDelegate(InspectableWebContentsDelegate* delegate) = 0;
}; };
} // namespace brightray } // namespace brightray

View file

@ -0,0 +1,33 @@
#ifndef BRIGHTRAY_INSPECTABLE_WEB_CONTENTS_DELEGATE_H_
#define BRIGHTRAY_INSPECTABLE_WEB_CONTENTS_DELEGATE_H_
#include <string>
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.
// 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
#endif // BRIGHTRAY_INSPECTABLE_WEB_CONTENTS_DELEGATE_H_

View file

@ -8,6 +8,7 @@
#include "browser/browser_client.h" #include "browser/browser_client.h"
#include "browser/browser_context.h" #include "browser/browser_context.h"
#include "browser/browser_main_parts.h" #include "browser/browser_main_parts.h"
#include "browser/inspectable_web_contents_delegate.h"
#include "browser/inspectable_web_contents_view.h" #include "browser/inspectable_web_contents_view.h"
#include "base/prefs/pref_registry_simple.h" #include "base/prefs/pref_registry_simple.h"
@ -40,7 +41,8 @@ void InspectableWebContentsImpl::RegisterPrefs(PrefRegistrySimple* registry) {
InspectableWebContentsImpl::InspectableWebContentsImpl( InspectableWebContentsImpl::InspectableWebContentsImpl(
content::WebContents* web_contents) content::WebContents* web_contents)
: web_contents_(web_contents) { : web_contents_(web_contents),
delegate_(nullptr) {
auto context = static_cast<BrowserContext*>( auto context = static_cast<BrowserContext*>(
web_contents_->GetBrowserContext()); web_contents_->GetBrowserContext());
dock_side_ = context->prefs()->GetString(kDockSidePref); dock_side_ = context->prefs()->GetString(kDockSidePref);
@ -92,10 +94,21 @@ void InspectableWebContentsImpl::ShowDevTools() {
std::string()); std::string());
} }
if (delegate_ && delegate_->DevToolsShow(&dock_side_))
return;
view_->SetDockSide(dock_side_); view_->SetDockSide(dock_side_);
view_->ShowDevTools(); view_->ShowDevTools();
} }
void InspectableWebContentsImpl::CloseDevTools() {
if (IsDevToolsViewShowing()) {
view_->CloseDevTools();
devtools_web_contents_.reset();
web_contents_->GetView()->Focus();
}
}
bool InspectableWebContentsImpl::IsDevToolsViewShowing() { bool InspectableWebContentsImpl::IsDevToolsViewShowing() {
return devtools_web_contents_ && view_->IsDevToolsViewShowing(); return devtools_web_contents_ && view_->IsDevToolsViewShowing();
} }
@ -111,17 +124,20 @@ void InspectableWebContentsImpl::ActivateWindow() {
} }
void InspectableWebContentsImpl::CloseWindow() { void InspectableWebContentsImpl::CloseWindow() {
view_->CloseDevTools(); CloseDevTools();
devtools_web_contents_.reset();
web_contents_->GetView()->Focus();
} }
void InspectableWebContentsImpl::MoveWindow(int x, int y) { void InspectableWebContentsImpl::MoveWindow(int x, int y) {
} }
void InspectableWebContentsImpl::SetDockSide(const std::string& side) { 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; return;
} else if (!view_->SetDockSide(side)) {
return;
}
dock_side_ = side; dock_side_ = side;
@ -137,10 +153,14 @@ void InspectableWebContentsImpl::OpenInNewTab(const std::string& url) {
void InspectableWebContentsImpl::SaveToFile( void InspectableWebContentsImpl::SaveToFile(
const std::string& url, const std::string& content, bool save_as) { const std::string& url, const std::string& content, bool save_as) {
if (delegate_)
delegate_->DevToolsSaveToFile(url, content, save_as);
} }
void InspectableWebContentsImpl::AppendToFile( void InspectableWebContentsImpl::AppendToFile(
const std::string& url, const std::string& content) { const std::string& url, const std::string& content) {
if (delegate_)
delegate_->DevToolsAppendToFile(url, content);
} }
void InspectableWebContentsImpl::RequestFileSystems() { void InspectableWebContentsImpl::RequestFileSystems() {

View file

@ -23,6 +23,7 @@ class DevToolsClientHost;
namespace brightray { namespace brightray {
class InspectableWebContentsDelegate;
class InspectableWebContentsView; class InspectableWebContentsView;
class InspectableWebContentsImpl : class InspectableWebContentsImpl :
@ -41,8 +42,13 @@ class InspectableWebContentsImpl :
virtual content::WebContents* GetWebContents() const OVERRIDE; virtual content::WebContents* GetWebContents() const OVERRIDE;
virtual void ShowDevTools() OVERRIDE; virtual void ShowDevTools() OVERRIDE;
virtual void CloseDevTools() OVERRIDE;
virtual bool IsDevToolsViewShowing() OVERRIDE; virtual bool IsDevToolsViewShowing() OVERRIDE;
virtual void SetDelegate(InspectableWebContentsDelegate* delegate) {
delegate_ = delegate;
}
content::WebContents* devtools_web_contents() { content::WebContents* devtools_web_contents() {
return devtools_web_contents_.get(); return devtools_web_contents_.get();
} }
@ -101,6 +107,8 @@ class InspectableWebContentsImpl :
scoped_ptr<DevToolsEmbedderMessageDispatcher> embedder_message_dispatcher_; scoped_ptr<DevToolsEmbedderMessageDispatcher> embedder_message_dispatcher_;
InspectableWebContentsDelegate* delegate_;
DISALLOW_COPY_AND_ASSIGN(InspectableWebContentsImpl); DISALLOW_COPY_AND_ASSIGN(InspectableWebContentsImpl);
}; };

View file

@ -12,6 +12,7 @@ class InspectableWebContentsView {
virtual gfx::NativeView GetNativeView() const = 0; virtual gfx::NativeView GetNativeView() const = 0;
virtual void ShowDevTools() = 0; virtual void ShowDevTools() = 0;
// Hide the DevTools view.
virtual void CloseDevTools() = 0; virtual void CloseDevTools() = 0;
virtual bool IsDevToolsViewShowing() = 0; virtual bool IsDevToolsViewShowing() = 0;
virtual bool SetDockSide(const std::string& side) = 0; virtual bool SetDockSide(const std::string& side) = 0;

View file

@ -15,6 +15,7 @@ class InspectableWebContentsViewMac : public InspectableWebContentsView {
public: public:
explicit InspectableWebContentsViewMac( explicit InspectableWebContentsViewMac(
InspectableWebContentsImpl* inspectable_web_contents_impl); InspectableWebContentsImpl* inspectable_web_contents_impl);
virtual ~InspectableWebContentsViewMac();
virtual gfx::NativeView GetNativeView() const OVERRIDE; virtual gfx::NativeView GetNativeView() const OVERRIDE;
virtual void ShowDevTools() OVERRIDE; virtual void ShowDevTools() OVERRIDE;

View file

@ -17,6 +17,10 @@ InspectableWebContentsViewMac::InspectableWebContentsViewMac(InspectableWebConte
view_([[BRYInspectableWebContentsView alloc] initWithInspectableWebContentsViewMac:this]) { view_([[BRYInspectableWebContentsView alloc] initWithInspectableWebContentsViewMac:this]) {
} }
InspectableWebContentsViewMac::~InspectableWebContentsViewMac() {
[view_ removeFromNotificationCenter];
}
gfx::NativeView InspectableWebContentsViewMac::GetNativeView() const { gfx::NativeView InspectableWebContentsViewMac::GetNativeView() const {
return view_.get(); return view_.get();
} }

View file

@ -8,6 +8,7 @@
BRYInspectableWebContentsViewPrivate *_private; BRYInspectableWebContentsViewPrivate *_private;
} }
- (void)removeFromNotificationCenter;
- (IBAction)showDevTools:(id)sender; - (IBAction)showDevTools:(id)sender;
@end @end

View file

@ -76,6 +76,10 @@ void SetActive(content::WebContents* web_contents, bool active) {
[super dealloc]; [super dealloc];
} }
- (void)removeFromNotificationCenter {
[NSNotificationCenter.defaultCenter removeObserver:self];
}
- (IBAction)showDevTools:(id)sender { - (IBAction)showDevTools:(id)sender {
_private->inspectableWebContentsView->inspectable_web_contents()->ShowDevTools(); _private->inspectableWebContentsView->inspectable_web_contents()->ShowDevTools();
} }