File: media_remoting_dialog_view.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 (135 lines) | stat: -rw-r--r-- 5,132 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
// Copyright 2018 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/media_router/media_remoting_dialog_view.h"

#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/media_router/media_router_ui_service.h"
#include "chrome/browser/ui/toolbar/cast/cast_toolbar_button_controller.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/media_router/cast_toolbar_button.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/grit/generated_resources.h"
#include "components/media_router/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/views/controls/button/checkbox.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/style/typography.h"

namespace media_router {

MediaRemotingDialogCoordinatorViews::MediaRemotingDialogCoordinatorViews(
    content::WebContents* web_contents)
    : web_contents_(web_contents) {
  DCHECK(web_contents);
}

MediaRemotingDialogCoordinatorViews::~MediaRemotingDialogCoordinatorViews() =
    default;

bool MediaRemotingDialogCoordinatorViews::Show(
    PermissionCallback permission_callback) {
  DCHECK(permission_callback);

  Hide();  // Close the previous dialog if it is still showing.

  Browser* const browser = chrome::FindBrowserWithTab(web_contents_);
  if (!browser) {
    std::move(permission_callback).Run(false);
    return false;
  }
  views::View* const icon_view = BrowserView::GetBrowserViewForBrowser(browser)
                                     ->toolbar()
                                     ->GetCastButton();
  Profile* const profile =
      Profile::FromBrowserContext(web_contents_->GetBrowserContext());
  PrefService* const pref_service = profile->GetPrefs();
  CastToolbarButtonController* const action_controller =
      MediaRouterUIService::Get(profile)->action_controller();

  auto remoting_dialog = std::make_unique<MediaRemotingDialogView>(
      icon_view, pref_service, action_controller,
      std::move(permission_callback));
  tracker_.SetView(remoting_dialog.get());
  views::BubbleDialogDelegateView::CreateBubble(std::move(remoting_dialog))
      ->Show();
  return true;
}

void MediaRemotingDialogCoordinatorViews::Hide() {
  if (IsShowing()) {
    tracker_.view()->GetWidget()->Close();
  }
}

bool MediaRemotingDialogCoordinatorViews::IsShowing() const {
  return !!tracker_.view();
}

MediaRemotingDialogView::MediaRemotingDialogView(
    views::View* anchor_view,
    PrefService* pref_service,
    CastToolbarButtonController* action_controller,
    MediaRemotingDialogCoordinator::PermissionCallback callback)
    : BubbleDialogDelegateView(anchor_view, views::BubbleBorder::TOP_RIGHT),
      pref_service_(pref_service),
      action_controller_(action_controller),
      permission_callback_(std::move(callback)) {
  DCHECK(pref_service_);
  SetShowCloseButton(true);
  SetTitle(IDS_MEDIA_ROUTER_REMOTING_DIALOG_TITLE);
  SetButtonLabel(ui::mojom::DialogButton::kOk,
                 l10n_util::GetStringUTF16(
                     IDS_MEDIA_ROUTER_REMOTING_DIALOG_OPTIMIZE_BUTTON));
  SetButtonLabel(ui::mojom::DialogButton::kCancel,
                 l10n_util::GetStringUTF16(
                     IDS_MEDIA_ROUTER_REMOTING_DIALOG_CANCEL_BUTTON));

  SetAcceptCallback(base::BindOnce(&MediaRemotingDialogView::ReportPermission,
                                   base::Unretained(this), true));
  SetCancelCallback(base::BindOnce(&MediaRemotingDialogView::ReportPermission,
                                   base::Unretained(this), false));

  set_fixed_width(views::LayoutProvider::Get()->GetDistanceMetric(
      views::DISTANCE_BUBBLE_PREFERRED_WIDTH));

  SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kVertical));
  // Depress the Cast toolbar icon.
  action_controller_->OnDialogShown();
}

MediaRemotingDialogView::~MediaRemotingDialogView() {
  action_controller_->OnDialogHidden();
}

void MediaRemotingDialogView::Init() {
  views::Label* body_text = new views::Label(
      l10n_util::GetStringUTF16(IDS_MEDIA_ROUTER_REMOTING_DIALOG_BODY_TEXT),
      views::style::CONTEXT_DIALOG_BODY_TEXT, views::style::STYLE_PRIMARY);
  body_text->SetMultiLine(true);
  body_text->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  AddChildViewRaw(body_text);

  remember_choice_checkbox_ = new views::Checkbox(
      l10n_util::GetStringUTF16(IDS_MEDIA_ROUTER_REMOTING_DIALOG_CHECKBOX));
  AddChildViewRaw(remember_choice_checkbox_.get());
}

void MediaRemotingDialogView::ReportPermission(bool allowed) {
  DCHECK(remember_choice_checkbox_);
  DCHECK(permission_callback_);
  if (remember_choice_checkbox_->GetChecked()) {
    pref_service_->SetBoolean(prefs::kMediaRouterMediaRemotingEnabled, allowed);
  }
  std::move(permission_callback_).Run(allowed);
}

BEGIN_METADATA(MediaRemotingDialogView)
END_METADATA

}  // namespace media_router