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

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

Issue 1037483003: [PresentationAPI] Implementing send() from WebPresentationClient to the PresentationService. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: No (c) in new files Copyright message. Created 5 years, 7 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"
11 #include "content/public/browser/content_browser_client.h" 11 #include "content/public/browser/content_browser_client.h"
12 #include "content/public/browser/navigation_details.h" 12 #include "content/public/browser/navigation_details.h"
13 #include "content/public/browser/presentation_session_message.h" 13 #include "content/public/browser/presentation_session_message.h"
14 #include "content/public/browser/render_frame_host.h" 14 #include "content/public/browser/render_frame_host.h"
15 #include "content/public/browser/render_process_host.h" 15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/web_contents.h" 16 #include "content/public/browser/web_contents.h"
17 #include "content/public/common/content_client.h" 17 #include "content/public/common/content_client.h"
18 #include "content/public/common/frame_navigate_params.h" 18 #include "content/public/common/frame_navigate_params.h"
19 #include "content/public/common/presentation_constants.h"
19 20
20 namespace { 21 namespace {
21 22
22 // The return value takes ownership of the contents of |input|. 23 // The return value takes ownership of the contents of |input|.
23 presentation::SessionMessagePtr ToMojoSessionMessage( 24 presentation::SessionMessagePtr ToMojoSessionMessage(
24 content::PresentationSessionMessage* input) { 25 content::PresentationSessionMessage* input) {
25 presentation::SessionMessagePtr output(presentation::SessionMessage::New()); 26 presentation::SessionMessagePtr output(presentation::SessionMessage::New());
26 output->presentation_url.Swap(&input->presentation_url); 27 output->presentation_url.Swap(&input->presentation_url);
27 output->presentation_id.Swap(&input->presentation_id); 28 output->presentation_id.Swap(&input->presentation_id);
28 if (input->is_binary()) { 29 if (input->is_binary()) {
29 // binary data 30 // binary data
30 output->type = presentation::PresentationMessageType:: 31 output->type = presentation::PresentationMessageType::
31 PRESENTATION_MESSAGE_TYPE_ARRAY_BUFFER; 32 PRESENTATION_MESSAGE_TYPE_ARRAY_BUFFER;
32 output->data.Swap(input->data.get()); 33 output->data.Swap(input->data.get());
33 } else { 34 } else {
34 // string message 35 // string message
35 output->type = 36 output->type =
36 presentation::PresentationMessageType::PRESENTATION_MESSAGE_TYPE_TEXT; 37 presentation::PresentationMessageType::PRESENTATION_MESSAGE_TYPE_TEXT;
37 output->message.Swap(input->message.get()); 38 output->message.Swap(input->message.get());
38 } 39 }
39 return output.Pass(); 40 return output.Pass();
40 } 41 }
41 42
42 } // namespace 43 scoped_ptr<content::PresentationSessionMessage> GetPresentationSessionMessage(
44 presentation::SessionMessagePtr input) {
45 DCHECK(!input.is_null());
46 scoped_ptr<content::PresentationSessionMessage> output;
47 if (input->type == presentation::PresentationMessageType::
48 PRESENTATION_MESSAGE_TYPE_TEXT) {
49 DCHECK(!input->message.is_null());
50 DCHECK(input->data.is_null());
51 // Return null PresentationSessionMessage if size exceeds.
52 if (input->message.size() > content::kMaxPresentationSessionMessageSize)
53 return output.Pass();
54
55 output = content::PresentationSessionMessage::CreateStringMessage(
56 input->presentation_url,
57 input->presentation_id,
58 make_scoped_ptr(new std::string));
59 input->message.Swap(output->message.get());
60
61 } else if (input->type == presentation::PresentationMessageType::
62 PRESENTATION_MESSAGE_TYPE_ARRAY_BUFFER) {
63 DCHECK(!input->data.is_null());
64 DCHECK(input->message.is_null());
65 // Return null PresentationSessionMessage if size exceeds.
66 if (input->data.size() > content::kMaxPresentationSessionMessageSize)
67 return output.Pass();
68
69 output = content::PresentationSessionMessage::CreateBinaryMessage(
70 input->presentation_url,
71 input->presentation_id,
72 make_scoped_ptr(new std::vector<uint8_t>));
73 input->data.Swap(output->data.get());
74 }
75
76 return output.Pass();
77 }
78
79 } // namespace
43 80
44 namespace content { 81 namespace content {
45 82
46 PresentationServiceImpl::PresentationServiceImpl( 83 PresentationServiceImpl::PresentationServiceImpl(
47 RenderFrameHost* render_frame_host, 84 RenderFrameHost* render_frame_host,
48 WebContents* web_contents, 85 WebContents* web_contents,
49 PresentationServiceDelegate* delegate) 86 PresentationServiceDelegate* delegate)
50 : WebContentsObserver(web_contents), 87 : WebContentsObserver(web_contents),
51 delegate_(delegate), 88 delegate_(delegate),
52 is_start_session_pending_(false), 89 is_start_session_pending_(false),
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 368
332 // Remove listener for old default presentation URL. 369 // Remove listener for old default presentation URL.
333 delegate_->RemoveScreenAvailabilityListener( 370 delegate_->RemoveScreenAvailabilityListener(
334 render_process_id_, 371 render_process_id_,
335 render_frame_id_, 372 render_frame_id_,
336 old_it->second.get()); 373 old_it->second.get());
337 availability_contexts_.erase(old_it); 374 availability_contexts_.erase(old_it);
338 DoSetDefaultPresentationUrl(new_default_url, default_presentation_id); 375 DoSetDefaultPresentationUrl(new_default_url, default_presentation_id);
339 } 376 }
340 377
378
379 void PresentationServiceImpl::SendSessionMessage(
380 presentation::SessionMessagePtr session_message,
381 const SendMessageMojoCallback& callback) {
382 DVLOG(2) << "SendSessionMessage";
383 DCHECK(!session_message.is_null());
384 // send_message_callback_ should be null by now, otherwise resetting of
385 // send_message_callback_ with new callback will drop the old callback.
386 if (!delegate_ || send_message_callback_) {
387 callback.Run(false);
388 return;
389 }
390
391 send_message_callback_.reset(new SendMessageMojoCallback(callback));
392 delegate_->SendMessage(
393 render_process_id_,
394 render_frame_id_,
395 GetPresentationSessionMessage(session_message.Pass()),
396 base::Bind(&PresentationServiceImpl::OnSendMessageCallback,
397 weak_factory_.GetWeakPtr()));
398 }
399
400 void PresentationServiceImpl::OnSendMessageCallback() {
401 // It is possible that Reset() is invoked before receiving this callback.
402 // So, always check send_message_callback_ for non-null.
403 if (send_message_callback_) {
404 send_message_callback_->Run(true);
405 send_message_callback_.reset();
406 }
407 }
408
341 void PresentationServiceImpl::CloseSession( 409 void PresentationServiceImpl::CloseSession(
342 const mojo::String& presentation_url, 410 const mojo::String& presentation_url,
343 const mojo::String& presentation_id) { 411 const mojo::String& presentation_id) {
344 NOTIMPLEMENTED(); 412 NOTIMPLEMENTED();
345 } 413 }
346 414
347 void PresentationServiceImpl::ListenForSessionStateChange( 415 void PresentationServiceImpl::ListenForSessionStateChange(
348 const SessionStateCallback& callback) { 416 const SessionStateCallback& callback) {
349 NOTIMPLEMENTED(); 417 NOTIMPLEMENTED();
350 } 418 }
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 default_presentation_id_.clear(); 509 default_presentation_id_.clear();
442 availability_contexts_.clear(); 510 availability_contexts_.clear();
443 queued_start_session_requests_.clear(); 511 queued_start_session_requests_.clear();
444 FlushNewSessionCallbacks(); 512 FlushNewSessionCallbacks();
445 default_session_start_context_.reset(); 513 default_session_start_context_.reset();
446 if (on_session_messages_callback_.get()) { 514 if (on_session_messages_callback_.get()) {
447 on_session_messages_callback_->Run( 515 on_session_messages_callback_->Run(
448 mojo::Array<presentation::SessionMessagePtr>()); 516 mojo::Array<presentation::SessionMessagePtr>());
449 on_session_messages_callback_.reset(); 517 on_session_messages_callback_.reset();
450 } 518 }
519 if (send_message_callback_) {
520 // Run the callback with false, indicating the renderer to stop sending
521 // the requests and invalidate all pending requests.
522 send_message_callback_->Run(false);
523 send_message_callback_.reset();
524 }
451 } 525 }
452 526
453 // static 527 // static
454 void PresentationServiceImpl::InvokeNewSessionMojoCallbackWithError( 528 void PresentationServiceImpl::InvokeNewSessionMojoCallbackWithError(
455 const NewSessionMojoCallback& callback) { 529 const NewSessionMojoCallback& callback) {
456 callback.Run( 530 callback.Run(
457 presentation::PresentationSessionInfoPtr(), 531 presentation::PresentationSessionInfoPtr(),
458 presentation::PresentationError::From( 532 presentation::PresentationError::From(
459 PresentationError(PRESENTATION_ERROR_UNKNOWN, "Internal error"))); 533 PresentationError(PRESENTATION_ERROR_UNKNOWN, "Internal error")));
460 } 534 }
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 void PresentationServiceImpl::DefaultSessionStartContext::Reset() { 664 void PresentationServiceImpl::DefaultSessionStartContext::Reset() {
591 ScopedVector<DefaultSessionMojoCallback> callbacks; 665 ScopedVector<DefaultSessionMojoCallback> callbacks;
592 callbacks.swap(callbacks_); 666 callbacks.swap(callbacks_);
593 for (const auto& callback : callbacks) 667 for (const auto& callback : callbacks)
594 callback->Run(presentation::PresentationSessionInfoPtr()); 668 callback->Run(presentation::PresentationSessionInfoPtr());
595 session_.reset(); 669 session_.reset();
596 } 670 }
597 671
598 } // namespace content 672 } // namespace content
599 673
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698