Improved ServiceDiscoveryClientMdns restart logic to avoid crashes.
[chromium-blink-merge.git] / chrome / browser / local_discovery / service_discovery_client_mdns.h
blobef6f31fe238f7d868a7a6c7efb34d179fd328d27
1 // Copyright 2014 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.
5 #ifndef CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_MDNS_H_
6 #define CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_MDNS_H_
8 #include <set>
9 #include <string>
11 #include "base/cancelable_callback.h"
12 #include "base/observer_list.h"
13 #include "chrome/browser/local_discovery/service_discovery_shared_client.h"
14 #include "chrome/common/local_discovery/service_discovery_client.h"
15 #include "net/base/network_change_notifier.h"
16 #include "net/dns/mdns_client.h"
18 namespace local_discovery {
20 // Implementation of ServiceDiscoverySharedClient with front-end of UI thread
21 // and networking code on IO thread.
22 class ServiceDiscoveryClientMdns
23 : public ServiceDiscoverySharedClient,
24 public net::NetworkChangeNotifier::NetworkChangeObserver {
25 public:
26 class Proxy;
27 ServiceDiscoveryClientMdns();
29 // ServiceDiscoveryClient implementation.
30 virtual scoped_ptr<ServiceWatcher> CreateServiceWatcher(
31 const std::string& service_type,
32 const ServiceWatcher::UpdatedCallback& callback) OVERRIDE;
33 virtual scoped_ptr<ServiceResolver> CreateServiceResolver(
34 const std::string& service_name,
35 const ServiceResolver::ResolveCompleteCallback& callback) OVERRIDE;
36 virtual scoped_ptr<LocalDomainResolver> CreateLocalDomainResolver(
37 const std::string& domain,
38 net::AddressFamily address_family,
39 const LocalDomainResolver::IPAddressCallback& callback) OVERRIDE;
41 // net::NetworkChangeNotifier::NetworkChangeObserver implementation.
42 virtual void OnNetworkChanged(
43 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
45 private:
46 virtual ~ServiceDiscoveryClientMdns();
48 void ScheduleStartNewClient();
49 void StartNewClient();
50 void OnInterfaceListReady(const net::InterfaceIndexFamilyList& interfaces);
51 void OnMdnsInitialized(bool success);
52 void ReportSuccess();
53 void InvalidateWeakPtrs();
54 void OnBeforeMdnsDestroy();
55 void DestroyMdns();
57 bool PostToMdnsThread(const base::Closure& task);
59 ObserverList<Proxy, true> proxies_;
61 scoped_refptr<base::SequencedTaskRunner> mdns_runner_;
63 // Access only on |mdns_runner_| thread.
64 scoped_ptr<net::MDnsClient> mdns_;
66 // Access only on |mdns_runner_| thread.
67 scoped_ptr<ServiceDiscoveryClient> client_;
69 // Counter of restart attempts we have made after network change.
70 int restart_attempts_;
72 // If false delay tasks until initialization is posted to |mdns_runner_|
73 // thread.
74 bool need_dalay_mdns_tasks_;
76 // Delayed |mdns_runner_| tasks.
77 std::vector<base::Closure> delayed_tasks_;
79 base::WeakPtrFactory<ServiceDiscoveryClientMdns> weak_ptr_factory_;
81 DISALLOW_COPY_AND_ASSIGN(ServiceDiscoveryClientMdns);
84 } // namespace local_discovery
86 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_MDNS_H_