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

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: Compile fix 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);
haibinlu 2015/07/30 23:59:11 "From" uses copy instead of move
imcheng 2015/08/03 18:56:33 We use copy here because the original message (|in
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 input->data.Swap(&output->data);
81 input->data.Swap(output->data.get());
82 return output.Pass(); 75 return output.Pass();
83 } 76 }
84 case presentation::PresentationMessageType:: 77 case presentation::PRESENTATION_MESSAGE_TYPE_BLOB: {
85 PRESENTATION_MESSAGE_TYPE_BLOB: {
86 DCHECK(!input->data.is_null()); 78 DCHECK(!input->data.is_null());
87 DCHECK(input->message.is_null()); 79 DCHECK(input->message.is_null());
88 if (input->data.size() > content::kMaxPresentationSessionMessageSize) 80 if (input->data.size() > content::kMaxPresentationSessionMessageSize)
89 return output.Pass(); 81 return output.Pass();
90 82
91 output = content::PresentationSessionMessage::CreateBlobMessage( 83 output.reset(
92 input->presentation_url, input->presentation_id, 84 new PresentationSessionMessage(PresentationMessageType::BLOB));
93 make_scoped_ptr(new std::vector<uint8_t>)); 85 input->data.Swap(&output->data);
94 input->data.Swap(output->data.get());
95 return output.Pass(); 86 return output.Pass();
96 } 87 }
97 } 88 }
98 89
99 NOTREACHED() << "Invalid presentation message type " << input->type; 90 NOTREACHED() << "Invalid presentation message type " << input->type;
100 return output.Pass(); 91 return output.Pass();
101 } 92 }
102 93
103 void InvokeNewSessionMojoCallbackWithError( 94 void InvokeNewSessionMojoCallbackWithError(
104 const NewSessionMojoCallback& callback) { 95 const NewSessionMojoCallback& callback) {
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 if (screen_availability_listener_.get()) 358 if (screen_availability_listener_.get())
368 ResetScreenAvailabilityListener(new_default_url); 359 ResetScreenAvailabilityListener(new_default_url);
369 360
370 delegate_->SetDefaultPresentationUrl( 361 delegate_->SetDefaultPresentationUrl(
371 render_process_id_, 362 render_process_id_,
372 render_frame_id_, 363 render_frame_id_,
373 default_presentation_url); 364 default_presentation_url);
374 default_presentation_url_ = default_presentation_url; 365 default_presentation_url_ = default_presentation_url;
375 } 366 }
376 367
377
378 void PresentationServiceImpl::SendSessionMessage( 368 void PresentationServiceImpl::SendSessionMessage(
369 presentation::PresentationSessionInfoPtr session,
379 presentation::SessionMessagePtr session_message, 370 presentation::SessionMessagePtr session_message,
380 const SendMessageMojoCallback& callback) { 371 const SendMessageMojoCallback& callback) {
381 DVLOG(2) << "SendSessionMessage"; 372 DVLOG(2) << "SendSessionMessage";
382 DCHECK(!session_message.is_null()); 373 DCHECK(!session_message.is_null());
383 // send_message_callback_ should be null by now, otherwise resetting of 374 // send_message_callback_ should be null by now, otherwise resetting of
384 // send_message_callback_ with new callback will drop the old callback. 375 // send_message_callback_ with new callback will drop the old callback.
385 if (!delegate_ || send_message_callback_) { 376 if (!delegate_ || send_message_callback_) {
386 callback.Run(false); 377 callback.Run(false);
387 return; 378 return;
388 } 379 }
389 380
390 send_message_callback_.reset(new SendMessageMojoCallback(callback)); 381 send_message_callback_.reset(new SendMessageMojoCallback(callback));
391 delegate_->SendMessage( 382 delegate_->SendMessage(
392 render_process_id_, 383 render_process_id_, render_frame_id_,
393 render_frame_id_, 384 session.To<PresentationSessionInfo>(),
394 GetPresentationSessionMessage(session_message.Pass()), 385 GetPresentationSessionMessage(session_message.Pass()),
395 base::Bind(&PresentationServiceImpl::OnSendMessageCallback, 386 base::Bind(&PresentationServiceImpl::OnSendMessageCallback,
396 weak_factory_.GetWeakPtr())); 387 weak_factory_.GetWeakPtr()));
397 } 388 }
398 389
399 void PresentationServiceImpl::OnSendMessageCallback(bool sent) { 390 void PresentationServiceImpl::OnSendMessageCallback(bool sent) {
400 // It is possible that Reset() is invoked before receiving this callback. 391 // It is possible that Reset() is invoked before receiving this callback.
401 // So, always check send_message_callback_ for non-null. 392 // So, always check send_message_callback_ for non-null.
402 if (send_message_callback_) { 393 if (send_message_callback_) {
403 send_message_callback_->Run(sent); 394 send_message_callback_->Run(sent);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 bool PresentationServiceImpl::FrameMatches( 427 bool PresentationServiceImpl::FrameMatches(
437 content::RenderFrameHost* render_frame_host) const { 428 content::RenderFrameHost* render_frame_host) const {
438 if (!render_frame_host) 429 if (!render_frame_host)
439 return false; 430 return false;
440 431
441 return render_frame_host->GetProcess()->GetID() == render_process_id_ && 432 return render_frame_host->GetProcess()->GetID() == render_process_id_ &&
442 render_frame_host->GetRoutingID() == render_frame_id_; 433 render_frame_host->GetRoutingID() == render_frame_id_;
443 } 434 }
444 435
445 void PresentationServiceImpl::ListenForSessionMessages( 436 void PresentationServiceImpl::ListenForSessionMessages(
446 const SessionMessagesCallback& callback) { 437 presentation::PresentationSessionInfoPtr session) {
447 DVLOG(2) << "ListenForSessionMessages"; 438 DVLOG(2) << "ListenForSessionMessages";
448 if (!delegate_) { 439 if (!delegate_)
449 callback.Run(mojo::Array<presentation::SessionMessagePtr>());
450 return; 440 return;
451 }
452 441
453 // Crash early if renderer is misbehaving. 442 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( 443 delegate_->ListenForSessionMessages(
458 render_process_id_, render_frame_id_, 444 render_process_id_, render_frame_id_, session_info,
459 base::Bind(&PresentationServiceImpl::OnSessionMessages, 445 base::Bind(&PresentationServiceImpl::OnSessionMessages,
460 weak_factory_.GetWeakPtr())); 446 weak_factory_.GetWeakPtr(), session_info));
461 } 447 }
462 448
463 void PresentationServiceImpl::OnSessionMessages( 449 void PresentationServiceImpl::OnSessionMessages(
464 scoped_ptr<ScopedVector<PresentationSessionMessage>> messages) { 450 const PresentationSessionInfo& session,
465 if (!on_session_messages_callback_.get()) { 451 const ScopedVector<PresentationSessionMessage>& messages) {
466 // The Reset method of this class was invoked. 452 DCHECK(client_);
467 return;
468 }
469 453
470 if (!messages.get() || messages->empty()) { 454 DVLOG(2) << "OnSessionMessages";
471 // Error handling. Session is either closed or in error state. 455 mojo::Array<presentation::SessionMessagePtr> mojoMessages(messages.size());
472 on_session_messages_callback_->Run( 456 for (size_t i = 0; i < messages.size(); ++i)
473 mojo::Array<presentation::SessionMessagePtr>()); 457 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 458
482 on_session_messages_callback_.reset(); 459 client_->OnSessionMessagesReceived(
460 presentation::PresentationSessionInfo::From(session),
461 mojoMessages.Pass());
483 } 462 }
484 463
485 void PresentationServiceImpl::DidNavigateAnyFrame( 464 void PresentationServiceImpl::DidNavigateAnyFrame(
486 content::RenderFrameHost* render_frame_host, 465 content::RenderFrameHost* render_frame_host,
487 const content::LoadCommittedDetails& details, 466 const content::LoadCommittedDetails& details,
488 const content::FrameNavigateParams& params) { 467 const content::FrameNavigateParams& params) {
489 DVLOG(2) << "PresentationServiceImpl::DidNavigateAnyFrame"; 468 DVLOG(2) << "PresentationServiceImpl::DidNavigateAnyFrame";
490 if (!FrameMatches(render_frame_host)) 469 if (!FrameMatches(render_frame_host))
491 return; 470 return;
492 471
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
637 616
638 void PresentationServiceImpl::DefaultSessionStartContext::Reset() { 617 void PresentationServiceImpl::DefaultSessionStartContext::Reset() {
639 ScopedVector<DefaultSessionMojoCallback> callbacks; 618 ScopedVector<DefaultSessionMojoCallback> callbacks;
640 callbacks.swap(callbacks_); 619 callbacks.swap(callbacks_);
641 for (const auto& callback : callbacks) 620 for (const auto& callback : callbacks)
642 callback->Run(presentation::PresentationSessionInfoPtr()); 621 callback->Run(presentation::PresentationSessionInfoPtr());
643 session_.reset(); 622 session_.reset();
644 } 623 }
645 624
646 } // namespace content 625 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698