Browse Source

content: Fix WebContentsUserData for usage across components.

https://chromium-review.googlesource.com/c/chromium/src/+/1354402
deepak1556 6 years ago
parent
commit
9439ac108d

+ 1 - 2
atom/browser/api/atom_api_web_contents.cc

@@ -999,8 +999,7 @@ 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::kNativeWindowRelayUserDataKey);
+  bool has_window = devtools->GetUserData(NativeWindowRelay::UserDataKey());
   if (owner_window() && !has_window)
     handle->SetOwnerWindow(devtools, owner_window());
 

+ 4 - 0
atom/browser/api/atom_api_web_contents_view.cc

@@ -31,9 +31,13 @@ class WebContentsViewRelay
 
   atom::api::WebContentsView* view_ = nullptr;
 
+  WEB_CONTENTS_USER_DATA_KEY_DECL();
+
   DISALLOW_COPY_AND_ASSIGN(WebContentsViewRelay);
 };
 
+WEB_CONTENTS_USER_DATA_KEY_IMPL(WebContentsViewRelay)
+
 }  // namespace
 
 namespace atom {

+ 14 - 0
atom/browser/child_web_contents_tracker.cc

@@ -0,0 +1,14 @@
+// Copyright (c) 2019 GitHub, Inc.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#include "atom/browser/child_web_contents_tracker.h"
+
+namespace atom {
+
+ChildWebContentsTracker::ChildWebContentsTracker(
+    content::WebContents* web_contents) {}
+
+WEB_CONTENTS_USER_DATA_KEY_IMPL(ChildWebContentsTracker)
+
+}  // namespace atom

+ 3 - 2
atom/browser/child_web_contents_tracker.h

@@ -18,11 +18,12 @@ struct ChildWebContentsTracker
   GURL url;
   std::string frame_name;
 
-  explicit ChildWebContentsTracker(content::WebContents* web_contents) {}
-
  private:
+  explicit ChildWebContentsTracker(content::WebContents* web_contents);
   friend class content::WebContentsUserData<ChildWebContentsTracker>;
 
+  WEB_CONTENTS_USER_DATA_KEY_DECL();
+
   DISALLOW_COPY_AND_ASSIGN(ChildWebContentsTracker);
 };
 

+ 1 - 2
atom/browser/common_web_contents_delegate.cc

@@ -213,8 +213,7 @@ void CommonWebContentsDelegate::SetOwnerWindow(
                                             owner_window->GetWeakPtr());
   } else {
     owner_window_ = nullptr;
-    web_contents->RemoveUserData(
-        NativeWindowRelay::kNativeWindowRelayUserDataKey);
+    web_contents->RemoveUserData(NativeWindowRelay::UserDataKey());
   }
 #if BUILDFLAG(ENABLE_OSR)
   auto* osr_wcv = GetOffScreenWebContentsView();

+ 6 - 7
atom/browser/native_window.cc

@@ -577,18 +577,15 @@ 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)));
+  if (!web_contents->GetUserData(UserDataKey())) {
+    web_contents->SetUserData(UserDataKey(),
+                              base::WrapUnique(new NativeWindowRelay(window)));
+  }
 }
 
 NativeWindowRelay::NativeWindowRelay(base::WeakPtr<NativeWindow> window)
@@ -596,4 +593,6 @@ NativeWindowRelay::NativeWindowRelay(base::WeakPtr<NativeWindow> window)
 
 NativeWindowRelay::~NativeWindowRelay() = default;
 
+WEB_CONTENTS_USER_DATA_KEY_IMPL(NativeWindowRelay)
+
 }  // namespace atom

+ 2 - 2
atom/browser/native_window.h

@@ -360,8 +360,6 @@ class NativeWindow : public base::SupportsUserData,
 class NativeWindowRelay
     : public content::WebContentsUserData<NativeWindowRelay> {
  public:
-  static const void* const kNativeWindowRelayUserDataKey;
-
   static void CreateForWebContents(content::WebContents*,
                                    base::WeakPtr<NativeWindow>);
 
@@ -369,6 +367,8 @@ class NativeWindowRelay
 
   NativeWindow* GetNativeWindow() const { return native_window_.get(); }
 
+  WEB_CONTENTS_USER_DATA_KEY_DECL();
+
  private:
   friend class content::WebContentsUserData<NativeWindow>;
   explicit NativeWindowRelay(base::WeakPtr<NativeWindow> window);

+ 2 - 0
atom/browser/printing/print_preview_message_handler.cc

@@ -195,4 +195,6 @@ void PrintPreviewMessageHandler::RejectPromise(int request_id) {
   promise->RejectWithErrorMessage("Failed to generate PDF");
 }
 
+WEB_CONTENTS_USER_DATA_KEY_IMPL(PrintPreviewMessageHandler)
+
 }  // namespace atom

+ 2 - 0
atom/browser/printing/print_preview_message_handler.h

@@ -67,6 +67,8 @@ class PrintPreviewMessageHandler
 
   base::WeakPtrFactory<PrintPreviewMessageHandler> weak_ptr_factory_;
 
+  WEB_CONTENTS_USER_DATA_KEY_DECL();
+
   DISALLOW_COPY_AND_ASSIGN(PrintPreviewMessageHandler);
 };
 

+ 2 - 0
atom/browser/web_contents_permission_helper.cc

@@ -154,4 +154,6 @@ bool WebContentsPermissionHelper::CheckMediaAccessPermission(
   return CheckPermission(content::PermissionType::AUDIO_CAPTURE, &details);
 }
 
+WEB_CONTENTS_USER_DATA_KEY_IMPL(WebContentsPermissionHelper)
+
 }  // namespace atom

+ 2 - 0
atom/browser/web_contents_permission_helper.h

@@ -53,6 +53,8 @@ class WebContentsPermissionHelper
 
   content::WebContents* web_contents_;
 
+  WEB_CONTENTS_USER_DATA_KEY_DECL();
+
   DISALLOW_COPY_AND_ASSIGN(WebContentsPermissionHelper);
 };
 

+ 2 - 0
atom/browser/web_contents_preferences.cc

@@ -436,4 +436,6 @@ void WebContentsPreferences::OverrideWebkitPrefs(
     prefs->default_encoding = encoding;
 }
 
+WEB_CONTENTS_USER_DATA_KEY_IMPL(WebContentsPreferences)
+
 }  // namespace atom

+ 2 - 0
atom/browser/web_contents_preferences.h

@@ -82,6 +82,8 @@ class WebContentsPreferences
   base::Value preference_ = base::Value(base::Value::Type::DICTIONARY);
   base::Value last_preference_ = base::Value(base::Value::Type::DICTIONARY);
 
+  WEB_CONTENTS_USER_DATA_KEY_DECL();
+
   DISALLOW_COPY_AND_ASSIGN(WebContentsPreferences);
 };
 

+ 2 - 0
atom/browser/web_contents_zoom_controller.cc

@@ -278,4 +278,6 @@ void WebContentsZoomController::SetZoomFactorOnNavigationIfNeeded(
   SetZoomLevel(zoom_level);
 }
 
+WEB_CONTENTS_USER_DATA_KEY_IMPL(WebContentsZoomController)
+
 }  // namespace atom

+ 2 - 0
atom/browser/web_contents_zoom_controller.h

@@ -112,6 +112,8 @@ class WebContentsZoomController
 
   content::HostZoomMap* host_zoom_map_;
 
+  WEB_CONTENTS_USER_DATA_KEY_DECL();
+
   DISALLOW_COPY_AND_ASSIGN(WebContentsZoomController);
 };
 

+ 1 - 0
filenames.gni

@@ -251,6 +251,7 @@ filenames = {
     "atom/browser/browser_observer.h",
     "atom/browser/browser_process_impl.cc",
     "atom/browser/browser_process_impl.h",
+    "atom/browser/child_web_contents_tracker.cc",
     "atom/browser/child_web_contents_tracker.h",
     "atom/browser/common_web_contents_delegate_mac.mm",
     "atom/browser/common_web_contents_delegate_views.cc",