Changes for start_with_ext Telemetry test to work on Windows.
[chromium-blink-merge.git] / chrome / browser / local_discovery / service_discovery_client_mac.h
blob573d63099571e61d49603e44892289423e4ef4a1
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 #import <Foundation/Foundation.h>
9 #include <string>
11 #include "base/mac/scoped_nsobject.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "chrome/browser/local_discovery/service_discovery_shared_client.h"
16 #include "content/public/browser/browser_thread.h"
18 namespace base {
19 class Thread;
22 namespace local_discovery {
24 template <class T>
25 class ServiceDiscoveryThreadDeleter {
26 public:
27 inline void operator()(T* t) { t->DeleteSoon(); }
30 // Implementation of ServiceDiscoveryClient that uses the Bonjour SDK.
31 // https://developer.apple.com/library/mac/documentation/Networking/Conceptual/
32 // NSNetServiceProgGuide/Articles/BrowsingForServices.html
33 class ServiceDiscoveryClientMac : public ServiceDiscoverySharedClient {
34 public:
35 ServiceDiscoveryClientMac();
37 private:
38 ~ServiceDiscoveryClientMac() override;
40 // ServiceDiscoveryClient implementation.
41 scoped_ptr<ServiceWatcher> CreateServiceWatcher(
42 const std::string& service_type,
43 const ServiceWatcher::UpdatedCallback& callback) override;
44 scoped_ptr<ServiceResolver> CreateServiceResolver(
45 const std::string& service_name,
46 const ServiceResolver::ResolveCompleteCallback& callback) override;
47 scoped_ptr<LocalDomainResolver> CreateLocalDomainResolver(
48 const std::string& domain,
49 net::AddressFamily address_family,
50 const LocalDomainResolver::IPAddressCallback& callback) override;
52 void StartThreadIfNotStarted();
54 scoped_ptr<base::Thread> service_discovery_thread_;
56 DISALLOW_COPY_AND_ASSIGN(ServiceDiscoveryClientMac);
59 class ServiceWatcherImplMac : public ServiceWatcher {
60 public:
61 class NetServiceBrowserContainer {
62 public:
63 NetServiceBrowserContainer(
64 const std::string& service_type,
65 const ServiceWatcher::UpdatedCallback& callback,
66 scoped_refptr<base::SingleThreadTaskRunner> service_discovery_runner);
67 ~NetServiceBrowserContainer();
69 void Start();
70 void DiscoverNewServices();
72 void OnServicesUpdate(ServiceWatcher::UpdateType update,
73 const std::string& service);
75 void DeleteSoon();
77 private:
78 void StartOnDiscoveryThread();
79 void DiscoverOnDiscoveryThread();
81 bool IsOnServiceDiscoveryThread() {
82 return base::ThreadTaskRunnerHandle::Get() ==
83 service_discovery_runner_.get();
86 std::string service_type_;
87 ServiceWatcher::UpdatedCallback callback_;
89 scoped_refptr<base::SingleThreadTaskRunner> callback_runner_;
90 scoped_refptr<base::SingleThreadTaskRunner> service_discovery_runner_;
92 base::scoped_nsobject<id> delegate_;
93 base::scoped_nsobject<NSNetServiceBrowser> browser_;
94 base::WeakPtrFactory<NetServiceBrowserContainer> weak_factory_;
97 ServiceWatcherImplMac(
98 const std::string& service_type,
99 const ServiceWatcher::UpdatedCallback& callback,
100 scoped_refptr<base::SingleThreadTaskRunner> service_discovery_runner);
102 void OnServicesUpdate(ServiceWatcher::UpdateType update,
103 const std::string& service);
105 private:
106 ~ServiceWatcherImplMac() override;
108 void Start() override;
109 void DiscoverNewServices(bool force_update) override;
110 void SetActivelyRefreshServices(bool actively_refresh_services) override;
111 std::string GetServiceType() const override;
113 std::string service_type_;
114 ServiceWatcher::UpdatedCallback callback_;
115 bool started_;
117 scoped_ptr<NetServiceBrowserContainer,
118 ServiceDiscoveryThreadDeleter<NetServiceBrowserContainer> >
119 container_;
120 base::WeakPtrFactory<ServiceWatcherImplMac> weak_factory_;
122 DISALLOW_COPY_AND_ASSIGN(ServiceWatcherImplMac);
125 class ServiceResolverImplMac : public ServiceResolver {
126 public:
127 class NetServiceContainer {
128 public:
129 NetServiceContainer(
130 const std::string& service_name,
131 const ServiceResolver::ResolveCompleteCallback& callback,
132 scoped_refptr<base::SingleThreadTaskRunner> service_discovery_runner);
134 virtual ~NetServiceContainer();
136 void StartResolving();
138 void OnResolveUpdate(RequestStatus);
140 void SetServiceForTesting(base::scoped_nsobject<NSNetService> service);
142 void DeleteSoon();
144 private:
145 void StartResolvingOnDiscoveryThread();
147 bool IsOnServiceDiscoveryThread() {
148 return base::ThreadTaskRunnerHandle::Get() ==
149 service_discovery_runner_.get();
152 const std::string service_name_;
153 ServiceResolver::ResolveCompleteCallback callback_;
155 scoped_refptr<base::SingleThreadTaskRunner> callback_runner_;
156 scoped_refptr<base::SingleThreadTaskRunner> service_discovery_runner_;
158 base::scoped_nsobject<id> delegate_;
159 base::scoped_nsobject<NSNetService> service_;
160 ServiceDescription service_description_;
161 base::WeakPtrFactory<NetServiceContainer> weak_factory_;
164 ServiceResolverImplMac(
165 const std::string& service_name,
166 const ServiceResolver::ResolveCompleteCallback& callback,
167 scoped_refptr<base::SingleThreadTaskRunner> service_discovery_runner);
169 // Testing methods.
170 NetServiceContainer* GetContainerForTesting();
172 private:
173 ~ServiceResolverImplMac() override;
175 void StartResolving() override;
176 std::string GetName() const override;
178 void OnResolveComplete(RequestStatus status,
179 const ServiceDescription& description);
181 const std::string service_name_;
182 ServiceResolver::ResolveCompleteCallback callback_;
183 bool has_resolved_;
185 scoped_ptr<NetServiceContainer,
186 ServiceDiscoveryThreadDeleter<NetServiceContainer> > container_;
187 base::WeakPtrFactory<ServiceResolverImplMac> weak_factory_;
189 DISALLOW_COPY_AND_ASSIGN(ServiceResolverImplMac);
192 } // namespace local_discovery
194 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_MAC_H_