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