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_) { | |
Vitaly Buka (NO REVIEWS)
2015/12/02 18:32:10
use of {} is inconsistent with the rest of the fil
| |
79 base::IgnoreResult(it.release()); | |
80 } | |
81 sockets_.clear(); | |
76 } | 82 } |
77 | 83 |
78 // net::MDnsSocketFactory implementation: | 84 // net::MDnsSocketFactory implementation: |
79 void CreateSockets( | 85 void CreateSockets( |
80 ScopedVector<net::DatagramServerSocket>* sockets) override { | 86 std::vector<scoped_ptr<net::DatagramServerSocket>>* sockets) override { |
81 sockets->swap(sockets_); | 87 sockets->swap(sockets_); |
82 Reset(); | 88 Reset(); |
83 } | 89 } |
84 | 90 |
85 void AddSocket(const SocketInfo& socket_info) { | 91 void AddSocket(const SocketInfo& socket_info) { |
86 // Takes ownership of socket_info.socket; | 92 // Takes ownership of socket_info.socket; |
87 ScopedSocketFactory platform_factory(socket_info.socket); | 93 ScopedSocketFactory platform_factory(socket_info.socket); |
88 scoped_ptr<net::DatagramServerSocket> socket( | 94 scoped_ptr<net::DatagramServerSocket> socket( |
89 net::CreateAndBindMDnsSocket(socket_info.address_family, | 95 net::CreateAndBindMDnsSocket(socket_info.address_family, |
90 socket_info.interface_index)); | 96 socket_info.interface_index)); |
91 if (socket) { | 97 if (socket) { |
92 socket->DetachFromThread(); | 98 socket->DetachFromThread(); |
93 sockets_.push_back(socket.release()); | 99 sockets_.push_back(std::move(socket)); |
94 } | 100 } |
95 } | 101 } |
96 | 102 |
97 void Reset() { | 103 void Reset() { |
98 sockets_.clear(); | 104 sockets_.clear(); |
99 } | 105 } |
100 | 106 |
101 private: | 107 private: |
102 ScopedVector<net::DatagramServerSocket> sockets_; | 108 std::vector<scoped_ptr<net::DatagramServerSocket>> sockets_; |
103 | 109 |
104 DISALLOW_COPY_AND_ASSIGN(PreCreatedMDnsSocketFactory); | 110 DISALLOW_COPY_AND_ASSIGN(PreCreatedMDnsSocketFactory); |
105 }; | 111 }; |
106 | 112 |
107 base::LazyInstance<PreCreatedMDnsSocketFactory> | 113 base::LazyInstance<PreCreatedMDnsSocketFactory> |
108 g_local_discovery_socket_factory = LAZY_INSTANCE_INITIALIZER; | 114 g_local_discovery_socket_factory = LAZY_INSTANCE_INITIALIZER; |
109 | 115 |
110 #if defined(OS_WIN) | 116 #if defined(OS_WIN) |
111 | 117 |
112 void ClosePlatformSocket(net::SocketDescriptor socket) { | 118 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)); | 476 id, success, address_ipv4, address_ipv6)); |
471 } | 477 } |
472 | 478 |
473 void ServiceDiscoveryMessageHandler::Send(IPC::Message* msg) { | 479 void ServiceDiscoveryMessageHandler::Send(IPC::Message* msg) { |
474 utility_task_runner_->PostTask(FROM_HERE, | 480 utility_task_runner_->PostTask(FROM_HERE, |
475 base::Bind(&SendHostMessageOnUtilityThread, | 481 base::Bind(&SendHostMessageOnUtilityThread, |
476 msg)); | 482 msg)); |
477 } | 483 } |
478 | 484 |
479 } // namespace local_discovery | 485 } // namespace local_discovery |
OLD | NEW |