| 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_channel_api.h" | 5 #include "chrome/browser/extensions/api/cast_channel/cast_channel_api.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h" | 14 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h" |
| 15 #include "chrome/browser/net/chrome_net_log.h" | 15 #include "chrome/browser/net/chrome_net_log.h" |
| 16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "extensions/browser/event_router.h" | 17 #include "extensions/browser/event_router.h" |
| 18 #include "net/base/ip_endpoint.h" | 18 #include "net/base/ip_endpoint.h" |
| 19 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 20 #include "net/base/net_util.h" | 20 #include "net/base/net_util.h" |
| 21 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 22 | 22 |
| 23 // Default timeout interval for connection setup. |
| 24 // Used if not otherwise specified at ConnectInfo::timeout. |
| 25 const int kDefaultConnectTimeoutMillis = 5000; // 5 seconds. |
| 26 |
| 23 namespace extensions { | 27 namespace extensions { |
| 24 | 28 |
| 25 namespace Close = cast_channel::Close; | 29 namespace Close = cast_channel::Close; |
| 26 namespace OnError = cast_channel::OnError; | 30 namespace OnError = cast_channel::OnError; |
| 27 namespace OnMessage = cast_channel::OnMessage; | 31 namespace OnMessage = cast_channel::OnMessage; |
| 28 namespace Open = cast_channel::Open; | 32 namespace Open = cast_channel::Open; |
| 29 namespace Send = cast_channel::Send; | 33 namespace Send = cast_channel::Send; |
| 30 using cast_channel::CastSocket; | 34 using cast_channel::CastSocket; |
| 31 using cast_channel::ChannelAuthType; | 35 using cast_channel::ChannelAuthType; |
| 32 using cast_channel::ChannelError; | 36 using cast_channel::ChannelError; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 g_factory = LAZY_INSTANCE_INITIALIZER; | 80 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 77 | 81 |
| 78 // static | 82 // static |
| 79 BrowserContextKeyedAPIFactory<CastChannelAPI>* | 83 BrowserContextKeyedAPIFactory<CastChannelAPI>* |
| 80 CastChannelAPI::GetFactoryInstance() { | 84 CastChannelAPI::GetFactoryInstance() { |
| 81 return g_factory.Pointer(); | 85 return g_factory.Pointer(); |
| 82 } | 86 } |
| 83 | 87 |
| 84 scoped_ptr<CastSocket> CastChannelAPI::CreateCastSocket( | 88 scoped_ptr<CastSocket> CastChannelAPI::CreateCastSocket( |
| 85 const std::string& extension_id, const net::IPEndPoint& ip_endpoint, | 89 const std::string& extension_id, const net::IPEndPoint& ip_endpoint, |
| 86 ChannelAuthType channel_auth) { | 90 ChannelAuthType channel_auth, int timeout_millis) { |
| 87 if (socket_for_test_.get()) { | 91 if (socket_for_test_.get()) { |
| 88 return socket_for_test_.Pass(); | 92 return socket_for_test_.Pass(); |
| 89 } else { | 93 } else { |
| 90 return scoped_ptr<CastSocket>( | 94 return scoped_ptr<CastSocket>( |
| 91 new CastSocket(extension_id, ip_endpoint, channel_auth, this, | 95 new CastSocket(extension_id, ip_endpoint, channel_auth, this, |
| 92 g_browser_process->net_log())); | 96 g_browser_process->net_log(), |
| 97 timeout_millis)); |
| 93 } | 98 } |
| 94 } | 99 } |
| 95 | 100 |
| 96 void CastChannelAPI::SetSocketForTest(scoped_ptr<CastSocket> socket_for_test) { | 101 void CastChannelAPI::SetSocketForTest(scoped_ptr<CastSocket> socket_for_test) { |
| 97 socket_for_test_ = socket_for_test.Pass(); | 102 socket_for_test_ = socket_for_test.Pass(); |
| 98 } | 103 } |
| 99 | 104 |
| 100 void CastChannelAPI::OnError(const CastSocket* socket, | 105 void CastChannelAPI::OnError(const CastSocket* socket, |
| 101 cast_channel::ChannelError error) { | 106 cast_channel::ChannelError error) { |
| 102 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 107 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 VLOG(2) << "IP: " << ip_address_str << " Port: " << port_str; | 242 VLOG(2) << "IP: " << ip_address_str << " Port: " << port_str; |
| 238 int port; | 243 int port; |
| 239 if (!base::StringToInt(port_str, &port)) | 244 if (!base::StringToInt(port_str, &port)) |
| 240 return false; | 245 return false; |
| 241 connect_info->ip_address = ip_address_str; | 246 connect_info->ip_address = ip_address_str; |
| 242 connect_info->port = port; | 247 connect_info->port = port; |
| 243 connect_info->auth = auth_required ? | 248 connect_info->auth = auth_required ? |
| 244 cast_channel::CHANNEL_AUTH_TYPE_SSL_VERIFIED : | 249 cast_channel::CHANNEL_AUTH_TYPE_SSL_VERIFIED : |
| 245 cast_channel::CHANNEL_AUTH_TYPE_SSL; | 250 cast_channel::CHANNEL_AUTH_TYPE_SSL; |
| 246 return true; | 251 return true; |
| 247 }; | 252 } |
| 248 | 253 |
| 249 net::IPEndPoint* CastChannelOpenFunction::ParseConnectInfo( | 254 net::IPEndPoint* CastChannelOpenFunction::ParseConnectInfo( |
| 250 const ConnectInfo& connect_info) { | 255 const ConnectInfo& connect_info) { |
| 251 net::IPAddressNumber ip_address; | 256 net::IPAddressNumber ip_address; |
| 252 if (!net::ParseIPLiteralToNumber(connect_info.ip_address, &ip_address)) { | 257 if (!net::ParseIPLiteralToNumber(connect_info.ip_address, &ip_address)) { |
| 253 return NULL; | 258 return NULL; |
| 254 } | 259 } |
| 255 if (connect_info.port < 0 || connect_info.port > | 260 if (connect_info.port < 0 || connect_info.port > |
| 256 std::numeric_limits<unsigned short>::max()) { | 261 std::numeric_limits<unsigned short>::max()) { |
| 257 return NULL; | 262 return NULL; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 ip_endpoint_.reset(ParseConnectInfo(*connect_info_)); | 298 ip_endpoint_.reset(ParseConnectInfo(*connect_info_)); |
| 294 return ip_endpoint_.get() != NULL; | 299 return ip_endpoint_.get() != NULL; |
| 295 } | 300 } |
| 296 return false; | 301 return false; |
| 297 } | 302 } |
| 298 | 303 |
| 299 void CastChannelOpenFunction::AsyncWorkStart() { | 304 void CastChannelOpenFunction::AsyncWorkStart() { |
| 300 DCHECK(api_); | 305 DCHECK(api_); |
| 301 DCHECK(ip_endpoint_.get()); | 306 DCHECK(ip_endpoint_.get()); |
| 302 scoped_ptr<CastSocket> socket = api_->CreateCastSocket( | 307 scoped_ptr<CastSocket> socket = api_->CreateCastSocket( |
| 303 extension_->id(), *ip_endpoint_, channel_auth_); | 308 extension_->id(), |
| 309 *ip_endpoint_, |
| 310 channel_auth_, |
| 311 (connect_info_->timeout.get() ? *connect_info_->timeout |
| 312 : kDefaultConnectTimeoutMillis)); |
| 304 new_channel_id_ = AddSocket(socket.release()); | 313 new_channel_id_ = AddSocket(socket.release()); |
| 305 GetSocket(new_channel_id_)->Connect( | 314 GetSocket(new_channel_id_)->Connect( |
| 306 base::Bind(&CastChannelOpenFunction::OnOpen, this)); | 315 base::Bind(&CastChannelOpenFunction::OnOpen, this)); |
| 307 } | 316 } |
| 308 | 317 |
| 309 void CastChannelOpenFunction::OnOpen(int result) { | 318 void CastChannelOpenFunction::OnOpen(int result) { |
| 310 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 319 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 311 SetResultFromSocket(new_channel_id_); | 320 SetResultFromSocket(new_channel_id_); |
| 312 AsyncWorkCompleted(); | 321 AsyncWorkCompleted(); |
| 313 } | 322 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 SetResultFromError(cast_channel::CHANNEL_ERROR_SOCKET_ERROR); | 373 SetResultFromError(cast_channel::CHANNEL_ERROR_SOCKET_ERROR); |
| 365 } else { | 374 } else { |
| 366 int channel_id = params_->channel.channel_id; | 375 int channel_id = params_->channel.channel_id; |
| 367 SetResultFromSocket(channel_id); | 376 SetResultFromSocket(channel_id); |
| 368 RemoveSocket(channel_id); | 377 RemoveSocket(channel_id); |
| 369 } | 378 } |
| 370 AsyncWorkCompleted(); | 379 AsyncWorkCompleted(); |
| 371 } | 380 } |
| 372 | 381 |
| 373 } // namespace extensions | 382 } // namespace extensions |
| OLD | NEW |