Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(480)

Side by Side Diff: net/dns/mdns_client_impl.cc

Issue 1475553002: Remove ScopedVector from CreateSockets (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "net/dns/mdns_client_impl.h" 5 #include "net/dns/mdns_client_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <queue> 8 #include <queue>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 26 matching lines...) Expand all
37 // The fractions of the record's original TTL after which an active listener 37 // The fractions of the record's original TTL after which an active listener
38 // (one that had |SetActiveRefresh(true)| called) will send a query to refresh 38 // (one that had |SetActiveRefresh(true)| called) will send a query to refresh
39 // its cache. This happens both at 85% of the original TTL and again at 95% of 39 // its cache. This happens both at 85% of the original TTL and again at 95% of
40 // the original TTL. 40 // the original TTL.
41 const double kListenerRefreshRatio1 = 0.85; 41 const double kListenerRefreshRatio1 = 0.85;
42 const double kListenerRefreshRatio2 = 0.95; 42 const double kListenerRefreshRatio2 = 0.95;
43 43
44 } // namespace 44 } // namespace
45 45
46 void MDnsSocketFactoryImpl::CreateSockets( 46 void MDnsSocketFactoryImpl::CreateSockets(
47 ScopedVector<DatagramServerSocket>* sockets) { 47 std::vector<scoped_ptr<DatagramServerSocket>>* sockets) {
48 InterfaceIndexFamilyList interfaces(GetMDnsInterfacesToBind()); 48 InterfaceIndexFamilyList interfaces(GetMDnsInterfacesToBind());
49 for (size_t i = 0; i < interfaces.size(); ++i) { 49 for (size_t i = 0; i < interfaces.size(); ++i) {
50 DCHECK(interfaces[i].second == ADDRESS_FAMILY_IPV4 || 50 DCHECK(interfaces[i].second == ADDRESS_FAMILY_IPV4 ||
51 interfaces[i].second == ADDRESS_FAMILY_IPV6); 51 interfaces[i].second == ADDRESS_FAMILY_IPV6);
52 scoped_ptr<DatagramServerSocket> socket( 52 scoped_ptr<DatagramServerSocket> socket(
53 CreateAndBindMDnsSocket(interfaces[i].second, interfaces[i].first)); 53 CreateAndBindMDnsSocket(interfaces[i].second, interfaces[i].first));
54 if (socket) 54 if (socket)
55 sockets->push_back(socket.Pass()); 55 sockets->push_back(socket.Pass());
56 } 56 }
57 } 57 }
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 } 135 }
136 136
137 MDnsConnection::MDnsConnection(MDnsConnection::Delegate* delegate) 137 MDnsConnection::MDnsConnection(MDnsConnection::Delegate* delegate)
138 : delegate_(delegate), weak_ptr_factory_(this) { 138 : delegate_(delegate), weak_ptr_factory_(this) {
139 } 139 }
140 140
141 MDnsConnection::~MDnsConnection() { 141 MDnsConnection::~MDnsConnection() {
142 } 142 }
143 143
144 bool MDnsConnection::Init(MDnsSocketFactory* socket_factory) { 144 bool MDnsConnection::Init(MDnsSocketFactory* socket_factory) {
145 ScopedVector<DatagramServerSocket> sockets; 145 std::vector<scoped_ptr<DatagramServerSocket>> sockets;
146 socket_factory->CreateSockets(&sockets); 146 socket_factory->CreateSockets(&sockets);
147 147
148 for (size_t i = 0; i < sockets.size(); ++i) { 148 for (scoped_ptr<DatagramServerSocket>& socket : sockets) {
149 socket_handlers_.push_back( 149 socket_handlers_.push_back(make_scoped_ptr(
150 new MDnsConnection::SocketHandler(make_scoped_ptr(sockets[i]), this)); 150 new MDnsConnection::SocketHandler(std::move(socket), this)));
151 } 151 }
152 sockets.weak_clear();
153 152
154 // All unbound sockets need to be bound before processing untrusted input. 153 // All unbound sockets need to be bound before processing untrusted input.
155 // This is done for security reasons, so that an attacker can't get an unbound 154 // This is done for security reasons, so that an attacker can't get an unbound
156 // socket. 155 // socket.
157 for (size_t i = 0; i < socket_handlers_.size();) { 156 for (size_t i = 0; i < socket_handlers_.size();) {
158 int rv = socket_handlers_[i]->Start(); 157 int rv = socket_handlers_[i]->Start();
159 if (rv != OK) { 158 if (rv != OK) {
160 socket_handlers_.erase(socket_handlers_.begin() + i); 159 socket_handlers_.erase(socket_handlers_.begin() + i);
161 VLOG(1) << "Start failed, socket=" << i << ", error=" << rv; 160 VLOG(1) << "Start failed, socket=" << i << ", error=" << rv;
162 } else { 161 } else {
163 ++i; 162 ++i;
164 } 163 }
165 } 164 }
166 VLOG(1) << "Sockets ready:" << socket_handlers_.size(); 165 VLOG(1) << "Sockets ready:" << socket_handlers_.size();
167 return !socket_handlers_.empty(); 166 return !socket_handlers_.empty();
168 } 167 }
169 168
170 void MDnsConnection::Send(const scoped_refptr<IOBuffer>& buffer, 169 void MDnsConnection::Send(const scoped_refptr<IOBuffer>& buffer,
171 unsigned size) { 170 unsigned size) {
172 for (size_t i = 0; i < socket_handlers_.size(); ++i) 171 for (scoped_ptr<SocketHandler>& handler : socket_handlers_)
173 socket_handlers_[i]->Send(buffer, size); 172 handler->Send(buffer, size);
174 } 173 }
175 174
176 void MDnsConnection::PostOnError(SocketHandler* loop, int rv) { 175 void MDnsConnection::PostOnError(SocketHandler* loop, int rv) {
177 VLOG(1) << "Socket error. id=" 176 int id = 0;
178 << std::find(socket_handlers_.begin(), socket_handlers_.end(), loop) - 177 for (const auto& it : socket_handlers_) {
179 socket_handlers_.begin() << ", error=" << rv; 178 if (it.get() == loop)
179 break;
180 id++;
181 }
182 VLOG(1) << "Socket error. id=" << id << ", error=" << rv;
180 // Post to allow deletion of this object by delegate. 183 // Post to allow deletion of this object by delegate.
181 base::ThreadTaskRunnerHandle::Get()->PostTask( 184 base::ThreadTaskRunnerHandle::Get()->PostTask(
182 FROM_HERE, 185 FROM_HERE,
183 base::Bind(&MDnsConnection::OnError, weak_ptr_factory_.GetWeakPtr(), rv)); 186 base::Bind(&MDnsConnection::OnError, weak_ptr_factory_.GetWeakPtr(), rv));
184 } 187 }
185 188
186 void MDnsConnection::OnError(int rv) { 189 void MDnsConnection::OnError(int rv) {
187 // TODO(noamsml): Specific handling of intermittent errors that can be handled 190 // TODO(noamsml): Specific handling of intermittent errors that can be handled
188 // in the connection. 191 // in the connection.
189 delegate_->OnConnectionError(rv); 192 delegate_->OnConnectionError(rv);
(...skipping 551 matching lines...) Expand 10 before | Expand all | Expand 10 after
741 744
742 void MDnsTransactionImpl::OnNsecRecord(const std::string& name, unsigned type) { 745 void MDnsTransactionImpl::OnNsecRecord(const std::string& name, unsigned type) {
743 TriggerCallback(RESULT_NSEC, NULL); 746 TriggerCallback(RESULT_NSEC, NULL);
744 } 747 }
745 748
746 void MDnsTransactionImpl::OnCachePurged() { 749 void MDnsTransactionImpl::OnCachePurged() {
747 // TODO(noamsml): Cache purge situations not yet implemented 750 // TODO(noamsml): Cache purge situations not yet implemented
748 } 751 }
749 752
750 } // namespace net 753 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698