base::DictionaryValue* options)
: content::WebContentsObserver(web_contents),
inspectable_web_contents_(
- brightray::InspectableWebContents::Create(web_contents)),
- window_going_to_destroy_(false),
- can_destroy_window_(false) {
+ brightray::InspectableWebContents::Create(web_contents)) {
web_contents->SetDelegate(this);
windows_.push_back(this);
inspectable_web_contents()->GetView()->CloseDevTools();
}
-void NativeWindow::RequestToDestroyWindow() {
- if (window_going_to_destroy_)
- return;
-
- window_going_to_destroy_ = true;
-
+void NativeWindow::CloseWebContents() {
content::WebContents* web_contents(GetWebContents());
- web_contents->OnCloseStarted();
-
if (web_contents->NeedToFireBeforeUnload())
web_contents->GetRenderViewHost()->FirePageBeforeUnload(false);
else
web_contents->Close();
}
-bool NativeWindow::CanClose() {
- return !GetWebContents()->NeedToFireBeforeUnload() || can_destroy_window_;
-}
-
content::WebContents* NativeWindow::GetWebContents() const {
return inspectable_web_contents_->GetWebContents();
}
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) {
// 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.
- Close();
+ CloseImmediately();
}
bool NativeWindow::OnMessageReceived(const IPC::Message& message) {
void InitFromOptions(base::DictionaryValue* options);
virtual void Close() = 0;
+ virtual void CloseImmediately() = 0;
virtual void Move(const gfx::Rect& pos) = 0;
virtual void Focus(bool focus) = 0;
virtual void Show() = 0;
virtual void ShowDevTools();
virtual void CloseDevTools();
- // Close the web page in this window and then desctruct.
- virtual void RequestToDestroyWindow();
-
- // Used by platform dependent code to determine whether the window can be
- // closed. A window can only be closed when the beforeunload handler
- // doesn't prevent it.
- bool CanClose();
+ // The same with closing a tab in a real browser.
+ //
+ // Should be called by platform code when user want to close the window.
+ virtual void CloseWebContents();
content::WebContents* GetWebContents() const;
virtual content::JavaScriptDialogManager*
GetJavaScriptDialogManager() 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.
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
scoped_ptr<brightray::InspectableWebContents> inspectable_web_contents_;
bool window_going_to_destroy_;
- bool can_destroy_window_;
DISALLOW_COPY_AND_ASSIGN(NativeWindow);
};
// NativeWindow implementation.
virtual void Close() OVERRIDE;
+ virtual void CloseImmediately() OVERRIDE;
virtual void Move(const gfx::Rect& pos) OVERRIDE;
virtual void Focus(bool focus) OVERRIDE;
virtual void Show() OVERRIDE;
return self;
}
-- (BOOL)windowShouldClose:(id)window {
- if (!shell_->CanClose()) {
- shell_->RequestToDestroyWindow();
- return NO;
- }
-
- [self release];
+- (void)windowWillClose:(NSNotification *)notification {
+ [self autorelease];
+}
- return YES;
+- (BOOL)windowShouldClose:(id)window {
+ // When user tries to close the window by clicking the close button, we do
+ // 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;
}
@end
[window() performClose:nil];
}
+void NativeWindowMac::CloseImmediately() {
+ [window() close];
+}
+
void NativeWindowMac::Move(const gfx::Rect& pos) {
NSRect cocoa_bounds = NSMakeRect(pos.x(), 0,
pos.width(),