| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/dial/dial_service.h" | 5 #include "chrome/browser/extensions/api/dial/dial_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 #include "third_party/cros_system_api/dbus/service_constants.h" | 32 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 33 #endif | 33 #endif |
| 34 | 34 |
| 35 using base::Time; | 35 using base::Time; |
| 36 using base::TimeDelta; | 36 using base::TimeDelta; |
| 37 using content::BrowserThread; | 37 using content::BrowserThread; |
| 38 using net::HttpResponseHeaders; | 38 using net::HttpResponseHeaders; |
| 39 using net::HttpUtil; | 39 using net::HttpUtil; |
| 40 using net::IOBufferWithSize; | 40 using net::IOBufferWithSize; |
| 41 using net::IPAddressNumber; | 41 using net::IPAddressNumber; |
| 42 using net::IPEndPoint; | |
| 43 using net::NetworkInterface; | 42 using net::NetworkInterface; |
| 44 using net::NetworkInterfaceList; | 43 using net::NetworkInterfaceList; |
| 45 using net::StringIOBuffer; | 44 using net::StringIOBuffer; |
| 46 using net::UDPSocket; | 45 using net::UDPSocket; |
| 47 | 46 |
| 48 namespace extensions { | 47 namespace extensions { |
| 49 | 48 |
| 50 namespace { | 49 namespace { |
| 51 | 50 |
| 52 // The total number of requests to make per discovery cycle. | 51 // The total number of requests to make per discovery cycle. |
| 53 const int kDialMaxRequests = 4; | 52 const int kDialMaxRequests = 4; |
| 54 | 53 |
| 55 // The interval to wait between successive requests. | 54 // The interval to wait between successive requests. |
| 56 const int kDialRequestIntervalMillis = 1000; | 55 const int kDialRequestIntervalMillis = 1000; |
| 57 | 56 |
| 58 // The maximum delay a device may wait before responding (MX). | 57 // The maximum delay a device may wait before responding (MX). |
| 59 const int kDialMaxResponseDelaySecs = 1; | 58 const int kDialMaxResponseDelaySecs = 1; |
| 60 | 59 |
| 61 // The maximum time a response is expected after a M-SEARCH request. | 60 // The maximum time a response is expected after a M-SEARCH request. |
| 62 const int kDialResponseTimeoutSecs = 2; | 61 const int kDialResponseTimeoutSecs = 2; |
| 63 | 62 |
| 64 // The multicast IP address for discovery. | 63 // The multicast IP address for discovery. |
| 65 const char kDialRequestAddress[] = "239.255.255.250"; | 64 const char kDialRequestAddress[] = "239.255.255.250"; |
| 66 | 65 |
| 67 // The UDP port number for discovery. | 66 // The UDP port number for discovery. |
| 68 const int kDialRequestPort = 1900; | 67 const uint16 kDialRequestPort = 1900; |
| 69 | 68 |
| 70 // The DIAL service type as part of the search request. | 69 // The DIAL service type as part of the search request. |
| 71 const char kDialSearchType[] = "urn:dial-multiscreen-org:service:dial:1"; | 70 const char kDialSearchType[] = "urn:dial-multiscreen-org:service:dial:1"; |
| 72 | 71 |
| 73 // SSDP headers parsed from the response. | 72 // SSDP headers parsed from the response. |
| 74 const char kSsdpLocationHeader[] = "LOCATION"; | 73 const char kSsdpLocationHeader[] = "LOCATION"; |
| 75 const char kSsdpCacheControlHeader[] = "CACHE-CONTROL"; | 74 const char kSsdpCacheControlHeader[] = "CACHE-CONTROL"; |
| 76 const char kSsdpConfigIdHeader[] = "CONFIGID.UPNP.ORG"; | 75 const char kSsdpConfigIdHeader[] = "CONFIGID.UPNP.ORG"; |
| 77 const char kSsdpUsnHeader[] = "USN"; | 76 const char kSsdpUsnHeader[] = "USN"; |
| 78 | 77 |
| 79 // The receive buffer size, in bytes. | 78 // The receive buffer size, in bytes. |
| 80 const int kDialRecvBufferSize = 1500; | 79 const int kDialRecvBufferSize = 1500; |
| 81 | 80 |
| 82 // Gets a specific header from |headers| and puts it in |value|. | 81 // Gets a specific header from |headers| and puts it in |value|. |
| 83 bool GetHeader(HttpResponseHeaders* headers, const char* name, | 82 bool GetHeader(HttpResponseHeaders* headers, const char* name, |
| 84 std::string* value) { | 83 std::string* value) { |
| 85 return headers->EnumerateHeader(NULL, std::string(name), value); | 84 return headers->EnumerateHeader(NULL, std::string(name), value); |
| 86 } | 85 } |
| 87 | 86 |
| 88 // Returns the request string. | 87 // Returns the request string. |
| 89 std::string BuildRequest() { | 88 std::string BuildRequest() { |
| 90 // Extra line at the end to make UPnP lib happy. | 89 // Extra line at the end to make UPnP lib happy. |
| 91 chrome::VersionInfo version; | 90 chrome::VersionInfo version; |
| 92 std::string request(base::StringPrintf( | 91 std::string request(base::StringPrintf( |
| 93 "M-SEARCH * HTTP/1.1\r\n" | 92 "M-SEARCH * HTTP/1.1\r\n" |
| 94 "HOST: %s:%i\r\n" | 93 "HOST: %s:%u\r\n" |
| 95 "MAN: \"ssdp:discover\"\r\n" | 94 "MAN: \"ssdp:discover\"\r\n" |
| 96 "MX: %d\r\n" | 95 "MX: %d\r\n" |
| 97 "ST: %s\r\n" | 96 "ST: %s\r\n" |
| 98 "USER-AGENT: %s/%s %s\r\n" | 97 "USER-AGENT: %s/%s %s\r\n" |
| 99 "\r\n", | 98 "\r\n", |
| 100 kDialRequestAddress, | 99 kDialRequestAddress, |
| 101 kDialRequestPort, | 100 kDialRequestPort, |
| 102 kDialMaxResponseDelaySecs, | 101 kDialMaxResponseDelaySecs, |
| 103 kDialSearchType, | 102 kDialSearchType, |
| 104 version.Name().c_str(), | 103 version.Name().c_str(), |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 DCHECK(bind_ip_address.size() == net::kIPv4AddressSize); | 168 DCHECK(bind_ip_address.size() == net::kIPv4AddressSize); |
| 170 | 169 |
| 171 net::RandIntCallback rand_cb = base::Bind(&base::RandInt); | 170 net::RandIntCallback rand_cb = base::Bind(&base::RandInt); |
| 172 socket_.reset(new UDPSocket(net::DatagramSocket::RANDOM_BIND, | 171 socket_.reset(new UDPSocket(net::DatagramSocket::RANDOM_BIND, |
| 173 rand_cb, | 172 rand_cb, |
| 174 net_log, | 173 net_log, |
| 175 net_log_source)); | 174 net_log_source)); |
| 176 socket_->AllowBroadcast(); | 175 socket_->AllowBroadcast(); |
| 177 | 176 |
| 178 // 0 means bind a random port | 177 // 0 means bind a random port |
| 179 IPEndPoint address(bind_ip_address, 0); | 178 net::IPEndPoint address(bind_ip_address, 0); |
| 180 | 179 |
| 181 if (!CheckResult("Bind", socket_->Bind(address))) | 180 if (!CheckResult("Bind", socket_->Bind(address))) |
| 182 return false; | 181 return false; |
| 183 | 182 |
| 184 DCHECK(socket_.get()); | 183 DCHECK(socket_.get()); |
| 185 | 184 |
| 186 recv_buffer_ = new IOBufferWithSize(kDialRecvBufferSize); | 185 recv_buffer_ = new IOBufferWithSize(kDialRecvBufferSize); |
| 187 return ReadSocket(); | 186 return ReadSocket(); |
| 188 } | 187 } |
| 189 | 188 |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 num_requests_sent_(0), | 378 num_requests_sent_(0), |
| 380 max_requests_(kDialMaxRequests), | 379 max_requests_(kDialMaxRequests), |
| 381 finish_delay_(TimeDelta::FromMilliseconds((kDialMaxRequests - 1) * | 380 finish_delay_(TimeDelta::FromMilliseconds((kDialMaxRequests - 1) * |
| 382 kDialRequestIntervalMillis) + | 381 kDialRequestIntervalMillis) + |
| 383 TimeDelta::FromSeconds(kDialResponseTimeoutSecs)), | 382 TimeDelta::FromSeconds(kDialResponseTimeoutSecs)), |
| 384 request_interval_( | 383 request_interval_( |
| 385 TimeDelta::FromMilliseconds(kDialRequestIntervalMillis)) { | 384 TimeDelta::FromMilliseconds(kDialRequestIntervalMillis)) { |
| 386 IPAddressNumber address; | 385 IPAddressNumber address; |
| 387 bool success = net::ParseIPLiteralToNumber(kDialRequestAddress, &address); | 386 bool success = net::ParseIPLiteralToNumber(kDialRequestAddress, &address); |
| 388 DCHECK(success); | 387 DCHECK(success); |
| 389 send_address_ = IPEndPoint(address, kDialRequestPort); | 388 send_address_ = net::IPEndPoint(address, kDialRequestPort); |
| 390 send_buffer_ = new StringIOBuffer(BuildRequest()); | 389 send_buffer_ = new StringIOBuffer(BuildRequest()); |
| 391 net_log_ = net_log; | 390 net_log_ = net_log; |
| 392 net_log_source_.type = net::NetLog::SOURCE_UDP_SOCKET; | 391 net_log_source_.type = net::NetLog::SOURCE_UDP_SOCKET; |
| 393 net_log_source_.id = net_log_->NextID(); | 392 net_log_source_.id = net_log_->NextID(); |
| 394 } | 393 } |
| 395 | 394 |
| 396 DialServiceImpl::~DialServiceImpl() { | 395 DialServiceImpl::~DialServiceImpl() { |
| 397 DCHECK(thread_checker_.CalledOnValidThread()); | 396 DCHECK(thread_checker_.CalledOnValidThread()); |
| 398 } | 397 } |
| 399 | 398 |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 615 for (ScopedVector<DialSocket>::const_iterator iter = dial_sockets_.begin(); | 614 for (ScopedVector<DialSocket>::const_iterator iter = dial_sockets_.begin(); |
| 616 iter != dial_sockets_.end(); | 615 iter != dial_sockets_.end(); |
| 617 ++iter) { | 616 ++iter) { |
| 618 if (!((*iter)->IsClosed())) | 617 if (!((*iter)->IsClosed())) |
| 619 return true; | 618 return true; |
| 620 } | 619 } |
| 621 return false; | 620 return false; |
| 622 } | 621 } |
| 623 | 622 |
| 624 } // namespace extensions | 623 } // namespace extensions |
| OLD | NEW |