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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 net::SocketDescriptor socket; | 59 net::SocketDescriptor socket; |
60 net::AddressFamily address_family; | 60 net::AddressFamily address_family; |
61 uint32 interface_index; | 61 uint32 interface_index; |
62 }; | 62 }; |
63 | 63 |
64 // Returns list of sockets preallocated before. | 64 // Returns list of sockets preallocated before. |
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 // Not empty if process exits too fast, before starting mDns code. If |
| 70 // happened, destructors may crash accessing destroyed global objects. |
| 71 sockets_.weak_clear(); |
70 } | 72 } |
71 | 73 |
72 // net::MDnsSocketFactory implementation: | 74 // net::MDnsSocketFactory implementation: |
73 virtual void CreateSockets( | 75 virtual void CreateSockets( |
74 ScopedVector<net::DatagramServerSocket>* sockets) OVERRIDE { | 76 ScopedVector<net::DatagramServerSocket>* sockets) OVERRIDE { |
75 for (size_t i = 0; i < sockets_.size(); ++i) { | 77 sockets->swap(sockets_); |
76 // Takes ownership of sockets_[i].socket; | 78 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 } | 79 } |
86 | 80 |
87 void AddSocket(const SocketInfo& socket) { | 81 void AddSocket(const SocketInfo& socket_info) { |
88 sockets_.push_back(socket); | 82 // Takes ownership of socket_info.socket; |
| 83 ScopedSocketFactory platform_factory(socket_info.socket); |
| 84 scoped_ptr<net::DatagramServerSocket> socket( |
| 85 net::CreateAndBindMDnsSocket(socket_info.address_family, |
| 86 socket_info.interface_index)); |
| 87 if (socket) { |
| 88 socket->DetachFromThread(); |
| 89 sockets_.push_back(socket.release()); |
| 90 } |
89 } | 91 } |
90 | 92 |
91 void Reset() { | 93 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(); | 94 sockets_.clear(); |
97 } | 95 } |
98 | 96 |
99 private: | 97 private: |
100 std::vector<SocketInfo> sockets_; | 98 ScopedVector<net::DatagramServerSocket> sockets_; |
101 | 99 |
102 DISALLOW_COPY_AND_ASSIGN(PreCreatedMDnsSocketFactory); | 100 DISALLOW_COPY_AND_ASSIGN(PreCreatedMDnsSocketFactory); |
103 }; | 101 }; |
104 | 102 |
105 base::LazyInstance<PreCreatedMDnsSocketFactory> | 103 base::LazyInstance<PreCreatedMDnsSocketFactory> |
106 g_local_discovery_socket_factory = LAZY_INSTANCE_INITIALIZER; | 104 g_local_discovery_socket_factory = LAZY_INSTANCE_INITIALIZER; |
107 | 105 |
108 #if defined(OS_WIN) | 106 #if defined(OS_WIN) |
109 | 107 |
110 void ClosePlatformSocket(net::SocketDescriptor socket) { | 108 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)); | 466 id, success, address_ipv4, address_ipv6)); |
469 } | 467 } |
470 | 468 |
471 void ServiceDiscoveryMessageHandler::Send(IPC::Message* msg) { | 469 void ServiceDiscoveryMessageHandler::Send(IPC::Message* msg) { |
472 utility_task_runner_->PostTask(FROM_HERE, | 470 utility_task_runner_->PostTask(FROM_HERE, |
473 base::Bind(&SendHostMessageOnUtilityThread, | 471 base::Bind(&SendHostMessageOnUtilityThread, |
474 msg)); | 472 msg)); |
475 } | 473 } |
476 | 474 |
477 } // namespace local_discovery | 475 } // namespace local_discovery |
OLD | NEW |