| 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 sockets->swap(sockets_); | 75 for (size_t i = 0; i < sockets_.size(); ++i) { |
| 76 Reset(); | 76 // Takes ownership of sockets_[i].socket; |
| 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(); |
| 77 } | 85 } |
| 78 | 86 |
| 79 void AddSocket(const SocketInfo& socket_info) { | 87 void AddSocket(const SocketInfo& socket) { |
| 80 // Takes ownership of socket_info.socket; | 88 sockets_.push_back(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 } |
| 92 sockets_.clear(); | 96 sockets_.clear(); |
| 93 } | 97 } |
| 94 | 98 |
| 95 private: | 99 private: |
| 96 ScopedVector<net::DatagramServerSocket> sockets_; | 100 std::vector<SocketInfo> sockets_; |
| 97 | 101 |
| 98 DISALLOW_COPY_AND_ASSIGN(PreCreatedMDnsSocketFactory); | 102 DISALLOW_COPY_AND_ASSIGN(PreCreatedMDnsSocketFactory); |
| 99 }; | 103 }; |
| 100 | 104 |
| 101 base::LazyInstance<PreCreatedMDnsSocketFactory> | 105 base::LazyInstance<PreCreatedMDnsSocketFactory> |
| 102 g_local_discovery_socket_factory = LAZY_INSTANCE_INITIALIZER; | 106 g_local_discovery_socket_factory = LAZY_INSTANCE_INITIALIZER; |
| 103 | 107 |
| 104 #if defined(OS_WIN) | 108 #if defined(OS_WIN) |
| 105 | 109 |
| 106 void ClosePlatformSocket(net::SocketDescriptor socket) { | 110 void ClosePlatformSocket(net::SocketDescriptor socket) { |
| (...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 id, success, address_ipv4, address_ipv6)); | 468 id, success, address_ipv4, address_ipv6)); |
| 465 } | 469 } |
| 466 | 470 |
| 467 void ServiceDiscoveryMessageHandler::Send(IPC::Message* msg) { | 471 void ServiceDiscoveryMessageHandler::Send(IPC::Message* msg) { |
| 468 utility_task_runner_->PostTask(FROM_HERE, | 472 utility_task_runner_->PostTask(FROM_HERE, |
| 469 base::Bind(&SendHostMessageOnUtilityThread, | 473 base::Bind(&SendHostMessageOnUtilityThread, |
| 470 msg)); | 474 msg)); |
| 471 } | 475 } |
| 472 | 476 |
| 473 } // namespace local_discovery | 477 } // namespace local_discovery |
| OLD | NEW |