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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 class PreCreatedMDnsSocketFactory : public net::MDnsSocketFactory { | 65 class PreCreatedMDnsSocketFactory : public net::MDnsSocketFactory { |
66 public: | 66 public: |
67 PreCreatedMDnsSocketFactory() {} | 67 PreCreatedMDnsSocketFactory() {} |
68 virtual ~PreCreatedMDnsSocketFactory() { | 68 virtual ~PreCreatedMDnsSocketFactory() { |
69 Reset(); | 69 Reset(); |
70 } | 70 } |
71 | 71 |
72 // net::MDnsSocketFactory implementation: | 72 // net::MDnsSocketFactory implementation: |
73 virtual void CreateSockets( | 73 virtual void CreateSockets( |
74 ScopedVector<net::DatagramServerSocket>* sockets) OVERRIDE { | 74 ScopedVector<net::DatagramServerSocket>* sockets) OVERRIDE { |
75 for (size_t i = 0; i < sockets_.size(); ++i) { | 75 sockets->swap(sockets_); |
76 // Takes ownership of sockets_[i].socket; | 76 Reset(); |
77 ScopedSocketFactory platform_factory(sockets_[i].socket); | |
78 scoped_ptr<net::DatagramServerSocket> socket( | |
79 net::CreateAndBindMDnsSocket(sockets_[i].address_family, | |
80 sockets_[i].interface_index)); | |
81 if (socket) | |
82 sockets->push_back(socket.release()); | |
83 } | |
84 sockets_.clear(); | |
85 } | 77 } |
86 | 78 |
87 void AddSocket(const SocketInfo& socket) { | 79 void AddSocket(const SocketInfo& socket_info) { |
88 sockets_.push_back(socket); | 80 // Takes ownership of socket_info.socket; |
| 81 ScopedSocketFactory platform_factory(socket_info.socket); |
| 82 scoped_ptr<net::DatagramServerSocket> socket( |
| 83 net::CreateAndBindMDnsSocket(socket_info.address_family, |
| 84 socket_info.interface_index)); |
| 85 if (socket) { |
| 86 socket->DetachFromThread(); |
| 87 sockets_.push_back(socket.release()); |
| 88 } |
89 } | 89 } |
90 | 90 |
91 void Reset() { | 91 void Reset() { |
92 for (size_t i = 0; i < sockets_.size(); ++i) { | |
93 if (sockets_[i].socket != net::kInvalidSocket) | |
94 ClosePlatformSocket(sockets_[i].socket); | |
95 } | |
96 sockets_.clear(); | 92 sockets_.clear(); |
97 } | 93 } |
98 | 94 |
99 private: | 95 private: |
100 std::vector<SocketInfo> sockets_; | 96 ScopedVector<net::DatagramServerSocket> sockets_; |
101 | 97 |
102 DISALLOW_COPY_AND_ASSIGN(PreCreatedMDnsSocketFactory); | 98 DISALLOW_COPY_AND_ASSIGN(PreCreatedMDnsSocketFactory); |
103 }; | 99 }; |
104 | 100 |
105 base::LazyInstance<PreCreatedMDnsSocketFactory> | 101 base::LazyInstance<PreCreatedMDnsSocketFactory> |
106 g_local_discovery_socket_factory = LAZY_INSTANCE_INITIALIZER; | 102 g_local_discovery_socket_factory = LAZY_INSTANCE_INITIALIZER; |
107 | 103 |
108 #if defined(OS_WIN) | 104 #if defined(OS_WIN) |
109 | 105 |
110 void ClosePlatformSocket(net::SocketDescriptor socket) { | 106 void ClosePlatformSocket(net::SocketDescriptor socket) { |
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 id, success, address_ipv4, address_ipv6)); | 464 id, success, address_ipv4, address_ipv6)); |
469 } | 465 } |
470 | 466 |
471 void ServiceDiscoveryMessageHandler::Send(IPC::Message* msg) { | 467 void ServiceDiscoveryMessageHandler::Send(IPC::Message* msg) { |
472 utility_task_runner_->PostTask(FROM_HERE, | 468 utility_task_runner_->PostTask(FROM_HERE, |
473 base::Bind(&SendHostMessageOnUtilityThread, | 469 base::Bind(&SendHostMessageOnUtilityThread, |
474 msg)); | 470 msg)); |
475 } | 471 } |
476 | 472 |
477 } // namespace local_discovery | 473 } // namespace local_discovery |
OLD | NEW |