File: tab_ui_helper.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; 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 (104 lines) | stat: -rw-r--r-- 3,712 bytes parent folder | download | duplicates (3)
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
// Copyright 2017 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/ui/tab_ui_helper.h"

#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "build/build_config.h"
#include "chrome/browser/favicon/favicon_utils.h"
#include "chrome/browser/sessions/session_restore.h"
#include "chrome/browser/ui/tabs/public/tab_features.h"
#include "chrome/browser/ui/tabs/saved_tab_groups/saved_tab_group_web_contents_listener.h"
#include "chrome/grit/generated_resources.h"
#include "components/tabs/public/tab_interface.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/resources/grit/ui_resources.h"

namespace {

// Whether the throbber should be shown for a restored tab after it becomes
// visible, instead of when it's active in the tab strip (this signal is known
// to be broken crbug.com/413080225#comment8).
BASE_FEATURE(kSessionRestoreShowThrobberOnVisible,
             "SessionRestoreShowThrobberOnVisible",
             base::FEATURE_DISABLED_BY_DEFAULT);

}  // namespace

TabUIHelper::TabUIHelper(tabs::TabInterface& tab_interface)
    : ContentsObservingTabFeature(tab_interface) {}

TabUIHelper::~TabUIHelper() = default;

std::u16string TabUIHelper::GetTitle() const {
  tabs::TabInterface* const tab_interface =
      tabs::TabInterface::GetFromContents(web_contents());
  const tab_groups::SavedTabGroupWebContentsListener* wc_listener =
      tab_interface->GetTabFeatures()->saved_tab_group_web_contents_listener();
  if (wc_listener) {
    if (const std::optional<tab_groups::DeferredTabState>& deferred_tab_state =
            wc_listener->deferred_tab_state()) {
      return deferred_tab_state.value().title();
    }
  }

  const std::u16string& contents_title = web_contents()->GetTitle();
  if (!contents_title.empty()) {
    return contents_title;
  }

#if BUILDFLAG(IS_MAC)
  return l10n_util::GetStringUTF16(IDS_BROWSER_WINDOW_MAC_TAB_UNTITLED);
#else
  return std::u16string();
#endif
}

ui::ImageModel TabUIHelper::GetFavicon() const {
  tabs::TabInterface* const tab_interface =
      tabs::TabInterface::GetFromContents(web_contents());
  const tab_groups::SavedTabGroupWebContentsListener* wc_listener =
      tab_interface->GetTabFeatures()->saved_tab_group_web_contents_listener();
  if (wc_listener) {
    if (const std::optional<tab_groups::DeferredTabState>& deferred_tab_state =
            wc_listener->deferred_tab_state()) {
      return deferred_tab_state.value().favicon();
    }
  }

  return ui::ImageModel::FromImage(
      favicon::TabFaviconFromWebContents(web_contents()));
}

bool TabUIHelper::ShouldHideThrobber() const {
  // We want to hide a background tab's throbber during page load if it is
  // created by session restore. A restored tab's favicon is already fetched
  // by |SessionRestoreDelegate|.
  if (created_by_session_restore_ && !was_active_at_least_once_) {
    return true;
  }

  return false;
}

void TabUIHelper::SetWasActiveAtLeastOnce() {
  if (!base::FeatureList::IsEnabled(kSessionRestoreShowThrobberOnVisible)) {
    was_active_at_least_once_ = true;
  }
}

void TabUIHelper::DidStopLoading() {
  // Reset the properties after the initial navigation finishes loading, so that
  // latter navigations are not affected. Note that the prerendered page won't
  // reset the properties because DidStopLoading is not called for prerendering.
  created_by_session_restore_ = false;
}

void TabUIHelper::OnVisibilityChanged(content::Visibility visiblity) {
  if (base::FeatureList::IsEnabled(kSessionRestoreShowThrobberOnVisible) &&
      visiblity == content::Visibility::VISIBLE) {
    was_active_at_least_once_ = true;
  }
}