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

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

Issue 2174693004: [Presentation API] Add support to content/ for multiple URLs per PresentationRequest. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 3 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 <string>
11 #include <utility> 10 #include <utility>
12 #include <vector>
13 11
14 #include "base/logging.h" 12 #include "base/logging.h"
15 #include "base/stl_util.h" 13 #include "base/stl_util.h"
16 #include "content/browser/presentation/presentation_type_converters.h" 14 #include "content/browser/presentation/presentation_type_converters.h"
17 #include "content/public/browser/content_browser_client.h" 15 #include "content/public/browser/content_browser_client.h"
18 #include "content/public/browser/navigation_details.h" 16 #include "content/public/browser/navigation_details.h"
19 #include "content/public/browser/presentation_session_message.h" 17 #include "content/public/browser/presentation_session_message.h"
20 #include "content/public/browser/render_frame_host.h" 18 #include "content/public/browser/render_frame_host.h"
21 #include "content/public/browser/render_process_host.h" 19 #include "content/public/browser/render_process_host.h"
22 #include "content/public/browser/web_contents.h" 20 #include "content/public/browser/web_contents.h"
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 this, std::move(request))); 173 this, std::move(request)));
176 } 174 }
177 175
178 void PresentationServiceImpl::SetClient( 176 void PresentationServiceImpl::SetClient(
179 blink::mojom::PresentationServiceClientPtr client) { 177 blink::mojom::PresentationServiceClientPtr client) {
180 DCHECK(!client_.get()); 178 DCHECK(!client_.get());
181 // TODO(imcheng): Set ErrorHandler to listen for errors. 179 // TODO(imcheng): Set ErrorHandler to listen for errors.
182 client_ = std::move(client); 180 client_ = std::move(client);
183 } 181 }
184 182
185 void PresentationServiceImpl::ListenForScreenAvailability( 183 void PresentationServiceImpl::ListenForScreenAvailability(const GURL& url) {
186 const std::string& url) {
187 DVLOG(2) << "ListenForScreenAvailability " << url; 184 DVLOG(2) << "ListenForScreenAvailability " << url;
188 if (!delegate_) { 185 if (!delegate_) {
189 client_->OnScreenAvailabilityUpdated(url, false); 186 client_->OnScreenAvailabilityUpdated(url, false);
190 return; 187 return;
191 } 188 }
192 189
193 if (screen_availability_listeners_.count(url)) 190 if (screen_availability_listeners_.count(url.spec()))
194 return; 191 return;
195 192
196 std::unique_ptr<ScreenAvailabilityListenerImpl> listener( 193 std::unique_ptr<ScreenAvailabilityListenerImpl> listener(
197 new ScreenAvailabilityListenerImpl(url, this)); 194 new ScreenAvailabilityListenerImpl(url.spec(), this));
198 if (delegate_->AddScreenAvailabilityListener( 195 if (delegate_->AddScreenAvailabilityListener(
199 render_process_id_, 196 render_process_id_,
200 render_frame_id_, 197 render_frame_id_,
201 listener.get())) { 198 listener.get())) {
202 screen_availability_listeners_[url] = std::move(listener); 199 screen_availability_listeners_[url.spec()] = std::move(listener);
203 } else { 200 } else {
204 DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request."; 201 DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request.";
205 } 202 }
206 } 203 }
207 204
208 void PresentationServiceImpl::StopListeningForScreenAvailability( 205 void PresentationServiceImpl::StopListeningForScreenAvailability(
209 const std::string& url) { 206 const GURL& url) {
210 DVLOG(2) << "StopListeningForScreenAvailability " << url; 207 DVLOG(2) << "StopListeningForScreenAvailability " << url.spec();
211 if (!delegate_) 208 if (!delegate_)
212 return; 209 return;
213 210
214 auto listener_it = screen_availability_listeners_.find(url); 211 auto listener_it = screen_availability_listeners_.find(url.spec());
215 if (listener_it == screen_availability_listeners_.end()) 212 if (listener_it == screen_availability_listeners_.end())
216 return; 213 return;
217 214
218 delegate_->RemoveScreenAvailabilityListener( 215 delegate_->RemoveScreenAvailabilityListener(
219 render_process_id_, render_frame_id_, listener_it->second.get()); 216 render_process_id_, render_frame_id_, listener_it->second.get());
220 screen_availability_listeners_.erase(listener_it); 217 screen_availability_listeners_.erase(listener_it);
221 } 218 }
222 219
223 void PresentationServiceImpl::StartSession(const std::string& presentation_url, 220 void PresentationServiceImpl::StartSession(
224 const NewSessionCallback& callback) { 221 const std::vector<GURL>& presentation_urls,
222 const NewSessionCallback& callback) {
225 DVLOG(2) << "StartSession"; 223 DVLOG(2) << "StartSession";
226 if (!delegate_) { 224 if (!delegate_) {
227 callback.Run( 225 callback.Run(
228 blink::mojom::PresentationSessionInfoPtr(), 226 blink::mojom::PresentationSessionInfoPtr(),
229 blink::mojom::PresentationError::From(PresentationError( 227 blink::mojom::PresentationError::From(PresentationError(
230 PRESENTATION_ERROR_NO_AVAILABLE_SCREENS, "No screens found."))); 228 PRESENTATION_ERROR_NO_AVAILABLE_SCREENS, "No screens found.")));
231 return; 229 return;
232 } 230 }
233 231
234 // There is a StartSession request in progress. To avoid queueing up 232 // There is a StartSession request in progress. To avoid queueing up
235 // requests, the incoming request is rejected. 233 // requests, the incoming request is rejected.
236 if (start_session_request_id_ != kInvalidRequestSessionId) { 234 if (start_session_request_id_ != kInvalidRequestSessionId) {
237 InvokeNewSessionCallbackWithError(callback); 235 InvokeNewSessionCallbackWithError(callback);
238 return; 236 return;
239 } 237 }
240 238
239 std::vector<std::string> presentation_urls_for_delegate(
240 presentation_urls.size());
241 std::transform(presentation_urls.begin(), presentation_urls.end(),
242 presentation_urls_for_delegate.begin(),
243 [](const GURL& url) { return url.spec(); });
244
241 start_session_request_id_ = GetNextRequestSessionId(); 245 start_session_request_id_ = GetNextRequestSessionId();
242 pending_start_session_cb_.reset(new NewSessionCallbackWrapper(callback)); 246 pending_start_session_cb_.reset(new NewSessionCallbackWrapper(callback));
243 delegate_->StartSession( 247 delegate_->StartSession(
244 render_process_id_, render_frame_id_, presentation_url, 248 render_process_id_, render_frame_id_, presentation_urls_for_delegate,
245 base::Bind(&PresentationServiceImpl::OnStartSessionSucceeded, 249 base::Bind(&PresentationServiceImpl::OnStartSessionSucceeded,
246 weak_factory_.GetWeakPtr(), start_session_request_id_), 250 weak_factory_.GetWeakPtr(), start_session_request_id_),
247 base::Bind(&PresentationServiceImpl::OnStartSessionError, 251 base::Bind(&PresentationServiceImpl::OnStartSessionError,
248 weak_factory_.GetWeakPtr(), start_session_request_id_)); 252 weak_factory_.GetWeakPtr(), start_session_request_id_));
249 } 253 }
250 254
251 void PresentationServiceImpl::JoinSession( 255 void PresentationServiceImpl::JoinSession(
252 const std::string& presentation_url, 256 const std::vector<GURL>& presentation_urls,
253 const base::Optional<std::string>& presentation_id, 257 const base::Optional<std::string>& presentation_id,
254 const NewSessionCallback& callback) { 258 const NewSessionCallback& callback) {
255 DVLOG(2) << "JoinSession"; 259 DVLOG(2) << "JoinSession";
256 if (!delegate_) { 260 if (!delegate_) {
257 callback.Run(blink::mojom::PresentationSessionInfoPtr(), 261 callback.Run(blink::mojom::PresentationSessionInfoPtr(),
258 blink::mojom::PresentationError::From(PresentationError( 262 blink::mojom::PresentationError::From(PresentationError(
259 PRESENTATION_ERROR_NO_PRESENTATION_FOUND, 263 PRESENTATION_ERROR_NO_PRESENTATION_FOUND,
260 "Error joining route: No matching route"))); 264 "Error joining route: No matching route")));
261 return; 265 return;
262 } 266 }
263 267
268 std::vector<std::string> presentation_urls_for_delegate(
269 presentation_urls.size());
270 std::transform(presentation_urls.begin(), presentation_urls.end(),
271 presentation_urls_for_delegate.begin(),
272 [](const GURL& url) { return url.spec(); });
273
264 int request_session_id = RegisterJoinSessionCallback(callback); 274 int request_session_id = RegisterJoinSessionCallback(callback);
265 if (request_session_id == kInvalidRequestSessionId) { 275 if (request_session_id == kInvalidRequestSessionId) {
266 InvokeNewSessionCallbackWithError(callback); 276 InvokeNewSessionCallbackWithError(callback);
267 return; 277 return;
268 } 278 }
269 delegate_->JoinSession( 279 delegate_->JoinSession(
270 render_process_id_, render_frame_id_, presentation_url, 280 render_process_id_, render_frame_id_, presentation_urls_for_delegate,
271 presentation_id.value_or(std::string()), 281 presentation_id.value_or(std::string()),
272 base::Bind(&PresentationServiceImpl::OnJoinSessionSucceeded, 282 base::Bind(&PresentationServiceImpl::OnJoinSessionSucceeded,
273 weak_factory_.GetWeakPtr(), request_session_id), 283 weak_factory_.GetWeakPtr(), request_session_id),
274 base::Bind(&PresentationServiceImpl::OnJoinSessionError, 284 base::Bind(&PresentationServiceImpl::OnJoinSessionError,
275 weak_factory_.GetWeakPtr(), request_session_id)); 285 weak_factory_.GetWeakPtr(), request_session_id));
276 } 286 }
277 287
278 int PresentationServiceImpl::RegisterJoinSessionCallback( 288 int PresentationServiceImpl::RegisterJoinSessionCallback(
279 const NewSessionCallback& callback) { 289 const NewSessionCallback& callback) {
280 if (pending_join_session_cbs_.size() >= kMaxNumQueuedSessionRequests) 290 if (pending_join_session_cbs_.size() >= kMaxNumQueuedSessionRequests)
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 auto it = pending_join_session_cbs_.find(request_session_id); 360 auto it = pending_join_session_cbs_.find(request_session_id);
351 if (it == pending_join_session_cbs_.end()) 361 if (it == pending_join_session_cbs_.end())
352 return false; 362 return false;
353 363
354 DCHECK(it->second.get()); 364 DCHECK(it->second.get());
355 it->second->Run(std::move(session), std::move(error)); 365 it->second->Run(std::move(session), std::move(error));
356 pending_join_session_cbs_.erase(it); 366 pending_join_session_cbs_.erase(it);
357 return true; 367 return true;
358 } 368 }
359 369
360 void PresentationServiceImpl::SetDefaultPresentationURL( 370 void PresentationServiceImpl::SetDefaultPresentationUrls(
361 const std::string& url) { 371 const std::vector<GURL>& presentation_urls) {
362 DVLOG(2) << "SetDefaultPresentationURL"; 372 DVLOG(2) << "SetDefaultPresentationUrls";
363 if (!delegate_) 373 if (!delegate_)
364 return; 374 return;
365 375
366 if (default_presentation_url_ == url) 376 std::vector<std::string> presentation_urls_for_delegate(
377 presentation_urls.size());
378 std::transform(presentation_urls.begin(), presentation_urls.end(),
379 presentation_urls_for_delegate.begin(),
380 [](const GURL& url) { return url.spec(); });
381
382 if (default_presentation_urls_ == presentation_urls_for_delegate)
367 return; 383 return;
368 384
369 default_presentation_url_ = url; 385 default_presentation_urls_ = presentation_urls_for_delegate;
370 delegate_->SetDefaultPresentationUrl( 386 delegate_->SetDefaultPresentationUrls(
371 render_process_id_, render_frame_id_, url, 387 render_process_id_, render_frame_id_, presentation_urls_for_delegate,
372 base::Bind(&PresentationServiceImpl::OnDefaultPresentationStarted, 388 base::Bind(&PresentationServiceImpl::OnDefaultPresentationStarted,
373 weak_factory_.GetWeakPtr())); 389 weak_factory_.GetWeakPtr()));
374 } 390 }
375 391
376 void PresentationServiceImpl::SendSessionMessage( 392 void PresentationServiceImpl::SendSessionMessage(
377 blink::mojom::PresentationSessionInfoPtr session, 393 blink::mojom::PresentationSessionInfoPtr session,
378 blink::mojom::SessionMessagePtr session_message, 394 blink::mojom::SessionMessagePtr session_message,
379 const SendSessionMessageCallback& callback) { 395 const SendSessionMessageCallback& callback) {
380 DVLOG(2) << "SendSessionMessage"; 396 DVLOG(2) << "SendSessionMessage";
381 DCHECK(!session_message.is_null()); 397 DCHECK(!session_message.is_null());
(...skipping 16 matching lines...) Expand all
398 void PresentationServiceImpl::OnSendMessageCallback(bool sent) { 414 void PresentationServiceImpl::OnSendMessageCallback(bool sent) {
399 // It is possible that Reset() is invoked before receiving this callback. 415 // It is possible that Reset() is invoked before receiving this callback.
400 // So, always check send_message_callback_ for non-null. 416 // So, always check send_message_callback_ for non-null.
401 if (send_message_callback_) { 417 if (send_message_callback_) {
402 send_message_callback_->Run(sent); 418 send_message_callback_->Run(sent);
403 send_message_callback_.reset(); 419 send_message_callback_.reset();
404 } 420 }
405 } 421 }
406 422
407 void PresentationServiceImpl::CloseConnection( 423 void PresentationServiceImpl::CloseConnection(
408 const std::string& presentation_url, 424 const GURL& presentation_url,
409 const std::string& presentation_id) { 425 const std::string& presentation_id) {
410 DVLOG(2) << "CloseConnection " << presentation_id; 426 DVLOG(2) << "CloseConnection " << presentation_id;
411 if (delegate_) 427 if (delegate_)
412 delegate_->CloseConnection(render_process_id_, render_frame_id_, 428 delegate_->CloseConnection(render_process_id_, render_frame_id_,
413 presentation_id); 429 presentation_id);
414 } 430 }
415 431
416 void PresentationServiceImpl::Terminate(const std::string& presentation_url, 432 void PresentationServiceImpl::Terminate(const GURL& presentation_url,
417 const std::string& presentation_id) { 433 const std::string& presentation_id) {
418 DVLOG(2) << "Terminate " << presentation_id; 434 DVLOG(2) << "Terminate " << presentation_id;
419 if (delegate_) 435 if (delegate_)
420 delegate_->Terminate(render_process_id_, render_frame_id_, presentation_id); 436 delegate_->Terminate(render_process_id_, render_frame_id_, presentation_id);
421 } 437 }
422 438
423 void PresentationServiceImpl::OnConnectionStateChanged( 439 void PresentationServiceImpl::OnConnectionStateChanged(
424 const PresentationSessionInfo& connection, 440 const PresentationSessionInfo& connection,
425 const PresentationConnectionStateChangeInfo& info) { 441 const PresentationConnectionStateChangeInfo& info) {
426 DCHECK(client_.get()); 442 DCHECK(client_.get());
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
517 << "should've been deleted during RenderFrameDeleted()."; 533 << "should've been deleted during RenderFrameDeleted().";
518 Reset(); 534 Reset();
519 delete this; 535 delete this;
520 } 536 }
521 537
522 void PresentationServiceImpl::Reset() { 538 void PresentationServiceImpl::Reset() {
523 DVLOG(2) << "PresentationServiceImpl::Reset"; 539 DVLOG(2) << "PresentationServiceImpl::Reset";
524 if (delegate_) 540 if (delegate_)
525 delegate_->Reset(render_process_id_, render_frame_id_); 541 delegate_->Reset(render_process_id_, render_frame_id_);
526 542
527 default_presentation_url_.clear(); 543 default_presentation_urls_.clear();
528 544
529 screen_availability_listeners_.clear(); 545 screen_availability_listeners_.clear();
530 546
531 start_session_request_id_ = kInvalidRequestSessionId; 547 start_session_request_id_ = kInvalidRequestSessionId;
532 pending_start_session_cb_.reset(); 548 pending_start_session_cb_.reset();
533 549
534 pending_join_session_cbs_.clear(); 550 pending_join_session_cbs_.clear();
535 551
536 if (on_session_messages_callback_.get()) { 552 if (on_session_messages_callback_.get()) {
537 on_session_messages_callback_->Run( 553 on_session_messages_callback_->Run(
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
575 ~ScreenAvailabilityListenerImpl() { 591 ~ScreenAvailabilityListenerImpl() {
576 } 592 }
577 593
578 std::string PresentationServiceImpl::ScreenAvailabilityListenerImpl 594 std::string PresentationServiceImpl::ScreenAvailabilityListenerImpl
579 ::GetAvailabilityUrl() const { 595 ::GetAvailabilityUrl() const {
580 return availability_url_; 596 return availability_url_;
581 } 597 }
582 598
583 void PresentationServiceImpl::ScreenAvailabilityListenerImpl 599 void PresentationServiceImpl::ScreenAvailabilityListenerImpl
584 ::OnScreenAvailabilityChanged(bool available) { 600 ::OnScreenAvailabilityChanged(bool available) {
585 service_->client_->OnScreenAvailabilityUpdated(availability_url_, available); 601 service_->client_->OnScreenAvailabilityUpdated(GURL(availability_url_),
602 available);
586 } 603 }
587 604
588 void PresentationServiceImpl::ScreenAvailabilityListenerImpl 605 void PresentationServiceImpl::ScreenAvailabilityListenerImpl
589 ::OnScreenAvailabilityNotSupported() { 606 ::OnScreenAvailabilityNotSupported() {
590 service_->client_->OnScreenAvailabilityNotSupported(availability_url_); 607 service_->client_->OnScreenAvailabilityNotSupported(GURL(availability_url_));
591 } 608 }
592 609
593 PresentationServiceImpl::NewSessionCallbackWrapper 610 PresentationServiceImpl::NewSessionCallbackWrapper
594 ::NewSessionCallbackWrapper(const NewSessionCallback& callback) 611 ::NewSessionCallbackWrapper(const NewSessionCallback& callback)
595 : callback_(callback) { 612 : callback_(callback) {
596 } 613 }
597 614
598 PresentationServiceImpl::NewSessionCallbackWrapper 615 PresentationServiceImpl::NewSessionCallbackWrapper
599 ::~NewSessionCallbackWrapper() { 616 ::~NewSessionCallbackWrapper() {
600 if (!callback_.is_null()) 617 if (!callback_.is_null())
601 InvokeNewSessionCallbackWithError(callback_); 618 InvokeNewSessionCallbackWithError(callback_);
602 } 619 }
603 620
604 void PresentationServiceImpl::NewSessionCallbackWrapper::Run( 621 void PresentationServiceImpl::NewSessionCallbackWrapper::Run(
605 blink::mojom::PresentationSessionInfoPtr session, 622 blink::mojom::PresentationSessionInfoPtr session,
606 blink::mojom::PresentationErrorPtr error) { 623 blink::mojom::PresentationErrorPtr error) {
607 DCHECK(!callback_.is_null()); 624 DCHECK(!callback_.is_null());
608 callback_.Run(std::move(session), std::move(error)); 625 callback_.Run(std::move(session), std::move(error));
609 callback_.Reset(); 626 callback_.Reset();
610 } 627 }
611 628
612 } // namespace content 629 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698