Correctly handle window's lifetime when it's closed.

This commit is contained in:
Cheng Zhao 2013-05-01 23:28:01 +08:00
parent 9e489cae0c
commit afe07521c5
7 changed files with 23 additions and 8 deletions

View file

@ -44,13 +44,24 @@ Window::Window(v8::Handle<v8::Object> wrapper, base::DictionaryValue* options)
}
Window::~Window() {
window_->RemoveObserver(this);
}
void Window::OnPageTitleUpdated(bool* prevent_default,
const std::string& title) {
scoped_ptr<base::ListValue> args(new base::ListValue);
args->AppendString(title);
*prevent_default = Emit("page-title-updated", args.get());
base::ListValue args;
args.AppendString(title);
*prevent_default = Emit("page-title-updated", &args);
}
void Window::WillCloseWindow(bool* prevent_default) {
base::ListValue args;
*prevent_default = Emit("close", &args);
}
void Window::OnWindowClosed() {
// Free memory immediately when window is closed.
delete this;
}
// static

View file

@ -35,6 +35,8 @@ class Window : public EventEmitter,
// Implementations of NativeWindowObserver.
virtual void OnPageTitleUpdated(bool* prevent_default,
const std::string& title) OVERRIDE;
virtual void WillCloseWindow(bool* prevent_default) OVERRIDE;
virtual void OnWindowClosed() OVERRIDE;
private:
static v8::Handle<v8::Value> New(const v8::Arguments &args);

View file

@ -183,7 +183,7 @@ void NativeWindow::CloseContents(content::WebContents* source) {
// memory will not be freed until you call delete.
// In this way, it would be safe to manage windows via smart pointers. If you
// want to free memory when the window is closed, you can do deleting by
// overriding WillCloseWindow method in the observer.
// overriding the WillCloseWindow method in the observer.
CloseImmediately();
FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowClosed());

View file

@ -157,8 +157,6 @@ class NativeWindow : public content::WebContentsDelegate,
scoped_ptr<brightray::InspectableWebContents> inspectable_web_contents_;
bool window_going_to_destroy_;
DISALLOW_COPY_AND_ASSIGN(NativeWindow);
};

View file

@ -50,7 +50,7 @@ class NativeWindowMac : public NativeWindow {
virtual void SetKiosk(bool kiosk) OVERRIDE;
virtual bool IsKiosk() OVERRIDE;
NSWindow* window() const { return window_; }
NSWindow*& window() { return window_; }
protected:
void SetNonLionFullscreen(bool fullscreen);

View file

@ -37,6 +37,7 @@
}
- (void)windowWillClose:(NSNotification *)notification {
shell_->window() = nil;
[self autorelease];
}
@ -114,6 +115,8 @@ NativeWindowMac::NativeWindowMac(content::WebContents* web_contents,
}
NativeWindowMac::~NativeWindowMac() {
if (window())
[window() release];
}
void NativeWindowMac::Close() {
@ -121,6 +124,7 @@ void NativeWindowMac::Close() {
}
void NativeWindowMac::CloseImmediately() {
[window() orderOut:nil];
[window() close];
}

2
vendor/brightray vendored

@ -1 +1 @@
Subproject commit 65af4131fc8d5b3450f89950dddc08be8a720860
Subproject commit a51999007ff325562a7c5a73a76c198a080b37a8