diff --git a/browser/ui/views/sidebar/sidebar_container_view.cc b/browser/ui/views/sidebar/sidebar_container_view.cc index 181f07afe147..b583913a3b7c 100644 --- a/browser/ui/views/sidebar/sidebar_container_view.cc +++ b/browser/ui/views/sidebar/sidebar_container_view.cc @@ -186,7 +186,6 @@ void SidebarContainerView::WillShowSidePanel() { StartObservingContextualSidePanelEntry(active_web_contents); auto* global_registry = side_panel_coordinator_->GetWindowRegistry(); - AddSidePanelRegistryObservation(global_registry); for (const auto& entry : global_registry->entries()) { AddSidePanelEntryObservation(entry.get()); } @@ -803,14 +802,6 @@ void SidebarContainerView::OnEntryHidden(SidePanelEntry* entry) { } } -void SidebarContainerView::OnEntryWillDestroy(SidePanelEntry* entry) { - side_panel_entries_observed_.erase(entry); -} - -void SidebarContainerView::OnRegistryWillDestroy(SidePanelRegistry* registry) { - side_panel_registry_observation_.RemoveObservation(registry); -} - void SidebarContainerView::UpdateActiveItemState() { DVLOG(1) << "Update active item state"; @@ -878,7 +869,6 @@ void SidebarContainerView::StartObservingContextualSidePanelEntry( } // Adding observations for the side panel entries from tab not seen before. - AddSidePanelRegistryObservation(registry); for (const auto& entry : registry->entries()) { AddSidePanelEntryObservation(entry.get()); } @@ -907,20 +897,11 @@ BraveBrowser* SidebarContainerView::GetBraveBrowser() const { } void SidebarContainerView::AddSidePanelEntryObservation(SidePanelEntry* entry) { - if (side_panel_entries_observed_.count(entry)) { + if (entry->IsBeingObservedBy(this)) { return; } - - side_panel_entries_observed_.insert(entry); entry->AddObserver(this); } -void SidebarContainerView::AddSidePanelRegistryObservation( - SidePanelRegistry* registry) { - if (side_panel_registry_observation_.IsObservingSource(registry)) { - return; - } - side_panel_registry_observation_.AddObservation(registry); -} BEGIN_METADATA(SidebarContainerView) END_METADATA diff --git a/browser/ui/views/sidebar/sidebar_container_view.h b/browser/ui/views/sidebar/sidebar_container_view.h index f213e02c6edf..7bfba7dbcab6 100644 --- a/browser/ui/views/sidebar/sidebar_container_view.h +++ b/browser/ui/views/sidebar/sidebar_container_view.h @@ -58,7 +58,6 @@ class SidebarContainerView public SidebarShowOptionsEventDetectWidget::Delegate, public sidebar::SidebarModel::Observer, public SidePanelEntryObserver, - public SidePanelRegistry::Observer, public SidePanelViewStateObserver, public TabStripModelObserver { METADATA_HEADER(SidebarContainerView, views::View) @@ -132,10 +131,6 @@ class SidebarContainerView void OnEntryShown(SidePanelEntry* entry) override; void OnEntryHidden(SidePanelEntry* entry) override; - // SidePanelRegistry::Observer: - void OnEntryWillDestroy(SidePanelEntry* entry) override; - void OnRegistryWillDestroy(SidePanelRegistry* registry) override; - // TabStripModelObserver: void OnTabStripModelChanged( TabStripModel* tab_strip_model, @@ -204,9 +199,6 @@ class SidebarContainerView // Adds a passive observation for the given side panel entry. void AddSidePanelEntryObservation(SidePanelEntry* entry); - // Adds a observation for the given side panel registry. - void AddSidePanelRegistryObservation(SidePanelRegistry* registry); - raw_ptr browser_ = nullptr; raw_ptr side_panel_coordinator_ = nullptr; raw_ptr side_panel_ = nullptr; @@ -230,17 +222,6 @@ class SidebarContainerView base::ScopedObservation sidebar_model_observation_{this}; - - // The multishot observation for the side panel registries, which will - // deregister themselves when going out of scope. - base::ScopedMultiSourceObservation - side_panel_registry_observation_{this}; - - // This is a passive observations for side panel entries, to prevent double - // observer additions. These observations are supposed to outlive the observed - // objects, so `RemoveObservation` is never called on these entries. - base::flat_set side_panel_entries_observed_; }; #endif // BRAVE_BROWSER_UI_VIEWS_SIDEBAR_SIDEBAR_CONTAINER_VIEW_H_ diff --git a/chromium_src/chrome/browser/ui/views/side_panel/side_panel_entry.cc b/chromium_src/chrome/browser/ui/views/side_panel/side_panel_entry.cc new file mode 100644 index 000000000000..cf0ce8fc21aa --- /dev/null +++ b/chromium_src/chrome/browser/ui/views/side_panel/side_panel_entry.cc @@ -0,0 +1,12 @@ +/* Copyright (c) 2024 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#include "chrome/browser/ui/views/side_panel/side_panel_entry.h" + +#include "src/chrome/browser/ui/views/side_panel/side_panel_entry.cc" + +bool SidePanelEntry::IsBeingObservedBy(SidePanelEntryObserver* observer) { + return observers_.HasObserver(observer); +} diff --git a/chromium_src/chrome/browser/ui/views/side_panel/side_panel_entry.h b/chromium_src/chrome/browser/ui/views/side_panel/side_panel_entry.h new file mode 100644 index 000000000000..25b056890129 --- /dev/null +++ b/chromium_src/chrome/browser/ui/views/side_panel/side_panel_entry.h @@ -0,0 +1,17 @@ +/* Copyright (c) 2024 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_VIEWS_SIDE_PANEL_SIDE_PANEL_ENTRY_H_ +#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_VIEWS_SIDE_PANEL_SIDE_PANEL_ENTRY_H_ + +#define SupportsNewTabButton(...) \ + SupportsNewTabButton(__VA_ARGS__); \ + bool IsBeingObservedBy(SidePanelEntryObserver* observer) + +#include "src/chrome/browser/ui/views/side_panel/side_panel_entry.h" // IWYU pragma: export + +#undef SupportsNewTabButton + +#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_VIEWS_SIDE_PANEL_SIDE_PANEL_ENTRY_H_ diff --git a/chromium_src/chrome/browser/ui/views/side_panel/side_panel_registry.cc b/chromium_src/chrome/browser/ui/views/side_panel/side_panel_registry.cc deleted file mode 100644 index 40fce4cdadbb..000000000000 --- a/chromium_src/chrome/browser/ui/views/side_panel/side_panel_registry.cc +++ /dev/null @@ -1,36 +0,0 @@ -/* Copyright (c) 2024 The Brave Authors. All rights reserved. - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at https://mozilla.org/MPL/2.0/. */ - -#include "chrome/browser/ui/views/side_panel/side_panel_registry.h" - -#include "chrome/browser/ui/views/side_panel/side_panel_coordinator.h" - -#define RemoveObserver(...) \ - RemoveObserver(__VA_ARGS__); \ - OnEntryWillDestroy(entry) - -#include "src/chrome/browser/ui/views/side_panel/side_panel_registry.cc" - -#undef RemoveObserver - -RegistryScopeDestructionNotifier::RegistryScopeDestructionNotifier( - SidePanelRegistry* registry) - : registry_(registry) {} -RegistryScopeDestructionNotifier::~RegistryScopeDestructionNotifier() { - registry_->observers_.Notify( - &SidePanelRegistry::Observer::OnRegistryWillDestroy, registry_); -} - -void SidePanelRegistry::AddObserver(SidePanelRegistry::Observer* observer) { - observers_.AddObserver(observer); -} - -void SidePanelRegistry::RemoveObserver(SidePanelRegistry::Observer* observer) { - observers_.RemoveObserver(observer); -} - -void SidePanelRegistry::OnEntryWillDestroy(SidePanelEntry* entry) { - observers_.Notify(&SidePanelRegistry::Observer::OnEntryWillDestroy, entry); -} diff --git a/chromium_src/chrome/browser/ui/views/side_panel/side_panel_registry.h b/chromium_src/chrome/browser/ui/views/side_panel/side_panel_registry.h deleted file mode 100644 index a138b2691cac..000000000000 --- a/chromium_src/chrome/browser/ui/views/side_panel/side_panel_registry.h +++ /dev/null @@ -1,59 +0,0 @@ -/* Copyright (c) 2024 The Brave Authors. All rights reserved. - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at https://mozilla.org/MPL/2.0/. */ - -#ifndef BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_VIEWS_SIDE_PANEL_SIDE_PANEL_REGISTRY_H_ -#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_VIEWS_SIDE_PANEL_SIDE_PANEL_REGISTRY_H_ - -#include "base/memory/raw_ptr.h" - -class SidePanelRegistry; - -// This is a scope to be injected into the `SidePanelRegistry` class to allow -// the posting of `OnRegistryWillDestroy`. This avoids a patch for the default -// destructor declaration for `SidePanelRegistry`. It is important to be -// cautious with this pattern, as its destructor is run at point where -// `SidePanelRegistry` is partially destroyed, so it must only access hire -// members, and not call any `SidePanelRegistry` methods. -class RegistryScopeDestructionNotifier { - public: - explicit RegistryScopeDestructionNotifier(SidePanelRegistry* registry); - ~RegistryScopeDestructionNotifier(); - - private: - raw_ptr registry_; -}; - -// This override injects an `Observer` class into `SidePanelRegistry`. This -// allows us to observer both the life time of `SidePanelRegistry`, and of -// `SidePanelEntry` instances. This is necessary for `SidebarContainerView` to -// be able to observer individual `SidePanelEntry` instances, but at the same -// time to avoid calling `RemoveObserver` on destroyed entries. -#define ClearCachedEntryViews \ - Unused() {} \ - class Observer : public base::CheckedObserver { \ - public: \ - virtual void OnEntryWillDestroy(SidePanelEntry* entry) {} \ - virtual void OnRegistryWillDestroy(SidePanelRegistry* registry) {} \ - \ - protected: \ - ~Observer() override = default; \ - }; \ - \ - private: \ - friend class RegistryScopeDestructionNotifier; \ - base::ObserverList observers_; \ - RegistryScopeDestructionNotifier destruction_notifier_{this}; \ - \ - public: \ - void AddObserver(Observer* observer); \ - void RemoveObserver(Observer* observer); \ - void OnEntryWillDestroy(SidePanelEntry* entry); \ - void ClearCachedEntryViews - -#include "src/chrome/browser/ui/views/side_panel/side_panel_registry.h" // IWYU pragma: export - -#undef ClearCachedEntryViews - -#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_VIEWS_SIDE_PANEL_SIDE_PANEL_REGISTRY_H_