]> khamidou — GitHub mirrors - electron.git/commitdiff
Add WebContents.getOwnerBrowserWindow
authorCheng Zhao <zcbenz@gmail.com>
Thu, 1 Oct 2015 05:45:59 +0000 (13:45 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 1 Oct 2015 05:45:59 +0000 (13:45 +0800)
atom/browser/api/atom_api_web_contents.cc
atom/browser/api/atom_api_web_contents.h
atom/browser/api/atom_api_window.cc
atom/browser/api/atom_api_window.h
atom/browser/native_window.h

index 90c5a5146c3b541d332b9163212d010a482f1b70..bbe244da7e887420d5822e387043219d32984ece 100644 (file)
@@ -7,6 +7,7 @@
 #include <set>
 
 #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<v8::Value> WebContents::GetWebPreferences(v8::Isolate* isolate) {
   return mate::ConvertToV8(isolate, *web_preferences->web_preferences());
 }
 
+v8::Local<v8::Value> WebContents::GetOwnerBrowserWindow() {
+  if (owner_window())
+    return Window::From(isolate(), owner_window());
+  else
+    return v8::Null(isolate());
+}
+
 v8::Local<v8::Value> WebContents::Session(v8::Isolate* isolate) {
   return v8::Local<v8::Value>::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)
index fa32d812e684bdd107cab111eb0f3bb1746f4bdc..bbf331848c5501d2f9f0f42eb5660ab70fc67d58 100644 (file)
@@ -134,6 +134,9 @@ class WebContents : public mate::TrackableObject<WebContents>,
   // Returns the web preferences of current WebContents.
   v8::Local<v8::Value> GetWebPreferences(v8::Isolate* isolate);
 
+  // Returns the owner window.
+  v8::Local<v8::Value> GetOwnerBrowserWindow();
+
   // Properties.
   v8::Local<v8::Value> Session(v8::Isolate* isolate);
   v8::Local<v8::Value> DevToolsWebContents(v8::Isolate* isolate);
index 7536c708768a345b3e398d5d8145c159a87d8f01..c4beaace67285cfe1ded02f57c93dd0fa95c38b5 100644 (file)
@@ -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<v8::Value> 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
index 8cf26fe58e8a7f9aaa11378fb0d9beaba8a361d2..d2886b5fac9476925fa86a99ddf5140b78eff18c 100644 (file)
@@ -43,6 +43,10 @@ class Window : public mate::TrackableObject<Window>,
   static void BuildPrototype(v8::Isolate* isolate,
                              v8::Local<v8::ObjectTemplate> prototype);
 
+  // Returns the BrowserWindow object from |native_window|.
+  static v8::Local<v8::Value> From(v8::Isolate* isolate,
+                                   NativeWindow* native_window);
+
   NativeWindow* window() const { return window_.get(); }
 
  protected:
index 6350ad247c125e0558f49e368fcdae35dc8f1d1d..751644e4595cffec38c5ea200d680b9bf5d7bd0c 100644 (file)
@@ -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<void(const SkBitmap& bitmap)>;