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.
5 #ifndef CHROME_UTILITY_LOCAL_DISCOVERY_SERVICE_DISCOVERY_MESSAGE_HANDLER_H_
6 #define CHROME_UTILITY_LOCAL_DISCOVERY_SERVICE_DISCOVERY_MESSAGE_HANDLER_H_
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"
20 struct FileDescriptor;
25 namespace tracked_objects {
29 namespace local_discovery {
31 class ServiceDiscoveryClient;
33 // Handles messages related to local discovery inside utility process.
34 class ServiceDiscoveryMessageHandler : public chrome::UtilityMessageHandler {
36 ServiceDiscoveryMessageHandler();
37 virtual ~ServiceDiscoveryMessageHandler();
39 // UtilityMessageHandler implementation.
40 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
42 static void PreSandboxStartup();
45 typedef std::map<uint64, linked_ptr<ServiceWatcher> > ServiceWatchers;
46 typedef std::map<uint64, linked_ptr<ServiceResolver> > ServiceResolvers;
47 typedef std::map<uint64, linked_ptr<LocalDomainResolver> >
50 // Lazy initializes ServiceDiscoveryClient.
51 bool InitializeThread();
52 void PostTask(const tracked_objects::Location& from_here,
53 const base::Closure& task);
55 // IPC message handlers.
56 void OnSetSockets(const base::FileDescriptor& socket_v4,
57 const base::FileDescriptor& socket_v6);
58 void OnStartWatcher(uint64 id, const std::string& service_type);
59 void OnDiscoverServices(uint64 id, bool force_update);
60 void OnDestroyWatcher(uint64 id);
61 void OnResolveService(uint64 id, const std::string& service_name);
62 void OnDestroyResolver(uint64 id);
63 void OnResolveLocalDomain(uint64 id, const std::string& domain,
64 net::AddressFamily address_family);
65 void OnDestroyLocalDomainResolver(uint64 id);
67 void InitializeMdns();
68 void StartWatcher(uint64 id, const std::string& service_type);
69 void DiscoverServices(uint64 id, bool force_update);
70 void DestroyWatcher(uint64 id);
71 void ResolveService(uint64 id, const std::string& service_name);
72 void DestroyResolver(uint64 id);
73 void ResolveLocalDomain(uint64 id, const std::string& domain,
74 net::AddressFamily address_family);
75 void DestroyLocalDomainResolver(uint64 id);
77 void ShutdownLocalDiscovery();
78 void ShutdownOnIOThread();
80 // Is called by ServiceWatcher as callback.
81 void OnServiceUpdated(uint64 id,
82 ServiceWatcher::UpdateType update,
83 const std::string& name);
85 // Is called by ServiceResolver as callback.
86 void OnServiceResolved(uint64 id,
87 ServiceResolver::RequestStatus status,
88 const ServiceDescription& description);
90 // Is called by LocalDomainResolver as callback.
91 void OnLocalDomainResolved(uint64 id,
93 const net::IPAddressNumber& address_ipv4,
94 const net::IPAddressNumber& address_ipv6);
96 void Send(IPC::Message* msg);
98 ServiceWatchers service_watchers_;
99 ServiceResolvers service_resolvers_;
100 LocalDomainResolvers local_domain_resolvers_;
102 scoped_ptr<net::MDnsClient> mdns_client_;
103 scoped_ptr<ServiceDiscoveryClient> service_discovery_client_;
105 scoped_refptr<base::TaskRunner> utility_task_runner_;
106 scoped_refptr<base::TaskRunner> discovery_task_runner_;
107 scoped_ptr<base::Thread> discovery_thread_;
110 } // namespace local_discovery
112 #endif // CHROME_UTILITY_LOCAL_DISCOVERY_SERVICE_DISCOVERY_MESSAGE_HANDLER_H_