From f2c7943d42eb83ca81c73c619ac9e727744b8967 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 1 Oct 2015 13:45:59 +0800 Subject: [PATCH] Add WebContents.getOwnerBrowserWindow --- atom/browser/api/atom_api_web_contents.cc | 9 +++++++++ atom/browser/api/atom_api_web_contents.h | 3 +++ atom/browser/api/atom_api_window.cc | 11 +++++++++++ atom/browser/api/atom_api_window.h | 4 ++++ atom/browser/native_window.h | 4 +++- 5 files changed, 30 insertions(+), 1 deletion(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 90c5a5146c3b..bbe244da7e88 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -7,6 +7,7 @@ #include #include "atom/browser/api/atom_api_session.h" +#include "atom/browser/api/atom_api_window.h" #include "atom/browser/atom_browser_client.h" #include "atom/browser/atom_browser_context.h" #include "atom/browser/atom_browser_main_parts.h" @@ -914,6 +915,13 @@ v8::Local WebContents::GetWebPreferences(v8::Isolate* isolate) { return mate::ConvertToV8(isolate, *web_preferences->web_preferences()); } +v8::Local WebContents::GetOwnerBrowserWindow() { + if (owner_window()) + return Window::From(isolate(), owner_window()); + else + return v8::Null(isolate()); +} + v8::Local WebContents::Session(v8::Isolate* isolate) { return v8::Local::New(isolate, session_); } @@ -981,6 +989,7 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder( .SetMethod("setAllowTransparency", &WebContents::SetAllowTransparency) .SetMethod("isGuest", &WebContents::IsGuest) .SetMethod("getWebPreferences", &WebContents::GetWebPreferences) + .SetMethod("getOwnerBrowserWindow", &WebContents::GetOwnerBrowserWindow) .SetMethod("hasServiceWorker", &WebContents::HasServiceWorker) .SetMethod("unregisterServiceWorker", &WebContents::UnregisterServiceWorker) diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index fa32d812e684..bbf331848c55 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -134,6 +134,9 @@ class WebContents : public mate::TrackableObject, // Returns the web preferences of current WebContents. v8::Local GetWebPreferences(v8::Isolate* isolate); + // Returns the owner window. + v8::Local GetOwnerBrowserWindow(); + // Properties. v8::Local Session(v8::Isolate* isolate); v8::Local DevToolsWebContents(v8::Isolate* isolate); diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index 7536c708768a..c4beaace6728 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -92,6 +92,7 @@ Window::Window(v8::Isolate* isolate, const mate::Dictionary& options) { web_contents->SetOwnerWindow(window_.get()); window_->InitFromOptions(options); window_->AddObserver(this); + AttachAsUserData(window_.get()); } Window::~Window() { @@ -592,6 +593,16 @@ void Window::BuildPrototype(v8::Isolate* isolate, .SetProperty("webContents", &Window::WebContents, true); } +// static +v8::Local Window::From(v8::Isolate* isolate, + NativeWindow* native_window) { + auto existing = TrackableObject::FromWrappedClass(isolate, native_window); + if (existing) + return existing->GetWrapper(isolate); + else + return v8::Null(isolate); +} + } // namespace api } // namespace atom diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index 8cf26fe58e8a..d2886b5fac94 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -43,6 +43,10 @@ class Window : public mate::TrackableObject, static void BuildPrototype(v8::Isolate* isolate, v8::Local prototype); + // Returns the BrowserWindow object from |native_window|. + static v8::Local From(v8::Isolate* isolate, + NativeWindow* native_window); + NativeWindow* window() const { return window_.get(); } protected: diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index 6350ad247c12..751644e4595c 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -15,6 +15,7 @@ #include "base/memory/scoped_ptr.h" #include "base/memory/weak_ptr.h" #include "base/observer_list.h" +#include "base/supports_user_data.h" #include "content/public/browser/readback_types.h" #include "content/public/browser/web_contents_observer.h" #include "content/public/browser/web_contents_user_data.h" @@ -49,7 +50,8 @@ namespace atom { struct DraggableRegion; -class NativeWindow : public content::WebContentsObserver { +class NativeWindow : public base::SupportsUserData, + public content::WebContentsObserver { public: using CapturePageCallback = base::Callback;