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

Side by Side Diff: extensions/browser/api/cast_channel/cast_channel_api.cc

Issue 2942993003: [cast_channel] Make CastMessageHandler a CastSocket::Observer instead of CastTransport::Delegate (Closed)
Patch Set: Created 3 years, 6 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "extensions/browser/api/cast_channel/cast_channel_api.h" 5 #include "extensions/browser/api/cast_channel/cast_channel_api.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <limits> 10 #include <limits>
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 *ip_endpoint_, ExtensionsBrowserClient::Get()->GetNetLog(), 287 *ip_endpoint_, ExtensionsBrowserClient::Get()->GetNetLog(),
288 base::TimeDelta::FromMilliseconds(connect_info.timeout.get() 288 base::TimeDelta::FromMilliseconds(connect_info.timeout.get()
289 ? *connect_info.timeout 289 ? *connect_info.timeout
290 : kDefaultConnectTimeoutMillis), 290 : kDefaultConnectTimeoutMillis),
291 liveness_timeout_, ping_interval_, api_->GetLogger(), 291 liveness_timeout_, ping_interval_, api_->GetLogger(),
292 connect_info.capabilities.get() ? *connect_info.capabilities 292 connect_info.capabilities.get() ? *connect_info.capabilities
293 : CastDeviceCapability::NONE); 293 : CastDeviceCapability::NONE);
294 } 294 }
295 new_channel_id_ = cast_socket_service_->AddSocket(base::WrapUnique(socket)); 295 new_channel_id_ = cast_socket_service_->AddSocket(base::WrapUnique(socket));
296 296
297 if (!socket->HasObserver(extension_->id())) {
imcheng 2017/06/16 23:39:25 Since we are thinking about sharing CastSocket acr
zhaobin 2017/06/20 00:54:36 Doing it a little differently. 1. Create one obse
298 socket->AddObserver(extension_->id(),
mark a. foltz 2017/06/16 23:26:14 I think this code should only run once per extensi
zhaobin 2017/06/20 00:54:36 Yes. Done.
299 base::MakeUnique<CastMessageHandler>(
300 base::Bind(&CastChannelAPI::SendEvent,
301 api_->AsWeakPtr(), extension_->id()),
302 api_->GetLogger()));
303 }
297 // Construct read delegates. 304 // Construct read delegates.
298 std::unique_ptr<CastTransport::Delegate> delegate( 305 socket->Connect(base::Bind(&CastChannelOpenFunction::OnOpen, this));
299 base::MakeUnique<CastMessageHandler>(
300 base::Bind(&CastChannelAPI::SendEvent, api_->AsWeakPtr(),
301 extension_->id()),
302 socket, api_->GetLogger()));
303 socket->Connect(std::move(delegate),
304 base::Bind(&CastChannelOpenFunction::OnOpen, this));
305 } 306 }
306 307
307 void CastChannelOpenFunction::OnOpen(ChannelError result) { 308 void CastChannelOpenFunction::OnOpen(ChannelError result) {
308 DCHECK_CURRENTLY_ON(BrowserThread::IO); 309 DCHECK_CURRENTLY_ON(BrowserThread::IO);
309 VLOG(1) << "Connect finished, OnOpen invoked."; 310 VLOG(1) << "Connect finished, OnOpen invoked.";
310 // TODO: If we failed to open the CastSocket, we may want to clean up here, 311 // TODO: If we failed to open the CastSocket, we may want to clean up here,
311 // rather than relying on the extension to call close(). This can be done by 312 // rather than relying on the extension to call close(). This can be done by
312 // calling RemoveSocket() and api_->GetLogger()->ClearLastError(channel_id). 313 // calling RemoveSocket() and api_->GetLogger()->ClearLastError(channel_id).
313 if (result != ChannelError::UNKNOWN) { 314 if (result != ChannelError::UNKNOWN) {
314 CastSocket* socket = cast_socket_service_->GetSocket(new_channel_id_); 315 CastSocket* socket = cast_socket_service_->GetSocket(new_channel_id_);
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 SetResultFromSocket(*socket); 426 SetResultFromSocket(*socket);
426 // This will delete |socket|. 427 // This will delete |socket|.
427 cast_socket_service_->RemoveSocket(channel_id); 428 cast_socket_service_->RemoveSocket(channel_id);
428 api_->GetLogger()->ClearLastError(channel_id); 429 api_->GetLogger()->ClearLastError(channel_id);
429 } 430 }
430 AsyncWorkCompleted(); 431 AsyncWorkCompleted();
431 } 432 }
432 433
433 CastChannelOpenFunction::CastMessageHandler::CastMessageHandler( 434 CastChannelOpenFunction::CastMessageHandler::CastMessageHandler(
434 const EventDispatchCallback& ui_dispatch_cb, 435 const EventDispatchCallback& ui_dispatch_cb,
435 CastSocket* socket,
436 scoped_refptr<Logger> logger) 436 scoped_refptr<Logger> logger)
437 : ui_dispatch_cb_(ui_dispatch_cb), socket_(socket), logger_(logger) { 437 : ui_dispatch_cb_(ui_dispatch_cb), logger_(logger) {
438 DCHECK(socket_);
439 DCHECK(logger_); 438 DCHECK(logger_);
440 } 439 }
441 440
442 CastChannelOpenFunction::CastMessageHandler::~CastMessageHandler() { 441 CastChannelOpenFunction::CastMessageHandler::~CastMessageHandler() {
443 } 442 }
444 443
445 void CastChannelOpenFunction::CastMessageHandler::OnError( 444 void CastChannelOpenFunction::CastMessageHandler::OnError(
445 const cast_channel::CastSocket& socket,
446 ChannelError error_state) { 446 ChannelError error_state) {
447 DCHECK_CURRENTLY_ON(BrowserThread::IO); 447 DCHECK_CURRENTLY_ON(BrowserThread::IO);
448 448
449 ChannelInfo channel_info; 449 ChannelInfo channel_info;
450 FillChannelInfo(*socket_, &channel_info); 450 FillChannelInfo(socket, &channel_info);
451 channel_info.error_state = api::cast_channel::ToChannelError(error_state); 451 channel_info.error_state = api::cast_channel::ToChannelError(error_state);
452 ErrorInfo error_info; 452 ErrorInfo error_info;
453 FillErrorInfo(channel_info.error_state, logger_->GetLastError(socket_->id()), 453 FillErrorInfo(channel_info.error_state, logger_->GetLastError(socket.id()),
454 &error_info); 454 &error_info);
455 455
456 std::unique_ptr<base::ListValue> results = 456 std::unique_ptr<base::ListValue> results =
457 OnError::Create(channel_info, error_info); 457 OnError::Create(channel_info, error_info);
458 std::unique_ptr<Event> event(new Event( 458 std::unique_ptr<Event> event(new Event(
459 events::CAST_CHANNEL_ON_ERROR, OnError::kEventName, std::move(results))); 459 events::CAST_CHANNEL_ON_ERROR, OnError::kEventName, std::move(results)));
460 BrowserThread::PostTask( 460 BrowserThread::PostTask(
461 BrowserThread::UI, FROM_HERE, 461 BrowserThread::UI, FROM_HERE,
462 base::Bind(ui_dispatch_cb_, base::Passed(std::move(event)))); 462 base::Bind(ui_dispatch_cb_, base::Passed(std::move(event))));
463 } 463 }
464 464
465 void CastChannelOpenFunction::CastMessageHandler::OnMessage( 465 void CastChannelOpenFunction::CastMessageHandler::OnMessage(
466 const cast_channel::CastSocket& socket,
466 const CastMessage& message) { 467 const CastMessage& message) {
467 DCHECK_CURRENTLY_ON(BrowserThread::IO); 468 DCHECK_CURRENTLY_ON(BrowserThread::IO);
468 469
469 MessageInfo message_info; 470 MessageInfo message_info;
470 CastMessageToMessageInfo(message, &message_info); 471 CastMessageToMessageInfo(message, &message_info);
471 ChannelInfo channel_info; 472 ChannelInfo channel_info;
472 FillChannelInfo(*socket_, &channel_info); 473 FillChannelInfo(socket, &channel_info);
473 VLOG(1) << "Received message " << ParamToString(message_info) 474 VLOG(1) << "Received message " << ParamToString(message_info)
474 << " on channel " << ParamToString(channel_info); 475 << " on channel " << ParamToString(channel_info);
475 476
476 std::unique_ptr<base::ListValue> results = 477 std::unique_ptr<base::ListValue> results =
477 OnMessage::Create(channel_info, message_info); 478 OnMessage::Create(channel_info, message_info);
478 std::unique_ptr<Event> event(new Event(events::CAST_CHANNEL_ON_MESSAGE, 479 std::unique_ptr<Event> event(new Event(events::CAST_CHANNEL_ON_MESSAGE,
479 OnMessage::kEventName, 480 OnMessage::kEventName,
480 std::move(results))); 481 std::move(results)));
481 BrowserThread::PostTask( 482 BrowserThread::PostTask(
482 BrowserThread::UI, FROM_HERE, 483 BrowserThread::UI, FROM_HERE,
483 base::Bind(ui_dispatch_cb_, base::Passed(std::move(event)))); 484 base::Bind(ui_dispatch_cb_, base::Passed(std::move(event))));
484 } 485 }
485 486
486 void CastChannelOpenFunction::CastMessageHandler::Start() {
487 }
488
489 } // namespace extensions 487 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698