Roll src/third_party/WebKit 8b42d1d:744641d (svn 186770:186771)
[chromium-blink-merge.git] / chrome / browser / local_discovery / service_discovery_client_mac.h
blob17f19cf58eeec48ef9483985aa034760ace17667
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 ~ServiceDiscoveryClientMac() override;
37 // ServiceDiscoveryClient implementation.
38 scoped_ptr<ServiceWatcher> CreateServiceWatcher(
39 const std::string& service_type,
40 const ServiceWatcher::UpdatedCallback& callback) override;
41 scoped_ptr<ServiceResolver> CreateServiceResolver(
42 const std::string& service_name,
43 const ServiceResolver::ResolveCompleteCallback& callback) override;
44 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 ~ServiceWatcherImplMac() override;
105 void Start() override;
106 void DiscoverNewServices(bool force_update) override;
107 void SetActivelyRefreshServices(bool actively_refresh_services) override;
108 std::string GetServiceType() const override;
110 std::string service_type_;
111 ServiceWatcher::UpdatedCallback callback_;
112 bool started_;
114 scoped_ptr<NetServiceBrowserContainer,
115 ServiceDiscoveryThreadDeleter<NetServiceBrowserContainer> >
116 container_;
117 base::WeakPtrFactory<ServiceWatcherImplMac> weak_factory_;
119 DISALLOW_COPY_AND_ASSIGN(ServiceWatcherImplMac);
122 class ServiceResolverImplMac : public ServiceResolver {
123 public:
124 class NetServiceContainer {
125 public:
126 NetServiceContainer(
127 const std::string& service_name,
128 const ServiceResolver::ResolveCompleteCallback& callback,
129 scoped_refptr<base::MessageLoopProxy> service_discovery_runner);
131 virtual ~NetServiceContainer();
133 void StartResolving();
135 void OnResolveUpdate(RequestStatus);
137 void SetServiceForTesting(base::scoped_nsobject<NSNetService> service);
139 void DeleteSoon();
141 private:
142 void StartResolvingOnDiscoveryThread();
144 bool IsOnServiceDiscoveryThread() {
145 return base::MessageLoopProxy::current() ==
146 service_discovery_runner_.get();
149 const std::string service_name_;
150 ServiceResolver::ResolveCompleteCallback callback_;
152 scoped_refptr<base::MessageLoopProxy> callback_runner_;
153 scoped_refptr<base::MessageLoopProxy> service_discovery_runner_;
155 base::scoped_nsobject<id> delegate_;
156 base::scoped_nsobject<NSNetService> service_;
157 ServiceDescription service_description_;
158 base::WeakPtrFactory<NetServiceContainer> weak_factory_;
161 ServiceResolverImplMac(
162 const std::string& service_name,
163 const ServiceResolver::ResolveCompleteCallback& callback,
164 scoped_refptr<base::MessageLoopProxy> service_discovery_runner);
166 // Testing methods.
167 NetServiceContainer* GetContainerForTesting();
169 private:
170 ~ServiceResolverImplMac() override;
172 void StartResolving() override;
173 std::string GetName() const override;
175 void OnResolveComplete(RequestStatus status,
176 const ServiceDescription& description);
178 const std::string service_name_;
179 ServiceResolver::ResolveCompleteCallback callback_;
180 bool has_resolved_;
182 scoped_ptr<NetServiceContainer,
183 ServiceDiscoveryThreadDeleter<NetServiceContainer> > container_;
184 base::WeakPtrFactory<ServiceResolverImplMac> weak_factory_;
186 DISALLOW_COPY_AND_ASSIGN(ServiceResolverImplMac);
189 } // namespace local_discovery
191 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_MAC_H_