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_dialog_controller.h"
12 #include "chrome/browser/media/router/media_router_factory.h"
13 #include "chrome/browser/media/router/media_router_mojo_impl.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.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/grit/generated_resources.h"
20 #include "grit/theme_resources.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/base/resource/resource_bundle.h"
23 #include "ui/gfx/image/image_skia.h"
25 using media_router::MediaRouterDialogController
;
27 MediaRouterAction::MediaRouterAction(Browser
* browser
)
28 : media_router::IssuesObserver(GetMediaRouter(browser
)),
29 media_router::MediaRoutesObserver(GetMediaRouter(browser
)),
30 media_router_active_icon_(
31 ui::ResourceBundle::GetSharedInstance()
32 .GetImageNamed(IDR_MEDIA_ROUTER_ACTIVE_ICON
)),
33 media_router_error_icon_(ui::ResourceBundle::GetSharedInstance()
34 .GetImageNamed(IDR_MEDIA_ROUTER_ERROR_ICON
)),
35 media_router_idle_icon_(ui::ResourceBundle::GetSharedInstance()
36 .GetImageNamed(IDR_MEDIA_ROUTER_IDLE_ICON
)),
37 media_router_warning_icon_(
38 ui::ResourceBundle::GetSharedInstance()
39 .GetImageNamed(IDR_MEDIA_ROUTER_WARNING_ICON
)),
40 current_icon_(&media_router_idle_icon_
),
41 has_local_route_(false),
43 platform_delegate_(MediaRouterActionPlatformDelegate::Create(browser
)),
44 contextual_menu_(browser
) {}
46 MediaRouterAction::~MediaRouterAction() {
49 std::string
MediaRouterAction::GetId() const {
50 return ComponentToolbarActionsFactory::kMediaRouterActionId
;
53 void MediaRouterAction::SetDelegate(ToolbarActionViewDelegate
* delegate
) {
57 gfx::Image
MediaRouterAction::GetIcon(content::WebContents
* web_contents
,
58 const gfx::Size
& size
) {
59 return *current_icon_
;
62 base::string16
MediaRouterAction::GetActionName() const {
63 return l10n_util::GetStringUTF16(IDS_MEDIA_ROUTER_TITLE
);
66 base::string16
MediaRouterAction::GetAccessibleName(
67 content::WebContents
* web_contents
) const {
68 return GetTooltip(web_contents
);
71 base::string16
MediaRouterAction::GetTooltip(
72 content::WebContents
* web_contents
) const {
73 return l10n_util::GetStringUTF16(IDS_MEDIA_ROUTER_SHARE_YOUR_SCREEN_TEXT
);
76 bool MediaRouterAction::IsEnabled(
77 content::WebContents
* web_contents
) const {
81 bool MediaRouterAction::WantsToRun(
82 content::WebContents
* web_contents
) const {
86 bool MediaRouterAction::HasPopup(
87 content::WebContents
* web_contents
) const {
91 void MediaRouterAction::HidePopup() {
92 GetMediaRouterDialogController()->HideMediaRouterDialog();
95 gfx::NativeView
MediaRouterAction::GetPopupNativeView() {
99 ui::MenuModel
* MediaRouterAction::GetContextMenu() {
100 return contextual_menu_
.menu_model();
103 bool MediaRouterAction::ExecuteAction(bool by_user
) {
104 GetMediaRouterDialogController()->ShowMediaRouterDialog();
105 if (platform_delegate_
)
106 platform_delegate_
->CloseOverflowMenuIfOpen();
110 void MediaRouterAction::UpdateState() {
112 delegate_
->UpdateState();
115 bool MediaRouterAction::DisabledClickOpensMenu() const {
119 void MediaRouterAction::OnIssueUpdated(const media_router::Issue
* issue
) {
120 issue_
.reset(issue
? new media_router::Issue(*issue
) : nullptr);
125 void MediaRouterAction::OnRoutesUpdated(
126 const std::vector
<media_router::MediaRoute
>& routes
) {
128 std::find_if(routes
.begin(), routes
.end(),
129 [](const media_router::MediaRoute
& route
) {
130 return route
.is_local(); }) !=
135 MediaRouterDialogController
*
136 MediaRouterAction::GetMediaRouterDialogController() {
138 content::WebContents
* web_contents
= delegate_
->GetCurrentWebContents();
139 DCHECK(web_contents
);
140 return MediaRouterDialogController::GetOrCreateForWebContents(web_contents
);
143 media_router::MediaRouter
* MediaRouterAction::GetMediaRouter(Browser
* browser
) {
144 return media_router::MediaRouterFactory::GetApiForBrowserContext(
145 static_cast<content::BrowserContext
*>(browser
->profile()));
148 void MediaRouterAction::MaybeUpdateIcon() {
149 const gfx::Image
* new_icon
= GetCurrentIcon();
151 // Update the current state if it has changed.
152 if (new_icon
!= current_icon_
) {
153 current_icon_
= new_icon
;
155 // Tell the associated view to update its icon to reflect the change made
158 delegate_
->UpdateState();
162 const gfx::Image
* MediaRouterAction::GetCurrentIcon() const {
163 // Highest priority is to indicate whether there's an issue.
165 if (issue_
->severity() == media_router::Issue::FATAL
)
166 return &media_router_error_icon_
;
167 if (issue_
->severity() == media_router::Issue::WARNING
)
168 return &media_router_warning_icon_
;
171 return has_local_route_
?
172 &media_router_active_icon_
: &media_router_idle_icon_
;