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

Side by Side Diff: content/renderer/presentation/presentation_dispatcher.cc

Issue 2471263003: [Presentation API] (4th)(1-UA blink side) Add WebPresentationConnection and WebPresentationConnecti… (Closed)
Patch Set: resolve code review comments from dcheng Created 3 years, 11 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/renderer/presentation/presentation_dispatcher.h" 5 #include "content/renderer/presentation/presentation_dispatcher.h"
6 6
7 #include <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/threading/thread_task_runner_handle.h" 14 #include "base/threading/thread_task_runner_handle.h"
15 #include "content/public/common/presentation_constants.h" 15 #include "content/public/common/presentation_constants.h"
16 #include "content/public/renderer/render_frame.h" 16 #include "content/public/renderer/render_frame.h"
17 #include "mojo/public/cpp/bindings/type_converter.h" 17 #include "mojo/public/cpp/bindings/type_converter.h"
18 #include "services/service_manager/public/cpp/interface_provider.h" 18 #include "services/service_manager/public/cpp/interface_provider.h"
19 #include "third_party/WebKit/public/platform/WebString.h" 19 #include "third_party/WebKit/public/platform/WebString.h"
20 #include "third_party/WebKit/public/platform/WebURL.h" 20 #include "third_party/WebKit/public/platform/WebURL.h"
21 #include "third_party/WebKit/public/platform/WebVector.h" 21 #include "third_party/WebKit/public/platform/WebVector.h"
22 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nAvailabilityObserver.h" 22 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nAvailabilityObserver.h"
23 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nConnectionCallbacks.h"
23 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nController.h" 24 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nController.h"
24 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nError.h" 25 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nError.h"
25 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nReceiver.h" 26 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nReceiver.h"
26 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nSessionInfo.h" 27 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nSessionInfo.h"
27 #include "third_party/WebKit/public/platform/modules/presentation/presentation.m ojom.h" 28 #include "third_party/WebKit/public/platform/modules/presentation/presentation.m ojom.h"
28 #include "third_party/WebKit/public/web/WebLocalFrame.h" 29 #include "third_party/WebKit/public/web/WebLocalFrame.h"
29 #include "url/gurl.h" 30 #include "url/gurl.h"
30 31
31 namespace mojo { 32 namespace mojo {
32 33
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 } 94 }
94 } 95 }
95 96
96 } // namespace 97 } // namespace
97 98
98 namespace content { 99 namespace content {
99 100
100 PresentationDispatcher::PresentationDispatcher(RenderFrame* render_frame) 101 PresentationDispatcher::PresentationDispatcher(RenderFrame* render_frame)
101 : RenderFrameObserver(render_frame), 102 : RenderFrameObserver(render_frame),
102 controller_(nullptr), 103 controller_(nullptr),
103 binding_(this) { 104 receiver_(nullptr),
104 } 105 binding_(this) {}
105 106
106 PresentationDispatcher::~PresentationDispatcher() { 107 PresentationDispatcher::~PresentationDispatcher() {
107 // Controller should be destroyed before the dispatcher when frame is 108 // Controller should be destroyed before the dispatcher when frame is
108 // destroyed. 109 // destroyed.
109 DCHECK(!controller_); 110 DCHECK(!controller_);
110 } 111 }
111 112
112 void PresentationDispatcher::setController( 113 void PresentationDispatcher::setController(
113 blink::WebPresentationController* controller) { 114 blink::WebPresentationController* controller) {
114 // There shouldn't be any swapping from one non-null controller to another. 115 // There shouldn't be any swapping from one non-null controller to another.
115 DCHECK(controller != controller_ && (!controller || !controller_)); 116 DCHECK(controller != controller_ && (!controller || !controller_));
116 controller_ = controller; 117 controller_ = controller;
117 // The controller is set to null when the frame is about to be detached. 118 // The controller is set to null when the frame is about to be detached.
118 // Nothing is listening for screen availability anymore but the Mojo service 119 // Nothing is listening for screen availability anymore but the Mojo service
119 // will know about the frame being detached anyway. 120 // will know about the frame being detached anyway.
120 } 121 }
121 122
122 void PresentationDispatcher::startSession( 123 void PresentationDispatcher::startSession(
123 const blink::WebVector<blink::WebURL>& presentationUrls, 124 const blink::WebVector<blink::WebURL>& presentationUrls,
124 std::unique_ptr<blink::WebPresentationConnectionCallback> callback) { 125 std::unique_ptr<blink::WebPresentationConnectionCallbacks> callback) {
125 DCHECK(callback); 126 DCHECK(callback);
126 ConnectToPresentationServiceIfNeeded(); 127 ConnectToPresentationServiceIfNeeded();
127 128
128 std::vector<GURL> urls; 129 std::vector<GURL> urls;
129 for (const auto& url : presentationUrls) 130 for (const auto& url : presentationUrls)
130 urls.push_back(url); 131 urls.push_back(url);
131 132
132 // The dispatcher owns the service so |this| will be valid when 133 // The dispatcher owns the service so |this| will be valid when
133 // OnSessionCreated() is called. |callback| needs to be alive and also needs 134 // OnSessionCreated() is called. |callback| needs to be alive and also needs
134 // to be destroyed so we transfer its ownership to the mojo callback. 135 // to be destroyed so we transfer its ownership to the mojo callback.
135 presentation_service_->StartSession( 136 presentation_service_->StartSession(
136 urls, base::Bind(&PresentationDispatcher::OnSessionCreated, 137 urls, base::Bind(&PresentationDispatcher::OnSessionCreated,
137 base::Unretained(this), base::Passed(&callback))); 138 base::Unretained(this), base::Passed(&callback)));
138 } 139 }
139 140
140 void PresentationDispatcher::joinSession( 141 void PresentationDispatcher::joinSession(
141 const blink::WebVector<blink::WebURL>& presentationUrls, 142 const blink::WebVector<blink::WebURL>& presentationUrls,
142 const blink::WebString& presentationId, 143 const blink::WebString& presentationId,
143 std::unique_ptr<blink::WebPresentationConnectionCallback> callback) { 144 std::unique_ptr<blink::WebPresentationConnectionCallbacks> callback) {
144 DCHECK(callback); 145 DCHECK(callback);
145 ConnectToPresentationServiceIfNeeded(); 146 ConnectToPresentationServiceIfNeeded();
146 147
147 std::vector<GURL> urls; 148 std::vector<GURL> urls;
148 for (const auto& url : presentationUrls) 149 for (const auto& url : presentationUrls)
149 urls.push_back(url); 150 urls.push_back(url);
150 151
151 // The dispatcher owns the service so |this| will be valid when 152 // The dispatcher owns the service so |this| will be valid when
152 // OnSessionCreated() is called. |callback| needs to be alive and also needs 153 // OnSessionCreated() is called. |callback| needs to be alive and also needs
153 // to be destroyed so we transfer its ownership to the mojo callback. 154 // to be destroyed so we transfer its ownership to the mojo callback.
154 presentation_service_->JoinSession( 155 presentation_service_->JoinSession(
155 urls, presentationId.utf8(), 156 urls, presentationId.utf8(),
156 base::Bind(&PresentationDispatcher::OnSessionCreated, 157 base::Bind(&PresentationDispatcher::OnSessionCreated,
157 base::Unretained(this), base::Passed(&callback))); 158 base::Unretained(this), base::Passed(&callback)));
158 } 159 }
159 160
160 void PresentationDispatcher::sendString(const blink::WebURL& presentationUrl, 161 void PresentationDispatcher::sendString(
161 const blink::WebString& presentationId, 162 const blink::WebURL& presentationUrl,
162 const blink::WebString& message) { 163 const blink::WebString& presentationId,
164 const blink::WebString& message,
165 const blink::WebPresentationConnectionProxy* connection_proxy) {
163 if (message.utf8().size() > kMaxPresentationConnectionMessageSize) { 166 if (message.utf8().size() > kMaxPresentationConnectionMessageSize) {
164 // TODO(crbug.com/459008): Limit the size of individual messages to 64k 167 // TODO(crbug.com/459008): Limit the size of individual messages to 64k
165 // for now. Consider throwing DOMException or splitting bigger messages 168 // for now. Consider throwing DOMException or splitting bigger messages
166 // into smaller chunks later. 169 // into smaller chunks later.
167 LOG(WARNING) << "message size exceeded limit!"; 170 LOG(WARNING) << "message size exceeded limit!";
168 return; 171 return;
169 } 172 }
170 173
171 message_request_queue_.push(base::WrapUnique( 174 message_request_queue_.push(base::WrapUnique(CreateSendTextMessageRequest(
172 CreateSendTextMessageRequest(presentationUrl, presentationId, message))); 175 presentationUrl, presentationId, message, connection_proxy)));
173 // Start processing request if only one in the queue. 176 // Start processing request if only one in the queue.
174 if (message_request_queue_.size() == 1) 177 if (message_request_queue_.size() == 1)
175 DoSendMessage(message_request_queue_.front().get()); 178 DoSendMessage(message_request_queue_.front().get());
176 } 179 }
177 180
178 void PresentationDispatcher::sendArrayBuffer( 181 void PresentationDispatcher::sendArrayBuffer(
179 const blink::WebURL& presentationUrl, 182 const blink::WebURL& presentationUrl,
180 const blink::WebString& presentationId, 183 const blink::WebString& presentationId,
181 const uint8_t* data, 184 const uint8_t* data,
182 size_t length) { 185 size_t length,
186 const blink::WebPresentationConnectionProxy* connection_proxy) {
183 DCHECK(data); 187 DCHECK(data);
184 if (length > kMaxPresentationConnectionMessageSize) { 188 if (length > kMaxPresentationConnectionMessageSize) {
185 // TODO(crbug.com/459008): Same as in sendString(). 189 // TODO(crbug.com/459008): Same as in sendString().
186 LOG(WARNING) << "data size exceeded limit!"; 190 LOG(WARNING) << "data size exceeded limit!";
187 return; 191 return;
188 } 192 }
189 193
190 message_request_queue_.push(base::WrapUnique(CreateSendBinaryMessageRequest( 194 message_request_queue_.push(base::WrapUnique(CreateSendBinaryMessageRequest(
191 presentationUrl, presentationId, 195 presentationUrl, presentationId,
192 blink::mojom::PresentationMessageType::BINARY, data, length))); 196 blink::mojom::PresentationMessageType::BINARY, data, length,
197 connection_proxy)));
193 // Start processing request if only one in the queue. 198 // Start processing request if only one in the queue.
194 if (message_request_queue_.size() == 1) 199 if (message_request_queue_.size() == 1)
195 DoSendMessage(message_request_queue_.front().get()); 200 DoSendMessage(message_request_queue_.front().get());
196 } 201 }
197 202
198 void PresentationDispatcher::sendBlobData( 203 void PresentationDispatcher::sendBlobData(
199 const blink::WebURL& presentationUrl, 204 const blink::WebURL& presentationUrl,
200 const blink::WebString& presentationId, 205 const blink::WebString& presentationId,
201 const uint8_t* data, 206 const uint8_t* data,
202 size_t length) { 207 size_t length,
208 const blink::WebPresentationConnectionProxy* connection_proxy) {
203 DCHECK(data); 209 DCHECK(data);
204 if (length > kMaxPresentationConnectionMessageSize) { 210 if (length > kMaxPresentationConnectionMessageSize) {
205 // TODO(crbug.com/459008): Same as in sendString(). 211 // TODO(crbug.com/459008): Same as in sendString().
206 LOG(WARNING) << "data size exceeded limit!"; 212 LOG(WARNING) << "data size exceeded limit!";
207 return; 213 return;
208 } 214 }
209 215
210 message_request_queue_.push(base::WrapUnique(CreateSendBinaryMessageRequest( 216 message_request_queue_.push(base::WrapUnique(CreateSendBinaryMessageRequest(
211 presentationUrl, presentationId, 217 presentationUrl, presentationId,
212 blink::mojom::PresentationMessageType::BINARY, data, length))); 218 blink::mojom::PresentationMessageType::BINARY, data, length,
219 connection_proxy)));
213 // Start processing request if only one in the queue. 220 // Start processing request if only one in the queue.
214 if (message_request_queue_.size() == 1) 221 if (message_request_queue_.size() == 1)
215 DoSendMessage(message_request_queue_.front().get()); 222 DoSendMessage(message_request_queue_.front().get());
216 } 223 }
217 224
218 void PresentationDispatcher::DoSendMessage(SendMessageRequest* request) { 225 void PresentationDispatcher::DoSendMessage(SendMessageRequest* request) {
219 ConnectToPresentationServiceIfNeeded(); 226 ConnectToPresentationServiceIfNeeded();
220 227
221 presentation_service_->SendConnectionMessage( 228 presentation_service_->SendConnectionMessage(
222 std::move(request->session_info), std::move(request->message), 229 std::move(request->session_info), std::move(request->message),
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 return; 407 return;
401 408
402 if (!session_info.is_null()) { 409 if (!session_info.is_null()) {
403 presentation_service_->ListenForConnectionMessages(session_info.Clone()); 410 presentation_service_->ListenForConnectionMessages(session_info.Clone());
404 controller_->didStartDefaultSession( 411 controller_->didStartDefaultSession(
405 mojo::ConvertTo<blink::WebPresentationSessionInfo>(session_info)); 412 mojo::ConvertTo<blink::WebPresentationSessionInfo>(session_info));
406 } 413 }
407 } 414 }
408 415
409 void PresentationDispatcher::OnSessionCreated( 416 void PresentationDispatcher::OnSessionCreated(
410 std::unique_ptr<blink::WebPresentationConnectionCallback> callback, 417 std::unique_ptr<blink::WebPresentationConnectionCallbacks> callback,
411 blink::mojom::PresentationSessionInfoPtr session_info, 418 blink::mojom::PresentationSessionInfoPtr session_info,
412 blink::mojom::PresentationErrorPtr error) { 419 blink::mojom::PresentationErrorPtr error) {
413 DCHECK(callback); 420 DCHECK(callback);
414 if (!error.is_null()) { 421 if (!error.is_null()) {
415 DCHECK(session_info.is_null()); 422 DCHECK(session_info.is_null());
416 callback->onError(blink::WebPresentationError( 423 callback->onError(blink::WebPresentationError(
417 GetWebPresentationErrorTypeFromMojo(error->error_type), 424 GetWebPresentationErrorTypeFromMojo(error->error_type),
418 blink::WebString::fromUTF8(error->message))); 425 blink::WebString::fromUTF8(error->message)));
419 return; 426 return;
420 } 427 }
421 428
422 DCHECK(!session_info.is_null()); 429 DCHECK(!session_info.is_null());
423 presentation_service_->ListenForConnectionMessages(session_info.Clone()); 430 presentation_service_->ListenForConnectionMessages(session_info.Clone());
424 callback->onSuccess( 431 callback->onSuccess(
425 mojo::ConvertTo<blink::WebPresentationSessionInfo>(session_info)); 432 mojo::ConvertTo<blink::WebPresentationSessionInfo>(session_info));
426 } 433 }
427 434
428 void PresentationDispatcher::OnReceiverConnectionAvailable( 435 void PresentationDispatcher::OnReceiverConnectionAvailable(
429 blink::mojom::PresentationSessionInfoPtr session_info, 436 blink::mojom::PresentationSessionInfoPtr session_info,
430 blink::mojom::PresentationConnectionPtr, 437 blink::mojom::PresentationConnectionPtr,
431 blink::mojom::PresentationConnectionRequest) { 438 blink::mojom::PresentationConnectionRequest) {
432 if (receiver_) { 439 if (!receiver_) {
imcheng 2017/01/20 20:15:43 revert?
zhaobin 2017/01/23 19:38:49 Done.
433 receiver_->onReceiverConnectionAvailable( 440 receiver_->onReceiverConnectionAvailable(
434 mojo::ConvertTo<blink::WebPresentationSessionInfo>(session_info)); 441 mojo::ConvertTo<blink::WebPresentationSessionInfo>(session_info));
435 } 442 }
436 } 443 }
437 444
438 void PresentationDispatcher::OnConnectionStateChanged( 445 void PresentationDispatcher::OnConnectionStateChanged(
439 blink::mojom::PresentationSessionInfoPtr session_info, 446 blink::mojom::PresentationSessionInfoPtr session_info,
440 blink::mojom::PresentationConnectionState state) { 447 blink::mojom::PresentationConnectionState state) {
441 if (!controller_) 448 if (!controller_)
442 return; 449 return;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 status->listening_state = ListeningState::WAITING; 521 status->listening_state = ListeningState::WAITING;
515 presentation_service_->ListenForScreenAvailability(status->url); 522 presentation_service_->ListenForScreenAvailability(status->url);
516 } else { 523 } else {
517 status->listening_state = ListeningState::INACTIVE; 524 status->listening_state = ListeningState::INACTIVE;
518 presentation_service_->StopListeningForScreenAvailability(status->url); 525 presentation_service_->StopListeningForScreenAvailability(status->url);
519 } 526 }
520 } 527 }
521 528
522 PresentationDispatcher::SendMessageRequest::SendMessageRequest( 529 PresentationDispatcher::SendMessageRequest::SendMessageRequest(
523 blink::mojom::PresentationSessionInfoPtr session_info, 530 blink::mojom::PresentationSessionInfoPtr session_info,
524 blink::mojom::ConnectionMessagePtr message) 531 blink::mojom::ConnectionMessagePtr message,
525 : session_info(std::move(session_info)), message(std::move(message)) {} 532 const blink::WebPresentationConnectionProxy* connection_proxy)
533 : session_info(std::move(session_info)),
534 message(std::move(message)),
535 connection_proxy(connection_proxy) {}
526 536
527 PresentationDispatcher::SendMessageRequest::~SendMessageRequest() {} 537 PresentationDispatcher::SendMessageRequest::~SendMessageRequest() {}
528 538
529 // static 539 // static
530 PresentationDispatcher::SendMessageRequest* 540 PresentationDispatcher::SendMessageRequest*
531 PresentationDispatcher::CreateSendTextMessageRequest( 541 PresentationDispatcher::CreateSendTextMessageRequest(
532 const blink::WebURL& presentationUrl, 542 const blink::WebURL& presentationUrl,
533 const blink::WebString& presentationId, 543 const blink::WebString& presentationId,
534 const blink::WebString& message) { 544 const blink::WebString& message,
545 const blink::WebPresentationConnectionProxy* connection_proxy) {
535 blink::mojom::PresentationSessionInfoPtr session_info = 546 blink::mojom::PresentationSessionInfoPtr session_info =
536 blink::mojom::PresentationSessionInfo::New(); 547 blink::mojom::PresentationSessionInfo::New();
537 session_info->url = presentationUrl; 548 session_info->url = presentationUrl;
538 session_info->id = presentationId.utf8(); 549 session_info->id = presentationId.utf8();
539 550
540 blink::mojom::ConnectionMessagePtr session_message = 551 blink::mojom::ConnectionMessagePtr session_message =
541 blink::mojom::ConnectionMessage::New(); 552 blink::mojom::ConnectionMessage::New();
542 session_message->type = blink::mojom::PresentationMessageType::TEXT; 553 session_message->type = blink::mojom::PresentationMessageType::TEXT;
543 session_message->message = message.utf8(); 554 session_message->message = message.utf8();
544 return new SendMessageRequest(std::move(session_info), 555 return new SendMessageRequest(std::move(session_info),
545 std::move(session_message)); 556 std::move(session_message), connection_proxy);
546 } 557 }
547 558
548 // static 559 // static
549 PresentationDispatcher::SendMessageRequest* 560 PresentationDispatcher::SendMessageRequest*
550 PresentationDispatcher::CreateSendBinaryMessageRequest( 561 PresentationDispatcher::CreateSendBinaryMessageRequest(
551 const blink::WebURL& presentationUrl, 562 const blink::WebURL& presentationUrl,
552 const blink::WebString& presentationId, 563 const blink::WebString& presentationId,
553 blink::mojom::PresentationMessageType type, 564 blink::mojom::PresentationMessageType type,
554 const uint8_t* data, 565 const uint8_t* data,
555 size_t length) { 566 size_t length,
567 const blink::WebPresentationConnectionProxy* connection_proxy) {
556 blink::mojom::PresentationSessionInfoPtr session_info = 568 blink::mojom::PresentationSessionInfoPtr session_info =
557 blink::mojom::PresentationSessionInfo::New(); 569 blink::mojom::PresentationSessionInfo::New();
558 session_info->url = presentationUrl; 570 session_info->url = presentationUrl;
559 session_info->id = presentationId.utf8(); 571 session_info->id = presentationId.utf8();
560 572
561 blink::mojom::ConnectionMessagePtr session_message = 573 blink::mojom::ConnectionMessagePtr session_message =
562 blink::mojom::ConnectionMessage::New(); 574 blink::mojom::ConnectionMessage::New();
563 session_message->type = type; 575 session_message->type = type;
564 session_message->data = std::vector<uint8_t>(data, data + length); 576 session_message->data = std::vector<uint8_t>(data, data + length);
565 return new SendMessageRequest(std::move(session_info), 577 return new SendMessageRequest(std::move(session_info),
566 std::move(session_message)); 578 std::move(session_message), connection_proxy);
567 } 579 }
568 580
569 PresentationDispatcher::AvailabilityStatus::AvailabilityStatus( 581 PresentationDispatcher::AvailabilityStatus::AvailabilityStatus(
570 const GURL& availability_url) 582 const GURL& availability_url)
571 : url(availability_url), 583 : url(availability_url),
572 last_known_availability(false), 584 last_known_availability(false),
573 listening_state(ListeningState::INACTIVE) {} 585 listening_state(ListeningState::INACTIVE) {}
574 586
575 PresentationDispatcher::AvailabilityStatus::~AvailabilityStatus() { 587 PresentationDispatcher::AvailabilityStatus::~AvailabilityStatus() {
576 } 588 }
577 589
578 } // namespace content 590 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698