Simply the closing model.

This commit is contained in:
Cheng Zhao 2013-05-01 16:12:00 +08:00
parent 9f1fe4d2c2
commit edd8410c24
4 changed files with 23 additions and 49 deletions

View file

@ -39,9 +39,7 @@ NativeWindow::NativeWindow(content::WebContents* web_contents,
base::DictionaryValue* options) base::DictionaryValue* options)
: content::WebContentsObserver(web_contents), : content::WebContentsObserver(web_contents),
inspectable_web_contents_( inspectable_web_contents_(
brightray::InspectableWebContents::Create(web_contents)), brightray::InspectableWebContents::Create(web_contents)) {
window_going_to_destroy_(false),
can_destroy_window_(false) {
web_contents->SetDelegate(this); web_contents->SetDelegate(this);
windows_.push_back(this); windows_.push_back(this);
@ -135,26 +133,15 @@ void NativeWindow::CloseDevTools() {
inspectable_web_contents()->GetView()->CloseDevTools(); inspectable_web_contents()->GetView()->CloseDevTools();
} }
void NativeWindow::RequestToDestroyWindow() { void NativeWindow::CloseWebContents() {
if (window_going_to_destroy_)
return;
window_going_to_destroy_ = true;
content::WebContents* web_contents(GetWebContents()); content::WebContents* web_contents(GetWebContents());
web_contents->OnCloseStarted();
if (web_contents->NeedToFireBeforeUnload()) if (web_contents->NeedToFireBeforeUnload())
web_contents->GetRenderViewHost()->FirePageBeforeUnload(false); web_contents->GetRenderViewHost()->FirePageBeforeUnload(false);
else else
web_contents->Close(); web_contents->Close();
} }
bool NativeWindow::CanClose() {
return !GetWebContents()->NeedToFireBeforeUnload() || can_destroy_window_;
}
content::WebContents* NativeWindow::GetWebContents() const { content::WebContents* NativeWindow::GetWebContents() const {
return inspectable_web_contents_->GetWebContents(); return inspectable_web_contents_->GetWebContents();
} }
@ -184,24 +171,11 @@ content::JavaScriptDialogManager* NativeWindow::GetJavaScriptDialogManager() {
return dialog_manager_.get(); return dialog_manager_.get();
} }
void NativeWindow::BeforeUnloadFired(content::WebContents* source,
bool proceed,
bool* proceed_to_fire_unload) {
*proceed_to_fire_unload = proceed;
if (proceed && window_going_to_destroy_) {
can_destroy_window_ = true;
} else {
window_going_to_destroy_ = false;
can_destroy_window_ = false;
}
}
void NativeWindow::CloseContents(content::WebContents* source) { void NativeWindow::CloseContents(content::WebContents* source) {
// When the web contents is gone, close the window immediately, but the // When the web contents is gone, close the window immediately, but the
// wrapper itself will not get destroyed until you call delete. // memory will not be freed until you call delete.
// In this way, it would be safe to manage windows via smart pointers. // In this way, it would be safe to manage windows via smart pointers.
Close(); CloseImmediately();
} }
bool NativeWindow::OnMessageReceived(const IPC::Message& message) { bool NativeWindow::OnMessageReceived(const IPC::Message& message) {

View file

@ -62,6 +62,7 @@ class NativeWindow : public content::WebContentsDelegate,
void InitFromOptions(base::DictionaryValue* options); void InitFromOptions(base::DictionaryValue* options);
virtual void Close() = 0; virtual void Close() = 0;
virtual void CloseImmediately() = 0;
virtual void Move(const gfx::Rect& pos) = 0; virtual void Move(const gfx::Rect& pos) = 0;
virtual void Focus(bool focus) = 0; virtual void Focus(bool focus) = 0;
virtual void Show() = 0; virtual void Show() = 0;
@ -94,13 +95,10 @@ class NativeWindow : public content::WebContentsDelegate,
virtual void ShowDevTools(); virtual void ShowDevTools();
virtual void CloseDevTools(); virtual void CloseDevTools();
// Close the web page in this window and then desctruct. // The same with closing a tab in a real browser.
virtual void RequestToDestroyWindow(); //
// Should be called by platform code when user want to close the window.
// Used by platform dependent code to determine whether the window can be virtual void CloseWebContents();
// closed. A window can only be closed when the beforeunload handler
// doesn't prevent it.
bool CanClose();
content::WebContents* GetWebContents() const; content::WebContents* GetWebContents() const;
@ -129,9 +127,6 @@ class NativeWindow : public content::WebContentsDelegate,
virtual content::JavaScriptDialogManager* virtual content::JavaScriptDialogManager*
GetJavaScriptDialogManager() OVERRIDE; GetJavaScriptDialogManager() OVERRIDE;
virtual void CloseContents(content::WebContents* source) OVERRIDE; virtual void CloseContents(content::WebContents* source) OVERRIDE;
virtual void BeforeUnloadFired(content::WebContents* source,
bool proceed,
bool* proceed_to_fire_unload) OVERRIDE;
// Implementations of content::WebContentsObserver. // Implementations of content::WebContentsObserver.
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
@ -163,7 +158,6 @@ class NativeWindow : public content::WebContentsDelegate,
scoped_ptr<brightray::InspectableWebContents> inspectable_web_contents_; scoped_ptr<brightray::InspectableWebContents> inspectable_web_contents_;
bool window_going_to_destroy_; bool window_going_to_destroy_;
bool can_destroy_window_;
DISALLOW_COPY_AND_ASSIGN(NativeWindow); DISALLOW_COPY_AND_ASSIGN(NativeWindow);
}; };

View file

@ -20,6 +20,7 @@ class NativeWindowMac : public NativeWindow {
// NativeWindow implementation. // NativeWindow implementation.
virtual void Close() OVERRIDE; virtual void Close() OVERRIDE;
virtual void CloseImmediately() OVERRIDE;
virtual void Move(const gfx::Rect& pos) OVERRIDE; virtual void Move(const gfx::Rect& pos) OVERRIDE;
virtual void Focus(bool focus) OVERRIDE; virtual void Focus(bool focus) OVERRIDE;
virtual void Show() OVERRIDE; virtual void Show() OVERRIDE;

View file

@ -36,15 +36,16 @@
return self; return self;
} }
- (void)windowWillClose:(NSNotification *)notification {
[self autorelease];
}
- (BOOL)windowShouldClose:(id)window { - (BOOL)windowShouldClose:(id)window {
if (!shell_->CanClose()) { // When user tries to close the window by clicking the close button, we do
shell_->RequestToDestroyWindow(); // not close the window immediately, instead we try to close the web page
// fisrt, and when the web page is closed the window will also be closed.
shell_->CloseWebContents();
return NO; return NO;
}
[self release];
return YES;
} }
@end @end
@ -121,6 +122,10 @@ void NativeWindowMac::Close() {
[window() performClose:nil]; [window() performClose:nil];
} }
void NativeWindowMac::CloseImmediately() {
[window() close];
}
void NativeWindowMac::Move(const gfx::Rect& pos) { void NativeWindowMac::Move(const gfx::Rect& pos) {
NSRect cocoa_bounds = NSMakeRect(pos.x(), 0, NSRect cocoa_bounds = NSMakeRect(pos.x(), 0,
pos.width(), pos.width(),