service_discovery_host_client.h revision 3551c9c881056c480085172ff9840cab31610854
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_HOST_CLIENT_H_
6#define CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_HOST_CLIENT_H_
7
8#include <map>
9#include <string>
10
11#include "base/memory/singleton.h"
12#include "base/threading/non_thread_safe.h"
13#include "chrome/common/local_discovery/service_discovery_client.h"
14#include "content/public/browser/utility_process_host_client.h"
15
16namespace base {
17class TaskRunner;
18}
19
20namespace content {
21class UtilityProcessHost;
22}
23
24namespace local_discovery {
25
26// Implementation of ServiceDiscoveryClient that delegates all functionality to
27// utility process.
28class ServiceDiscoveryHostClient : public base::NonThreadSafe,
29                                   public ServiceDiscoveryClient,
30                                   public content::UtilityProcessHostClient {
31 public:
32  ServiceDiscoveryHostClient();
33
34  // Starts utility process with ServiceDiscoveryClient.
35  void Start();
36
37  // Shutdowns utility process.
38  void Shutdown();
39
40  // ServiceDiscoveryClient implementation.
41  virtual scoped_ptr<ServiceWatcher> CreateServiceWatcher(
42      const std::string& service_type,
43      const ServiceWatcher::UpdatedCallback& callback) OVERRIDE;
44  virtual scoped_ptr<ServiceResolver> CreateServiceResolver(
45      const std::string& service_name,
46      const ServiceResolver::ResolveCompleteCallback& callback) OVERRIDE;
47  virtual scoped_ptr<LocalDomainResolver> CreateLocalDomainResolver(
48      const std::string& domain,
49      net::AddressFamily address_family,
50      const LocalDomainResolver::IPAddressCallback& callback) OVERRIDE;
51
52  // UtilityProcessHostClient implementation.
53  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
54
55 protected:
56  virtual ~ServiceDiscoveryHostClient();
57
58 private:
59  class ServiceWatcherProxy;
60  class ServiceResolverProxy;
61  class LocalDomainResolverProxy;
62
63  typedef std::map<uint64, ServiceWatcher::UpdatedCallback> WatcherCallbacks;
64  typedef std::map<uint64, ServiceResolver::ResolveCompleteCallback>
65      ResolverCallbacks;
66  typedef std::map<uint64, LocalDomainResolver::IPAddressCallback>
67      DomainResolverCallbacks;
68
69  void StartOnIOThread();
70  void ShutdownOnIOThread();
71
72  void Send(IPC::Message* msg);
73  void SendOnIOThread(IPC::Message* msg);
74
75  uint64 RegisterWatcherCallback(
76      const ServiceWatcher::UpdatedCallback& callback);
77  uint64 RegisterResolverCallback(
78      const ServiceResolver::ResolveCompleteCallback& callback);
79  uint64 RegisterLocalDomainResolverCallback(
80      const LocalDomainResolver::IPAddressCallback& callback);
81
82  void UnregisterWatcherCallback(uint64 id);
83  void UnregisterResolverCallback(uint64 id);
84  void UnregisterLocalDomainResolverCallback(uint64 id);
85
86  // IPC Message handlers.
87  void OnWatcherCallback(uint64 id,
88                         ServiceWatcher::UpdateType update,
89                         const std::string& service_name);
90  void OnResolverCallback(uint64 id,
91                          ServiceResolver::RequestStatus status,
92                          const ServiceDescription& description);
93  void OnLocalDomainResolverCallback(uint64 id,
94                                     bool success,
95                                     const net::IPAddressNumber& address);
96
97
98  // Runs watcher callback on owning thread.
99  void RunWatcherCallback(uint64 id,
100                          ServiceWatcher::UpdateType update,
101                          const std::string& service_name);
102  // Runs resolver callback on owning thread.
103  void RunResolverCallback(uint64 id,
104                           ServiceResolver::RequestStatus status,
105                           const ServiceDescription& description);
106  // Runs local domain resolver callback on owning thread.
107  void RunLocalDomainResolverCallback(uint64 id,
108                                      bool success,
109                                      const net::IPAddressNumber& address);
110
111
112  base::WeakPtr<content::UtilityProcessHost> utility_host_;
113
114  // Incrementing counter to assign ID to watchers and resolvers.
115  uint64 current_id_;
116  WatcherCallbacks service_watcher_callbacks_;
117  ResolverCallbacks service_resolver_callbacks_;
118  DomainResolverCallbacks domain_resolver_callbacks_;
119  scoped_refptr<base::TaskRunner> callback_runner_;
120
121  DISALLOW_COPY_AND_ASSIGN(ServiceDiscoveryHostClient);
122};
123
124class ServiceDiscoveryHostClientFactory : public base::NonThreadSafe {
125 public:
126  ServiceDiscoveryHostClientFactory();
127  ~ServiceDiscoveryHostClientFactory();
128
129  static ServiceDiscoveryHostClient* GetClient();
130  static void ReleaseClient();
131
132 private:
133  friend class Singleton<ServiceDiscoveryHostClientFactory>;
134
135  static ServiceDiscoveryHostClientFactory* GetInstance();
136
137  ServiceDiscoveryHostClient* GetClientInternal();
138  void ReleaseClientInternal();
139
140  scoped_refptr<ServiceDiscoveryHostClient> instance_;
141  int references_;
142};
143
144}  // namespace local_discovery
145
146#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_HOST_CLIENT_H_
147