]> khamidou — GitHub mirrors - electron.git/commitdiff
Store NativeWindow's weak ptr in WebContents
authorCheng Zhao <zcbenz@gmail.com>
Wed, 24 Jun 2015 14:14:46 +0000 (22:14 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 24 Jun 2015 14:14:46 +0000 (22:14 +0800)
atom/browser/api/atom_api_web_contents.cc
atom/browser/atom_download_manager_delegate.cc
atom/browser/common_web_contents_delegate.cc
atom/browser/native_window.cc
atom/browser/native_window.h

index 63244922ce8f7b7209536db85ee538ac19039cee..1163820d2c54be044b13489c9d3d88a46159c9e9 100644 (file)
@@ -177,8 +177,11 @@ WebContents::WebContents(const mate::Dictionary& options)
 
   NativeWindow* owner_window = nullptr;
   WebContents* embedder = nullptr;
-  if (options.Get("embedder", &embedder) && embedder)
-    owner_window = NativeWindow::FromWebContents(embedder->web_contents());
+  if (options.Get("embedder", &embedder) && embedder) {
+    auto relay = NativeWindowRelay::FromWebContents(embedder->web_contents());
+    if (relay)
+      owner_window = relay->window.get();
+  }
 
   AttachAsUserData(web_contents);
   InitWithWebContents(web_contents, owner_window);
index eb6f48efe85160686131c2c42b4e6409b2937c6e..46c4af2dc387e07d45a023d4253a3ca6bd427e02 100644 (file)
@@ -65,12 +65,15 @@ void AtomDownloadManagerDelegate::OnDownloadPathGenerated(
   if (!item)
     return;
 
+  NativeWindow* window = nullptr;
+  auto relay = NativeWindowRelay::FromWebContents(item->GetWebContents());
+  if (relay)
+    window = relay->window.get();
+
   file_dialog::Filters filters;
   base::FilePath path;
-  auto owner_window_ = NativeWindow::FromWebContents(item->GetWebContents());
-  if (!file_dialog::ShowSaveDialog(
-          owner_window_, item->GetURL().spec(),
-          default_path, filters, &path)) {
+  if (!file_dialog::ShowSaveDialog(window, item->GetURL().spec(), default_path,
+                                   filters, &path)) {
     return;
   }
 
index 97fee2f755d1462d0f6fbc3c3f30760299366a1b..5ab8ed0ee1829ab27040187eb664cd3e9feb98da 100644 (file)
@@ -123,6 +123,9 @@ void CommonWebContentsDelegate::InitWithWebContents(
   owner_window_ = owner_window;
   web_contents->SetDelegate(this);
 
+  NativeWindowRelay* relay = new NativeWindowRelay(owner_window_->GetWeakPtr());
+  web_contents->SetUserData(relay->key, relay);
+
   printing::PrintViewManagerBasic::CreateForWebContents(web_contents);
   printing::PrintPreviewMessageHandler::CreateForWebContents(web_contents);
 
index 71fe65e0d8c6b9deec356312fcd5e3353cbe8a4d..e40459e2c75e6fee3280b18458835da3a687768c 100644 (file)
@@ -56,6 +56,8 @@ using content::NavigationEntry;
 using content::RenderWidgetHostView;
 using content::RenderWidgetHost;
 
+DEFINE_WEB_CONTENTS_USER_DATA_KEY(atom::NativeWindowRelay);
+
 namespace atom {
 
 namespace {
index 4e6f9d113fb76c0d7bbf943dd0506254e74659e0..8f3a04c886618d85edf1e8f43db723121abce1ed 100644 (file)
@@ -17,6 +17,7 @@
 #include "base/memory/weak_ptr.h"
 #include "base/observer_list.h"
 #include "content/public/browser/readback_types.h"
+#include "content/public/browser/web_contents_user_data.h"
 #include "native_mate/persistent_dictionary.h"
 #include "ui/gfx/image/image.h"
 
@@ -304,6 +305,21 @@ class NativeWindow : public CommonWebContentsDelegate,
   DISALLOW_COPY_AND_ASSIGN(NativeWindow);
 };
 
+
+// This class provides a hook to get a NativeWindow from a WebContents.
+class NativeWindowRelay :
+    public content::WebContentsUserData<NativeWindowRelay> {
+ public:
+  explicit NativeWindowRelay(base::WeakPtr<NativeWindow> window)
+    : key(UserDataKey()), window(window) {}
+
+  void* key;
+  base::WeakPtr<NativeWindow> window;
+
+ private:
+  friend class content::WebContentsUserData<NativeWindow>;
+};
+
 }  // namespace atom
 
 #endif  // ATOM_BROWSER_NATIVE_WINDOW_H_