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

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

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

Powered by Google App Engine
This is Rietveld 408576698