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