| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/extensions/api/cast_channel/cast_socket.h" | 5 #include "extensions/browser/api/cast_channel/cast_socket.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/numerics/safe_conversions.h" | 13 #include "base/numerics/safe_conversions.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/sys_byteorder.h" | 15 #include "base/sys_byteorder.h" |
| 16 #include "chrome/browser/extensions/api/cast_channel/cast_auth_util.h" | 16 #include "extensions/browser/api/cast_channel/cast_auth_util.h" |
| 17 #include "chrome/browser/extensions/api/cast_channel/cast_channel.pb.h" | 17 #include "extensions/browser/api/cast_channel/cast_channel.pb.h" |
| 18 #include "chrome/browser/extensions/api/cast_channel/cast_message_util.h" | 18 #include "extensions/browser/api/cast_channel/cast_message_util.h" |
| 19 #include "net/base/address_list.h" | 19 #include "net/base/address_list.h" |
| 20 #include "net/base/host_port_pair.h" | 20 #include "net/base/host_port_pair.h" |
| 21 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
| 22 #include "net/base/net_util.h" | 22 #include "net/base/net_util.h" |
| 23 #include "net/cert/cert_verifier.h" | 23 #include "net/cert/cert_verifier.h" |
| 24 #include "net/cert/x509_certificate.h" | 24 #include "net/cert/x509_certificate.h" |
| 25 #include "net/http/transport_security_state.h" | 25 #include "net/http/transport_security_state.h" |
| 26 #include "net/socket/client_socket_factory.h" | 26 #include "net/socket/client_socket_factory.h" |
| 27 #include "net/socket/client_socket_handle.h" | 27 #include "net/socket/client_socket_handle.h" |
| 28 #include "net/socket/ssl_client_socket.h" | 28 #include "net/socket/ssl_client_socket.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 42 // the socket is idle for this length of time, and the socket will be closed | 42 // the socket is idle for this length of time, and the socket will be closed |
| 43 // after 9 failed probes. So the total idle time before close is 10 * | 43 // after 9 failed probes. So the total idle time before close is 10 * |
| 44 // kTcpKeepAliveDelaySecs. | 44 // kTcpKeepAliveDelaySecs. |
| 45 const int kTcpKeepAliveDelaySecs = 10; | 45 const int kTcpKeepAliveDelaySecs = 10; |
| 46 | 46 |
| 47 } // namespace | 47 } // namespace |
| 48 | 48 |
| 49 namespace extensions { | 49 namespace extensions { |
| 50 | 50 |
| 51 static base::LazyInstance<BrowserContextKeyedAPIFactory< | 51 static base::LazyInstance<BrowserContextKeyedAPIFactory< |
| 52 ApiResourceManager<api::cast_channel::CastSocket> > > g_factory = | 52 ApiResourceManager<core_api::cast_channel::CastSocket> > > g_factory = |
| 53 LAZY_INSTANCE_INITIALIZER; | 53 LAZY_INSTANCE_INITIALIZER; |
| 54 | 54 |
| 55 // static | 55 // static |
| 56 template <> | 56 template <> |
| 57 BrowserContextKeyedAPIFactory< | 57 BrowserContextKeyedAPIFactory< |
| 58 ApiResourceManager<api::cast_channel::CastSocket> >* | 58 ApiResourceManager<core_api::cast_channel::CastSocket> >* |
| 59 ApiResourceManager<api::cast_channel::CastSocket>::GetFactoryInstance() { | 59 ApiResourceManager<core_api::cast_channel::CastSocket>::GetFactoryInstance() { |
| 60 return g_factory.Pointer(); | 60 return g_factory.Pointer(); |
| 61 } | 61 } |
| 62 | 62 |
| 63 namespace api { | 63 namespace core_api { |
| 64 namespace cast_channel { | 64 namespace cast_channel { |
| 65 | 65 |
| 66 CastSocket::CastSocket(const std::string& owner_extension_id, | 66 CastSocket::CastSocket(const std::string& owner_extension_id, |
| 67 const net::IPEndPoint& ip_endpoint, | 67 const net::IPEndPoint& ip_endpoint, |
| 68 ChannelAuthType channel_auth, | 68 ChannelAuthType channel_auth, |
| 69 CastSocket::Delegate* delegate, | 69 CastSocket::Delegate* delegate, |
| 70 net::NetLog* net_log) : | 70 net::NetLog* net_log) : |
| 71 ApiResource(owner_extension_id), | 71 ApiResource(owner_extension_id), |
| 72 channel_id_(0), | 72 channel_id_(0), |
| 73 ip_endpoint_(ip_endpoint), | 73 ip_endpoint_(ip_endpoint), |
| (...skipping 601 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 675 message_proto.SerializeToString(message_data); | 675 message_proto.SerializeToString(message_data); |
| 676 size_t message_size = message_data->size(); | 676 size_t message_size = message_data->size(); |
| 677 if (message_size > MessageHeader::max_message_size()) { | 677 if (message_size > MessageHeader::max_message_size()) { |
| 678 message_data->clear(); | 678 message_data->clear(); |
| 679 return false; | 679 return false; |
| 680 } | 680 } |
| 681 CastSocket::MessageHeader header; | 681 CastSocket::MessageHeader header; |
| 682 header.SetMessageSize(message_size); | 682 header.SetMessageSize(message_size); |
| 683 header.PrependToString(message_data); | 683 header.PrependToString(message_data); |
| 684 return true; | 684 return true; |
| 685 }; | 685 } |
| 686 | 686 |
| 687 void CastSocket::CloseWithError(ChannelError error) { | 687 void CastSocket::CloseWithError(ChannelError error) { |
| 688 DCHECK(CalledOnValidThread()); | 688 DCHECK(CalledOnValidThread()); |
| 689 socket_.reset(NULL); | 689 socket_.reset(NULL); |
| 690 ready_state_ = READY_STATE_CLOSED; | 690 ready_state_ = READY_STATE_CLOSED; |
| 691 error_state_ = error; | 691 error_state_ = error; |
| 692 if (delegate_) | 692 if (delegate_) |
| 693 delegate_->OnError(this, error); | 693 delegate_->OnError(this, error); |
| 694 } | 694 } |
| 695 | 695 |
| 696 std::string CastSocket::CastUrl() const { | 696 std::string CastSocket::CastUrl() const { |
| 697 return ((channel_auth_ == CHANNEL_AUTH_TYPE_SSL_VERIFIED) ? | 697 return ((channel_auth_ == CHANNEL_AUTH_TYPE_SSL_VERIFIED) ? |
| 698 "casts://" : "cast://") + ip_endpoint_.ToString(); | 698 "casts://" : "cast://") + ip_endpoint_.ToString(); |
| 699 } | 699 } |
| 700 | 700 |
| 701 bool CastSocket::CalledOnValidThread() const { | 701 bool CastSocket::CalledOnValidThread() const { |
| 702 return thread_checker_.CalledOnValidThread(); | 702 return thread_checker_.CalledOnValidThread(); |
| 703 } | 703 } |
| 704 | 704 |
| 705 CastSocket::MessageHeader::MessageHeader() : message_size(0) { } | 705 CastSocket::MessageHeader::MessageHeader() : message_size(0) { } |
| 706 | 706 |
| 707 void CastSocket::MessageHeader::SetMessageSize(size_t size) { | 707 void CastSocket::MessageHeader::SetMessageSize(size_t size) { |
| 708 DCHECK(size < static_cast<size_t>(kuint32max)); | 708 DCHECK_LT(size, static_cast<size_t>(kuint32max)); |
| 709 DCHECK(size > 0); | 709 DCHECK_GT(size, 0U); |
| 710 message_size = static_cast<size_t>(size); | 710 message_size = static_cast<size_t>(size); |
| 711 } | 711 } |
| 712 | 712 |
| 713 // TODO(mfoltz): Investigate replacing header serialization with base::Pickle, | 713 // TODO(mfoltz): Investigate replacing header serialization with base::Pickle, |
| 714 // if bit-for-bit compatible. | 714 // if bit-for-bit compatible. |
| 715 void CastSocket::MessageHeader::PrependToString(std::string* str) { | 715 void CastSocket::MessageHeader::PrependToString(std::string* str) { |
| 716 MessageHeader output = *this; | 716 MessageHeader output = *this; |
| 717 output.message_size = base::HostToNet32(message_size); | 717 output.message_size = base::HostToNet32(message_size); |
| 718 size_t header_size = base::checked_cast<size_t,uint32>( | 718 size_t header_size = base::checked_cast<size_t,uint32>( |
| 719 MessageHeader::header_size()); | 719 MessageHeader::header_size()); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 747 if (!Serialize(message_proto, &message_data)) | 747 if (!Serialize(message_proto, &message_data)) |
| 748 return false; | 748 return false; |
| 749 io_buffer = new net::DrainableIOBuffer(new net::StringIOBuffer(message_data), | 749 io_buffer = new net::DrainableIOBuffer(new net::StringIOBuffer(message_data), |
| 750 message_data.size()); | 750 message_data.size()); |
| 751 return true; | 751 return true; |
| 752 } | 752 } |
| 753 | 753 |
| 754 CastSocket::WriteRequest::~WriteRequest() { } | 754 CastSocket::WriteRequest::~WriteRequest() { } |
| 755 | 755 |
| 756 } // namespace cast_channel | 756 } // namespace cast_channel |
| 757 } // namespace api | 757 } // namespace core_api |
| 758 } // namespace extensions | 758 } // namespace extensions |
| 759 | 759 |
| 760 #undef VLOG_WITH_CONNECTION | 760 #undef VLOG_WITH_CONNECTION |
| OLD | NEW |