File: cast_toolbar_button_controller.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 (250 lines) | stat: -rw-r--r-- 9,409 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
// Copyright 2016 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/toolbar/cast/cast_toolbar_button_controller.h"

#include <algorithm>

#include "base/functional/bind.h"
#include "base/observer_list.h"
#include "chrome/browser/media/router/media_router_feature.h"
#include "chrome/browser/ui/actions/chrome_action_id.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_actions.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/media_router/cast_browser_controller.h"
#include "chrome/browser/ui/views/toolbar/pinned_toolbar_actions_container.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/common/pref_names.h"
#include "components/media_router/browser/media_router.h"
#include "components/media_router/browser/media_router_factory.h"
#include "components/media_router/browser/media_router_metrics.h"
#include "components/media_router/common/pref_names.h"
#include "components/media_router/common/providers/cast/cast_media_source.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "ui/actions/actions.h"

CastToolbarButtonController::CastToolbarButtonController(Profile* profile)
    : CastToolbarButtonController(
          profile,
          media_router::MediaRouterFactory::GetApiForBrowserContext(profile)) {}

CastToolbarButtonController::~CastToolbarButtonController() = default;

// static
bool CastToolbarButtonController::IsActionShownByPolicy(Profile* profile) {
  CHECK(profile);
  const PrefService::Preference* pref =
      profile->GetPrefs()->FindPreference(prefs::kShowCastIconInToolbar);
  bool show = false;
  if (pref->IsManaged() && pref->GetValue()->is_bool()) {
    show = pref->GetValue()->GetBool();
  }
  return show;
}

// static
bool CastToolbarButtonController::GetAlwaysShowActionPref(Profile* profile) {
  CHECK(profile);
  return profile->GetPrefs()->GetBoolean(prefs::kShowCastIconInToolbar);
}

// static
void CastToolbarButtonController::SetAlwaysShowActionPref(Profile* profile,
                                                          bool always_show) {
  CHECK(profile);
  profile->GetPrefs()->SetBoolean(prefs::kShowCastIconInToolbar, always_show);
}

void CastToolbarButtonController::OnIssue(const media_router::Issue& issue) {
  // Don't show the toolbar button if it receives a permission rejected issue.
  has_issue_ = !issue.is_permission_rejected_issue();
  MaybeToggleIconVisibility();
}

void CastToolbarButtonController::OnIssuesCleared() {
  has_issue_ = false;
  MaybeToggleIconVisibility();
}

void CastToolbarButtonController::OnRoutesUpdated(
    const std::vector<media_router::MediaRoute>& routes) {
  has_local_display_route_ =
      std::ranges::any_of(routes, [](const media_router::MediaRoute& route) {
        // The Cast icon should be hidden if there only are
        // non-local and non-display routes.
        if (!route.is_local()) {
          return false;
        }
        // When the feature is enabled, presentation routes are
        // controlled through the global media controls most of the
        // time. So we do not request to show the Cast icon when
        // there only are presentation routes.
        // In other words, the Cast icon is shown when there are
        // mirroring or local file sources.
        return route.media_source().IsTabMirroringSource() ||
               route.media_source().IsDesktopMirroringSource();
      });
  MaybeToggleIconVisibility();
}

void CastToolbarButtonController::OnDialogShown() {
  dialog_count_++;
  MaybeToggleIconVisibility();
  for (Observer& observer : observers_) {
    observer.ActivateIcon();
  }
}

void CastToolbarButtonController::OnDialogHidden() {
  DCHECK_GT(dialog_count_, 0u);
  if (dialog_count_) {
    dialog_count_--;
  }
  if (dialog_count_ == 0) {
    for (Observer& observer : observers_) {
      observer.DeactivateIcon();
    }
    // Call MaybeToggleIconVisibility() asynchronously, so that the action icon
    // doesn't get hidden until we have a chance to show a context menu.
    content::GetUIThreadTaskRunner({})->PostTask(
        FROM_HERE,
        base::BindOnce(&CastToolbarButtonController::MaybeToggleIconVisibility,
                       weak_factory_.GetWeakPtr()));
  }
}

void CastToolbarButtonController::OnContextMenuShown() {
  DCHECK(!context_menu_shown_);
  context_menu_shown_ = true;
  // Once the context menu is shown, we no longer need to keep track of the
  // mouse or touch press.
  keep_visible_for_right_click_or_hold_ = false;
  MaybeToggleIconVisibility();
}

void CastToolbarButtonController::OnContextMenuHidden() {
  DCHECK(context_menu_shown_);
  context_menu_shown_ = false;
  MaybeToggleIconVisibility();
}

void CastToolbarButtonController::UpdateIcon() {
  for (Browser* browser : chrome::FindAllBrowsersWithProfile(profile_)) {
    browser->browser_window_features()->cast_browser_controller()->UpdateIcon();
  }
}

void CastToolbarButtonController::KeepIconShownOnPressed() {
  DCHECK(!keep_visible_for_right_click_or_hold_);
  keep_visible_for_right_click_or_hold_ = true;
  MaybeToggleIconVisibility();
}

void CastToolbarButtonController::MaybeHideIconOnReleased() {
  keep_visible_for_right_click_or_hold_ = false;
  MaybeToggleIconVisibility();
}

void CastToolbarButtonController::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
}

void CastToolbarButtonController::RemoveObserver(Observer* observer) {
  observers_.RemoveObserver(observer);
}

bool CastToolbarButtonController::ShouldEnableAction() const {
  return shown_by_policy_ || has_local_display_route_ || has_issue_ ||
         dialog_count_ || context_menu_shown_ ||
         keep_visible_for_right_click_or_hold_ ||
         GetAlwaysShowActionPref(profile_);
}

CastToolbarButtonController::CastToolbarButtonController(
    Profile* profile,
    media_router::MediaRouter* router)
    : media_router::IssuesObserver(router->GetIssueManager()),
      media_router::MediaRoutesObserver(router),
      profile_(profile),
      shown_by_policy_(
          CastToolbarButtonController::IsActionShownByPolicy(profile)) {
  CHECK(profile_);
  media_router::IssuesObserver::Init();
  pref_change_registrar_.Init(profile->GetPrefs());
  pref_change_registrar_.Add(
      prefs::kShowCastIconInToolbar,
      base::BindRepeating(
          &CastToolbarButtonController::MaybeToggleIconVisibility,
          base::Unretained(this)));
  if (base::FeatureList::IsEnabled(features::kPinnedCastButton)) {
    pref_change_registrar_.Add(
        media_router::prefs::kMediaRouterMediaRemotingEnabled,
        base::BindRepeating(
            &CastToolbarButtonController::UpdateToggleMediaRouterRemotingAction,
            base::Unretained(this)));
  }
}

void CastToolbarButtonController::MaybeToggleIconVisibility() {
  if (base::FeatureList::IsEnabled(features::kPinnedCastButton)) {
    // Pin media router if it should be pinned based on enterprise policy.
    if (IsActionShownByPolicy(profile_)) {
      PinnedToolbarActionsModel* const actions_model =
          PinnedToolbarActionsModel::Get(profile_);
      actions_model->UpdatePinnedState(kActionRouteMedia, true);
    }

    for (Browser* browser : chrome::FindAllBrowsersWithProfile(profile_)) {
      auto* action_item = actions::ActionManager::Get().FindAction(
          kActionRouteMedia, browser->browser_actions()->root_action_item());
      // Update the action item's pinnable state based on the enterprise policy.
      if (IsActionShownByPolicy(profile_)) {
        action_item->SetProperty(
            actions::kActionItemPinnableKey,
            std::underlying_type_t<actions::ActionPinnableState>(
                actions::ActionPinnableState::kEnterpriseControlled));
      } else {
        action_item->SetProperty(
            actions::kActionItemPinnableKey,
            std::underlying_type_t<actions::ActionPinnableState>(
                actions::ActionPinnableState::kPinnable));
      }
      // Update the toolbar button's visibility.
      if (auto* container = BrowserView::GetBrowserViewForBrowser(browser)
                                ->toolbar()
                                ->pinned_toolbar_actions_container()) {
        container->ShowActionEphemerallyInToolbar(kActionRouteMedia,
                                                  ShouldEnableAction());
      }
    }
    return;
  }

  if (ShouldEnableAction()) {
    for (Observer& observer : observers_) {
      observer.ShowIcon();
    }
  } else {
    for (Observer& observer : observers_) {
      observer.HideIcon();
    }
  }
}

void CastToolbarButtonController::UpdateToggleMediaRouterRemotingAction() {
  bool checked = profile_->GetPrefs()->GetBoolean(
      media_router::prefs::kMediaRouterMediaRemotingEnabled);
  for (Browser* browser : chrome::FindAllBrowsersWithProfile(profile_)) {
    actions::ActionManager::Get()
        .FindAction(kActionMediaRouterToggleMediaRemoting,
                    browser->browser_actions()->root_action_item())
        ->SetChecked(checked);
  }
}