summaryrefslogtreecommitdiffstats
path: root/chrome/browser/local_discovery/service_discovery_client_mac.h
blob: ab14864db521ea2c6254a270ef91eee27021c98a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_MAC_H_
#define CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_MAC_H_

#import <Foundation/Foundation.h>
#include <string>

#include "base/mac/scoped_nsobject.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/single_thread_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "chrome/browser/local_discovery/service_discovery_shared_client.h"
#include "content/public/browser/browser_thread.h"

namespace base {
class Thread;
}

namespace local_discovery {

template <class T>
class ServiceDiscoveryThreadDeleter {
 public:
  inline void operator()(T* t) { t->DeleteSoon(); }
};

// Implementation of ServiceDiscoveryClient that uses the Bonjour SDK.
// https://developer.apple.com/library/mac/documentation/Networking/Conceptual/
// NSNetServiceProgGuide/Articles/BrowsingForServices.html
class ServiceDiscoveryClientMac : public ServiceDiscoverySharedClient {
 public:
  ServiceDiscoveryClientMac();

 private:
  ~ServiceDiscoveryClientMac() override;

  // ServiceDiscoveryClient implementation.
  scoped_ptr<ServiceWatcher> CreateServiceWatcher(
      const std::string& service_type,
      const ServiceWatcher::UpdatedCallback& callback) override;
  scoped_ptr<ServiceResolver> CreateServiceResolver(
      const std::string& service_name,
      const ServiceResolver::ResolveCompleteCallback& callback) override;
  scoped_ptr<LocalDomainResolver> CreateLocalDomainResolver(
      const std::string& domain,
      net::AddressFamily address_family,
      const LocalDomainResolver::IPAddressCallback& callback) override;

  void StartThreadIfNotStarted();

  scoped_ptr<base::Thread> service_discovery_thread_;

  DISALLOW_COPY_AND_ASSIGN(ServiceDiscoveryClientMac);
};

class ServiceWatcherImplMac : public ServiceWatcher {
 public:
  class NetServiceBrowserContainer {
   public:
    NetServiceBrowserContainer(
        const std::string& service_type,
        const ServiceWatcher::UpdatedCallback& callback,
        scoped_refptr<base::SingleThreadTaskRunner> service_discovery_runner);
    ~NetServiceBrowserContainer();

    void Start();
    void DiscoverNewServices();

    void OnServicesUpdate(ServiceWatcher::UpdateType update,
                          const std::string& service);

    void DeleteSoon();

   private:
    void StartOnDiscoveryThread();
    void DiscoverOnDiscoveryThread();

    bool IsOnServiceDiscoveryThread() {
      return base::ThreadTaskRunnerHandle::Get() ==
             service_discovery_runner_.get();
    }

    std::string service_type_;
    ServiceWatcher::UpdatedCallback callback_;

    scoped_refptr<base::SingleThreadTaskRunner> callback_runner_;
    scoped_refptr<base::SingleThreadTaskRunner> service_discovery_runner_;

    base::scoped_nsobject<id> delegate_;
    base::scoped_nsobject<NSNetServiceBrowser> browser_;
    base::WeakPtrFactory<NetServiceBrowserContainer> weak_factory_;
  };

  ServiceWatcherImplMac(
      const std::string& service_type,
      const ServiceWatcher::UpdatedCallback& callback,
      scoped_refptr<base::SingleThreadTaskRunner> service_discovery_runner);

  void OnServicesUpdate(ServiceWatcher::UpdateType update,
                        const std::string& service);

 private:
  ~ServiceWatcherImplMac() override;

  void Start() override;
  void DiscoverNewServices(bool force_update) override;
  void SetActivelyRefreshServices(bool actively_refresh_services) override;
  std::string GetServiceType() const override;

  std::string service_type_;
  ServiceWatcher::UpdatedCallback callback_;
  bool started_;

  scoped_ptr<NetServiceBrowserContainer,
             ServiceDiscoveryThreadDeleter<NetServiceBrowserContainer> >
      container_;
  base::WeakPtrFactory<ServiceWatcherImplMac> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(ServiceWatcherImplMac);
};

class ServiceResolverImplMac : public ServiceResolver {
 public:
  class NetServiceContainer {
   public:
    NetServiceContainer(
        const std::string& service_name,
        const ServiceResolver::ResolveCompleteCallback& callback,
        scoped_refptr<base::SingleThreadTaskRunner> service_discovery_runner);

    virtual ~NetServiceContainer();

    void StartResolving();

    void OnResolveUpdate(RequestStatus);

    void SetServiceForTesting(base::scoped_nsobject<NSNetService> service);

    void DeleteSoon();

   private:
    void StartResolvingOnDiscoveryThread();

    bool IsOnServiceDiscoveryThread() {
      return base::ThreadTaskRunnerHandle::Get() ==
             service_discovery_runner_.get();
    }

    const std::string service_name_;
    ServiceResolver::ResolveCompleteCallback callback_;

    scoped_refptr<base::SingleThreadTaskRunner> callback_runner_;
    scoped_refptr<base::SingleThreadTaskRunner> service_discovery_runner_;

    base::scoped_nsobject<id> delegate_;
    base::scoped_nsobject<NSNetService> service_;
    ServiceDescription service_description_;
    base::WeakPtrFactory<NetServiceContainer> weak_factory_;
  };

  ServiceResolverImplMac(
      const std::string& service_name,
      const ServiceResolver::ResolveCompleteCallback& callback,
      scoped_refptr<base::SingleThreadTaskRunner> service_discovery_runner);

  // Testing methods.
  NetServiceContainer* GetContainerForTesting();

 private:
  ~ServiceResolverImplMac() override;

  void StartResolving() override;
  std::string GetName() const override;

  void OnResolveComplete(RequestStatus status,
                         const ServiceDescription& description);

  const std::string service_name_;
  ServiceResolver::ResolveCompleteCallback callback_;
  bool has_resolved_;

  scoped_ptr<NetServiceContainer,
             ServiceDiscoveryThreadDeleter<NetServiceContainer> > container_;
  base::WeakPtrFactory<ServiceResolverImplMac> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(ServiceResolverImplMac);
};

}  // namespace local_discovery

#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_MAC_H_