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

Side by Side Diff: chrome/browser/ui/webui/media_router/media_router_dialog_controller_impl.cc

Issue 2155293002: Show the Cast toolbar icon ephemerally when Cast is in use (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 4 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/webui/media_router/media_router_dialog_controller_im pl.h" 5 #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller_im pl.h"
6 6
7 #include <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/trace_event/trace_event.h" 12 #include "base/trace_event/trace_event.h"
13 #include "build/build_config.h" 13 #include "build/build_config.h"
14 #include "chrome/browser/media/router/presentation_service_delegate_impl.h" 14 #include "chrome/browser/media/router/presentation_service_delegate_impl.h"
15 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/browser_finder.h" 16 #include "chrome/browser/ui/browser_finder.h"
17 #include "chrome/browser/ui/browser_window.h" 17 #include "chrome/browser/ui/browser_window.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h"
18 #include "chrome/browser/ui/toolbar/media_router_action.h" 20 #include "chrome/browser/ui/toolbar/media_router_action.h"
19 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h" 21 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h"
20 #include "chrome/browser/ui/webui/media_router/media_router_ui.h" 22 #include "chrome/browser/ui/webui/media_router/media_router_ui.h"
21 #include "chrome/common/url_constants.h" 23 #include "chrome/common/url_constants.h"
22 #include "components/guest_view/browser/guest_view_base.h" 24 #include "components/guest_view/browser/guest_view_base.h"
23 #include "components/web_modal/web_contents_modal_dialog_host.h" 25 #include "components/web_modal/web_contents_modal_dialog_host.h"
24 #include "content/public/browser/navigation_controller.h" 26 #include "content/public/browser/navigation_controller.h"
25 #include "content/public/browser/navigation_details.h" 27 #include "content/public/browser/navigation_details.h"
26 #include "content/public/browser/navigation_entry.h" 28 #include "content/public/browser/navigation_entry.h"
27 #include "content/public/browser/render_frame_host.h" 29 #include "content/public/browser/render_frame_host.h"
(...skipping 17 matching lines...) Expand all
45 namespace { 47 namespace {
46 48
47 constexpr const int kMaxHeight = 2000; 49 constexpr const int kMaxHeight = 2000;
48 constexpr const int kMinHeight = 80; 50 constexpr const int kMinHeight = 80;
49 constexpr const int kWidth = 340; 51 constexpr const int kWidth = 340;
50 52
51 // WebDialogDelegate that specifies what the Media Router dialog 53 // WebDialogDelegate that specifies what the Media Router dialog
52 // will look like. 54 // will look like.
53 class MediaRouterDialogDelegate : public WebDialogDelegate { 55 class MediaRouterDialogDelegate : public WebDialogDelegate {
54 public: 56 public:
55 MediaRouterDialogDelegate(base::WeakPtr<MediaRouterAction> action, 57 explicit MediaRouterDialogDelegate(
56 const base::WeakPtr<MediaRouterDialogControllerImpl>& controller) 58 const base::WeakPtr<MediaRouterDialogControllerImpl>& controller)
57 : action_(action), 59 : controller_(controller) {}
58 controller_(controller) {}
59 ~MediaRouterDialogDelegate() override {} 60 ~MediaRouterDialogDelegate() override {}
60 61
61 // WebDialogDelegate implementation. 62 // WebDialogDelegate implementation.
62 ui::ModalType GetDialogModalType() const override { 63 ui::ModalType GetDialogModalType() const override {
63 // Not used, returning dummy value. 64 // Not used, returning dummy value.
64 return ui::MODAL_TYPE_WINDOW; 65 return ui::MODAL_TYPE_WINDOW;
65 } 66 }
66 67
67 base::string16 GetDialogTitle() const override { 68 base::string16 GetDialogTitle() const override {
68 return base::string16(); 69 return base::string16();
69 } 70 }
70 71
71 GURL GetDialogContentURL() const override { 72 GURL GetDialogContentURL() const override {
72 return GURL(chrome::kChromeUIMediaRouterURL); 73 return GURL(chrome::kChromeUIMediaRouterURL);
73 } 74 }
74 75
75 void GetWebUIMessageHandlers( 76 void GetWebUIMessageHandlers(
76 std::vector<WebUIMessageHandler*>* handlers) const override { 77 std::vector<WebUIMessageHandler*>* handlers) const override {
77 // MediaRouterUI adds its own message handlers. 78 // MediaRouterUI adds its own message handlers.
78 } 79 }
79 80
80 void GetDialogSize(gfx::Size* size) const override; 81 void GetDialogSize(gfx::Size* size) const override {
82 DCHECK(size);
83 // GetDialogSize() is called when the browser window resizes. We may want to
84 // update the maximum height of the dialog and scale the WebUI to the new
85 // height. |size| is not set because the dialog is auto-resizeable.
86 controller_->UpdateMaxDialogSize();
87 }
81 88
82 std::string GetDialogArgs() const override { 89 std::string GetDialogArgs() const override {
83 return std::string(); 90 return std::string();
84 } 91 }
85 92
86 void OnDialogClosed(const std::string& json_retval) override {
87 // We don't delete |this| here because this class is owned
88 // by ConstrainedWebDialogDelegate.
89 if (action_)
90 action_->OnPopupHidden();
91 }
92
93 void OnCloseContents(WebContents* source, bool* out_close_dialog) override { 93 void OnCloseContents(WebContents* source, bool* out_close_dialog) override {
94 if (out_close_dialog) 94 if (out_close_dialog)
95 *out_close_dialog = true; 95 *out_close_dialog = true;
96 } 96 }
97 97
98 bool ShouldShowDialogTitle() const override { 98 bool ShouldShowDialogTitle() const override {
99 return false; 99 return false;
100 } 100 }
101 101
102 private: 102 private:
103 base::WeakPtr<MediaRouterAction> action_;
104 base::WeakPtr<MediaRouterDialogControllerImpl> controller_; 103 base::WeakPtr<MediaRouterDialogControllerImpl> controller_;
105 104
106 DISALLOW_COPY_AND_ASSIGN(MediaRouterDialogDelegate); 105 DISALLOW_COPY_AND_ASSIGN(MediaRouterDialogDelegate);
107 }; 106 };
108 107
109 void MediaRouterDialogDelegate::GetDialogSize(gfx::Size* size) const {
110 DCHECK(size);
111 // GetDialogSize() is called when the browser window resizes. We may want to
112 // update the maximum height of the dialog and scale the WebUI to the new
113 // height. |size| is not set because the dialog is auto-resizeable.
114 controller_->UpdateMaxDialogSize();
115 }
116
117 } // namespace 108 } // namespace
118 109
119 // static 110 // static
120 MediaRouterDialogControllerImpl* 111 MediaRouterDialogControllerImpl*
121 MediaRouterDialogControllerImpl::GetOrCreateForWebContents( 112 MediaRouterDialogControllerImpl::GetOrCreateForWebContents(
122 WebContents* web_contents) { 113 WebContents* web_contents) {
123 DCHECK(web_contents); 114 DCHECK(web_contents);
124 // This call does nothing if the controller already exists. 115 // This call does nothing if the controller already exists.
125 MediaRouterDialogControllerImpl::CreateForWebContents(web_contents); 116 MediaRouterDialogControllerImpl::CreateForWebContents(web_contents);
126 return MediaRouterDialogControllerImpl::FromWebContents(web_contents); 117 return MediaRouterDialogControllerImpl::FromWebContents(web_contents);
(...skipping 26 matching lines...) Expand all
153 dialog_controller_->CloseMediaRouterDialog(); 144 dialog_controller_->CloseMediaRouterDialog();
154 } 145 }
155 146
156 MediaRouterDialogControllerImpl* const dialog_controller_; 147 MediaRouterDialogControllerImpl* const dialog_controller_;
157 }; 148 };
158 149
159 MediaRouterDialogControllerImpl::MediaRouterDialogControllerImpl( 150 MediaRouterDialogControllerImpl::MediaRouterDialogControllerImpl(
160 WebContents* web_contents) 151 WebContents* web_contents)
161 : MediaRouterDialogController(web_contents), 152 : MediaRouterDialogController(web_contents),
162 media_router_dialog_pending_(false), 153 media_router_dialog_pending_(false),
154 tab_strip_model_observer_(this),
163 weak_ptr_factory_(this) { 155 weak_ptr_factory_(this) {
156 tab_strip_model_observer_.Add(
157 chrome::FindBrowserWithWebContents(initiator())->tab_strip_model());
164 } 158 }
165 159
166 MediaRouterDialogControllerImpl::~MediaRouterDialogControllerImpl() { 160 MediaRouterDialogControllerImpl::~MediaRouterDialogControllerImpl() {
167 } 161 }
168 162
169 WebContents* MediaRouterDialogControllerImpl::GetMediaRouterDialog() const { 163 WebContents* MediaRouterDialogControllerImpl::GetMediaRouterDialog() const {
170 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 164 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
171 return dialog_observer_.get() ? dialog_observer_->web_contents() : nullptr; 165 return dialog_observer_.get() ? dialog_observer_->web_contents() : nullptr;
172 } 166 }
173 167
174 void MediaRouterDialogControllerImpl::SetMediaRouterAction( 168 void MediaRouterDialogControllerImpl::SetMediaRouterAction(
175 const base::WeakPtr<MediaRouterAction>& action) { 169 const base::WeakPtr<MediaRouterAction>& action) {
176 if (!action_) 170 action_ = action;
177 action_ = action;
178 }
179
180 bool MediaRouterDialogControllerImpl::IsShowingMediaRouterDialog() const {
181 return GetMediaRouterDialog() != nullptr;
182 } 171 }
183 172
184 void MediaRouterDialogControllerImpl::UpdateMaxDialogSize() { 173 void MediaRouterDialogControllerImpl::UpdateMaxDialogSize() {
185 WebContents* media_router_dialog = GetMediaRouterDialog(); 174 WebContents* media_router_dialog = GetMediaRouterDialog();
186 if (!media_router_dialog) 175 if (!media_router_dialog)
187 return; 176 return;
188 177
189 content::WebUI* web_ui = media_router_dialog->GetWebUI(); 178 content::WebUI* web_ui = media_router_dialog->GetWebUI();
190 if (web_ui) { 179 if (web_ui) {
191 MediaRouterUI* media_router_ui = 180 MediaRouterUI* media_router_ui =
192 static_cast<MediaRouterUI*>(web_ui->GetController()); 181 static_cast<MediaRouterUI*>(web_ui->GetController());
193 if (media_router_ui) { 182 if (media_router_ui) {
194 Browser* browser = chrome::FindBrowserWithWebContents(initiator()); 183 Browser* browser = chrome::FindBrowserWithWebContents(initiator());
195 web_modal::WebContentsModalDialogHost* host = nullptr; 184 web_modal::WebContentsModalDialogHost* host = nullptr;
196 if (browser) 185 if (browser)
197 host = browser->window()->GetWebContentsModalDialogHost(); 186 host = browser->window()->GetWebContentsModalDialogHost();
198 187
199 gfx::Size maxSize = host ? 188 gfx::Size maxSize = host ?
200 host->GetMaximumDialogSize() : 189 host->GetMaximumDialogSize() :
201 initiator()->GetContainerBounds().size(); 190 initiator()->GetContainerBounds().size();
202 191
203 // The max height of the dialog should be 90% of the browser window 192 // The max height of the dialog should be 90% of the browser window
204 // height. The width stays fixed. 193 // height. The width stays fixed.
205 maxSize.Enlarge(0, -0.1 * maxSize.height()); 194 maxSize.Enlarge(0, -0.1 * maxSize.height());
206 media_router_ui->UpdateMaxDialogHeight(maxSize.height()); 195 media_router_ui->UpdateMaxDialogHeight(maxSize.height());
207 } 196 }
208 } 197 }
209 } 198 }
210 199
200 bool MediaRouterDialogControllerImpl::IsShowingMediaRouterDialog() const {
201 return GetMediaRouterDialog() != nullptr;
202 }
203
204 bool MediaRouterDialogControllerImpl::ShowMediaRouterDialog() {
205 if (!action_)
206 ShowMediaRouterAction();
207 return MediaRouterDialogController::ShowMediaRouterDialog();
208 }
209
210 void MediaRouterDialogControllerImpl::ActiveTabChanged(
211 content::WebContents* old_contents,
212 content::WebContents* new_contents,
213 int index,
214 int reason) {
215 if (IsShowingMediaRouterDialog() && !action_)
216 ShowMediaRouterAction();
217 }
218
211 void MediaRouterDialogControllerImpl::CloseMediaRouterDialog() { 219 void MediaRouterDialogControllerImpl::CloseMediaRouterDialog() {
212 WebContents* media_router_dialog = GetMediaRouterDialog(); 220 WebContents* media_router_dialog = GetMediaRouterDialog();
213 if (!media_router_dialog) 221 if (!media_router_dialog)
214 return; 222 return;
215 223
216 content::WebUI* web_ui = media_router_dialog->GetWebUI(); 224 content::WebUI* web_ui = media_router_dialog->GetWebUI();
217 if (web_ui) { 225 if (web_ui) {
218 MediaRouterUI* media_router_ui = 226 MediaRouterUI* media_router_ui =
219 static_cast<MediaRouterUI*>(web_ui->GetController()); 227 static_cast<MediaRouterUI*>(web_ui->GetController());
220 if (media_router_ui) 228 if (media_router_ui)
221 media_router_ui->Close(); 229 media_router_ui->Close();
222 } 230 }
223 } 231 }
224 232
225 void MediaRouterDialogControllerImpl::CreateMediaRouterDialog() { 233 void MediaRouterDialogControllerImpl::CreateMediaRouterDialog() {
226 DCHECK(!dialog_observer_.get()); 234 DCHECK(!dialog_observer_.get());
227 235
228 base::Time dialog_creation_time = base::Time::Now(); 236 base::Time dialog_creation_time = base::Time::Now();
229 TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("media_router", "UI", initiator()); 237 TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("media_router", "UI", initiator());
230 238
231 Profile* profile = 239 Profile* profile =
232 Profile::FromBrowserContext(initiator()->GetBrowserContext()); 240 Profile::FromBrowserContext(initiator()->GetBrowserContext());
233 DCHECK(profile); 241 DCHECK(profile);
234 242
235 // |web_dialog_delegate|'s owner is |constrained_delegate|. 243 // |web_dialog_delegate|'s owner is |constrained_delegate|.
236 // |constrained_delegate| is owned by the parent |views::View|. 244 // |constrained_delegate| is owned by the parent |views::View|.
237 WebDialogDelegate* web_dialog_delegate = 245 WebDialogDelegate* web_dialog_delegate =
238 new MediaRouterDialogDelegate(action_, weak_ptr_factory_.GetWeakPtr()); 246 new MediaRouterDialogDelegate(weak_ptr_factory_.GetWeakPtr());
239 247
240 // |ShowConstrainedWebDialogWithAutoResize()| will end up creating 248 // |ShowConstrainedWebDialogWithAutoResize()| will end up creating
241 // ConstrainedWebDialogDelegateViewViews containing a WebContents containing 249 // ConstrainedWebDialogDelegateViewViews containing a WebContents containing
242 // the MediaRouterUI, using the provided |web_dialog_delegate|. Then, the 250 // the MediaRouterUI, using the provided |web_dialog_delegate|. Then, the
243 // view is shown as a modal dialog constrained to the |initiator| WebContents. 251 // view is shown as a modal dialog constrained to the |initiator| WebContents.
244 // The dialog will resize between the given minimum and maximum size bounds 252 // The dialog will resize between the given minimum and maximum size bounds
245 // based on the currently rendered contents. 253 // based on the currently rendered contents.
246 ConstrainedWebDialogDelegate* constrained_delegate = 254 ConstrainedWebDialogDelegate* constrained_delegate =
247 ShowConstrainedWebDialogWithAutoResize( 255 ShowConstrainedWebDialogWithAutoResize(
248 profile, web_dialog_delegate, initiator(), 256 profile, web_dialog_delegate, initiator(),
(...skipping 10 matching lines...) Expand all
259 MediaRouterUI* media_router_ui = static_cast<MediaRouterUI*>( 267 MediaRouterUI* media_router_ui = static_cast<MediaRouterUI*>(
260 media_router_dialog->GetWebUI()->GetController()); 268 media_router_dialog->GetWebUI()->GetController());
261 DCHECK(media_router_ui); 269 DCHECK(media_router_ui);
262 media_router_ui->SetUIInitializationTimer(dialog_creation_time); 270 media_router_ui->SetUIInitializationTimer(dialog_creation_time);
263 } 271 }
264 272
265 media_router_dialog_pending_ = true; 273 media_router_dialog_pending_ = true;
266 274
267 dialog_observer_.reset(new DialogWebContentsObserver( 275 dialog_observer_.reset(new DialogWebContentsObserver(
268 media_router_dialog, this)); 276 media_router_dialog, this));
269
270 if (action_)
271 action_->OnPopupShown();
272 } 277 }
273 278
274 void MediaRouterDialogControllerImpl::Reset() { 279 void MediaRouterDialogControllerImpl::Reset() {
275 MediaRouterDialogController::Reset(); 280 MediaRouterDialogController::Reset();
276 dialog_observer_.reset(); 281 dialog_observer_.reset();
282 if (action_)
283 action_->OnPopupHidden();
277 } 284 }
278 285
279 void MediaRouterDialogControllerImpl::OnDialogNavigated( 286 void MediaRouterDialogControllerImpl::OnDialogNavigated(
280 const content::LoadCommittedDetails& details) { 287 const content::LoadCommittedDetails& details) {
281 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 288 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
282 WebContents* media_router_dialog = GetMediaRouterDialog(); 289 WebContents* media_router_dialog = GetMediaRouterDialog();
283 CHECK(media_router_dialog); 290 CHECK(media_router_dialog);
284 ui::PageTransition transition_type = details.entry->GetTransitionType(); 291 ui::PageTransition transition_type = details.entry->GetTransitionType();
285 content::NavigationType nav_type = details.type; 292 content::NavigationType nav_type = details.type;
286 293
287 // New |media_router_dialog| is created. 294 // New |media_router_dialog| is created.
288 DCHECK(media_router_dialog_pending_); 295 DCHECK(media_router_dialog_pending_);
289 DCHECK(ui::PageTransitionCoreTypeIs(transition_type, 296 DCHECK(ui::PageTransitionCoreTypeIs(transition_type,
290 ui::PAGE_TRANSITION_AUTO_TOPLEVEL) && 297 ui::PAGE_TRANSITION_AUTO_TOPLEVEL) &&
291 nav_type == content::NAVIGATION_TYPE_NEW_PAGE) 298 nav_type == content::NAVIGATION_TYPE_NEW_PAGE)
292 << "transition_type: " << transition_type << ", " 299 << "transition_type: " << transition_type << ", "
293 << "nav_type: " << nav_type; 300 << "nav_type: " << nav_type;
294 301
295 media_router_dialog_pending_ = false; 302 media_router_dialog_pending_ = false;
296 303
297 PopulateDialog(media_router_dialog); 304 PopulateDialog(media_router_dialog);
305
306 if (action_)
307 action_->OnPopupShown();
298 } 308 }
299 309
300 void MediaRouterDialogControllerImpl::PopulateDialog( 310 void MediaRouterDialogControllerImpl::PopulateDialog(
301 content::WebContents* media_router_dialog) { 311 content::WebContents* media_router_dialog) {
302 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 312 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
303 DCHECK(media_router_dialog); 313 DCHECK(media_router_dialog);
304 if (!initiator() || !media_router_dialog->GetWebUI()) { 314 if (!initiator() || !media_router_dialog->GetWebUI()) {
305 Reset(); 315 Reset();
306 return; 316 return;
307 } 317 }
(...skipping 10 matching lines...) Expand all
318 PresentationServiceDelegateImpl::GetOrCreateForWebContents(initiator()) 328 PresentationServiceDelegateImpl::GetOrCreateForWebContents(initiator())
319 ->GetWeakPtr(); 329 ->GetWeakPtr();
320 if (!create_connection_request.get()) { 330 if (!create_connection_request.get()) {
321 media_router_ui->InitWithDefaultMediaSource(delegate); 331 media_router_ui->InitWithDefaultMediaSource(delegate);
322 } else { 332 } else {
323 media_router_ui->InitWithPresentationSessionRequest( 333 media_router_ui->InitWithPresentationSessionRequest(
324 initiator(), delegate, std::move(create_connection_request)); 334 initiator(), delegate, std::move(create_connection_request));
325 } 335 }
326 } 336 }
327 337
338 void MediaRouterDialogControllerImpl::ShowMediaRouterAction() {
339 Profile* profile =
340 Profile::FromBrowserContext(initiator()->GetBrowserContext());
341 if (!profile)
342 return;
343
344 ToolbarActionsModel* model = ToolbarActionsModel::Get(profile);
345 if (model &&
346 !model->HasComponentAction(
347 ComponentToolbarActionsFactory::kMediaRouterActionId)) {
348 model->AddComponentAction(
349 ComponentToolbarActionsFactory::kMediaRouterActionId);
350 }
351 }
352
328 } // namespace media_router 353 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698