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

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::move(*(input->data));
53 } else { 53 } else {
54 output->data = mojo::Array<uint8_t>::From(*input->data); 54 output->data = *(input->data);
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());
71 std::unique_ptr<content::PresentationSessionMessage> output; 70 std::unique_ptr<content::PresentationSessionMessage> output;
71 if (input.is_null())
72 return output;
73
72 switch (input->type) { 74 switch (input->type) {
73 case blink::mojom::PresentationMessageType::TEXT: { 75 case blink::mojom::PresentationMessageType::TEXT: {
74 DCHECK(!input->message.is_null()); 76 // Return nullptr PresentationSessionMessage if invalid (unset |message|,
75 DCHECK(input->data.is_null()); 77 // set |data|, or size too large).
76 // Return null PresentationSessionMessage if size exceeds. 78 if (input->data || !input->message ||
77 if (input->message.size() > content::kMaxPresentationSessionMessageSize) 79 input->message->size() > content::kMaxPresentationSessionMessageSize)
78 return output; 80 return output;
79 81
80 output.reset( 82 output.reset(
81 new PresentationSessionMessage(PresentationMessageType::TEXT)); 83 new PresentationSessionMessage(PresentationMessageType::TEXT));
82 input->message.Swap(&output->message); 84 output->message = std::move(input->message.value());
83 return output; 85 return output;
84 } 86 }
85 case blink::mojom::PresentationMessageType::ARRAY_BUFFER: { 87 case blink::mojom::PresentationMessageType::ARRAY_BUFFER: {
86 DCHECK(!input->data.is_null()); 88 // Return nullptr PresentationSessionMessage if invalid (unset |data|, set
87 DCHECK(input->message.is_null()); 89 // |message|, or size too large).
88 if (input->data.size() > content::kMaxPresentationSessionMessageSize) 90 if (!input->data || input->message ||
91 input->data->size() > content::kMaxPresentationSessionMessageSize)
89 return output; 92 return output;
90 93
91 output.reset(new PresentationSessionMessage( 94 output.reset(new PresentationSessionMessage(
92 PresentationMessageType::ARRAY_BUFFER)); 95 PresentationMessageType::ARRAY_BUFFER));
93 output->data.reset(new std::vector<uint8_t>); 96 output->data.reset(
94 input->data.Swap(output->data.get()); 97 new std::vector<uint8_t>(std::move(input->data.value())));
95 return output; 98 return output;
96 } 99 }
97 case blink::mojom::PresentationMessageType::BLOB: { 100 case blink::mojom::PresentationMessageType::BLOB: {
98 DCHECK(!input->data.is_null()); 101 // Return nullptr PresentationSessionMessage if invalid (unset |data|, set
99 DCHECK(input->message.is_null()); 102 // |message|, or size too large).
100 if (input->data.size() > content::kMaxPresentationSessionMessageSize) 103 if (!input->data || input->message ||
104 input->data->size() > content::kMaxPresentationSessionMessageSize)
101 return output; 105 return output;
102 106
103 output.reset( 107 output.reset(
104 new PresentationSessionMessage(PresentationMessageType::BLOB)); 108 new PresentationSessionMessage(PresentationMessageType::BLOB));
105 output->data.reset(new std::vector<uint8_t>); 109 output->data.reset(
106 input->data.Swap(output->data.get()); 110 new std::vector<uint8_t>(std::move(input->data.value())));
107 return output; 111 return output;
108 } 112 }
109 } 113 }
110 114
111 NOTREACHED() << "Invalid presentation message type " << input->type; 115 NOTREACHED() << "Invalid presentation message type " << input->type;
112 return output; 116 return output;
113 } 117 }
114 118
115 void InvokeNewSessionCallbackWithError( 119 void InvokeNewSessionCallbackWithError(
116 const PresentationServiceImpl::NewSessionCallback& callback) { 120 const PresentationServiceImpl::NewSessionCallback& callback) {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 } 176 }
173 177
174 void PresentationServiceImpl::SetClient( 178 void PresentationServiceImpl::SetClient(
175 blink::mojom::PresentationServiceClientPtr client) { 179 blink::mojom::PresentationServiceClientPtr client) {
176 DCHECK(!client_.get()); 180 DCHECK(!client_.get());
177 // TODO(imcheng): Set ErrorHandler to listen for errors. 181 // TODO(imcheng): Set ErrorHandler to listen for errors.
178 client_ = std::move(client); 182 client_ = std::move(client);
179 } 183 }
180 184
181 void PresentationServiceImpl::ListenForScreenAvailability( 185 void PresentationServiceImpl::ListenForScreenAvailability(
182 const mojo::String& url) { 186 const std::string& url) {
183 DVLOG(2) << "ListenForScreenAvailability " << url; 187 DVLOG(2) << "ListenForScreenAvailability " << url;
184 if (!delegate_) { 188 if (!delegate_) {
185 client_->OnScreenAvailabilityUpdated(url, false); 189 client_->OnScreenAvailabilityUpdated(url, false);
186 return; 190 return;
187 } 191 }
188 192
189 const std::string& availability_url = url.get(); 193 if (screen_availability_listeners_.count(url))
190 if (screen_availability_listeners_.count(availability_url))
191 return; 194 return;
192 195
193 std::unique_ptr<ScreenAvailabilityListenerImpl> listener( 196 std::unique_ptr<ScreenAvailabilityListenerImpl> listener(
194 new ScreenAvailabilityListenerImpl(availability_url, this)); 197 new ScreenAvailabilityListenerImpl(url, this));
195 if (delegate_->AddScreenAvailabilityListener( 198 if (delegate_->AddScreenAvailabilityListener(
196 render_process_id_, 199 render_process_id_,
197 render_frame_id_, 200 render_frame_id_,
198 listener.get())) { 201 listener.get())) {
199 screen_availability_listeners_[availability_url] = std::move(listener); 202 screen_availability_listeners_[url] = std::move(listener);
200 } else { 203 } else {
201 DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request."; 204 DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request.";
202 } 205 }
203 } 206 }
204 207
205 void PresentationServiceImpl::StopListeningForScreenAvailability( 208 void PresentationServiceImpl::StopListeningForScreenAvailability(
206 const mojo::String& url) { 209 const std::string& url) {
207 DVLOG(2) << "StopListeningForScreenAvailability " << url; 210 DVLOG(2) << "StopListeningForScreenAvailability " << url;
208 if (!delegate_) 211 if (!delegate_)
209 return; 212 return;
210 213
211 const std::string& availability_url = url.get(); 214 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()) 215 if (listener_it == screen_availability_listeners_.end())
214 return; 216 return;
215 217
216 delegate_->RemoveScreenAvailabilityListener( 218 delegate_->RemoveScreenAvailabilityListener(
217 render_process_id_, render_frame_id_, listener_it->second.get()); 219 render_process_id_, render_frame_id_, listener_it->second.get());
218 screen_availability_listeners_.erase(listener_it); 220 screen_availability_listeners_.erase(listener_it);
219 } 221 }
220 222
221 void PresentationServiceImpl::StartSession(const mojo::String& presentation_url, 223 void PresentationServiceImpl::StartSession(const std::string& presentation_url,
222 const NewSessionCallback& callback) { 224 const NewSessionCallback& callback) {
223 DVLOG(2) << "StartSession"; 225 DVLOG(2) << "StartSession";
224 if (!delegate_) { 226 if (!delegate_) {
225 callback.Run( 227 callback.Run(
226 blink::mojom::PresentationSessionInfoPtr(), 228 blink::mojom::PresentationSessionInfoPtr(),
227 blink::mojom::PresentationError::From(PresentationError( 229 blink::mojom::PresentationError::From(PresentationError(
228 PRESENTATION_ERROR_NO_AVAILABLE_SCREENS, "No screens found."))); 230 PRESENTATION_ERROR_NO_AVAILABLE_SCREENS, "No screens found.")));
229 return; 231 return;
230 } 232 }
231 233
232 // There is a StartSession request in progress. To avoid queueing up 234 // There is a StartSession request in progress. To avoid queueing up
233 // requests, the incoming request is rejected. 235 // requests, the incoming request is rejected.
234 if (start_session_request_id_ != kInvalidRequestSessionId) { 236 if (start_session_request_id_ != kInvalidRequestSessionId) {
235 InvokeNewSessionCallbackWithError(callback); 237 InvokeNewSessionCallbackWithError(callback);
236 return; 238 return;
237 } 239 }
238 240
239 start_session_request_id_ = GetNextRequestSessionId(); 241 start_session_request_id_ = GetNextRequestSessionId();
240 pending_start_session_cb_.reset(new NewSessionCallbackWrapper(callback)); 242 pending_start_session_cb_.reset(new NewSessionCallbackWrapper(callback));
241 delegate_->StartSession( 243 delegate_->StartSession(
242 render_process_id_, render_frame_id_, presentation_url, 244 render_process_id_, render_frame_id_, presentation_url,
243 base::Bind(&PresentationServiceImpl::OnStartSessionSucceeded, 245 base::Bind(&PresentationServiceImpl::OnStartSessionSucceeded,
244 weak_factory_.GetWeakPtr(), start_session_request_id_), 246 weak_factory_.GetWeakPtr(), start_session_request_id_),
245 base::Bind(&PresentationServiceImpl::OnStartSessionError, 247 base::Bind(&PresentationServiceImpl::OnStartSessionError,
246 weak_factory_.GetWeakPtr(), start_session_request_id_)); 248 weak_factory_.GetWeakPtr(), start_session_request_id_));
247 } 249 }
248 250
249 void PresentationServiceImpl::JoinSession( 251 void PresentationServiceImpl::JoinSession(
250 const mojo::String& presentation_url, 252 const std::string& presentation_url,
251 const mojo::String& presentation_id, 253 const base::Optional<std::string>& presentation_id,
252 const NewSessionCallback& callback) { 254 const NewSessionCallback& callback) {
253 DVLOG(2) << "JoinSession"; 255 DVLOG(2) << "JoinSession";
254 if (!delegate_) { 256 if (!delegate_) {
255 callback.Run(blink::mojom::PresentationSessionInfoPtr(), 257 callback.Run(blink::mojom::PresentationSessionInfoPtr(),
256 blink::mojom::PresentationError::From(PresentationError( 258 blink::mojom::PresentationError::From(PresentationError(
257 PRESENTATION_ERROR_NO_PRESENTATION_FOUND, 259 PRESENTATION_ERROR_NO_PRESENTATION_FOUND,
258 "Error joining route: No matching route"))); 260 "Error joining route: No matching route")));
259 return; 261 return;
260 } 262 }
261 263
262 int request_session_id = RegisterJoinSessionCallback(callback); 264 int request_session_id = RegisterJoinSessionCallback(callback);
263 if (request_session_id == kInvalidRequestSessionId) { 265 if (request_session_id == kInvalidRequestSessionId) {
264 InvokeNewSessionCallbackWithError(callback); 266 InvokeNewSessionCallbackWithError(callback);
265 return; 267 return;
266 } 268 }
267 delegate_->JoinSession( 269 delegate_->JoinSession(
268 render_process_id_, 270 render_process_id_, render_frame_id_, presentation_url,
269 render_frame_id_, 271 presentation_id.value_or(std::string()),
270 presentation_url,
271 presentation_id,
272 base::Bind(&PresentationServiceImpl::OnJoinSessionSucceeded, 272 base::Bind(&PresentationServiceImpl::OnJoinSessionSucceeded,
273 weak_factory_.GetWeakPtr(), request_session_id), 273 weak_factory_.GetWeakPtr(), request_session_id),
274 base::Bind(&PresentationServiceImpl::OnJoinSessionError, 274 base::Bind(&PresentationServiceImpl::OnJoinSessionError,
275 weak_factory_.GetWeakPtr(), request_session_id)); 275 weak_factory_.GetWeakPtr(), request_session_id));
276 } 276 }
277 277
278 int PresentationServiceImpl::RegisterJoinSessionCallback( 278 int PresentationServiceImpl::RegisterJoinSessionCallback(
279 const NewSessionCallback& callback) { 279 const NewSessionCallback& callback) {
280 if (pending_join_session_cbs_.size() >= kMaxNumQueuedSessionRequests) 280 if (pending_join_session_cbs_.size() >= kMaxNumQueuedSessionRequests)
281 return kInvalidRequestSessionId; 281 return kInvalidRequestSessionId;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 if (it == pending_join_session_cbs_.end()) 351 if (it == pending_join_session_cbs_.end())
352 return false; 352 return false;
353 353
354 DCHECK(it->second.get()); 354 DCHECK(it->second.get());
355 it->second->Run(std::move(session), std::move(error)); 355 it->second->Run(std::move(session), std::move(error));
356 pending_join_session_cbs_.erase(it); 356 pending_join_session_cbs_.erase(it);
357 return true; 357 return true;
358 } 358 }
359 359
360 void PresentationServiceImpl::SetDefaultPresentationURL( 360 void PresentationServiceImpl::SetDefaultPresentationURL(
361 const mojo::String& url) { 361 const std::string& url) {
362 DVLOG(2) << "SetDefaultPresentationURL"; 362 DVLOG(2) << "SetDefaultPresentationURL";
363 if (!delegate_) 363 if (!delegate_)
364 return; 364 return;
365 365
366 const std::string& new_default_url = url.get(); 366 if (default_presentation_url_ == url)
367 if (default_presentation_url_ == new_default_url)
368 return; 367 return;
369 368
370 default_presentation_url_ = new_default_url; 369 default_presentation_url_ = url;
371 delegate_->SetDefaultPresentationUrl( 370 delegate_->SetDefaultPresentationUrl(
372 render_process_id_, render_frame_id_, new_default_url, 371 render_process_id_, render_frame_id_, url,
373 base::Bind(&PresentationServiceImpl::OnDefaultPresentationStarted, 372 base::Bind(&PresentationServiceImpl::OnDefaultPresentationStarted,
374 weak_factory_.GetWeakPtr())); 373 weak_factory_.GetWeakPtr()));
375 } 374 }
376 375
377 void PresentationServiceImpl::SendSessionMessage( 376 void PresentationServiceImpl::SendSessionMessage(
378 blink::mojom::PresentationSessionInfoPtr session, 377 blink::mojom::PresentationSessionInfoPtr session,
379 blink::mojom::SessionMessagePtr session_message, 378 blink::mojom::SessionMessagePtr session_message,
380 const SendSessionMessageCallback& callback) { 379 const SendSessionMessageCallback& callback) {
381 DVLOG(2) << "SendSessionMessage"; 380 DVLOG(2) << "SendSessionMessage";
382 DCHECK(!session_message.is_null()); 381 DCHECK(!session_message.is_null());
(...skipping 16 matching lines...) Expand all
399 void PresentationServiceImpl::OnSendMessageCallback(bool sent) { 398 void PresentationServiceImpl::OnSendMessageCallback(bool sent) {
400 // It is possible that Reset() is invoked before receiving this callback. 399 // It is possible that Reset() is invoked before receiving this callback.
401 // So, always check send_message_callback_ for non-null. 400 // So, always check send_message_callback_ for non-null.
402 if (send_message_callback_) { 401 if (send_message_callback_) {
403 send_message_callback_->Run(sent); 402 send_message_callback_->Run(sent);
404 send_message_callback_.reset(); 403 send_message_callback_.reset();
405 } 404 }
406 } 405 }
407 406
408 void PresentationServiceImpl::CloseConnection( 407 void PresentationServiceImpl::CloseConnection(
409 const mojo::String& presentation_url, 408 const std::string& presentation_url,
410 const mojo::String& presentation_id) { 409 const std::string& presentation_id) {
411 DVLOG(2) << "CloseConnection " << presentation_id; 410 DVLOG(2) << "CloseConnection " << presentation_id;
412 if (delegate_) 411 if (delegate_)
413 delegate_->CloseConnection(render_process_id_, render_frame_id_, 412 delegate_->CloseConnection(render_process_id_, render_frame_id_,
414 presentation_id); 413 presentation_id);
415 } 414 }
416 415
417 void PresentationServiceImpl::Terminate(const mojo::String& presentation_url, 416 void PresentationServiceImpl::Terminate(const std::string& presentation_url,
418 const mojo::String& presentation_id) { 417 const std::string& presentation_id) {
419 DVLOG(2) << "Terminate " << presentation_id; 418 DVLOG(2) << "Terminate " << presentation_id;
420 if (delegate_) 419 if (delegate_)
421 delegate_->Terminate(render_process_id_, render_frame_id_, presentation_id); 420 delegate_->Terminate(render_process_id_, render_frame_id_, presentation_id);
422 } 421 }
423 422
424 void PresentationServiceImpl::OnConnectionStateChanged( 423 void PresentationServiceImpl::OnConnectionStateChanged(
425 const PresentationSessionInfo& connection, 424 const PresentationSessionInfo& connection,
426 const PresentationConnectionStateChangeInfo& info) { 425 const PresentationConnectionStateChangeInfo& info) {
427 DCHECK(client_.get()); 426 DCHECK(client_.get());
428 if (info.state == PRESENTATION_CONNECTION_STATE_CLOSED) { 427 if (info.state == PRESENTATION_CONNECTION_STATE_CLOSED) {
(...skipping 30 matching lines...) Expand all
459 weak_factory_.GetWeakPtr(), session_info)); 458 weak_factory_.GetWeakPtr(), session_info));
460 } 459 }
461 460
462 void PresentationServiceImpl::OnSessionMessages( 461 void PresentationServiceImpl::OnSessionMessages(
463 const PresentationSessionInfo& session, 462 const PresentationSessionInfo& session,
464 const ScopedVector<PresentationSessionMessage>& messages, 463 const ScopedVector<PresentationSessionMessage>& messages,
465 bool pass_ownership) { 464 bool pass_ownership) {
466 DCHECK(client_); 465 DCHECK(client_);
467 466
468 DVLOG(2) << "OnSessionMessages"; 467 DVLOG(2) << "OnSessionMessages";
469 mojo::Array<blink::mojom::SessionMessagePtr> mojoMessages(messages.size()); 468 std::vector<blink::mojom::SessionMessagePtr> mojo_messages(messages.size());
470 for (size_t i = 0; i < messages.size(); ++i) 469 std::transform(messages.begin(), messages.end(), mojo_messages.begin(),
471 mojoMessages[i] = ToMojoSessionMessage(messages[i], pass_ownership); 470 [pass_ownership](PresentationSessionMessage* message) {
471 return ToMojoSessionMessage(message, pass_ownership);
472 });
472 473
473 client_->OnSessionMessagesReceived( 474 client_->OnSessionMessagesReceived(
474 blink::mojom::PresentationSessionInfo::From(session), 475 blink::mojom::PresentationSessionInfo::From(session),
475 std::move(mojoMessages)); 476 std::move(mojo_messages));
476 } 477 }
477 478
478 void PresentationServiceImpl::DidNavigateAnyFrame( 479 void PresentationServiceImpl::DidNavigateAnyFrame(
479 content::RenderFrameHost* render_frame_host, 480 content::RenderFrameHost* render_frame_host,
480 const content::LoadCommittedDetails& details, 481 const content::LoadCommittedDetails& details,
481 const content::FrameNavigateParams& params) { 482 const content::FrameNavigateParams& params) {
482 DVLOG(2) << "PresentationServiceImpl::DidNavigateAnyFrame"; 483 DVLOG(2) << "PresentationServiceImpl::DidNavigateAnyFrame";
483 if (!FrameMatches(render_frame_host)) 484 if (!FrameMatches(render_frame_host))
484 return; 485 return;
485 486
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
602 603
603 void PresentationServiceImpl::NewSessionCallbackWrapper::Run( 604 void PresentationServiceImpl::NewSessionCallbackWrapper::Run(
604 blink::mojom::PresentationSessionInfoPtr session, 605 blink::mojom::PresentationSessionInfoPtr session,
605 blink::mojom::PresentationErrorPtr error) { 606 blink::mojom::PresentationErrorPtr error) {
606 DCHECK(!callback_.is_null()); 607 DCHECK(!callback_.is_null());
607 callback_.Run(std::move(session), std::move(error)); 608 callback_.Run(std::move(session), std::move(error));
608 callback_.Reset(); 609 callback_.Reset();
609 } 610 }
610 611
611 } // namespace content 612 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698