From 34e54b93a45a147c0957b83a558ecfd138fb1497 Mon Sep 17 00:00:00 2001
From: Jeremy Apthorp <nornagon@nornagon.net>
Date: Tue, 2 Oct 2018 15:14:43 -0700
Subject: [PATCH] Remove WebContentsUserData::kLocatorKey

https://chromium-review.googlesource.com/c/chromium/src/+/1093015
---
 atom/browser/api/atom_api_web_contents.cc      | 10 ++++++----
 atom/browser/atom_download_manager_delegate.cc |  2 +-
 atom/browser/atom_javascript_dialog_manager.cc |  2 +-
 atom/browser/common_web_contents_delegate.cc   | 12 ++++++------
 atom/browser/native_window.cc                  | 18 +++++++++++++++---
 atom/browser/native_window.h                   | 16 +++++++++-------
 atom/browser/web_contents_preferences.cc       |  2 +-
 7 files changed, 39 insertions(+), 23 deletions(-)

diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc
index c353de359..87456d3dd 100644
--- a/atom/browser/api/atom_api_web_contents.cc
+++ b/atom/browser/api/atom_api_web_contents.cc
@@ -462,7 +462,7 @@ void WebContents::InitWithSessionAndOptions(v8::Isolate* isolate,
       auto* relay =
           NativeWindowRelay::FromWebContents(embedder_->web_contents());
       if (relay)
-        owner_window = relay->window.get();
+        owner_window = relay->GetNativeWindow();
     }
     if (owner_window)
       SetOwnerWindow(owner_window);
@@ -985,7 +985,8 @@ void WebContents::DevToolsOpened() {
 
   // Inherit owner window in devtools when it doesn't have one.
   auto* devtools = managed_web_contents()->GetDevToolsWebContents();
-  bool has_window = devtools->GetUserData(NativeWindowRelay::UserDataKey());
+  bool has_window =
+      devtools->GetUserData(NativeWindowRelay::kNativeWindowRelayUserDataKey);
   if (owner_window() && !has_window)
     handle->SetOwnerWindow(devtools, owner_window());
 
@@ -1820,7 +1821,8 @@ gfx::Size WebContents::GetSizeForNewRenderView(content::WebContents* wc) const {
   if (IsOffScreen() && wc == web_contents()) {
     auto* relay = NativeWindowRelay::FromWebContents(web_contents());
     if (relay) {
-      return relay->window->GetSize();
+      auto* owner_window = relay->GetNativeWindow();
+      return owner_window ? owner_window->GetSize() : gfx::Size();
     }
   }
 
@@ -1913,7 +1915,7 @@ void WebContents::SetEmbedder(const WebContents* embedder) {
     NativeWindow* owner_window = nullptr;
     auto* relay = NativeWindowRelay::FromWebContents(embedder->web_contents());
     if (relay) {
-      owner_window = relay->window.get();
+      owner_window = relay->GetNativeWindow();
     }
     if (owner_window)
       SetOwnerWindow(owner_window);
diff --git a/atom/browser/atom_download_manager_delegate.cc b/atom/browser/atom_download_manager_delegate.cc
index 1071f1620..112fa7be8 100644
--- a/atom/browser/atom_download_manager_delegate.cc
+++ b/atom/browser/atom_download_manager_delegate.cc
@@ -86,7 +86,7 @@ void AtomDownloadManagerDelegate::OnDownloadPathGenerated(
   auto* relay =
       web_contents ? NativeWindowRelay::FromWebContents(web_contents) : nullptr;
   if (relay)
-    window = relay->window.get();
+    window = relay->GetNativeWindow();
 
   auto* web_preferences = WebContentsPreferences::From(web_contents);
   bool offscreen =
diff --git a/atom/browser/atom_javascript_dialog_manager.cc b/atom/browser/atom_javascript_dialog_manager.cc
index f2b137bf1..4a3023766 100644
--- a/atom/browser/atom_javascript_dialog_manager.cc
+++ b/atom/browser/atom_javascript_dialog_manager.cc
@@ -72,7 +72,7 @@ void AtomJavaScriptDialogManager::RunJavaScriptDialog(
   if (web_preferences && !web_preferences->IsEnabled(options::kOffscreen)) {
     auto* relay = NativeWindowRelay::FromWebContents(web_contents);
     if (relay)
-      window = relay->window.get();
+      window = relay->GetNativeWindow();
   }
 
   atom::ShowMessageBox(
diff --git a/atom/browser/common_web_contents_delegate.cc b/atom/browser/common_web_contents_delegate.cc
index b4eea58dc..00a179698 100644
--- a/atom/browser/common_web_contents_delegate.cc
+++ b/atom/browser/common_web_contents_delegate.cc
@@ -189,17 +189,17 @@ void CommonWebContentsDelegate::SetOwnerWindow(NativeWindow* owner_window) {
 void CommonWebContentsDelegate::SetOwnerWindow(
     content::WebContents* web_contents,
     NativeWindow* owner_window) {
-  owner_window_ = owner_window ? owner_window->GetWeakPtr() : nullptr;
-  auto relay = std::make_unique<NativeWindowRelay>(owner_window_);
-  auto* relay_key = relay->key;
   if (owner_window) {
+    owner_window_ = owner_window->GetWeakPtr();
 #if defined(TOOLKIT_VIEWS) && !defined(OS_MACOSX)
     autofill_popup_.reset(new AutofillPopup());
 #endif
-    web_contents->SetUserData(relay_key, std::move(relay));
+    NativeWindowRelay::CreateForWebContents(web_contents,
+                                            owner_window->GetWeakPtr());
   } else {
-    web_contents->RemoveUserData(relay_key);
-    relay.reset();
+    owner_window_ = nullptr;
+    web_contents->RemoveUserData(
+        NativeWindowRelay::kNativeWindowRelayUserDataKey);
   }
 }
 
diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc
index ab575e4f5..21895e4b1 100644
--- a/atom/browser/native_window.cc
+++ b/atom/browser/native_window.cc
@@ -21,8 +21,6 @@
 #include "ui/display/win/screen_win.h"
 #endif
 
-DEFINE_WEB_CONTENTS_USER_DATA_KEY(atom::NativeWindowRelay);
-
 namespace atom {
 
 namespace {
@@ -577,8 +575,22 @@ const views::Widget* NativeWindow::GetWidget() const {
   return widget();
 }
 
+// static
+const void* const NativeWindowRelay::kNativeWindowRelayUserDataKey =
+    &NativeWindowRelay::kNativeWindowRelayUserDataKey;
+
+// static
+void NativeWindowRelay::CreateForWebContents(
+    content::WebContents* web_contents,
+    base::WeakPtr<NativeWindow> window) {
+  DCHECK(web_contents);
+  DCHECK(!web_contents->GetUserData(kNativeWindowRelayUserDataKey));
+  web_contents->SetUserData(kNativeWindowRelayUserDataKey,
+                            base::WrapUnique(new NativeWindowRelay(window)));
+}
+
 NativeWindowRelay::NativeWindowRelay(base::WeakPtr<NativeWindow> window)
-    : key(UserDataKey()), window(window) {}
+    : native_window_(window) {}
 
 NativeWindowRelay::~NativeWindowRelay() = default;
 
diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h
index d0de62249..5adb90434 100644
--- a/atom/browser/native_window.h
+++ b/atom/browser/native_window.h
@@ -346,18 +346,20 @@ class NativeWindow : public base::SupportsUserData,
 class NativeWindowRelay
     : public content::WebContentsUserData<NativeWindowRelay> {
  public:
-  explicit NativeWindowRelay(base::WeakPtr<NativeWindow> window);
+  static const void* const kNativeWindowRelayUserDataKey;
+
+  static void CreateForWebContents(content::WebContents*,
+                                   base::WeakPtr<NativeWindow>);
+
   ~NativeWindowRelay() override;
 
-  static void* UserDataKey() {
-    return content::WebContentsUserData<NativeWindowRelay>::UserDataKey();
-  }
-
-  void* key;
-  base::WeakPtr<NativeWindow> window;
+  NativeWindow* GetNativeWindow() const { return native_window_.get(); }
 
  private:
   friend class content::WebContentsUserData<NativeWindow>;
+  explicit NativeWindowRelay(base::WeakPtr<NativeWindow> window);
+
+  base::WeakPtr<NativeWindow> native_window_;
 };
 
 }  // namespace atom
diff --git a/atom/browser/web_contents_preferences.cc b/atom/browser/web_contents_preferences.cc
index da06856f0..8af76acaf 100644
--- a/atom/browser/web_contents_preferences.cc
+++ b/atom/browser/web_contents_preferences.cc
@@ -329,7 +329,7 @@ void WebContentsPreferences::AppendCommandLineSwitches(
       if (embedder) {
         auto* relay = NativeWindowRelay::FromWebContents(embedder);
         if (relay) {
-          auto* window = relay->window.get();
+          auto* window = relay->GetNativeWindow();
           if (window) {
             const bool visible = window->IsVisible() && !window->IsMinimized();
             if (!visible) {