#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/scoped_vector.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  
class ServiceDiscoveryHostClient
    : public ServiceDiscoveryClient,
      public content::UtilityProcessHostClient {
 public:
  ServiceDiscoveryHostClient();
  
  void Start(const base::Closure& error_callback);
  
  void Shutdown();
  
  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;
  virtual scoped_ptr<LocalDomainResolver> CreateLocalDomainResolver(
      const std::string& domain,
      net::AddressFamily address_family,
      const LocalDomainResolver::IPAddressCallback& callback) OVERRIDE;
  
  virtual void OnProcessCrashed(int exit_code) OVERRIDE;
  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
 protected:
  virtual ~ServiceDiscoveryHostClient();
 private:
  class ServiceWatcherProxy;
  class ServiceResolverProxy;
  class LocalDomainResolverProxy;
  friend class ServiceDiscoveryClientMdns;
  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  
  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);
  
  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);
  
  void RunWatcherCallback(uint64 id,
                          ServiceWatcher::UpdateType update,
                          const std::string& service_name);
  
  void RunResolverCallback(uint64 id,
                           ServiceResolver::RequestStatus status,
                           const ServiceDescription& description);
  
  void RunLocalDomainResolverCallback(uint64 id,
                                      bool success,
                                      const net::IPAddressNumber& address_ipv4,
                                      const net::IPAddressNumber& address_ipv6);
  base::WeakPtr<content::UtilityProcessHost> utility_host_;
  
  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);
};
}  
#endif