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_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_HOST_CLIENT_H_
6 #define CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_HOST_CLIENT_H_
12 #include "base/memory/scoped_vector.h"
13 #include "chrome/common/local_discovery/service_discovery_client.h"
14 #include "content/public/browser/utility_process_host_client.h"
16 struct LocalDiscoveryMsg_SocketInfo
;
23 class UtilityProcessHost
;
26 namespace local_discovery
{
29 typedef std::vector
<LocalDiscoveryMsg_SocketInfo
> SocketInfoList
;
32 // Implementation of ServiceDiscoveryClient that delegates all functionality to
34 class ServiceDiscoveryHostClient
35 : public ServiceDiscoveryClient
,
36 public content::UtilityProcessHostClient
{
38 ServiceDiscoveryHostClient();
40 // Starts utility process with ServiceDiscoveryClient.
41 void Start(const base::Closure
& error_callback
);
43 // Shutdowns utility process.
46 // ServiceDiscoveryClient implementation.
47 virtual scoped_ptr
<ServiceWatcher
> CreateServiceWatcher(
48 const std::string
& service_type
,
49 const ServiceWatcher::UpdatedCallback
& callback
) OVERRIDE
;
50 virtual scoped_ptr
<ServiceResolver
> CreateServiceResolver(
51 const std::string
& service_name
,
52 const ServiceResolver::ResolveCompleteCallback
& callback
) OVERRIDE
;
53 virtual scoped_ptr
<LocalDomainResolver
> CreateLocalDomainResolver(
54 const std::string
& domain
,
55 net::AddressFamily address_family
,
56 const LocalDomainResolver::IPAddressCallback
& callback
) OVERRIDE
;
58 // UtilityProcessHostClient implementation.
59 virtual void OnProcessCrashed(int exit_code
) OVERRIDE
;
60 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
63 virtual ~ServiceDiscoveryHostClient();
66 class ServiceWatcherProxy
;
67 class ServiceResolverProxy
;
68 class LocalDomainResolverProxy
;
69 friend class ServiceDiscoveryClientMdns
;
71 typedef std::map
<uint64
, ServiceWatcher::UpdatedCallback
> WatcherCallbacks
;
72 typedef std::map
<uint64
, ServiceResolver::ResolveCompleteCallback
>
74 typedef std::map
<uint64
, LocalDomainResolver::IPAddressCallback
>
75 DomainResolverCallbacks
;
77 void StartOnIOThread();
78 void ShutdownOnIOThread();
81 void OnSocketsReady(const SocketInfoList
& interfaces
);
84 void InvalidateWatchers();
86 void Send(IPC::Message
* msg
);
87 void SendOnIOThread(IPC::Message
* msg
);
89 uint64
RegisterWatcherCallback(
90 const ServiceWatcher::UpdatedCallback
& callback
);
91 uint64
RegisterResolverCallback(
92 const ServiceResolver::ResolveCompleteCallback
& callback
);
93 uint64
RegisterLocalDomainResolverCallback(
94 const LocalDomainResolver::IPAddressCallback
& callback
);
96 void UnregisterWatcherCallback(uint64 id
);
97 void UnregisterResolverCallback(uint64 id
);
98 void UnregisterLocalDomainResolverCallback(uint64 id
);
100 // IPC Message handlers.
102 void OnWatcherCallback(uint64 id
,
103 ServiceWatcher::UpdateType update
,
104 const std::string
& service_name
);
105 void OnResolverCallback(uint64 id
,
106 ServiceResolver::RequestStatus status
,
107 const ServiceDescription
& description
);
108 void OnLocalDomainResolverCallback(uint64 id
,
110 const net::IPAddressNumber
& address_ipv4
,
111 const net::IPAddressNumber
& address_ipv6
);
114 // Runs watcher callback on owning thread.
115 void RunWatcherCallback(uint64 id
,
116 ServiceWatcher::UpdateType update
,
117 const std::string
& service_name
);
118 // Runs resolver callback on owning thread.
119 void RunResolverCallback(uint64 id
,
120 ServiceResolver::RequestStatus status
,
121 const ServiceDescription
& description
);
122 // Runs local domain resolver callback on owning thread.
123 void RunLocalDomainResolverCallback(uint64 id
,
125 const net::IPAddressNumber
& address_ipv4
,
126 const net::IPAddressNumber
& address_ipv6
);
129 base::WeakPtr
<content::UtilityProcessHost
> utility_host_
;
131 // Incrementing counter to assign ID to watchers and resolvers.
133 base::Closure error_callback_
;
134 WatcherCallbacks service_watcher_callbacks_
;
135 ResolverCallbacks service_resolver_callbacks_
;
136 DomainResolverCallbacks domain_resolver_callbacks_
;
137 scoped_refptr
<base::TaskRunner
> callback_runner_
;
138 scoped_refptr
<base::TaskRunner
> io_runner_
;
139 ScopedVector
<IPC::Message
> delayed_messages_
;
141 DISALLOW_COPY_AND_ASSIGN(ServiceDiscoveryHostClient
);
144 } // namespace local_discovery
146 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_HOST_CLIENT_H_