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 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
15 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h" | 15 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h" |
16 #include "chrome/browser/net/chrome_net_log.h" | 16 #include "chrome/browser/net/chrome_net_log.h" |
17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
18 #include "extensions/browser/event_router.h" | 18 #include "extensions/browser/event_router.h" |
19 #include "net/base/ip_endpoint.h" | 19 #include "net/base/ip_endpoint.h" |
20 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
21 #include "net/base/net_util.h" | 21 #include "net/base/net_util.h" |
22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
23 | 23 |
| 24 // Default timeout interval for connection setup. |
| 25 // Used if not otherwise specified at ConnectInfo::timeout. |
| 26 const int kDefaultConnectTimeoutMillis = 5000; // 5 seconds. |
| 27 |
24 namespace extensions { | 28 namespace extensions { |
25 | 29 |
26 namespace Close = cast_channel::Close; | 30 namespace Close = cast_channel::Close; |
27 namespace OnError = cast_channel::OnError; | 31 namespace OnError = cast_channel::OnError; |
28 namespace OnMessage = cast_channel::OnMessage; | 32 namespace OnMessage = cast_channel::OnMessage; |
29 namespace Open = cast_channel::Open; | 33 namespace Open = cast_channel::Open; |
30 namespace Send = cast_channel::Send; | 34 namespace Send = cast_channel::Send; |
31 using cast_channel::CastSocket; | 35 using cast_channel::CastSocket; |
32 using cast_channel::ChannelAuthType; | 36 using cast_channel::ChannelAuthType; |
33 using cast_channel::ChannelError; | 37 using cast_channel::ChannelError; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 g_factory = LAZY_INSTANCE_INITIALIZER; | 96 g_factory = LAZY_INSTANCE_INITIALIZER; |
93 | 97 |
94 // static | 98 // static |
95 BrowserContextKeyedAPIFactory<CastChannelAPI>* | 99 BrowserContextKeyedAPIFactory<CastChannelAPI>* |
96 CastChannelAPI::GetFactoryInstance() { | 100 CastChannelAPI::GetFactoryInstance() { |
97 return g_factory.Pointer(); | 101 return g_factory.Pointer(); |
98 } | 102 } |
99 | 103 |
100 scoped_ptr<CastSocket> CastChannelAPI::CreateCastSocket( | 104 scoped_ptr<CastSocket> CastChannelAPI::CreateCastSocket( |
101 const std::string& extension_id, const net::IPEndPoint& ip_endpoint, | 105 const std::string& extension_id, const net::IPEndPoint& ip_endpoint, |
102 ChannelAuthType channel_auth) { | 106 ChannelAuthType channel_auth, const base::TimeDelta& timeout) { |
103 if (socket_for_test_.get()) { | 107 if (socket_for_test_.get()) { |
104 return socket_for_test_.Pass(); | 108 return socket_for_test_.Pass(); |
105 } else { | 109 } else { |
106 return scoped_ptr<CastSocket>( | 110 return scoped_ptr<CastSocket>( |
107 new CastSocket(extension_id, ip_endpoint, channel_auth, this, | 111 new CastSocket(extension_id, ip_endpoint, channel_auth, this, |
108 g_browser_process->net_log())); | 112 g_browser_process->net_log(), |
| 113 timeout)); |
109 } | 114 } |
110 } | 115 } |
111 | 116 |
112 void CastChannelAPI::SetSocketForTest(scoped_ptr<CastSocket> socket_for_test) { | 117 void CastChannelAPI::SetSocketForTest(scoped_ptr<CastSocket> socket_for_test) { |
113 socket_for_test_ = socket_for_test.Pass(); | 118 socket_for_test_ = socket_for_test.Pass(); |
114 } | 119 } |
115 | 120 |
116 void CastChannelAPI::OnError(const CastSocket* socket, | 121 void CastChannelAPI::OnError(const CastSocket* socket, |
117 cast_channel::ChannelError error) { | 122 cast_channel::ChannelError error) { |
118 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 123 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 } | 319 } |
315 channel_auth_ = connect_info_->auth; | 320 channel_auth_ = connect_info_->auth; |
316 ip_endpoint_.reset(ParseConnectInfo(*connect_info_)); | 321 ip_endpoint_.reset(ParseConnectInfo(*connect_info_)); |
317 return true; | 322 return true; |
318 } | 323 } |
319 | 324 |
320 void CastChannelOpenFunction::AsyncWorkStart() { | 325 void CastChannelOpenFunction::AsyncWorkStart() { |
321 DCHECK(api_); | 326 DCHECK(api_); |
322 DCHECK(ip_endpoint_.get()); | 327 DCHECK(ip_endpoint_.get()); |
323 scoped_ptr<CastSocket> socket = api_->CreateCastSocket( | 328 scoped_ptr<CastSocket> socket = api_->CreateCastSocket( |
324 extension_->id(), *ip_endpoint_, channel_auth_); | 329 extension_->id(), |
| 330 *ip_endpoint_, |
| 331 channel_auth_, |
| 332 base::TimeDelta::FromMilliseconds(connect_info_->timeout.get() |
| 333 ? *connect_info_->timeout |
| 334 : kDefaultConnectTimeoutMillis)); |
325 new_channel_id_ = AddSocket(socket.release()); | 335 new_channel_id_ = AddSocket(socket.release()); |
326 GetSocket(new_channel_id_)->Connect( | 336 GetSocket(new_channel_id_)->Connect( |
327 base::Bind(&CastChannelOpenFunction::OnOpen, this)); | 337 base::Bind(&CastChannelOpenFunction::OnOpen, this)); |
328 } | 338 } |
329 | 339 |
330 void CastChannelOpenFunction::OnOpen(int result) { | 340 void CastChannelOpenFunction::OnOpen(int result) { |
331 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 341 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 342 VLOG(1) << "Connect finished, OnOpen invoked."; |
332 SetResultFromSocket(new_channel_id_); | 343 SetResultFromSocket(new_channel_id_); |
333 AsyncWorkCompleted(); | 344 AsyncWorkCompleted(); |
334 } | 345 } |
335 | 346 |
336 CastChannelSendFunction::CastChannelSendFunction() { } | 347 CastChannelSendFunction::CastChannelSendFunction() { } |
337 | 348 |
338 CastChannelSendFunction::~CastChannelSendFunction() { } | 349 CastChannelSendFunction::~CastChannelSendFunction() { } |
339 | 350 |
340 bool CastChannelSendFunction::Prepare() { | 351 bool CastChannelSendFunction::Prepare() { |
341 params_ = Send::Params::Create(*args_); | 352 params_ = Send::Params::Create(*args_); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 SetResultFromError(cast_channel::CHANNEL_ERROR_SOCKET_ERROR); | 416 SetResultFromError(cast_channel::CHANNEL_ERROR_SOCKET_ERROR); |
406 } else { | 417 } else { |
407 int channel_id = params_->channel.channel_id; | 418 int channel_id = params_->channel.channel_id; |
408 SetResultFromSocket(channel_id); | 419 SetResultFromSocket(channel_id); |
409 RemoveSocket(channel_id); | 420 RemoveSocket(channel_id); |
410 } | 421 } |
411 AsyncWorkCompleted(); | 422 AsyncWorkCompleted(); |
412 } | 423 } |
413 | 424 |
414 } // namespace extensions | 425 } // namespace extensions |
OLD | NEW |