1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/toolbar/media_router_action.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/media/router/issue.h"
9 #include "chrome/browser/media/router/media_route.h"
10 #include "chrome/browser/media/router/media_router.h"
11 #include "chrome/browser/media/router/media_router_factory.h"
12 #include "chrome/browser/media/router/media_router_mojo_impl.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h"
17 #include "chrome/browser/ui/toolbar/media_router_action_platform_delegate.h"
18 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h"
19 #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller_impl.h"
20 #include "chrome/grit/generated_resources.h"
21 #include "grit/theme_resources.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/gfx/image/image_skia.h"
26 using media_router::MediaRouterDialogControllerImpl
;
28 MediaRouterAction::MediaRouterAction(Browser
* browser
)
29 : media_router::IssuesObserver(GetMediaRouter(browser
)),
30 media_router::MediaRoutesObserver(GetMediaRouter(browser
)),
31 media_router_active_icon_(
32 ui::ResourceBundle::GetSharedInstance()
33 .GetImageNamed(IDR_MEDIA_ROUTER_ACTIVE_ICON
)),
34 media_router_error_icon_(ui::ResourceBundle::GetSharedInstance()
35 .GetImageNamed(IDR_MEDIA_ROUTER_ERROR_ICON
)),
36 media_router_idle_icon_(ui::ResourceBundle::GetSharedInstance()
37 .GetImageNamed(IDR_MEDIA_ROUTER_IDLE_ICON
)),
38 media_router_warning_icon_(
39 ui::ResourceBundle::GetSharedInstance()
40 .GetImageNamed(IDR_MEDIA_ROUTER_WARNING_ICON
)),
41 current_icon_(&media_router_idle_icon_
),
42 has_local_route_(false),
45 platform_delegate_(MediaRouterActionPlatformDelegate::Create(browser
)),
46 contextual_menu_(browser
),
47 tab_strip_model_observer_(this),
48 weak_ptr_factory_(this) {
50 tab_strip_model_observer_
.Add(browser_
->tab_strip_model());
53 MediaRouterAction::~MediaRouterAction() {
56 std::string
MediaRouterAction::GetId() const {
57 return ComponentToolbarActionsFactory::kMediaRouterActionId
;
60 void MediaRouterAction::SetDelegate(ToolbarActionViewDelegate
* delegate
) {
63 // Updates the current popup state if |delegate_| is non-null and has
65 // In cases such as opening a new browser window, SetDelegate() will be
66 // called before the WebContents is set. In those cases, we update the popup
67 // state when ActiveTabChanged() is called.
68 if (delegate_
&& delegate_
->GetCurrentWebContents())
72 gfx::Image
MediaRouterAction::GetIcon(content::WebContents
* web_contents
,
73 const gfx::Size
& size
) {
74 return *current_icon_
;
77 base::string16
MediaRouterAction::GetActionName() const {
78 return l10n_util::GetStringUTF16(IDS_MEDIA_ROUTER_TITLE
);
81 base::string16
MediaRouterAction::GetAccessibleName(
82 content::WebContents
* web_contents
) const {
83 return GetTooltip(web_contents
);
86 base::string16
MediaRouterAction::GetTooltip(
87 content::WebContents
* web_contents
) const {
88 return l10n_util::GetStringUTF16(IDS_MEDIA_ROUTER_SHARE_YOUR_SCREEN_TEXT
);
91 bool MediaRouterAction::IsEnabled(
92 content::WebContents
* web_contents
) const {
96 bool MediaRouterAction::WantsToRun(
97 content::WebContents
* web_contents
) const {
101 bool MediaRouterAction::HasPopup(
102 content::WebContents
* web_contents
) const {
106 void MediaRouterAction::HidePopup() {
107 GetMediaRouterDialogController()->HideMediaRouterDialog();
111 gfx::NativeView
MediaRouterAction::GetPopupNativeView() {
115 ui::MenuModel
* MediaRouterAction::GetContextMenu() {
116 return contextual_menu_
.menu_model();
119 bool MediaRouterAction::ExecuteAction(bool by_user
) {
120 GetMediaRouterDialogController()->ShowMediaRouterDialog();
121 if (GetPlatformDelegate())
122 GetPlatformDelegate()->CloseOverflowMenuIfOpen();
126 void MediaRouterAction::UpdateState() {
128 delegate_
->UpdateState();
131 bool MediaRouterAction::DisabledClickOpensMenu() const {
135 void MediaRouterAction::OnIssueUpdated(const media_router::Issue
* issue
) {
136 issue_
.reset(issue
? new media_router::Issue(*issue
) : nullptr);
141 void MediaRouterAction::OnRoutesUpdated(
142 const std::vector
<media_router::MediaRoute
>& routes
) {
144 std::find_if(routes
.begin(), routes
.end(),
145 [](const media_router::MediaRoute
& route
) {
146 return route
.is_local(); }) !=
151 void MediaRouterAction::ActiveTabChanged(content::WebContents
* old_contents
,
152 content::WebContents
* new_contents
,
158 void MediaRouterAction::OnPopupHidden() {
160 delegate_
->OnPopupClosed();
163 void MediaRouterAction::OnPopupShown() {
164 // We depress the action regardless of whether ExecuteAction() was user
167 delegate_
->OnPopupShown(true);
170 void MediaRouterAction::UpdatePopupState() {
171 MediaRouterDialogControllerImpl
* controller
=
172 GetMediaRouterDialogController();
177 // Immediately keep track of MediaRouterAction in the controller. If it was
178 // already set, this should be a no-op.
179 controller
->SetMediaRouterAction(weak_ptr_factory_
.GetWeakPtr());
181 // Update the button in case the pressed state is out of sync with dialog
183 if (controller
->IsShowingMediaRouterDialog())
189 MediaRouterDialogControllerImpl
*
190 MediaRouterAction::GetMediaRouterDialogController() {
192 content::WebContents
* web_contents
= delegate_
->GetCurrentWebContents();
193 DCHECK(web_contents
);
194 return MediaRouterDialogControllerImpl::GetOrCreateForWebContents(
198 media_router::MediaRouter
* MediaRouterAction::GetMediaRouter(Browser
* browser
) {
199 return media_router::MediaRouterFactory::GetApiForBrowserContext(
200 static_cast<content::BrowserContext
*>(browser
->profile()));
203 MediaRouterActionPlatformDelegate
* MediaRouterAction::GetPlatformDelegate() {
204 return platform_delegate_
.get();
207 void MediaRouterAction::MaybeUpdateIcon() {
208 const gfx::Image
* new_icon
= GetCurrentIcon();
210 // Update the current state if it has changed.
211 if (new_icon
!= current_icon_
) {
212 current_icon_
= new_icon
;
214 // Tell the associated view to update its icon to reflect the change made
217 delegate_
->UpdateState();
221 const gfx::Image
* MediaRouterAction::GetCurrentIcon() const {
222 // Highest priority is to indicate whether there's an issue.
224 if (issue_
->severity() == media_router::Issue::FATAL
)
225 return &media_router_error_icon_
;
226 if (issue_
->severity() == media_router::Issue::WARNING
)
227 return &media_router_warning_icon_
;
230 return has_local_route_
?
231 &media_router_active_icon_
: &media_router_idle_icon_
;