service_discovery_host_client.h revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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/cancelable_callback.h" 12#include "base/memory/singleton.h" 13#include "base/threading/non_thread_safe.h" 14#include "chrome/common/local_discovery/service_discovery_client.h" 15#include "content/public/browser/utility_process_host_client.h" 16#include "net/base/network_change_notifier.h" 17 18namespace base { 19class TaskRunner; 20} 21 22namespace content { 23class UtilityProcessHost; 24} 25 26namespace local_discovery { 27 28// Implementation of ServiceDiscoveryClient that delegates all functionality to 29// utility process. 30class ServiceDiscoveryHostClient 31 : public base::NonThreadSafe, 32 public ServiceDiscoveryClient, 33 public content::UtilityProcessHostClient { 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 protected: 59 virtual ~ServiceDiscoveryHostClient(); 60 61 private: 62 class ServiceWatcherProxy; 63 class ServiceResolverProxy; 64 class LocalDomainResolverProxy; 65 friend class ServiceDiscoverySharedClient; 66 67 typedef std::map<uint64, ServiceWatcher::UpdatedCallback> WatcherCallbacks; 68 typedef std::map<uint64, ServiceResolver::ResolveCompleteCallback> 69 ResolverCallbacks; 70 typedef std::map<uint64, LocalDomainResolver::IPAddressCallback> 71 DomainResolverCallbacks; 72 73 void StartOnIOThread(); 74 void ShutdownOnIOThread(); 75 76 void InvalidateWatchers(); 77 78 void Send(IPC::Message* msg); 79 void SendOnIOThread(IPC::Message* msg); 80 81 uint64 RegisterWatcherCallback( 82 const ServiceWatcher::UpdatedCallback& callback); 83 uint64 RegisterResolverCallback( 84 const ServiceResolver::ResolveCompleteCallback& callback); 85 uint64 RegisterLocalDomainResolverCallback( 86 const LocalDomainResolver::IPAddressCallback& callback); 87 88 void UnregisterWatcherCallback(uint64 id); 89 void UnregisterResolverCallback(uint64 id); 90 void UnregisterLocalDomainResolverCallback(uint64 id); 91 92 // IPC Message handlers. 93 void OnWatcherCallback(uint64 id, 94 ServiceWatcher::UpdateType update, 95 const std::string& service_name); 96 void OnResolverCallback(uint64 id, 97 ServiceResolver::RequestStatus status, 98 const ServiceDescription& description); 99 void OnLocalDomainResolverCallback(uint64 id, 100 bool success, 101 const net::IPAddressNumber& address_ipv4, 102 const net::IPAddressNumber& address_ipv6); 103 104 105 // Runs watcher callback on owning thread. 106 void RunWatcherCallback(uint64 id, 107 ServiceWatcher::UpdateType update, 108 const std::string& service_name); 109 // Runs resolver callback on owning thread. 110 void RunResolverCallback(uint64 id, 111 ServiceResolver::RequestStatus status, 112 const ServiceDescription& description); 113 // Runs local domain resolver callback on owning thread. 114 void RunLocalDomainResolverCallback(uint64 id, 115 bool success, 116 const net::IPAddressNumber& address_ipv4, 117 const net::IPAddressNumber& address_ipv6); 118 119 120 base::WeakPtr<content::UtilityProcessHost> utility_host_; 121 122 // Incrementing counter to assign ID to watchers and resolvers. 123 uint64 current_id_; 124 WatcherCallbacks service_watcher_callbacks_; 125 ResolverCallbacks service_resolver_callbacks_; 126 DomainResolverCallbacks domain_resolver_callbacks_; 127 scoped_refptr<base::TaskRunner> callback_runner_; 128 scoped_refptr<base::TaskRunner> io_runner_; 129 130 DISALLOW_COPY_AND_ASSIGN(ServiceDiscoveryHostClient); 131}; 132 133class ServiceDiscoverySharedClient 134 : public base::RefCounted<ServiceDiscoverySharedClient>, 135 public base::NonThreadSafe, 136 public ServiceDiscoveryClient, 137 public net::NetworkChangeNotifier::NetworkChangeObserver { 138 public: 139 static scoped_refptr<ServiceDiscoverySharedClient> GetInstance(); 140 141 // ServiceDiscoveryClient implementation. 142 virtual scoped_ptr<ServiceWatcher> CreateServiceWatcher( 143 const std::string& service_type, 144 const ServiceWatcher::UpdatedCallback& callback) OVERRIDE; 145 virtual scoped_ptr<ServiceResolver> CreateServiceResolver( 146 const std::string& service_name, 147 const ServiceResolver::ResolveCompleteCallback& callback) OVERRIDE; 148 virtual scoped_ptr<LocalDomainResolver> CreateLocalDomainResolver( 149 const std::string& domain, 150 net::AddressFamily address_family, 151 const LocalDomainResolver::IPAddressCallback& callback) OVERRIDE; 152 153 // net::NetworkChangeNotifier::NetworkChangeObserver implementation. 154 virtual void OnNetworkChanged( 155 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; 156 157 private: 158 friend class base::RefCounted<ServiceDiscoverySharedClient>; 159 ServiceDiscoverySharedClient(); 160 virtual ~ServiceDiscoverySharedClient(); 161 162 void StartNewClient(); 163 164 base::CancelableClosure network_change_callback_; 165 166 scoped_refptr<ServiceDiscoveryHostClient> host_client_; 167 168 DISALLOW_COPY_AND_ASSIGN(ServiceDiscoverySharedClient); 169}; 170 171} // namespace local_discovery 172 173#endif // CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_HOST_CLIENT_H_ 174