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

Side by Side Diff: content/browser/presentation/presentation_service_impl.cc

Issue 2379703002: [Presentation API] (alternative) 1-UA: send message between controller and receiver page (Closed)
Patch Set: merge with master Created 4 years, 1 month 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 "content/browser/presentation/presentation_service_impl.h" 5 #include "content/browser/presentation/presentation_service_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <algorithm> 9 #include <algorithm>
10 #include <utility> 10 #include <utility>
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 callback.Run(blink::mojom::PresentationSessionInfoPtr(), 119 callback.Run(blink::mojom::PresentationSessionInfoPtr(),
120 blink::mojom::PresentationError::From(PresentationError( 120 blink::mojom::PresentationError::From(PresentationError(
121 PRESENTATION_ERROR_UNKNOWN, "Internal error"))); 121 PRESENTATION_ERROR_UNKNOWN, "Internal error")));
122 } 122 }
123 123
124 } // namespace 124 } // namespace
125 125
126 PresentationServiceImpl::PresentationServiceImpl( 126 PresentationServiceImpl::PresentationServiceImpl(
127 RenderFrameHost* render_frame_host, 127 RenderFrameHost* render_frame_host,
128 WebContents* web_contents, 128 WebContents* web_contents,
129 PresentationServiceDelegate* delegate) 129 ControllerPresentationServiceDelegate* controller_delegate,
130 ReceiverPresentationServiceDelegate* receiver_delegate)
130 : WebContentsObserver(web_contents), 131 : WebContentsObserver(web_contents),
131 delegate_(delegate), 132 controller_delegate_(controller_delegate),
133 receiver_delegate_(receiver_delegate),
132 start_session_request_id_(kInvalidRequestSessionId), 134 start_session_request_id_(kInvalidRequestSessionId),
133 weak_factory_(this) { 135 weak_factory_(this) {
134 DCHECK(render_frame_host); 136 DCHECK(render_frame_host);
135 DCHECK(web_contents); 137 DCHECK(web_contents);
136 CHECK(render_frame_host->IsRenderFrameLive()); 138 CHECK(render_frame_host->IsRenderFrameLive());
137 139
138 render_process_id_ = render_frame_host->GetProcess()->GetID(); 140 render_process_id_ = render_frame_host->GetProcess()->GetID();
139 render_frame_id_ = render_frame_host->GetRoutingID(); 141 render_frame_id_ = render_frame_host->GetRoutingID();
140 DVLOG(2) << "PresentationServiceImpl: " 142 DVLOG(2) << "PresentationServiceImpl: "
141 << render_process_id_ << ", " << render_frame_id_; 143 << render_process_id_ << ", " << render_frame_id_;
142 if (delegate_) 144
143 delegate_->AddObserver(render_process_id_, render_frame_id_, this); 145 if (auto delegate = GetPresentationServiceDelegate())
146 delegate->AddObserver(render_process_id_, render_frame_id_, this);
144 } 147 }
145 148
146 PresentationServiceImpl::~PresentationServiceImpl() { 149 PresentationServiceImpl::~PresentationServiceImpl() {
147 if (delegate_) 150 DVLOG(2) << __FUNCTION__ << ": " << render_process_id_ << ", "
148 delegate_->RemoveObserver(render_process_id_, render_frame_id_); 151 << render_frame_id_;
152
153 if (auto delegate = GetPresentationServiceDelegate())
154 delegate->RemoveObserver(render_process_id_, render_frame_id_);
149 } 155 }
150 156
151 // static 157 // static
152 void PresentationServiceImpl::CreateMojoService( 158 void PresentationServiceImpl::CreateMojoService(
153 RenderFrameHost* render_frame_host, 159 RenderFrameHost* render_frame_host,
154 mojo::InterfaceRequest<blink::mojom::PresentationService> request) { 160 mojo::InterfaceRequest<blink::mojom::PresentationService> request) {
155 DVLOG(2) << "CreateMojoService"; 161 DVLOG(2) << "CreateMojoService";
156 WebContents* web_contents = 162 WebContents* web_contents =
157 WebContents::FromRenderFrameHost(render_frame_host); 163 WebContents::FromRenderFrameHost(render_frame_host);
158 DCHECK(web_contents); 164 DCHECK(web_contents);
159 165
160 // This object will be deleted when the RenderFrameHost is about to be 166 // This object will be deleted when the RenderFrameHost is about to be
161 // deleted (RenderFrameDeleted). 167 // deleted (RenderFrameDeleted).
162 PresentationServiceImpl* impl = new PresentationServiceImpl( 168 PresentationServiceImpl* impl = new PresentationServiceImpl(
163 render_frame_host, 169 render_frame_host, web_contents,
164 web_contents, 170 GetContentClient()->browser()->GetControllerPresentationServiceDelegate(
165 GetContentClient()->browser()->GetPresentationServiceDelegate( 171 web_contents),
172 GetContentClient()->browser()->GetReceiverPresentationServiceDelegate(
166 web_contents)); 173 web_contents));
174 if (impl->receiver_delegate_) {
175 // In current implementation, web contents can be controller or receiver
176 // but not both.
177 impl->controller_delegate_ = nullptr;
178 }
167 impl->Bind(std::move(request)); 179 impl->Bind(std::move(request));
168 } 180 }
169 181
170 void PresentationServiceImpl::Bind( 182 void PresentationServiceImpl::Bind(
171 mojo::InterfaceRequest<blink::mojom::PresentationService> request) { 183 mojo::InterfaceRequest<blink::mojom::PresentationService> request) {
172 binding_.reset(new mojo::Binding<blink::mojom::PresentationService>( 184 binding_.reset(new mojo::Binding<blink::mojom::PresentationService>(
173 this, std::move(request))); 185 this, std::move(request)));
174 } 186 }
175 187
176 void PresentationServiceImpl::SetClient( 188 void PresentationServiceImpl::SetClient(
177 blink::mojom::PresentationServiceClientPtr client) { 189 blink::mojom::PresentationServiceClientPtr client) {
178 DCHECK(!client_.get()); 190 DCHECK(!client_.get());
179 // TODO(imcheng): Set ErrorHandler to listen for errors. 191 // TODO(imcheng): Set ErrorHandler to listen for errors.
180 client_ = std::move(client); 192 client_ = std::move(client);
193
194 if (receiver_delegate_) {
195 receiver_delegate_->RegisterReceiverConnectionAvailableCallback(
196 base::Bind(&PresentationServiceImpl::OnReceiverConnectionAvailable,
197 weak_factory_.GetWeakPtr()));
198 }
181 } 199 }
182 200
183 void PresentationServiceImpl::ListenForScreenAvailability(const GURL& url) { 201 void PresentationServiceImpl::ListenForScreenAvailability(const GURL& url) {
184 DVLOG(2) << "ListenForScreenAvailability " << url.spec(); 202 DVLOG(2) << "ListenForScreenAvailability " << url.spec();
185 if (!delegate_) { 203 if (!controller_delegate_) {
186 client_->OnScreenAvailabilityUpdated(url, false); 204 client_->OnScreenAvailabilityUpdated(url, false);
187 return; 205 return;
188 } 206 }
189 207
190 if (screen_availability_listeners_.count(url)) 208 if (screen_availability_listeners_.count(url))
191 return; 209 return;
192 210
193 std::unique_ptr<ScreenAvailabilityListenerImpl> listener( 211 std::unique_ptr<ScreenAvailabilityListenerImpl> listener(
194 new ScreenAvailabilityListenerImpl(url, this)); 212 new ScreenAvailabilityListenerImpl(url, this));
195 if (delegate_->AddScreenAvailabilityListener( 213 if (controller_delegate_->AddScreenAvailabilityListener(
196 render_process_id_, 214 render_process_id_, render_frame_id_, listener.get())) {
197 render_frame_id_,
198 listener.get())) {
199 screen_availability_listeners_[url] = std::move(listener); 215 screen_availability_listeners_[url] = std::move(listener);
200 } else { 216 } else {
201 DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request."; 217 DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request.";
202 } 218 }
203 } 219 }
204 220
205 void PresentationServiceImpl::StopListeningForScreenAvailability( 221 void PresentationServiceImpl::StopListeningForScreenAvailability(
206 const GURL& url) { 222 const GURL& url) {
207 DVLOG(2) << "StopListeningForScreenAvailability " << url.spec(); 223 DVLOG(2) << "StopListeningForScreenAvailability " << url.spec();
208 if (!delegate_) 224 if (!controller_delegate_)
209 return; 225 return;
210 226
211 auto listener_it = screen_availability_listeners_.find(url); 227 auto listener_it = screen_availability_listeners_.find(url);
212 if (listener_it == screen_availability_listeners_.end()) 228 if (listener_it == screen_availability_listeners_.end())
213 return; 229 return;
214 230
215 delegate_->RemoveScreenAvailabilityListener( 231 controller_delegate_->RemoveScreenAvailabilityListener(
216 render_process_id_, render_frame_id_, listener_it->second.get()); 232 render_process_id_, render_frame_id_, listener_it->second.get());
217 screen_availability_listeners_.erase(listener_it); 233 screen_availability_listeners_.erase(listener_it);
218 } 234 }
219 235
220 void PresentationServiceImpl::StartSession( 236 void PresentationServiceImpl::StartSession(
221 const std::vector<GURL>& presentation_urls, 237 const std::vector<GURL>& presentation_urls,
222 const NewSessionCallback& callback) { 238 const NewSessionCallback& callback) {
223 DVLOG(2) << "StartSession"; 239 DVLOG(2) << "StartSession";
224 if (!delegate_) { 240 if (!controller_delegate_) {
225 callback.Run( 241 callback.Run(
226 blink::mojom::PresentationSessionInfoPtr(), 242 blink::mojom::PresentationSessionInfoPtr(),
227 blink::mojom::PresentationError::From(PresentationError( 243 blink::mojom::PresentationError::From(PresentationError(
228 PRESENTATION_ERROR_NO_AVAILABLE_SCREENS, "No screens found."))); 244 PRESENTATION_ERROR_NO_AVAILABLE_SCREENS, "No screens found.")));
229 return; 245 return;
230 } 246 }
231 247
232 // There is a StartSession request in progress. To avoid queueing up 248 // There is a StartSession request in progress. To avoid queueing up
233 // requests, the incoming request is rejected. 249 // requests, the incoming request is rejected.
234 if (start_session_request_id_ != kInvalidRequestSessionId) { 250 if (start_session_request_id_ != kInvalidRequestSessionId) {
235 InvokeNewSessionCallbackWithError(callback); 251 InvokeNewSessionCallbackWithError(callback);
236 return; 252 return;
237 } 253 }
238 254
239 start_session_request_id_ = GetNextRequestSessionId(); 255 start_session_request_id_ = GetNextRequestSessionId();
240 pending_start_session_cb_.reset(new NewSessionCallbackWrapper(callback)); 256 pending_start_session_cb_.reset(new NewSessionCallbackWrapper(callback));
241 delegate_->StartSession( 257 controller_delegate_->StartSession(
242 render_process_id_, render_frame_id_, presentation_urls, 258 render_process_id_, render_frame_id_, presentation_urls,
243 base::Bind(&PresentationServiceImpl::OnStartSessionSucceeded, 259 base::Bind(&PresentationServiceImpl::OnStartSessionSucceeded,
244 weak_factory_.GetWeakPtr(), start_session_request_id_), 260 weak_factory_.GetWeakPtr(), start_session_request_id_),
245 base::Bind(&PresentationServiceImpl::OnStartSessionError, 261 base::Bind(&PresentationServiceImpl::OnStartSessionError,
246 weak_factory_.GetWeakPtr(), start_session_request_id_)); 262 weak_factory_.GetWeakPtr(), start_session_request_id_));
247 } 263 }
248 264
249 void PresentationServiceImpl::JoinSession( 265 void PresentationServiceImpl::JoinSession(
250 const std::vector<GURL>& presentation_urls, 266 const std::vector<GURL>& presentation_urls,
251 const base::Optional<std::string>& presentation_id, 267 const base::Optional<std::string>& presentation_id,
252 const NewSessionCallback& callback) { 268 const NewSessionCallback& callback) {
253 DVLOG(2) << "JoinSession"; 269 DVLOG(2) << "JoinSession";
254 if (!delegate_) { 270 if (!controller_delegate_) {
255 callback.Run(blink::mojom::PresentationSessionInfoPtr(), 271 callback.Run(blink::mojom::PresentationSessionInfoPtr(),
256 blink::mojom::PresentationError::From(PresentationError( 272 blink::mojom::PresentationError::From(PresentationError(
257 PRESENTATION_ERROR_NO_PRESENTATION_FOUND, 273 PRESENTATION_ERROR_NO_PRESENTATION_FOUND,
258 "Error joining route: No matching route"))); 274 "Error joining route: No matching route")));
259 return; 275 return;
260 } 276 }
261 277
262 int request_session_id = RegisterJoinSessionCallback(callback); 278 int request_session_id = RegisterJoinSessionCallback(callback);
263 if (request_session_id == kInvalidRequestSessionId) { 279 if (request_session_id == kInvalidRequestSessionId) {
264 InvokeNewSessionCallbackWithError(callback); 280 InvokeNewSessionCallbackWithError(callback);
265 return; 281 return;
266 } 282 }
267 delegate_->JoinSession( 283 controller_delegate_->JoinSession(
268 render_process_id_, render_frame_id_, presentation_urls, 284 render_process_id_, render_frame_id_, presentation_urls,
269 presentation_id.value_or(std::string()), 285 presentation_id.value_or(std::string()),
270 base::Bind(&PresentationServiceImpl::OnJoinSessionSucceeded, 286 base::Bind(&PresentationServiceImpl::OnJoinSessionSucceeded,
271 weak_factory_.GetWeakPtr(), request_session_id), 287 weak_factory_.GetWeakPtr(), request_session_id),
272 base::Bind(&PresentationServiceImpl::OnJoinSessionError, 288 base::Bind(&PresentationServiceImpl::OnJoinSessionError,
273 weak_factory_.GetWeakPtr(), request_session_id)); 289 weak_factory_.GetWeakPtr(), request_session_id));
274 } 290 }
275 291
276 int PresentationServiceImpl::RegisterJoinSessionCallback( 292 int PresentationServiceImpl::RegisterJoinSessionCallback(
277 const NewSessionCallback& callback) { 293 const NewSessionCallback& callback) {
278 if (pending_join_session_cbs_.size() >= kMaxNumQueuedSessionRequests) 294 if (pending_join_session_cbs_.size() >= kMaxNumQueuedSessionRequests)
279 return kInvalidRequestSessionId; 295 return kInvalidRequestSessionId;
280 296
281 int request_id = GetNextRequestSessionId(); 297 int request_id = GetNextRequestSessionId();
282 pending_join_session_cbs_[request_id].reset( 298 pending_join_session_cbs_[request_id].reset(
283 new NewSessionCallbackWrapper(callback)); 299 new NewSessionCallbackWrapper(callback));
284 return request_id; 300 return request_id;
285 } 301 }
286 302
287 void PresentationServiceImpl::ListenForConnectionStateChange( 303 void PresentationServiceImpl::ListenForConnectionStateChange(
288 const PresentationSessionInfo& connection) { 304 const PresentationSessionInfo& connection) {
289 if (delegate_) { 305 if (controller_delegate_) {
290 delegate_->ListenForConnectionStateChange( 306 controller_delegate_->ListenForConnectionStateChange(
291 render_process_id_, render_frame_id_, connection, 307 render_process_id_, render_frame_id_, connection,
292 base::Bind(&PresentationServiceImpl::OnConnectionStateChanged, 308 base::Bind(&PresentationServiceImpl::OnConnectionStateChanged,
293 weak_factory_.GetWeakPtr(), connection)); 309 weak_factory_.GetWeakPtr(), connection));
294 } 310 }
295 } 311 }
296 312
297 void PresentationServiceImpl::OnStartSessionSucceeded( 313 void PresentationServiceImpl::OnStartSessionSucceeded(
298 int request_session_id, 314 int request_session_id,
299 const PresentationSessionInfo& session_info) { 315 const PresentationSessionInfo& session_info) {
316 DVLOG(2) << "PresentationServiceImpl::OnStartSessionSucceeded"
317 << " [is_offscreen]: " << session_info.is_offscreen;
300 if (request_session_id != start_session_request_id_) 318 if (request_session_id != start_session_request_id_)
301 return; 319 return;
302 320
303 CHECK(pending_start_session_cb_.get()); 321 CHECK(pending_start_session_cb_.get());
304 pending_start_session_cb_->Run( 322 pending_start_session_cb_->Run(
305 blink::mojom::PresentationSessionInfo::From(session_info), 323 blink::mojom::PresentationSessionInfo::From(session_info),
306 blink::mojom::PresentationErrorPtr()); 324 blink::mojom::PresentationErrorPtr());
307 ListenForConnectionStateChange(session_info); 325 ListenForConnectionStateChange(session_info);
308 pending_start_session_cb_.reset(); 326 pending_start_session_cb_.reset();
309 start_session_request_id_ = kInvalidRequestSessionId; 327 start_session_request_id_ = kInvalidRequestSessionId;
310 } 328 }
311 329
312 void PresentationServiceImpl::OnStartSessionError( 330 void PresentationServiceImpl::OnStartSessionError(
313 int request_session_id, 331 int request_session_id,
314 const PresentationError& error) { 332 const PresentationError& error) {
315 if (request_session_id != start_session_request_id_) 333 if (request_session_id != start_session_request_id_)
316 return; 334 return;
317 335
318 CHECK(pending_start_session_cb_.get()); 336 CHECK(pending_start_session_cb_.get());
319 pending_start_session_cb_->Run(blink::mojom::PresentationSessionInfoPtr(), 337 pending_start_session_cb_->Run(blink::mojom::PresentationSessionInfoPtr(),
320 blink::mojom::PresentationError::From(error)); 338 blink::mojom::PresentationError::From(error));
321 pending_start_session_cb_.reset(); 339 pending_start_session_cb_.reset();
322 start_session_request_id_ = kInvalidRequestSessionId; 340 start_session_request_id_ = kInvalidRequestSessionId;
323 } 341 }
324 342
325 void PresentationServiceImpl::OnJoinSessionSucceeded( 343 void PresentationServiceImpl::OnJoinSessionSucceeded(
326 int request_session_id, 344 int request_session_id,
327 const PresentationSessionInfo& session_info) { 345 const PresentationSessionInfo& session_info) {
346 DVLOG(2) << "PresentationServiceImpl::OnJoinSessionSucceeded"
347 << " [is_offscreen]: " << session_info.is_offscreen;
328 if (RunAndEraseJoinSessionMojoCallback( 348 if (RunAndEraseJoinSessionMojoCallback(
329 request_session_id, 349 request_session_id,
330 blink::mojom::PresentationSessionInfo::From(session_info), 350 blink::mojom::PresentationSessionInfo::From(session_info),
331 blink::mojom::PresentationErrorPtr())) { 351 blink::mojom::PresentationErrorPtr())) {
332 ListenForConnectionStateChange(session_info); 352 ListenForConnectionStateChange(session_info);
333 } 353 }
334 } 354 }
335 355
336 void PresentationServiceImpl::OnJoinSessionError( 356 void PresentationServiceImpl::OnJoinSessionError(
337 int request_session_id, 357 int request_session_id,
(...skipping 13 matching lines...) Expand all
351 371
352 DCHECK(it->second.get()); 372 DCHECK(it->second.get());
353 it->second->Run(std::move(session), std::move(error)); 373 it->second->Run(std::move(session), std::move(error));
354 pending_join_session_cbs_.erase(it); 374 pending_join_session_cbs_.erase(it);
355 return true; 375 return true;
356 } 376 }
357 377
358 void PresentationServiceImpl::SetDefaultPresentationUrls( 378 void PresentationServiceImpl::SetDefaultPresentationUrls(
359 const std::vector<GURL>& presentation_urls) { 379 const std::vector<GURL>& presentation_urls) {
360 DVLOG(2) << "SetDefaultPresentationUrls"; 380 DVLOG(2) << "SetDefaultPresentationUrls";
361 if (!delegate_) 381 if (!controller_delegate_)
362 return; 382 return;
363 383
364 if (default_presentation_urls_ == presentation_urls) 384 if (default_presentation_urls_ == presentation_urls)
365 return; 385 return;
366 386
367 default_presentation_urls_ = presentation_urls; 387 default_presentation_urls_ = presentation_urls;
368 delegate_->SetDefaultPresentationUrls( 388 controller_delegate_->SetDefaultPresentationUrls(
369 render_process_id_, render_frame_id_, presentation_urls, 389 render_process_id_, render_frame_id_, presentation_urls,
370 base::Bind(&PresentationServiceImpl::OnDefaultPresentationStarted, 390 base::Bind(&PresentationServiceImpl::OnDefaultPresentationStarted,
371 weak_factory_.GetWeakPtr())); 391 weak_factory_.GetWeakPtr()));
372 } 392 }
373 393
374 void PresentationServiceImpl::SendSessionMessage( 394 void PresentationServiceImpl::SendSessionMessage(
375 blink::mojom::PresentationSessionInfoPtr session, 395 blink::mojom::PresentationSessionInfoPtr session,
376 blink::mojom::SessionMessagePtr session_message, 396 blink::mojom::SessionMessagePtr session_message,
377 const SendSessionMessageCallback& callback) { 397 const SendSessionMessageCallback& callback) {
378 DVLOG(2) << "SendSessionMessage"; 398 DVLOG(2) << "SendSessionMessage"
399 << " [id]: " << session->id;
379 DCHECK(!session_message.is_null()); 400 DCHECK(!session_message.is_null());
380 // send_message_callback_ should be null by now, otherwise resetting of 401 // send_message_callback_ should be null by now, otherwise resetting of
381 // send_message_callback_ with new callback will drop the old callback. 402 // send_message_callback_ with new callback will drop the old callback.
382 if (!delegate_ || send_message_callback_) { 403 if (!controller_delegate_ || send_message_callback_) {
383 callback.Run(false); 404 callback.Run(false);
384 return; 405 return;
385 } 406 }
386 407
387 send_message_callback_.reset(new SendSessionMessageCallback(callback)); 408 send_message_callback_.reset(new SendSessionMessageCallback(callback));
388 delegate_->SendMessage( 409 controller_delegate_->SendMessage(
389 render_process_id_, render_frame_id_, 410 render_process_id_, render_frame_id_,
390 session.To<PresentationSessionInfo>(), 411 session.To<PresentationSessionInfo>(),
391 GetPresentationSessionMessage(std::move(session_message)), 412 GetPresentationSessionMessage(std::move(session_message)),
392 base::Bind(&PresentationServiceImpl::OnSendMessageCallback, 413 base::Bind(&PresentationServiceImpl::OnSendMessageCallback,
393 weak_factory_.GetWeakPtr())); 414 weak_factory_.GetWeakPtr()));
394 } 415 }
395 416
396 void PresentationServiceImpl::OnSendMessageCallback(bool sent) { 417 void PresentationServiceImpl::OnSendMessageCallback(bool sent) {
397 // It is possible that Reset() is invoked before receiving this callback. 418 // It is possible that Reset() is invoked before receiving this callback.
398 // So, always check send_message_callback_ for non-null. 419 // So, always check send_message_callback_ for non-null.
399 if (send_message_callback_) { 420 if (send_message_callback_) {
400 send_message_callback_->Run(sent); 421 send_message_callback_->Run(sent);
401 send_message_callback_.reset(); 422 send_message_callback_.reset();
402 } 423 }
403 } 424 }
404 425
405 void PresentationServiceImpl::CloseConnection( 426 void PresentationServiceImpl::CloseConnection(
406 const GURL& presentation_url, 427 const GURL& presentation_url,
407 const std::string& presentation_id) { 428 const std::string& presentation_id) {
408 DVLOG(2) << "CloseConnection " << presentation_id; 429 DVLOG(2) << "CloseConnection " << presentation_id;
409 if (delegate_) 430 if (controller_delegate_)
410 delegate_->CloseConnection(render_process_id_, render_frame_id_, 431 controller_delegate_->CloseConnection(render_process_id_, render_frame_id_,
411 presentation_id); 432 presentation_id);
412 } 433 }
413 434
414 void PresentationServiceImpl::Terminate(const GURL& presentation_url, 435 void PresentationServiceImpl::Terminate(const GURL& presentation_url,
415 const std::string& presentation_id) { 436 const std::string& presentation_id) {
416 DVLOG(2) << "Terminate " << presentation_id; 437 DVLOG(2) << "Terminate " << presentation_id;
417 if (delegate_) 438 if (controller_delegate_)
418 delegate_->Terminate(render_process_id_, render_frame_id_, presentation_id); 439 controller_delegate_->Terminate(render_process_id_, render_frame_id_,
440 presentation_id);
419 } 441 }
420 442
421 void PresentationServiceImpl::OnConnectionStateChanged( 443 void PresentationServiceImpl::OnConnectionStateChanged(
422 const PresentationSessionInfo& connection, 444 const PresentationSessionInfo& connection,
423 const PresentationConnectionStateChangeInfo& info) { 445 const PresentationConnectionStateChangeInfo& info) {
424 DCHECK(client_.get()); 446 DCHECK(client_.get());
425 if (info.state == PRESENTATION_CONNECTION_STATE_CLOSED) { 447 if (info.state == PRESENTATION_CONNECTION_STATE_CLOSED) {
426 client_->OnConnectionClosed( 448 client_->OnConnectionClosed(
427 blink::mojom::PresentationSessionInfo::From(connection), 449 blink::mojom::PresentationSessionInfo::From(connection),
428 content::PresentationConnectionCloseReasonToMojo(info.close_reason), 450 content::PresentationConnectionCloseReasonToMojo(info.close_reason),
429 info.message); 451 info.message);
430 } else { 452 } else {
431 client_->OnConnectionStateChanged( 453 client_->OnConnectionStateChanged(
432 blink::mojom::PresentationSessionInfo::From(connection), 454 blink::mojom::PresentationSessionInfo::From(connection),
433 PresentationConnectionStateToMojo(info.state)); 455 PresentationConnectionStateToMojo(info.state));
434 } 456 }
435 } 457 }
436 458
437 bool PresentationServiceImpl::FrameMatches( 459 bool PresentationServiceImpl::FrameMatches(
438 content::RenderFrameHost* render_frame_host) const { 460 content::RenderFrameHost* render_frame_host) const {
439 if (!render_frame_host) 461 if (!render_frame_host)
440 return false; 462 return false;
441 463
442 return render_frame_host->GetProcess()->GetID() == render_process_id_ && 464 return render_frame_host->GetProcess()->GetID() == render_process_id_ &&
443 render_frame_host->GetRoutingID() == render_frame_id_; 465 render_frame_host->GetRoutingID() == render_frame_id_;
444 } 466 }
445 467
468 PresentationServiceDelegateBase*
469 PresentationServiceImpl::GetPresentationServiceDelegate() {
470 if (receiver_delegate_)
471 return receiver_delegate_;
472
473 return controller_delegate_;
474 }
475
446 void PresentationServiceImpl::ListenForSessionMessages( 476 void PresentationServiceImpl::ListenForSessionMessages(
447 blink::mojom::PresentationSessionInfoPtr session) { 477 blink::mojom::PresentationSessionInfoPtr session) {
448 DVLOG(2) << "ListenForSessionMessages"; 478 DVLOG(2) << "ListenForSessionMessages";
449 if (!delegate_) 479 if (!controller_delegate_)
450 return; 480 return;
451 481
452 PresentationSessionInfo session_info(session.To<PresentationSessionInfo>()); 482 PresentationSessionInfo session_info(session.To<PresentationSessionInfo>());
453 delegate_->ListenForSessionMessages( 483 controller_delegate_->ListenForSessionMessages(
454 render_process_id_, render_frame_id_, session_info, 484 render_process_id_, render_frame_id_, session_info,
455 base::Bind(&PresentationServiceImpl::OnSessionMessages, 485 base::Bind(&PresentationServiceImpl::OnSessionMessages,
456 weak_factory_.GetWeakPtr(), session_info)); 486 weak_factory_.GetWeakPtr(), session_info));
457 } 487 }
458 488
489 void PresentationServiceImpl::SetPresentationConnection(
490 blink::mojom::PresentationSessionInfoPtr session,
491 blink::mojom::PresentationConnectionPtr connection) {
492 DVLOG(2) << "SetPresentationConnection";
493
494 if (!controller_delegate_)
495 return;
496
497 PresentationSessionInfo session_info(session.To<PresentationSessionInfo>());
498 controller_delegate_->ConnectToOffscreenPresentation(
499 render_process_id_, render_frame_id_, session_info,
500 std::move(connection));
501 }
502
459 void PresentationServiceImpl::OnSessionMessages( 503 void PresentationServiceImpl::OnSessionMessages(
460 const PresentationSessionInfo& session, 504 const PresentationSessionInfo& session,
461 const ScopedVector<PresentationSessionMessage>& messages, 505 const ScopedVector<PresentationSessionMessage>& messages,
462 bool pass_ownership) { 506 bool pass_ownership) {
463 DCHECK(client_); 507 DCHECK(client_);
464 508 DVLOG(2) << "OnSessionMessages"
465 DVLOG(2) << "OnSessionMessages"; 509 << " [id]: " << session.presentation_id;
466 std::vector<blink::mojom::SessionMessagePtr> mojo_messages(messages.size()); 510 std::vector<blink::mojom::SessionMessagePtr> mojo_messages(messages.size());
467 std::transform(messages.begin(), messages.end(), mojo_messages.begin(), 511 std::transform(messages.begin(), messages.end(), mojo_messages.begin(),
468 [pass_ownership](PresentationSessionMessage* message) { 512 [pass_ownership](PresentationSessionMessage* message) {
469 return ToMojoSessionMessage(message, pass_ownership); 513 return ToMojoSessionMessage(message, pass_ownership);
470 }); 514 });
471 515
472 client_->OnSessionMessagesReceived( 516 client_->OnSessionMessagesReceived(
473 blink::mojom::PresentationSessionInfo::From(session), 517 blink::mojom::PresentationSessionInfo::From(session),
474 std::move(mojo_messages)); 518 std::move(mojo_messages));
475 } 519 }
476 520
521 void PresentationServiceImpl::OnReceiverConnectionAvailable(
522 const content::PresentationSessionInfo& session_info,
523 PresentationConnectionPtr&& controller) {
524 DVLOG(2) << "PresentationServiceImpl::OnReceiverConnectionAvailable";
525
526 client_->OnReceiverConnectionAvailable(
527 blink::mojom::PresentationSessionInfo::From(session_info),
528 std::move(controller));
529 }
530
477 void PresentationServiceImpl::DidNavigateAnyFrame( 531 void PresentationServiceImpl::DidNavigateAnyFrame(
478 content::RenderFrameHost* render_frame_host, 532 content::RenderFrameHost* render_frame_host,
479 const content::LoadCommittedDetails& details, 533 const content::LoadCommittedDetails& details,
480 const content::FrameNavigateParams& params) { 534 const content::FrameNavigateParams& params) {
481 DVLOG(2) << "PresentationServiceImpl::DidNavigateAnyFrame"; 535 DVLOG(2) << "PresentationServiceImpl::DidNavigateAnyFrame";
482 if (!FrameMatches(render_frame_host)) 536 if (!FrameMatches(render_frame_host))
483 return; 537 return;
484 538
485 std::string prev_url_host = details.previous_url.host(); 539 std::string prev_url_host = details.previous_url.host();
486 std::string curr_url_host = params.url.host(); 540 std::string curr_url_host = params.url.host();
(...skipping 25 matching lines...) Expand all
512 void PresentationServiceImpl::WebContentsDestroyed() { 566 void PresentationServiceImpl::WebContentsDestroyed() {
513 LOG(ERROR) << "PresentationServiceImpl is being deleted in " 567 LOG(ERROR) << "PresentationServiceImpl is being deleted in "
514 << "WebContentsDestroyed()! This shouldn't happen since it " 568 << "WebContentsDestroyed()! This shouldn't happen since it "
515 << "should've been deleted during RenderFrameDeleted()."; 569 << "should've been deleted during RenderFrameDeleted().";
516 Reset(); 570 Reset();
517 delete this; 571 delete this;
518 } 572 }
519 573
520 void PresentationServiceImpl::Reset() { 574 void PresentationServiceImpl::Reset() {
521 DVLOG(2) << "PresentationServiceImpl::Reset"; 575 DVLOG(2) << "PresentationServiceImpl::Reset";
522 if (delegate_) 576
523 delegate_->Reset(render_process_id_, render_frame_id_); 577 if (auto delegate = GetPresentationServiceDelegate())
578 delegate->Reset(render_process_id_, render_frame_id_);
524 579
525 default_presentation_urls_.clear(); 580 default_presentation_urls_.clear();
526 581
527 screen_availability_listeners_.clear(); 582 screen_availability_listeners_.clear();
528 583
529 start_session_request_id_ = kInvalidRequestSessionId; 584 start_session_request_id_ = kInvalidRequestSessionId;
530 pending_start_session_cb_.reset(); 585 pending_start_session_cb_.reset();
531 586
532 pending_join_session_cbs_.clear(); 587 pending_join_session_cbs_.clear();
533 588
534 if (on_session_messages_callback_.get()) { 589 if (on_session_messages_callback_.get()) {
535 on_session_messages_callback_->Run( 590 on_session_messages_callback_->Run(
536 mojo::Array<blink::mojom::SessionMessagePtr>()); 591 mojo::Array<blink::mojom::SessionMessagePtr>());
537 on_session_messages_callback_.reset(); 592 on_session_messages_callback_.reset();
538 } 593 }
539 594
540 if (send_message_callback_) { 595 if (send_message_callback_) {
541 // Run the callback with false, indicating the renderer to stop sending 596 // Run the callback with false, indicating the renderer to stop sending
542 // the requests and invalidate all pending requests. 597 // the requests and invalidate all pending requests.
543 send_message_callback_->Run(false); 598 send_message_callback_->Run(false);
544 send_message_callback_.reset(); 599 send_message_callback_.reset();
545 } 600 }
546 } 601 }
547 602
548 void PresentationServiceImpl::OnDelegateDestroyed() { 603 void PresentationServiceImpl::OnDelegateDestroyed() {
549 DVLOG(2) << "PresentationServiceImpl::OnDelegateDestroyed"; 604 DVLOG(2) << "PresentationServiceImpl::OnDelegateDestroyed";
550 delegate_ = nullptr; 605 controller_delegate_ = nullptr;
606 receiver_delegate_ = nullptr;
551 Reset(); 607 Reset();
552 } 608 }
553 609
554 void PresentationServiceImpl::OnDefaultPresentationStarted( 610 void PresentationServiceImpl::OnDefaultPresentationStarted(
555 const PresentationSessionInfo& connection) { 611 const PresentationSessionInfo& connection) {
612 DVLOG(2) << "PresentationServiceImpl::OnDefaultPresentationStarted"
613 << " [is_offscreen]: " << connection.is_offscreen;
556 DCHECK(client_.get()); 614 DCHECK(client_.get());
557 client_->OnDefaultSessionStarted( 615 client_->OnDefaultSessionStarted(
558 blink::mojom::PresentationSessionInfo::From(connection)); 616 blink::mojom::PresentationSessionInfo::From(connection));
559 ListenForConnectionStateChange(connection); 617 ListenForConnectionStateChange(connection);
560 } 618 }
561 619
562 PresentationServiceImpl::ScreenAvailabilityListenerImpl:: 620 PresentationServiceImpl::ScreenAvailabilityListenerImpl::
563 ScreenAvailabilityListenerImpl(const GURL& availability_url, 621 ScreenAvailabilityListenerImpl(const GURL& availability_url,
564 PresentationServiceImpl* service) 622 PresentationServiceImpl* service)
565 : availability_url_(availability_url), service_(service) { 623 : availability_url_(availability_url), service_(service) {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
599 657
600 void PresentationServiceImpl::NewSessionCallbackWrapper::Run( 658 void PresentationServiceImpl::NewSessionCallbackWrapper::Run(
601 blink::mojom::PresentationSessionInfoPtr session, 659 blink::mojom::PresentationSessionInfoPtr session,
602 blink::mojom::PresentationErrorPtr error) { 660 blink::mojom::PresentationErrorPtr error) {
603 DCHECK(!callback_.is_null()); 661 DCHECK(!callback_.is_null());
604 callback_.Run(std::move(session), std::move(error)); 662 callback_.Run(std::move(session), std::move(error));
605 callback_.Reset(); 663 callback_.Reset();
606 } 664 }
607 665
608 } // namespace content 666 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698