service_discovery_host_client.h revision a3f7b4e666c476898878fa745f637129375cd889
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/threading/non_thread_safe.h"
12#include "chrome/common/local_discovery/service_discovery_client.h"
13#include "content/public/browser/utility_process_host_client.h"
14
15namespace base {
16class TaskRunner;
17}
18
19namespace content {
20class UtilityProcessHost;
21}
22
23namespace local_discovery {
24
25// Implementation of ServiceDiscoveryClient that delegates all functionality to
26// utility process.
27class ServiceDiscoveryHostClient : public base::NonThreadSafe,
28                                   public ServiceDiscoveryClient,
29                                   public content::UtilityProcessHostClient {
30 public:
31  ServiceDiscoveryHostClient();
32
33  // Starts utility process with ServiceDiscoveryClient.
34  void Start();
35
36  // Shutdowns utility process.
37  void Shutdown();
38
39  // ServiceDiscoveryClient implementation.
40  virtual scoped_ptr<ServiceWatcher> CreateServiceWatcher(
41      const std::string& service_type,
42      const ServiceWatcher::UpdatedCallback& callback) OVERRIDE;
43  virtual scoped_ptr<ServiceResolver> CreateServiceResolver(
44      const std::string& service_name,
45      const ServiceResolver::ResolveCompleteCallback& callback) OVERRIDE;
46  virtual scoped_ptr<LocalDomainResolver> CreateLocalDomainResolver(
47      const std::string& domain,
48      net::AddressFamily address_family,
49      const LocalDomainResolver::IPAddressCallback& callback) OVERRIDE;
50
51  // UtilityProcessHostClient implementation.
52  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
53
54 protected:
55  virtual ~ServiceDiscoveryHostClient();
56
57 private:
58  class ServiceWatcherProxy;
59  class ServiceResolverProxy;
60
61  typedef std::map<uint64, ServiceWatcher::UpdatedCallback> WatcherCallbacks;
62  typedef std::map<uint64, ServiceResolver::ResolveCompleteCallback>
63      ResolverCallbacks;
64
65  void StartOnIOThread();
66  void ShutdownOnIOThread();
67
68  void Send(IPC::Message* msg);
69  void SendOnIOThread(IPC::Message* msg);
70
71  uint64 RegisterWatcherCallback(
72      const ServiceWatcher::UpdatedCallback& callback);
73  uint64 RegisterResolverCallback(
74      const ServiceResolver::ResolveCompleteCallback& callback);
75  void UnregisterWatcherCallback(uint64 id);
76  void UnregisterResolverCallback(uint64 id);
77
78  // IPC Message handlers.
79  void OnWatcherCallback(uint64 id,
80                         ServiceWatcher::UpdateType update,
81                         const std::string& service_name);
82  void OnResolverCallback(uint64 id,
83                          ServiceResolver::RequestStatus status,
84                          const ServiceDescription& description);
85
86  // Runs watcher callback on owning thread.
87  void RunWatcherCallback(uint64 id,
88                          ServiceWatcher::UpdateType update,
89                          const std::string& service_name);
90  // Runs resolver callback on owning thread.
91  void RunResolverCallback(uint64 id,
92                           ServiceResolver::RequestStatus status,
93                           const ServiceDescription& description);
94
95  base::WeakPtr<content::UtilityProcessHost> utility_host_;
96
97  // Incrementing counter to assign ID to watchers and resolvers.
98  uint64 current_id_;
99  WatcherCallbacks service_watcher_callbacks_;
100  ResolverCallbacks service_resolver_callbacks_;
101  scoped_refptr<base::TaskRunner> callback_runner_;
102
103  DISALLOW_COPY_AND_ASSIGN(ServiceDiscoveryHostClient);
104};
105
106}  // namespace local_discovery
107
108#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_HOST_CLIENT_H_
109