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