1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/glic/host/context/glic_pinned_tab_manager.h"
#include "chrome/browser/glic/glic_pref_names.h"
#include "chrome/browser/glic/host/context/glic_page_context_fetcher.h"
#include "chrome/browser/glic/host/context/glic_sharing_manager_impl.h"
#include "chrome/browser/glic/host/context/glic_tab_data.h"
#include "components/prefs/pref_service.h"
namespace glic {
namespace {
// An arbitrary limit.
const int32_t kDefaultMaxPinnedTabs = 5;
// A limit to use when the number set by the client is "unlimited". This is an
// arbitrary large number.
const int32_t kMaxPinnedTabs = 256;
bool IsForeground(content::Visibility visibility) {
return visibility != content::Visibility::HIDDEN;
}
} // namespace
class GlicPinnedTabManager::PinnedTabObserver
: public content::WebContentsObserver {
public:
PinnedTabObserver(
tabs::TabInterface* tab,
base::RepeatingCallback<void(tabs::TabHandle, glic::mojom::TabDataPtr)>
tab_data_changed,
base::OnceCallback<void(tabs::TabHandle)> tab_will_close)
: content::WebContentsObserver(tab->GetContents()),
tab_(tab),
tab_data_changed_(std::move(tab_data_changed)),
tab_will_close_(std::move(tab_will_close)) {
will_discard_contents_subscription_ =
tab_->RegisterWillDiscardContents(base::BindRepeating(
&PinnedTabObserver::OnWillDiscardContents, base::Unretained(this)));
will_detach_subscription_ = tab_->RegisterWillDetach(base::BindRepeating(
&PinnedTabObserver::OnWillDetach, base::Unretained(this)));
StartObservation(tab->GetContents());
content::WebContents* web_contents = tab->GetContents();
if (web_contents) {
is_audible_ = web_contents->IsCurrentlyAudible();
is_foreground_ = IsForeground(web_contents->GetVisibility());
}
}
~PinnedTabObserver() override { ClearObservation(); }
PinnedTabObserver(const PinnedTabObserver&) = delete;
PinnedTabObserver& operator=(const PinnedTabObserver&) = delete;
// content::WebContentsObserver.
void OnAudioStateChanged(bool audible) override {
bool was_observable = IsObservable();
is_audible_ = audible;
if (was_observable != IsObservable()) {
UpdateTabDataAndSend(CreateTabData(web_contents()));
}
}
void OnVisibilityChanged(content::Visibility visibility) override {
bool was_observable = IsObservable();
is_foreground_ = IsForeground(visibility);
if (was_observable != IsObservable()) {
UpdateTabDataAndSend(CreateTabData(web_contents()));
}
}
// tabs::TabInterface
void OnWillDetach(tabs::TabInterface* tab,
tabs::TabInterface::DetachReason reason) {
if (reason == tabs::TabInterface::DetachReason::kDelete) {
ClearObservation();
std::move(tab_will_close_).Run(tab->GetHandle());
}
}
void OnWillDiscardContents(tabs::TabInterface* tab,
content::WebContents* old_contents,
content::WebContents* new_contents) {
CHECK_EQ(web_contents(), old_contents);
StartObservation(new_contents);
}
void FocusedTabDataChanged(glic::mojom::TabDataPtr tab_data) {
UpdateTabDataAndSend(std::move(tab_data));
}
bool IsObservable() const { return is_foreground_ || is_audible_; }
private:
void UpdateTabDataAndSend(glic::mojom::TabDataPtr tab_data) {
// Add observability info.
tab_data->is_observable = IsObservable();
tab_data_changed_.Run(tab_->GetHandle(), std::move(tab_data));
}
void StartObservation(content::WebContents* contents) {
Observe(contents);
tab_data_observer_ = std::make_unique<TabDataObserver>(
contents, base::BindRepeating(&PinnedTabObserver::FocusedTabDataChanged,
base::Unretained(this)));
}
void ClearObservation() {
Observe(nullptr);
tab_data_observer_.reset();
}
raw_ptr<tabs::TabInterface> tab_;
base::CallbackListSubscription will_discard_contents_subscription_;
base::CallbackListSubscription will_detach_subscription_;
bool is_foreground_ = false;
bool is_audible_ = false;
std::unique_ptr<TabDataObserver> tab_data_observer_;
base::RepeatingCallback<void(tabs::TabHandle, glic::mojom::TabDataPtr)>
tab_data_changed_;
base::OnceCallback<void(tabs::TabHandle)> tab_will_close_;
};
GlicPinnedTabManager::PinnedTabEntry::PinnedTabEntry(
tabs::TabHandle tab_handle,
std::unique_ptr<PinnedTabObserver> tab_observer)
: tab_handle(tab_handle), tab_observer(std::move(tab_observer)) {}
GlicPinnedTabManager::PinnedTabEntry::~PinnedTabEntry() = default;
GlicPinnedTabManager::PinnedTabEntry::PinnedTabEntry(PinnedTabEntry&& other) {
*this = std::move(other);
}
GlicPinnedTabManager::PinnedTabEntry&
GlicPinnedTabManager::PinnedTabEntry::operator=(PinnedTabEntry&& other) {
tab_handle = std::move(other.tab_handle);
tab_observer = std::move(other.tab_observer);
return *this;
}
GlicPinnedTabManager::GlicPinnedTabManager(
GlicSharingManagerImpl* sharing_manager)
: sharing_manager_(sharing_manager),
max_pinned_tabs_(kDefaultMaxPinnedTabs) {}
GlicPinnedTabManager::~GlicPinnedTabManager() = default;
base::CallbackListSubscription
GlicPinnedTabManager::AddPinnedTabsChangedCallback(
PinnedTabsChangedCallback callback) {
return pinned_tabs_changed_callback_list_.Add(std::move(callback));
}
base::CallbackListSubscription
GlicPinnedTabManager::AddPinnedTabDataChangedCallback(
PinnedTabDataChangedCallback callback) {
return pinned_tab_data_changed_callback_list_.Add(std::move(callback));
}
base::CallbackListSubscription
GlicPinnedTabManager::AddTabPinningStatusChangedCallback(
TabPinningStatusChangedCallback callback) {
return pinning_status_changed_callback_list_.Add(std::move(callback));
}
bool GlicPinnedTabManager::PinTabs(
base::span<const tabs::TabHandle> tab_handles) {
bool pinning_fully_succeeded = true;
for (const auto tab_handle : tab_handles) {
if (pinned_tabs_.size() == static_cast<size_t>(max_pinned_tabs_)) {
pinning_fully_succeeded = false;
break;
}
auto* tab = tab_handle.Get();
if (!tab || IsTabPinned(tab_handle) ||
!sharing_manager_->IsBrowserValidForSharing(
tab->GetBrowserWindowInterface())) {
pinning_fully_succeeded = false;
continue;
}
pinned_tabs_.emplace_back(
tab_handle,
std::make_unique<PinnedTabObserver>(
tab_handle.Get(),
base::BindRepeating(&GlicPinnedTabManager::OnTabDataChanged,
weak_ptr_factory_.GetWeakPtr()),
base::BindOnce(&GlicPinnedTabManager::OnTabWillClose,
weak_ptr_factory_.GetWeakPtr())));
pinning_status_changed_callback_list_.Notify(tab_handle.Get(), true);
}
NotifyPinnedTabsChanged();
return pinning_fully_succeeded;
}
bool GlicPinnedTabManager::UnpinTabs(
base::span<const tabs::TabHandle> tab_handles) {
bool unpinning_fully_succeeded = true;
for (const auto tab_handle : tab_handles) {
auto* tab = tab_handle.Get();
if (!tab || !IsTabPinned(tab_handle)) {
unpinning_fully_succeeded = false;
continue;
}
std::erase_if(pinned_tabs_, [tab_handle](const PinnedTabEntry& entry) {
return entry.tab_handle == tab_handle;
});
pinning_status_changed_callback_list_.Notify(tab_handle.Get(), true);
}
NotifyPinnedTabsChanged();
return unpinning_fully_succeeded;
}
void GlicPinnedTabManager::UnpinAllTabs() {
std::vector<tabs::TabHandle> tabs_to_unpin;
for (auto& entry : pinned_tabs_) {
tabs_to_unpin.push_back(entry.tab_handle);
}
UnpinTabs(tabs_to_unpin);
}
const GlicPinnedTabManager::PinnedTabEntry*
GlicPinnedTabManager::GetPinnedTabEntry(tabs::TabHandle tab_handle) const {
auto it = std::find_if(pinned_tabs_.begin(), pinned_tabs_.end(),
[tab_handle](const PinnedTabEntry& entry) {
return entry.tab_handle == tab_handle;
});
if (it == pinned_tabs_.end()) {
return nullptr;
}
return &(*it);
}
uint32_t GlicPinnedTabManager::SetMaxPinnedTabs(uint32_t max_pinned_tabs) {
if (max_pinned_tabs < GetNumPinnedTabs()) {
max_pinned_tabs = GetNumPinnedTabs();
} else if (max_pinned_tabs > kMaxPinnedTabs) {
max_pinned_tabs = kMaxPinnedTabs;
}
max_pinned_tabs_ = max_pinned_tabs;
return max_pinned_tabs;
}
uint32_t GlicPinnedTabManager::GetMaxPinnedTabs() const {
return max_pinned_tabs_;
}
uint32_t GlicPinnedTabManager::GetNumPinnedTabs() const {
return static_cast<uint32_t>(pinned_tabs_.size());
}
bool GlicPinnedTabManager::IsTabPinned(tabs::TabHandle tab_handle) const {
return !!GetPinnedTabEntry(tab_handle);
}
std::vector<content::WebContents*> GlicPinnedTabManager::GetPinnedTabs() const {
std::vector<content::WebContents*> pinned_contents;
for (auto& entry : pinned_tabs_) {
pinned_contents.push_back(entry.tab_observer->web_contents());
}
return pinned_contents;
}
void GlicPinnedTabManager::NotifyPinnedTabsChanged() {
pinned_tabs_changed_callback_list_.Notify(GetPinnedTabs());
}
void GlicPinnedTabManager::OnTabDataChanged(tabs::TabHandle tab_handle,
glic::mojom::TabDataPtr tab_data) {
CHECK(IsTabPinned(tab_handle));
pinned_tab_data_changed_callback_list_.Notify(tab_data ? tab_data.get()
: nullptr);
}
void GlicPinnedTabManager::OnTabWillClose(tabs::TabHandle tab_handle) {
// TODO(b/426644733): Avoid n^2 work when closing all tabs.
CHECK(UnpinTabs({tab_handle}));
NotifyPinnedTabsChanged();
}
} // namespace glic
|