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

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

Issue 2181163003: [Presentation API] Convert presentation.mojom to new wrapper types. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Respond to dcheng@ comments Created 4 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 <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <algorithm> 9 #include <algorithm>
10 #include <string> 10 #include <string>
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 blink::mojom::SessionMessagePtr ToMojoSessionMessage( 42 blink::mojom::SessionMessagePtr ToMojoSessionMessage(
43 content::PresentationSessionMessage* input, 43 content::PresentationSessionMessage* input,
44 bool pass_ownership) { 44 bool pass_ownership) {
45 DCHECK(input); 45 DCHECK(input);
46 blink::mojom::SessionMessagePtr output(blink::mojom::SessionMessage::New()); 46 blink::mojom::SessionMessagePtr output(blink::mojom::SessionMessage::New());
47 if (input->is_binary()) { 47 if (input->is_binary()) {
48 // binary data 48 // binary data
49 DCHECK(input->data); 49 DCHECK(input->data);
50 output->type = blink::mojom::PresentationMessageType::ARRAY_BUFFER; 50 output->type = blink::mojom::PresentationMessageType::ARRAY_BUFFER;
51 if (pass_ownership) { 51 if (pass_ownership) {
52 output->data.Swap(input->data.get()); 52 output->data = std::vector<uint8_t>(std::move(*(input->data)));
dcheng 2016/07/28 01:48:44 I think this can just be std::move(*(input->data))
mark a. foltz 2016/07/28 18:28:24 Done.
53 } else { 53 } else {
54 output->data = mojo::Array<uint8_t>::From(*input->data); 54 output->data = std::vector<uint8_t>(*(input->data));
dcheng 2016/07/28 01:48:44 Ditto: I don't think we need to constructor the ve
mark a. foltz 2016/07/28 18:28:24 Done.
55 } 55 }
56 } else { 56 } else {
57 // string message 57 // string message
58 output->type = blink::mojom::PresentationMessageType::TEXT; 58 output->type = blink::mojom::PresentationMessageType::TEXT;
59 if (pass_ownership) { 59 if (pass_ownership) {
60 output->message.Swap(&input->message); 60 output->message = std::move(input->message);
61 } else { 61 } else {
62 output->message = input->message; 62 output->message = input->message;
63 } 63 }
64 } 64 }
65 return output; 65 return output;
66 } 66 }
67 67
68 std::unique_ptr<PresentationSessionMessage> GetPresentationSessionMessage( 68 std::unique_ptr<PresentationSessionMessage> GetPresentationSessionMessage(
69 blink::mojom::SessionMessagePtr input) { 69 blink::mojom::SessionMessagePtr input) {
70 DCHECK(!input.is_null()); 70 DCHECK(!input.is_null());
71 std::unique_ptr<content::PresentationSessionMessage> output; 71 std::unique_ptr<content::PresentationSessionMessage> output;
72 switch (input->type) { 72 switch (input->type) {
73 case blink::mojom::PresentationMessageType::TEXT: { 73 case blink::mojom::PresentationMessageType::TEXT: {
74 DCHECK(!input->message.is_null()); 74 // TODO(mfoltz): Should these be CHECKs?
imcheng 2016/07/27 20:59:38 Probably not. We shouldn't make the browser crash
mark a. foltz 2016/07/28 18:28:24 Now converted to return nullptr on invalid input.
75 DCHECK(input->data.is_null()); 75 DCHECK(!input->data);
76 DCHECK(input->message);
76 // Return null PresentationSessionMessage if size exceeds. 77 // Return null PresentationSessionMessage if size exceeds.
77 if (input->message.size() > content::kMaxPresentationSessionMessageSize) 78 if (input->message->size() > content::kMaxPresentationSessionMessageSize)
dcheng 2016/07/28 01:48:44 We need to handle unset values here, this code is
mark a. foltz 2016/07/28 18:28:24 Done.
78 return output; 79 return output;
79 80
80 output.reset( 81 output.reset(
81 new PresentationSessionMessage(PresentationMessageType::TEXT)); 82 new PresentationSessionMessage(PresentationMessageType::TEXT));
82 input->message.Swap(&output->message); 83 input->message->swap(output->message);
dcheng 2016/07/28 01:48:44 Nit: std::move() instead here for consistency (sam
mark a. foltz 2016/07/28 18:28:24 Done. No more swaps.
83 return output; 84 return output;
84 } 85 }
85 case blink::mojom::PresentationMessageType::ARRAY_BUFFER: { 86 case blink::mojom::PresentationMessageType::ARRAY_BUFFER: {
86 DCHECK(!input->data.is_null()); 87 // TODO(mfoltz): Should these be CHECKs?
87 DCHECK(input->message.is_null()); 88 DCHECK(input->data);
88 if (input->data.size() > content::kMaxPresentationSessionMessageSize) 89 DCHECK(!input->message);
90 if (input->data->size() > content::kMaxPresentationSessionMessageSize)
89 return output; 91 return output;
90 92
91 output.reset(new PresentationSessionMessage( 93 output.reset(new PresentationSessionMessage(
92 PresentationMessageType::ARRAY_BUFFER)); 94 PresentationMessageType::ARRAY_BUFFER));
93 output->data.reset(new std::vector<uint8_t>); 95 output->data.reset(new std::vector<uint8_t>);
94 input->data.Swap(output->data.get()); 96 input->data->swap(*(output->data));
95 return output; 97 return output;
96 } 98 }
97 case blink::mojom::PresentationMessageType::BLOB: { 99 case blink::mojom::PresentationMessageType::BLOB: {
98 DCHECK(!input->data.is_null()); 100 // TODO(mfoltz): Should these be CHECKs?
99 DCHECK(input->message.is_null()); 101 DCHECK(input->data);
100 if (input->data.size() > content::kMaxPresentationSessionMessageSize) 102 DCHECK(!input->message);
103 if (input->data->size() > content::kMaxPresentationSessionMessageSize)
101 return output; 104 return output;
102 105
103 output.reset( 106 output.reset(
104 new PresentationSessionMessage(PresentationMessageType::BLOB)); 107 new PresentationSessionMessage(PresentationMessageType::BLOB));
105 output->data.reset(new std::vector<uint8_t>); 108 output->data.reset(new std::vector<uint8_t>);
106 input->data.Swap(output->data.get()); 109 input->data->swap(*(output->data));
107 return output; 110 return output;
108 } 111 }
109 } 112 }
110 113
111 NOTREACHED() << "Invalid presentation message type " << input->type; 114 NOTREACHED() << "Invalid presentation message type " << input->type;
112 return output; 115 return output;
113 } 116 }
114 117
115 void InvokeNewSessionCallbackWithError( 118 void InvokeNewSessionCallbackWithError(
116 const PresentationServiceImpl::NewSessionCallback& callback) { 119 const PresentationServiceImpl::NewSessionCallback& callback) {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 } 175 }
173 176
174 void PresentationServiceImpl::SetClient( 177 void PresentationServiceImpl::SetClient(
175 blink::mojom::PresentationServiceClientPtr client) { 178 blink::mojom::PresentationServiceClientPtr client) {
176 DCHECK(!client_.get()); 179 DCHECK(!client_.get());
177 // TODO(imcheng): Set ErrorHandler to listen for errors. 180 // TODO(imcheng): Set ErrorHandler to listen for errors.
178 client_ = std::move(client); 181 client_ = std::move(client);
179 } 182 }
180 183
181 void PresentationServiceImpl::ListenForScreenAvailability( 184 void PresentationServiceImpl::ListenForScreenAvailability(
182 const mojo::String& url) { 185 const std::string& url) {
183 DVLOG(2) << "ListenForScreenAvailability " << url; 186 DVLOG(2) << "ListenForScreenAvailability " << url;
184 if (!delegate_) { 187 if (!delegate_) {
185 client_->OnScreenAvailabilityUpdated(url, false); 188 client_->OnScreenAvailabilityUpdated(url, false);
186 return; 189 return;
187 } 190 }
188 191
189 const std::string& availability_url = url.get(); 192 if (screen_availability_listeners_.count(url))
190 if (screen_availability_listeners_.count(availability_url))
191 return; 193 return;
192 194
193 std::unique_ptr<ScreenAvailabilityListenerImpl> listener( 195 std::unique_ptr<ScreenAvailabilityListenerImpl> listener(
194 new ScreenAvailabilityListenerImpl(availability_url, this)); 196 new ScreenAvailabilityListenerImpl(url, this));
195 if (delegate_->AddScreenAvailabilityListener( 197 if (delegate_->AddScreenAvailabilityListener(
196 render_process_id_, 198 render_process_id_,
197 render_frame_id_, 199 render_frame_id_,
198 listener.get())) { 200 listener.get())) {
199 screen_availability_listeners_[availability_url] = std::move(listener); 201 screen_availability_listeners_[url] = std::move(listener);
200 } else { 202 } else {
201 DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request."; 203 DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request.";
202 } 204 }
203 } 205 }
204 206
205 void PresentationServiceImpl::StopListeningForScreenAvailability( 207 void PresentationServiceImpl::StopListeningForScreenAvailability(
206 const mojo::String& url) { 208 const std::string& url) {
207 DVLOG(2) << "StopListeningForScreenAvailability " << url; 209 DVLOG(2) << "StopListeningForScreenAvailability " << url;
208 if (!delegate_) 210 if (!delegate_)
209 return; 211 return;
210 212
211 const std::string& availability_url = url.get(); 213 auto listener_it = screen_availability_listeners_.find(url);
212 auto listener_it = screen_availability_listeners_.find(availability_url);
213 if (listener_it == screen_availability_listeners_.end()) 214 if (listener_it == screen_availability_listeners_.end())
214 return; 215 return;
215 216
216 delegate_->RemoveScreenAvailabilityListener( 217 delegate_->RemoveScreenAvailabilityListener(
217 render_process_id_, render_frame_id_, listener_it->second.get()); 218 render_process_id_, render_frame_id_, listener_it->second.get());
218 screen_availability_listeners_.erase(listener_it); 219 screen_availability_listeners_.erase(listener_it);
219 } 220 }
220 221
221 void PresentationServiceImpl::StartSession(const mojo::String& presentation_url, 222 void PresentationServiceImpl::StartSession(const std::string& presentation_url,
222 const NewSessionCallback& callback) { 223 const NewSessionCallback& callback) {
223 DVLOG(2) << "StartSession"; 224 DVLOG(2) << "StartSession";
224 if (!delegate_) { 225 if (!delegate_) {
225 callback.Run( 226 callback.Run(
226 blink::mojom::PresentationSessionInfoPtr(), 227 blink::mojom::PresentationSessionInfoPtr(),
227 blink::mojom::PresentationError::From(PresentationError( 228 blink::mojom::PresentationError::From(PresentationError(
228 PRESENTATION_ERROR_NO_AVAILABLE_SCREENS, "No screens found."))); 229 PRESENTATION_ERROR_NO_AVAILABLE_SCREENS, "No screens found.")));
229 return; 230 return;
230 } 231 }
231 232
232 // There is a StartSession request in progress. To avoid queueing up 233 // There is a StartSession request in progress. To avoid queueing up
233 // requests, the incoming request is rejected. 234 // requests, the incoming request is rejected.
234 if (start_session_request_id_ != kInvalidRequestSessionId) { 235 if (start_session_request_id_ != kInvalidRequestSessionId) {
235 InvokeNewSessionCallbackWithError(callback); 236 InvokeNewSessionCallbackWithError(callback);
236 return; 237 return;
237 } 238 }
238 239
239 start_session_request_id_ = GetNextRequestSessionId(); 240 start_session_request_id_ = GetNextRequestSessionId();
240 pending_start_session_cb_.reset(new NewSessionCallbackWrapper(callback)); 241 pending_start_session_cb_.reset(new NewSessionCallbackWrapper(callback));
241 delegate_->StartSession( 242 delegate_->StartSession(
242 render_process_id_, render_frame_id_, presentation_url, 243 render_process_id_, render_frame_id_, presentation_url,
243 base::Bind(&PresentationServiceImpl::OnStartSessionSucceeded, 244 base::Bind(&PresentationServiceImpl::OnStartSessionSucceeded,
244 weak_factory_.GetWeakPtr(), start_session_request_id_), 245 weak_factory_.GetWeakPtr(), start_session_request_id_),
245 base::Bind(&PresentationServiceImpl::OnStartSessionError, 246 base::Bind(&PresentationServiceImpl::OnStartSessionError,
246 weak_factory_.GetWeakPtr(), start_session_request_id_)); 247 weak_factory_.GetWeakPtr(), start_session_request_id_));
247 } 248 }
248 249
249 void PresentationServiceImpl::JoinSession( 250 void PresentationServiceImpl::JoinSession(
250 const mojo::String& presentation_url, 251 const std::string& presentation_url,
251 const mojo::String& presentation_id, 252 const base::Optional<std::string>& presentation_id,
252 const NewSessionCallback& callback) { 253 const NewSessionCallback& callback) {
253 DVLOG(2) << "JoinSession"; 254 DVLOG(2) << "JoinSession";
254 if (!delegate_) { 255 if (!delegate_) {
255 callback.Run(blink::mojom::PresentationSessionInfoPtr(), 256 callback.Run(blink::mojom::PresentationSessionInfoPtr(),
256 blink::mojom::PresentationError::From(PresentationError( 257 blink::mojom::PresentationError::From(PresentationError(
257 PRESENTATION_ERROR_NO_PRESENTATION_FOUND, 258 PRESENTATION_ERROR_NO_PRESENTATION_FOUND,
258 "Error joining route: No matching route"))); 259 "Error joining route: No matching route")));
259 return; 260 return;
260 } 261 }
261 262
262 int request_session_id = RegisterJoinSessionCallback(callback); 263 int request_session_id = RegisterJoinSessionCallback(callback);
263 if (request_session_id == kInvalidRequestSessionId) { 264 if (request_session_id == kInvalidRequestSessionId) {
264 InvokeNewSessionCallbackWithError(callback); 265 InvokeNewSessionCallbackWithError(callback);
265 return; 266 return;
266 } 267 }
267 delegate_->JoinSession( 268 delegate_->JoinSession(
268 render_process_id_, 269 render_process_id_, render_frame_id_, presentation_url,
269 render_frame_id_, 270 presentation_id.value_or(std::string()),
270 presentation_url,
271 presentation_id,
272 base::Bind(&PresentationServiceImpl::OnJoinSessionSucceeded, 271 base::Bind(&PresentationServiceImpl::OnJoinSessionSucceeded,
273 weak_factory_.GetWeakPtr(), request_session_id), 272 weak_factory_.GetWeakPtr(), request_session_id),
274 base::Bind(&PresentationServiceImpl::OnJoinSessionError, 273 base::Bind(&PresentationServiceImpl::OnJoinSessionError,
275 weak_factory_.GetWeakPtr(), request_session_id)); 274 weak_factory_.GetWeakPtr(), request_session_id));
276 } 275 }
277 276
278 int PresentationServiceImpl::RegisterJoinSessionCallback( 277 int PresentationServiceImpl::RegisterJoinSessionCallback(
279 const NewSessionCallback& callback) { 278 const NewSessionCallback& callback) {
280 if (pending_join_session_cbs_.size() >= kMaxNumQueuedSessionRequests) 279 if (pending_join_session_cbs_.size() >= kMaxNumQueuedSessionRequests)
281 return kInvalidRequestSessionId; 280 return kInvalidRequestSessionId;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 if (it == pending_join_session_cbs_.end()) 350 if (it == pending_join_session_cbs_.end())
352 return false; 351 return false;
353 352
354 DCHECK(it->second.get()); 353 DCHECK(it->second.get());
355 it->second->Run(std::move(session), std::move(error)); 354 it->second->Run(std::move(session), std::move(error));
356 pending_join_session_cbs_.erase(it); 355 pending_join_session_cbs_.erase(it);
357 return true; 356 return true;
358 } 357 }
359 358
360 void PresentationServiceImpl::SetDefaultPresentationURL( 359 void PresentationServiceImpl::SetDefaultPresentationURL(
361 const mojo::String& url) { 360 const std::string& url) {
362 DVLOG(2) << "SetDefaultPresentationURL"; 361 DVLOG(2) << "SetDefaultPresentationURL";
363 if (!delegate_) 362 if (!delegate_)
364 return; 363 return;
365 364
366 const std::string& new_default_url = url.get(); 365 if (default_presentation_url_ == url)
367 if (default_presentation_url_ == new_default_url)
368 return; 366 return;
369 367
370 default_presentation_url_ = new_default_url; 368 default_presentation_url_ = url;
371 delegate_->SetDefaultPresentationUrl( 369 delegate_->SetDefaultPresentationUrl(
372 render_process_id_, render_frame_id_, new_default_url, 370 render_process_id_, render_frame_id_, url,
373 base::Bind(&PresentationServiceImpl::OnDefaultPresentationStarted, 371 base::Bind(&PresentationServiceImpl::OnDefaultPresentationStarted,
374 weak_factory_.GetWeakPtr())); 372 weak_factory_.GetWeakPtr()));
375 } 373 }
376 374
377 void PresentationServiceImpl::SendSessionMessage( 375 void PresentationServiceImpl::SendSessionMessage(
378 blink::mojom::PresentationSessionInfoPtr session, 376 blink::mojom::PresentationSessionInfoPtr session,
379 blink::mojom::SessionMessagePtr session_message, 377 blink::mojom::SessionMessagePtr session_message,
380 const SendSessionMessageCallback& callback) { 378 const SendSessionMessageCallback& callback) {
381 DVLOG(2) << "SendSessionMessage"; 379 DVLOG(2) << "SendSessionMessage";
382 DCHECK(!session_message.is_null()); 380 DCHECK(!session_message.is_null());
(...skipping 16 matching lines...) Expand all
399 void PresentationServiceImpl::OnSendMessageCallback(bool sent) { 397 void PresentationServiceImpl::OnSendMessageCallback(bool sent) {
400 // It is possible that Reset() is invoked before receiving this callback. 398 // It is possible that Reset() is invoked before receiving this callback.
401 // So, always check send_message_callback_ for non-null. 399 // So, always check send_message_callback_ for non-null.
402 if (send_message_callback_) { 400 if (send_message_callback_) {
403 send_message_callback_->Run(sent); 401 send_message_callback_->Run(sent);
404 send_message_callback_.reset(); 402 send_message_callback_.reset();
405 } 403 }
406 } 404 }
407 405
408 void PresentationServiceImpl::CloseConnection( 406 void PresentationServiceImpl::CloseConnection(
409 const mojo::String& presentation_url, 407 const std::string& presentation_url,
410 const mojo::String& presentation_id) { 408 const std::string& presentation_id) {
411 DVLOG(2) << "CloseConnection " << presentation_id; 409 DVLOG(2) << "CloseConnection " << presentation_id;
412 if (delegate_) 410 if (delegate_)
413 delegate_->CloseConnection(render_process_id_, render_frame_id_, 411 delegate_->CloseConnection(render_process_id_, render_frame_id_,
414 presentation_id); 412 presentation_id);
415 } 413 }
416 414
417 void PresentationServiceImpl::Terminate(const mojo::String& presentation_url, 415 void PresentationServiceImpl::Terminate(const std::string& presentation_url,
418 const mojo::String& presentation_id) { 416 const std::string& presentation_id) {
419 DVLOG(2) << "Terminate " << presentation_id; 417 DVLOG(2) << "Terminate " << presentation_id;
420 if (delegate_) 418 if (delegate_)
421 delegate_->Terminate(render_process_id_, render_frame_id_, presentation_id); 419 delegate_->Terminate(render_process_id_, render_frame_id_, presentation_id);
422 } 420 }
423 421
424 void PresentationServiceImpl::OnConnectionStateChanged( 422 void PresentationServiceImpl::OnConnectionStateChanged(
425 const PresentationSessionInfo& connection, 423 const PresentationSessionInfo& connection,
426 const PresentationConnectionStateChangeInfo& info) { 424 const PresentationConnectionStateChangeInfo& info) {
427 DCHECK(client_.get()); 425 DCHECK(client_.get());
428 if (info.state == PRESENTATION_CONNECTION_STATE_CLOSED) { 426 if (info.state == PRESENTATION_CONNECTION_STATE_CLOSED) {
(...skipping 30 matching lines...) Expand all
459 weak_factory_.GetWeakPtr(), session_info)); 457 weak_factory_.GetWeakPtr(), session_info));
460 } 458 }
461 459
462 void PresentationServiceImpl::OnSessionMessages( 460 void PresentationServiceImpl::OnSessionMessages(
463 const PresentationSessionInfo& session, 461 const PresentationSessionInfo& session,
464 const ScopedVector<PresentationSessionMessage>& messages, 462 const ScopedVector<PresentationSessionMessage>& messages,
465 bool pass_ownership) { 463 bool pass_ownership) {
466 DCHECK(client_); 464 DCHECK(client_);
467 465
468 DVLOG(2) << "OnSessionMessages"; 466 DVLOG(2) << "OnSessionMessages";
469 mojo::Array<blink::mojom::SessionMessagePtr> mojoMessages(messages.size()); 467 std::vector<blink::mojom::SessionMessagePtr> mojoMessages(messages.size());
470 for (size_t i = 0; i < messages.size(); ++i) 468 std::transform(messages.begin(), messages.end(), mojoMessages.begin(),
471 mojoMessages[i] = ToMojoSessionMessage(messages[i], pass_ownership); 469 [pass_ownership](PresentationSessionMessage* message) {
470 return ToMojoSessionMessage(message, pass_ownership);
471 });
472 472
473 client_->OnSessionMessagesReceived( 473 client_->OnSessionMessagesReceived(
474 blink::mojom::PresentationSessionInfo::From(session), 474 blink::mojom::PresentationSessionInfo::From(session),
475 std::move(mojoMessages)); 475 std::move(mojoMessages));
476 } 476 }
477 477
478 void PresentationServiceImpl::DidNavigateAnyFrame( 478 void PresentationServiceImpl::DidNavigateAnyFrame(
479 content::RenderFrameHost* render_frame_host, 479 content::RenderFrameHost* render_frame_host,
480 const content::LoadCommittedDetails& details, 480 const content::LoadCommittedDetails& details,
481 const content::FrameNavigateParams& params) { 481 const content::FrameNavigateParams& params) {
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
602 602
603 void PresentationServiceImpl::NewSessionCallbackWrapper::Run( 603 void PresentationServiceImpl::NewSessionCallbackWrapper::Run(
604 blink::mojom::PresentationSessionInfoPtr session, 604 blink::mojom::PresentationSessionInfoPtr session,
605 blink::mojom::PresentationErrorPtr error) { 605 blink::mojom::PresentationErrorPtr error) {
606 DCHECK(!callback_.is_null()); 606 DCHECK(!callback_.is_null());
607 callback_.Run(std::move(session), std::move(error)); 607 callback_.Run(std::move(session), std::move(error));
608 callback_.Reset(); 608 callback_.Reset();
609 } 609 }
610 610
611 } // namespace content 611 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698