[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / local_discovery / service_discovery_client_mac.h
blob4532e5e22684d53cca8e41d5a25f1eda77555133
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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_MAC_H_
6 #define CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_MAC_H_
8 #include <string>
10 #include "base/mac/scoped_nsobject.h"
11 #include "base/memory/weak_ptr.h"
12 #include "chrome/browser/local_discovery/service_discovery_shared_client.h"
13 #include "content/public/browser/browser_thread.h"
15 namespace base {
16 class Thread;
19 namespace local_discovery {
21 template <class T>
22 class ServiceDiscoveryThreadDeleter {
23 public:
24 inline void operator()(T* t) { t->DeleteSoon(); }
27 // Implementation of ServiceDiscoveryClient that uses the Bonjour SDK.
28 // https://developer.apple.com/library/mac/documentation/Networking/Conceptual/
29 // NSNetServiceProgGuide/Articles/BrowsingForServices.html
30 class ServiceDiscoveryClientMac : public ServiceDiscoverySharedClient {
31 public:
32 ServiceDiscoveryClientMac();
34 private:
35 virtual ~ServiceDiscoveryClientMac();
37 // ServiceDiscoveryClient implementation.
38 virtual scoped_ptr<ServiceWatcher> CreateServiceWatcher(
39 const std::string& service_type,
40 const ServiceWatcher::UpdatedCallback& callback) OVERRIDE;
41 virtual scoped_ptr<ServiceResolver> CreateServiceResolver(
42 const std::string& service_name,
43 const ServiceResolver::ResolveCompleteCallback& callback) OVERRIDE;
44 virtual scoped_ptr<LocalDomainResolver> CreateLocalDomainResolver(
45 const std::string& domain,
46 net::AddressFamily address_family,
47 const LocalDomainResolver::IPAddressCallback& callback) OVERRIDE;
49 void StartThreadIfNotStarted();
51 scoped_ptr<base::Thread> service_discovery_thread_;
53 DISALLOW_COPY_AND_ASSIGN(ServiceDiscoveryClientMac);
56 class ServiceWatcherImplMac : public ServiceWatcher {
57 public:
58 class NetServiceBrowserContainer {
59 public:
60 NetServiceBrowserContainer(
61 const std::string& service_type,
62 const ServiceWatcher::UpdatedCallback& callback,
63 scoped_refptr<base::MessageLoopProxy> service_discovery_runner);
64 ~NetServiceBrowserContainer();
66 void Start();
67 void DiscoverNewServices();
69 void OnServicesUpdate(ServiceWatcher::UpdateType update,
70 const std::string& service);
72 void DeleteSoon();
74 private:
75 void StartOnDiscoveryThread();
76 void DiscoverOnDiscoveryThread();
78 bool IsOnServiceDiscoveryThread() {
79 return base::MessageLoopProxy::current() ==
80 service_discovery_runner_.get();
83 std::string service_type_;
84 ServiceWatcher::UpdatedCallback callback_;
86 scoped_refptr<base::MessageLoopProxy> callback_runner_;
87 scoped_refptr<base::MessageLoopProxy> service_discovery_runner_;
89 base::scoped_nsobject<id> delegate_;
90 base::scoped_nsobject<NSNetServiceBrowser> browser_;
91 base::WeakPtrFactory<NetServiceBrowserContainer> weak_factory_;
94 ServiceWatcherImplMac(
95 const std::string& service_type,
96 const ServiceWatcher::UpdatedCallback& callback,
97 scoped_refptr<base::MessageLoopProxy> service_discovery_runner);
99 void OnServicesUpdate(ServiceWatcher::UpdateType update,
100 const std::string& service);
102 private:
103 virtual ~ServiceWatcherImplMac();
105 virtual void Start() OVERRIDE;
106 virtual void DiscoverNewServices(bool force_update) OVERRIDE;
107 virtual void SetActivelyRefreshServices(
108 bool actively_refresh_services) OVERRIDE;
109 virtual std::string GetServiceType() const OVERRIDE;
111 std::string service_type_;
112 ServiceWatcher::UpdatedCallback callback_;
113 bool started_;
115 scoped_ptr<NetServiceBrowserContainer,
116 ServiceDiscoveryThreadDeleter<NetServiceBrowserContainer> >
117 container_;
118 base::WeakPtrFactory<ServiceWatcherImplMac> weak_factory_;
120 DISALLOW_COPY_AND_ASSIGN(ServiceWatcherImplMac);
123 class ServiceResolverImplMac : public ServiceResolver {
124 public:
125 class NetServiceContainer {
126 public:
127 NetServiceContainer(
128 const std::string& service_name,
129 const ServiceResolver::ResolveCompleteCallback& callback,
130 scoped_refptr<base::MessageLoopProxy> service_discovery_runner);
132 virtual ~NetServiceContainer();
134 void StartResolving();
136 void OnResolveUpdate(RequestStatus);
138 void SetServiceForTesting(base::scoped_nsobject<NSNetService> service);
140 void DeleteSoon();
142 private:
143 void StartResolvingOnDiscoveryThread();
145 bool IsOnServiceDiscoveryThread() {
146 return base::MessageLoopProxy::current() ==
147 service_discovery_runner_.get();
150 const std::string service_name_;
151 ServiceResolver::ResolveCompleteCallback callback_;
153 scoped_refptr<base::MessageLoopProxy> callback_runner_;
154 scoped_refptr<base::MessageLoopProxy> service_discovery_runner_;
156 base::scoped_nsobject<id> delegate_;
157 base::scoped_nsobject<NSNetService> service_;
158 ServiceDescription service_description_;
159 base::WeakPtrFactory<NetServiceContainer> weak_factory_;
162 ServiceResolverImplMac(
163 const std::string& service_name,
164 const ServiceResolver::ResolveCompleteCallback& callback,
165 scoped_refptr<base::MessageLoopProxy> service_discovery_runner);
167 // Testing methods.
168 NetServiceContainer* GetContainerForTesting();
170 private:
171 virtual ~ServiceResolverImplMac();
173 virtual void StartResolving() OVERRIDE;
174 virtual std::string GetName() const OVERRIDE;
176 void OnResolveComplete(RequestStatus status,
177 const ServiceDescription& description);
179 const std::string service_name_;
180 ServiceResolver::ResolveCompleteCallback callback_;
181 bool has_resolved_;
183 scoped_ptr<NetServiceContainer,
184 ServiceDiscoveryThreadDeleter<NetServiceContainer> > container_;
185 base::WeakPtrFactory<ServiceResolverImplMac> weak_factory_;
187 DISALLOW_COPY_AND_ASSIGN(ServiceResolverImplMac);
190 } // namespace local_discovery
192 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_MAC_H_