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