File: service_discovery_host_client.h

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (148 lines) | stat: -rw-r--r-- 5,228 bytes parent folder | download
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// 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_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_HOST_CLIENT_H_
#define CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_HOST_CLIENT_H_

#include <map>
#include <string>
#include <vector>

#include "base/memory/ref_counted.h"
#include "base/memory/scoped_vector.h"
#include "base/memory/weak_ptr.h"
#include "chrome/common/local_discovery/service_discovery_client.h"
#include "content/public/browser/utility_process_host_client.h"

struct LocalDiscoveryMsg_SocketInfo;

namespace base {
class TaskRunner;
}

namespace content {
class UtilityProcessHost;
}

namespace local_discovery {

#if defined(OS_POSIX)
typedef std::vector<LocalDiscoveryMsg_SocketInfo> SocketInfoList;
#endif  // OS_POSIX

// Implementation of ServiceDiscoveryClient that delegates all functionality to
// utility process.
class ServiceDiscoveryHostClient
    : public ServiceDiscoveryClient,
      public content::UtilityProcessHostClient {
 public:
  ServiceDiscoveryHostClient();

  // Starts utility process with ServiceDiscoveryClient.
  void Start(const base::Closure& error_callback);

  // Shutdowns utility process.
  void Shutdown();

  // ServiceDiscoveryClient implementation.
  scoped_ptr<ServiceWatcher> CreateServiceWatcher(
      const std::string& service_type,
      const ServiceWatcher::UpdatedCallback& callback) override;
  scoped_ptr<ServiceResolver> CreateServiceResolver(
      const std::string& service_name,
      const ServiceResolver::ResolveCompleteCallback& callback) override;
  scoped_ptr<LocalDomainResolver> CreateLocalDomainResolver(
      const std::string& domain,
      net::AddressFamily address_family,
      const LocalDomainResolver::IPAddressCallback& callback) override;

  // UtilityProcessHostClient implementation.
  void OnProcessCrashed(int exit_code) override;
  bool OnMessageReceived(const IPC::Message& message) override;

 protected:
  ~ServiceDiscoveryHostClient() override;

 private:
  class ServiceWatcherProxy;
  class ServiceResolverProxy;
  class LocalDomainResolverProxy;
  friend class ServiceDiscoveryClientUtility;

  typedef std::map<uint64, ServiceWatcher::UpdatedCallback> WatcherCallbacks;
  typedef std::map<uint64, ServiceResolver::ResolveCompleteCallback>
      ResolverCallbacks;
  typedef std::map<uint64, LocalDomainResolver::IPAddressCallback>
      DomainResolverCallbacks;

  void StartOnIOThread();
  void ShutdownOnIOThread();

#if defined(OS_POSIX)
  void OnSocketsReady(const SocketInfoList& interfaces);
#endif  // OS_POSIX

  void InvalidateWatchers();

  void Send(IPC::Message* msg);
  void SendOnIOThread(IPC::Message* msg);

  uint64 RegisterWatcherCallback(
      const ServiceWatcher::UpdatedCallback& callback);
  uint64 RegisterResolverCallback(
      const ServiceResolver::ResolveCompleteCallback& callback);
  uint64 RegisterLocalDomainResolverCallback(
      const LocalDomainResolver::IPAddressCallback& callback);

  void UnregisterWatcherCallback(uint64 id);
  void UnregisterResolverCallback(uint64 id);
  void UnregisterLocalDomainResolverCallback(uint64 id);

  // IPC Message handlers.
  void OnError();
  void OnWatcherCallback(uint64 id,
                         ServiceWatcher::UpdateType update,
                         const std::string& service_name);
  void OnResolverCallback(uint64 id,
                          ServiceResolver::RequestStatus status,
                          const ServiceDescription& description);
  void OnLocalDomainResolverCallback(uint64 id,
                                     bool success,
                                     const net::IPAddressNumber& address_ipv4,
                                     const net::IPAddressNumber& address_ipv6);


  // Runs watcher callback on owning thread.
  void RunWatcherCallback(uint64 id,
                          ServiceWatcher::UpdateType update,
                          const std::string& service_name);
  // Runs resolver callback on owning thread.
  void RunResolverCallback(uint64 id,
                           ServiceResolver::RequestStatus status,
                           const ServiceDescription& description);
  // Runs local domain resolver callback on owning thread.
  void RunLocalDomainResolverCallback(uint64 id,
                                      bool success,
                                      const net::IPAddressNumber& address_ipv4,
                                      const net::IPAddressNumber& address_ipv6);


  base::WeakPtr<content::UtilityProcessHost> utility_host_;

  // Incrementing counter to assign ID to watchers and resolvers.
  uint64 current_id_;
  base::Closure error_callback_;
  WatcherCallbacks service_watcher_callbacks_;
  ResolverCallbacks service_resolver_callbacks_;
  DomainResolverCallbacks domain_resolver_callbacks_;
  scoped_refptr<base::TaskRunner> callback_runner_;
  scoped_refptr<base::TaskRunner> io_runner_;
  ScopedVector<IPC::Message> delayed_messages_;

  DISALLOW_COPY_AND_ASSIGN(ServiceDiscoveryHostClient);
};

}  // namespace local_discovery

#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_HOST_CLIENT_H_