Index: net/dns/mdns_client.h |
diff --git a/net/dns/mdns_client.h b/net/dns/mdns_client.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d404220fbf289c3dc812175a4e8ce1105794ec17 |
--- /dev/null |
+++ b/net/dns/mdns_client.h |
@@ -0,0 +1,128 @@ |
+// 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 NET_DNS_MDNS_CLIENT_H_ |
+#define NET_DNS_MDNS_CLIENT_H_ |
+ |
+#include <string> |
+#include <vector> |
+ |
+#include "base/callback.h" |
+#include "net/dns/dns_query.h" |
+#include "net/dns/dns_response.h" |
+#include "net/dns/record_parsed.h" |
+ |
+namespace net { |
+ |
+class RecordParsed; |
+ |
+enum MDnsUpdateType { |
+ kMDnsRecordAdded, |
+ kMDnsRecordChanged, |
+ kMDnsRecordRemoved |
+}; |
+ |
+enum MDnsTransactionResult { |
+ kMDnsTransactionSuccess, |
+ kMDnsTransactionTimeout, |
+ kMDnsTransactionNsec |
+}; |
+ |
+// A class representing a one-time unique-record lookup. A transaction takes one |
+// associated callback (see |MDnsClient::CreateTransaction|) and calls it when |
+// a matching record has been found, either from the cache or by querying the |
+// network. Transactions will time out after a reasonable number of seconds. |
+class MDnsTransaction { |
+ public: |
+ typedef base::Callback<void(MDnsTransactionResult, const RecordParsed*)> |
+ ResultCallback; |
+ |
+ // Destroying the transaction cancels it. |
+ virtual ~MDnsTransaction() {} |
+ |
+ // Get the host or service name for the transaction. |
+ virtual const std::string& GetName() const = 0; |
+ |
+ // Get the type for this transaction (SRV, TXT, A, AAA, etc) |
+ virtual uint16 GetType() const = 0; |
+}; |
+ |
+// A listener listens for updates regarding a specific record or set of records. |
+// If a listener is active (determined at its creation) it will send queries |
+// when appropriate. |
+class MDnsListener { |
+ public: |
+ class Delegate { |
+ public: |
+ virtual ~Delegate() {} |
+ |
+ // Called when a record is added, removed or updated. |
+ virtual void OnRecordUpdate(MDnsUpdateType update, |
+ const RecordParsed* record) = 0; |
+ |
+ // Called when a record is marked nonexistent by an NSEC record. |
+ virtual void OnNsecRecord(const std::string& name, unsigned type) = 0; |
+ }; |
+ |
+ // Destroying the listener stops listening. |
+ virtual ~MDnsListener() {} |
+ |
+ // Get the host or service name for this query. |
+ // Return an empty string for no name. |
+ virtual const std::string& GetName() const = 0; |
+ |
+ // Get the type for this query (SRV, TXT, A, AAA, etc) |
+ virtual uint16 GetType() const = 0; |
+ |
+ // Whether this listener is active (true) or passive (false) |
+ virtual bool IsActive() const = 0; |
+ |
+ // Applies only to listeners with names. Will send out a query for new |
+ // information. |force_refresh_cache| will force a refresh of all cached |
+ // entities. |
+ virtual bool SendQuery(bool force_refresh_cache) = 0; |
+ |
+ // Applies only to listeners with names. Query mDNS cache synchronously for |
+ // either single- or multi- valued records. |
+ virtual bool QueryCache(std::vector<const RecordParsed*>* records) const = 0; |
+}; |
+ |
+// This class listens for Multicast DNS on the local network. You can access |
+// information regarding multicast DNS either by creating an |MDnsListener| to |
+// be notified of new records, or by creating an |MDnsTransaction| to look up |
+// the value of a specific records. |
+class MDnsClient { |
+ public: |
+ virtual ~MDnsClient() {} |
+ |
+ // Create listener object for RRType |rrtype| and name |name|. |
+ // If |name| is an empty string, listen to all notification of type |
+ // |rrtype|. If a listener is |active|, it will send queries when the client |
+ // thinks they are needed. If |alert_existing_records| is true, a listener's |
+ // delegate will be alerted for records already in the cache when it is |
+ // created. |
+ virtual scoped_ptr<MDnsListener> CreateListener( |
+ uint16 rrtype, |
+ const std::string& name, |
+ bool active, |
+ bool alert_existing_records, |
+ MDnsListener::Delegate* delegate) = 0; |
+ |
+ // Create a transaction to Query MDNS for a single-value query |
+ // (A, AAAA, TXT, and SRV) asynchronously. May defer to cache. |
+ virtual scoped_ptr<MDnsTransaction> CreateTransaction( |
+ uint16 rrtype, |
+ const std::string& name, |
+ const MDnsTransaction::ResultCallback& callback) = 0; |
+ |
+ // Lazily create and return static instance for MDnsClient. |
+ static MDnsClient* GetInstance(); |
+ |
+ // Set the global instance (for testing). MUST be called before the first call |
+ // to GetInstance. |
+ static void SetInstance(MDnsClient* instance); |
+}; |
+ |
+} // net |
+#endif // NET_DNS_MDNS_CLIENT_H_ |