service_discovery_client_mac.h revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
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.
4
5#ifndef CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_MAC_H_
6#define CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_MAC_H_
7
8#include <string>
9
10#include "base/mac/scoped_nsobject.h"
11#include "chrome/browser/local_discovery/service_discovery_shared_client.h"
12
13namespace local_discovery {
14
15// Implementation of ServiceDiscoveryClient that uses the Bonjour SDK.
16// https://developer.apple.com/library/mac/documentation/Networking/Conceptual/
17// NSNetServiceProgGuide/Articles/BrowsingForServices.html
18class ServiceDiscoveryClientMac : public ServiceDiscoverySharedClient {
19 public:
20  ServiceDiscoveryClientMac();
21
22 private:
23  virtual ~ServiceDiscoveryClientMac();
24
25  // ServiceDiscoveryClient implementation.
26  virtual scoped_ptr<ServiceWatcher> CreateServiceWatcher(
27      const std::string& service_type,
28      const ServiceWatcher::UpdatedCallback& callback) OVERRIDE;
29  virtual scoped_ptr<ServiceResolver> CreateServiceResolver(
30      const std::string& service_name,
31      const ServiceResolver::ResolveCompleteCallback& callback) OVERRIDE;
32  virtual scoped_ptr<LocalDomainResolver> CreateLocalDomainResolver(
33      const std::string& domain,
34      net::AddressFamily address_family,
35      const LocalDomainResolver::IPAddressCallback& callback) OVERRIDE;
36
37  DISALLOW_COPY_AND_ASSIGN(ServiceDiscoveryClientMac);
38};
39
40class ServiceWatcherImplMac : public ServiceWatcher {
41 public:
42  ServiceWatcherImplMac(const std::string& service_type,
43                        const ServiceWatcher::UpdatedCallback& callback);
44
45  void OnServicesUpdate(ServiceWatcher::UpdateType update,
46                        const std::string& service);
47
48 private:
49  virtual ~ServiceWatcherImplMac();
50
51  virtual void Start() OVERRIDE;
52  virtual void DiscoverNewServices(bool force_update) OVERRIDE;
53  virtual std::string GetServiceType() const OVERRIDE;
54
55  std::string service_type_;
56
57  ServiceWatcher::UpdatedCallback callback_;
58  bool started_;
59  base::scoped_nsobject<id> delegate_;
60  base::scoped_nsobject<NSNetServiceBrowser> browser_;
61
62  DISALLOW_COPY_AND_ASSIGN(ServiceWatcherImplMac);
63};
64
65class ServiceResolverImplMac : public ServiceResolver {
66 public:
67  ServiceResolverImplMac(
68      const std::string& service_name,
69      const ServiceResolver::ResolveCompleteCallback& callback);
70
71  void OnResolveUpdate(RequestStatus);
72
73  // Testing methods.
74  void SetServiceForTesting(
75      base::scoped_nsobject<NSNetService> service);
76
77 private:
78  virtual ~ServiceResolverImplMac();
79
80  virtual void StartResolving() OVERRIDE;
81  virtual std::string GetName() const OVERRIDE;
82
83  const std::string service_name_;
84  ServiceResolver::ResolveCompleteCallback callback_;
85  bool has_resolved_;
86  base::scoped_nsobject<id> delegate_;
87  base::scoped_nsobject<NSNetService> service_;
88  ServiceDescription service_description_;
89
90  DISALLOW_COPY_AND_ASSIGN(ServiceResolverImplMac);
91};
92
93}  // namespace local_discovery
94
95#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_MAC_H_
96