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
|
// 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/ui/views/tab_sharing/tab_sharing_infobar.h"
#include <memory>
#include <utility>
#include "base/functional/bind.h"
#include "build/build_config.h"
#include "chrome/browser/ui/layout_constants.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/chrome_typography.h"
#include "chrome/grit/generated_resources.h"
#include "components/infobars/content/content_infobar_manager.h"
#include "components/vector_icons/vector_icons.h"
#include "extensions/common/constants.h"
#include "media/capture/capture_switches.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/ui_base_features.h"
#include "ui/base/window_open_disposition.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/link.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/style/platform_style.h"
#include "ui/views/view_class_properties.h"
namespace {
constexpr auto kCapturedSurfaceControlIndicatorButtonInsets =
gfx::Insets::VH(4, 8);
url::Origin GetOriginFromId(content::GlobalRenderFrameHostId rfh_id) {
content::RenderFrameHost* rfh = content::RenderFrameHost::FromID(rfh_id);
if (!rfh) {
return {};
}
return rfh->GetLastCommittedOrigin();
}
} // namespace
TabSharingInfoBar::TabSharingInfoBar(
std::unique_ptr<TabSharingInfoBarDelegate> delegate,
content::GlobalRenderFrameHostId shared_tab_id,
content::GlobalRenderFrameHostId capturer_id,
const std::u16string& shared_tab_name,
const std::u16string& capturer_name,
TabSharingInfoBarDelegate::TabRole role,
TabSharingInfoBarDelegate::TabShareType capture_type)
: InfoBarView(std::move(delegate)) {
auto* delegate_ptr = GetDelegate();
status_message_view_ = AddChildView(
CreateStatusMessageView(shared_tab_id, capturer_id, shared_tab_name,
capturer_name, role, capture_type));
const int buttons = delegate_ptr->GetButtons();
const auto create_button =
[&](TabSharingInfoBarDelegate::TabSharingInfoBarButton type,
void (TabSharingInfoBar::*click_function)(),
int button_context = views::style::CONTEXT_BUTTON_MD) {
const bool use_text_color_for_icon =
type != TabSharingInfoBarDelegate::kCapturedSurfaceControlIndicator;
auto* button = AddChildView(std::make_unique<views::MdTextButton>(
base::BindRepeating(click_function, base::Unretained(this)),
delegate_ptr->GetButtonLabel(type), button_context,
use_text_color_for_icon));
button->SetProperty(
views::kMarginsKey,
gfx::Insets::VH(ChromeLayoutProvider::Get()->GetDistanceMetric(
DISTANCE_TOAST_CONTROL_VERTICAL),
0));
const bool is_default_button =
type == buttons || type == TabSharingInfoBarDelegate::kStop;
button->SetStyle(is_default_button ? ui::ButtonStyle::kProminent
: ui::ButtonStyle::kTonal);
button->SetImageModel(views::Button::STATE_NORMAL,
delegate_ptr->GetButtonImage(type));
button->SetEnabled(delegate_ptr->IsButtonEnabled(type));
button->SetTooltipText(delegate_ptr->GetButtonTooltip(type));
return button;
};
if (buttons & TabSharingInfoBarDelegate::kStop) {
stop_button_ = create_button(TabSharingInfoBarDelegate::kStop,
&TabSharingInfoBar::StopButtonPressed);
}
if (buttons & TabSharingInfoBarDelegate::kShareThisTabInstead) {
share_this_tab_instead_button_ =
create_button(TabSharingInfoBarDelegate::kShareThisTabInstead,
&TabSharingInfoBar::ShareThisTabInsteadButtonPressed);
}
if (buttons & TabSharingInfoBarDelegate::kQuickNav &&
!base::FeatureList::IsEnabled(features::kTabCaptureInfobarLinks)) {
quick_nav_button_ =
create_button(TabSharingInfoBarDelegate::kQuickNav,
&TabSharingInfoBar::QuickNavButtonPressed);
}
if (buttons & TabSharingInfoBarDelegate::kCapturedSurfaceControlIndicator) {
csc_indicator_button_ = create_button(
TabSharingInfoBarDelegate::kCapturedSurfaceControlIndicator,
&TabSharingInfoBar::OnCapturedSurfaceControlActivityIndicatorPressed,
CONTEXT_OMNIBOX_PRIMARY);
csc_indicator_button_->SetStyle(ui::ButtonStyle::kDefault);
csc_indicator_button_->SetCornerRadius(
GetLayoutConstant(TOOLBAR_CORNER_RADIUS));
csc_indicator_button_->SetCustomPadding(
kCapturedSurfaceControlIndicatorButtonInsets);
csc_indicator_button_->SetTextColor(
views::Button::ButtonState::STATE_NORMAL, ui::kColorSysOnSurface);
}
// TODO(crbug.com/378107817): It seems like link_ isn't always needed, but
// it's added regardless. See about only adding when necessary.
link_ = AddChildView(CreateLink(delegate_ptr->GetLinkText()));
}
TabSharingInfoBar::~TabSharingInfoBar() = default;
void TabSharingInfoBar::Layout(PassKey) {
LayoutSuperclass<InfoBarView>(this);
if (stop_button_) {
stop_button_->SizeToPreferredSize();
}
if (share_this_tab_instead_button_) {
share_this_tab_instead_button_->SizeToPreferredSize();
}
if (quick_nav_button_) {
quick_nav_button_->SizeToPreferredSize();
}
if (csc_indicator_button_) {
csc_indicator_button_->SizeToPreferredSize();
}
int x = GetStartX();
Views views;
views.push_back(status_message_view_.get());
views.push_back(link_.get());
AssignWidths(&views, std::max(0, GetEndX() - x - NonLabelWidth()));
ChromeLayoutProvider* layout_provider = ChromeLayoutProvider::Get();
status_message_view_->SetPosition(
gfx::Point(x, OffsetY(status_message_view_)));
x = status_message_view_->bounds().right() +
layout_provider->GetDistanceMetric(
DISTANCE_INFOBAR_HORIZONTAL_ICON_LABEL_PADDING);
// Add buttons into a vector to be displayed in an ordered row.
// Depending on the PlatformStyle, reverse the vector so the stop button will
// be on the correct leading style.
std::vector<views::MdTextButton*> order_of_buttons;
if (stop_button_) {
order_of_buttons.push_back(stop_button_);
}
if (share_this_tab_instead_button_) {
order_of_buttons.push_back(share_this_tab_instead_button_);
}
if (quick_nav_button_) {
order_of_buttons.push_back(quick_nav_button_);
}
if (csc_indicator_button_) {
order_of_buttons.push_back(csc_indicator_button_);
}
if constexpr (!views::PlatformStyle::kIsOkButtonLeading) {
std::ranges::reverse(order_of_buttons);
}
for (views::MdTextButton* button : order_of_buttons) {
button->SetPosition(gfx::Point(x, OffsetY(button)));
x = button->bounds().right() +
layout_provider->GetDistanceMetric(
views::DISTANCE_RELATED_BUTTON_HORIZONTAL);
}
link_->SetPosition(gfx::Point(GetEndX() - link_->width(), OffsetY(link_)));
}
void TabSharingInfoBar::StopButtonPressed() {
if (!owner()) {
return; // We're closing; don't call anything, it might access the owner.
}
RecordUma(TabSharingInfoBarInteraction::kStopButtonClicked);
GetDelegate()->Stop();
}
void TabSharingInfoBar::ShareThisTabInsteadButtonPressed() {
if (!owner()) {
return; // We're closing; don't call anything, it might access the owner.
}
RecordUma(TabSharingInfoBarInteraction::kShareThisTabInsteadButtonClicked);
GetDelegate()->ShareThisTabInstead();
}
void TabSharingInfoBar::QuickNavButtonPressed() {
if (!owner()) {
return; // We're closing; don't call anything, it might access the owner.
}
GetDelegate()->QuickNav();
}
void TabSharingInfoBar::OnCapturedSurfaceControlActivityIndicatorPressed() {
if (!owner()) {
return; // We're closing; don't call anything, it might access the owner.
}
GetDelegate()->OnCapturedSurfaceControlActivityIndicatorPressed();
}
std::unique_ptr<views::View> TabSharingInfoBar::CreateStatusMessageView(
content::GlobalRenderFrameHostId shared_tab_id,
content::GlobalRenderFrameHostId capturer_id,
const std::u16string& shared_tab_name,
const std::u16string& capturer_name,
TabSharingInfoBarDelegate::TabRole role,
TabSharingInfoBarDelegate::TabShareType capture_type) const {
TabSharingStatusMessageView::EndpointInfo shared_tab_info(
shared_tab_name,
TabSharingStatusMessageView::EndpointInfo::TargetType::kCapturedTab,
shared_tab_id);
TabSharingStatusMessageView::EndpointInfo capturer_info(
capturer_name,
TabSharingStatusMessageView::EndpointInfo::TargetType::kCapturingTab,
capturer_id);
if (base::FeatureList::IsEnabled(features::kTabCaptureInfobarLinks) &&
GetOriginFromId(capturer_id).scheme() != extensions::kExtensionScheme) {
return TabSharingStatusMessageView::Create(capturer_id, shared_tab_info,
capturer_info, capturer_name,
role, capture_type);
} else {
return CreateStatusMessageLabel(shared_tab_info, capturer_info,
capturer_name, role, capture_type);
}
}
std::unique_ptr<views::Label> TabSharingInfoBar::CreateStatusMessageLabel(
const TabSharingStatusMessageView::EndpointInfo& shared_tab_info,
const TabSharingStatusMessageView::EndpointInfo& capturer_info,
const std::u16string& capturer_name,
TabSharingInfoBarDelegate::TabRole role,
TabSharingInfoBarDelegate::TabShareType capture_type) const {
std::unique_ptr<views::Label> label =
CreateLabel(TabSharingStatusMessageView::GetMessageText(
shared_tab_info, capturer_info, capturer_name, role, capture_type));
label->SetElideBehavior(gfx::ELIDE_TAIL);
return label;
}
TabSharingInfoBarDelegate* TabSharingInfoBar::GetDelegate() {
return static_cast<TabSharingInfoBarDelegate*>(delegate());
}
int TabSharingInfoBar::GetContentMinimumWidth() const {
return status_message_view_->GetMinimumSize().width() +
link_->GetMinimumSize().width() + NonLabelWidth();
}
int TabSharingInfoBar::NonLabelWidth() const {
ChromeLayoutProvider* layout_provider = ChromeLayoutProvider::Get();
const int label_spacing = layout_provider->GetDistanceMetric(
views::DISTANCE_RELATED_LABEL_HORIZONTAL);
const int button_spacing = layout_provider->GetDistanceMetric(
views::DISTANCE_RELATED_BUTTON_HORIZONTAL);
const int button_count =
(stop_button_ ? 1 : 0) + (share_this_tab_instead_button_ ? 1 : 0) +
(quick_nav_button_ ? 1 : 0) + (csc_indicator_button_ ? 1 : 0);
int width = (button_count == 0) ? 0 : label_spacing;
width += std::max(0, button_spacing * (button_count - 1));
width += stop_button_ ? stop_button_->width() : 0;
width += share_this_tab_instead_button_
? share_this_tab_instead_button_->width()
: 0;
width += quick_nav_button_ ? quick_nav_button_->width() : 0;
width += csc_indicator_button_ ? csc_indicator_button_->width() : 0;
return width + ((width && !link_->GetText().empty()) ? label_spacing : 0);
}
std::unique_ptr<infobars::InfoBar> CreateTabSharingInfoBar(
std::unique_ptr<TabSharingInfoBarDelegate> delegate,
content::GlobalRenderFrameHostId shared_tab_id,
content::GlobalRenderFrameHostId capturer_id,
const std::u16string& shared_tab_name,
const std::u16string& capturer_name,
TabSharingInfoBarDelegate::TabRole role,
TabSharingInfoBarDelegate::TabShareType capture_type) {
return std::make_unique<TabSharingInfoBar>(std::move(delegate), shared_tab_id,
capturer_id, shared_tab_name,
capturer_name, role, capture_type);
}
|