OLD | NEW |
---|---|
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/render_frame_host.h" | 14 #include "content/public/browser/render_frame_host.h" |
14 #include "content/public/browser/render_process_host.h" | 15 #include "content/public/browser/render_process_host.h" |
15 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
16 #include "content/public/common/content_client.h" | 17 #include "content/public/common/content_client.h" |
17 #include "content/public/common/frame_navigate_params.h" | 18 #include "content/public/common/frame_navigate_params.h" |
18 | 19 |
20 namespace { | |
21 | |
22 // The return value takes ownership of the actual message of |input|. | |
mark a. foltz
2015/05/05 00:22:13
s/the actual message/the contents/
haibinlu
2015/05/05 00:42:15
Done.
| |
23 presentation::SessionMessagePtr ToMojoSessionMessage( | |
24 content::PresentationSessionMessage* input) { | |
25 presentation::SessionMessagePtr output(presentation::SessionMessage::New()); | |
26 output->presentation_url = input->presentation_url; | |
mark a. foltz
2015/05/05 00:22:13
Use std::swap() here, so that we avoid copying and
haibinlu
2015/05/05 00:42:15
Done.
| |
27 output->presentation_id = input->presentation_id; | |
mark a. foltz
2015/05/05 00:22:13
Ditto
haibinlu
2015/05/05 00:42:15
Done.
| |
28 if (input->is_binary()) { | |
29 // binary data | |
30 output->type = presentation::PresentationMessageType:: | |
31 PRESENTATION_MESSAGE_TYPE_ARRAY_BUFFER; | |
32 output->data.Swap(input->data.get()); | |
33 } else { | |
34 // string message | |
35 output->type = | |
36 presentation::PresentationMessageType::PRESENTATION_MESSAGE_TYPE_TEXT; | |
37 output->message.Swap(input->message.get()); | |
38 } | |
39 return output.Pass(); | |
40 } | |
41 | |
42 } // namespace | |
43 | |
19 namespace content { | 44 namespace content { |
20 | 45 |
21 PresentationServiceImpl::PresentationServiceImpl( | 46 PresentationServiceImpl::PresentationServiceImpl( |
22 RenderFrameHost* render_frame_host, | 47 RenderFrameHost* render_frame_host, |
23 WebContents* web_contents, | 48 WebContents* web_contents, |
24 PresentationServiceDelegate* delegate) | 49 PresentationServiceDelegate* delegate) |
25 : WebContentsObserver(web_contents), | 50 : WebContentsObserver(web_contents), |
26 delegate_(delegate), | 51 delegate_(delegate), |
27 is_start_session_pending_(false), | 52 is_start_session_pending_(false), |
28 next_request_session_id_(0), | 53 next_request_session_id_(0), |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
328 content::RenderFrameHost* render_frame_host) const { | 353 content::RenderFrameHost* render_frame_host) const { |
329 if (!render_frame_host) | 354 if (!render_frame_host) |
330 return false; | 355 return false; |
331 | 356 |
332 return render_frame_host->GetProcess()->GetID() == render_process_id_ && | 357 return render_frame_host->GetProcess()->GetID() == render_process_id_ && |
333 render_frame_host->GetRoutingID() == render_frame_id_; | 358 render_frame_host->GetRoutingID() == render_frame_id_; |
334 } | 359 } |
335 | 360 |
336 void PresentationServiceImpl::ListenForSessionMessages( | 361 void PresentationServiceImpl::ListenForSessionMessages( |
337 const SessionMessagesCallback& callback) { | 362 const SessionMessagesCallback& callback) { |
338 NOTIMPLEMENTED(); | 363 DVLOG(2) << "ListenForSessionMessages"; |
364 if (!delegate_) { | |
365 callback.Run(mojo::Array<presentation::SessionMessagePtr>()); | |
mark a. foltz
2015/05/05 00:22:13
Does |callback| also need to be reset() here?
haibinlu
2015/05/05 00:42:15
no. callback is a const.
| |
366 return; | |
367 } | |
368 | |
369 // Crash early if renderer is misbehaving. | |
370 CHECK(!on_session_messages_callback_.get()); | |
371 | |
372 on_session_messages_callback_.reset(new SessionMessagesCallback(callback)); | |
373 delegate_->ListenForSessionMessages( | |
374 render_process_id_, render_frame_id_, | |
375 base::Bind(&PresentationServiceImpl::OnSessionMessages, | |
376 weak_factory_.GetWeakPtr())); | |
377 } | |
378 | |
379 void PresentationServiceImpl::OnSessionMessages( | |
380 scoped_ptr<ScopedVector<PresentationSessionMessage>> messages) { | |
381 DCHECK(messages.get() && !messages->empty()); | |
382 if (!on_session_messages_callback_.get()) { | |
383 // The Reset method of this class was invoked. | |
384 return; | |
385 } | |
386 | |
387 mojo::Array<presentation::SessionMessagePtr> mojoMessages(messages->size()); | |
388 for (size_t i = 0; i < messages->size(); ++i) { | |
389 mojoMessages[i] = ToMojoSessionMessage((*messages)[i]); | |
390 } | |
391 | |
392 on_session_messages_callback_->Run(mojoMessages.Pass()); | |
393 on_session_messages_callback_.reset(); | |
339 } | 394 } |
340 | 395 |
341 void PresentationServiceImpl::DidNavigateAnyFrame( | 396 void PresentationServiceImpl::DidNavigateAnyFrame( |
342 content::RenderFrameHost* render_frame_host, | 397 content::RenderFrameHost* render_frame_host, |
343 const content::LoadCommittedDetails& details, | 398 const content::LoadCommittedDetails& details, |
344 const content::FrameNavigateParams& params) { | 399 const content::FrameNavigateParams& params) { |
345 DVLOG(2) << "PresentationServiceImpl::DidNavigateAnyFrame"; | 400 DVLOG(2) << "PresentationServiceImpl::DidNavigateAnyFrame"; |
346 if (!FrameMatches(render_frame_host)) | 401 if (!FrameMatches(render_frame_host)) |
347 return; | 402 return; |
348 | 403 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
381 DVLOG(2) << "PresentationServiceImpl::Reset"; | 436 DVLOG(2) << "PresentationServiceImpl::Reset"; |
382 if (delegate_) | 437 if (delegate_) |
383 delegate_->Reset(render_process_id_, render_frame_id_); | 438 delegate_->Reset(render_process_id_, render_frame_id_); |
384 | 439 |
385 default_presentation_url_.clear(); | 440 default_presentation_url_.clear(); |
386 default_presentation_id_.clear(); | 441 default_presentation_id_.clear(); |
387 availability_contexts_.clear(); | 442 availability_contexts_.clear(); |
388 queued_start_session_requests_.clear(); | 443 queued_start_session_requests_.clear(); |
389 FlushNewSessionCallbacks(); | 444 FlushNewSessionCallbacks(); |
390 default_session_start_context_.reset(); | 445 default_session_start_context_.reset(); |
446 if (on_session_messages_callback_.get()) { | |
447 on_session_messages_callback_->Run( | |
448 mojo::Array<presentation::SessionMessagePtr>()); | |
449 on_session_messages_callback_.reset(); | |
450 } | |
391 } | 451 } |
392 | 452 |
393 // static | 453 // static |
394 void PresentationServiceImpl::InvokeNewSessionMojoCallbackWithError( | 454 void PresentationServiceImpl::InvokeNewSessionMojoCallbackWithError( |
395 const NewSessionMojoCallback& callback) { | 455 const NewSessionMojoCallback& callback) { |
396 callback.Run( | 456 callback.Run( |
397 presentation::PresentationSessionInfoPtr(), | 457 presentation::PresentationSessionInfoPtr(), |
398 presentation::PresentationError::From( | 458 presentation::PresentationError::From( |
399 PresentationError(PRESENTATION_ERROR_UNKNOWN, "Internal error"))); | 459 PresentationError(PRESENTATION_ERROR_UNKNOWN, "Internal error"))); |
400 } | 460 } |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
530 void PresentationServiceImpl::DefaultSessionStartContext::Reset() { | 590 void PresentationServiceImpl::DefaultSessionStartContext::Reset() { |
531 ScopedVector<DefaultSessionMojoCallback> callbacks; | 591 ScopedVector<DefaultSessionMojoCallback> callbacks; |
532 callbacks.swap(callbacks_); | 592 callbacks.swap(callbacks_); |
533 for (const auto& callback : callbacks) | 593 for (const auto& callback : callbacks) |
534 callback->Run(presentation::PresentationSessionInfoPtr()); | 594 callback->Run(presentation::PresentationSessionInfoPtr()); |
535 session_.reset(); | 595 session_.reset(); |
536 } | 596 } |
537 | 597 |
538 } // namespace content | 598 } // namespace content |
539 | 599 |
OLD | NEW |