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 <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | |
10 #include <algorithm> | 9 #include <algorithm> |
11 #include <string> | 10 #include <string> |
| 11 #include <utility> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
16 #include "content/browser/presentation/presentation_type_converters.h" | 16 #include "content/browser/presentation/presentation_type_converters.h" |
17 #include "content/public/browser/content_browser_client.h" | 17 #include "content/public/browser/content_browser_client.h" |
18 #include "content/public/browser/navigation_details.h" | 18 #include "content/public/browser/navigation_details.h" |
19 #include "content/public/browser/presentation_session_message.h" | 19 #include "content/public/browser/presentation_session_message.h" |
20 #include "content/public/browser/render_frame_host.h" | 20 #include "content/public/browser/render_frame_host.h" |
21 #include "content/public/browser/render_process_host.h" | 21 #include "content/public/browser/render_process_host.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 } else { | 57 } else { |
58 // string message | 58 // string message |
59 output->type = | 59 output->type = |
60 presentation::PresentationMessageType::PRESENTATION_MESSAGE_TYPE_TEXT; | 60 presentation::PresentationMessageType::PRESENTATION_MESSAGE_TYPE_TEXT; |
61 if (pass_ownership) { | 61 if (pass_ownership) { |
62 output->message.Swap(&input->message); | 62 output->message.Swap(&input->message); |
63 } else { | 63 } else { |
64 output->message = input->message; | 64 output->message = input->message; |
65 } | 65 } |
66 } | 66 } |
67 return output.Pass(); | 67 return output; |
68 } | 68 } |
69 | 69 |
70 scoped_ptr<PresentationSessionMessage> GetPresentationSessionMessage( | 70 scoped_ptr<PresentationSessionMessage> GetPresentationSessionMessage( |
71 presentation::SessionMessagePtr input) { | 71 presentation::SessionMessagePtr input) { |
72 DCHECK(!input.is_null()); | 72 DCHECK(!input.is_null()); |
73 scoped_ptr<content::PresentationSessionMessage> output; | 73 scoped_ptr<content::PresentationSessionMessage> output; |
74 switch (input->type) { | 74 switch (input->type) { |
75 case presentation::PRESENTATION_MESSAGE_TYPE_TEXT: { | 75 case presentation::PRESENTATION_MESSAGE_TYPE_TEXT: { |
76 DCHECK(!input->message.is_null()); | 76 DCHECK(!input->message.is_null()); |
77 DCHECK(input->data.is_null()); | 77 DCHECK(input->data.is_null()); |
78 // Return null PresentationSessionMessage if size exceeds. | 78 // Return null PresentationSessionMessage if size exceeds. |
79 if (input->message.size() > content::kMaxPresentationSessionMessageSize) | 79 if (input->message.size() > content::kMaxPresentationSessionMessageSize) |
80 return output.Pass(); | 80 return output; |
81 | 81 |
82 output.reset( | 82 output.reset( |
83 new PresentationSessionMessage(PresentationMessageType::TEXT)); | 83 new PresentationSessionMessage(PresentationMessageType::TEXT)); |
84 input->message.Swap(&output->message); | 84 input->message.Swap(&output->message); |
85 return output.Pass(); | 85 return output; |
86 } | 86 } |
87 case presentation::PRESENTATION_MESSAGE_TYPE_ARRAY_BUFFER: { | 87 case presentation::PRESENTATION_MESSAGE_TYPE_ARRAY_BUFFER: { |
88 DCHECK(!input->data.is_null()); | 88 DCHECK(!input->data.is_null()); |
89 DCHECK(input->message.is_null()); | 89 DCHECK(input->message.is_null()); |
90 if (input->data.size() > content::kMaxPresentationSessionMessageSize) | 90 if (input->data.size() > content::kMaxPresentationSessionMessageSize) |
91 return output.Pass(); | 91 return output; |
92 | 92 |
93 output.reset(new PresentationSessionMessage( | 93 output.reset(new PresentationSessionMessage( |
94 PresentationMessageType::ARRAY_BUFFER)); | 94 PresentationMessageType::ARRAY_BUFFER)); |
95 output->data.reset(new std::vector<uint8_t>); | 95 output->data.reset(new std::vector<uint8_t>); |
96 input->data.Swap(output->data.get()); | 96 input->data.Swap(output->data.get()); |
97 return output.Pass(); | 97 return output; |
98 } | 98 } |
99 case presentation::PRESENTATION_MESSAGE_TYPE_BLOB: { | 99 case presentation::PRESENTATION_MESSAGE_TYPE_BLOB: { |
100 DCHECK(!input->data.is_null()); | 100 DCHECK(!input->data.is_null()); |
101 DCHECK(input->message.is_null()); | 101 DCHECK(input->message.is_null()); |
102 if (input->data.size() > content::kMaxPresentationSessionMessageSize) | 102 if (input->data.size() > content::kMaxPresentationSessionMessageSize) |
103 return output.Pass(); | 103 return output; |
104 | 104 |
105 output.reset( | 105 output.reset( |
106 new PresentationSessionMessage(PresentationMessageType::BLOB)); | 106 new PresentationSessionMessage(PresentationMessageType::BLOB)); |
107 output->data.reset(new std::vector<uint8_t>); | 107 output->data.reset(new std::vector<uint8_t>); |
108 input->data.Swap(output->data.get()); | 108 input->data.Swap(output->data.get()); |
109 return output.Pass(); | 109 return output; |
110 } | 110 } |
111 } | 111 } |
112 | 112 |
113 NOTREACHED() << "Invalid presentation message type " << input->type; | 113 NOTREACHED() << "Invalid presentation message type " << input->type; |
114 return output.Pass(); | 114 return output; |
115 } | 115 } |
116 | 116 |
117 void InvokeNewSessionMojoCallbackWithError( | 117 void InvokeNewSessionMojoCallbackWithError( |
118 const NewSessionMojoCallback& callback) { | 118 const NewSessionMojoCallback& callback) { |
119 callback.Run( | 119 callback.Run( |
120 presentation::PresentationSessionInfoPtr(), | 120 presentation::PresentationSessionInfoPtr(), |
121 presentation::PresentationError::From( | 121 presentation::PresentationError::From( |
122 PresentationError(PRESENTATION_ERROR_UNKNOWN, "Internal error"))); | 122 PresentationError(PRESENTATION_ERROR_UNKNOWN, "Internal error"))); |
123 } | 123 } |
124 | 124 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 DCHECK(web_contents); | 158 DCHECK(web_contents); |
159 | 159 |
160 // This object will be deleted when the RenderFrameHost is about to be | 160 // This object will be deleted when the RenderFrameHost is about to be |
161 // deleted (RenderFrameDeleted) or if a connection error occurred | 161 // deleted (RenderFrameDeleted) or if a connection error occurred |
162 // (OnConnectionError). | 162 // (OnConnectionError). |
163 PresentationServiceImpl* impl = new PresentationServiceImpl( | 163 PresentationServiceImpl* impl = new PresentationServiceImpl( |
164 render_frame_host, | 164 render_frame_host, |
165 web_contents, | 165 web_contents, |
166 GetContentClient()->browser()->GetPresentationServiceDelegate( | 166 GetContentClient()->browser()->GetPresentationServiceDelegate( |
167 web_contents)); | 167 web_contents)); |
168 impl->Bind(request.Pass()); | 168 impl->Bind(std::move(request)); |
169 } | 169 } |
170 | 170 |
171 void PresentationServiceImpl::Bind( | 171 void PresentationServiceImpl::Bind( |
172 mojo::InterfaceRequest<presentation::PresentationService> request) { | 172 mojo::InterfaceRequest<presentation::PresentationService> request) { |
173 binding_.reset(new mojo::Binding<presentation::PresentationService>( | 173 binding_.reset(new mojo::Binding<presentation::PresentationService>( |
174 this, request.Pass())); | 174 this, std::move(request))); |
175 binding_->set_connection_error_handler([this]() { | 175 binding_->set_connection_error_handler([this]() { |
176 DVLOG(1) << "Connection error"; | 176 DVLOG(1) << "Connection error"; |
177 delete this; | 177 delete this; |
178 }); | 178 }); |
179 } | 179 } |
180 | 180 |
181 void PresentationServiceImpl::SetClient( | 181 void PresentationServiceImpl::SetClient( |
182 presentation::PresentationServiceClientPtr client) { | 182 presentation::PresentationServiceClientPtr client) { |
183 DCHECK(!client_.get()); | 183 DCHECK(!client_.get()); |
184 // TODO(imcheng): Set ErrorHandler to listen for errors. | 184 // TODO(imcheng): Set ErrorHandler to listen for errors. |
185 client_ = client.Pass(); | 185 client_ = std::move(client); |
186 } | 186 } |
187 | 187 |
188 void PresentationServiceImpl::ListenForScreenAvailability( | 188 void PresentationServiceImpl::ListenForScreenAvailability( |
189 const mojo::String& url) { | 189 const mojo::String& url) { |
190 DVLOG(2) << "ListenForScreenAvailability " << url; | 190 DVLOG(2) << "ListenForScreenAvailability " << url; |
191 if (!delegate_) { | 191 if (!delegate_) { |
192 client_->OnScreenAvailabilityUpdated(url, false); | 192 client_->OnScreenAvailabilityUpdated(url, false); |
193 return; | 193 return; |
194 } | 194 } |
195 | 195 |
196 const std::string& availability_url = url.get(); | 196 const std::string& availability_url = url.get(); |
197 if (screen_availability_listeners_.count(availability_url)) | 197 if (screen_availability_listeners_.count(availability_url)) |
198 return; | 198 return; |
199 | 199 |
200 scoped_ptr<ScreenAvailabilityListenerImpl> listener( | 200 scoped_ptr<ScreenAvailabilityListenerImpl> listener( |
201 new ScreenAvailabilityListenerImpl(availability_url, this)); | 201 new ScreenAvailabilityListenerImpl(availability_url, this)); |
202 if (delegate_->AddScreenAvailabilityListener( | 202 if (delegate_->AddScreenAvailabilityListener( |
203 render_process_id_, | 203 render_process_id_, |
204 render_frame_id_, | 204 render_frame_id_, |
205 listener.get())) { | 205 listener.get())) { |
206 screen_availability_listeners_[availability_url] = listener.Pass(); | 206 screen_availability_listeners_[availability_url] = std::move(listener); |
207 } else { | 207 } else { |
208 DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request."; | 208 DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request."; |
209 } | 209 } |
210 } | 210 } |
211 | 211 |
212 void PresentationServiceImpl::StopListeningForScreenAvailability( | 212 void PresentationServiceImpl::StopListeningForScreenAvailability( |
213 const mojo::String& url) { | 213 const mojo::String& url) { |
214 DVLOG(2) << "StopListeningForScreenAvailability " << url; | 214 DVLOG(2) << "StopListeningForScreenAvailability " << url; |
215 if (!delegate_) | 215 if (!delegate_) |
216 return; | 216 return; |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 | 356 |
357 bool PresentationServiceImpl::RunAndEraseJoinSessionMojoCallback( | 357 bool PresentationServiceImpl::RunAndEraseJoinSessionMojoCallback( |
358 int request_session_id, | 358 int request_session_id, |
359 presentation::PresentationSessionInfoPtr session, | 359 presentation::PresentationSessionInfoPtr session, |
360 presentation::PresentationErrorPtr error) { | 360 presentation::PresentationErrorPtr error) { |
361 auto it = pending_join_session_cbs_.find(request_session_id); | 361 auto it = pending_join_session_cbs_.find(request_session_id); |
362 if (it == pending_join_session_cbs_.end()) | 362 if (it == pending_join_session_cbs_.end()) |
363 return false; | 363 return false; |
364 | 364 |
365 DCHECK(it->second.get()); | 365 DCHECK(it->second.get()); |
366 it->second->Run(session.Pass(), error.Pass()); | 366 it->second->Run(std::move(session), std::move(error)); |
367 pending_join_session_cbs_.erase(it); | 367 pending_join_session_cbs_.erase(it); |
368 return true; | 368 return true; |
369 } | 369 } |
370 | 370 |
371 void PresentationServiceImpl::SetDefaultPresentationURL( | 371 void PresentationServiceImpl::SetDefaultPresentationURL( |
372 const mojo::String& url) { | 372 const mojo::String& url) { |
373 DVLOG(2) << "SetDefaultPresentationURL"; | 373 DVLOG(2) << "SetDefaultPresentationURL"; |
374 if (!delegate_) | 374 if (!delegate_) |
375 return; | 375 return; |
376 | 376 |
(...skipping 18 matching lines...) Expand all Loading... |
395 // send_message_callback_ with new callback will drop the old callback. | 395 // send_message_callback_ with new callback will drop the old callback. |
396 if (!delegate_ || send_message_callback_) { | 396 if (!delegate_ || send_message_callback_) { |
397 callback.Run(false); | 397 callback.Run(false); |
398 return; | 398 return; |
399 } | 399 } |
400 | 400 |
401 send_message_callback_.reset(new SendMessageMojoCallback(callback)); | 401 send_message_callback_.reset(new SendMessageMojoCallback(callback)); |
402 delegate_->SendMessage( | 402 delegate_->SendMessage( |
403 render_process_id_, render_frame_id_, | 403 render_process_id_, render_frame_id_, |
404 session.To<PresentationSessionInfo>(), | 404 session.To<PresentationSessionInfo>(), |
405 GetPresentationSessionMessage(session_message.Pass()), | 405 GetPresentationSessionMessage(std::move(session_message)), |
406 base::Bind(&PresentationServiceImpl::OnSendMessageCallback, | 406 base::Bind(&PresentationServiceImpl::OnSendMessageCallback, |
407 weak_factory_.GetWeakPtr())); | 407 weak_factory_.GetWeakPtr())); |
408 } | 408 } |
409 | 409 |
410 void PresentationServiceImpl::OnSendMessageCallback(bool sent) { | 410 void PresentationServiceImpl::OnSendMessageCallback(bool sent) { |
411 // It is possible that Reset() is invoked before receiving this callback. | 411 // It is possible that Reset() is invoked before receiving this callback. |
412 // So, always check send_message_callback_ for non-null. | 412 // So, always check send_message_callback_ for non-null. |
413 if (send_message_callback_) { | 413 if (send_message_callback_) { |
414 send_message_callback_->Run(sent); | 414 send_message_callback_->Run(sent); |
415 send_message_callback_.reset(); | 415 send_message_callback_.reset(); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 bool pass_ownership) { | 469 bool pass_ownership) { |
470 DCHECK(client_); | 470 DCHECK(client_); |
471 | 471 |
472 DVLOG(2) << "OnSessionMessages"; | 472 DVLOG(2) << "OnSessionMessages"; |
473 mojo::Array<presentation::SessionMessagePtr> mojoMessages(messages.size()); | 473 mojo::Array<presentation::SessionMessagePtr> mojoMessages(messages.size()); |
474 for (size_t i = 0; i < messages.size(); ++i) | 474 for (size_t i = 0; i < messages.size(); ++i) |
475 mojoMessages[i] = ToMojoSessionMessage(messages[i], pass_ownership); | 475 mojoMessages[i] = ToMojoSessionMessage(messages[i], pass_ownership); |
476 | 476 |
477 client_->OnSessionMessagesReceived( | 477 client_->OnSessionMessagesReceived( |
478 presentation::PresentationSessionInfo::From(session), | 478 presentation::PresentationSessionInfo::From(session), |
479 mojoMessages.Pass()); | 479 std::move(mojoMessages)); |
480 } | 480 } |
481 | 481 |
482 void PresentationServiceImpl::DidNavigateAnyFrame( | 482 void PresentationServiceImpl::DidNavigateAnyFrame( |
483 content::RenderFrameHost* render_frame_host, | 483 content::RenderFrameHost* render_frame_host, |
484 const content::LoadCommittedDetails& details, | 484 const content::LoadCommittedDetails& details, |
485 const content::FrameNavigateParams& params) { | 485 const content::FrameNavigateParams& params) { |
486 DVLOG(2) << "PresentationServiceImpl::DidNavigateAnyFrame"; | 486 DVLOG(2) << "PresentationServiceImpl::DidNavigateAnyFrame"; |
487 if (!FrameMatches(render_frame_host)) | 487 if (!FrameMatches(render_frame_host)) |
488 return; | 488 return; |
489 | 489 |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 PresentationServiceImpl::NewSessionMojoCallbackWrapper | 593 PresentationServiceImpl::NewSessionMojoCallbackWrapper |
594 ::~NewSessionMojoCallbackWrapper() { | 594 ::~NewSessionMojoCallbackWrapper() { |
595 if (!callback_.is_null()) | 595 if (!callback_.is_null()) |
596 InvokeNewSessionMojoCallbackWithError(callback_); | 596 InvokeNewSessionMojoCallbackWithError(callback_); |
597 } | 597 } |
598 | 598 |
599 void PresentationServiceImpl::NewSessionMojoCallbackWrapper::Run( | 599 void PresentationServiceImpl::NewSessionMojoCallbackWrapper::Run( |
600 presentation::PresentationSessionInfoPtr session, | 600 presentation::PresentationSessionInfoPtr session, |
601 presentation::PresentationErrorPtr error) { | 601 presentation::PresentationErrorPtr error) { |
602 DCHECK(!callback_.is_null()); | 602 DCHECK(!callback_.is_null()); |
603 callback_.Run(session.Pass(), error.Pass()); | 603 callback_.Run(std::move(session), std::move(error)); |
604 callback_.reset(); | 604 callback_.reset(); |
605 } | 605 } |
606 | 606 |
607 } // namespace content | 607 } // namespace content |
OLD | NEW |