Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: chrome/browser/ui/toolbar/media_router_action.cc

Issue 2332693003: Show media router toolbar icon ephemerally for active local routes and issues (Closed)
Patch Set: Address Devlin's comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/metrics/user_metrics.h" 7 #include "base/metrics/user_metrics.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/media/router/issue.h" 9 #include "chrome/browser/media/router/issue.h"
10 #include "chrome/browser/media/router/media_route.h" 10 #include "chrome/browser/media/router/media_route.h"
(...skipping 23 matching lines...) Expand all
34 } 34 }
35 35
36 } // namespace 36 } // namespace
37 37
38 MediaRouterAction::MediaRouterAction(Browser* browser, 38 MediaRouterAction::MediaRouterAction(Browser* browser,
39 ToolbarActionsBar* toolbar_actions_bar) 39 ToolbarActionsBar* toolbar_actions_bar)
40 : media_router::IssuesObserver(GetMediaRouter(browser)), 40 : media_router::IssuesObserver(GetMediaRouter(browser)),
41 media_router::MediaRoutesObserver(GetMediaRouter(browser)), 41 media_router::MediaRoutesObserver(GetMediaRouter(browser)),
42 current_icon_(gfx::VectorIconId::MEDIA_ROUTER_IDLE), 42 current_icon_(gfx::VectorIconId::MEDIA_ROUTER_IDLE),
43 has_local_display_route_(false), 43 has_local_display_route_(false),
44 has_dialog_(false),
44 delegate_(nullptr), 45 delegate_(nullptr),
45 browser_(browser), 46 browser_(browser),
46 toolbar_actions_bar_(toolbar_actions_bar), 47 toolbar_actions_bar_(toolbar_actions_bar),
47 platform_delegate_(MediaRouterActionPlatformDelegate::Create(browser)), 48 platform_delegate_(MediaRouterActionPlatformDelegate::Create(browser)),
48 contextual_menu_(browser), 49 contextual_menu_(browser),
49 tab_strip_model_observer_(this), 50 tab_strip_model_observer_(this),
51 toolbar_actions_bar_observer_(this),
50 weak_ptr_factory_(this) { 52 weak_ptr_factory_(this) {
51 DCHECK(browser_); 53 DCHECK(browser_);
52 DCHECK(toolbar_actions_bar_); 54 DCHECK(toolbar_actions_bar_);
53 tab_strip_model_observer_.Add(browser_->tab_strip_model()); 55 tab_strip_model_observer_.Add(browser_->tab_strip_model());
54 56 toolbar_actions_bar_observer_.Add(toolbar_actions_bar_);
55 RegisterObserver(); 57 RegisterObserver();
56 } 58 }
57 59
58 MediaRouterAction::~MediaRouterAction() { 60 MediaRouterAction::~MediaRouterAction() {
59 UnregisterObserver(); 61 UnregisterObserver();
60 } 62 }
61 63
62 std::string MediaRouterAction::GetId() const { 64 std::string MediaRouterAction::GetId() const {
63 return ComponentToolbarActionsFactory::kMediaRouterActionId; 65 return ComponentToolbarActionsFactory::kMediaRouterActionId;
64 } 66 }
65 67
66 void MediaRouterAction::SetDelegate(ToolbarActionViewDelegate* delegate) { 68 void MediaRouterAction::SetDelegate(ToolbarActionViewDelegate* delegate) {
67 delegate_ = delegate; 69 delegate_ = delegate;
68 70
69 // Updates the current popup state if |delegate_| is non-null and has
70 // WebContents ready.
71 // In cases such as opening a new browser window, SetDelegate() will be 71 // In cases such as opening a new browser window, SetDelegate() will be
72 // called before the WebContents is set. In those cases, we update the popup 72 // called before the WebContents is set. In those cases, we register with the
73 // state when ActiveTabChanged() is called. 73 // dialog controller when ActiveTabChanged() is called.
74 if (delegate_ && delegate_->GetCurrentWebContents()) 74 if (delegate_ && delegate_->GetCurrentWebContents())
75 UpdatePopupState(); 75 RegisterWithDialogController();
76 } 76 }
77 77
78 gfx::Image MediaRouterAction::GetIcon(content::WebContents* web_contents, 78 gfx::Image MediaRouterAction::GetIcon(content::WebContents* web_contents,
79 const gfx::Size& size) { 79 const gfx::Size& size) {
80 // Color is defined in the icon. 80 // Color is defined in the icon.
81 return gfx::Image( 81 return gfx::Image(
82 gfx::CreateVectorIcon(current_icon_, gfx::kPlaceholderColor)); 82 gfx::CreateVectorIcon(current_icon_, gfx::kPlaceholderColor));
83 } 83 }
84 84
85 base::string16 MediaRouterAction::GetActionName() const { 85 base::string16 MediaRouterAction::GetActionName() const {
(...skipping 20 matching lines...) Expand all
106 return false; 106 return false;
107 } 107 }
108 108
109 bool MediaRouterAction::HasPopup( 109 bool MediaRouterAction::HasPopup(
110 content::WebContents* web_contents) const { 110 content::WebContents* web_contents) const {
111 return true; 111 return true;
112 } 112 }
113 113
114 void MediaRouterAction::HidePopup() { 114 void MediaRouterAction::HidePopup() {
115 GetMediaRouterDialogController()->HideMediaRouterDialog(); 115 GetMediaRouterDialogController()->HideMediaRouterDialog();
116 OnPopupHidden();
117 } 116 }
118 117
119 gfx::NativeView MediaRouterAction::GetPopupNativeView() { 118 gfx::NativeView MediaRouterAction::GetPopupNativeView() {
120 return nullptr; 119 return nullptr;
121 } 120 }
122 121
123 ui::MenuModel* MediaRouterAction::GetContextMenu() { 122 ui::MenuModel* MediaRouterAction::GetContextMenu() {
124 return contextual_menu_.menu_model(); 123 return contextual_menu_.menu_model();
125 } 124 }
126 125
127 bool MediaRouterAction::ExecuteAction(bool by_user) { 126 bool MediaRouterAction::ExecuteAction(bool by_user) {
128 base::RecordAction(base::UserMetricsAction("MediaRouter_Icon_Click")); 127 base::RecordAction(base::UserMetricsAction("MediaRouter_Icon_Click"));
129 128
130 if (GetMediaRouterDialogController()->IsShowingMediaRouterDialog()) { 129 if (GetMediaRouterDialogController()->IsShowingMediaRouterDialog()) {
131 GetMediaRouterDialogController()->HideMediaRouterDialog(); 130 GetMediaRouterDialogController()->HideMediaRouterDialog();
132 return false; 131 return false;
133 } 132 }
134 133
135 GetMediaRouterDialogController()->ShowMediaRouterDialog(); 134 GetMediaRouterDialogController()->ShowMediaRouterDialog();
136 if (GetPlatformDelegate()) { 135 if (GetPlatformDelegate()) {
137 media_router::MediaRouterMetrics::RecordMediaRouterDialogOrigin( 136 media_router::MediaRouterMetrics::RecordMediaRouterDialogOrigin(
138 GetPlatformDelegate()->CloseOverflowMenuIfOpen() ? 137 GetPlatformDelegate()->CloseOverflowMenuIfOpen() ?
139 media_router::MediaRouterDialogOpenOrigin::OVERFLOW_MENU : 138 media_router::MediaRouterDialogOpenOrigin::OVERFLOW_MENU :
140 media_router::MediaRouterDialogOpenOrigin::TOOLBAR); 139 media_router::MediaRouterDialogOpenOrigin::TOOLBAR);
141 } 140 }
142 return true; 141 return true;
143 } 142 }
144 143
145 void MediaRouterAction::UpdateState() { 144 void MediaRouterAction::UpdateState() {
146 if (delegate_) 145 DCHECK(delegate_);
msw 2016/09/21 15:40:59 nit: no need to dcheck right before deref
takumif 2016/09/21 17:08:18 Removed.
147 delegate_->UpdateState(); 146 delegate_->UpdateState();
148 } 147 }
149 148
150 bool MediaRouterAction::DisabledClickOpensMenu() const { 149 bool MediaRouterAction::DisabledClickOpensMenu() const {
151 return false; 150 return false;
152 } 151 }
153 152
154 void MediaRouterAction::OnIssueUpdated(const media_router::Issue* issue) { 153 void MediaRouterAction::OnIssueUpdated(const media_router::Issue* issue) {
155 issue_.reset(issue ? new media_router::Issue(*issue) : nullptr); 154 issue_.reset(issue ? new media_router::Issue(*issue) : nullptr);
156
157 MaybeUpdateIcon(); 155 MaybeUpdateIcon();
158 } 156 }
159 157
160 void MediaRouterAction::OnRoutesUpdated( 158 void MediaRouterAction::OnRoutesUpdated(
161 const std::vector<media_router::MediaRoute>& routes, 159 const std::vector<media_router::MediaRoute>& routes,
162 const std::vector<media_router::MediaRoute::Id>& joinable_route_ids) { 160 const std::vector<media_router::MediaRoute::Id>& joinable_route_ids) {
163 has_local_display_route_ = 161 has_local_display_route_ =
164 std::find_if(routes.begin(), routes.end(), 162 std::find_if(routes.begin(), routes.end(),
165 [](const media_router::MediaRoute& route) { 163 [](const media_router::MediaRoute& route) {
166 return route.is_local() && route.for_display(); 164 return route.is_local() && route.for_display();
167 }) != routes.end(); 165 }) != routes.end();
168 MaybeUpdateIcon(); 166 MaybeUpdateIcon();
169 } 167 }
170 168
171 void MediaRouterAction::ActiveTabChanged(content::WebContents* old_contents, 169 void MediaRouterAction::ActiveTabChanged(content::WebContents* old_contents,
172 content::WebContents* new_contents, 170 content::WebContents* new_contents,
173 int index, 171 int index,
174 int reason) { 172 int reason) {
175 UpdatePopupState(); 173 RegisterWithDialogController();
174 UpdateDialogState();
176 } 175 }
177 176
178 void MediaRouterAction::OnPopupHidden() { 177 void MediaRouterAction::OnToolbarActionsBarAnimationEnded() {
179 if (delegate_) 178 // Depress the action if the dialog is shown.
msw 2016/09/21 15:40:59 nit: just call UpdateDialogState()?
takumif 2016/09/21 17:08:18 Done.
180 delegate_->OnPopupClosed(); 179 DCHECK(delegate_);
180 if (GetMediaRouterDialogController()->IsShowingMediaRouterDialog())
181 OnDialogShown();
181 } 182 }
182 183
183 void MediaRouterAction::OnPopupShown() { 184 void MediaRouterAction::OnDialogHidden() {
184 // We depress the action regardless of whether ExecuteAction() was user 185 if (has_dialog_) {
185 // initiated. 186 has_dialog_ = false;
186 if (delegate_) 187 delegate_->OnPopupClosed();
187 delegate_->OnPopupShown(true); 188 }
188 } 189 }
189 190
190 void MediaRouterAction::UpdatePopupState() { 191 void MediaRouterAction::OnDialogShown() {
192 if (!has_dialog_) {
193 has_dialog_ = true;
194 // We depress the action regardless of whether ExecuteAction() was user
195 // initiated.
196 delegate_->OnPopupShown(true);
197 }
198 }
199
200 void MediaRouterAction::RegisterWithDialogController() {
191 MediaRouterDialogControllerImpl* controller = 201 MediaRouterDialogControllerImpl* controller =
192 GetMediaRouterDialogController(); 202 GetMediaRouterDialogController();
193 203
194 if (!controller) 204 if (!controller)
195 return; 205 return;
196 206
197 // When each browser window is created, its toolbar creates a 207 // |controller| keeps track of |this| if |this| was created with the browser
198 // MediaRouterAction that is only destroyed when the browser window is torn 208 // window or ephemerally by activating the Cast functionality. If |this| was
199 // down. |controller| will keep track of that instance. If |this| was created 209 // created in overflow mode, it will be destroyed when the overflow menu is
200 // in overflow mode, it will be destroyed when the overflow menu is closed. 210 // closed.
201 // If SetMediaRouterAction() was previously called, this is a no-op.
202 if (!toolbar_actions_bar_->in_overflow_mode()) 211 if (!toolbar_actions_bar_->in_overflow_mode())
203 controller->SetMediaRouterAction(weak_ptr_factory_.GetWeakPtr()); 212 controller->SetMediaRouterAction(weak_ptr_factory_.GetWeakPtr());
213 }
204 214
205 // Update the button in case the pressed state is out of sync with dialog 215 void MediaRouterAction::UpdateDialogState() {
206 // visibility. 216 if (GetMediaRouterDialogController()->IsShowingMediaRouterDialog())
207 if (controller->IsShowingMediaRouterDialog()) 217 OnDialogShown();
208 OnPopupShown();
209 else 218 else
210 OnPopupHidden(); 219 OnDialogHidden();
211 } 220 }
212 221
213 MediaRouterDialogControllerImpl* 222 MediaRouterDialogControllerImpl*
214 MediaRouterAction::GetMediaRouterDialogController() { 223 MediaRouterAction::GetMediaRouterDialogController() {
215 DCHECK(delegate_); 224 DCHECK(delegate_);
216 content::WebContents* web_contents = delegate_->GetCurrentWebContents(); 225 content::WebContents* web_contents = delegate_->GetCurrentWebContents();
217 DCHECK(web_contents); 226 DCHECK(web_contents);
218 return MediaRouterDialogControllerImpl::GetOrCreateForWebContents( 227 return MediaRouterDialogControllerImpl::GetOrCreateForWebContents(
219 web_contents); 228 web_contents);
220 } 229 }
221 230
222 MediaRouterActionPlatformDelegate* MediaRouterAction::GetPlatformDelegate() { 231 MediaRouterActionPlatformDelegate* MediaRouterAction::GetPlatformDelegate() {
223 return platform_delegate_.get(); 232 return platform_delegate_.get();
224 } 233 }
225 234
226 void MediaRouterAction::MaybeUpdateIcon() { 235 void MediaRouterAction::MaybeUpdateIcon() {
227 gfx::VectorIconId new_icon = GetCurrentIcon(); 236 gfx::VectorIconId new_icon = GetCurrentIcon();
228 237
229 // Update the current state if it has changed. 238 // Update the current state if it has changed.
230 if (new_icon != current_icon_) { 239 if (new_icon != current_icon_) {
231 current_icon_ = new_icon; 240 current_icon_ = new_icon;
232 241
233 // Tell the associated view to update its icon to reflect the change made 242 // Tell the associated view to update its icon to reflect the change made
234 // above. 243 // above. If MaybeUpdateIcon() was called as a result of instantiating
244 // |this|, then |delegate_| may not be set yet.
235 if (delegate_) 245 if (delegate_)
236 delegate_->UpdateState(); 246 UpdateState();
237 } 247 }
238 } 248 }
239 249
240 gfx::VectorIconId MediaRouterAction::GetCurrentIcon() const { 250 gfx::VectorIconId MediaRouterAction::GetCurrentIcon() const {
241 // Highest priority is to indicate whether there's an issue. 251 // Highest priority is to indicate whether there's an issue.
242 if (issue_) { 252 if (issue_) {
243 if (issue_->severity() == media_router::Issue::FATAL) 253 if (issue_->severity() == media_router::Issue::FATAL)
244 return gfx::VectorIconId::MEDIA_ROUTER_ERROR; 254 return gfx::VectorIconId::MEDIA_ROUTER_ERROR;
245 if (issue_->severity() == media_router::Issue::WARNING) 255 if (issue_->severity() == media_router::Issue::WARNING)
246 return gfx::VectorIconId::MEDIA_ROUTER_WARNING; 256 return gfx::VectorIconId::MEDIA_ROUTER_WARNING;
247 } 257 }
248 258
249 return has_local_display_route_ ? gfx::VectorIconId::MEDIA_ROUTER_ACTIVE 259 return has_local_display_route_ ? gfx::VectorIconId::MEDIA_ROUTER_ACTIVE
250 : gfx::VectorIconId::MEDIA_ROUTER_IDLE; 260 : gfx::VectorIconId::MEDIA_ROUTER_IDLE;
251 } 261 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698