diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index 3e4700328036..3e1bb6a3464f 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -136,6 +136,8 @@ void Window::OnWindowClosed() { Emit("closed"); + RemoveFromParentChildWindows(); + // Destroy the native class when window is closed. base::MessageLoop::current()->PostTask(FROM_HERE, GetDestroyClosure()); } @@ -650,13 +652,17 @@ void Window::SetAspectRatio(double aspect_ratio, mate::Arguments* args) { window_->SetAspectRatio(aspect_ratio, extra_size); } -void Window::SetParentWindow(mate::Arguments* args) { - v8::Local value; - NativeWindow* parent; - if (args->GetNext(&value) && - mate::ConvertFromV8(isolate(), value, &parent)) { +void Window::SetParentWindow(v8::Local value, + mate::Arguments* args) { + mate::Handle parent; + if (value->IsNull()) { + RemoveFromParentChildWindows(); + parent_window_.Reset(); + window_->SetParentWindow(nullptr); + } else if (mate::ConvertFromV8(isolate(), value, &parent)) { parent_window_.Reset(isolate(), value); - window_->SetParentWindow(parent); + window_->SetParentWindow(parent->window_.get()); + parent->child_windows_.Set(isolate(), ID(), GetWrapper()); } else { args->ThrowError("Must pass BrowserWindow instance or null"); } @@ -669,6 +675,10 @@ v8::Local Window::GetParentWindow() { return v8::Local::New(isolate(), parent_window_); } +std::vector> Window::GetChildWindows() { + return child_windows_.Values(isolate()); +} + v8::Local Window::GetNativeWindowHandle() { gfx::AcceleratedWidget handle = window_->GetAcceleratedWidget(); return ToBuffer( @@ -694,6 +704,15 @@ v8::Local Window::WebContents(v8::Isolate* isolate) { return v8::Local::New(isolate, web_contents_); } +void Window::RemoveFromParentChildWindows() { + if (parent_window_.IsEmpty()) + return; + + mate::Handle parent; + if (mate::ConvertFromV8(isolate(), GetParentWindow(), &parent)) + parent->child_windows_.Remove(ID()); +} + // static void Window::BuildPrototype(v8::Isolate* isolate, v8::Local prototype) { @@ -718,6 +737,7 @@ void Window::BuildPrototype(v8::Isolate* isolate, .SetMethod("setAspectRatio", &Window::SetAspectRatio) .SetMethod("setParentWindow", &Window::SetParentWindow) .SetMethod("getParentWindow", &Window::GetParentWindow) + .SetMethod("getChildWindows", &Window::GetChildWindows) .SetMethod("getNativeWindowHandle", &Window::GetNativeWindowHandle) .SetMethod("getBounds", &Window::GetBounds) .SetMethod("setBounds", &Window::SetBounds) diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index 007854897ffa..0cc238afb07d 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -6,15 +6,16 @@ #define ATOM_BROWSER_API_ATOM_API_WINDOW_H_ #include +#include #include #include -#include "base/memory/scoped_ptr.h" #include "ui/gfx/image/image.h" #include "atom/browser/api/trackable_object.h" #include "atom/browser/native_window.h" #include "atom/browser/native_window_observer.h" #include "atom/common/api/atom_api_native_image.h" +#include "atom/common/key_weak_map.h" #include "native_mate/handle.h" class GURL; @@ -159,8 +160,9 @@ class Window : public mate::TrackableObject, void SetMenuBarVisibility(bool visible); bool IsMenuBarVisible(); void SetAspectRatio(double aspect_ratio, mate::Arguments* args); - void SetParentWindow(mate::Arguments* args); + void SetParentWindow(v8::Local value, mate::Arguments* args); v8::Local GetParentWindow(); + std::vector> GetChildWindows(); v8::Local GetNativeWindowHandle(); #if defined(OS_WIN) @@ -183,6 +185,9 @@ class Window : public mate::TrackableObject, int32_t ID() const; v8::Local WebContents(v8::Isolate* isolate); + // Helpers. + void RemoveFromParentChildWindows(); + #if defined(OS_WIN) typedef std::map MessageCallbackMap; MessageCallbackMap messages_callback_map_; @@ -191,6 +196,7 @@ class Window : public mate::TrackableObject, v8::Global web_contents_; v8::Global menu_; v8::Global parent_window_; + KeyWeakMap child_windows_; api::WebContents* api_web_contents_; diff --git a/atom/common/key_weak_map.h b/atom/common/key_weak_map.h index bce34bfe0f6e..9c66785c41d4 100644 --- a/atom/common/key_weak_map.h +++ b/atom/common/key_weak_map.h @@ -14,10 +14,6 @@ namespace atom { -namespace internal { - -} // namespace internal - // Like ES6's WeakMap, but the key is Integer and the value is Weak Pointer. template class KeyWeakMap {