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

Side by Side Diff: chrome/browser/media/router/presentation_service_delegate_impl.cc

Issue 1406013003: [Presentation API / Media Router] Clean up default pres URL logic. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix compile again Created 5 years, 2 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/media/router/presentation_service_delegate_impl.h" 5 #include "chrome/browser/media/router/presentation_service_delegate_impl.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/containers/scoped_ptr_hash_map.h" 9 #include "base/containers/scoped_ptr_hash_map.h"
10 #include "base/containers/small_map.h" 10 #include "base/containers/small_map.h"
(...skipping 19 matching lines...) Expand all
30 DEFINE_WEB_CONTENTS_USER_DATA_KEY( 30 DEFINE_WEB_CONTENTS_USER_DATA_KEY(
31 media_router::PresentationServiceDelegateImpl); 31 media_router::PresentationServiceDelegateImpl);
32 32
33 using content::RenderFrameHost; 33 using content::RenderFrameHost;
34 34
35 namespace media_router { 35 namespace media_router {
36 36
37 namespace { 37 namespace {
38 38
39 using DelegateObserver = content::PresentationServiceDelegate::Observer; 39 using DelegateObserver = content::PresentationServiceDelegate::Observer;
40 using PresentationSessionErrorCallback =
41 content::PresentationServiceDelegate::PresentationSessionErrorCallback;
42 using PresentationSessionSuccessCallback =
43 content::PresentationServiceDelegate::PresentationSessionSuccessCallback;
44
45 // Returns the unique identifier for the supplied RenderFrameHost.
46 RenderFrameHostId GetRenderFrameHostId(RenderFrameHost* render_frame_host) {
47 int render_process_id = render_frame_host->GetProcess()->GetID();
48 int render_frame_id = render_frame_host->GetRoutingID();
49 return RenderFrameHostId(render_process_id, render_frame_id);
50 }
51 40
52 // Gets the last committed URL for the render frame specified by 41 // Gets the last committed URL for the render frame specified by
53 // |render_frame_host_id|. 42 // |render_frame_host_id|.
54 GURL GetLastCommittedURLForFrame(RenderFrameHostId render_frame_host_id) { 43 GURL GetLastCommittedURLForFrame(RenderFrameHostId render_frame_host_id) {
55 RenderFrameHost* render_frame_host = RenderFrameHost::FromID( 44 RenderFrameHost* render_frame_host = RenderFrameHost::FromID(
56 render_frame_host_id.first, render_frame_host_id.second); 45 render_frame_host_id.first, render_frame_host_id.second);
57 DCHECK(render_frame_host); 46 DCHECK(render_frame_host);
58 return render_frame_host->GetLastCommittedURL(); 47 return render_frame_host->GetLastCommittedURL();
59 } 48 }
60 49
(...skipping 16 matching lines...) Expand all
77 bool RemoveScreenAvailabilityListener( 66 bool RemoveScreenAvailabilityListener(
78 content::PresentationScreenAvailabilityListener* listener); 67 content::PresentationScreenAvailabilityListener* listener);
79 bool HasScreenAvailabilityListenerForTest( 68 bool HasScreenAvailabilityListenerForTest(
80 const MediaSource::Id& source_id) const; 69 const MediaSource::Id& source_id) const;
81 std::string GetDefaultPresentationId() const; 70 std::string GetDefaultPresentationId() const;
82 void ListenForSessionStateChange( 71 void ListenForSessionStateChange(
83 const content::SessionStateChangedCallback& state_changed_cb); 72 const content::SessionStateChangedCallback& state_changed_cb);
84 void ListenForSessionMessages( 73 void ListenForSessionMessages(
85 const content::PresentationSessionInfo& session, 74 const content::PresentationSessionInfo& session,
86 const content::PresentationSessionMessageCallback& message_cb); 75 const content::PresentationSessionMessageCallback& message_cb);
76
77 // Sets this frame's default prsentation request and callback.
mark a. foltz 2015/10/20 20:15:25 Typo in presentation request. Also a slight prefe
imcheng 2015/10/24 00:41:19 Done.
78 void SetDefaultPresentationRequest(
79 const PresentationRequest& default_presentation_request,
80 const content::PresentationSessionStartedCallback& callback);
81
82 // Clear this frame's default prsentation request and callback.
mark a. foltz 2015/10/20 20:15:25 Typo here
imcheng 2015/10/24 00:41:19 Done.
83 void ClearDefaultPresentationRequest();
87 void Reset(); 84 void Reset();
88 85
89 const MediaRoute::Id GetRouteId(const std::string& presentation_id) const; 86 const MediaRoute::Id GetRouteId(const std::string& presentation_id) const;
90 const std::vector<MediaRoute::Id> GetRouteIds() const; 87 const std::vector<MediaRoute::Id> GetRouteIds() const;
91 void OnPresentationSessionClosed(const std::string& presentation_id); 88 void OnPresentationSessionClosed(const std::string& presentation_id);
92 89
93 void OnPresentationSessionStarted( 90 void OnPresentationSessionStarted(
94 bool is_default_presentation, 91 const content::PresentationSessionInfo& session,
92 const MediaRoute::Id& route_id);
93 void OnDefaultPresentationSessionStarted(
mark a. foltz 2015/10/20 20:15:25 I wonder if this class needs to expose two differe
imcheng 2015/10/24 00:41:19 This is motivated by the need to pass in a callbac
94 const PresentationRequest& request,
95 const content::PresentationSessionInfo& session, 95 const content::PresentationSessionInfo& session,
96 const MediaRoute::Id& route_id); 96 const MediaRoute::Id& route_id);
97 void OnPresentationServiceDelegateDestroyed() const; 97 void OnPresentationServiceDelegateDestroyed() const;
98 98
99 void set_delegate_observer(DelegateObserver* observer) { 99 void set_delegate_observer(DelegateObserver* observer) {
100 delegate_observer_ = observer; 100 delegate_observer_ = observer;
101 } 101 }
102 102
103 private: 103 private:
104 MediaSource GetMediaSourceFromListener( 104 MediaSource GetMediaSourceFromListener(
105 content::PresentationScreenAvailabilityListener* listener) const; 105 content::PresentationScreenAvailabilityListener* listener) const;
106 MediaRouteIdToPresentationSessionMapping route_id_to_presentation_; 106 MediaRouteIdToPresentationSessionMapping route_id_to_presentation_;
107 base::SmallMap<std::map<std::string, MediaRoute::Id>> 107 base::SmallMap<std::map<std::string, MediaRoute::Id>>
108 presentation_id_to_route_id_; 108 presentation_id_to_route_id_;
109 scoped_ptr<PresentationMediaSinksObserver> sinks_observer_; 109 scoped_ptr<PresentationMediaSinksObserver> sinks_observer_;
110 scoped_ptr<PresentationSessionStateObserver> session_state_observer_; 110 scoped_ptr<PresentationSessionStateObserver> session_state_observer_;
111 ScopedVector<PresentationSessionMessagesObserver> session_messages_observers_; 111 ScopedVector<PresentationSessionMessagesObserver> session_messages_observers_;
112 112
113 // Default presentation request for this frame, if any.
114 scoped_ptr<PresentationRequest> default_presentation_request_;
115
116 // Callback to invoke when default presentation has started.
117 content::PresentationSessionStartedCallback
118 default_presentation_started_callback_;
119
113 // References to the owning WebContents, and the corresponding MediaRouter. 120 // References to the owning WebContents, and the corresponding MediaRouter.
114 const content::WebContents* web_contents_; 121 const content::WebContents* web_contents_;
115 MediaRouter* router_; 122 MediaRouter* router_;
116 123
117 DelegateObserver* delegate_observer_; 124 DelegateObserver* delegate_observer_;
118 }; 125 };
119 126
120 PresentationFrame::PresentationFrame(content::WebContents* web_contents, 127 PresentationFrame::PresentationFrame(content::WebContents* web_contents,
121 MediaRouter* router) 128 MediaRouter* router)
122 : web_contents_(web_contents), 129 : web_contents_(web_contents),
123 router_(router), 130 router_(router),
124 delegate_observer_(nullptr) { 131 delegate_observer_(nullptr) {
125 DCHECK(web_contents_); 132 DCHECK(web_contents_);
126 DCHECK(router_); 133 DCHECK(router_);
127 } 134 }
128 135
129 PresentationFrame::~PresentationFrame() { 136 PresentationFrame::~PresentationFrame() {
130 } 137 }
131 138
132 void PresentationFrame::OnPresentationServiceDelegateDestroyed() const { 139 void PresentationFrame::OnPresentationServiceDelegateDestroyed() const {
133 if (delegate_observer_) 140 if (delegate_observer_)
134 delegate_observer_->OnDelegateDestroyed(); 141 delegate_observer_->OnDelegateDestroyed();
135 } 142 }
136 143
137 void PresentationFrame::OnPresentationSessionStarted( 144 void PresentationFrame::OnPresentationSessionStarted(
138 bool is_default_presentation,
139 const content::PresentationSessionInfo& session, 145 const content::PresentationSessionInfo& session,
140 const MediaRoute::Id& route_id) { 146 const MediaRoute::Id& route_id) {
141 presentation_id_to_route_id_[session.presentation_id] = route_id; 147 presentation_id_to_route_id_[session.presentation_id] = route_id;
142 route_id_to_presentation_.Add(route_id, session); 148 route_id_to_presentation_.Add(route_id, session);
143 if (session_state_observer_) 149 if (session_state_observer_)
144 session_state_observer_->OnPresentationSessionConnected(route_id); 150 session_state_observer_->OnPresentationSessionConnected(route_id);
145 if (is_default_presentation && delegate_observer_) 151 }
146 delegate_observer_->OnDefaultPresentationStarted(session); 152
153 void PresentationFrame::OnDefaultPresentationSessionStarted(
mark a. foltz 2015/10/20 20:15:25 Is this called only for browser initiated requests
imcheng 2015/10/24 00:41:19 Only for browser initiated requests.
154 const PresentationRequest& request,
155 const content::PresentationSessionInfo& session,
156 const MediaRoute::Id& route_id) {
157 OnPresentationSessionStarted(session, route_id);
158 if (default_presentation_request_ &&
159 default_presentation_request_->Equals(request)) {
160 default_presentation_started_callback_.Run(session);
161 }
147 } 162 }
148 163
149 void PresentationFrame::OnPresentationSessionClosed( 164 void PresentationFrame::OnPresentationSessionClosed(
150 const std::string& presentation_id) { 165 const std::string& presentation_id) {
151 auto it = presentation_id_to_route_id_.find(presentation_id); 166 auto it = presentation_id_to_route_id_.find(presentation_id);
152 if (it != presentation_id_to_route_id_.end()) { 167 if (it != presentation_id_to_route_id_.end()) {
153 route_id_to_presentation_.Remove(it->second); 168 route_id_to_presentation_.Remove(it->second);
154 presentation_id_to_route_id_.erase(it); 169 presentation_id_to_route_id_.erase(it);
155 } 170 }
156 // TODO(imcheng): Notify |session_state_observer_|? 171 // TODO(imcheng): Notify |session_state_observer_|?
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 route_id_to_presentation_.Clear(); 220 route_id_to_presentation_.Clear();
206 221
207 for (const auto& pid_route_id : presentation_id_to_route_id_) 222 for (const auto& pid_route_id : presentation_id_to_route_id_)
208 router_->OnPresentationSessionDetached(pid_route_id.second); 223 router_->OnPresentationSessionDetached(pid_route_id.second);
209 224
210 presentation_id_to_route_id_.clear(); 225 presentation_id_to_route_id_.clear();
211 sinks_observer_.reset(); 226 sinks_observer_.reset();
212 if (session_state_observer_) 227 if (session_state_observer_)
213 session_state_observer_->Reset(); 228 session_state_observer_->Reset();
214 session_messages_observers_.clear(); 229 session_messages_observers_.clear();
230
mark a. foltz 2015/10/20 20:15:25 Extra newline
imcheng 2015/10/24 00:41:19 Ok. But added a new line after L228 for the if blo
231 ClearDefaultPresentationRequest();
215 } 232 }
216 233
217 void PresentationFrame::ListenForSessionStateChange( 234 void PresentationFrame::ListenForSessionStateChange(
218 const content::SessionStateChangedCallback& state_changed_cb) { 235 const content::SessionStateChangedCallback& state_changed_cb) {
219 CHECK(!session_state_observer_.get()); 236 CHECK(!session_state_observer_.get());
220 session_state_observer_.reset(new PresentationSessionStateObserver( 237 session_state_observer_.reset(new PresentationSessionStateObserver(
221 state_changed_cb, &route_id_to_presentation_, router_)); 238 state_changed_cb, &route_id_to_presentation_, router_));
222 } 239 }
223 240
224 void PresentationFrame::ListenForSessionMessages( 241 void PresentationFrame::ListenForSessionMessages(
225 const content::PresentationSessionInfo& session, 242 const content::PresentationSessionInfo& session,
226 const content::PresentationSessionMessageCallback& message_cb) { 243 const content::PresentationSessionMessageCallback& message_cb) {
227 auto it = presentation_id_to_route_id_.find(session.presentation_id); 244 auto it = presentation_id_to_route_id_.find(session.presentation_id);
228 if (it == presentation_id_to_route_id_.end()) { 245 if (it == presentation_id_to_route_id_.end()) {
229 DVLOG(2) << "ListenForSessionMessages: no route for " 246 DVLOG(2) << "ListenForSessionMessages: no route for "
230 << session.presentation_id; 247 << session.presentation_id;
231 return; 248 return;
232 } 249 }
233 250
234 session_messages_observers_.push_back( 251 session_messages_observers_.push_back(
235 new PresentationSessionMessagesObserver(message_cb, it->second, router_)); 252 new PresentationSessionMessagesObserver(message_cb, it->second, router_));
236 } 253 }
237 254
255 void PresentationFrame::SetDefaultPresentationRequest(
256 const PresentationRequest& default_presentation_request,
257 const content::PresentationSessionStartedCallback& callback) {
258 default_presentation_started_callback_ = callback;
mark a. foltz 2015/10/20 20:15:25 Do we need to reset default_presentation_started_c
imcheng 2015/10/24 00:41:19 The Equals check below is just an optimization. Th
259 if (!default_presentation_request_ ||
260 !default_presentation_request_->Equals(default_presentation_request)) {
261 default_presentation_request_.reset(
262 new PresentationRequest(default_presentation_request));
263 }
264 }
265
266 void PresentationFrame::ClearDefaultPresentationRequest() {
267 default_presentation_request_.reset();
268 default_presentation_started_callback_.Reset();
269 }
270
238 MediaSource PresentationFrame::GetMediaSourceFromListener( 271 MediaSource PresentationFrame::GetMediaSourceFromListener(
239 content::PresentationScreenAvailabilityListener* listener) const { 272 content::PresentationScreenAvailabilityListener* listener) const {
240 // If the default presentation URL is empty then fall back to tab mirroring. 273 // If the default presentation URL is empty then fall back to tab mirroring.
241 std::string availability_url(listener->GetAvailabilityUrl()); 274 std::string availability_url(listener->GetAvailabilityUrl());
242 return availability_url.empty() 275 return availability_url.empty()
243 ? MediaSourceForTab(SessionTabHelper::IdForTab(web_contents_)) 276 ? MediaSourceForTab(SessionTabHelper::IdForTab(web_contents_))
244 : MediaSourceForPresentationUrl(availability_url); 277 : MediaSourceForPresentationUrl(availability_url);
245 } 278 }
246 279
247 // Used by PresentationServiceDelegateImpl to manage PresentationFrames. 280 // Used by PresentationServiceDelegateImpl to manage PresentationFrames.
(...skipping 10 matching lines...) Expand all
258 bool RemoveScreenAvailabilityListener( 291 bool RemoveScreenAvailabilityListener(
259 const RenderFrameHostId& render_frame_host_id, 292 const RenderFrameHostId& render_frame_host_id,
260 content::PresentationScreenAvailabilityListener* listener); 293 content::PresentationScreenAvailabilityListener* listener);
261 void ListenForSessionStateChange( 294 void ListenForSessionStateChange(
262 const RenderFrameHostId& render_frame_host_id, 295 const RenderFrameHostId& render_frame_host_id,
263 const content::SessionStateChangedCallback& state_changed_cb); 296 const content::SessionStateChangedCallback& state_changed_cb);
264 void ListenForSessionMessages( 297 void ListenForSessionMessages(
265 const RenderFrameHostId& render_frame_host_id, 298 const RenderFrameHostId& render_frame_host_id,
266 const content::PresentationSessionInfo& session, 299 const content::PresentationSessionInfo& session,
267 const content::PresentationSessionMessageCallback& message_cb); 300 const content::PresentationSessionMessageCallback& message_cb);
301
302 // Sets or clears the default presentation request and callback for the given
303 // frame. Also sets / clears the default presentation requests for the owning
304 // tab WebContents.
305 void SetDefaultPresentationUrl(
306 const RenderFrameHostId& render_frame_host_id,
307 const std::string& default_presentation_url,
308 const content::PresentationSessionStartedCallback& callback);
268 void AddDelegateObserver(const RenderFrameHostId& render_frame_host_id, 309 void AddDelegateObserver(const RenderFrameHostId& render_frame_host_id,
269 DelegateObserver* observer); 310 DelegateObserver* observer);
270 void RemoveDelegateObserver(const RenderFrameHostId& render_frame_host_id); 311 void RemoveDelegateObserver(const RenderFrameHostId& render_frame_host_id);
312 void AddDefaultPresentationRequestObserver(
313 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver*
314 observer);
315 void RemoveDefaultPresentationRequestObserver(
316 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver*
317 observer);
271 void Reset(const RenderFrameHostId& render_frame_host_id); 318 void Reset(const RenderFrameHostId& render_frame_host_id);
272 bool HasScreenAvailabilityListenerForTest( 319 bool HasScreenAvailabilityListenerForTest(
273 const RenderFrameHostId& render_frame_host_id, 320 const RenderFrameHostId& render_frame_host_id,
274 const MediaSource::Id& source_id) const; 321 const MediaSource::Id& source_id) const;
275 void SetMediaRouterForTest(MediaRouter* router); 322 void SetMediaRouterForTest(MediaRouter* router);
276 323
277 void OnPresentationSessionStarted( 324 void OnPresentationSessionStarted(
278 const RenderFrameHostId& render_frame_host_id, 325 const RenderFrameHostId& render_frame_host_id,
279 bool is_default_presentation, 326 const content::PresentationSessionInfo& session,
327 const MediaRoute::Id& route_id);
328 void OnDefaultPresentationSessionStarted(
329 const PresentationRequest& request,
280 const content::PresentationSessionInfo& session, 330 const content::PresentationSessionInfo& session,
281 const MediaRoute::Id& route_id); 331 const MediaRoute::Id& route_id);
282 332
283 void OnPresentationSessionClosed( 333 void OnPresentationSessionClosed(
284 const RenderFrameHostId& render_frame_host_id, 334 const RenderFrameHostId& render_frame_host_id,
285 const std::string& presentation_id); 335 const std::string& presentation_id);
286 const MediaRoute::Id GetRouteId(const RenderFrameHostId& render_frame_host_id, 336 const MediaRoute::Id GetRouteId(const RenderFrameHostId& render_frame_host_id,
287 const std::string& presentation_id) const; 337 const std::string& presentation_id) const;
288 const std::vector<MediaRoute::Id> GetRouteIds( 338 const std::vector<MediaRoute::Id> GetRouteIds(
289 const RenderFrameHostId& render_frame_host_id) const; 339 const RenderFrameHostId& render_frame_host_id) const;
290 340
341 const PresentationRequest* default_presentation_request() const {
mark a. foltz 2015/10/20 20:15:25 Which request is this returning? Isn't there pote
imcheng 2015/10/24 00:41:19 This returns the "tab-level" default presentation
342 return default_presentation_request_.get();
343 }
344
291 private: 345 private:
292 PresentationFrame* GetOrAddPresentationFrame( 346 PresentationFrame* GetOrAddPresentationFrame(
293 const RenderFrameHostId& render_frame_host_id); 347 const RenderFrameHostId& render_frame_host_id);
294 348
349 // Sets the default presentation request for the owning WebContents and
350 // notifies observers of changes.
351 void SetDefaultPresentationRequest(
352 const PresentationRequest& default_presentation_request);
353
354 // Clears the default presentation request for the owning WebContents and
355 // notifies observers of changes.
356 void ClearDefaultPresentationRequest();
357
295 // Maps a frame identifier to a PresentationFrame object for frames 358 // Maps a frame identifier to a PresentationFrame object for frames
296 // that are using presentation API. 359 // that are using presentation API.
297 base::ScopedPtrHashMap<RenderFrameHostId, scoped_ptr<PresentationFrame>> 360 base::ScopedPtrHashMap<RenderFrameHostId, scoped_ptr<PresentationFrame>>
298 presentation_frames_; 361 presentation_frames_;
299 362
363 // Default presentation request for the owning tab WebContents.
364 scoped_ptr<PresentationRequest> default_presentation_request_;
365
366 // References to the observers listening for changes to this tab WebContent's
367 // default presentation.
368 base::ObserverList<
369 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver>
370 default_presentation_request_observers_;
371
300 // References to the owning WebContents, and the corresponding MediaRouter. 372 // References to the owning WebContents, and the corresponding MediaRouter.
301 MediaRouter* router_; 373 MediaRouter* router_;
302 content::WebContents* web_contents_; 374 content::WebContents* web_contents_;
303 }; 375 };
304 376
305 PresentationFrameManager::PresentationFrameManager( 377 PresentationFrameManager::PresentationFrameManager(
306 content::WebContents* web_contents, 378 content::WebContents* web_contents,
307 MediaRouter* router) 379 MediaRouter* router)
308 : router_(router), web_contents_(web_contents) { 380 : router_(router), web_contents_(web_contents) {
309 DCHECK(web_contents_); 381 DCHECK(web_contents_);
310 DCHECK(router_); 382 DCHECK(router_);
311 } 383 }
312 384
313 PresentationFrameManager::~PresentationFrameManager() { 385 PresentationFrameManager::~PresentationFrameManager() {
314 for (auto& frame : presentation_frames_) 386 for (auto& frame : presentation_frames_)
315 frame.second->OnPresentationServiceDelegateDestroyed(); 387 frame.second->OnPresentationServiceDelegateDestroyed();
316 } 388 }
317 389
318 void PresentationFrameManager::OnPresentationSessionStarted( 390 void PresentationFrameManager::OnPresentationSessionStarted(
319 const RenderFrameHostId& render_frame_host_id, 391 const RenderFrameHostId& render_frame_host_id,
320 bool is_default_presentation,
321 const content::PresentationSessionInfo& session, 392 const content::PresentationSessionInfo& session,
322 const MediaRoute::Id& route_id) { 393 const MediaRoute::Id& route_id) {
323 auto presentation_frame = presentation_frames_.get(render_frame_host_id); 394 auto presentation_frame = presentation_frames_.get(render_frame_host_id);
324 if (presentation_frame) 395 if (presentation_frame)
325 presentation_frame->OnPresentationSessionStarted(is_default_presentation, 396 presentation_frame->OnPresentationSessionStarted(session, route_id);
326 session, route_id); 397 }
398
399 void PresentationFrameManager::OnDefaultPresentationSessionStarted(
400 const PresentationRequest& request,
401 const content::PresentationSessionInfo& session,
402 const MediaRoute::Id& route_id) {
403 auto presentation_frame =
404 presentation_frames_.get(request.render_frame_host_id());
405 if (presentation_frame)
406 presentation_frame->OnDefaultPresentationSessionStarted(request, session,
407 route_id);
327 } 408 }
328 409
329 void PresentationFrameManager::OnPresentationSessionClosed( 410 void PresentationFrameManager::OnPresentationSessionClosed(
330 const RenderFrameHostId& render_frame_host_id, 411 const RenderFrameHostId& render_frame_host_id,
331 const std::string& presentation_id) { 412 const std::string& presentation_id) {
332 auto presentation_frame = presentation_frames_.get(render_frame_host_id); 413 auto presentation_frame = presentation_frames_.get(render_frame_host_id);
333 if (presentation_frame) 414 if (presentation_frame)
334 presentation_frame->OnPresentationSessionClosed(presentation_id); 415 presentation_frame->OnPresentationSessionClosed(presentation_id);
335 } 416 }
336 417
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 presentation_frames_.get(render_frame_host_id); 471 presentation_frames_.get(render_frame_host_id);
391 if (!presentation_frame) { 472 if (!presentation_frame) {
392 DVLOG(2) << "ListenForSessionMessages: PresentationFrame does not exist " 473 DVLOG(2) << "ListenForSessionMessages: PresentationFrame does not exist "
393 << "for: (" << render_frame_host_id.first << ", " 474 << "for: (" << render_frame_host_id.first << ", "
394 << render_frame_host_id.second << ")"; 475 << render_frame_host_id.second << ")";
395 return; 476 return;
396 } 477 }
397 presentation_frame->ListenForSessionMessages(session, message_cb); 478 presentation_frame->ListenForSessionMessages(session, message_cb);
398 } 479 }
399 480
481 void PresentationFrameManager::SetDefaultPresentationUrl(
482 const RenderFrameHostId& render_frame_host_id,
483 const std::string& default_presentation_url,
484 const content::PresentationSessionStartedCallback& callback) {
485 PresentationFrame* presentation_frame =
486 GetOrAddPresentationFrame(render_frame_host_id);
487 if (default_presentation_url.empty()) {
488 ClearDefaultPresentationRequest();
mark a. foltz 2015/10/20 20:15:25 This design keeps track of default requests at two
imcheng 2015/10/24 00:41:19 This patch fixes the issue where the default reque
489 presentation_frame->ClearDefaultPresentationRequest();
490 } else {
491 DCHECK(!callback.is_null());
492
493 GURL frame_url(GetLastCommittedURLForFrame(render_frame_host_id));
494 PresentationRequest request(render_frame_host_id, default_presentation_url,
495 frame_url);
496 presentation_frame->SetDefaultPresentationRequest(request, callback);
497 SetDefaultPresentationRequest(request);
498 }
499 }
500
400 void PresentationFrameManager::AddDelegateObserver( 501 void PresentationFrameManager::AddDelegateObserver(
401 const RenderFrameHostId& render_frame_host_id, 502 const RenderFrameHostId& render_frame_host_id,
402 DelegateObserver* observer) { 503 DelegateObserver* observer) {
403 auto presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); 504 auto presentation_frame = GetOrAddPresentationFrame(render_frame_host_id);
404 presentation_frame->set_delegate_observer(observer); 505 presentation_frame->set_delegate_observer(observer);
405 } 506 }
406 507
407 void PresentationFrameManager::RemoveDelegateObserver( 508 void PresentationFrameManager::RemoveDelegateObserver(
408 const RenderFrameHostId& render_frame_host_id) { 509 const RenderFrameHostId& render_frame_host_id) {
409 auto presentation_frame = presentation_frames_.get(render_frame_host_id); 510 auto presentation_frame = presentation_frames_.get(render_frame_host_id);
410 if (presentation_frame) { 511 if (presentation_frame) {
411 presentation_frame->set_delegate_observer(nullptr); 512 presentation_frame->set_delegate_observer(nullptr);
412 presentation_frames_.erase(render_frame_host_id); 513 presentation_frames_.erase(render_frame_host_id);
413 } 514 }
414 } 515 }
415 516
517 void PresentationFrameManager::AddDefaultPresentationRequestObserver(
518 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver*
519 observer) {
520 default_presentation_request_observers_.AddObserver(observer);
521 }
522
523 void PresentationFrameManager::RemoveDefaultPresentationRequestObserver(
524 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver*
525 observer) {
526 default_presentation_request_observers_.RemoveObserver(observer);
527 }
528
416 void PresentationFrameManager::Reset( 529 void PresentationFrameManager::Reset(
417 const RenderFrameHostId& render_frame_host_id) { 530 const RenderFrameHostId& render_frame_host_id) {
418 auto presentation_frame = presentation_frames_.get(render_frame_host_id); 531 auto presentation_frame = presentation_frames_.get(render_frame_host_id);
419 if (presentation_frame) 532 if (presentation_frame)
420 presentation_frame->Reset(); 533 presentation_frame->Reset();
534
535 if (default_presentation_request_ &&
536 render_frame_host_id ==
537 default_presentation_request_->render_frame_host_id()) {
538 ClearDefaultPresentationRequest();
539 }
421 } 540 }
422 541
423 PresentationFrame* PresentationFrameManager::GetOrAddPresentationFrame( 542 PresentationFrame* PresentationFrameManager::GetOrAddPresentationFrame(
424 const RenderFrameHostId& render_frame_host_id) { 543 const RenderFrameHostId& render_frame_host_id) {
425 if (!presentation_frames_.contains(render_frame_host_id)) { 544 if (!presentation_frames_.contains(render_frame_host_id)) {
426 presentation_frames_.add( 545 presentation_frames_.add(
427 render_frame_host_id, 546 render_frame_host_id,
428 scoped_ptr<PresentationFrame>( 547 scoped_ptr<PresentationFrame>(
429 new PresentationFrame(web_contents_, router_))); 548 new PresentationFrame(web_contents_, router_)));
430 } 549 }
431 return presentation_frames_.get(render_frame_host_id); 550 return presentation_frames_.get(render_frame_host_id);
432 } 551 }
433 552
553 void PresentationFrameManager::ClearDefaultPresentationRequest() {
554 if (!default_presentation_request_)
555 return;
556
557 default_presentation_request_.reset();
558 FOR_EACH_OBSERVER(
559 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver,
560 default_presentation_request_observers_,
561 OnDefaultPresentationChanged(nullptr));
562 }
563
564 void PresentationFrameManager::SetDefaultPresentationRequest(
565 const PresentationRequest& default_presentation_request) {
566 if (default_presentation_request_ &&
567 default_presentation_request_->Equals(default_presentation_request))
568 return;
569
570 default_presentation_request_.reset(
571 new PresentationRequest(default_presentation_request));
572 FOR_EACH_OBSERVER(
573 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver,
574 default_presentation_request_observers_,
575 OnDefaultPresentationChanged(default_presentation_request_.get()));
576 }
577
434 void PresentationFrameManager::SetMediaRouterForTest(MediaRouter* router) { 578 void PresentationFrameManager::SetMediaRouterForTest(MediaRouter* router) {
435 router_ = router; 579 router_ = router;
436 } 580 }
437 581
438 PresentationServiceDelegateImpl* 582 PresentationServiceDelegateImpl*
439 PresentationServiceDelegateImpl::GetOrCreateForWebContents( 583 PresentationServiceDelegateImpl::GetOrCreateForWebContents(
440 content::WebContents* web_contents) { 584 content::WebContents* web_contents) {
441 DCHECK(web_contents); 585 DCHECK(web_contents);
442 // CreateForWebContents does nothing if the delegate instance already exists. 586 // CreateForWebContents does nothing if the delegate instance already exists.
443 PresentationServiceDelegateImpl::CreateForWebContents(web_contents); 587 PresentationServiceDelegateImpl::CreateForWebContents(web_contents);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 content::PresentationScreenAvailabilityListener* listener) { 631 content::PresentationScreenAvailabilityListener* listener) {
488 DCHECK(listener); 632 DCHECK(listener);
489 frame_manager_->RemoveScreenAvailabilityListener( 633 frame_manager_->RemoveScreenAvailabilityListener(
490 RenderFrameHostId(render_process_id, render_frame_id), listener); 634 RenderFrameHostId(render_process_id, render_frame_id), listener);
491 } 635 }
492 636
493 void PresentationServiceDelegateImpl::Reset(int render_process_id, 637 void PresentationServiceDelegateImpl::Reset(int render_process_id,
494 int render_frame_id) { 638 int render_frame_id) {
495 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 639 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
496 frame_manager_->Reset(render_frame_host_id); 640 frame_manager_->Reset(render_frame_host_id);
497 if (render_frame_host_id == default_presentation_render_frame_host_id_) {
498 UpdateDefaultMediaSourceAndNotifyObservers(RenderFrameHostId(),
499 MediaSource(), GURL());
500 }
501 } 641 }
502 642
503 void PresentationServiceDelegateImpl::SetDefaultPresentationUrl( 643 void PresentationServiceDelegateImpl::SetDefaultPresentationUrl(
504 int render_process_id, 644 int render_process_id,
505 int render_frame_id, 645 int render_frame_id,
506 const std::string& default_presentation_url) { 646 const std::string& default_presentation_url,
647 const content::PresentationSessionStartedCallback& callback) {
507 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 648 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
508 if (IsMainFrame(render_process_id, render_frame_id)) { 649 frame_manager_->SetDefaultPresentationUrl(render_frame_host_id,
509 // This is the main frame, which means tab-level default presentation 650 default_presentation_url, callback);
510 // might have been updated.
511 MediaSource default_source;
512 if (!default_presentation_url.empty())
513 default_source = MediaSourceForPresentationUrl(default_presentation_url);
514
515 GURL default_frame_url = GetLastCommittedURLForFrame(render_frame_host_id);
516 UpdateDefaultMediaSourceAndNotifyObservers(
517 render_frame_host_id, default_source, default_frame_url);
518 }
519 }
520
521 bool PresentationServiceDelegateImpl::IsMainFrame(int render_process_id,
522 int render_frame_id) const {
523 RenderFrameHost* main_frame = web_contents_->GetMainFrame();
524 return main_frame &&
525 GetRenderFrameHostId(main_frame) ==
526 RenderFrameHostId(render_process_id, render_frame_id);
527 }
528
529 void PresentationServiceDelegateImpl::
530 UpdateDefaultMediaSourceAndNotifyObservers(
531 const RenderFrameHostId& render_frame_host_id,
532 const MediaSource& new_default_source,
533 const GURL& new_default_frame_url) {
534 default_presentation_render_frame_host_id_ = render_frame_host_id;
535 if (!new_default_source.Equals(default_source_) ||
536 new_default_frame_url != default_frame_url_) {
537 default_source_ = new_default_source;
538 default_frame_url_ = new_default_frame_url;
539 FOR_EACH_OBSERVER(
540 DefaultMediaSourceObserver, default_media_source_observers_,
541 OnDefaultMediaSourceChanged(default_source_, default_frame_url_));
542 }
543 } 651 }
544 652
545 void PresentationServiceDelegateImpl::OnJoinRouteResponse( 653 void PresentationServiceDelegateImpl::OnJoinRouteResponse(
546 int render_process_id, 654 int render_process_id,
547 int render_frame_id, 655 int render_frame_id,
548 const content::PresentationSessionInfo& session, 656 const content::PresentationSessionInfo& session,
549 const PresentationSessionSuccessCallback& success_cb, 657 const content::PresentationSessionStartedCallback& success_cb,
550 const PresentationSessionErrorCallback& error_cb, 658 const content::PresentationSessionErrorCallback& error_cb,
551 const MediaRoute* route, 659 const MediaRoute* route,
552 const std::string& presentation_id, 660 const std::string& presentation_id,
553 const std::string& error_text) { 661 const std::string& error_text) {
554 if (!route) { 662 if (!route) {
555 error_cb.Run(content::PresentationError( 663 error_cb.Run(content::PresentationError(
556 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, error_text)); 664 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, error_text));
557 } else { 665 } else {
558 DVLOG(1) << "OnJoinRouteResponse: " 666 DVLOG(1) << "OnJoinRouteResponse: "
559 << "route_id: " << route->media_route_id() 667 << "route_id: " << route->media_route_id()
560 << ", presentation URL: " << session.presentation_url 668 << ", presentation URL: " << session.presentation_url
561 << ", presentation ID: " << session.presentation_id; 669 << ", presentation ID: " << session.presentation_id;
562 DCHECK_EQ(session.presentation_id, presentation_id); 670 DCHECK_EQ(session.presentation_id, presentation_id);
563 frame_manager_->OnPresentationSessionStarted( 671 frame_manager_->OnPresentationSessionStarted(
564 RenderFrameHostId(render_process_id, render_frame_id), false, session, 672 RenderFrameHostId(render_process_id, render_frame_id), session,
565 route->media_route_id()); 673 route->media_route_id());
566 success_cb.Run(session); 674 success_cb.Run(session);
567 } 675 }
568 } 676 }
569 677
570 void PresentationServiceDelegateImpl::OnStartSessionSucceeded( 678 void PresentationServiceDelegateImpl::OnStartSessionSucceeded(
571 int render_process_id, 679 int render_process_id,
572 int render_frame_id, 680 int render_frame_id,
573 const PresentationSessionSuccessCallback& success_cb, 681 const content::PresentationSessionStartedCallback& success_cb,
574 const content::PresentationSessionInfo& new_session, 682 const content::PresentationSessionInfo& new_session,
575 const MediaRoute::Id& route_id) { 683 const MediaRoute::Id& route_id) {
576 DVLOG(1) << "OnStartSessionSucceeded: " 684 DVLOG(1) << "OnStartSessionSucceeded: "
577 << "route_id: " << route_id 685 << "route_id: " << route_id
578 << ", presentation URL: " << new_session.presentation_url 686 << ", presentation URL: " << new_session.presentation_url
579 << ", presentation ID: " << new_session.presentation_id; 687 << ", presentation ID: " << new_session.presentation_id;
580 frame_manager_->OnPresentationSessionStarted( 688 frame_manager_->OnPresentationSessionStarted(
581 RenderFrameHostId(render_process_id, render_frame_id), false, new_session, 689 RenderFrameHostId(render_process_id, render_frame_id), new_session,
582 route_id); 690 route_id);
583 success_cb.Run(new_session); 691 success_cb.Run(new_session);
584 } 692 }
585 693
586 void PresentationServiceDelegateImpl::StartSession( 694 void PresentationServiceDelegateImpl::StartSession(
587 int render_process_id, 695 int render_process_id,
588 int render_frame_id, 696 int render_frame_id,
589 const std::string& presentation_url, 697 const std::string& presentation_url,
590 const PresentationSessionSuccessCallback& success_cb, 698 const content::PresentationSessionStartedCallback& success_cb,
591 const PresentationSessionErrorCallback& error_cb) { 699 const content::PresentationSessionErrorCallback& error_cb) {
592 if (presentation_url.empty() || !IsValidPresentationUrl(presentation_url)) { 700 if (presentation_url.empty() || !IsValidPresentationUrl(presentation_url)) {
593 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, 701 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN,
594 "Invalid presentation arguments.")); 702 "Invalid presentation arguments."));
595 return; 703 return;
596 } 704 }
705
597 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 706 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
598
599 scoped_ptr<CreatePresentationSessionRequest> context( 707 scoped_ptr<CreatePresentationSessionRequest> context(
600 new CreatePresentationSessionRequest( 708 new CreatePresentationSessionRequest(
601 presentation_url, GetLastCommittedURLForFrame(render_frame_host_id), 709 render_frame_host_id, presentation_url,
710 GetLastCommittedURLForFrame(render_frame_host_id),
602 base::Bind(&PresentationServiceDelegateImpl::OnStartSessionSucceeded, 711 base::Bind(&PresentationServiceDelegateImpl::OnStartSessionSucceeded,
603 weak_factory_.GetWeakPtr(), render_process_id, 712 weak_factory_.GetWeakPtr(), render_process_id,
604 render_frame_id, success_cb), 713 render_frame_id, success_cb),
605 error_cb)); 714 error_cb));
606 MediaRouterDialogController* controller = 715 MediaRouterDialogController* controller =
607 MediaRouterDialogController::GetOrCreateForWebContents(web_contents_); 716 MediaRouterDialogController::GetOrCreateForWebContents(web_contents_);
608 if (!controller->ShowMediaRouterDialogForPresentation(context.Pass())) { 717 if (!controller->ShowMediaRouterDialogForPresentation(context.Pass())) {
609 LOG(ERROR) << "Media router dialog already exists. Ignoring StartSession."; 718 LOG(ERROR) << "Media router dialog already exists. Ignoring StartSession.";
610 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, 719 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN,
611 "Unable to create dialog.")); 720 "Unable to create dialog."));
612 return; 721 return;
613 } 722 }
614 } 723 }
615 724
616 void PresentationServiceDelegateImpl::JoinSession( 725 void PresentationServiceDelegateImpl::JoinSession(
617 int render_process_id, 726 int render_process_id,
618 int render_frame_id, 727 int render_frame_id,
619 const std::string& presentation_url, 728 const std::string& presentation_url,
620 const std::string& presentation_id, 729 const std::string& presentation_id,
621 const PresentationSessionSuccessCallback& success_cb, 730 const content::PresentationSessionStartedCallback& success_cb,
622 const PresentationSessionErrorCallback& error_cb) { 731 const content::PresentationSessionErrorCallback& error_cb) {
623 std::vector<MediaRouteResponseCallback> route_response_callbacks; 732 std::vector<MediaRouteResponseCallback> route_response_callbacks;
624 route_response_callbacks.push_back(base::Bind( 733 route_response_callbacks.push_back(base::Bind(
625 &PresentationServiceDelegateImpl::OnJoinRouteResponse, 734 &PresentationServiceDelegateImpl::OnJoinRouteResponse,
626 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, 735 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id,
627 content::PresentationSessionInfo(presentation_url, presentation_id), 736 content::PresentationSessionInfo(presentation_url, presentation_id),
628 success_cb, error_cb)); 737 success_cb, error_cb));
629 router_->JoinRoute( 738 router_->JoinRoute(
630 MediaSourceForPresentationUrl(presentation_url).id(), presentation_id, 739 MediaSourceForPresentationUrl(presentation_url).id(), presentation_id,
631 GetLastCommittedURLForFrame( 740 GetLastCommittedURLForFrame(
632 RenderFrameHostId(render_process_id, render_frame_id)) 741 RenderFrameHostId(render_process_id, render_frame_id))
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
682 791
683 void PresentationServiceDelegateImpl::ListenForSessionStateChange( 792 void PresentationServiceDelegateImpl::ListenForSessionStateChange(
684 int render_process_id, 793 int render_process_id,
685 int render_frame_id, 794 int render_frame_id,
686 const content::SessionStateChangedCallback& state_changed_cb) { 795 const content::SessionStateChangedCallback& state_changed_cb) {
687 frame_manager_->ListenForSessionStateChange( 796 frame_manager_->ListenForSessionStateChange(
688 RenderFrameHostId(render_process_id, render_frame_id), state_changed_cb); 797 RenderFrameHostId(render_process_id, render_frame_id), state_changed_cb);
689 } 798 }
690 799
691 void PresentationServiceDelegateImpl::OnRouteResponse( 800 void PresentationServiceDelegateImpl::OnRouteResponse(
801 const PresentationRequest& presentation_request,
692 const MediaRoute* route, 802 const MediaRoute* route,
693 const std::string& presentation_id, 803 const std::string& presentation_id,
694 const std::string& error) { 804 const std::string& error) {
695 if (!route) 805 if (!route)
696 return; 806 return;
697 const MediaSource& source = route->media_source(); 807
698 DCHECK(!source.Empty()); 808 content::PresentationSessionInfo session_info(
699 if (!default_source_.Equals(source)) 809 presentation_request.presentation_url(), presentation_id);
700 return; 810 frame_manager_->OnDefaultPresentationSessionStarted(
701 RenderFrameHost* main_frame = web_contents_->GetMainFrame(); 811 presentation_request, session_info, route->media_route_id());
702 if (!main_frame)
703 return;
704 RenderFrameHostId render_frame_host_id(GetRenderFrameHostId(main_frame));
705 frame_manager_->OnPresentationSessionStarted(
706 render_frame_host_id, true,
707 content::PresentationSessionInfo(PresentationUrlFromMediaSource(source),
708 presentation_id),
709 route->media_route_id());
710 } 812 }
711 813
712 void PresentationServiceDelegateImpl::AddDefaultMediaSourceObserver( 814 void PresentationServiceDelegateImpl::AddDefaultPresentationRequestObserver(
713 DefaultMediaSourceObserver* observer) { 815 DefaultPresentationRequestObserver* observer) {
714 default_media_source_observers_.AddObserver(observer); 816 frame_manager_->AddDefaultPresentationRequestObserver(observer);
715 } 817 }
716 818
717 void PresentationServiceDelegateImpl::RemoveDefaultMediaSourceObserver( 819 void PresentationServiceDelegateImpl::RemoveDefaultPresentationRequestObserver(
718 DefaultMediaSourceObserver* observer) { 820 DefaultPresentationRequestObserver* observer) {
719 default_media_source_observers_.RemoveObserver(observer); 821 frame_manager_->RemoveDefaultPresentationRequestObserver(observer);
822 }
823
824 const PresentationRequest*
825 PresentationServiceDelegateImpl::GetDefaultPresentationRequest() const {
826 return frame_manager_->default_presentation_request();
720 } 827 }
721 828
722 base::WeakPtr<PresentationServiceDelegateImpl> 829 base::WeakPtr<PresentationServiceDelegateImpl>
723 PresentationServiceDelegateImpl::GetWeakPtr() { 830 PresentationServiceDelegateImpl::GetWeakPtr() {
724 return weak_factory_.GetWeakPtr(); 831 return weak_factory_.GetWeakPtr();
725 } 832 }
726 833
727 void PresentationServiceDelegateImpl::SetMediaRouterForTest( 834 void PresentationServiceDelegateImpl::SetMediaRouterForTest(
728 MediaRouter* router) { 835 MediaRouter* router) {
729 router_ = router; 836 router_ = router;
730 frame_manager_->SetMediaRouterForTest(router); 837 frame_manager_->SetMediaRouterForTest(router);
731 } 838 }
732 839
733 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( 840 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest(
734 int render_process_id, 841 int render_process_id,
735 int render_frame_id, 842 int render_frame_id,
736 const MediaSource::Id& source_id) const { 843 const MediaSource::Id& source_id) const {
737 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 844 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
738 return frame_manager_->HasScreenAvailabilityListenerForTest( 845 return frame_manager_->HasScreenAvailabilityListenerForTest(
739 render_frame_host_id, source_id); 846 render_frame_host_id, source_id);
740 } 847 }
741 848
742 } // namespace media_router 849 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698