- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / utility / local_discovery / service_discovery_message_handler.h
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 namespace net {
16 class MDnsClient;
17 }
18
19 namespace base {
20 struct FileDescriptor;
21 class TaskRunner;
22 class Thread;
23 }
24
25 namespace tracked_objects {
26 class Location;
27 }
28
29 namespace local_discovery {
30
31 class ServiceDiscoveryClient;
32
33 // Handles messages related to local discovery inside utility process.
34 class ServiceDiscoveryMessageHandler : public chrome::UtilityMessageHandler {
35  public:
36   ServiceDiscoveryMessageHandler();
37   virtual ~ServiceDiscoveryMessageHandler();
38
39   // UtilityMessageHandler implementation.
40   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
41
42   static void PreSandboxStartup();
43
44  private:
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> >
48       LocalDomainResolvers;
49
50   // Lazy initializes ServiceDiscoveryClient.
51   bool InitializeThread();
52   void PostTask(const tracked_objects::Location& from_here,
53                 const base::Closure& task);
54
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);
66
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);
76
77   void ShutdownLocalDiscovery();
78   void ShutdownOnIOThread();
79
80   // Is called by ServiceWatcher as callback.
81   void OnServiceUpdated(uint64 id,
82                         ServiceWatcher::UpdateType update,
83                         const std::string& name);
84
85   // Is called by ServiceResolver as callback.
86   void OnServiceResolved(uint64 id,
87                          ServiceResolver::RequestStatus status,
88                          const ServiceDescription& description);
89
90   // Is called by LocalDomainResolver as callback.
91   void OnLocalDomainResolved(uint64 id,
92                              bool success,
93                              const net::IPAddressNumber& address_ipv4,
94                              const net::IPAddressNumber& address_ipv6);
95
96   void Send(IPC::Message* msg);
97
98   ServiceWatchers service_watchers_;
99   ServiceResolvers service_resolvers_;
100   LocalDomainResolvers local_domain_resolvers_;
101
102   scoped_ptr<net::MDnsClient> mdns_client_;
103   scoped_ptr<ServiceDiscoveryClient> service_discovery_client_;
104
105   scoped_refptr<base::TaskRunner> utility_task_runner_;
106   scoped_refptr<base::TaskRunner> discovery_task_runner_;
107   scoped_ptr<base::Thread> discovery_thread_;
108 };
109
110 }  // namespace local_discovery
111
112 #endif  // CHROME_UTILITY_LOCAL_DISCOVERY_SERVICE_DISCOVERY_MESSAGE_HANDLER_H_