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_UTILITY_LOCAL_DISCOVERY_SERVICE_DISCOVERY_MESSAGE_HANDLER_H_ 6 #define CHROME_UTILITY_LOCAL_DISCOVERY_SERVICE_DISCOVERY_MESSAGE_HANDLER_H_ 7 8 #include <map> 9 #include <string> 10 11 #include "base/memory/linked_ptr.h" 12 #include "chrome/common/local_discovery/service_discovery_client.h" 13 #include "chrome/utility/utility_message_handler.h" 14 15 struct LocalDiscoveryMsg_SocketInfo; 16 17 namespace net { 18 class MDnsClient; 19 } 20 21 namespace base { 22 struct FileDescriptor; 23 class TaskRunner; 24 class Thread; 25 } 26 27 namespace tracked_objects { 28 class Location; 29 } 30 31 namespace local_discovery { 32 33 class ServiceDiscoveryClient; 34 35 // Handles messages related to local discovery inside utility process. 36 class ServiceDiscoveryMessageHandler : public UtilityMessageHandler { 37 public: 38 ServiceDiscoveryMessageHandler(); 39 virtual ~ServiceDiscoveryMessageHandler(); 40 41 // UtilityMessageHandler implementation. 42 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 43 44 static void PreSandboxStartup(); 45 46 private: 47 typedef std::map<uint64, linked_ptr<ServiceWatcher> > ServiceWatchers; 48 typedef std::map<uint64, linked_ptr<ServiceResolver> > ServiceResolvers; 49 typedef std::map<uint64, linked_ptr<LocalDomainResolver> > 50 LocalDomainResolvers; 51 52 // Lazy initializes ServiceDiscoveryClient. 53 bool InitializeThread(); 54 void PostTask(const tracked_objects::Location& from_here, 55 const base::Closure& task); 56 57 // IPC message handlers. 58 #if defined(OS_POSIX) 59 void OnSetSockets(const std::vector<LocalDiscoveryMsg_SocketInfo>& sockets); 60 #endif // OS_POSIX 61 void OnStartWatcher(uint64 id, const std::string& service_type); 62 void OnDiscoverServices(uint64 id, bool force_update); 63 void OnSetActivelyRefreshServices(uint64 id, bool actively_refresh_services); 64 void OnDestroyWatcher(uint64 id); 65 void OnResolveService(uint64 id, const std::string& service_name); 66 void OnDestroyResolver(uint64 id); 67 void OnResolveLocalDomain(uint64 id, const std::string& domain, 68 net::AddressFamily address_family); 69 void OnDestroyLocalDomainResolver(uint64 id); 70 71 void InitializeMdns(); 72 void StartWatcher(uint64 id, const std::string& service_type); 73 void DiscoverServices(uint64 id, bool force_update); 74 void SetActivelyRefreshServices(uint64 id, bool actively_refresh_services); 75 void DestroyWatcher(uint64 id); 76 void ResolveService(uint64 id, const std::string& service_name); 77 void DestroyResolver(uint64 id); 78 void ResolveLocalDomain(uint64 id, const std::string& domain, 79 net::AddressFamily address_family); 80 void DestroyLocalDomainResolver(uint64 id); 81 82 void ShutdownLocalDiscovery(); 83 void ShutdownOnIOThread(); 84 85 // Is called by ServiceWatcher as callback. 86 void OnServiceUpdated(uint64 id, 87 ServiceWatcher::UpdateType update, 88 const std::string& name); 89 90 // Is called by ServiceResolver as callback. 91 void OnServiceResolved(uint64 id, 92 ServiceResolver::RequestStatus status, 93 const ServiceDescription& description); 94 95 // Is called by LocalDomainResolver as callback. 96 void OnLocalDomainResolved(uint64 id, 97 bool success, 98 const net::IPAddressNumber& address_ipv4, 99 const net::IPAddressNumber& address_ipv6); 100 101 void Send(IPC::Message* msg); 102 103 ServiceWatchers service_watchers_; 104 ServiceResolvers service_resolvers_; 105 LocalDomainResolvers local_domain_resolvers_; 106 107 scoped_ptr<net::MDnsClient> mdns_client_; 108 scoped_ptr<ServiceDiscoveryClient> service_discovery_client_; 109 110 scoped_refptr<base::TaskRunner> utility_task_runner_; 111 scoped_refptr<base::TaskRunner> discovery_task_runner_; 112 scoped_ptr<base::Thread> discovery_thread_; 113 }; 114 115 } // namespace local_discovery 116 117 #endif // CHROME_UTILITY_LOCAL_DISCOVERY_SERVICE_DISCOVERY_MESSAGE_HANDLER_H_ 118