diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 63244922ce8..1163820d2c5 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -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); diff --git a/atom/browser/atom_download_manager_delegate.cc b/atom/browser/atom_download_manager_delegate.cc index eb6f48efe85..46c4af2dc38 100644 --- a/atom/browser/atom_download_manager_delegate.cc +++ b/atom/browser/atom_download_manager_delegate.cc @@ -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; } diff --git a/atom/browser/common_web_contents_delegate.cc b/atom/browser/common_web_contents_delegate.cc index 97fee2f755d..5ab8ed0ee18 100644 --- a/atom/browser/common_web_contents_delegate.cc +++ b/atom/browser/common_web_contents_delegate.cc @@ -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); diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index 71fe65e0d8c..e40459e2c75 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -56,6 +56,8 @@ using content::NavigationEntry; using content::RenderWidgetHostView; using content::RenderWidgetHost; +DEFINE_WEB_CONTENTS_USER_DATA_KEY(atom::NativeWindowRelay); + namespace atom { namespace { diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index 4e6f9d113fb..8f3a04c8866 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -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 { + public: + explicit NativeWindowRelay(base::WeakPtr window) + : key(UserDataKey()), window(window) {} + + void* key; + base::WeakPtr window; + + private: + friend class content::WebContentsUserData; +}; + } // namespace atom #endif // ATOM_BROWSER_NATIVE_WINDOW_H_