| 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 "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 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 15 #include "base/stl_util.h" | |
| 16 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "base/time/clock.h" | 16 #include "base/time/clock.h" |
| 18 #include "base/time/default_clock.h" | 17 #include "base/time/default_clock.h" |
| 19 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 20 #include "base/timer/timer.h" | 19 #include "base/timer/timer.h" |
| 21 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 22 #include "net/base/rand_callback.h" | 21 #include "net/base/rand_callback.h" |
| 23 #include "net/dns/dns_protocol.h" | 22 #include "net/dns/dns_protocol.h" |
| 24 #include "net/dns/dns_util.h" | 23 #include "net/dns/dns_util.h" |
| 25 #include "net/dns/record_rdata.h" | 24 #include "net/dns/record_rdata.h" |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 delegate_->HandlePacket(response, bytes_read); | 200 delegate_->HandlePacket(response, bytes_read); |
| 202 } | 201 } |
| 203 | 202 |
| 204 MDnsClientImpl::Core::Core(base::Clock* clock, base::Timer* timer) | 203 MDnsClientImpl::Core::Core(base::Clock* clock, base::Timer* timer) |
| 205 : clock_(clock), | 204 : clock_(clock), |
| 206 cleanup_timer_(timer), | 205 cleanup_timer_(timer), |
| 207 connection_(new MDnsConnection(this)) { | 206 connection_(new MDnsConnection(this)) { |
| 208 } | 207 } |
| 209 | 208 |
| 210 MDnsClientImpl::Core::~Core() { | 209 MDnsClientImpl::Core::~Core() { |
| 211 base::STLDeleteValues(&listeners_); | |
| 212 } | 210 } |
| 213 | 211 |
| 214 bool MDnsClientImpl::Core::Init(MDnsSocketFactory* socket_factory) { | 212 bool MDnsClientImpl::Core::Init(MDnsSocketFactory* socket_factory) { |
| 215 return connection_->Init(socket_factory); | 213 return connection_->Init(socket_factory); |
| 216 } | 214 } |
| 217 | 215 |
| 218 bool MDnsClientImpl::Core::SendQuery(uint16_t rrtype, const std::string& name) { | 216 bool MDnsClientImpl::Core::SendQuery(uint16_t rrtype, const std::string& name) { |
| 219 std::string name_dns; | 217 std::string name_dns; |
| 220 if (!DNSDomainFromDot(name, &name_dns)) | 218 if (!DNSDomainFromDot(name, &name_dns)) |
| 221 return false; | 219 return false; |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 ListenerMap::iterator listener_map_iterator = listeners_.find(key); | 339 ListenerMap::iterator listener_map_iterator = listeners_.find(key); |
| 342 if (listener_map_iterator == listeners_.end()) return; | 340 if (listener_map_iterator == listeners_.end()) return; |
| 343 | 341 |
| 344 for (auto& observer : *listener_map_iterator->second) | 342 for (auto& observer : *listener_map_iterator->second) |
| 345 observer.HandleRecordUpdate(update_type, record); | 343 observer.HandleRecordUpdate(update_type, record); |
| 346 } | 344 } |
| 347 | 345 |
| 348 void MDnsClientImpl::Core::AddListener( | 346 void MDnsClientImpl::Core::AddListener( |
| 349 MDnsListenerImpl* listener) { | 347 MDnsListenerImpl* listener) { |
| 350 ListenerKey key(listener->GetName(), listener->GetType()); | 348 ListenerKey key(listener->GetName(), listener->GetType()); |
| 351 std::pair<ListenerMap::iterator, bool> observer_insert_result = | |
| 352 listeners_.insert(make_pair( | |
| 353 key, static_cast<base::ObserverList<MDnsListenerImpl>*>(NULL))); | |
| 354 | 349 |
| 355 // If an equivalent key does not exist, actually create the observer list. | 350 std::unique_ptr<base::ObserverList<MDnsListenerImpl>>& observer_list = |
| 356 if (observer_insert_result.second) | 351 listeners_[key]; |
| 357 observer_insert_result.first->second = | |
| 358 new base::ObserverList<MDnsListenerImpl>(); | |
| 359 | 352 |
| 360 base::ObserverList<MDnsListenerImpl>* observer_list = | 353 if (!observer_list) |
| 361 observer_insert_result.first->second; | 354 observer_list = base::MakeUnique<base::ObserverList<MDnsListenerImpl>>(); |
| 362 | 355 |
| 363 observer_list->AddObserver(listener); | 356 observer_list->AddObserver(listener); |
| 364 } | 357 } |
| 365 | 358 |
| 366 void MDnsClientImpl::Core::RemoveListener(MDnsListenerImpl* listener) { | 359 void MDnsClientImpl::Core::RemoveListener(MDnsListenerImpl* listener) { |
| 367 ListenerKey key(listener->GetName(), listener->GetType()); | 360 ListenerKey key(listener->GetName(), listener->GetType()); |
| 368 ListenerMap::iterator observer_list_iterator = listeners_.find(key); | 361 ListenerMap::iterator observer_list_iterator = listeners_.find(key); |
| 369 | 362 |
| 370 DCHECK(observer_list_iterator != listeners_.end()); | 363 DCHECK(observer_list_iterator != listeners_.end()); |
| 371 DCHECK(observer_list_iterator->second->HasObserver(listener)); | 364 DCHECK(observer_list_iterator->second->HasObserver(listener)); |
| 372 | 365 |
| 373 observer_list_iterator->second->RemoveObserver(listener); | 366 observer_list_iterator->second->RemoveObserver(listener); |
| 374 | 367 |
| 375 // Remove the observer list from the map if it is empty | 368 // Remove the observer list from the map if it is empty |
| 376 if (!observer_list_iterator->second->might_have_observers()) { | 369 if (!observer_list_iterator->second->might_have_observers()) { |
| 377 // Schedule the actual removal for later in case the listener removal | 370 // Schedule the actual removal for later in case the listener removal |
| 378 // happens while iterating over the observer list. | 371 // happens while iterating over the observer list. |
| 379 base::ThreadTaskRunnerHandle::Get()->PostTask( | 372 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 380 FROM_HERE, base::Bind(&MDnsClientImpl::Core::CleanupObserverList, | 373 FROM_HERE, base::Bind(&MDnsClientImpl::Core::CleanupObserverList, |
| 381 AsWeakPtr(), key)); | 374 AsWeakPtr(), key)); |
| 382 } | 375 } |
| 383 } | 376 } |
| 384 | 377 |
| 385 void MDnsClientImpl::Core::CleanupObserverList(const ListenerKey& key) { | 378 void MDnsClientImpl::Core::CleanupObserverList(const ListenerKey& key) { |
| 386 ListenerMap::iterator found = listeners_.find(key); | 379 ListenerMap::iterator found = listeners_.find(key); |
| 387 if (found != listeners_.end() && !found->second->might_have_observers()) { | 380 if (found != listeners_.end() && !found->second->might_have_observers()) { |
| 388 delete found->second; | |
| 389 listeners_.erase(found); | 381 listeners_.erase(found); |
| 390 } | 382 } |
| 391 } | 383 } |
| 392 | 384 |
| 393 void MDnsClientImpl::Core::ScheduleCleanup(base::Time cleanup) { | 385 void MDnsClientImpl::Core::ScheduleCleanup(base::Time cleanup) { |
| 394 // Cleanup is already scheduled, no need to do anything. | 386 // Cleanup is already scheduled, no need to do anything. |
| 395 if (cleanup == scheduled_cleanup_) { | 387 if (cleanup == scheduled_cleanup_) { |
| 396 return; | 388 return; |
| 397 } | 389 } |
| 398 scheduled_cleanup_ = cleanup; | 390 scheduled_cleanup_ = cleanup; |
| (...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 749 | 741 |
| 750 void MDnsTransactionImpl::OnNsecRecord(const std::string& name, unsigned type) { | 742 void MDnsTransactionImpl::OnNsecRecord(const std::string& name, unsigned type) { |
| 751 TriggerCallback(RESULT_NSEC, NULL); | 743 TriggerCallback(RESULT_NSEC, NULL); |
| 752 } | 744 } |
| 753 | 745 |
| 754 void MDnsTransactionImpl::OnCachePurged() { | 746 void MDnsTransactionImpl::OnCachePurged() { |
| 755 // TODO(noamsml): Cache purge situations not yet implemented | 747 // TODO(noamsml): Cache purge situations not yet implemented |
| 756 } | 748 } |
| 757 | 749 |
| 758 } // namespace net | 750 } // namespace net |
| OLD | NEW |