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 #include "chrome/utility/local_discovery/service_discovery_message_handler.h"
9 #include "base/lazy_instance.h"
10 #include "chrome/common/local_discovery/local_discovery_messages.h"
11 #include "chrome/common/local_discovery/service_discovery_client_impl.h"
12 #include "content/public/utility/utility_thread.h"
13 #include "net/socket/socket_descriptor.h"
14 #include "net/udp/datagram_server_socket.h"
16 namespace local_discovery
{
20 void ClosePlatformSocket(net::SocketDescriptor socket
);
22 // Sets socket factory used by |net::CreatePlatformSocket|. Implemetation
23 // keeps single socket that will be returned to the first call to
24 // |net::CreatePlatformSocket| during object lifetime.
25 class ScopedSocketFactory
: public net::PlatformSocketFactory
{
27 explicit ScopedSocketFactory(net::SocketDescriptor socket
) : socket_(socket
) {
28 net::PlatformSocketFactory::SetInstance(this);
31 virtual ~ScopedSocketFactory() {
32 net::PlatformSocketFactory::SetInstance(NULL
);
33 ClosePlatformSocket(socket_
);
34 socket_
= net::kInvalidSocket
;
37 virtual net::SocketDescriptor
CreateSocket(int family
, int type
,
38 int protocol
) OVERRIDE
{
39 DCHECK_EQ(type
, SOCK_DGRAM
);
40 DCHECK(family
== AF_INET
|| family
== AF_INET6
);
41 net::SocketDescriptor result
= net::kInvalidSocket
;
42 std::swap(result
, socket_
);
47 net::SocketDescriptor socket_
;
48 DISALLOW_COPY_AND_ASSIGN(ScopedSocketFactory
);
52 SocketInfo(net::SocketDescriptor socket
,
53 net::AddressFamily address_family
,
54 uint32 interface_index
)
56 address_family(address_family
),
57 interface_index(interface_index
) {
59 net::SocketDescriptor socket
;
60 net::AddressFamily address_family
;
61 uint32 interface_index
;
64 // Returns list of sockets preallocated before.
65 class PreCreatedMDnsSocketFactory
: public net::MDnsSocketFactory
{
67 PreCreatedMDnsSocketFactory() {}
68 virtual ~PreCreatedMDnsSocketFactory() {
69 // Not empty if process exits too fast, before starting mDns code. If
70 // happened, destructors may crash accessing destroyed global objects.
71 sockets_
.weak_clear();
74 // net::MDnsSocketFactory implementation:
75 virtual void CreateSockets(
76 ScopedVector
<net::DatagramServerSocket
>* sockets
) OVERRIDE
{
77 sockets
->swap(sockets_
);
81 void AddSocket(const SocketInfo
& socket_info
) {
82 // Takes ownership of socket_info.socket;
83 ScopedSocketFactory
platform_factory(socket_info
.socket
);
84 scoped_ptr
<net::DatagramServerSocket
> socket(
85 net::CreateAndBindMDnsSocket(socket_info
.address_family
,
86 socket_info
.interface_index
));
88 socket
->DetachFromThread();
89 sockets_
.push_back(socket
.release());
98 ScopedVector
<net::DatagramServerSocket
> sockets_
;
100 DISALLOW_COPY_AND_ASSIGN(PreCreatedMDnsSocketFactory
);
103 base::LazyInstance
<PreCreatedMDnsSocketFactory
>
104 g_local_discovery_socket_factory
= LAZY_INSTANCE_INITIALIZER
;
108 void ClosePlatformSocket(net::SocketDescriptor socket
) {
109 ::closesocket(socket
);
112 void StaticInitializeSocketFactory() {
113 net::InterfaceIndexFamilyList
interfaces(net::GetMDnsInterfacesToBind());
114 for (size_t i
= 0; i
< interfaces
.size(); ++i
) {
115 DCHECK(interfaces
[i
].second
== net::ADDRESS_FAMILY_IPV4
||
116 interfaces
[i
].second
== net::ADDRESS_FAMILY_IPV6
);
117 net::SocketDescriptor descriptor
=
118 net::CreatePlatformSocket(
119 net::ConvertAddressFamily(interfaces
[i
].second
), SOCK_DGRAM
,
121 g_local_discovery_socket_factory
.Get().AddSocket(
122 SocketInfo(descriptor
, interfaces
[i
].second
, interfaces
[i
].first
));
128 void ClosePlatformSocket(net::SocketDescriptor socket
) {
132 void StaticInitializeSocketFactory() {
137 void SendHostMessageOnUtilityThread(IPC::Message
* msg
) {
138 content::UtilityThread::Get()->Send(msg
);
141 std::string
WatcherUpdateToString(ServiceWatcher::UpdateType update
) {
143 case ServiceWatcher::UPDATE_ADDED
:
144 return "UPDATE_ADDED";
145 case ServiceWatcher::UPDATE_CHANGED
:
146 return "UPDATE_CHANGED";
147 case ServiceWatcher::UPDATE_REMOVED
:
148 return "UPDATE_REMOVED";
149 case ServiceWatcher::UPDATE_INVALIDATED
:
150 return "UPDATE_INVALIDATED";
152 return "Unknown Update";
155 std::string
ResolverStatusToString(ServiceResolver::RequestStatus status
) {
157 case ServiceResolver::STATUS_SUCCESS
:
158 return "STATUS_SUCESS";
159 case ServiceResolver::STATUS_REQUEST_TIMEOUT
:
160 return "STATUS_REQUEST_TIMEOUT";
161 case ServiceResolver::STATUS_KNOWN_NONEXISTENT
:
162 return "STATUS_KNOWN_NONEXISTENT";
164 return "Unknown Status";
169 ServiceDiscoveryMessageHandler::ServiceDiscoveryMessageHandler() {
172 ServiceDiscoveryMessageHandler::~ServiceDiscoveryMessageHandler() {
173 DCHECK(!discovery_thread_
);
176 void ServiceDiscoveryMessageHandler::PreSandboxStartup() {
177 StaticInitializeSocketFactory();
180 void ServiceDiscoveryMessageHandler::InitializeMdns() {
181 if (service_discovery_client_
|| mdns_client_
)
184 mdns_client_
= net::MDnsClient::CreateDefault();
186 mdns_client_
->StartListening(g_local_discovery_socket_factory
.Pointer());
187 // Close unused sockets.
188 g_local_discovery_socket_factory
.Get().Reset();
190 VLOG(1) << "Failed to start MDnsClient";
191 Send(new LocalDiscoveryHostMsg_Error());
195 service_discovery_client_
.reset(
196 new local_discovery::ServiceDiscoveryClientImpl(mdns_client_
.get()));
199 bool ServiceDiscoveryMessageHandler::InitializeThread() {
200 if (discovery_task_runner_
)
202 if (discovery_thread_
)
204 utility_task_runner_
= base::MessageLoop::current()->message_loop_proxy();
205 discovery_thread_
.reset(new base::Thread("ServiceDiscoveryThread"));
206 base::Thread::Options
thread_options(base::MessageLoop::TYPE_IO
, 0);
207 if (discovery_thread_
->StartWithOptions(thread_options
)) {
208 discovery_task_runner_
= discovery_thread_
->message_loop_proxy();
209 discovery_task_runner_
->PostTask(FROM_HERE
,
210 base::Bind(&ServiceDiscoveryMessageHandler::InitializeMdns
,
211 base::Unretained(this)));
213 return discovery_task_runner_
!= NULL
;
216 bool ServiceDiscoveryMessageHandler::OnMessageReceived(
217 const IPC::Message
& message
) {
219 IPC_BEGIN_MESSAGE_MAP(ServiceDiscoveryMessageHandler
, message
)
220 #if defined(OS_POSIX)
221 IPC_MESSAGE_HANDLER(LocalDiscoveryMsg_SetSockets
, OnSetSockets
)
223 IPC_MESSAGE_HANDLER(LocalDiscoveryMsg_StartWatcher
, OnStartWatcher
)
224 IPC_MESSAGE_HANDLER(LocalDiscoveryMsg_DiscoverServices
, OnDiscoverServices
)
225 IPC_MESSAGE_HANDLER(LocalDiscoveryMsg_SetActivelyRefreshServices
,
226 OnSetActivelyRefreshServices
)
227 IPC_MESSAGE_HANDLER(LocalDiscoveryMsg_DestroyWatcher
, OnDestroyWatcher
)
228 IPC_MESSAGE_HANDLER(LocalDiscoveryMsg_ResolveService
, OnResolveService
)
229 IPC_MESSAGE_HANDLER(LocalDiscoveryMsg_DestroyResolver
, OnDestroyResolver
)
230 IPC_MESSAGE_HANDLER(LocalDiscoveryMsg_ResolveLocalDomain
,
231 OnResolveLocalDomain
)
232 IPC_MESSAGE_HANDLER(LocalDiscoveryMsg_DestroyLocalDomainResolver
,
233 OnDestroyLocalDomainResolver
)
234 IPC_MESSAGE_HANDLER(LocalDiscoveryMsg_ShutdownLocalDiscovery
,
235 ShutdownLocalDiscovery
)
236 IPC_MESSAGE_UNHANDLED(handled
= false)
237 IPC_END_MESSAGE_MAP()
241 void ServiceDiscoveryMessageHandler::PostTask(
242 const tracked_objects::Location
& from_here
,
243 const base::Closure
& task
) {
244 if (!InitializeThread())
246 discovery_task_runner_
->PostTask(from_here
, task
);
249 #if defined(OS_POSIX)
250 void ServiceDiscoveryMessageHandler::OnSetSockets(
251 const std::vector
<LocalDiscoveryMsg_SocketInfo
>& sockets
) {
252 for (size_t i
= 0; i
< sockets
.size(); ++i
) {
253 g_local_discovery_socket_factory
.Get().AddSocket(
254 SocketInfo(sockets
[i
].descriptor
.fd
, sockets
[i
].address_family
,
255 sockets
[i
].interface_index
));
260 void ServiceDiscoveryMessageHandler::OnStartWatcher(
262 const std::string
& service_type
) {
264 base::Bind(&ServiceDiscoveryMessageHandler::StartWatcher
,
265 base::Unretained(this), id
, service_type
));
268 void ServiceDiscoveryMessageHandler::OnDiscoverServices(uint64 id
,
271 base::Bind(&ServiceDiscoveryMessageHandler::DiscoverServices
,
272 base::Unretained(this), id
, force_update
));
275 void ServiceDiscoveryMessageHandler::OnSetActivelyRefreshServices(
276 uint64 id
, bool actively_refresh_services
) {
279 &ServiceDiscoveryMessageHandler::SetActivelyRefreshServices
,
280 base::Unretained(this), id
, actively_refresh_services
));
283 void ServiceDiscoveryMessageHandler::OnDestroyWatcher(uint64 id
) {
285 base::Bind(&ServiceDiscoveryMessageHandler::DestroyWatcher
,
286 base::Unretained(this), id
));
289 void ServiceDiscoveryMessageHandler::OnResolveService(
291 const std::string
& service_name
) {
293 base::Bind(&ServiceDiscoveryMessageHandler::ResolveService
,
294 base::Unretained(this), id
, service_name
));
297 void ServiceDiscoveryMessageHandler::OnDestroyResolver(uint64 id
) {
299 base::Bind(&ServiceDiscoveryMessageHandler::DestroyResolver
,
300 base::Unretained(this), id
));
303 void ServiceDiscoveryMessageHandler::OnResolveLocalDomain(
304 uint64 id
, const std::string
& domain
,
305 net::AddressFamily address_family
) {
307 base::Bind(&ServiceDiscoveryMessageHandler::ResolveLocalDomain
,
308 base::Unretained(this), id
, domain
, address_family
));
311 void ServiceDiscoveryMessageHandler::OnDestroyLocalDomainResolver(uint64 id
) {
314 &ServiceDiscoveryMessageHandler::DestroyLocalDomainResolver
,
315 base::Unretained(this), id
));
318 void ServiceDiscoveryMessageHandler::StartWatcher(
320 const std::string
& service_type
) {
321 VLOG(1) << "StartWatcher, id=" << id
<< ", type=" << service_type
;
322 if (!service_discovery_client_
)
324 DCHECK(!ContainsKey(service_watchers_
, id
));
325 scoped_ptr
<ServiceWatcher
> watcher(
326 service_discovery_client_
->CreateServiceWatcher(
328 base::Bind(&ServiceDiscoveryMessageHandler::OnServiceUpdated
,
329 base::Unretained(this), id
)));
331 service_watchers_
[id
].reset(watcher
.release());
334 void ServiceDiscoveryMessageHandler::DiscoverServices(uint64 id
,
336 VLOG(1) << "DiscoverServices, id=" << id
;
337 if (!service_discovery_client_
)
339 DCHECK(ContainsKey(service_watchers_
, id
));
340 service_watchers_
[id
]->DiscoverNewServices(force_update
);
343 void ServiceDiscoveryMessageHandler::SetActivelyRefreshServices(
345 bool actively_refresh_services
) {
346 VLOG(1) << "ActivelyRefreshServices, id=" << id
;
347 if (!service_discovery_client_
)
349 DCHECK(ContainsKey(service_watchers_
, id
));
350 service_watchers_
[id
]->SetActivelyRefreshServices(actively_refresh_services
);
353 void ServiceDiscoveryMessageHandler::DestroyWatcher(uint64 id
) {
354 VLOG(1) << "DestoryWatcher, id=" << id
;
355 if (!service_discovery_client_
)
357 service_watchers_
.erase(id
);
360 void ServiceDiscoveryMessageHandler::ResolveService(
362 const std::string
& service_name
) {
363 VLOG(1) << "ResolveService, id=" << id
<< ", name=" << service_name
;
364 if (!service_discovery_client_
)
366 DCHECK(!ContainsKey(service_resolvers_
, id
));
367 scoped_ptr
<ServiceResolver
> resolver(
368 service_discovery_client_
->CreateServiceResolver(
370 base::Bind(&ServiceDiscoveryMessageHandler::OnServiceResolved
,
371 base::Unretained(this), id
)));
372 resolver
->StartResolving();
373 service_resolvers_
[id
].reset(resolver
.release());
376 void ServiceDiscoveryMessageHandler::DestroyResolver(uint64 id
) {
377 VLOG(1) << "DestroyResolver, id=" << id
;
378 if (!service_discovery_client_
)
380 service_resolvers_
.erase(id
);
383 void ServiceDiscoveryMessageHandler::ResolveLocalDomain(
385 const std::string
& domain
,
386 net::AddressFamily address_family
) {
387 VLOG(1) << "ResolveLocalDomain, id=" << id
<< ", domain=" << domain
;
388 if (!service_discovery_client_
)
390 DCHECK(!ContainsKey(local_domain_resolvers_
, id
));
391 scoped_ptr
<LocalDomainResolver
> resolver(
392 service_discovery_client_
->CreateLocalDomainResolver(
393 domain
, address_family
,
394 base::Bind(&ServiceDiscoveryMessageHandler::OnLocalDomainResolved
,
395 base::Unretained(this), id
)));
397 local_domain_resolvers_
[id
].reset(resolver
.release());
400 void ServiceDiscoveryMessageHandler::DestroyLocalDomainResolver(uint64 id
) {
401 VLOG(1) << "DestroyLocalDomainResolver, id=" << id
;
402 if (!service_discovery_client_
)
404 local_domain_resolvers_
.erase(id
);
407 void ServiceDiscoveryMessageHandler::ShutdownLocalDiscovery() {
408 if (!discovery_task_runner_
)
411 discovery_task_runner_
->PostTask(
413 base::Bind(&ServiceDiscoveryMessageHandler::ShutdownOnIOThread
,
414 base::Unretained(this)));
416 // This will wait for message loop to drain, so ShutdownOnIOThread will
417 // definitely be called.
418 discovery_thread_
.reset();
421 void ServiceDiscoveryMessageHandler::ShutdownOnIOThread() {
422 VLOG(1) << "ShutdownLocalDiscovery";
423 service_watchers_
.clear();
424 service_resolvers_
.clear();
425 local_domain_resolvers_
.clear();
426 service_discovery_client_
.reset();
427 mdns_client_
.reset();
430 void ServiceDiscoveryMessageHandler::OnServiceUpdated(
432 ServiceWatcher::UpdateType update
,
433 const std::string
& name
) {
434 VLOG(1) << "OnServiceUpdated, id=" << id
435 << ", status=" << WatcherUpdateToString(update
) << ", name=" << name
;
436 DCHECK(service_discovery_client_
);
438 Send(new LocalDiscoveryHostMsg_WatcherCallback(id
, update
, name
));
441 void ServiceDiscoveryMessageHandler::OnServiceResolved(
443 ServiceResolver::RequestStatus status
,
444 const ServiceDescription
& description
) {
445 VLOG(1) << "OnServiceResolved, id=" << id
446 << ", status=" << ResolverStatusToString(status
)
447 << ", name=" << description
.service_name
;
449 DCHECK(service_discovery_client_
);
450 Send(new LocalDiscoveryHostMsg_ResolverCallback(id
, status
, description
));
453 void ServiceDiscoveryMessageHandler::OnLocalDomainResolved(
456 const net::IPAddressNumber
& address_ipv4
,
457 const net::IPAddressNumber
& address_ipv6
) {
458 VLOG(1) << "OnLocalDomainResolved, id=" << id
459 << ", IPv4=" << (address_ipv4
.empty() ? "" :
460 net::IPAddressToString(address_ipv4
))
461 << ", IPv6=" << (address_ipv6
.empty() ? "" :
462 net::IPAddressToString(address_ipv6
));
464 DCHECK(service_discovery_client_
);
465 Send(new LocalDiscoveryHostMsg_LocalDomainResolverCallback(
466 id
, success
, address_ipv4
, address_ipv6
));
469 void ServiceDiscoveryMessageHandler::Send(IPC::Message
* msg
) {
470 utility_task_runner_
->PostTask(FROM_HERE
,
471 base::Bind(&SendHostMessageOnUtilityThread
,
475 } // namespace local_discovery