Store NativeWindow's weak ptr in WebContents

This commit is contained in:
Cheng Zhao 2015-06-24 22:14:46 +08:00
parent 2532318bee
commit 2bfa9da82e
5 changed files with 33 additions and 6 deletions

View 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);

View 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;
}

View 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);

View 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 {

View 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_