| 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 #include <vector> |
| 8 | 9 |
| 9 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 10 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 12 #include "chrome/common/local_discovery/local_discovery_messages.h" | 14 #include "chrome/common/local_discovery/local_discovery_messages.h" |
| 13 #include "chrome/common/local_discovery/service_discovery_client_impl.h" | 15 #include "chrome/common/local_discovery/service_discovery_client_impl.h" |
| 14 #include "content/public/utility/utility_thread.h" | 16 #include "content/public/utility/utility_thread.h" |
| 15 #include "net/base/net_util.h" | 17 #include "net/base/net_util.h" |
| 16 #include "net/socket/socket_descriptor.h" | 18 #include "net/socket/socket_descriptor.h" |
| 17 #include "net/udp/datagram_server_socket.h" | 19 #include "net/udp/datagram_server_socket.h" |
| 18 | 20 |
| 19 namespace local_discovery { | 21 namespace local_discovery { |
| 20 | 22 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 uint32 interface_index; | 67 uint32 interface_index; |
| 66 }; | 68 }; |
| 67 | 69 |
| 68 // Returns list of sockets preallocated before. | 70 // Returns list of sockets preallocated before. |
| 69 class PreCreatedMDnsSocketFactory : public net::MDnsSocketFactory { | 71 class PreCreatedMDnsSocketFactory : public net::MDnsSocketFactory { |
| 70 public: | 72 public: |
| 71 PreCreatedMDnsSocketFactory() {} | 73 PreCreatedMDnsSocketFactory() {} |
| 72 ~PreCreatedMDnsSocketFactory() override { | 74 ~PreCreatedMDnsSocketFactory() override { |
| 73 // Not empty if process exits too fast, before starting mDns code. If | 75 // Not empty if process exits too fast, before starting mDns code. If |
| 74 // happened, destructors may crash accessing destroyed global objects. | 76 // happened, destructors may crash accessing destroyed global objects. |
| 75 sockets_.weak_clear(); | 77 // TODO This sounds memory leak, check and do better if possible |
| 78 for (scoped_ptr<net::DatagramServerSocket>& it : sockets_) |
| 79 base::IgnoreResult(it.release()); |
| 80 sockets_.clear(); |
| 76 } | 81 } |
| 77 | 82 |
| 78 // net::MDnsSocketFactory implementation: | 83 // net::MDnsSocketFactory implementation: |
| 79 void CreateSockets( | 84 void CreateSockets( |
| 80 ScopedVector<net::DatagramServerSocket>* sockets) override { | 85 std::vector<scoped_ptr<net::DatagramServerSocket>>* sockets) override { |
| 81 sockets->swap(sockets_); | 86 sockets->swap(sockets_); |
| 82 Reset(); | 87 Reset(); |
| 83 } | 88 } |
| 84 | 89 |
| 85 void AddSocket(const SocketInfo& socket_info) { | 90 void AddSocket(const SocketInfo& socket_info) { |
| 86 // Takes ownership of socket_info.socket; | 91 // Takes ownership of socket_info.socket; |
| 87 ScopedSocketFactory platform_factory(socket_info.socket); | 92 ScopedSocketFactory platform_factory(socket_info.socket); |
| 88 scoped_ptr<net::DatagramServerSocket> socket( | 93 scoped_ptr<net::DatagramServerSocket> socket( |
| 89 net::CreateAndBindMDnsSocket(socket_info.address_family, | 94 net::CreateAndBindMDnsSocket(socket_info.address_family, |
| 90 socket_info.interface_index)); | 95 socket_info.interface_index)); |
| 91 if (socket) { | 96 if (socket) { |
| 92 socket->DetachFromThread(); | 97 socket->DetachFromThread(); |
| 93 sockets_.push_back(socket.release()); | 98 sockets_.push_back(std::move(socket)); |
| 94 } | 99 } |
| 95 } | 100 } |
| 96 | 101 |
| 97 void Reset() { | 102 void Reset() { |
| 98 sockets_.clear(); | 103 sockets_.clear(); |
| 99 } | 104 } |
| 100 | 105 |
| 101 private: | 106 private: |
| 102 ScopedVector<net::DatagramServerSocket> sockets_; | 107 std::vector<scoped_ptr<net::DatagramServerSocket>> sockets_; |
| 103 | 108 |
| 104 DISALLOW_COPY_AND_ASSIGN(PreCreatedMDnsSocketFactory); | 109 DISALLOW_COPY_AND_ASSIGN(PreCreatedMDnsSocketFactory); |
| 105 }; | 110 }; |
| 106 | 111 |
| 107 base::LazyInstance<PreCreatedMDnsSocketFactory> | 112 base::LazyInstance<PreCreatedMDnsSocketFactory> |
| 108 g_local_discovery_socket_factory = LAZY_INSTANCE_INITIALIZER; | 113 g_local_discovery_socket_factory = LAZY_INSTANCE_INITIALIZER; |
| 109 | 114 |
| 110 #if defined(OS_WIN) | 115 #if defined(OS_WIN) |
| 111 | 116 |
| 112 void ClosePlatformSocket(net::SocketDescriptor socket) { | 117 void ClosePlatformSocket(net::SocketDescriptor socket) { |
| (...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 id, success, address_ipv4, address_ipv6)); | 475 id, success, address_ipv4, address_ipv6)); |
| 471 } | 476 } |
| 472 | 477 |
| 473 void ServiceDiscoveryMessageHandler::Send(IPC::Message* msg) { | 478 void ServiceDiscoveryMessageHandler::Send(IPC::Message* msg) { |
| 474 utility_task_runner_->PostTask(FROM_HERE, | 479 utility_task_runner_->PostTask(FROM_HERE, |
| 475 base::Bind(&SendHostMessageOnUtilityThread, | 480 base::Bind(&SendHostMessageOnUtilityThread, |
| 476 msg)); | 481 msg)); |
| 477 } | 482 } |
| 478 | 483 |
| 479 } // namespace local_discovery | 484 } // namespace local_discovery |
| OLD | NEW |