File: tab_sharing_infobar_delegate.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 (403 lines) | stat: -rw-r--r-- 14,356 bytes parent folder | download | duplicates (5)
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
// Copyright 2019 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_sharing/tab_sharing_infobar_delegate.h"

#include <string>
#include <utility>

#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/bubble_anchor_util.h"
#include "chrome/browser/ui/page_info/page_info_dialog.h"
#include "chrome/browser/ui/tab_sharing/tab_sharing_ui.h"
#include "chrome/grit/generated_resources.h"
#include "components/infobars/content/content_infobar_manager.h"
#include "components/infobars/core/confirm_infobar_delegate.h"
#include "components/infobars/core/infobar.h"
#include "components/page_load_metrics/browser/metrics_web_contents_observer.h"
#include "components/strings/grit/components_strings.h"
#include "components/url_formatter/elide_url.h"
#include "components/vector_icons/vector_icons.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/common/content_features.h"
#include "media/capture/capture_switches.h"
#include "net/base/url_util.h"
#include "third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/strings/grit/ui_strings.h"

namespace {
using TabRole = ::TabSharingInfoBarDelegate::TabRole;

constexpr int kCapturedSurfaceControlIndicatorButtonIconHeight = 16;
}  // namespace

class TabSharingInfoBarDelegate::TabSharingInfoBarDelegateButton {
 public:
  TabSharingInfoBarDelegateButton() = default;

  // Deleted copy and assignment operator.
  TabSharingInfoBarDelegateButton(const TabSharingInfoBarDelegateButton&) =
      delete;
  TabSharingInfoBarDelegateButton& operator=(
      const TabSharingInfoBarDelegateButton&) = delete;

  virtual ~TabSharingInfoBarDelegateButton() = default;
  virtual void Click(infobars::InfoBar* infobar) = 0;
  virtual std::u16string GetLabel() const = 0;
  virtual ui::ImageModel GetImage() const { return {}; }
  virtual bool IsEnabled() const { return true; }
  virtual std::u16string GetTooltip() const { return u""; }
};

using TabSharingInfoBarDelegateButton =
    ::TabSharingInfoBarDelegate::TabSharingInfoBarDelegateButton;

class TabSharingInfoBarDelegate::StopButton
    : public TabSharingInfoBarDelegateButton {
 public:
  StopButton(TabSharingUI* ui,
             TabSharingInfoBarDelegate::TabShareType capture_type)
      : ui_(ui), capture_type_(capture_type) {}
  ~StopButton() override = default;

  void Click(infobars::InfoBar* infobar) override { ui_->StopSharing(); }

  std::u16string GetLabel() const override {
    switch (capture_type_) {
      case TabSharingInfoBarDelegate::TabShareType::CAST:
        return l10n_util::GetStringUTF16(IDS_TAB_CASTING_INFOBAR_STOP_BUTTON);
      case TabSharingInfoBarDelegate::TabShareType::CAPTURE:
        return l10n_util::GetStringUTF16(IDS_TAB_SHARING_INFOBAR_STOP_BUTTON);
    }
    NOTREACHED();
  }

 private:
  const raw_ptr<TabSharingUI, AcrossTasksDanglingUntriaged> ui_;
  const TabShareType capture_type_;
};

// Represents a button which, when clicked, changes the tab being shared/cast to
// be the current tab (the one associated with this infobar.)
class TabSharingInfoBarDelegate::ShareTabInsteadButton
    : public TabSharingInfoBarDelegateButton {
 public:
  ShareTabInsteadButton(TabSharingUI* ui,
                        TabSharingInfoBarDelegate::ButtonState button_state,
                        TabSharingInfoBarDelegate::TabShareType capture_type)
      : ui_(ui), button_state_(button_state), capture_type_(capture_type) {}

  ~ShareTabInsteadButton() override = default;

  void Click(infobars::InfoBar* infobar) override {
    DCHECK(ui_);  // Not verified in ctor to keep tests simple.
    ui_->StartSharing(infobar);
  }

  std::u16string GetLabel() const override {
    switch (capture_type_) {
      case TabSharingInfoBarDelegate::TabShareType::CAST:
        return l10n_util::GetStringUTF16(IDS_TAB_CASTING_INFOBAR_CAST_BUTTON);
      case TabSharingInfoBarDelegate::TabShareType::CAPTURE:
        return l10n_util::GetStringUTF16(IDS_TAB_SHARING_INFOBAR_SHARE_BUTTON);
    }
    NOTREACHED();
  }

  bool IsEnabled() const override {
    return button_state_ == TabSharingInfoBarDelegate::ButtonState::ENABLED;
  }

  std::u16string GetTooltip() const override {
    return button_state_ == TabSharingInfoBarDelegate::ButtonState::DISABLED
               ? l10n_util::GetStringUTF16(
                     IDS_POLICY_DLP_SCREEN_SHARE_BLOCKED_TITLE)
               : u"";
  }

 private:
  const raw_ptr<TabSharingUI, AcrossTasksDanglingUntriaged> ui_;
  const TabSharingInfoBarDelegate::ButtonState button_state_;
  const TabSharingInfoBarDelegate::TabShareType capture_type_;
};

// Represents a button which, when clicked, changes the activated tab to be
// the one which was hard-coded into this infobar. The intended use for this
// class is for the captured tab to activate the capturing tab, and vice versa.
class TabSharingInfoBarDelegate::SwitchToTabButton
    : public TabSharingInfoBarDelegateButton {
 public:
  SwitchToTabButton(content::GlobalRenderFrameHostId focus_target,
                    bool focus_target_is_capturer)
      : focus_target_(focus_target),
        focus_target_is_capturer_(focus_target_is_capturer) {}
  ~SwitchToTabButton() override = default;

  void Click(infobars::InfoBar* infobar) override {
    content::RenderFrameHost* const rfh =
        content::RenderFrameHost::FromID(focus_target_);
    if (!rfh) {
      return;
    }

    page_load_metrics::MetricsWebContentsObserver::RecordFeatureUsage(
        rfh, focus_target_is_capturer_
                 ? blink::mojom::WebFeature::kTabSharingBarSwitchToCapturer
                 : blink::mojom::WebFeature::kTabSharingBarSwitchToCapturee);

    content::WebContents* const web_contents =
        content::WebContents::FromRenderFrameHost(rfh);
    DCHECK(web_contents);

    web_contents->GetDelegate()->ActivateContents(web_contents);
    Browser* const browser = chrome::FindBrowserWithTab(web_contents);
    if (browser && browser->window()) {
      browser->window()->Activate();
    }
  }

  std::u16string GetLabel() const override {
    // TODO(crbug.com/40188004): Hard-code this text into the button.
    content::RenderFrameHost* const rfh =
        content::RenderFrameHost::FromID(focus_target_);
    if (!rfh) {
      return GetDefaultLabel();
    }
    return l10n_util::GetStringFUTF16(
        IDS_TAB_SHARING_INFOBAR_SWITCH_TO_BUTTON,
        url_formatter::FormatOriginForSecurityDisplay(
            rfh->GetLastCommittedOrigin(),
            url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS));
  }

 private:
  std::u16string GetDefaultLabel() const {
    return l10n_util::GetStringUTF16(
        focus_target_is_capturer_
            ? IDS_TAB_SHARING_INFOBAR_SWITCH_TO_CAPTURER_BUTTON
            : IDS_TAB_SHARING_INFOBAR_SWITCH_TO_CAPTURED_BUTTON);
  }

  const content::GlobalRenderFrameHostId focus_target_;
  const bool focus_target_is_capturer_;
};

class TabSharingInfoBarDelegate::CscIndicatorButton
    : public TabSharingInfoBarDelegateButton {
 public:
  explicit CscIndicatorButton(content::WebContents* web_contents)
      : web_contents_(web_contents ? web_contents->GetWeakPtr() : nullptr) {}
  ~CscIndicatorButton() override = default;

  void Click(infobars::InfoBar* infobar) override {
    if (!web_contents_) {
      return;
    }
    ShowPageInfoDialog(web_contents_.get(), base::DoNothing(),
                       bubble_anchor_util::Anchor::kLocationBar,
                       ContentSettingsType::CAPTURED_SURFACE_CONTROL);
  }

  std::u16string GetLabel() const override {
    return l10n_util::GetStringUTF16(
        IDS_TAB_SHARING_INFOBAR_CAPTURED_SURFACE_CONTROL_PERMISSION_BUTTON);
  }

  bool IsEnabled() const override { return true; }

  ui::ImageModel GetImage() const override {
    return ui::ImageModel::FromVectorIcon(
        vector_icons::kTouchpadMouseIcon, ui::kColorSysPrimary,
        kCapturedSurfaceControlIndicatorButtonIconHeight);
  }

 private:
  const base::WeakPtr<content::WebContents> web_contents_;
};

bool TabSharingInfoBarDelegate::IsCapturedTab(TabRole role) {
  switch (role) {
    case TabRole::kCapturingTab:
    case TabRole::kOtherTab:
      return false;
    case TabRole::kCapturedTab:
    case TabRole::kSelfCapturingTab:
      return true;
  }
  NOTREACHED();
}

bool TabSharingInfoBarDelegate::IsCapturingTab(TabRole role) {
  switch (role) {
    case TabRole::kCapturingTab:
    case TabRole::kSelfCapturingTab:
      return true;
    case TabRole::kCapturedTab:
    case TabRole::kOtherTab:
      return false;
  }
  NOTREACHED();
}

// static
infobars::InfoBar* TabSharingInfoBarDelegate::Create(
    infobars::InfoBarManager* infobar_manager,
    infobars::InfoBar* old_infobar,
    content::GlobalRenderFrameHostId shared_tab_id,
    content::GlobalRenderFrameHostId capturer_id,
    const std::u16string& shared_tab_name,
    const std::u16string& capturer_name,
    content::WebContents* web_contents,
    TabRole role,
    ButtonState share_this_tab_instead_button_state,
    content::GlobalRenderFrameHostId focus_target,
    bool captured_surface_control_active,
    TabSharingUI* ui,
    TabShareType capture_type) {
  DCHECK(infobar_manager);
  std::unique_ptr<infobars::InfoBar> new_infobar = CreateTabSharingInfoBar(
      base::WrapUnique(new TabSharingInfoBarDelegate(
          web_contents, role, share_this_tab_instead_button_state, focus_target,
          captured_surface_control_active, ui, capture_type)),
      shared_tab_id, capturer_id, shared_tab_name, capturer_name, role,
      capture_type);
  return old_infobar ? infobar_manager->ReplaceInfoBar(old_infobar,
                                                       std::move(new_infobar))
                     : infobar_manager->AddInfoBar(std::move(new_infobar));
}

TabSharingInfoBarDelegate::TabSharingInfoBarDelegate(
    content::WebContents* web_contents,
    TabRole role,
    ButtonState share_this_tab_instead_button_state,
    content::GlobalRenderFrameHostId focus_target,
    bool captured_surface_control_active,
    TabSharingUI* ui,
    TabShareType capture_type)
    : ui_(ui), capture_type_(capture_type) {
  stop_button_ = std::make_unique<StopButton>(ui_, capture_type_);

  if (share_this_tab_instead_button_state != ButtonState::NOT_SHOWN) {
    share_this_tab_instead_button_ = std::make_unique<ShareTabInsteadButton>(
        ui_, share_this_tab_instead_button_state, capture_type);
  }

  if (focus_target) {
    quick_nav_button_ =
        std::make_unique<SwitchToTabButton>(focus_target, IsCapturedTab(role));
  }

  // Note that kSelfCapturingTab is intentionally disregarded,
  // because write-access CapturedSurfaceControl APIs are disallowed
  // in that case anyway.
  //
  // TODO(crbug.com/324468211): Hide the button if Captured Surface Control
  // is set to BLOCKED or ASK through the user's interaction with PageInfo.
  if (role == TabRole::kCapturingTab && captured_surface_control_active) {
    csc_indicator_button_ = std::make_unique<CscIndicatorButton>(web_contents);
  }
}

TabSharingInfoBarDelegate::~TabSharingInfoBarDelegate() = default;

bool TabSharingInfoBarDelegate::EqualsDelegate(
    InfoBarDelegate* delegate) const {
  return false;
}

bool TabSharingInfoBarDelegate::ShouldExpire(
    const NavigationDetails& details) const {
  return false;
}

infobars::InfoBarDelegate::InfoBarIdentifier
TabSharingInfoBarDelegate::GetIdentifier() const {
  return TAB_SHARING_INFOBAR_DELEGATE;
}

std::u16string TabSharingInfoBarDelegate::GetButtonLabel(
    TabSharingInfoBarButton button) const {
  return GetButton(button).GetLabel();
}

ui::ImageModel TabSharingInfoBarDelegate::GetButtonImage(
    TabSharingInfoBarButton button) const {
  return GetButton(button).GetImage();
}

bool TabSharingInfoBarDelegate::IsButtonEnabled(
    TabSharingInfoBarButton button) const {
  return GetButton(button).IsEnabled();
}

std::u16string TabSharingInfoBarDelegate::GetButtonTooltip(
    TabSharingInfoBarButton button) const {
  return GetButton(button).GetTooltip();
}

int TabSharingInfoBarDelegate::GetButtons() const {
  return (stop_button_ ? kStop : 0) |
         (share_this_tab_instead_button_ ? kShareThisTabInstead : 0) |
         (quick_nav_button_ ? kQuickNav : 0) |
         (csc_indicator_button_ ? kCapturedSurfaceControlIndicator : 0);
}

void TabSharingInfoBarDelegate::Stop() {
  GetButton(kStop).Click(infobar());
}

void TabSharingInfoBarDelegate::ShareThisTabInstead() {
  GetButton(kShareThisTabInstead).Click(infobar());
}

void TabSharingInfoBarDelegate::QuickNav() {
  GetButton(kQuickNav).Click(infobar());
}

void TabSharingInfoBarDelegate::
    OnCapturedSurfaceControlActivityIndicatorPressed() {
  GetButton(kCapturedSurfaceControlIndicator).Click(infobar());
}

bool TabSharingInfoBarDelegate::IsCloseable() const {
  return false;
}

const gfx::VectorIcon& TabSharingInfoBarDelegate::GetVectorIcon() const {
  return base::FeatureList::IsEnabled(features::kTabCaptureInfobarLinks)
             ? vector_icons::kScreenShareIcon
             : vector_icons::kScreenShareOldIcon;
}

const TabSharingInfoBarDelegateButton& TabSharingInfoBarDelegate::GetButton(
    TabSharingInfoBarButton button) const {
  switch (button) {
    case TabSharingInfoBarButton::kNone:
      break;
    case TabSharingInfoBarButton::kStop:
      return *stop_button_;
    case TabSharingInfoBarButton::kShareThisTabInstead:
      return *share_this_tab_instead_button_;
    case TabSharingInfoBarButton::kQuickNav:
      return *quick_nav_button_;
    case TabSharingInfoBarButton::kCapturedSurfaceControlIndicator:
      return *csc_indicator_button_;
  }
  NOTREACHED();
}

TabSharingInfoBarDelegateButton& TabSharingInfoBarDelegate::GetButton(
    TabSharingInfoBarButton button) {
  return const_cast<TabSharingInfoBarDelegateButton&>(
      const_cast<const TabSharingInfoBarDelegate*>(this)->GetButton(button));
}