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