File: tab_renderer_data.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (204 lines) | stat: -rw-r--r-- 8,995 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
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
// Copyright 2012 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/tabs/tab_renderer_data.h"

#include "base/process/kill.h"
#include "build/build_config.h"
#include "chrome/browser/collaboration/messaging/messaging_backend_service_factory.h"
#include "chrome/browser/favicon/favicon_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/resource_coordinator/lifecycle_unit_state.mojom-shared.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/performance_controls/memory_saver_utils.h"
#include "chrome/browser/ui/performance_controls/tab_resource_usage_tab_helper.h"
#include "chrome/browser/ui/tab_ui_helper.h"
#include "chrome/browser/ui/tabs/public/tab_features.h"
#include "chrome/browser/ui/tabs/saved_tab_groups/collaboration_messaging_tab_data.h"
#include "chrome/browser/ui/tabs/saved_tab_groups/saved_tab_group_utils.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/tabs/tab_strip_model_delegate.h"
#include "chrome/browser/ui/tabs/tab_utils.h"
#include "chrome/browser/ui/thumbnails/thumbnail_image.h"
#include "chrome/browser/ui/thumbnails/thumbnail_tab_helper.h"
#include "chrome/browser/ui/web_applications/web_app_browser_controller.h"
#include "chrome/browser/ui/web_applications/web_app_tabbed_utils.h"
#include "components/collaboration/public/messaging/messaging_backend_service.h"
#include "components/performance_manager/public/features.h"
#include "components/saved_tab_groups/public/features.h"
#include "components/saved_tab_groups/public/tab_group_sync_service.h"
#include "components/security_interstitials/content/security_interstitial_tab_helper.h"
#include "components/tabs/public/tab_interface.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"

namespace {

using collaboration::messaging::MessagingBackendService;
using collaboration::messaging::MessagingBackendServiceFactory;
using collaboration::messaging::PersistentMessage;

base::WeakPtr<tab_groups::CollaborationMessagingTabData>
GetCollaborationMessage(tabs::TabInterface* tab) {
  if (!tab_groups::SavedTabGroupUtils::SupportsSharedTabGroups()) {
    return nullptr;
  }

  auto* data = tab->GetTabFeatures()->collaboration_messaging_tab_data();
  if (!data) {
    return nullptr;
  }

  return data->GetWeakPtr();
}

}  // namespace

// static
TabRendererData TabRendererData::FromTabInModel(const TabStripModel* model,
                                                int index) {
  tabs::TabInterface* const tab = model->GetTabAtIndex(index);
  CHECK(tab);
  content::WebContents* const contents = tab->GetContents();
  CHECK(contents);

  // If the tab is showing a lookalike interstitial ("Did you mean example.com"
  // on éxample.com), don't show the URL in the hover card because it's
  // misleading.
  security_interstitials::SecurityInterstitialTabHelper*
      security_interstitial_tab_helper = security_interstitials::
          SecurityInterstitialTabHelper::FromWebContents(contents);
  bool should_display_url =
      !security_interstitial_tab_helper ||
      !security_interstitial_tab_helper->IsDisplayingInterstitial() ||
      security_interstitial_tab_helper->ShouldDisplayURL();
  TabRendererData data;

  tabs::TabFeatures* const features = tab->GetTabFeatures();
  TabUIHelper* const tab_ui_helper = features->tab_ui_helper();
  data.favicon = tab_ui_helper->GetFavicon();
  data.title = tab_ui_helper->GetTitle();

  // Tabbed web apps should use the app icon on the home tab.
  BrowserWindowInterface* browser = tab->GetBrowserWindowInterface();

  if (browser && browser->GetAppBrowserController() &&
      browser->GetAppBrowserController()->ShouldShowAppIconOnTab(index)) {
    web_app::WebAppBrowserController* app_controller =
        browser->GetAppBrowserController()->AsWebAppBrowserController();
    if (app_controller) {
      gfx::ImageSkia home_tab_icon = app_controller->GetHomeTabIcon();
      if (!home_tab_icon.isNull()) {
        data.is_monochrome_favicon = true;
        data.favicon = ui::ImageModel::FromImageSkia(home_tab_icon);
      } else {
        home_tab_icon = app_controller->GetFallbackHomeTabIcon();
        if (!home_tab_icon.isNull()) {
          data.favicon = ui::ImageModel::FromImageSkia(home_tab_icon);
        }
      }
    }
  }

  ThumbnailTabHelper* const thumbnail_tab_helper =
      ThumbnailTabHelper::FromWebContents(contents);
  if (thumbnail_tab_helper) {
    data.thumbnail = thumbnail_tab_helper->thumbnail();
  }
  data.is_tab_discarded = contents->WasDiscarded();

  data.collaboration_messaging = GetCollaborationMessage(tab);
  data.network_state = TabNetworkStateForWebContents(contents);
  data.visible_url = contents->GetVisibleURL();
  // Allow empty title for chrome-untrusted:// URLs.
  if (data.title.empty() &&
      data.visible_url.SchemeIs(content::kChromeUIUntrustedScheme)) {
    data.should_render_empty_title = true;
  }
  data.last_committed_url = contents->GetLastCommittedURL();
  data.should_display_url = should_display_url;
  data.crashed_status = contents->GetCrashedStatus();
  data.incognito = contents->GetBrowserContext()->IsOffTheRecord();
  data.pinned = tab->IsPinned();
  data.show_icon =
      data.pinned || model->delegate()->ShouldDisplayFavicon(contents);
  data.blocked = model->IsTabBlocked(index);
  data.should_hide_throbber = tab_ui_helper->ShouldHideThrobber();
  data.alert_state = GetTabAlertStatesForTab(tab);

  content::NavigationEntry* entry =
      contents->GetController().GetLastCommittedEntry();
  data.should_themify_favicon =
      entry && favicon::ShouldThemifyFaviconForEntry(entry);

  std::optional<mojom::LifecycleUnitDiscardReason> discard_reason =
      memory_saver::GetDiscardReason(contents);

  // Only show discard status for tabs that were proactively discarded or
  // suggested by the PerformanceDetectionManager to prevent confusion to users
  // on why a tab was discarded. Also, the favicon discard animation may use
  // resources so the animation should be limited to prevent performance issues.
  data.should_show_discard_status =
      memory_saver::IsURLSupported(contents->GetURL()) &&
      contents->WasDiscarded() && discard_reason.has_value() &&
      (discard_reason.value() == mojom::LifecycleUnitDiscardReason::PROACTIVE ||
       discard_reason.value() == mojom::LifecycleUnitDiscardReason::SUGGESTED);

  if (contents->WasDiscarded()) {
    data.discarded_memory_savings_in_bytes =
        memory_saver::GetDiscardedMemorySavingsInBytes(contents);
  }

  if (const auto* const resource_tab_helper =
          tab->GetTabFeatures()->resource_usage_helper()) {
    data.tab_resource_usage = resource_tab_helper->resource_usage();
  }

  // Attach the weak pointer to the TabInterface
  data.tab_interface = tab->GetWeakPtr();

  return data;
}

TabRendererData::TabRendererData() = default;
TabRendererData::TabRendererData(const TabRendererData& other) = default;
TabRendererData::TabRendererData(TabRendererData&& other) = default;

TabRendererData& TabRendererData::operator=(const TabRendererData& other) =
    default;
TabRendererData& TabRendererData::operator=(TabRendererData&& other) = default;

TabRendererData::~TabRendererData() = default;

bool TabRendererData::operator==(const TabRendererData& other) const {
  return favicon == other.favicon && thumbnail == other.thumbnail &&
         network_state == other.network_state && title == other.title &&
         visible_url == other.visible_url &&
         last_committed_url == other.last_committed_url &&
         should_display_url == other.should_display_url &&
         crashed_status == other.crashed_status &&
         incognito == other.incognito && show_icon == other.show_icon &&
         pinned == other.pinned && blocked == other.blocked &&
         alert_state == other.alert_state &&
         should_hide_throbber == other.should_hide_throbber &&
         is_tab_discarded == other.is_tab_discarded &&
         should_show_discard_status == other.should_show_discard_status &&
         discarded_memory_savings_in_bytes ==
             other.discarded_memory_savings_in_bytes &&
         tab_resource_usage == other.tab_resource_usage &&
         is_monochrome_favicon == other.is_monochrome_favicon &&
         tab_interface.get() == other.tab_interface.get();
}

bool TabRendererData::IsCrashed() const {
  return (crashed_status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED ||
#if BUILDFLAG(IS_CHROMEOS)
          crashed_status ==
              base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM ||
#endif
          crashed_status == base::TERMINATION_STATUS_PROCESS_CRASHED ||
          crashed_status == base::TERMINATION_STATUS_ABNORMAL_TERMINATION ||
          crashed_status == base::TERMINATION_STATUS_LAUNCH_FAILED);
}