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

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

Powered by Google App Engine
This is Rietveld 408576698