OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/toolbar/media_router_action.h" | 5 #include "chrome/browser/ui/toolbar/media_router_action.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.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" | |
8 #include "chrome/browser/media/router/media_router_dialog_controller.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/ui/browser.h" | |
9 #include "chrome/browser/ui/toolbar/media_router_action_platform_delegate.h" | 15 #include "chrome/browser/ui/toolbar/media_router_action_platform_delegate.h" |
10 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h" | 16 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h" |
11 #include "chrome/grit/generated_resources.h" | 17 #include "chrome/grit/generated_resources.h" |
12 #include "grit/theme_resources.h" | 18 #include "grit/theme_resources.h" |
13 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
14 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
15 #include "ui/gfx/image/image_skia.h" | 21 #include "ui/gfx/image/image_skia.h" |
16 | 22 |
17 using media_router::MediaRouterDialogController; | 23 using media_router::MediaRouterDialogController; |
18 | 24 |
19 MediaRouterAction::MediaRouterAction(Browser* browser) | 25 MediaRouterAction::MediaRouterAction(Browser* browser) |
20 : id_("media_router_action"), | 26 : media_router::IssuesObserver(GetMediaRouter(browser)), |
27 media_router::MediaRoutesObserver(GetMediaRouter(browser)), | |
28 id_("media_router_action"), | |
21 name_(l10n_util::GetStringUTF16(IDS_MEDIA_ROUTER_TITLE)), | 29 name_(l10n_util::GetStringUTF16(IDS_MEDIA_ROUTER_TITLE)), |
30 media_router_active_icon_(ui::ResourceBundle::GetSharedInstance(). | |
31 GetImageNamed(IDR_MEDIA_ROUTER_ACTIVE_ICON)), | |
32 media_router_error_icon_(ui::ResourceBundle::GetSharedInstance(). | |
33 GetImageNamed(IDR_MEDIA_ROUTER_ERROR_ICON)), | |
22 media_router_idle_icon_(ui::ResourceBundle::GetSharedInstance(). | 34 media_router_idle_icon_(ui::ResourceBundle::GetSharedInstance(). |
23 GetImageNamed(IDR_MEDIA_ROUTER_IDLE_ICON)), | 35 GetImageNamed(IDR_MEDIA_ROUTER_IDLE_ICON)), |
36 media_router_warning_icon_(ui::ResourceBundle::GetSharedInstance(). | |
37 GetImageNamed(IDR_MEDIA_ROUTER_WARNING_ICON)), | |
38 state_(MEDIA_ROUTER_ACTION_IDLE), | |
39 issue_(nullptr), | |
40 has_local_routes_(false), | |
24 delegate_(nullptr), | 41 delegate_(nullptr), |
25 platform_delegate_(MediaRouterActionPlatformDelegate::Create(browser)), | 42 platform_delegate_(MediaRouterActionPlatformDelegate::Create(browser)), |
26 contextual_menu_(browser) { | 43 contextual_menu_(browser) { |
27 } | 44 } |
28 | 45 |
29 MediaRouterAction::~MediaRouterAction() { | 46 MediaRouterAction::~MediaRouterAction() { |
30 } | 47 } |
31 | 48 |
32 const std::string& MediaRouterAction::GetId() const { | 49 const std::string& MediaRouterAction::GetId() const { |
33 return id_; | 50 return id_; |
34 } | 51 } |
35 | 52 |
36 void MediaRouterAction::SetDelegate(ToolbarActionViewDelegate* delegate) { | 53 void MediaRouterAction::SetDelegate(ToolbarActionViewDelegate* delegate) { |
37 delegate_ = delegate; | 54 delegate_ = delegate; |
38 } | 55 } |
39 | 56 |
40 gfx::Image MediaRouterAction::GetIcon(content::WebContents* web_contents, | 57 gfx::Image MediaRouterAction::GetIcon(content::WebContents* web_contents, |
41 const gfx::Size& size) { | 58 const gfx::Size& size) { |
42 // TODO(apacible): Return icon based on casting state. | 59 switch (state_) { |
43 return media_router_idle_icon_; | 60 case MEDIA_ROUTER_ACTION_IDLE: |
61 return media_router_idle_icon_; | |
62 case MEDIA_ROUTER_ACTION_ACTIVE: | |
63 return media_router_active_icon_; | |
64 case MEDIA_ROUTER_ACTION_ERROR: | |
65 return media_router_error_icon_; | |
66 case MEDIA_ROUTER_ACTION_WARNING: | |
67 return media_router_warning_icon_; | |
68 default: | |
Peter Kasting
2015/08/03 21:28:51
Don't add a default case when your switch handles
apacible
2015/08/05 06:47:55
Acknowledged; ended up removing enum.
| |
69 NOTREACHED(); | |
70 return media_router_idle_icon_; | |
71 } | |
44 } | 72 } |
45 | 73 |
46 base::string16 MediaRouterAction::GetActionName() const { | 74 base::string16 MediaRouterAction::GetActionName() const { |
47 return name_; | 75 return name_; |
48 } | 76 } |
49 | 77 |
50 base::string16 MediaRouterAction::GetAccessibleName( | 78 base::string16 MediaRouterAction::GetAccessibleName( |
51 content::WebContents* web_contents) const { | 79 content::WebContents* web_contents) const { |
52 return GetTooltip(web_contents); | 80 return GetTooltip(web_contents); |
53 } | 81 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
91 bool MediaRouterAction::ExecuteAction(bool by_user) { | 119 bool MediaRouterAction::ExecuteAction(bool by_user) { |
92 GetMediaRouterDialogController()->ShowMediaRouterDialog(); | 120 GetMediaRouterDialogController()->ShowMediaRouterDialog(); |
93 if (platform_delegate_) | 121 if (platform_delegate_) |
94 platform_delegate_->CloseOverflowMenuIfOpen(); | 122 platform_delegate_->CloseOverflowMenuIfOpen(); |
95 return true; | 123 return true; |
96 } | 124 } |
97 | 125 |
98 void MediaRouterAction::UpdateState() { | 126 void MediaRouterAction::UpdateState() { |
99 } | 127 } |
100 | 128 |
129 bool MediaRouterAction::DisabledClickOpensMenu() const { | |
130 return false; | |
131 } | |
132 | |
133 void MediaRouterAction::OnIssueUpdated(const media_router::Issue* issue) { | |
134 if (issue) | |
135 issue_.reset(new media_router::Issue(*issue)); | |
136 else | |
137 issue_.reset(); | |
Peter Kasting
2015/08/03 21:28:51
Nit: Simpler:
issue.reset(issue ? new media_rou
apacible
2015/08/05 06:47:55
Done.
| |
138 | |
139 MaybeUpdateIcon(); | |
140 } | |
141 | |
142 void MediaRouterAction::OnRoutesUpdated( | |
143 const std::vector<media_router::MediaRoute>& routes) { | |
144 // Check if any of the routes are local. | |
145 auto it = std::find_if(routes.begin(), routes.end(), | |
146 [](const media_router::MediaRoute& route) { | |
147 return route.is_local(); | |
148 }); | |
149 | |
150 has_local_routes_ = it != routes.end(); | |
Peter Kasting
2015/08/03 21:28:51
Nit: Simpler:
has_local_routes_ =
std::fi
apacible
2015/08/05 06:47:55
Done.
| |
151 | |
152 MaybeUpdateIcon(); | |
153 } | |
154 | |
101 MediaRouterDialogController* | 155 MediaRouterDialogController* |
102 MediaRouterAction::GetMediaRouterDialogController() { | 156 MediaRouterAction::GetMediaRouterDialogController() { |
103 DCHECK(delegate_); | 157 DCHECK(delegate_); |
104 content::WebContents* web_contents = delegate_->GetCurrentWebContents(); | 158 content::WebContents* web_contents = delegate_->GetCurrentWebContents(); |
105 DCHECK(web_contents); | 159 DCHECK(web_contents); |
106 return MediaRouterDialogController::GetOrCreateForWebContents(web_contents); | 160 return MediaRouterDialogController::GetOrCreateForWebContents(web_contents); |
107 } | 161 } |
108 | 162 |
109 bool MediaRouterAction::DisabledClickOpensMenu() const { | 163 media_router::MediaRouter* MediaRouterAction::GetMediaRouter(Browser* browser) { |
110 return false; | 164 return media_router::MediaRouterFactory::GetApiForBrowserContext( |
165 (content::BrowserContext*) browser->profile()); | |
Peter Kasting
2015/08/03 21:28:51
C++-style casts, please
apacible
2015/08/05 06:47:55
Done.
| |
111 } | 166 } |
167 | |
168 void MediaRouterAction::MaybeUpdateIcon() { | |
169 MediaRouterActionState new_state = GetMediaRouterActionState(); | |
170 | |
171 // Update the current state if it has changed. | |
172 if (new_state != state_) { | |
173 state_ = new_state; | |
174 | |
175 // Update the icon to reflect the new state. | |
176 if (delegate_) | |
177 delegate_->UpdateState(); | |
178 } | |
179 } | |
180 | |
181 MediaRouterActionState MediaRouterAction::GetMediaRouterActionState() { | |
182 // Highest priority is to indicate whether there's an issue. | |
183 if (issue_) { | |
184 if (issue_.get()->severity() == media_router::Issue::FATAL) | |
Peter Kasting
2015/08/03 21:28:51
Don't use .get() just to call ->. Use -> directly
apacible
2015/08/05 06:47:55
Done.
| |
185 return MEDIA_ROUTER_ACTION_ERROR; | |
186 else if (issue_.get()->severity() == media_router::Issue::WARNING) | |
Peter Kasting
2015/08/03 21:28:51
No else after return
apacible
2015/08/05 06:47:55
Done.
| |
187 return MEDIA_ROUTER_ACTION_WARNING; | |
188 } | |
189 | |
190 if (has_local_routes_) | |
191 return MEDIA_ROUTER_ACTION_ACTIVE; | |
192 | |
193 return MEDIA_ROUTER_ACTION_IDLE; | |
Peter Kasting
2015/08/03 21:28:51
Nit: Simpler:
return has_local_routes_ ?
apacible
2015/08/05 06:47:55
Done.
| |
194 } | |
OLD | NEW |