summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorvitalybuka@chromium.org <vitalybuka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-07-18 22:04:56 +0000
committervitalybuka@chromium.org <vitalybuka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-07-18 22:04:56 +0000
commit686e985ee27bb868dc2ca361e9fe59658387f95b (patch)
treed7ba243316650f75862bc580091db843981e739c
parent19bcf9dbcb4462147f58a77bc1d5a3520356f3cd (diff)
downloadchromium_src-686e985ee27bb868dc2ca361e9fe59658387f95b.zip
chromium_src-686e985ee27bb868dc2ca361e9fe59658387f95b.tar.gz
chromium_src-686e985ee27bb868dc2ca361e9fe59658387f95b.tar.bz2
Run local discovery code in utility process.
BUG=245391 Review URL: https://chromiumcodereview.appspot.com/19284005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@212447 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/local_discovery/service_discovery_host_client.cc242
-rw-r--r--chrome/browser/local_discovery/service_discovery_host_client.h102
-rw-r--r--chrome/chrome.gyp2
-rw-r--r--chrome/chrome_browser.gypi2
-rw-r--r--chrome/chrome_common.gypi1
-rw-r--r--chrome/common/common_message_generator.h4
-rw-r--r--chrome/common/local_discovery/local_discovery_messages.h65
-rw-r--r--chrome/utility/chrome_content_utility_client.cc10
-rw-r--r--chrome/utility/local_discovery/service_discovery_message_handler.cc102
-rw-r--r--chrome/utility/local_discovery/service_discovery_message_handler.h64
-rw-r--r--ipc/ipc_message_start.h1
11 files changed, 594 insertions, 1 deletions
diff --git a/chrome/browser/local_discovery/service_discovery_host_client.cc b/chrome/browser/local_discovery/service_discovery_host_client.cc
new file mode 100644
index 0000000..2d6db4b
--- /dev/null
+++ b/chrome/browser/local_discovery/service_discovery_host_client.cc
@@ -0,0 +1,242 @@
+// 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.
+
+#include "chrome/browser/local_discovery/service_discovery_host_client.h"
+
+#include "chrome/common/local_discovery/local_discovery_messages.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/utility_process_host.h"
+
+namespace local_discovery {
+
+using content::BrowserThread;
+using content::UtilityProcessHost;
+
+class ServiceDiscoveryHostClient::ServiceWatcherProxy : public ServiceWatcher {
+ public:
+ ServiceWatcherProxy(ServiceDiscoveryHostClient* host,
+ const std::string& service_type,
+ const ServiceWatcher::UpdatedCallback& callback)
+ : host_(host),
+ service_type_(service_type),
+ id_(host_->RegisterWatcherCallback(callback)),
+ started_(false) {
+ }
+
+ virtual ~ServiceWatcherProxy() {
+ host_->UnregisterWatcherCallback(id_);
+ if (started_)
+ host_->Send(new LocalDiscoveryMsg_DestroyWatcher(id_));
+ }
+
+ virtual void Start() OVERRIDE {
+ DCHECK(!started_);
+ host_->Send(new LocalDiscoveryMsg_StartWatcher(id_, service_type_));
+ started_ = true;
+ }
+
+ virtual void DiscoverNewServices(bool force_update) OVERRIDE {
+ DCHECK(started_);
+ host_->Send(new LocalDiscoveryMsg_DiscoverServices(id_, force_update));
+ }
+
+ virtual std::string GetServiceType() const OVERRIDE {
+ return service_type_;
+ }
+
+ private:
+ scoped_refptr<ServiceDiscoveryHostClient> host_;
+ const std::string service_type_;
+ const uint64 id_;
+ bool started_;
+};
+
+class ServiceDiscoveryHostClient::ServiceResolverProxy
+ : public ServiceResolver {
+ public:
+ ServiceResolverProxy(ServiceDiscoveryHostClient* host,
+ const std::string& service_name,
+ const ServiceResolver::ResolveCompleteCallback& callback)
+ : host_(host),
+ service_name_(service_name),
+ id_(host->RegisterResolverCallback(callback)),
+ started_(false) {
+ }
+
+ virtual ~ServiceResolverProxy() {
+ host_->UnregisterResolverCallback(id_);
+ if (started_)
+ host_->Send(new LocalDiscoveryMsg_DestroyResolver(id_));
+ }
+
+ virtual void StartResolving() OVERRIDE {
+ DCHECK(!started_);
+ host_->Send(new LocalDiscoveryMsg_ResolveService(id_, service_name_));
+ started_ = true;
+ }
+
+ virtual std::string GetName() const OVERRIDE {
+ return service_name_;
+ }
+
+ private:
+ scoped_refptr<ServiceDiscoveryHostClient> host_;
+ const std::string service_name_;
+ const uint64 id_;
+ bool started_;
+};
+
+ServiceDiscoveryHostClient::ServiceDiscoveryHostClient() : current_id_(0) {
+ callback_runner_ = base::MessageLoop::current()->message_loop_proxy();
+}
+
+ServiceDiscoveryHostClient::~ServiceDiscoveryHostClient() {
+ DCHECK(CalledOnValidThread());
+ DCHECK(service_watcher_callbacks_.empty());
+ DCHECK(service_resolver_callbacks_.empty());
+}
+
+scoped_ptr<ServiceWatcher> ServiceDiscoveryHostClient::CreateServiceWatcher(
+ const std::string& service_type,
+ const ServiceWatcher::UpdatedCallback& callback) {
+ DCHECK(CalledOnValidThread());
+ return scoped_ptr<ServiceWatcher>(
+ new ServiceWatcherProxy(this, service_type, callback));
+}
+
+scoped_ptr<ServiceResolver> ServiceDiscoveryHostClient::CreateServiceResolver(
+ const std::string& service_name,
+ const ServiceResolver::ResolveCompleteCallback& callback) {
+ DCHECK(CalledOnValidThread());
+ return scoped_ptr<ServiceResolver>(
+ new ServiceResolverProxy(this, service_name, callback));
+}
+
+uint64 ServiceDiscoveryHostClient::RegisterWatcherCallback(
+ const ServiceWatcher::UpdatedCallback& callback) {
+ DCHECK(CalledOnValidThread());
+ DCHECK(!ContainsKey(service_watcher_callbacks_, current_id_ + 1));
+ service_watcher_callbacks_[++current_id_] = callback;
+ return current_id_;
+}
+
+uint64 ServiceDiscoveryHostClient::RegisterResolverCallback(
+ const ServiceResolver::ResolveCompleteCallback& callback) {
+ DCHECK(CalledOnValidThread());
+ DCHECK(!ContainsKey(service_resolver_callbacks_, current_id_ + 1));
+ service_resolver_callbacks_[++current_id_] = callback;
+ return current_id_;
+}
+
+void ServiceDiscoveryHostClient::UnregisterWatcherCallback(uint64 id) {
+ DCHECK(CalledOnValidThread());
+ DCHECK(ContainsKey(service_watcher_callbacks_, id));
+ service_watcher_callbacks_.erase(id);
+}
+
+void ServiceDiscoveryHostClient::UnregisterResolverCallback(uint64 id) {
+ DCHECK(CalledOnValidThread());
+ DCHECK(ContainsKey(service_resolver_callbacks_, id));
+ service_resolver_callbacks_.erase(id);
+}
+
+void ServiceDiscoveryHostClient::Start() {
+ DCHECK(CalledOnValidThread());
+ BrowserThread::PostTask(
+ BrowserThread::IO,
+ FROM_HERE,
+ base::Bind(&ServiceDiscoveryHostClient::StartOnIOThread, this));
+}
+
+void ServiceDiscoveryHostClient::Shutdown() {
+ DCHECK(CalledOnValidThread());
+ BrowserThread::PostTask(
+ BrowserThread::IO,
+ FROM_HERE,
+ base::Bind(&ServiceDiscoveryHostClient::ShutdownOnIOThread, this));
+}
+
+void ServiceDiscoveryHostClient::StartOnIOThread() {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ utility_host_ = UtilityProcessHost::Create(
+ this, base::MessageLoopProxy::current().get())->AsWeakPtr();
+ if (utility_host_) {
+ utility_host_->EnableZygote();
+ // TODO(vitalybuka): enable sandbox. http://crbug.com/245391
+ utility_host_->DisableSandbox();
+ utility_host_->StartBatchMode();
+ }
+}
+
+void ServiceDiscoveryHostClient::ShutdownOnIOThread() {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ if (utility_host_)
+ utility_host_->EndBatchMode();
+}
+
+void ServiceDiscoveryHostClient::Send(IPC::Message* msg) {
+ DCHECK(CalledOnValidThread());
+ BrowserThread::PostTask(
+ BrowserThread::IO,
+ FROM_HERE,
+ base::Bind(base::IgnoreResult(&content::UtilityProcessHost::Send),
+ utility_host_, msg));
+}
+
+bool ServiceDiscoveryHostClient::OnMessageReceived(
+ const IPC::Message& message) {
+ bool handled = true;
+ IPC_BEGIN_MESSAGE_MAP(ServiceDiscoveryHostClient, message)
+ IPC_MESSAGE_HANDLER(LocalDiscoveryHostMsg_WatcherCallback,
+ OnWatcherCallback)
+ IPC_MESSAGE_HANDLER(LocalDiscoveryHostMsg_ResolverCallback,
+ OnResolverCallback)
+ IPC_MESSAGE_UNHANDLED(handled = false)
+ IPC_END_MESSAGE_MAP()
+ return handled;
+}
+
+void ServiceDiscoveryHostClient::OnWatcherCallback(
+ uint64 id,
+ ServiceWatcher::UpdateType update,
+ const std::string& service_name) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ callback_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&ServiceDiscoveryHostClient::RunWatcherCallback, this, id,
+ update, service_name));
+}
+
+void ServiceDiscoveryHostClient::OnResolverCallback(
+ uint64 id,
+ ServiceResolver::RequestStatus status,
+ const ServiceDescription& description) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ callback_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&ServiceDiscoveryHostClient::RunResolverCallback, this, id,
+ status, description));
+}
+
+void ServiceDiscoveryHostClient::RunWatcherCallback(
+ uint64 id,
+ ServiceWatcher::UpdateType update,
+ const std::string& service_name) {
+ DCHECK(CalledOnValidThread());
+ WatcherCallbacks::iterator it = service_watcher_callbacks_.find(id);
+ if (it != service_watcher_callbacks_.end() && !it->second.is_null())
+ it->second.Run(update, service_name);
+}
+
+void ServiceDiscoveryHostClient::RunResolverCallback(
+ uint64 id,
+ ServiceResolver::RequestStatus status,
+ const ServiceDescription& description) {
+ DCHECK(CalledOnValidThread());
+ ResolverCallbacks::iterator it = service_resolver_callbacks_.find(id);
+ if (it != service_resolver_callbacks_.end() && !it->second.is_null())
+ it->second.Run(status, description);
+}
+
+} // namespace local_discovery
diff --git a/chrome/browser/local_discovery/service_discovery_host_client.h b/chrome/browser/local_discovery/service_discovery_host_client.h
new file mode 100644
index 0000000..4bdf08b
--- /dev/null
+++ b/chrome/browser/local_discovery/service_discovery_host_client.h
@@ -0,0 +1,102 @@
+// 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 "base/threading/non_thread_safe.h"
+#include "chrome/common/local_discovery/service_discovery_client.h"
+#include "content/public/browser/utility_process_host_client.h"
+
+namespace base {
+class TaskRunner;
+}
+
+namespace content {
+class UtilityProcessHost;
+}
+
+namespace local_discovery {
+
+// Implementation of ServiceDiscoveryClient that delegates all functionality to
+// utility process.
+class ServiceDiscoveryHostClient : public base::NonThreadSafe,
+ public ServiceDiscoveryClient,
+ public content::UtilityProcessHostClient {
+ public:
+ ServiceDiscoveryHostClient();
+
+ // Starts utility process with ServiceDiscoveryClient.
+ void Start();
+
+ // Shutdowns utility process.
+ void Shutdown();
+
+ // ServiceDiscoveryClient implementation.
+ virtual scoped_ptr<ServiceWatcher> CreateServiceWatcher(
+ const std::string& service_type,
+ const ServiceWatcher::UpdatedCallback& callback) OVERRIDE;
+ virtual scoped_ptr<ServiceResolver> CreateServiceResolver(
+ const std::string& service_name,
+ const ServiceResolver::ResolveCompleteCallback& callback) OVERRIDE;
+
+ // UtilityProcessHostClient implementation.
+ virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
+
+ protected:
+ virtual ~ServiceDiscoveryHostClient();
+
+ private:
+ class ServiceWatcherProxy;
+ class ServiceResolverProxy;
+
+ typedef std::map<uint64, ServiceWatcher::UpdatedCallback> WatcherCallbacks;
+ typedef std::map<uint64, ServiceResolver::ResolveCompleteCallback>
+ ResolverCallbacks;
+
+ void StartOnIOThread();
+ void ShutdownOnIOThread();
+
+ void Send(IPC::Message* msg);
+
+ uint64 RegisterWatcherCallback(
+ const ServiceWatcher::UpdatedCallback& callback);
+ uint64 RegisterResolverCallback(
+ const ServiceResolver::ResolveCompleteCallback& callback);
+ void UnregisterWatcherCallback(uint64 id);
+ void UnregisterResolverCallback(uint64 id);
+
+ // IPC Message handlers.
+ void OnWatcherCallback(uint64 id,
+ ServiceWatcher::UpdateType update,
+ const std::string& service_name);
+ void OnResolverCallback(uint64 id,
+ ServiceResolver::RequestStatus status,
+ const ServiceDescription& description);
+
+ // 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);
+
+ base::WeakPtr<content::UtilityProcessHost> utility_host_;
+
+ // Incrementing counter to assign ID to watchers and resolvers.
+ uint64 current_id_;
+ WatcherCallbacks service_watcher_callbacks_;
+ ResolverCallbacks service_resolver_callbacks_;
+ scoped_refptr<base::TaskRunner> callback_runner_;
+
+ DISALLOW_COPY_AND_ASSIGN(ServiceDiscoveryHostClient);
+};
+
+} // namespace local_discovery
+
+#endif // CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_HOST_CLIENT_H_
diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp
index a5db908..7711b86 100644
--- a/chrome/chrome.gyp
+++ b/chrome/chrome.gyp
@@ -380,6 +380,8 @@
'utility/local_discovery/local_domain_resolver.h',
'utility/local_discovery/service_discovery_client_impl.cc',
'utility/local_discovery/service_discovery_client_impl.h',
+ 'utility/local_discovery/service_discovery_message_handler.cc',
+ 'utility/local_discovery/service_discovery_message_handler.h',
]
}],
],
diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi
index 3c8dea2..2b79c88 100644
--- a/chrome/chrome_browser.gypi
+++ b/chrome/chrome_browser.gypi
@@ -3140,6 +3140,8 @@
'browser/local_discovery/privet_device_lister.cc',
'browser/local_discovery/privet_device_lister_impl.h',
'browser/local_discovery/privet_device_lister_impl.cc',
+ 'browser/local_discovery/service_discovery_host_client.cc',
+ 'browser/local_discovery/service_discovery_host_client.h',
]
}]
],
diff --git a/chrome/chrome_common.gypi b/chrome/chrome_common.gypi
index 9c7ba2d..0498820 100644
--- a/chrome/chrome_common.gypi
+++ b/chrome/chrome_common.gypi
@@ -593,6 +593,7 @@
}],
['enable_mdns == 1', {
'sources': [
+ 'common/local_discovery/local_discovery_messages.h',
'common/local_discovery/service_discovery_client.cc',
'common/local_discovery/service_discovery_client.h',
]
diff --git a/chrome/common/common_message_generator.h b/chrome/common/common_message_generator.h
index 0c1fedf..9e11e25 100644
--- a/chrome/common/common_message_generator.h
+++ b/chrome/common/common_message_generator.h
@@ -19,6 +19,10 @@
#include "chrome/common/tts_messages.h"
#include "chrome/common/validation_message_messages.h"
+#if defined(ENABLE_MDNS)
+#include "chrome/common/local_discovery/local_discovery_messages.h"
+#endif
+
#if defined(ENABLE_WEBRTC)
#include "chrome/common/media/webrtc_logging_messages.h"
#endif
diff --git a/chrome/common/local_discovery/local_discovery_messages.h b/chrome/common/local_discovery/local_discovery_messages.h
new file mode 100644
index 0000000..87f0027
--- /dev/null
+++ b/chrome/common/local_discovery/local_discovery_messages.h
@@ -0,0 +1,65 @@
+// 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.
+
+// Defines local discovery messages between the browser and utility process.
+
+#include "chrome/common/local_discovery/service_discovery_client.h"
+#include "ipc/ipc_message_macros.h"
+
+#define IPC_MESSAGE_START LocalDiscoveryMsgStart
+
+IPC_STRUCT_TRAITS_BEGIN(local_discovery::ServiceDescription)
+ IPC_STRUCT_TRAITS_MEMBER(service_name)
+ IPC_STRUCT_TRAITS_MEMBER(address)
+ IPC_STRUCT_TRAITS_MEMBER(metadata)
+ IPC_STRUCT_TRAITS_MEMBER(ip_address)
+ IPC_STRUCT_TRAITS_MEMBER(last_seen)
+IPC_STRUCT_TRAITS_END()
+
+IPC_ENUM_TRAITS(local_discovery::ServiceWatcher::UpdateType)
+IPC_ENUM_TRAITS(local_discovery::ServiceResolver::RequestStatus)
+
+//------------------------------------------------------------------------------
+// Utility process messages:
+// These are messages from the browser to the utility process.
+
+// Creates watcher and starts listening in utility process.
+IPC_MESSAGE_CONTROL2(LocalDiscoveryMsg_StartWatcher,
+ uint64 /* id */,
+ std::string /* service_type */)
+
+// Discovers new services.
+IPC_MESSAGE_CONTROL2(LocalDiscoveryMsg_DiscoverServices,
+ uint64 /* id */,
+ bool /* force_update */)
+
+// Destroys watcher in utility process.
+IPC_MESSAGE_CONTROL1(LocalDiscoveryMsg_DestroyWatcher,
+ uint64 /* id */)
+
+// Creates service resolver and starts resolving service in utility process.
+IPC_MESSAGE_CONTROL2(LocalDiscoveryMsg_ResolveService,
+ uint64 /* id */,
+ std::string /* service_name */)
+
+// Destroys service resolver in utility process.
+IPC_MESSAGE_CONTROL1(LocalDiscoveryMsg_DestroyResolver,
+ uint64 /* id */)
+
+//------------------------------------------------------------------------------
+// Utility process host messages:
+// These are messages from the utility process to the browser.
+
+// Notifies browser process about new services.
+IPC_MESSAGE_CONTROL3(LocalDiscoveryHostMsg_WatcherCallback,
+ uint64 /* id */,
+ local_discovery::ServiceWatcher::UpdateType /* update */,
+ std::string /* service_name */)
+
+// Notifies browser process about service resolution results.
+IPC_MESSAGE_CONTROL3(
+ LocalDiscoveryHostMsg_ResolverCallback,
+ uint64 /* id */,
+ local_discovery::ServiceResolver::RequestStatus /* status */,
+ local_discovery::ServiceDescription /* description */)
diff --git a/chrome/utility/chrome_content_utility_client.cc b/chrome/utility/chrome_content_utility_client.cc
index 3a4991b..967352b 100644
--- a/chrome/utility/chrome_content_utility_client.cc
+++ b/chrome/utility/chrome_content_utility_client.cc
@@ -53,6 +53,10 @@
#include "printing/backend/print_backend.h"
#endif
+#if defined(ENABLE_MDNS)
+#include "chrome/utility/local_discovery/service_discovery_message_handler.h"
+#endif // ENABLE_MDNS
+
namespace chrome {
namespace {
@@ -70,7 +74,11 @@ void ReleaseProcessIfNeeded() {
ChromeContentUtilityClient::ChromeContentUtilityClient() {
#if !defined(OS_ANDROID)
handlers_.push_back(new ProfileImportHandler());
-#endif
+#endif // OS_ANDROID
+
+#if defined(ENABLE_MDNS)
+ handlers_.push_back(new local_discovery::ServiceDiscoveryMessageHandler());
+#endif // ENABLE_MDNS
}
ChromeContentUtilityClient::~ChromeContentUtilityClient() {
diff --git a/chrome/utility/local_discovery/service_discovery_message_handler.cc b/chrome/utility/local_discovery/service_discovery_message_handler.cc
new file mode 100644
index 0000000..e401226
--- /dev/null
+++ b/chrome/utility/local_discovery/service_discovery_message_handler.cc
@@ -0,0 +1,102 @@
+// 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.
+
+#include "chrome/utility/local_discovery/service_discovery_message_handler.h"
+
+#include "chrome/common/local_discovery/local_discovery_messages.h"
+#include "chrome/utility/local_discovery/service_discovery_client_impl.h"
+#include "content/public/utility/utility_thread.h"
+
+namespace local_discovery {
+
+ServiceDiscoveryMessageHandler::ServiceDiscoveryMessageHandler() {
+}
+
+ServiceDiscoveryMessageHandler::~ServiceDiscoveryMessageHandler() {
+}
+
+void ServiceDiscoveryMessageHandler::Initialize() {
+ if (!service_discovery_client_) {
+ mdns_client_ = net::MDnsClient::CreateDefault();
+ mdns_client_->StartListening();
+ service_discovery_client_.reset(
+ new local_discovery::ServiceDiscoveryClientImpl(mdns_client_.get()));
+ }
+}
+
+bool ServiceDiscoveryMessageHandler::OnMessageReceived(
+ const IPC::Message& message) {
+ bool handled = true;
+ IPC_BEGIN_MESSAGE_MAP(ServiceDiscoveryMessageHandler, message)
+ IPC_MESSAGE_HANDLER(LocalDiscoveryMsg_StartWatcher, OnStartWatcher)
+ IPC_MESSAGE_HANDLER(LocalDiscoveryMsg_DiscoverServices, OnDiscoverServices)
+ IPC_MESSAGE_HANDLER(LocalDiscoveryMsg_DestroyWatcher, OnDestroyWatcher)
+ IPC_MESSAGE_HANDLER(LocalDiscoveryMsg_ResolveService, OnResolveService)
+ IPC_MESSAGE_HANDLER(LocalDiscoveryMsg_DestroyResolver, OnDestroyResolver)
+ IPC_MESSAGE_UNHANDLED(handled = false)
+ IPC_END_MESSAGE_MAP()
+ return handled;
+}
+
+void ServiceDiscoveryMessageHandler::OnStartWatcher(
+ uint64 id,
+ const std::string& service_type) {
+ Initialize();
+ DCHECK(!ContainsKey(service_watchers_, id));
+ scoped_ptr<ServiceWatcher> watcher(
+ service_discovery_client_->CreateServiceWatcher(
+ service_type,
+ base::Bind(&ServiceDiscoveryMessageHandler::OnServiceUpdated,
+ base::Unretained(this), id)));
+ watcher->Start();
+ service_watchers_[id].reset(watcher.release());
+}
+
+void ServiceDiscoveryMessageHandler::OnDiscoverServices(uint64 id,
+ bool force_update) {
+ DCHECK(ContainsKey(service_watchers_, id));
+ service_watchers_[id]->DiscoverNewServices(force_update);
+}
+
+void ServiceDiscoveryMessageHandler::OnDestroyWatcher(uint64 id) {
+ DCHECK(ContainsKey(service_watchers_, id));
+ service_watchers_.erase(id);
+}
+
+void ServiceDiscoveryMessageHandler::OnResolveService(
+ uint64 id,
+ const std::string& service_name) {
+ Initialize();
+ DCHECK(!ContainsKey(service_resolvers_, id));
+ scoped_ptr<ServiceResolver> resolver(
+ service_discovery_client_->CreateServiceResolver(
+ service_name,
+ base::Bind(&ServiceDiscoveryMessageHandler::OnServiceResolved,
+ base::Unretained(this), id)));
+ resolver->StartResolving();
+ service_resolvers_[id].reset(resolver.release());
+}
+
+void ServiceDiscoveryMessageHandler::OnDestroyResolver(uint64 id) {
+ DCHECK(ContainsKey(service_resolvers_, id));
+ service_resolvers_.erase(id);
+}
+
+void ServiceDiscoveryMessageHandler::OnServiceUpdated(
+ uint64 id,
+ ServiceWatcher::UpdateType update,
+ const std::string& name) {
+ content::UtilityThread::Get()->Send(
+ new LocalDiscoveryHostMsg_WatcherCallback(id, update, name));
+}
+
+void ServiceDiscoveryMessageHandler::OnServiceResolved(
+ uint64 id,
+ ServiceResolver::RequestStatus status,
+ const ServiceDescription& description) {
+ content::UtilityThread::Get()->Send(
+ new LocalDiscoveryHostMsg_ResolverCallback(id, status, description));
+}
+
+} // namespace local_discovery
diff --git a/chrome/utility/local_discovery/service_discovery_message_handler.h b/chrome/utility/local_discovery/service_discovery_message_handler.h
new file mode 100644
index 0000000..8b4bd1a
--- /dev/null
+++ b/chrome/utility/local_discovery/service_discovery_message_handler.h
@@ -0,0 +1,64 @@
+// 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 <map>
+
+#include "base/memory/linked_ptr.h"
+#include "chrome/common/local_discovery/service_discovery_client.h"
+#include "chrome/utility/utility_message_handler.h"
+
+namespace net {
+class MDnsClient;
+}
+
+namespace local_discovery {
+
+class ServiceDiscoveryClient;
+
+// Handles messages related to local discovery inside utility process.
+class ServiceDiscoveryMessageHandler : public chrome::UtilityMessageHandler {
+ public:
+ ServiceDiscoveryMessageHandler();
+ virtual ~ServiceDiscoveryMessageHandler();
+
+ // UtilityMessageHandler implementation.
+ virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
+
+ private:
+ typedef std::map<uint64, linked_ptr<ServiceWatcher> > ServiceWatchers;
+ typedef std::map<uint64, linked_ptr<ServiceResolver> > ServiceResolvers;
+
+ // Lazy initializes ServiceDiscoveryClient.
+ void Initialize();
+
+ // IPC message handlers.
+ void OnStartWatcher(uint64 id, const std::string& service_type);
+ void OnDiscoverServices(uint64 id, bool force_update);
+ void OnDestroyWatcher(uint64 id);
+ void OnResolveService(uint64 id, const std::string& service_name);
+ void OnDestroyResolver(uint64 id);
+
+ // Is called by ServiceWatcher as callback.
+ void OnServiceUpdated(uint64 id,
+ ServiceWatcher::UpdateType update,
+ const std::string& name);
+
+ // Is called by ServiceResolver as callback.
+ void OnServiceResolved(uint64 id,
+ ServiceResolver::RequestStatus status,
+ const ServiceDescription& description);
+
+ ServiceWatchers service_watchers_;
+ ServiceResolvers service_resolvers_;
+
+ scoped_ptr<net::MDnsClient> mdns_client_;
+ scoped_ptr<ServiceDiscoveryClient> service_discovery_client_;
+};
+
+} // namespace local_discovery
+
+#endif // CHROME_UTILITY_LOCAL_DISCOVERY_SERVICE_DISCOVERY_MESSAGE_HANDLER_H_
diff --git a/ipc/ipc_message_start.h b/ipc/ipc_message_start.h
index 2ebb832..79c823b 100644
--- a/ipc/ipc_message_start.h
+++ b/ipc/ipc_message_start.h
@@ -87,6 +87,7 @@ enum IPCMessageStart {
NaClHostMsgStart,
WebRTCIdentityMsgStart,
EncodedVideoCaptureMsgStart,
+ LocalDiscoveryMsgStart,
LastIPCMsgStart // Must come last.
};