Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2299)

Unified Diff: chrome/browser/local_discovery/service_discovery_client_impl.h

Issue 16272006: In-browser DNS-based service discovery system (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mdns_implementation
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/local_discovery/service_discovery_client_impl.h
diff --git a/chrome/browser/local_discovery/service_discovery_client_impl.h b/chrome/browser/local_discovery/service_discovery_client_impl.h
new file mode 100644
index 0000000000000000000000000000000000000000..608db60667a8965f72994ec967175df77bec3a14
--- /dev/null
+++ b/chrome/browser/local_discovery/service_discovery_client_impl.h
@@ -0,0 +1,213 @@
+// Copyright (c) 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_CLIENT_IMPL_H_
+#define CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_IMPL_H_
+
+#include <set>
+#include <string>
+#include <vector>
+
+#include "base/callback.h"
+#include "base/cancelable_callback.h"
+#include "base/memory/weak_ptr.h"
+#include "base/message_loop.h"
+#include "chrome/browser/local_discovery/service_discovery_client.h"
+#include "net/dns/mdns_client.h"
+
+namespace local_discovery {
+
+class ServiceDiscoveryClientImpl : public ServiceDiscoveryClient {
+ public:
+ ServiceDiscoveryClientImpl();
+ virtual ~ServiceDiscoveryClientImpl();
+
+ // Create a service object listening for DNS-SD service announcements on
+ // service type |service_type|.
+ virtual scoped_ptr<ServiceTypeWatcher> CreateServiceTypeWatcher(
+ const std::string& service_type,
+ bool active,
+ bool alert_existing_services,
+ ServiceTypeWatcher::Delegate* delegate) OVERRIDE;
+
+ // Create a service object listening for DNS-SD service announcements on
+ // service |service_name|. |delegate| may be null in which case the
+ // object will not recieve continuous announcements but may still query
+ // about the relevant service.
+ virtual scoped_ptr<ServiceReader> CreateServiceReader(
+ const std::string& service_name,
+ ServiceReader::Delegate* delegate) OVERRIDE;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(ServiceDiscoveryClientImpl);
+};
+
+class ServiceTypeWatcherImpl : public ServiceTypeWatcher,
+ public net::MDnsListener::Delegate {
+ public:
+ ServiceTypeWatcherImpl(const std::string& service_type,
+ bool active,
+ bool alert_existing_services,
+ ServiceTypeWatcher::Delegate* delegate);
+ // Listening will automatically stop when the destructor is called.
+ virtual ~ServiceTypeWatcherImpl();
+
+ virtual bool Start() OVERRIDE;
+
+ // Get all known services names of this watcher's type. Return them in
+ // |services|.
+ virtual void GetAvailableServices(
+ std::vector<std::string>* services) const OVERRIDE;
+
+ // Probe for services of this type.
+ virtual void DiscoverNewServices() OVERRIDE;
+
+ // Probe for services of this type and also force early invalidation
+ // of existing services, having the effect of throwing away invalidated
+ // services. This method should NOT be called periodically, but only
+ // in response to user action.
+ virtual void ForceUpdateServices() OVERRIDE;
+
+ virtual std::string GetServiceType() const OVERRIDE;
+
+ virtual bool IsActive() const OVERRIDE;
+
+ virtual void OnRecordUpdate(net::MDnsListener::UpdateType update,
+ const net::RecordParsed* record) OVERRIDE;
+
+ virtual void OnNsecRecord(const std::string& name, unsigned rrtype) OVERRIDE;
+
+ virtual void OnCachePurged() OVERRIDE;
+
+ virtual void OnTransactionResponse(net::MDnsTransaction::Result result,
+ const net::RecordParsed* record);
+
+ private:
+ void AddService(const std::string& service);
+ void RemoveService(const std::string& service);
+ bool CreateTransaction(bool active, bool alert_existing_services,
+ bool force_refresh);
+
+ std::string service_type_;
+ bool active_;
+ bool alert_existing_services_;
+ std::set<std::string> services_;
+ scoped_ptr<net::MDnsTransaction> transaction_;
+ scoped_ptr<net::MDnsListener> listener_;
+
+ ServiceTypeWatcher::Delegate* delegate_;
+ bool started_;
+
+ DISALLOW_COPY_AND_ASSIGN(ServiceTypeWatcherImpl);
+};
+
+class ServiceReaderImpl
+ : public ServiceReader,
+ public net::MDnsListener::Delegate,
+ public base::SupportsWeakPtr<ServiceReaderImpl> {
+ public:
+ ServiceReaderImpl(const std::string& service_name,
+ ServiceReader::Delegate* delegate);
+
+ // Listening will automatically stop when the destructor is called.
+ virtual ~ServiceReaderImpl();
+
+ virtual bool Start() OVERRIDE;
+
+ // Get the cached TXT record for a service. Returns true and fills |metadata|
+ // if the record exists. Use NULL for |metadata| to simply check if the value
+ // is present.
+ virtual bool GetMetadata(std::vector<std::string>* metadata) const OVERRIDE;
+
+ // Read the TXT record for a service. If |force_refresh| is
+ // true, force a refresh of the record even if it is cached, otherwise
+ // defer to the cache when it is available.
+ virtual void ReadMetadata(
+ const ServiceReader::MetadataCallback& callback,
+ bool force_refresh) OVERRIDE;
+
+ // Get the cached SRV record for a service. Returns true and fills |address|
+ // if the record exists. Use NULL for |address| to simply check if the value
+ // is present.
+ virtual bool GetAddress(net::HostPortPair* address) const OVERRIDE;
+
+ // Read the SRV record for a service. If |force_refresh| is
+ // true, force a refresh of the record even if it is cached, otherwise
+ // defer to the cache when it is available.
+ virtual void ReadAddress(const ServiceReader::AddressCallback& callback,
+ bool force_refresh) OVERRIDE;
+
+ virtual std::string GetHumanReadableName() const OVERRIDE;
+ virtual std::string GetType() const OVERRIDE;
+ virtual std::string GetName() const OVERRIDE;
+
+ // Check whether the service is available.
+ virtual bool IsAvailable() const OVERRIDE;
+
+ // Return the last time the SRV record has been seen. Return base::Time for
+ // unavailable records.
+ virtual void ReadLastSeen(
+ const ServiceReader::LastSeenCallback& callback) OVERRIDE;
+
+ virtual void OnRecordUpdate(net::MDnsListener::UpdateType update,
+ const net::RecordParsed* record) OVERRIDE;
+
+ virtual void OnNsecRecord(const std::string& name, unsigned type) OVERRIDE;
+
+ virtual void OnCachePurged() OVERRIDE;
+
+ private:
+ // Respond to transaction finishing for SRV records.
+ void SrvRecordTransactionResponse(net::MDnsTransaction::Result status,
+ const net::RecordParsed* record);
+
+ // Respond to transaction finishing for TXT records.
+ void TxtRecordTransactionResponse(net::MDnsTransaction::Result status,
+ const net::RecordParsed* record);
+
+ void LastSeenTransactionResponse(
+ const ServiceReader::LastSeenCallback& callback,
+ net::MDnsTransaction::Result status,
+ const net::RecordParsed* record);
+
+ // Convert a TXT record to a vector of strings (metadata).
+ const std::vector<std::string>& RecordToMetadata(
+ const net::RecordParsed* record) const;
+
+ // Convert an SRV record to a host and port pair.
+ net::HostPortPair RecordToAddress(
+ const net::RecordParsed* record) const;
+
+ // Convert an MDns status to a service discovery status.
+ ServiceReader::RequestStatus MDnsStatusToRequestStatus(
+ net::MDnsTransaction::Result status) const;
+
+ bool CreateTxtTransaction();
+ bool CreateSrvTransaction();
+
+ std::string service_name_;
+ ServiceReader::Delegate* delegate_;
+
+ scoped_ptr<net::MDnsListener> txt_listener_;
+ scoped_ptr<net::MDnsListener> srv_listener_;
+
+ scoped_ptr<net::MDnsTransaction> txt_transaction_;
+ scoped_ptr<net::MDnsTransaction> srv_transaction_;
+ scoped_ptr<net::MDnsTransaction> last_seen_transaction_;
+
+ std::vector<ServiceReader::AddressCallback> address_callbacks_;
+ std::vector<ServiceReader::MetadataCallback> metadata_callbacks_;
+
+ bool has_address_;
+ bool has_metadata_;
+ net::HostPortPair address_;
+ std::vector<std::string> metadata_;
+ bool started_;
+
+ DISALLOW_COPY_AND_ASSIGN(ServiceReaderImpl);
+};
+
+} // namespace local_discovery
+
+#endif // CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_IMPL_H_

Powered by Google App Engine
This is Rietveld 408576698