| 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/utility/local_discovery/service_discovery_message_handler.h" | 5 #include "chrome/utility/local_discovery/service_discovery_message_handler.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "chrome/common/local_discovery/local_discovery_messages.h" | 10 #include "chrome/common/local_discovery/local_discovery_messages.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 | 21 |
| 22 // Sets socket factory used by |net::CreatePlatformSocket|. Implemetation | 22 // Sets socket factory used by |net::CreatePlatformSocket|. Implemetation |
| 23 // keeps single socket that will be returned to the first call to | 23 // keeps single socket that will be returned to the first call to |
| 24 // |net::CreatePlatformSocket| during object lifetime. | 24 // |net::CreatePlatformSocket| during object lifetime. |
| 25 class ScopedSocketFactory : public net::PlatformSocketFactory { | 25 class ScopedSocketFactory : public net::PlatformSocketFactory { |
| 26 public: | 26 public: |
| 27 explicit ScopedSocketFactory(net::SocketDescriptor socket) : socket_(socket) { | 27 explicit ScopedSocketFactory(net::SocketDescriptor socket) : socket_(socket) { |
| 28 net::PlatformSocketFactory::SetInstance(this); | 28 net::PlatformSocketFactory::SetInstance(this); |
| 29 } | 29 } |
| 30 | 30 |
| 31 virtual ~ScopedSocketFactory() { | 31 ~ScopedSocketFactory() override { |
| 32 net::PlatformSocketFactory::SetInstance(NULL); | 32 net::PlatformSocketFactory::SetInstance(NULL); |
| 33 ClosePlatformSocket(socket_); | 33 ClosePlatformSocket(socket_); |
| 34 socket_ = net::kInvalidSocket; | 34 socket_ = net::kInvalidSocket; |
| 35 } | 35 } |
| 36 | 36 |
| 37 virtual net::SocketDescriptor CreateSocket(int family, int type, | 37 net::SocketDescriptor CreateSocket(int family, |
| 38 int protocol) override { | 38 int type, |
| 39 int protocol) override { |
| 39 DCHECK_EQ(type, SOCK_DGRAM); | 40 DCHECK_EQ(type, SOCK_DGRAM); |
| 40 DCHECK(family == AF_INET || family == AF_INET6); | 41 DCHECK(family == AF_INET || family == AF_INET6); |
| 41 net::SocketDescriptor result = net::kInvalidSocket; | 42 net::SocketDescriptor result = net::kInvalidSocket; |
| 42 std::swap(result, socket_); | 43 std::swap(result, socket_); |
| 43 return result; | 44 return result; |
| 44 } | 45 } |
| 45 | 46 |
| 46 private: | 47 private: |
| 47 net::SocketDescriptor socket_; | 48 net::SocketDescriptor socket_; |
| 48 DISALLOW_COPY_AND_ASSIGN(ScopedSocketFactory); | 49 DISALLOW_COPY_AND_ASSIGN(ScopedSocketFactory); |
| 49 }; | 50 }; |
| 50 | 51 |
| 51 struct SocketInfo { | 52 struct SocketInfo { |
| 52 SocketInfo(net::SocketDescriptor socket, | 53 SocketInfo(net::SocketDescriptor socket, |
| 53 net::AddressFamily address_family, | 54 net::AddressFamily address_family, |
| 54 uint32 interface_index) | 55 uint32 interface_index) |
| 55 : socket(socket), | 56 : socket(socket), |
| 56 address_family(address_family), | 57 address_family(address_family), |
| 57 interface_index(interface_index) { | 58 interface_index(interface_index) { |
| 58 } | 59 } |
| 59 net::SocketDescriptor socket; | 60 net::SocketDescriptor socket; |
| 60 net::AddressFamily address_family; | 61 net::AddressFamily address_family; |
| 61 uint32 interface_index; | 62 uint32 interface_index; |
| 62 }; | 63 }; |
| 63 | 64 |
| 64 // Returns list of sockets preallocated before. | 65 // Returns list of sockets preallocated before. |
| 65 class PreCreatedMDnsSocketFactory : public net::MDnsSocketFactory { | 66 class PreCreatedMDnsSocketFactory : public net::MDnsSocketFactory { |
| 66 public: | 67 public: |
| 67 PreCreatedMDnsSocketFactory() {} | 68 PreCreatedMDnsSocketFactory() {} |
| 68 virtual ~PreCreatedMDnsSocketFactory() { | 69 ~PreCreatedMDnsSocketFactory() override { |
| 69 // Not empty if process exits too fast, before starting mDns code. If | 70 // Not empty if process exits too fast, before starting mDns code. If |
| 70 // happened, destructors may crash accessing destroyed global objects. | 71 // happened, destructors may crash accessing destroyed global objects. |
| 71 sockets_.weak_clear(); | 72 sockets_.weak_clear(); |
| 72 } | 73 } |
| 73 | 74 |
| 74 // net::MDnsSocketFactory implementation: | 75 // net::MDnsSocketFactory implementation: |
| 75 virtual void CreateSockets( | 76 void CreateSockets( |
| 76 ScopedVector<net::DatagramServerSocket>* sockets) override { | 77 ScopedVector<net::DatagramServerSocket>* sockets) override { |
| 77 sockets->swap(sockets_); | 78 sockets->swap(sockets_); |
| 78 Reset(); | 79 Reset(); |
| 79 } | 80 } |
| 80 | 81 |
| 81 void AddSocket(const SocketInfo& socket_info) { | 82 void AddSocket(const SocketInfo& socket_info) { |
| 82 // Takes ownership of socket_info.socket; | 83 // Takes ownership of socket_info.socket; |
| 83 ScopedSocketFactory platform_factory(socket_info.socket); | 84 ScopedSocketFactory platform_factory(socket_info.socket); |
| 84 scoped_ptr<net::DatagramServerSocket> socket( | 85 scoped_ptr<net::DatagramServerSocket> socket( |
| 85 net::CreateAndBindMDnsSocket(socket_info.address_family, | 86 net::CreateAndBindMDnsSocket(socket_info.address_family, |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 id, success, address_ipv4, address_ipv6)); | 467 id, success, address_ipv4, address_ipv6)); |
| 467 } | 468 } |
| 468 | 469 |
| 469 void ServiceDiscoveryMessageHandler::Send(IPC::Message* msg) { | 470 void ServiceDiscoveryMessageHandler::Send(IPC::Message* msg) { |
| 470 utility_task_runner_->PostTask(FROM_HERE, | 471 utility_task_runner_->PostTask(FROM_HERE, |
| 471 base::Bind(&SendHostMessageOnUtilityThread, | 472 base::Bind(&SendHostMessageOnUtilityThread, |
| 472 msg)); | 473 msg)); |
| 473 } | 474 } |
| 474 | 475 |
| 475 } // namespace local_discovery | 476 } // namespace local_discovery |
| OLD | NEW |