summaryrefslogtreecommitdiffstats
path: root/chrome/utility/local_discovery/service_discovery_message_handler.h
blob: ce0435d147c808c732b7fd82e44ce85ebbe52bf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_UTILITY_LOCAL_DISCOVERY_SERVICE_DISCOVERY_MESSAGE_HANDLER_H_
#define CHROME_UTILITY_LOCAL_DISCOVERY_SERVICE_DISCOVERY_MESSAGE_HANDLER_H_

#include <stdint.h>

#include <map>
#include <string>

#include "base/memory/linked_ptr.h"
#include "build/build_config.h"
#include "chrome/common/local_discovery/service_discovery_client.h"
#include "chrome/utility/utility_message_handler.h"

struct LocalDiscoveryMsg_SocketInfo;

namespace net {
class MDnsClient;
}

namespace base {
struct FileDescriptor;
class TaskRunner;
class Thread;
}

namespace tracked_objects {
class Location;
}

namespace local_discovery {

class ServiceDiscoveryClient;

// Handles messages related to local discovery inside utility process.
class ServiceDiscoveryMessageHandler : public UtilityMessageHandler {
 public:
  ServiceDiscoveryMessageHandler();
  ~ServiceDiscoveryMessageHandler() override;

  // UtilityMessageHandler implementation.
  bool OnMessageReceived(const IPC::Message& message) override;

  static void PreSandboxStartup();

 private:
  typedef std::map<uint64_t, linked_ptr<ServiceWatcher>> ServiceWatchers;
  typedef std::map<uint64_t, linked_ptr<ServiceResolver>> ServiceResolvers;
  typedef std::map<uint64_t, linked_ptr<LocalDomainResolver>>
      LocalDomainResolvers;

  // Lazy initializes ServiceDiscoveryClient.
  bool InitializeThread();
  void PostTask(const tracked_objects::Location& from_here,
                const base::Closure& task);

  // IPC message handlers.
#if defined(OS_POSIX)
  void OnSetSockets(const std::vector<LocalDiscoveryMsg_SocketInfo>& sockets);
#endif  // OS_POSIX
  void OnStartWatcher(uint64_t id, const std::string& service_type);
  void OnDiscoverServices(uint64_t id, bool force_update);
  void OnSetActivelyRefreshServices(uint64_t id,
                                    bool actively_refresh_services);
  void OnDestroyWatcher(uint64_t id);
  void OnResolveService(uint64_t id, const std::string& service_name);
  void OnDestroyResolver(uint64_t id);
  void OnResolveLocalDomain(uint64_t id,
                            const std::string& domain,
                            net::AddressFamily address_family);
  void OnDestroyLocalDomainResolver(uint64_t id);

  void InitializeMdns();
  void StartWatcher(uint64_t id, const std::string& service_type);
  void DiscoverServices(uint64_t id, bool force_update);
  void SetActivelyRefreshServices(uint64_t id, bool actively_refresh_services);
  void DestroyWatcher(uint64_t id);
  void ResolveService(uint64_t id, const std::string& service_name);
  void DestroyResolver(uint64_t id);
  void ResolveLocalDomain(uint64_t id,
                          const std::string& domain,
                          net::AddressFamily address_family);
  void DestroyLocalDomainResolver(uint64_t id);

  void ShutdownLocalDiscovery();
  void ShutdownOnIOThread();

  // Is called by ServiceWatcher as callback.
  void OnServiceUpdated(uint64_t id,
                        ServiceWatcher::UpdateType update,
                        const std::string& name);

  // Is called by ServiceResolver as callback.
  void OnServiceResolved(uint64_t id,
                         ServiceResolver::RequestStatus status,
                         const ServiceDescription& description);

  // Is called by LocalDomainResolver as callback.
  void OnLocalDomainResolved(uint64_t id,
                             bool success,
                             const net::IPAddressNumber& address_ipv4,
                             const net::IPAddressNumber& address_ipv6);

  void Send(IPC::Message* msg);

  ServiceWatchers service_watchers_;
  ServiceResolvers service_resolvers_;
  LocalDomainResolvers local_domain_resolvers_;

  scoped_ptr<net::MDnsClient> mdns_client_;
  scoped_ptr<ServiceDiscoveryClient> service_discovery_client_;

  scoped_refptr<base::TaskRunner> utility_task_runner_;
  scoped_refptr<base::TaskRunner> discovery_task_runner_;
  scoped_ptr<base::Thread> discovery_thread_;
};

}  // namespace local_discovery

#endif  // CHROME_UTILITY_LOCAL_DISCOVERY_SERVICE_DISCOVERY_MESSAGE_HANDLER_H_