File: glic_focused_tab_manager.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (450 lines) | stat: -rw-r--r-- 15,008 bytes parent folder | download | duplicates (2)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// Copyright 2024 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_focused_tab_manager.h"

#include <optional>

#include "base/functional/bind.h"
#include "chrome/browser/glic/host/context/glic_tab_data.h"
#include "chrome/browser/glic/widget/glic_window_controller.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "content/public/common/url_constants.h"
#include "ui/views/widget/widget.h"
#if BUILDFLAG(IS_MAC)
#include "ui/base/cocoa/appkit_utils.h"
#endif

namespace glic {

namespace {

constexpr base::TimeDelta kDebounceDelay = base::Seconds(0.1);

// URLs allowed to be focused despite other URL validity checks.
// Note: other, non-url-based focus checks still apply.
const base::flat_set<GURL>& GetURLAllowList() {
  static const base::flat_set<GURL> kURLAllowList = {
      // Allow 'blank' pages to avoid flicker during tab creation.
      GURL(),
      GURL("about:blank"),
      GURL(chrome::kChromeUINewTabPageThirdPartyURL),
      GURL(chrome::kChromeUINewTabPageURL),
      GURL(chrome::kChromeUINewTabURL),
      GURL(chrome::kChromeUIWhatsNewURL)};

  return kURLAllowList;
}

}  // namespace

GlicFocusedTabManager::GlicFocusedTabManager(
    Profile* profile,
    GlicWindowController& window_controller)
    : profile_(profile),
      window_controller_(window_controller),
      focused_tab_data_(NoFocusedTabData()) {
  BrowserList::GetInstance()->AddObserver(this);
  window_activation_subscription_ =
      window_controller.AddWindowActivationChangedCallback(base::BindRepeating(
          &GlicFocusedTabManager::OnGlicWindowActivationChanged,
          base::Unretained(this)));
  window_controller.AddStateObserver(this);
}

GlicFocusedTabManager::~GlicFocusedTabManager() {
  browser_subscriptions_.clear();
  widget_observation_.Reset();
  BrowserList::GetInstance()->RemoveObserver(this);
  window_controller_->RemoveStateObserver(this);
}

base::CallbackListSubscription
GlicFocusedTabManager::AddFocusedTabChangedCallback(
    FocusedTabChangedCallback callback) {
  return focused_callback_list_.Add(std::move(callback));
}

base::CallbackListSubscription
GlicFocusedTabManager::AddFocusedTabInstanceChangedCallback(
    FocusedTabInstanceChangedCallback callback) {
  return focused_instance_callback_list_.Add(std::move(callback));
}

base::CallbackListSubscription
GlicFocusedTabManager::AddFocusedTabOrCandidateInstanceChangedCallback(
    FocusedTabOrCandidateInstanceChangedCallback callback) {
  return focused_or_candidate_instance_callback_list_.Add(std::move(callback));
}

base::CallbackListSubscription
GlicFocusedTabManager::AddFocusedTabDataChangedCallback(
    FocusedTabDataChangedCallback callback) {
  return focused_data_callback_list_.Add(std::move(callback));
}

void GlicFocusedTabManager::OnBrowserAdded(Browser* browser) {
  // Subscribe to active tab changes to this browser if it's valid.
  if (IsBrowserValid(browser)) {
    std::vector<base::CallbackListSubscription> subscriptions;

    subscriptions.push_back(browser->RegisterDidBecomeActive(
        base::BindRepeating(&GlicFocusedTabManager::OnBrowserBecameActive,
                            base::Unretained(this))));

    subscriptions.push_back(browser->RegisterDidBecomeInactive(
        base::BindRepeating(&GlicFocusedTabManager::OnBrowserBecameInactive,
                            base::Unretained(this))));

    subscriptions.push_back(browser->RegisterActiveTabDidChange(
        base::BindRepeating(&GlicFocusedTabManager::OnActiveTabChanged,
                            base::Unretained(this))));

    browser_subscriptions_[browser] = std::move(subscriptions);
  }
}

void GlicFocusedTabManager::OnBrowserRemoved(Browser* browser) {
  // Remove the browser if it exists in the map.
  browser_subscriptions_.erase(browser);
  MaybeUpdateFocusedTab();
}

void GlicFocusedTabManager::OnBrowserBecameActive(
    BrowserWindowInterface* browser_interface) {
  // Observe for browser window minimization changes.
  widget_observation_.Reset();
  views::Widget* widget = browser_interface->TopContainer()->GetWidget();
  widget_observation_.Observe(widget);

  // We need to force-notify because even if the focused tab doesn't change, it
  // can be in a different browser window (i.e., the user drag-n-drop the
  // focused tab into a new window). Let the subscribers to decide what to do in
  // this case.
  //
  // TODO(crbug.com/393578218): We should have dedicated subscription lists for
  // different types of notifications.
  MaybeUpdateFocusedTab(/*force_notify=*/true);
}

void GlicFocusedTabManager::OnBrowserBecameInactive(
    BrowserWindowInterface* browser_interface) {
  // Debounce these updates in case Glic Window is about to become active.
  MaybeUpdateFocusedTab(/*force_notify=*/true, /*debounce=*/true);
}

void GlicFocusedTabManager::OnGlicWindowActivationChanged(bool active) {
  // Debounce updates when Glic Window becomes inactive in case a browser window
  // is about to become active.
  MaybeUpdateFocusedTab(/*force_notify=*/false, /*debounce=*/!active);
}

void GlicFocusedTabManager::OnWidgetShowStateChanged(views::Widget* widget) {
  MaybeUpdateFocusedTab();
}

void GlicFocusedTabManager::OnWidgetVisibilityChanged(views::Widget* widget,
                                                      bool visible) {
  MaybeUpdateFocusedTab();
}

void GlicFocusedTabManager::OnWidgetVisibilityOnScreenChanged(
    views::Widget* widget,
    bool visible) {
  MaybeUpdateFocusedTab();
}

void GlicFocusedTabManager::OnWidgetDestroyed(views::Widget* widget) {
  widget_observation_.Reset();
}

void GlicFocusedTabManager::OnActiveTabChanged(
    BrowserWindowInterface* browser_interface) {
  MaybeUpdateFocusedTab();
}

void GlicFocusedTabManager::PrimaryPageChanged(content::Page& page) {
  // We always want to trigger our notify callback here (even if focused tab
  // remains the same) so that subscribers can update if they care about primary
  // page changed events.
  MaybeUpdateFocusedTab(/*force_notify=*/true);
}

void GlicFocusedTabManager::FocusedTabDataChanged(
    glic::mojom::TabDataPtr tab_data) {
  // `TabDataObserver` is responsible for firing this when appropriate, we just
  // forward events along.
  // Note: we omit calling `MaybeUpdateFocusedTab()` here because observing web
  // contents for changes that might impact focused tab container or candidate
  // are handled separately.
  NotifyFocusedTabDataChanged(std::move(tab_data));
}

void GlicFocusedTabManager::PanelStateChanged(
    const glic::mojom::PanelState& panel_state,
    Browser*) {
  MaybeUpdateFocusedTab();
}

void GlicFocusedTabManager::MaybeUpdateFocusedTab(bool force_notify,
                                                  bool debounce) {
  // Cache any calls with force_notify set to true so they don't get swallowed
  // by subsequent calls without it. Otherwise necessary updates might get
  // dropped.
  if (force_notify) {
    cached_force_notify_ = true;
  }
  if (debounce) {
    debouncer_.Start(
        FROM_HERE, kDebounceDelay,
        base::BindOnce(&GlicFocusedTabManager::PerformMaybeUpdateFocusedTab,
                       base::Unretained(this), cached_force_notify_));
  } else {
    // Stop any pending debounced calls so they don't fire needlessly later.
    debouncer_.Stop();
    PerformMaybeUpdateFocusedTab(cached_force_notify_);
  }
}

void GlicFocusedTabManager::PerformMaybeUpdateFocusedTab(bool force_notify) {
  cached_force_notify_ = false;
  struct FocusedTabState new_focused_tab_state = ComputeFocusedTabState();
  bool focus_changed = !focused_tab_state_.IsSame(new_focused_tab_state);
  bool focused_instance_changed = !IsWeakPtrSame(
      focused_tab_state_.focused_tab, new_focused_tab_state.focused_tab);
  bool focused_or_candidate_instance_changed =
      focused_instance_changed ||
      !IsWeakPtrSame(focused_tab_state_.candidate_tab,
                     new_focused_tab_state.candidate_tab);
  if (focus_changed) {
    focused_tab_state_ = new_focused_tab_state;
    focused_tab_data_ = GetFocusedTabData(new_focused_tab_state);
  }

  // If we have one, observe tab candidate. If not, whether that's because there
  // was never one, or because it's been invalidated, turn off tab candidate
  // observation.
  Observe(focused_tab_state_.candidate_tab.get());

  // Similarly set up or turn off tab data observation for the focused tab.
  focused_tab_data_observer_ = std::make_unique<TabDataObserver>(
      focused_tab_state_.focused_tab.get(),
      /*disconnect_on_primary_page_changed=*/false,
      base::BindRepeating(&GlicFocusedTabManager::FocusedTabDataChanged,
                          base::Unretained(this)));

  if (focused_instance_changed) {
    NotifyFocusedTabInstanceChanged(focused_tab_state_.focused_tab.get());
    NotifyFocusedTabDataChanged(
        CreateTabData(focused_tab_state_.focused_tab.get()));
  }

  if (focused_or_candidate_instance_changed) {
    NotifyFocusedTabOrCandidateInstanceChanged(focused_tab_data_);
  }

  if (focus_changed || force_notify) {
    NotifyFocusedTabChanged();
  }
}

struct GlicFocusedTabManager::FocusedTabState
GlicFocusedTabManager::ComputeFocusedTabState() {
  struct FocusedTabState focused_tab_state = FocusedTabState();

  BrowserWindowInterface* candidate_browser = ComputeBrowserCandidate();
  if (candidate_browser) {
    focused_tab_state.candidate_browser = candidate_browser->GetWeakPtr();
  }
  if (!IsBrowserStateValid(candidate_browser)) {
    return focused_tab_state;
  }

  focused_tab_state.focused_browser = focused_tab_state.candidate_browser;

  content::WebContents* candidate_tab = ComputeTabCandidate(candidate_browser);
  if (candidate_tab) {
    focused_tab_state.candidate_tab = candidate_tab->GetWeakPtr();
  }
  if (!IsTabStateValid(candidate_tab)) {
    return focused_tab_state;
  }

  focused_tab_state.focused_tab = focused_tab_state.candidate_tab;

  return focused_tab_state;
}

BrowserWindowInterface* GlicFocusedTabManager::ComputeBrowserCandidate() {
#if BUILDFLAG(IS_MAC)
  if (!ui::IsActiveApplication()) {
    return nullptr;
  }
#endif

  if (window_controller_->IsAttached()) {
    // When attached, we only allow focus if attached window is active.
    Browser* const attached_browser = window_controller_->attached_browser();
    if (attached_browser &&
        (attached_browser->IsActive() || window_controller_->IsActive()) &&
        IsBrowserValid(attached_browser)) {
      return attached_browser;
    }
    return nullptr;
  }

  if (window_controller_->IsActive()) {
    Browser* const profile_last_active =
        chrome::FindLastActiveWithProfile(profile_);
    return IsBrowserValid(profile_last_active) ? profile_last_active : nullptr;
  }

  Browser* const active_browser = BrowserList::GetInstance()->GetLastActive();
  if (active_browser && active_browser->IsActive() &&
      IsBrowserValid(active_browser)) {
    return active_browser;
  }

  return nullptr;
}

content::WebContents* GlicFocusedTabManager::ComputeTabCandidate(
    BrowserWindowInterface* browser_interface) {
  if (IsBrowserValid(browser_interface) &&
      IsBrowserStateValid(browser_interface)) {
    content::WebContents* active_contents =
        browser_interface->GetActiveTabInterface()
            ? browser_interface->GetActiveTabInterface()->GetContents()
            : nullptr;
    if (IsTabValid(active_contents)) {
      return active_contents;
    }
  }

  return nullptr;
}

void GlicFocusedTabManager::NotifyFocusedTabChanged() {
  focused_callback_list_.Notify(GetFocusedTabData());
}

void GlicFocusedTabManager::NotifyFocusedTabInstanceChanged(
    content::WebContents* web_contents) {
  focused_instance_callback_list_.Notify(web_contents);
}

void GlicFocusedTabManager::NotifyFocusedTabOrCandidateInstanceChanged(
    FocusedTabData focused_tab_data) {
  focused_or_candidate_instance_callback_list_.Notify(focused_tab_data);
}

void GlicFocusedTabManager::NotifyFocusedTabDataChanged(
    glic::mojom::TabDataPtr tab_data) {
  focused_data_callback_list_.Notify(tab_data ? tab_data.get() : nullptr);
}

bool GlicFocusedTabManager::IsBrowserValid(
    BrowserWindowInterface* browser_interface) {
  if (!browser_interface) {
    return false;
  }

  if (browser_interface->GetProfile() != profile_) {
    return false;
  }

  if (browser_interface->GetProfile()->IsOffTheRecord()) {
    return false;
  }

  return true;
}

bool GlicFocusedTabManager::IsBrowserStateValid(
    BrowserWindowInterface* browser_interface) {
  if (!browser_interface) {
    return false;
  }

  if (browser_interface->IsMinimized()) {
    return false;
  }

  if (!browser_interface->IsVisible()) {
    return false;
  }

  if (!browser_interface->IsVisibleOnScreen()) {
    return false;
  }

  return true;
}

bool GlicFocusedTabManager::IsTabValid(content::WebContents* web_contents) {
  return web_contents != nullptr;
}

bool GlicFocusedTabManager::IsTabStateValid(
    content::WebContents* web_contents) {
  if (!web_contents) {
    return false;
  }

  auto url =
      const_cast<content::WebContents*>(web_contents)->GetLastCommittedURL();
  if (url.SchemeIsHTTPOrHTTPS() || url.SchemeIsFile() ||
      GetURLAllowList().contains(url)) {
    return true;
  }

  return false;
}

FocusedTabData GlicFocusedTabManager::GetFocusedTabData(
    const GlicFocusedTabManager::FocusedTabState& focused_state) {
  if (focused_state.focused_tab) {
    return {focused_state.focused_tab};
  }

  if (focused_state.candidate_tab) {
    return {NoFocusedTabData("no focusable tab",
                             focused_state.candidate_tab.get())};
  }

  if (focused_state.focused_browser) {
    return {NoFocusedTabData("no focusable tab")};
  }

  if (focused_state.candidate_browser) {
    return {NoFocusedTabData("no focusable browser window")};
  }

  return {NoFocusedTabData("no browser window")};
}

FocusedTabData GlicFocusedTabManager::GetFocusedTabData() {
  return focused_tab_data_;
}

GlicFocusedTabManager::FocusedTabState::FocusedTabState() = default;
GlicFocusedTabManager::FocusedTabState::~FocusedTabState() = default;
GlicFocusedTabManager::FocusedTabState::FocusedTabState(
    const GlicFocusedTabManager::FocusedTabState& src) = default;
GlicFocusedTabManager::FocusedTabState&
GlicFocusedTabManager::FocusedTabState::operator=(
    const GlicFocusedTabManager::FocusedTabState& src) = default;

bool GlicFocusedTabManager::FocusedTabState::IsSame(
    const FocusedTabState& other) const {
  return IsWeakPtrSame(candidate_browser, other.candidate_browser) &&
         IsWeakPtrSame(focused_browser, other.focused_browser) &&
         IsWeakPtrSame(candidate_tab, other.candidate_tab) &&
         IsWeakPtrSame(focused_tab, other.focused_tab);
}
}  // namespace glic