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

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

Issue 1314413005: [Presentation API] 1-UA presentation support + presenter APIs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments #18-21 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/scoped_ptr_map.h" 10 #include "base/containers/scoped_ptr_map.h"
11 #include "base/containers/small_map.h" 11 #include "base/containers/small_map.h"
12 #include "base/guid.h"
13 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
15 #include "chrome/browser/media/router/create_presentation_session_request.h" 14 #include "chrome/browser/media/router/create_presentation_session_request.h"
16 #include "chrome/browser/media/router/media_route.h" 15 #include "chrome/browser/media/router/media_route.h"
17 #include "chrome/browser/media/router/media_router.h" 16 #include "chrome/browser/media/router/media_router.h"
18 #include "chrome/browser/media/router/media_router_dialog_controller.h" 17 #include "chrome/browser/media/router/media_router_dialog_controller.h"
19 #include "chrome/browser/media/router/media_router_factory.h" 18 #include "chrome/browser/media/router/media_router_factory.h"
20 #include "chrome/browser/media/router/media_sink.h" 19 #include "chrome/browser/media/router/media_sink.h"
21 #include "chrome/browser/media/router/media_source_helper.h" 20 #include "chrome/browser/media/router/media_source_helper.h"
21 #include "chrome/browser/media/router/offscreen_presentation_manager.h"
22 #include "chrome/browser/media/router/offscreen_presentation_manager_factory.h"
22 #include "chrome/browser/media/router/presentation_media_sinks_observer.h" 23 #include "chrome/browser/media/router/presentation_media_sinks_observer.h"
23 #include "chrome/browser/media/router/presentation_session_messages_observer.h" 24 #include "chrome/browser/media/router/presentation_session_messages_observer.h"
24 #include "chrome/browser/media/router/presentation_session_state_observer.h" 25 #include "chrome/browser/media/router/presentation_session_state_observer.h"
25 #include "chrome/browser/sessions/session_tab_helper.h" 26 #include "chrome/browser/sessions/session_tab_helper.h"
26 #include "content/public/browser/presentation_screen_availability_listener.h" 27 #include "content/public/browser/presentation_screen_availability_listener.h"
27 #include "content/public/browser/presentation_session.h" 28 #include "content/public/browser/presentation_session.h"
28 #include "content/public/browser/presentation_session_state_listener.h" 29 #include "content/public/browser/presentation_session_state_listener.h"
29 #include "content/public/browser/render_frame_host.h" 30 #include "content/public/browser/render_frame_host.h"
30 #include "content/public/browser/render_process_host.h" 31 #include "content/public/browser/render_process_host.h"
31 32
32 DEFINE_WEB_CONTENTS_USER_DATA_KEY( 33 DEFINE_WEB_CONTENTS_USER_DATA_KEY(
33 media_router::PresentationServiceDelegateImpl); 34 media_router::PresentationServiceDelegateImpl);
34 35
35 using content::RenderFrameHost; 36 using content::RenderFrameHost;
36 37
37 namespace media_router { 38 namespace media_router {
38 39
40 using OffscreenPresentationSession =
41 OffscreenPresentationManager::OffscreenPresentationSession;
42
39 namespace { 43 namespace {
40 44
41 using DelegateObserver = content::PresentationServiceDelegate::Observer; 45 using DelegateObserver = content::PresentationServiceDelegate::Observer;
42 using PresentationSessionErrorCallback = 46 using PresentationSessionErrorCallback =
43 content::PresentationServiceDelegate::PresentationSessionErrorCallback; 47 content::PresentationServiceDelegate::PresentationSessionErrorCallback;
44 using PresentationSessionSuccessCallback = 48 using PresentationSessionStartedCallback =
45 content::PresentationServiceDelegate::PresentationSessionSuccessCallback; 49 content::PresentationServiceDelegate::PresentationSessionStartedCallback;
46 50
47 // Returns the unique identifier for the supplied RenderFrameHost. 51 // Returns the unique identifier for the supplied RenderFrameHost.
48 RenderFrameHostId GetRenderFrameHostId(RenderFrameHost* render_frame_host) { 52 RenderFrameHostId GetRenderFrameHostId(RenderFrameHost* render_frame_host) {
49 int render_process_id = render_frame_host->GetProcess()->GetID(); 53 int render_process_id = render_frame_host->GetProcess()->GetID();
50 int render_frame_id = render_frame_host->GetRoutingID(); 54 int render_frame_id = render_frame_host->GetRoutingID();
51 return RenderFrameHostId(render_process_id, render_frame_id); 55 return RenderFrameHostId(render_process_id, render_frame_id);
52 } 56 }
53 57
54 // Gets the last committed URL for the render frame specified by 58 // Gets the last committed URL for the render frame specified by
55 // |render_frame_host_id|. 59 // |render_frame_host_id|.
56 GURL GetLastCommittedURLForFrame(RenderFrameHostId render_frame_host_id) { 60 GURL GetLastCommittedURLForFrame(RenderFrameHostId render_frame_host_id) {
57 RenderFrameHost* render_frame_host = RenderFrameHost::FromID( 61 RenderFrameHost* render_frame_host = RenderFrameHost::FromID(
58 render_frame_host_id.first, render_frame_host_id.second); 62 render_frame_host_id.first, render_frame_host_id.second);
59 DCHECK(render_frame_host); 63 DCHECK(render_frame_host);
60 return render_frame_host->GetLastCommittedURL(); 64 return render_frame_host->GetLastCommittedURL();
61 } 65 }
62 66
63 } // namespace 67 } // namespace
64 68
65 // Used by PresentationServiceDelegateImpl to manage 69 // Used by PresentationServiceDelegateImpl to manage
66 // listeners and default presentation info in a render frame. 70 // listeners and default presentation info in a render frame.
67 // Its lifetime: 71 // Its lifetime:
68 // * PresentationFrameManager AddDelegateObserver 72 // * PresentationFrameManager AddDelegateObserver
69 // * Reset 0+ times. 73 // * Reset 0+ times.
70 // * PresentationFrameManager.RemoveDelegateObserver. 74 // * PresentationFrameManager.RemoveDelegateObserver.
71 class PresentationFrame { 75 class PresentationFrame {
72 public: 76 public:
73 PresentationFrame(content::WebContents* web_contents, MediaRouter* router); 77 PresentationFrame(content::WebContents* web_contents,
78 const RenderFrameHostId& render_frame_host_id,
79 MediaRouter* router);
74 ~PresentationFrame(); 80 ~PresentationFrame();
75 81
76 // Mirror corresponding APIs in PresentationServiceDelegateImpl. 82 // Mirror corresponding APIs in PresentationServiceDelegateImpl.
77 bool SetScreenAvailabilityListener( 83 bool SetScreenAvailabilityListener(
78 content::PresentationScreenAvailabilityListener* listener); 84 content::PresentationScreenAvailabilityListener* listener);
79 bool RemoveScreenAvailabilityListener( 85 bool RemoveScreenAvailabilityListener(
80 content::PresentationScreenAvailabilityListener* listener); 86 content::PresentationScreenAvailabilityListener* listener);
81 bool HasScreenAvailabilityListenerForTest( 87 bool HasScreenAvailabilityListenerForTest(
82 const MediaSource::Id& source_id) const; 88 const MediaSource::Id& source_id) const;
83 std::string GetDefaultPresentationId() const; 89 std::string GetDefaultPresentationId() const;
90 void SendMessage(const content::PresentationSessionInfo& session,
91 scoped_ptr<content::PresentationSessionMessage> message,
92 const content::SendMessageCallback send_message_cb);
84 bool ListenForSessionStateChange( 93 bool ListenForSessionStateChange(
85 content::PresentationSessionStateListener* listener); 94 content::PresentationSessionStateListener* listener);
86 void ListenForSessionMessages( 95 void ListenForSessionMessages(
87 const content::PresentationSessionInfo& session, 96 const content::PresentationSessionInfo& session,
88 const content::PresentationSessionMessageCallback& message_cb); 97 const content::PresentationSessionMessageCallback& message_cb);
89 void Reset(); 98 void Reset();
90 99
91 const MediaRoute::Id GetRouteId(const std::string& presentation_id) const; 100 const MediaRoute::Id GetRouteId(const std::string& presentation_id) const;
92 const std::vector<MediaRoute::Id> GetRouteIds() const;
93 void OnPresentationSessionClosed(const std::string& presentation_id); 101 void OnPresentationSessionClosed(const std::string& presentation_id);
94 102
95 void OnPresentationSessionStarted( 103 void OnPresentationSessionStarted(
96 bool is_default_presentation, 104 bool is_default_presentation,
97 const content::PresentationSessionInfo& session, 105 const content::PresentationSessionInfo& session,
98 const MediaRoute::Id& route_id); 106 const MediaRoute& route);
99 void OnPresentationServiceDelegateDestroyed() const; 107 void OnPresentationServiceDelegateDestroyed() const;
100 108
101 void set_delegate_observer(DelegateObserver* observer) { 109 void set_delegate_observer(DelegateObserver* observer) {
102 delegate_observer_ = observer; 110 delegate_observer_ = observer;
103 } 111 }
104 112
105 private: 113 private:
106 MediaSource GetMediaSourceFromListener( 114 MediaSource GetMediaSourceFromListener(
107 content::PresentationScreenAvailabilityListener* listener) const; 115 content::PresentationScreenAvailabilityListener* listener) const;
116
117 // Gets the OffscreenPresentationSession associated with |presentation_id|
118 // within this frame. Returns nullptr if |presentation_id| is not
119 // registered as an offscreen presentation.
120 OffscreenPresentationSession* FindOffscreenSession(
121 const std::string& presentation_id) const;
122
108 base::SmallMap<std::map<std::string, MediaRoute::Id>> 123 base::SmallMap<std::map<std::string, MediaRoute::Id>>
109 presentation_id_to_route_id_; 124 presentation_id_to_route_id_;
110 scoped_ptr<PresentationMediaSinksObserver> sinks_observer_; 125 scoped_ptr<PresentationMediaSinksObserver> sinks_observer_;
111 126
127 // Controller objects for offscreen presentations. Note that offscreen
128 // presentations are manipulated with these objects instead of the observers
129 // and MediaRouter objects below. Maps from presentation ID to the
130 // corresponding OffscreenPresentationSession within this frame.
131 base::ScopedPtrMap<std::string, scoped_ptr<OffscreenPresentationSession>>
132 offscreen_presentation_sessions_;
133
134 // For non-offscreen presentations.
112 // Maps from presentation ID to the corresponding presentation session's 135 // Maps from presentation ID to the corresponding presentation session's
113 // state change observer within this frame. 136 // state change observer within this frame.
114 base::ScopedPtrMap<std::string, scoped_ptr<PresentationSessionStateObserver>> 137 base::ScopedPtrMap<std::string, scoped_ptr<PresentationSessionStateObserver>>
115 session_state_observers_; 138 session_state_observers_;
116 ScopedVector<PresentationSessionMessagesObserver> session_messages_observers_; 139 ScopedVector<PresentationSessionMessagesObserver> session_messages_observers_;
117 140
118 // References to the owning WebContents, and the corresponding MediaRouter. 141 // References to the owning WebContents.
119 const content::WebContents* web_contents_; 142 const content::WebContents* const web_contents_;
120 MediaRouter* router_; 143
144 // ID of owning RenderFrameHost.
145 const RenderFrameHostId render_frame_host_id_;
146
147 // References to MediaRouter / OffscreenPresentationManager owned by
148 // the associated BrowserContext. They (and the BrowserContext) are
149 // guaranteed to outlive this object.
150 MediaRouter* const router_;
151 OffscreenPresentationManager* const offscreen_presentation_manager_;
121 152
122 DelegateObserver* delegate_observer_; 153 DelegateObserver* delegate_observer_;
123 }; 154 };
124 155
125 PresentationFrame::PresentationFrame(content::WebContents* web_contents, 156 PresentationFrame::PresentationFrame(
126 MediaRouter* router) 157 content::WebContents* web_contents,
158 const RenderFrameHostId& render_frame_host_id,
159 MediaRouter* router)
127 : web_contents_(web_contents), 160 : web_contents_(web_contents),
161 render_frame_host_id_(render_frame_host_id),
128 router_(router), 162 router_(router),
163 offscreen_presentation_manager_(
164 OffscreenPresentationManagerFactory::GetOrCreateForBrowserContext(
165 web_contents_->GetBrowserContext())),
129 delegate_observer_(nullptr) { 166 delegate_observer_(nullptr) {
130 DCHECK(web_contents_); 167 DCHECK(web_contents_);
131 DCHECK(router_); 168 DCHECK(router_);
169 DCHECK(offscreen_presentation_manager_);
132 } 170 }
133 171
134 PresentationFrame::~PresentationFrame() { 172 PresentationFrame::~PresentationFrame() {
135 } 173 }
136 174
137 void PresentationFrame::OnPresentationServiceDelegateDestroyed() const { 175 void PresentationFrame::OnPresentationServiceDelegateDestroyed() const {
138 if (delegate_observer_) 176 if (delegate_observer_)
139 delegate_observer_->OnDelegateDestroyed(); 177 delegate_observer_->OnDelegateDestroyed();
140 } 178 }
141 179
142 void PresentationFrame::OnPresentationSessionStarted( 180 void PresentationFrame::OnPresentationSessionStarted(
143 bool is_default_presentation, 181 bool is_default_presentation,
144 const content::PresentationSessionInfo& session, 182 const content::PresentationSessionInfo& session,
145 const MediaRoute::Id& route_id) { 183 const MediaRoute& route) {
146 presentation_id_to_route_id_[session.presentation_id] = route_id; 184 const std::string& presentation_id = session.presentation_id;
185 presentation_id_to_route_id_[presentation_id] = route.media_route_id();
147 if (is_default_presentation && delegate_observer_) 186 if (is_default_presentation && delegate_observer_)
148 delegate_observer_->OnDefaultPresentationStarted(session); 187 delegate_observer_->OnDefaultPresentationStarted(session);
188 if (route.is_offscreen_presentation()) {
189 DCHECK(!ContainsKey(offscreen_presentation_sessions_, presentation_id));
190 scoped_ptr<OffscreenPresentationSession> offscreen_session =
191 offscreen_presentation_manager_->ConnectToOffscreenPresentation(
192 presentation_id, render_frame_host_id_);
193
194 // |offscreen_session| could be nullptr for a couple of reasons:
195 // 1) An OffscreenPresentationSession for presentation_id already exists.
196 // This could be due to a race between two or more route requests.
197 // 2) The receiver is already gone, so the route will die soon. This should
198 // happen very rarely since the receiver has to unregister itself between
199 // when offscreen presentation was created and when the resulting route
200 // arrived at MR.
201 if (!offscreen_session) {
202 // TODO(imcheng): we should probably reject the route request and detach
203 // the route in this case. crbug.com/513859
204 DLOG(ERROR) << "CreateOffscreenPresentationConnection returned nullptr.";
205 } else {
206 offscreen_presentation_sessions_.insert(presentation_id,
207 offscreen_session.Pass());
208 }
209 }
149 } 210 }
150 211
151 void PresentationFrame::OnPresentationSessionClosed( 212 void PresentationFrame::OnPresentationSessionClosed(
152 const std::string& presentation_id) { 213 const std::string& presentation_id) {
153 auto it = presentation_id_to_route_id_.find(presentation_id); 214 auto it = presentation_id_to_route_id_.find(presentation_id);
154 if (it != presentation_id_to_route_id_.end()) { 215 if (it != presentation_id_to_route_id_.end()) {
155 presentation_id_to_route_id_.erase(it); 216 presentation_id_to_route_id_.erase(it);
156 } 217 }
157 // TODO(imcheng): Notify PresentationSessionStateObserver? 218 // TODO(imcheng): Notify PresentationSessionStateObserver?
158 } 219 }
159 220
160 const MediaRoute::Id PresentationFrame::GetRouteId( 221 const MediaRoute::Id PresentationFrame::GetRouteId(
161 const std::string& presentation_id) const { 222 const std::string& presentation_id) const {
162 auto it = presentation_id_to_route_id_.find(presentation_id); 223 auto it = presentation_id_to_route_id_.find(presentation_id);
163 return it != presentation_id_to_route_id_.end() ? it->second : ""; 224 return it != presentation_id_to_route_id_.end() ? it->second : "";
164 } 225 }
165 226
166 const std::vector<MediaRoute::Id> PresentationFrame::GetRouteIds() const {
167 std::vector<MediaRoute::Id> route_ids;
168 for (const auto& e : presentation_id_to_route_id_)
169 route_ids.push_back(e.second);
170 return route_ids;
171 }
172
173 bool PresentationFrame::SetScreenAvailabilityListener( 227 bool PresentationFrame::SetScreenAvailabilityListener(
174 content::PresentationScreenAvailabilityListener* listener) { 228 content::PresentationScreenAvailabilityListener* listener) {
175 if (sinks_observer_ && sinks_observer_->listener() == listener) 229 if (sinks_observer_ && sinks_observer_->listener() == listener)
176 return false; 230 return false;
177 231
178 MediaSource source(GetMediaSourceFromListener(listener)); 232 MediaSource source(GetMediaSourceFromListener(listener));
179 sinks_observer_.reset( 233 sinks_observer_.reset(
180 new PresentationMediaSinksObserver(router_, listener, source)); 234 new PresentationMediaSinksObserver(router_, listener, source));
181 235
182 if (!sinks_observer_->is_active()) { 236 if (!sinks_observer_->is_active()) {
(...skipping 18 matching lines...) Expand all
201 const MediaSource::Id& source_id) const { 255 const MediaSource::Id& source_id) const {
202 return sinks_observer_ && sinks_observer_->source().id() == source_id; 256 return sinks_observer_ && sinks_observer_->source().id() == source_id;
203 } 257 }
204 258
205 void PresentationFrame::Reset() { 259 void PresentationFrame::Reset() {
206 for (const auto& pid_route_id : presentation_id_to_route_id_) 260 for (const auto& pid_route_id : presentation_id_to_route_id_)
207 router_->OnPresentationSessionDetached(pid_route_id.second); 261 router_->OnPresentationSessionDetached(pid_route_id.second);
208 262
209 presentation_id_to_route_id_.clear(); 263 presentation_id_to_route_id_.clear();
210 sinks_observer_.reset(); 264 sinks_observer_.reset();
265 offscreen_presentation_sessions_.clear();
211 session_state_observers_.clear(); 266 session_state_observers_.clear();
212 session_messages_observers_.clear(); 267 session_messages_observers_.clear();
213 } 268 }
214 269
270 void PresentationFrame::SendMessage(
271 const content::PresentationSessionInfo& session,
272 scoped_ptr<content::PresentationSessionMessage> message,
273 const content::SendMessageCallback send_message_cb) {
274 OffscreenPresentationSession* offscreen_session =
275 FindOffscreenSession(session.presentation_id);
276 if (offscreen_session) {
277 offscreen_session->SendMessage(message.Pass(), send_message_cb);
278 } else {
279 auto it = presentation_id_to_route_id_.find(session.presentation_id);
280 if (it == presentation_id_to_route_id_.end()) {
281 DVLOG(2) << "ListenForSessionMessages: no route for "
282 << session.presentation_id;
283 return;
284 }
285 if (message->is_binary()) {
286 router_->SendRouteBinaryMessage(it->second, message->data.Pass(),
287 send_message_cb);
288 } else {
289 router_->SendRouteMessage(it->second, message->message, send_message_cb);
290 }
291 }
292 }
293
294 OffscreenPresentationSession* PresentationFrame::FindOffscreenSession(
295 const std::string& presentation_id) const {
296 auto offscreen_session_it =
297 offscreen_presentation_sessions_.find(presentation_id);
298 return offscreen_session_it != offscreen_presentation_sessions_.end()
299 ? offscreen_session_it->second
300 : nullptr;
301 }
302
215 bool PresentationFrame::ListenForSessionStateChange( 303 bool PresentationFrame::ListenForSessionStateChange(
216 content::PresentationSessionStateListener* listener) { 304 content::PresentationSessionStateListener* listener) {
217 std::string presentation_id(listener->GetSessionInfo().presentation_id); 305 std::string presentation_id(listener->GetSessionInfo().presentation_id);
218 auto it = presentation_id_to_route_id_.find(presentation_id); 306 OffscreenPresentationSession* offscreen_session =
219 if (it == presentation_id_to_route_id_.end()) { 307 FindOffscreenSession(presentation_id);
220 DVLOG(2) << "ListenForSessionStateChange: no route for " << presentation_id; 308 if (offscreen_session) {
221 return false; 309 offscreen_session->ListenForStateChanges(listener);
310 return true;
222 } 311 }
223 312
224 if (ContainsKey(session_state_observers_, presentation_id)) { 313 if (ContainsKey(session_state_observers_, presentation_id)) {
225 DVLOG(2) << "ListenForSessionStateChange: already contains a state " 314 DVLOG(2) << "ListenForSessionStateChange: already contains a state "
226 << "observer for session " << presentation_id; 315 << "observer for session " << presentation_id;
227 return false; 316 return false;
228 } 317 }
229 318
319 auto it = presentation_id_to_route_id_.find(presentation_id);
320 if (it == presentation_id_to_route_id_.end()) {
321 DVLOG(2) << "ListenForSessionStateChange: no route for " << presentation_id;
322 return false;
323 }
324
230 session_state_observers_.insert( 325 session_state_observers_.insert(
231 presentation_id, make_scoped_ptr(new PresentationSessionStateObserver( 326 presentation_id, make_scoped_ptr(new PresentationSessionStateObserver(
232 listener, it->second, router_))); 327 listener, it->second, router_)));
233 return true; 328 return true;
234 } 329 }
235 330
236 void PresentationFrame::ListenForSessionMessages( 331 void PresentationFrame::ListenForSessionMessages(
237 const content::PresentationSessionInfo& session, 332 const content::PresentationSessionInfo& session,
238 const content::PresentationSessionMessageCallback& message_cb) { 333 const content::PresentationSessionMessageCallback& message_cb) {
239 auto it = presentation_id_to_route_id_.find(session.presentation_id); 334 OffscreenPresentationSession* offscreen_session =
240 if (it == presentation_id_to_route_id_.end()) { 335 FindOffscreenSession(session.presentation_id);
241 DVLOG(2) << "ListenForSessionMessages: no route for " 336 if (offscreen_session) {
242 << session.presentation_id; 337 offscreen_session->ListenForMessages(message_cb);
243 return; 338 } else {
339 auto it = presentation_id_to_route_id_.find(session.presentation_id);
340 if (it == presentation_id_to_route_id_.end()) {
341 DVLOG(2) << "ListenForSessionMessages: no route for "
342 << session.presentation_id;
343 return;
344 }
345 session_messages_observers_.push_back(
346 new PresentationSessionMessagesObserver(message_cb, it->second,
347 router_));
244 } 348 }
245
246 session_messages_observers_.push_back(
247 new PresentationSessionMessagesObserver(message_cb, it->second, router_));
248 } 349 }
249 350
250 MediaSource PresentationFrame::GetMediaSourceFromListener( 351 MediaSource PresentationFrame::GetMediaSourceFromListener(
251 content::PresentationScreenAvailabilityListener* listener) const { 352 content::PresentationScreenAvailabilityListener* listener) const {
252 // If the default presentation URL is empty then fall back to tab mirroring. 353 // If the default presentation URL is empty then fall back to tab mirroring.
253 std::string availability_url(listener->GetAvailabilityUrl()); 354 std::string availability_url(listener->GetAvailabilityUrl());
254 return availability_url.empty() 355 return availability_url.empty()
255 ? MediaSourceForTab(SessionTabHelper::IdForTab(web_contents_)) 356 ? MediaSourceForTab(SessionTabHelper::IdForTab(web_contents_))
256 : MediaSourceForPresentationUrl(availability_url); 357 : MediaSourceForPresentationUrl(availability_url);
257 } 358 }
258 359
259 // Used by PresentationServiceDelegateImpl to manage PresentationFrames. 360 // Used by PresentationServiceDelegateImpl to manage PresentationFrames.
260 class PresentationFrameManager { 361 class PresentationFrameManager {
261 public: 362 public:
262 PresentationFrameManager(content::WebContents* web_contents, 363 PresentationFrameManager(content::WebContents* web_contents,
263 MediaRouter* router); 364 MediaRouter* router);
264 ~PresentationFrameManager(); 365 ~PresentationFrameManager();
265 366
266 // Mirror corresponding APIs in PresentationServiceDelegateImpl. 367 // Mirror corresponding APIs in PresentationServiceDelegateImpl.
267 bool SetScreenAvailabilityListener( 368 bool SetScreenAvailabilityListener(
268 const RenderFrameHostId& render_frame_host_id, 369 const RenderFrameHostId& render_frame_host_id,
269 content::PresentationScreenAvailabilityListener* listener); 370 content::PresentationScreenAvailabilityListener* listener);
270 bool RemoveScreenAvailabilityListener( 371 bool RemoveScreenAvailabilityListener(
271 const RenderFrameHostId& render_frame_host_id, 372 const RenderFrameHostId& render_frame_host_id,
272 content::PresentationScreenAvailabilityListener* listener); 373 content::PresentationScreenAvailabilityListener* listener);
374 void SendMessage(const RenderFrameHostId& render_frame_host_id,
375 const content::PresentationSessionInfo& session,
376 scoped_ptr<content::PresentationSessionMessage> message,
377 const content::SendMessageCallback send_message_cb);
273 bool ListenForSessionStateChange( 378 bool ListenForSessionStateChange(
274 const RenderFrameHostId& render_frame_host_id, 379 const RenderFrameHostId& render_frame_host_id,
275 content::PresentationSessionStateListener* listener); 380 content::PresentationSessionStateListener* listener);
276 void ListenForSessionMessages( 381 void ListenForSessionMessages(
277 const RenderFrameHostId& render_frame_host_id, 382 const RenderFrameHostId& render_frame_host_id,
278 const content::PresentationSessionInfo& session, 383 const content::PresentationSessionInfo& session,
279 const content::PresentationSessionMessageCallback& message_cb); 384 const content::PresentationSessionMessageCallback& message_cb);
280 void AddDelegateObserver(const RenderFrameHostId& render_frame_host_id, 385 void AddDelegateObserver(const RenderFrameHostId& render_frame_host_id,
281 DelegateObserver* observer); 386 DelegateObserver* observer);
282 void RemoveDelegateObserver(const RenderFrameHostId& render_frame_host_id); 387 void RemoveDelegateObserver(const RenderFrameHostId& render_frame_host_id);
283 void Reset(const RenderFrameHostId& render_frame_host_id); 388 void Reset(const RenderFrameHostId& render_frame_host_id);
284 bool HasScreenAvailabilityListenerForTest( 389 bool HasScreenAvailabilityListenerForTest(
285 const RenderFrameHostId& render_frame_host_id, 390 const RenderFrameHostId& render_frame_host_id,
286 const MediaSource::Id& source_id) const; 391 const MediaSource::Id& source_id) const;
287 void SetMediaRouterForTest(MediaRouter* router); 392 void SetMediaRouterForTest(MediaRouter* router);
288 393
289 void OnPresentationSessionStarted( 394 void OnPresentationSessionStarted(
290 const RenderFrameHostId& render_frame_host_id, 395 const RenderFrameHostId& render_frame_host_id,
291 bool is_default_presentation, 396 bool is_default_presentation,
292 const content::PresentationSessionInfo& session, 397 const content::PresentationSessionInfo& session,
293 const MediaRoute::Id& route_id); 398 const MediaRoute& route);
294 399
295 void OnPresentationSessionClosed( 400 void OnPresentationSessionClosed(
296 const RenderFrameHostId& render_frame_host_id, 401 const RenderFrameHostId& render_frame_host_id,
297 const std::string& presentation_id); 402 const std::string& presentation_id);
298 const MediaRoute::Id GetRouteId(const RenderFrameHostId& render_frame_host_id, 403 const MediaRoute::Id GetRouteId(const RenderFrameHostId& render_frame_host_id,
299 const std::string& presentation_id) const; 404 const std::string& presentation_id) const;
300 const std::vector<MediaRoute::Id> GetRouteIds(
301 const RenderFrameHostId& render_frame_host_id) const;
302 405
303 private: 406 private:
304 PresentationFrame* GetOrAddPresentationFrame( 407 PresentationFrame* GetOrAddPresentationFrame(
305 const RenderFrameHostId& render_frame_host_id); 408 const RenderFrameHostId& render_frame_host_id);
306 409
307 // Maps a frame identifier to a PresentationFrame object for frames 410 // Maps a frame identifier to a PresentationFrame object for frames
308 // that are using presentation API. 411 // that are using presentation API.
309 base::ScopedPtrHashMap<RenderFrameHostId, scoped_ptr<PresentationFrame>> 412 base::ScopedPtrHashMap<RenderFrameHostId, scoped_ptr<PresentationFrame>>
310 presentation_frames_; 413 presentation_frames_;
311 414
312 // References to the owning WebContents, and the corresponding MediaRouter. 415 // References to the owning WebContents, and the corresponding MediaRouter.
416 content::WebContents* const web_contents_;
313 MediaRouter* router_; 417 MediaRouter* router_;
314 content::WebContents* web_contents_;
315 }; 418 };
316 419
317 PresentationFrameManager::PresentationFrameManager( 420 PresentationFrameManager::PresentationFrameManager(
318 content::WebContents* web_contents, 421 content::WebContents* web_contents,
319 MediaRouter* router) 422 MediaRouter* router)
320 : router_(router), web_contents_(web_contents) { 423 : web_contents_(web_contents), router_(router) {
321 DCHECK(web_contents_); 424 DCHECK(web_contents_);
322 DCHECK(router_); 425 DCHECK(router_);
323 } 426 }
324 427
325 PresentationFrameManager::~PresentationFrameManager() { 428 PresentationFrameManager::~PresentationFrameManager() {
326 for (auto& frame : presentation_frames_) 429 for (auto& frame : presentation_frames_)
327 frame.second->OnPresentationServiceDelegateDestroyed(); 430 frame.second->OnPresentationServiceDelegateDestroyed();
328 } 431 }
329 432
330 void PresentationFrameManager::OnPresentationSessionStarted( 433 void PresentationFrameManager::OnPresentationSessionStarted(
331 const RenderFrameHostId& render_frame_host_id, 434 const RenderFrameHostId& render_frame_host_id,
332 bool is_default_presentation, 435 bool is_default_presentation,
333 const content::PresentationSessionInfo& session, 436 const content::PresentationSessionInfo& session,
334 const MediaRoute::Id& route_id) { 437 const MediaRoute& route) {
335 auto presentation_frame = presentation_frames_.get(render_frame_host_id); 438 auto presentation_frame = presentation_frames_.get(render_frame_host_id);
336 if (presentation_frame) 439 if (presentation_frame) {
337 presentation_frame->OnPresentationSessionStarted(is_default_presentation, 440 presentation_frame->OnPresentationSessionStarted(is_default_presentation,
338 session, route_id); 441 session, route);
442 }
339 } 443 }
340 444
341 void PresentationFrameManager::OnPresentationSessionClosed( 445 void PresentationFrameManager::OnPresentationSessionClosed(
342 const RenderFrameHostId& render_frame_host_id, 446 const RenderFrameHostId& render_frame_host_id,
343 const std::string& presentation_id) { 447 const std::string& presentation_id) {
344 auto presentation_frame = presentation_frames_.get(render_frame_host_id); 448 auto presentation_frame = presentation_frames_.get(render_frame_host_id);
345 if (presentation_frame) 449 if (presentation_frame)
346 presentation_frame->OnPresentationSessionClosed(presentation_id); 450 presentation_frame->OnPresentationSessionClosed(presentation_id);
347 } 451 }
348 452
349 const MediaRoute::Id PresentationFrameManager::GetRouteId( 453 const MediaRoute::Id PresentationFrameManager::GetRouteId(
350 const RenderFrameHostId& render_frame_host_id, 454 const RenderFrameHostId& render_frame_host_id,
351 const std::string& presentation_id) const { 455 const std::string& presentation_id) const {
352 auto presentation_frame = presentation_frames_.get(render_frame_host_id); 456 auto presentation_frame = presentation_frames_.get(render_frame_host_id);
353 return presentation_frame ? presentation_frame->GetRouteId(presentation_id) 457 return presentation_frame ? presentation_frame->GetRouteId(presentation_id)
354 : ""; 458 : "";
355 } 459 }
356 460
357 const std::vector<MediaRoute::Id> PresentationFrameManager::GetRouteIds(
358 const RenderFrameHostId& render_frame_host_id) const {
359 auto presentation_frame = presentation_frames_.get(render_frame_host_id);
360 return presentation_frame ? presentation_frame->GetRouteIds()
361 : std::vector<MediaRoute::Id>();
362 }
363
364 bool PresentationFrameManager::SetScreenAvailabilityListener( 461 bool PresentationFrameManager::SetScreenAvailabilityListener(
365 const RenderFrameHostId& render_frame_host_id, 462 const RenderFrameHostId& render_frame_host_id,
366 content::PresentationScreenAvailabilityListener* listener) { 463 content::PresentationScreenAvailabilityListener* listener) {
367 DCHECK(listener); 464 DCHECK(listener);
368 auto presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); 465 auto presentation_frame = GetOrAddPresentationFrame(render_frame_host_id);
369 return presentation_frame->SetScreenAvailabilityListener(listener); 466 return presentation_frame->SetScreenAvailabilityListener(listener);
370 } 467 }
371 468
372 bool PresentationFrameManager::RemoveScreenAvailabilityListener( 469 bool PresentationFrameManager::RemoveScreenAvailabilityListener(
373 const RenderFrameHostId& render_frame_host_id, 470 const RenderFrameHostId& render_frame_host_id,
374 content::PresentationScreenAvailabilityListener* listener) { 471 content::PresentationScreenAvailabilityListener* listener) {
375 DCHECK(listener); 472 DCHECK(listener);
376 auto presentation_frame = presentation_frames_.get(render_frame_host_id); 473 auto presentation_frame = presentation_frames_.get(render_frame_host_id);
377 return presentation_frame && 474 return presentation_frame &&
378 presentation_frame->RemoveScreenAvailabilityListener(listener); 475 presentation_frame->RemoveScreenAvailabilityListener(listener);
379 } 476 }
380 477
381 bool PresentationFrameManager::HasScreenAvailabilityListenerForTest( 478 bool PresentationFrameManager::HasScreenAvailabilityListenerForTest(
382 const RenderFrameHostId& render_frame_host_id, 479 const RenderFrameHostId& render_frame_host_id,
383 const MediaSource::Id& source_id) const { 480 const MediaSource::Id& source_id) const {
384 auto presentation_frame = presentation_frames_.get(render_frame_host_id); 481 auto presentation_frame = presentation_frames_.get(render_frame_host_id);
385 return presentation_frame && 482 return presentation_frame &&
386 presentation_frame->HasScreenAvailabilityListenerForTest(source_id); 483 presentation_frame->HasScreenAvailabilityListenerForTest(source_id);
387 } 484 }
388 485
486 void PresentationFrameManager::SendMessage(
487 const RenderFrameHostId& render_frame_host_id,
488 const content::PresentationSessionInfo& session,
489 scoped_ptr<content::PresentationSessionMessage> message,
490 const content::SendMessageCallback send_message_cb) {
491 PresentationFrame* presentation_frame =
492 presentation_frames_.get(render_frame_host_id);
493 if (!presentation_frame) {
494 DVLOG(2) << "SendMessage: PresentationFrame does not exist "
495 << "for: (" << render_frame_host_id.first << ", "
496 << render_frame_host_id.second << ")";
497 send_message_cb.Run(false);
498 return;
499 }
500
501 presentation_frame->SendMessage(session, message.Pass(), send_message_cb);
502 }
503
389 bool PresentationFrameManager::ListenForSessionStateChange( 504 bool PresentationFrameManager::ListenForSessionStateChange(
390 const RenderFrameHostId& render_frame_host_id, 505 const RenderFrameHostId& render_frame_host_id,
391 content::PresentationSessionStateListener* listener) { 506 content::PresentationSessionStateListener* listener) {
392 PresentationFrame* presentation_frame = 507 PresentationFrame* presentation_frame =
393 GetOrAddPresentationFrame(render_frame_host_id); 508 GetOrAddPresentationFrame(render_frame_host_id);
394 return presentation_frame->ListenForSessionStateChange(listener); 509 return presentation_frame->ListenForSessionStateChange(listener);
395 } 510 }
396 511
397 void PresentationFrameManager::ListenForSessionMessages( 512 void PresentationFrameManager::ListenForSessionMessages(
398 const RenderFrameHostId& render_frame_host_id, 513 const RenderFrameHostId& render_frame_host_id,
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
430 auto presentation_frame = presentation_frames_.get(render_frame_host_id); 545 auto presentation_frame = presentation_frames_.get(render_frame_host_id);
431 if (presentation_frame) 546 if (presentation_frame)
432 presentation_frame->Reset(); 547 presentation_frame->Reset();
433 } 548 }
434 549
435 PresentationFrame* PresentationFrameManager::GetOrAddPresentationFrame( 550 PresentationFrame* PresentationFrameManager::GetOrAddPresentationFrame(
436 const RenderFrameHostId& render_frame_host_id) { 551 const RenderFrameHostId& render_frame_host_id) {
437 if (!presentation_frames_.contains(render_frame_host_id)) { 552 if (!presentation_frames_.contains(render_frame_host_id)) {
438 presentation_frames_.add( 553 presentation_frames_.add(
439 render_frame_host_id, 554 render_frame_host_id,
440 scoped_ptr<PresentationFrame>( 555 scoped_ptr<PresentationFrame>(new PresentationFrame(
441 new PresentationFrame(web_contents_, router_))); 556 web_contents_, render_frame_host_id, router_)));
442 } 557 }
443 return presentation_frames_.get(render_frame_host_id); 558 return presentation_frames_.get(render_frame_host_id);
444 } 559 }
445 560
446 void PresentationFrameManager::SetMediaRouterForTest(MediaRouter* router) { 561 void PresentationFrameManager::SetMediaRouterForTest(MediaRouter* router) {
447 router_ = router; 562 router_ = router;
448 } 563 }
449 564
450 PresentationServiceDelegateImpl* 565 PresentationServiceDelegateImpl*
451 PresentationServiceDelegateImpl::GetOrCreateForWebContents( 566 PresentationServiceDelegateImpl::GetOrCreateForWebContents(
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 frame_manager_->Reset(render_frame_host_id); 623 frame_manager_->Reset(render_frame_host_id);
509 if (render_frame_host_id == default_presentation_render_frame_host_id_) { 624 if (render_frame_host_id == default_presentation_render_frame_host_id_) {
510 UpdateDefaultMediaSourceAndNotifyObservers(RenderFrameHostId(), 625 UpdateDefaultMediaSourceAndNotifyObservers(RenderFrameHostId(),
511 MediaSource(), GURL()); 626 MediaSource(), GURL());
512 } 627 }
513 } 628 }
514 629
515 void PresentationServiceDelegateImpl::SetDefaultPresentationUrl( 630 void PresentationServiceDelegateImpl::SetDefaultPresentationUrl(
516 int render_process_id, 631 int render_process_id,
517 int render_frame_id, 632 int render_frame_id,
518 const std::string& default_presentation_url) { 633 const std::string& default_presentation_url,
634 const PresentationSessionStartedCallback& callback) {
519 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 635 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
520 if (IsMainFrame(render_process_id, render_frame_id)) { 636 if (IsMainFrame(render_process_id, render_frame_id)) {
521 // This is the main frame, which means tab-level default presentation 637 // This is the main frame, which means tab-level default presentation
522 // might have been updated. 638 // might have been updated.
523 MediaSource default_source; 639 MediaSource default_source;
524 if (!default_presentation_url.empty()) 640 if (!default_presentation_url.empty())
525 default_source = MediaSourceForPresentationUrl(default_presentation_url); 641 default_source = MediaSourceForPresentationUrl(default_presentation_url);
526 642
527 GURL default_frame_url = GetLastCommittedURLForFrame(render_frame_host_id); 643 GURL default_frame_url = GetLastCommittedURLForFrame(render_frame_host_id);
528 UpdateDefaultMediaSourceAndNotifyObservers( 644 UpdateDefaultMediaSourceAndNotifyObservers(
(...skipping 22 matching lines...) Expand all
551 FOR_EACH_OBSERVER( 667 FOR_EACH_OBSERVER(
552 DefaultMediaSourceObserver, default_media_source_observers_, 668 DefaultMediaSourceObserver, default_media_source_observers_,
553 OnDefaultMediaSourceChanged(default_source_, default_frame_url_)); 669 OnDefaultMediaSourceChanged(default_source_, default_frame_url_));
554 } 670 }
555 } 671 }
556 672
557 void PresentationServiceDelegateImpl::OnJoinRouteResponse( 673 void PresentationServiceDelegateImpl::OnJoinRouteResponse(
558 int render_process_id, 674 int render_process_id,
559 int render_frame_id, 675 int render_frame_id,
560 const content::PresentationSessionInfo& session, 676 const content::PresentationSessionInfo& session,
561 const PresentationSessionSuccessCallback& success_cb, 677 const PresentationSessionStartedCallback& success_cb,
562 const PresentationSessionErrorCallback& error_cb, 678 const PresentationSessionErrorCallback& error_cb,
563 const MediaRoute* route, 679 const MediaRoute* route,
564 const std::string& presentation_id, 680 const std::string& presentation_id,
565 const std::string& error_text) { 681 const std::string& error_text) {
566 if (!route) { 682 if (!route) {
567 error_cb.Run(content::PresentationError( 683 error_cb.Run(content::PresentationError(
568 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, error_text)); 684 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, error_text));
569 } else { 685 } else {
570 DVLOG(1) << "OnJoinRouteResponse: " 686 DVLOG(1) << "OnJoinRouteResponse: "
571 << "route_id: " << route->media_route_id() 687 << "route_id: " << route->media_route_id()
572 << ", presentation URL: " << session.presentation_url 688 << ", presentation URL: " << session.presentation_url
573 << ", presentation ID: " << session.presentation_id; 689 << ", presentation ID: " << session.presentation_id
690 << ", offscreen? " << route->is_offscreen_presentation();
574 DCHECK_EQ(session.presentation_id, presentation_id); 691 DCHECK_EQ(session.presentation_id, presentation_id);
575 frame_manager_->OnPresentationSessionStarted( 692 RenderFrameHostId rfh_id(render_process_id, render_frame_id);
576 RenderFrameHostId(render_process_id, render_frame_id), false, session, 693 frame_manager_->OnPresentationSessionStarted(rfh_id, false, session,
577 route->media_route_id()); 694 *route);
578 success_cb.Run(session); 695 success_cb.Run(session);
579 } 696 }
580 } 697 }
581 698
582 void PresentationServiceDelegateImpl::OnStartSessionSucceeded( 699 void PresentationServiceDelegateImpl::OnStartSessionSucceeded(
583 int render_process_id, 700 int render_process_id,
584 int render_frame_id, 701 int render_frame_id,
585 const PresentationSessionSuccessCallback& success_cb, 702 const PresentationSessionStartedCallback& success_cb,
586 const content::PresentationSessionInfo& new_session, 703 const content::PresentationSessionInfo& new_session,
587 const MediaRoute::Id& route_id) { 704 const MediaRoute& route) {
705 const MediaRoute::Id& route_id = route.media_route_id();
588 DVLOG(1) << "OnStartSessionSucceeded: " 706 DVLOG(1) << "OnStartSessionSucceeded: "
589 << "route_id: " << route_id 707 << "route_id: " << route_id
590 << ", presentation URL: " << new_session.presentation_url 708 << ", presentation URL: " << new_session.presentation_url
591 << ", presentation ID: " << new_session.presentation_id; 709 << ", presentation ID: " << new_session.presentation_id;
592 frame_manager_->OnPresentationSessionStarted( 710 RenderFrameHostId rfh_id(render_process_id, render_frame_id);
593 RenderFrameHostId(render_process_id, render_frame_id), false, new_session, 711 frame_manager_->OnPresentationSessionStarted(rfh_id, false, new_session,
594 route_id); 712 route);
595 success_cb.Run(new_session); 713 success_cb.Run(new_session);
596 } 714 }
597 715
598 void PresentationServiceDelegateImpl::StartSession( 716 void PresentationServiceDelegateImpl::StartSession(
599 int render_process_id, 717 int render_process_id,
600 int render_frame_id, 718 int render_frame_id,
601 const std::string& presentation_url, 719 const std::string& presentation_url,
602 const PresentationSessionSuccessCallback& success_cb, 720 const PresentationSessionStartedCallback& success_cb,
603 const PresentationSessionErrorCallback& error_cb) { 721 const PresentationSessionErrorCallback& error_cb) {
604 if (presentation_url.empty() || !IsValidPresentationUrl(presentation_url)) { 722 if (presentation_url.empty() || !IsValidPresentationUrl(presentation_url)) {
605 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, 723 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN,
606 "Invalid presentation arguments.")); 724 "Invalid presentation arguments."));
607 return; 725 return;
608 } 726 }
609 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 727 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
610 728
611 scoped_ptr<CreatePresentationSessionRequest> context( 729 scoped_ptr<CreatePresentationSessionRequest> context(
612 new CreatePresentationSessionRequest( 730 new CreatePresentationSessionRequest(
(...skipping 10 matching lines...) Expand all
623 "Unable to create dialog.")); 741 "Unable to create dialog."));
624 return; 742 return;
625 } 743 }
626 } 744 }
627 745
628 void PresentationServiceDelegateImpl::JoinSession( 746 void PresentationServiceDelegateImpl::JoinSession(
629 int render_process_id, 747 int render_process_id,
630 int render_frame_id, 748 int render_frame_id,
631 const std::string& presentation_url, 749 const std::string& presentation_url,
632 const std::string& presentation_id, 750 const std::string& presentation_id,
633 const PresentationSessionSuccessCallback& success_cb, 751 const PresentationSessionStartedCallback& success_cb,
634 const PresentationSessionErrorCallback& error_cb) { 752 const PresentationSessionErrorCallback& error_cb) {
635 std::vector<MediaRouteResponseCallback> route_response_callbacks; 753 std::vector<MediaRouteResponseCallback> route_response_callbacks;
636 route_response_callbacks.push_back(base::Bind( 754 route_response_callbacks.push_back(base::Bind(
637 &PresentationServiceDelegateImpl::OnJoinRouteResponse, 755 &PresentationServiceDelegateImpl::OnJoinRouteResponse,
638 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, 756 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id,
639 content::PresentationSessionInfo(presentation_url, presentation_id), 757 content::PresentationSessionInfo(presentation_url, presentation_id),
640 success_cb, error_cb)); 758 success_cb, error_cb));
641 router_->JoinRoute( 759 router_->JoinRoute(
642 MediaSourceForPresentationUrl(presentation_url).id(), presentation_id, 760 MediaSourceForPresentationUrl(presentation_url).id(), presentation_id,
643 GetLastCommittedURLForFrame( 761 GetLastCommittedURLForFrame(
(...skipping 13 matching lines...) Expand all
657 return; 775 return;
658 } 776 }
659 router_->CloseRoute(route_id); 777 router_->CloseRoute(route_id);
660 } 778 }
661 779
662 void PresentationServiceDelegateImpl::ListenForSessionMessages( 780 void PresentationServiceDelegateImpl::ListenForSessionMessages(
663 int render_process_id, 781 int render_process_id,
664 int render_frame_id, 782 int render_frame_id,
665 const content::PresentationSessionInfo& session, 783 const content::PresentationSessionInfo& session,
666 const content::PresentationSessionMessageCallback& message_cb) { 784 const content::PresentationSessionMessageCallback& message_cb) {
667 frame_manager_->ListenForSessionMessages( 785 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
668 RenderFrameHostId(render_process_id, render_frame_id), session, 786 frame_manager_->ListenForSessionMessages(render_frame_host_id, session,
669 message_cb); 787 message_cb);
670 } 788 }
671 789
672 void PresentationServiceDelegateImpl::SendMessage( 790 void PresentationServiceDelegateImpl::SendMessage(
673 int render_process_id, 791 int render_process_id,
674 int render_frame_id, 792 int render_frame_id,
675 const content::PresentationSessionInfo& session, 793 const content::PresentationSessionInfo& session,
676 scoped_ptr<content::PresentationSessionMessage> message, 794 scoped_ptr<content::PresentationSessionMessage> message,
677 const SendMessageCallback& send_message_cb) { 795 const content::SendMessageCallback& send_message_cb) {
678 const MediaRoute::Id& route_id = frame_manager_->GetRouteId( 796 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
679 RenderFrameHostId(render_process_id, render_frame_id), 797 frame_manager_->SendMessage(render_frame_host_id, session, message.Pass(),
680 session.presentation_id); 798 send_message_cb);
681 if (route_id.empty()) {
682 DVLOG(1) << "No active route for " << session.presentation_id;
683 send_message_cb.Run(false);
684 return;
685 }
686
687 if (message->is_binary()) {
688 router_->SendRouteBinaryMessage(route_id, message->data.Pass(),
689 send_message_cb);
690 } else {
691 router_->SendRouteMessage(route_id, message->message, send_message_cb);
692 }
693 } 799 }
694 800
695 bool PresentationServiceDelegateImpl::ListenForSessionStateChange( 801 bool PresentationServiceDelegateImpl::ListenForSessionStateChange(
696 int render_process_id, 802 int render_process_id,
697 int render_frame_id, 803 int render_frame_id,
698 content::PresentationSessionStateListener* listener) { 804 content::PresentationSessionStateListener* listener) {
699 return frame_manager_->ListenForSessionStateChange( 805 return frame_manager_->ListenForSessionStateChange(
700 RenderFrameHostId(render_process_id, render_frame_id), listener); 806 RenderFrameHostId(render_process_id, render_frame_id), listener);
701 } 807 }
702 808
809 void PresentationServiceDelegateImpl::NotifyWhenReceiverSessionIsAvailable(
810 int render_process_id,
811 int render_frame_id,
812 const content::PresentationReceiverSessionAvailableCallback& callback) {
813 // We only support receiver APIs in offscreen tabs created for offscreen
814 // presentations.
815 // See ReceiverPresentationServiceDelegateImpl for details.
816 NOTIMPLEMENTED();
817 callback.Run(nullptr);
818 }
819
703 void PresentationServiceDelegateImpl::OnRouteResponse( 820 void PresentationServiceDelegateImpl::OnRouteResponse(
704 const MediaRoute* route, 821 const MediaRoute* route,
705 const std::string& presentation_id, 822 const std::string& presentation_id,
706 const std::string& error) { 823 const std::string& error) {
707 if (!route) 824 if (!route)
708 return; 825 return;
826
709 const MediaSource& source = route->media_source(); 827 const MediaSource& source = route->media_source();
710 DCHECK(!source.Empty()); 828 DCHECK(!source.Empty());
711 if (!default_source_.Equals(source)) 829 if (!default_source_.Equals(source))
712 return; 830 return;
831
713 RenderFrameHost* main_frame = web_contents_->GetMainFrame(); 832 RenderFrameHost* main_frame = web_contents_->GetMainFrame();
714 if (!main_frame) 833 if (!main_frame)
715 return; 834 return;
835
716 RenderFrameHostId render_frame_host_id(GetRenderFrameHostId(main_frame)); 836 RenderFrameHostId render_frame_host_id(GetRenderFrameHostId(main_frame));
717 frame_manager_->OnPresentationSessionStarted( 837 content::PresentationSessionInfo session(
718 render_frame_host_id, true, 838 PresentationUrlFromMediaSource(source), presentation_id);
719 content::PresentationSessionInfo(PresentationUrlFromMediaSource(source), 839
720 presentation_id), 840 frame_manager_->OnPresentationSessionStarted(render_frame_host_id, true,
721 route->media_route_id()); 841 session, *route);
722 } 842 }
723 843
724 void PresentationServiceDelegateImpl::AddDefaultMediaSourceObserver( 844 void PresentationServiceDelegateImpl::AddDefaultMediaSourceObserver(
725 DefaultMediaSourceObserver* observer) { 845 DefaultMediaSourceObserver* observer) {
726 default_media_source_observers_.AddObserver(observer); 846 default_media_source_observers_.AddObserver(observer);
727 } 847 }
728 848
729 void PresentationServiceDelegateImpl::RemoveDefaultMediaSourceObserver( 849 void PresentationServiceDelegateImpl::RemoveDefaultMediaSourceObserver(
730 DefaultMediaSourceObserver* observer) { 850 DefaultMediaSourceObserver* observer) {
731 default_media_source_observers_.RemoveObserver(observer); 851 default_media_source_observers_.RemoveObserver(observer);
(...skipping 13 matching lines...) Expand all
745 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( 865 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest(
746 int render_process_id, 866 int render_process_id,
747 int render_frame_id, 867 int render_frame_id,
748 const MediaSource::Id& source_id) const { 868 const MediaSource::Id& source_id) const {
749 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 869 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
750 return frame_manager_->HasScreenAvailabilityListenerForTest( 870 return frame_manager_->HasScreenAvailabilityListenerForTest(
751 render_frame_host_id, source_id); 871 render_frame_host_id, source_id);
752 } 872 }
753 873
754 } // namespace media_router 874 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698