Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/dns/mdns_client_impl.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/message_loop_proxy.h" | |
| 9 #include "base/stl_util.h" | |
| 10 #include "base/time/default_clock.h" | |
| 11 #include "net/base/dns_util.h" | |
| 12 #include "net/base/net_errors.h" | |
| 13 #include "net/base/net_log.h" | |
| 14 #include "net/base/rand_callback.h" | |
| 15 #include "net/dns/dns_protocol.h" | |
| 16 #include "net/udp/datagram_socket.h" | |
| 17 | |
| 18 namespace net { | |
| 19 | |
| 20 namespace { | |
| 21 const char kMDnsMulticastGroupIPv4[] = "224.0.0.251"; | |
| 22 const char kMDnsMulticastGroupIPv6[] = "FF02::FB"; | |
| 23 const unsigned MDnsTransactionTimeoutSeconds = 3; | |
| 24 } | |
| 25 | |
| 26 MDnsConnection::SocketHandler::SocketHandler( | |
| 27 MDnsConnection* connection, const IPEndPoint& multicast_addr, | |
| 28 MDnsConnection::SocketFactory* socket_factory) | |
| 29 : socket_(socket_factory->CreateSocket()), connection_(connection), | |
| 30 response_(new DnsResponse(dns_protocol::kMaxMulticastSize)), | |
| 31 multicast_addr_(multicast_addr) { | |
| 32 } | |
| 33 | |
| 34 MDnsConnection::SocketHandler::~SocketHandler() { | |
| 35 } | |
| 36 | |
| 37 int MDnsConnection::SocketHandler::Start() { | |
| 38 int rv = BindSocket(); | |
| 39 if (rv != OK) { | |
| 40 return rv; | |
| 41 } | |
| 42 | |
| 43 return DoLoop(0); | |
| 44 } | |
| 45 | |
| 46 int MDnsConnection::SocketHandler::DoLoop(int rv) { | |
| 47 do { | |
| 48 if (rv > 0) | |
| 49 connection_->OnDatagramReceived(response_.get(), recv_addr_, rv); | |
| 50 | |
| 51 rv = socket_->RecvFrom( | |
| 52 response_->io_buffer(), | |
| 53 response_->io_buffer()->size(), | |
| 54 &recv_addr_, | |
| 55 base::Bind(&MDnsConnection::SocketHandler::OnDatagramReceived, | |
| 56 base::Unretained(this))); | |
| 57 } while (rv > 0); | |
| 58 | |
| 59 if (rv != ERR_IO_PENDING) | |
| 60 return rv; | |
| 61 | |
| 62 return OK; | |
| 63 } | |
| 64 | |
| 65 void MDnsConnection::SocketHandler::OnDatagramReceived(int rv) { | |
| 66 if (rv >= OK) | |
| 67 rv = DoLoop(rv); | |
| 68 | |
| 69 if (rv != OK) | |
| 70 connection_->OnError(this, rv); | |
| 71 } | |
| 72 | |
| 73 int MDnsConnection::SocketHandler::Send(IOBuffer* buffer, unsigned size) { | |
| 74 return socket_->SendTo( | |
| 75 buffer, size, multicast_addr_, | |
| 76 base::Bind(&MDnsConnection::SocketHandler::SendDone, | |
| 77 base::Unretained(this) )); | |
| 78 } | |
| 79 | |
| 80 void MDnsConnection::SocketHandler::SendDone(int rv) { | |
| 81 // TODO(noamsml): Retry logic. | |
| 82 } | |
| 83 | |
| 84 int MDnsConnection::SocketHandler::BindSocket() { | |
| 85 IPAddressNumber address_any(multicast_addr_.address().size()); | |
| 86 | |
| 87 IPEndPoint bind_endpoint(address_any, multicast_addr_.port()); | |
| 88 | |
| 89 socket_->AllowAddressReuse(); | |
| 90 int rv = socket_->Listen(bind_endpoint); | |
| 91 | |
| 92 if (rv < OK) return rv; | |
| 93 | |
| 94 socket_->SetMulticastLoopbackMode(false); | |
| 95 | |
| 96 return socket_->JoinGroup(multicast_addr_.address()); | |
| 97 } | |
| 98 | |
| 99 MDnsConnection::MDnsConnection(MDnsConnection::SocketFactory* socket_factory, | |
| 100 MDnsConnection::Delegate* delegate) | |
| 101 : socket_handler_ipv4_(this, | |
| 102 GetMDnsIPEndPoint(kMDnsMulticastGroupIPv4), | |
| 103 socket_factory), | |
| 104 socket_handler_ipv6_(this, | |
| 105 GetMDnsIPEndPoint(kMDnsMulticastGroupIPv6), | |
| 106 socket_factory), | |
| 107 delegate_(delegate) { | |
| 108 } | |
| 109 | |
| 110 MDnsConnection::~MDnsConnection() { | |
| 111 } | |
| 112 | |
| 113 int MDnsConnection::Init() { | |
| 114 int rv; | |
| 115 | |
| 116 rv = socket_handler_ipv4_.Start(); | |
| 117 if (rv != OK) return rv; | |
| 118 rv = socket_handler_ipv6_.Start(); | |
| 119 if (rv != OK) return rv; | |
| 120 | |
| 121 return OK; | |
| 122 } | |
| 123 | |
| 124 int MDnsConnection::Send(IOBuffer* buffer, unsigned size) { | |
| 125 int rv; | |
| 126 | |
| 127 rv = socket_handler_ipv4_.Send(buffer, size); | |
| 128 if (rv < OK && rv != ERR_IO_PENDING) return rv; | |
| 129 | |
| 130 rv = socket_handler_ipv6_.Send(buffer, size); | |
| 131 if (rv < OK && rv != ERR_IO_PENDING) return rv; | |
| 132 | |
| 133 return OK; | |
| 134 } | |
| 135 | |
| 136 void MDnsConnection::OnError(SocketHandler* loop, | |
| 137 int error) { | |
| 138 // TODO(noamsml): Specific handling of intermittent errors that can be handled | |
| 139 // in the connection. | |
| 140 delegate_->OnConnectionError(error); | |
| 141 } | |
| 142 | |
| 143 IPEndPoint MDnsConnection::GetMDnsIPEndPoint(const char* address) { | |
| 144 IPAddressNumber multicast_group_number; | |
| 145 bool success = ParseIPLiteralToNumber(address, | |
| 146 &multicast_group_number); | |
| 147 DCHECK(success); | |
| 148 return IPEndPoint(multicast_group_number, | |
| 149 dns_protocol::kDefaultPortMulticast); | |
| 150 } | |
| 151 | |
| 152 void MDnsConnection::OnDatagramReceived( | |
| 153 DnsResponse* response, | |
| 154 const IPEndPoint& recv_addr, | |
| 155 int bytes_read) { | |
| 156 // TODO(noamsml): More sophisticated error handling. | |
| 157 DCHECK_GT(bytes_read, 0); | |
| 158 delegate_->HandlePacket(response, bytes_read); | |
| 159 } | |
| 160 | |
| 161 class MDnsConnectionSocketFactoryImpl | |
| 162 : public MDnsConnection::SocketFactory { | |
| 163 public: | |
| 164 MDnsConnectionSocketFactoryImpl(); | |
| 165 virtual ~MDnsConnectionSocketFactoryImpl(); | |
| 166 | |
| 167 virtual scoped_ptr<DatagramServerSocket> CreateSocket() OVERRIDE; | |
| 168 }; | |
| 169 | |
| 170 MDnsConnectionSocketFactoryImpl::MDnsConnectionSocketFactoryImpl() { | |
| 171 } | |
| 172 | |
| 173 MDnsConnectionSocketFactoryImpl::~MDnsConnectionSocketFactoryImpl() { | |
| 174 } | |
| 175 | |
| 176 scoped_ptr<DatagramServerSocket> | |
| 177 MDnsConnectionSocketFactoryImpl::CreateSocket() { | |
| 178 return scoped_ptr<DatagramServerSocket>(new UDPServerSocket( | |
| 179 NULL, NetLog::Source())); | |
| 180 } | |
| 181 | |
| 182 // static | |
| 183 scoped_ptr<MDnsConnection::SocketFactory> | |
| 184 MDnsConnection::SocketFactory::CreateDefault() { | |
| 185 return scoped_ptr<MDnsConnection::SocketFactory>( | |
| 186 new MDnsConnectionSocketFactoryImpl); | |
| 187 } | |
| 188 | |
| 189 MDnsClientImpl::Core::Core(MDnsClientImpl* client, | |
| 190 MDnsConnection::SocketFactory* socket_factory) | |
| 191 : client_(client), connection_(new MDnsConnection(socket_factory, this)) { | |
| 192 } | |
| 193 | |
| 194 MDnsClientImpl::Core::~Core() { | |
| 195 STLDeleteValues(&listeners_); | |
| 196 } | |
| 197 | |
| 198 bool MDnsClientImpl::Core::Init() { | |
| 199 return connection_->Init() == OK; | |
| 200 } | |
| 201 | |
| 202 bool MDnsClientImpl::Core::SendQuery(uint16 rrtype, std::string name) { | |
| 203 std::string name_dns; | |
| 204 if (!DNSDomainFromDot(name, &name_dns)) | |
| 205 return false; | |
| 206 | |
| 207 DnsQuery query(0, name_dns, rrtype); | |
| 208 query.set_flags(0); // Remove the RD flag from the query. It is unneeded. | |
| 209 | |
| 210 return connection_->Send(query.io_buffer(), query.io_buffer()->size()) == OK; | |
| 211 } | |
| 212 | |
| 213 void MDnsClientImpl::Core::HandlePacket(DnsResponse* response, | |
| 214 int bytes_read) { | |
| 215 unsigned offset; | |
| 216 | |
| 217 if (!response->InitParseWithoutQuery(bytes_read)) { | |
| 218 LOG(WARNING) << "Could not understand an mDNS packet."; | |
| 219 return; // Message is unreadable. | |
| 220 } | |
| 221 | |
| 222 // TODO(noamsml): duplicate query suppression. | |
| 223 if (!(response->flags() & dns_protocol::kFlagResponse)) | |
| 224 return; // Message is a query. ignore it. | |
| 225 | |
| 226 DnsRecordParser parser = response->Parser(); | |
| 227 unsigned answer_count = response->answer_count() + | |
| 228 response->additional_answer_count(); | |
| 229 | |
| 230 for (unsigned i = 0; i < answer_count; i++) { | |
| 231 offset = parser.GetOffset(); | |
| 232 scoped_ptr<const RecordParsed> scoped_record = RecordParsed::CreateFrom( | |
| 233 &parser, base::Time::Now()); | |
| 234 | |
| 235 if (!scoped_record) { | |
| 236 LOG(WARNING) << "Could not understand an mDNS record."; | |
| 237 | |
| 238 if (offset == parser.GetOffset()) { | |
| 239 LOG(WARNING) << "Abandoned parsing the rest of the packet."; | |
| 240 return; // The parser did not advance, abort reading the packet. | |
| 241 } else { | |
| 242 continue; // We may be able to extract other records from the packet. | |
| 243 } | |
| 244 } | |
| 245 | |
| 246 if ((scoped_record->klass() & dns_protocol::kMDnsClassMask) != | |
| 247 dns_protocol::kClassIN) { | |
| 248 LOG(WARNING) << "Received an mDNS record with non-IN class. Ignoring."; | |
| 249 continue; // Ignore all records not in the IN class. | |
| 250 } | |
| 251 | |
| 252 // We want to retain a copy of the record pointer for updating listeners | |
| 253 // but we are passing ownership to the cache. | |
| 254 const RecordParsed* record = scoped_record.get(); | |
| 255 MDnsCache::UpdateType update = cache_.UpdateDnsRecord(scoped_record.Pass()); | |
| 256 | |
| 257 // Cleanup time may have changed. | |
| 258 ScheduleCleanup(cache_.next_expiration()); | |
| 259 | |
| 260 if (update != MDnsCache::NoChange) { | |
| 261 MDnsListener::UpdateType update_external; | |
| 262 | |
| 263 switch (update) { | |
| 264 case MDnsCache::RecordAdded: | |
| 265 update_external = MDnsListener::RECORD_ADDED; | |
| 266 break; | |
| 267 case MDnsCache::RecordChanged: | |
| 268 update_external = MDnsListener::RECORD_CHANGED; | |
| 269 break; | |
| 270 case MDnsCache::NoChange: | |
| 271 default: | |
| 272 NOTREACHED(); | |
| 273 // Dummy assignment to suppress compiler warning. | |
| 274 update_external = MDnsListener::RECORD_CHANGED; | |
| 275 break; | |
| 276 } | |
| 277 | |
| 278 AlertListeners(update_external, | |
| 279 ListenerKey(record->type(), record->name()), record); | |
| 280 // Alert listeners listening only for rrtype and not for name. | |
| 281 AlertListeners(update_external, ListenerKey(record->type(), ""), record); | |
| 282 } | |
| 283 } | |
| 284 } | |
| 285 | |
| 286 void MDnsClientImpl::Core::OnConnectionError(int error) { | |
| 287 // TODO(noamsml): On connection error, recreate connection and flush cache. | |
| 288 } | |
| 289 | |
| 290 void MDnsClientImpl::Core::AlertListeners( | |
| 291 MDnsListener::UpdateType update_type, | |
| 292 const ListenerKey& key, | |
| 293 const RecordParsed* record) { | |
| 294 ListenerMap::iterator listener_map_iterator = listeners_.find(key); | |
| 295 if (listener_map_iterator == listeners_.end()) return; | |
| 296 | |
| 297 FOR_EACH_OBSERVER(MDnsListenerImpl, *listener_map_iterator->second, | |
| 298 AlertDelegate(update_type, record)); | |
| 299 } | |
| 300 | |
| 301 void MDnsClientImpl::Core::AddListener( | |
| 302 MDnsListenerImpl* listener) { | |
| 303 ListenerKey key(listener->GetType(), listener->GetName()); | |
| 304 std::pair<ListenerMap::iterator, bool> observer_insert_result = | |
| 305 listeners_.insert( | |
| 306 make_pair(key, static_cast<ObserverList<MDnsListenerImpl>*>(NULL))); | |
| 307 | |
| 308 // If an equivalent key does not exist, actually create the observer list. | |
| 309 if (observer_insert_result.second) | |
| 310 observer_insert_result.first->second = new ObserverList<MDnsListenerImpl>(); | |
| 311 | |
| 312 ObserverList<MDnsListenerImpl>* observer_list = | |
| 313 observer_insert_result.first->second; | |
| 314 | |
| 315 observer_list->AddObserver(listener); | |
| 316 } | |
| 317 | |
| 318 void MDnsClientImpl::Core::RemoveListener(MDnsListenerImpl* listener) { | |
| 319 ListenerKey key(listener->GetType(), listener->GetName()); | |
| 320 ListenerMap::iterator observer_list_iterator = listeners_.find(key); | |
| 321 | |
| 322 DCHECK(observer_list_iterator != listeners_.end()); | |
| 323 DCHECK(observer_list_iterator->second->HasObserver(listener)); | |
| 324 | |
| 325 observer_list_iterator->second->RemoveObserver(listener); | |
| 326 | |
| 327 // Remove the observer list from the map if it is empty | |
| 328 if (observer_list_iterator->second->size() == 0) { | |
| 329 delete observer_list_iterator->second; | |
| 330 listeners_.erase(observer_list_iterator); | |
| 331 } | |
| 332 } | |
| 333 | |
| 334 void MDnsClientImpl::Core::ScheduleCleanup(base::Time cleanup) { | |
| 335 // Cleanup is already scheduled, no need to do anything. | |
| 336 if (cleanup == scheduled_cleanup_) return; | |
| 337 scheduled_cleanup_ = cleanup; | |
| 338 | |
| 339 // This cancels the previously scheduled cleanup. | |
| 340 cleanup_callback_.Reset(base::Bind( | |
| 341 &MDnsClientImpl::Core::DoCleanup, base::Unretained(this))); | |
| 342 | |
| 343 // If |cleanup| is empty, then no cleanup necessary. | |
| 344 if (cleanup != base::Time()) { | |
| 345 base::MessageLoop::current()->PostDelayedTask( | |
| 346 FROM_HERE, | |
| 347 cleanup_callback_.callback(), | |
| 348 cleanup - base::Time::Now()); | |
| 349 } | |
| 350 } | |
| 351 | |
| 352 void MDnsClientImpl::Core::DoCleanup() { | |
| 353 cache_.CleanupRecords(base::Time::Now(), base::Bind( | |
| 354 &MDnsClientImpl::Core::OnRecordRemoved, base::Unretained(this))); | |
| 355 | |
| 356 ScheduleCleanup(cache_.next_expiration()); | |
| 357 } | |
| 358 | |
| 359 void MDnsClientImpl::Core::OnRecordRemoved( | |
| 360 const RecordParsed* record) { | |
| 361 AlertListeners(MDnsListener::RECORD_REMOVED, | |
| 362 ListenerKey(record->type(), record->name()), record); | |
| 363 // Alert listeners listening only for rrtype and not for name. | |
| 364 AlertListeners(MDnsListener::RECORD_REMOVED, ListenerKey(record->type(), ""), | |
| 365 record); | |
| 366 } | |
| 367 | |
| 368 void MDnsClientImpl::Core::QueryCache( | |
| 369 uint16 rrtype, const std::string& name, | |
| 370 std::vector<const RecordParsed*>* records) const { | |
| 371 cache_.FindDnsRecords(rrtype, name, records, base::Time::Now()); | |
| 372 } | |
| 373 | |
| 374 MDnsClientImpl::MDnsClientImpl( | |
| 375 scoped_ptr<MDnsConnection::SocketFactory> socket_factory) | |
| 376 : listen_refs_(0), socket_factory_(socket_factory.Pass()) { | |
| 377 } | |
| 378 | |
| 379 MDnsClientImpl::~MDnsClientImpl() { | |
| 380 } | |
| 381 | |
| 382 bool MDnsClientImpl::AddListenRef() { | |
| 383 if (!core_.get()) { | |
| 384 core_.reset(new Core(this, socket_factory_.get())); | |
| 385 if (!core_->Init()) { | |
| 386 core_.reset(); | |
| 387 return false; | |
| 388 } | |
| 389 } | |
| 390 listen_refs_++; | |
| 391 return true; | |
| 392 } | |
| 393 | |
| 394 void MDnsClientImpl::SubtractListenRef() { | |
| 395 listen_refs_--; | |
| 396 if (listen_refs_ == 0) { | |
| 397 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | |
| 398 &MDnsClientImpl::Shutdown, base::Unretained(this))); | |
| 399 } | |
| 400 } | |
| 401 | |
| 402 void MDnsClientImpl::Shutdown() { | |
| 403 // We need to check that new listeners haven't been created. | |
| 404 if (listen_refs_ == 0) { | |
| 405 core_.reset(); | |
| 406 } | |
| 407 } | |
| 408 | |
| 409 bool MDnsClientImpl::IsListeningForTests() { | |
| 410 return core_.get() != NULL; | |
| 411 } | |
| 412 | |
| 413 scoped_ptr<MDnsListener> MDnsClientImpl::CreateListener( | |
| 414 uint16 rrtype, | |
| 415 const std::string& name, | |
| 416 MDnsListener::Delegate* delegate) { | |
| 417 return scoped_ptr<net::MDnsListener>( | |
| 418 new MDnsListenerImpl(rrtype, name, delegate, this)); | |
| 419 } | |
| 420 | |
| 421 scoped_ptr<MDnsTransaction> MDnsClientImpl::CreateTransaction( | |
| 422 uint16 rrtype, | |
| 423 const std::string& name, | |
| 424 int flags, | |
| 425 const MDnsTransaction::ResultCallback& callback) { | |
| 426 return scoped_ptr<MDnsTransaction>( | |
| 427 new MDnsTransactionImpl(rrtype, name, flags, callback, this)); | |
| 428 } | |
| 429 | |
| 430 MDnsListenerImpl::MDnsListenerImpl( | |
| 431 uint16 rrtype, | |
| 432 const std::string& name, | |
| 433 MDnsListener::Delegate* delegate, | |
| 434 MDnsClientImpl* client) | |
| 435 : rrtype_(rrtype), name_(name), client_(client), delegate_(delegate), | |
| 436 started_(false) { | |
| 437 } | |
| 438 | |
| 439 bool MDnsListenerImpl::Start() { | |
| 440 DCHECK(!started_); | |
| 441 | |
| 442 if (!client_->AddListenRef()) return false; | |
| 443 started_ = true; | |
| 444 | |
| 445 DCHECK(client_->core()); | |
| 446 client_->core()->AddListener(this); | |
| 447 | |
| 448 return true; | |
| 449 } | |
| 450 | |
| 451 MDnsListenerImpl::~MDnsListenerImpl() { | |
| 452 if (started_) { | |
| 453 DCHECK(client_->core()); | |
| 454 client_->core()->RemoveListener(this); | |
| 455 client_->SubtractListenRef(); | |
| 456 } | |
| 457 } | |
| 458 | |
| 459 const std::string& MDnsListenerImpl::GetName() const { | |
| 460 return name_; | |
| 461 } | |
| 462 | |
| 463 uint16 MDnsListenerImpl::GetType() const { | |
| 464 return rrtype_; | |
| 465 } | |
| 466 | |
| 467 void MDnsListenerImpl::AlertDelegate(MDnsListener::UpdateType update_type, | |
| 468 const RecordParsed* record) { | |
| 469 DCHECK(started_); | |
| 470 delegate_->OnRecordUpdate(update_type, record); | |
| 471 } | |
| 472 | |
| 473 MDnsTransactionImpl::MDnsTransactionImpl( | |
| 474 uint16 rrtype, | |
| 475 const std::string& name, | |
| 476 int flags, | |
| 477 const MDnsTransaction::ResultCallback& callback, | |
| 478 MDnsClientImpl* client) | |
| 479 : rrtype_(rrtype), name_(name), callback_(callback), client_(client), | |
| 480 started_(false), flags_(flags) { | |
| 481 DCHECK((flags_ & MDnsTransaction::FLAG_MASK) == flags_); | |
|
szym
2013/06/13 19:20:00
For future reference, you don't need the class nam
| |
| 482 DCHECK(flags_ & MDnsTransaction::QUERY_CACHE || | |
| 483 flags_ & MDnsTransaction::QUERY_NETWORK); | |
| 484 } | |
| 485 | |
| 486 MDnsTransactionImpl::~MDnsTransactionImpl() { | |
| 487 timeout_.Cancel(); | |
| 488 } | |
| 489 | |
| 490 bool MDnsTransactionImpl::Start() { | |
| 491 DCHECK(!started_); | |
| 492 started_ = true; | |
| 493 std::vector<const RecordParsed*> records; | |
| 494 base::WeakPtr<MDnsTransactionImpl> weak_this = AsWeakPtr(); | |
| 495 | |
| 496 if (flags_ & MDnsTransaction::QUERY_CACHE) { | |
| 497 if (client_->core()) { | |
| 498 client_->core()->QueryCache(rrtype_, name_, &records); | |
| 499 for (std::vector<const RecordParsed*>::iterator i = records.begin(); | |
| 500 i != records.end() && weak_this; ++i) { | |
| 501 weak_this->TriggerCallback(MDnsTransaction::RESULT_RECORD, | |
| 502 records.front()); | |
| 503 } | |
| 504 } | |
| 505 } | |
| 506 | |
| 507 if (!weak_this) return true; | |
| 508 | |
| 509 if (is_active() && (flags_ & MDnsTransaction::QUERY_NETWORK)) { | |
| 510 listener_ = client_->CreateListener(rrtype_, name_, this); | |
| 511 if (!listener_->Start()) return false; | |
| 512 | |
| 513 DCHECK(client_->core()); | |
| 514 if (!client_->core()->SendQuery(rrtype_, name_)) | |
| 515 return false; | |
| 516 | |
| 517 timeout_.Reset(base::Bind(&MDnsTransactionImpl::SignalTransactionOver, | |
| 518 weak_this)); | |
| 519 base::MessageLoop::current()->PostDelayedTask( | |
| 520 FROM_HERE, | |
| 521 timeout_.callback(), | |
| 522 base::TimeDelta::FromSeconds(MDnsTransactionTimeoutSeconds)); | |
| 523 | |
| 524 return listener_.get() != NULL; | |
| 525 } else { | |
| 526 // If this is a cache only query, signal that the transaction is over | |
| 527 // immediately. | |
| 528 SignalTransactionOver(); | |
| 529 } | |
| 530 | |
| 531 return true; | |
| 532 } | |
| 533 | |
| 534 const std::string& MDnsTransactionImpl::GetName() const { | |
| 535 return name_; | |
| 536 } | |
| 537 | |
| 538 uint16 MDnsTransactionImpl::GetType() const { | |
| 539 return rrtype_; | |
| 540 } | |
| 541 | |
| 542 void MDnsTransactionImpl::CacheRecordFound(const RecordParsed* record) { | |
| 543 DCHECK(started_); | |
| 544 OnRecordUpdate(MDnsListener::RECORD_ADDED, record); | |
| 545 } | |
| 546 | |
| 547 void MDnsTransactionImpl::TriggerCallback(MDnsTransaction::Result result, | |
| 548 const RecordParsed* record) { | |
| 549 DCHECK(started_); | |
| 550 if (!is_active()) return; | |
| 551 | |
| 552 // Ensure callback is run after touching all class state, so that | |
| 553 // the callback can delete the transaction. | |
| 554 MDnsTransaction::ResultCallback callback = callback_; | |
| 555 | |
| 556 if (flags_ & MDnsTransaction::SINGLE_RESULT) | |
| 557 Reset(); | |
| 558 | |
| 559 callback.Run(result, record); | |
| 560 } | |
| 561 | |
| 562 void MDnsTransactionImpl::Reset() { | |
| 563 callback_.Reset(); | |
| 564 listener_.reset(); | |
| 565 timeout_.Cancel(); | |
| 566 } | |
| 567 | |
| 568 void MDnsTransactionImpl::OnRecordUpdate(MDnsListener::UpdateType update, | |
| 569 const RecordParsed* record) { | |
| 570 DCHECK(started_); | |
| 571 if (update == MDnsListener::RECORD_ADDED || | |
| 572 update == MDnsListener::RECORD_CHANGED) | |
| 573 TriggerCallback(MDnsTransaction::RESULT_RECORD, record); | |
| 574 } | |
| 575 | |
| 576 void MDnsTransactionImpl::SignalTransactionOver() { | |
| 577 DCHECK(started_); | |
| 578 base::WeakPtr<MDnsTransactionImpl> weak_this = AsWeakPtr(); | |
| 579 | |
| 580 if (flags_ & MDnsTransaction::SINGLE_RESULT) { | |
| 581 TriggerCallback(MDnsTransaction::RESULT_NO_RESULTS, NULL); | |
| 582 } else { | |
| 583 TriggerCallback(MDnsTransaction::RESULT_DONE, NULL); | |
| 584 } | |
| 585 | |
| 586 if (weak_this) { | |
| 587 weak_this->Reset(); | |
| 588 } | |
| 589 } | |
| 590 | |
| 591 void MDnsTransactionImpl::OnNsecRecord(const std::string& name, unsigned type) { | |
| 592 // TODO(noamsml): NSEC records not yet implemented | |
| 593 } | |
| 594 | |
| 595 void MDnsTransactionImpl::OnCachePurged() { | |
| 596 // TODO(noamsml): Cache purge situations not yet implemented | |
| 597 } | |
| 598 | |
| 599 } // namespace net | |
| OLD | NEW |