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

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

Issue 1259073004: [Presentation API] Change ListenForSessionMessages API to client-style. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update comments Created 5 years, 4 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 <algorithm> 7 #include <algorithm>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "content/browser/presentation/presentation_type_converters.h" 10 #include "content/browser/presentation/presentation_type_converters.h"
(...skipping 11 matching lines...) Expand all
22 22
23 namespace { 23 namespace {
24 24
25 const int kInvalidRequestSessionId = -1; 25 const int kInvalidRequestSessionId = -1;
26 26
27 int GetNextRequestSessionId() { 27 int GetNextRequestSessionId() {
28 static int next_request_session_id = 0; 28 static int next_request_session_id = 0;
29 return ++next_request_session_id; 29 return ++next_request_session_id;
30 } 30 }
31 31
32 // The return value takes ownership of the contents of |input|.
33 presentation::SessionMessagePtr ToMojoSessionMessage( 32 presentation::SessionMessagePtr ToMojoSessionMessage(
34 content::PresentationSessionMessage* input) { 33 const content::PresentationSessionMessage& input) {
35 presentation::SessionMessagePtr output(presentation::SessionMessage::New()); 34 presentation::SessionMessagePtr output(presentation::SessionMessage::New());
36 output->presentation_url.Swap(&input->presentation_url); 35 if (input.is_binary()) {
37 output->presentation_id.Swap(&input->presentation_id);
38 if (input->is_binary()) {
39 // binary data 36 // binary data
40 output->type = presentation::PresentationMessageType:: 37 output->type = presentation::PresentationMessageType::
41 PRESENTATION_MESSAGE_TYPE_ARRAY_BUFFER; 38 PRESENTATION_MESSAGE_TYPE_ARRAY_BUFFER;
42 output->data.Swap(input->data.get()); 39 output->data = mojo::Array<uint8_t>::From(*input.data);
43 } else { 40 } else {
44 // string message 41 // string message
45 output->type = 42 output->type =
46 presentation::PresentationMessageType::PRESENTATION_MESSAGE_TYPE_TEXT; 43 presentation::PresentationMessageType::PRESENTATION_MESSAGE_TYPE_TEXT;
47 output->message.Swap(input->message.get()); 44 output->message = input.message;
48 } 45 }
49 return output.Pass(); 46 return output.Pass();
50 } 47 }
51 48
52 scoped_ptr<content::PresentationSessionMessage> GetPresentationSessionMessage( 49 scoped_ptr<PresentationSessionMessage> GetPresentationSessionMessage(
53 presentation::SessionMessagePtr input) { 50 presentation::SessionMessagePtr input) {
54 DCHECK(!input.is_null()); 51 DCHECK(!input.is_null());
55 scoped_ptr<content::PresentationSessionMessage> output; 52 scoped_ptr<content::PresentationSessionMessage> output;
56 switch (input->type) { 53 switch (input->type) {
57 case presentation::PresentationMessageType:: 54 case presentation::PRESENTATION_MESSAGE_TYPE_TEXT: {
58 PRESENTATION_MESSAGE_TYPE_TEXT: {
59 DCHECK(!input->message.is_null()); 55 DCHECK(!input->message.is_null());
60 DCHECK(input->data.is_null()); 56 DCHECK(input->data.is_null());
61 // Return null PresentationSessionMessage if size exceeds. 57 // Return null PresentationSessionMessage if size exceeds.
62 if (input->message.size() > content::kMaxPresentationSessionMessageSize) 58 if (input->message.size() > content::kMaxPresentationSessionMessageSize)
63 return output.Pass(); 59 return output.Pass();
64 60
65 output = content::PresentationSessionMessage::CreateStringMessage( 61 output.reset(
66 input->presentation_url, input->presentation_id, 62 new PresentationSessionMessage(PresentationMessageType::TEXT));
67 make_scoped_ptr(new std::string)); 63 input->message.Swap(&output->message);
68 input->message.Swap(output->message.get());
69 return output.Pass(); 64 return output.Pass();
70 } 65 }
71 case presentation::PresentationMessageType:: 66 case presentation::PRESENTATION_MESSAGE_TYPE_ARRAY_BUFFER: {
72 PRESENTATION_MESSAGE_TYPE_ARRAY_BUFFER: {
73 DCHECK(!input->data.is_null()); 67 DCHECK(!input->data.is_null());
74 DCHECK(input->message.is_null()); 68 DCHECK(input->message.is_null());
75 if (input->data.size() > content::kMaxPresentationSessionMessageSize) 69 if (input->data.size() > content::kMaxPresentationSessionMessageSize)
76 return output.Pass(); 70 return output.Pass();
77 71
78 output = content::PresentationSessionMessage::CreateArrayBufferMessage( 72 output.reset(new PresentationSessionMessage(
79 input->presentation_url, input->presentation_id, 73 PresentationMessageType::ARRAY_BUFFER));
80 make_scoped_ptr(new std::vector<uint8_t>)); 74 output->data.reset(new std::vector<uint8_t>);
81 input->data.Swap(output->data.get()); 75 input->data.Swap(output->data.get());
82 return output.Pass(); 76 return output.Pass();
83 } 77 }
84 case presentation::PresentationMessageType:: 78 case presentation::PRESENTATION_MESSAGE_TYPE_BLOB: {
85 PRESENTATION_MESSAGE_TYPE_BLOB: {
86 DCHECK(!input->data.is_null()); 79 DCHECK(!input->data.is_null());
87 DCHECK(input->message.is_null()); 80 DCHECK(input->message.is_null());
88 if (input->data.size() > content::kMaxPresentationSessionMessageSize) 81 if (input->data.size() > content::kMaxPresentationSessionMessageSize)
89 return output.Pass(); 82 return output.Pass();
90 83
91 output = content::PresentationSessionMessage::CreateBlobMessage( 84 output.reset(
92 input->presentation_url, input->presentation_id, 85 new PresentationSessionMessage(PresentationMessageType::BLOB));
93 make_scoped_ptr(new std::vector<uint8_t>)); 86 output->data.reset(new std::vector<uint8_t>);
94 input->data.Swap(output->data.get()); 87 input->data.Swap(output->data.get());
95 return output.Pass(); 88 return output.Pass();
96 } 89 }
97 } 90 }
98 91
99 NOTREACHED() << "Invalid presentation message type " << input->type; 92 NOTREACHED() << "Invalid presentation message type " << input->type;
100 return output.Pass(); 93 return output.Pass();
101 } 94 }
102 95
103 void InvokeNewSessionMojoCallbackWithError( 96 void InvokeNewSessionMojoCallbackWithError(
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 if (screen_availability_listener_.get()) 360 if (screen_availability_listener_.get())
368 ResetScreenAvailabilityListener(new_default_url); 361 ResetScreenAvailabilityListener(new_default_url);
369 362
370 delegate_->SetDefaultPresentationUrl( 363 delegate_->SetDefaultPresentationUrl(
371 render_process_id_, 364 render_process_id_,
372 render_frame_id_, 365 render_frame_id_,
373 default_presentation_url); 366 default_presentation_url);
374 default_presentation_url_ = default_presentation_url; 367 default_presentation_url_ = default_presentation_url;
375 } 368 }
376 369
377
378 void PresentationServiceImpl::SendSessionMessage( 370 void PresentationServiceImpl::SendSessionMessage(
371 presentation::PresentationSessionInfoPtr session,
379 presentation::SessionMessagePtr session_message, 372 presentation::SessionMessagePtr session_message,
380 const SendMessageMojoCallback& callback) { 373 const SendMessageMojoCallback& callback) {
381 DVLOG(2) << "SendSessionMessage"; 374 DVLOG(2) << "SendSessionMessage";
382 DCHECK(!session_message.is_null()); 375 DCHECK(!session_message.is_null());
383 // send_message_callback_ should be null by now, otherwise resetting of 376 // send_message_callback_ should be null by now, otherwise resetting of
384 // send_message_callback_ with new callback will drop the old callback. 377 // send_message_callback_ with new callback will drop the old callback.
385 if (!delegate_ || send_message_callback_) { 378 if (!delegate_ || send_message_callback_) {
386 callback.Run(false); 379 callback.Run(false);
387 return; 380 return;
388 } 381 }
389 382
390 send_message_callback_.reset(new SendMessageMojoCallback(callback)); 383 send_message_callback_.reset(new SendMessageMojoCallback(callback));
391 delegate_->SendMessage( 384 delegate_->SendMessage(
392 render_process_id_, 385 render_process_id_, render_frame_id_,
393 render_frame_id_, 386 session.To<PresentationSessionInfo>(),
394 GetPresentationSessionMessage(session_message.Pass()), 387 GetPresentationSessionMessage(session_message.Pass()),
395 base::Bind(&PresentationServiceImpl::OnSendMessageCallback, 388 base::Bind(&PresentationServiceImpl::OnSendMessageCallback,
396 weak_factory_.GetWeakPtr())); 389 weak_factory_.GetWeakPtr()));
397 } 390 }
398 391
399 void PresentationServiceImpl::OnSendMessageCallback(bool sent) { 392 void PresentationServiceImpl::OnSendMessageCallback(bool sent) {
400 // It is possible that Reset() is invoked before receiving this callback. 393 // It is possible that Reset() is invoked before receiving this callback.
401 // So, always check send_message_callback_ for non-null. 394 // So, always check send_message_callback_ for non-null.
402 if (send_message_callback_) { 395 if (send_message_callback_) {
403 send_message_callback_->Run(sent); 396 send_message_callback_->Run(sent);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 bool PresentationServiceImpl::FrameMatches( 429 bool PresentationServiceImpl::FrameMatches(
437 content::RenderFrameHost* render_frame_host) const { 430 content::RenderFrameHost* render_frame_host) const {
438 if (!render_frame_host) 431 if (!render_frame_host)
439 return false; 432 return false;
440 433
441 return render_frame_host->GetProcess()->GetID() == render_process_id_ && 434 return render_frame_host->GetProcess()->GetID() == render_process_id_ &&
442 render_frame_host->GetRoutingID() == render_frame_id_; 435 render_frame_host->GetRoutingID() == render_frame_id_;
443 } 436 }
444 437
445 void PresentationServiceImpl::ListenForSessionMessages( 438 void PresentationServiceImpl::ListenForSessionMessages(
446 const SessionMessagesCallback& callback) { 439 presentation::PresentationSessionInfoPtr session) {
447 DVLOG(2) << "ListenForSessionMessages"; 440 DVLOG(2) << "ListenForSessionMessages";
448 if (!delegate_) { 441 if (!delegate_)
449 callback.Run(mojo::Array<presentation::SessionMessagePtr>());
450 return; 442 return;
451 }
452 443
453 // Crash early if renderer is misbehaving. 444 PresentationSessionInfo session_info(session.To<PresentationSessionInfo>());
454 CHECK(!on_session_messages_callback_.get());
455
456 on_session_messages_callback_.reset(new SessionMessagesCallback(callback));
457 delegate_->ListenForSessionMessages( 445 delegate_->ListenForSessionMessages(
458 render_process_id_, render_frame_id_, 446 render_process_id_, render_frame_id_, session_info,
459 base::Bind(&PresentationServiceImpl::OnSessionMessages, 447 base::Bind(&PresentationServiceImpl::OnSessionMessages,
460 weak_factory_.GetWeakPtr())); 448 weak_factory_.GetWeakPtr(), session_info));
461 } 449 }
462 450
463 void PresentationServiceImpl::OnSessionMessages( 451 void PresentationServiceImpl::OnSessionMessages(
464 scoped_ptr<ScopedVector<PresentationSessionMessage>> messages) { 452 const PresentationSessionInfo& session,
465 if (!on_session_messages_callback_.get()) { 453 const ScopedVector<PresentationSessionMessage>& messages) {
466 // The Reset method of this class was invoked. 454 DCHECK(client_);
467 return;
468 }
469 455
470 if (!messages.get() || messages->empty()) { 456 DVLOG(2) << "OnSessionMessages";
471 // Error handling. Session is either closed or in error state. 457 mojo::Array<presentation::SessionMessagePtr> mojoMessages(messages.size());
472 on_session_messages_callback_->Run( 458 for (size_t i = 0; i < messages.size(); ++i)
473 mojo::Array<presentation::SessionMessagePtr>()); 459 mojoMessages[i] = ToMojoSessionMessage(*messages[i]);
474 } else {
475 mojo::Array<presentation::SessionMessagePtr> mojoMessages(messages->size());
476 for (size_t i = 0; i < messages->size(); ++i) {
477 mojoMessages[i] = ToMojoSessionMessage((*messages)[i]);
478 }
479 on_session_messages_callback_->Run(mojoMessages.Pass());
480 }
481 460
482 on_session_messages_callback_.reset(); 461 client_->OnSessionMessagesReceived(
462 presentation::PresentationSessionInfo::From(session),
463 mojoMessages.Pass());
483 } 464 }
484 465
485 void PresentationServiceImpl::DidNavigateAnyFrame( 466 void PresentationServiceImpl::DidNavigateAnyFrame(
486 content::RenderFrameHost* render_frame_host, 467 content::RenderFrameHost* render_frame_host,
487 const content::LoadCommittedDetails& details, 468 const content::LoadCommittedDetails& details,
488 const content::FrameNavigateParams& params) { 469 const content::FrameNavigateParams& params) {
489 DVLOG(2) << "PresentationServiceImpl::DidNavigateAnyFrame"; 470 DVLOG(2) << "PresentationServiceImpl::DidNavigateAnyFrame";
490 if (!FrameMatches(render_frame_host)) 471 if (!FrameMatches(render_frame_host))
491 return; 472 return;
492 473
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
637 618
638 void PresentationServiceImpl::DefaultSessionStartContext::Reset() { 619 void PresentationServiceImpl::DefaultSessionStartContext::Reset() {
639 ScopedVector<DefaultSessionMojoCallback> callbacks; 620 ScopedVector<DefaultSessionMojoCallback> callbacks;
640 callbacks.swap(callbacks_); 621 callbacks.swap(callbacks_);
641 for (const auto& callback : callbacks) 622 for (const auto& callback : callbacks)
642 callback->Run(presentation::PresentationSessionInfoPtr()); 623 callback->Run(presentation::PresentationSessionInfoPtr());
643 session_.reset(); 624 session_.reset();
644 } 625 }
645 626
646 } // namespace content 627 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698