Chromium Code Reviews| Index: net/dns/mdns_client_impl.h |
| diff --git a/net/dns/mdns_client_impl.h b/net/dns/mdns_client_impl.h |
| index d4363b35f1276f582da75dbd030d4244e4e8d6c5..975db9f6cb1788fae826c5ac8dabfec1b291ad41 100644 |
| --- a/net/dns/mdns_client_impl.h |
| +++ b/net/dns/mdns_client_impl.h |
| @@ -25,23 +25,24 @@ namespace net { |
| class MDnsListenerImpl; |
| -// This class represents a connection to the network for multicast DNS clients. |
| -// It reads data into DNSResponse objects and alerts the delegate that a packet |
| -// has been received. |
| +// A connection to the network for multicast DNS clients. It reads data into |
| +// DnsResponse objects and alerts the delegate that a packet has been received. |
| class MDnsConnection { |
| public: |
| class Delegate { |
| public: |
| // Handle an mDNS packet buffered in |response| with a size of |bytes_read|. |
| virtual void HandlePacket(DnsResponse* response, int bytes_read) = 0; |
| + virtual void OnConnectionError(int error) = 0; |
| virtual ~Delegate() {} |
| }; |
| // Start the connection. Will begin alerting the delegate of packets. |
| - virtual bool Init() = 0; |
| + // Returns a net error code. |
| + virtual int Init() = 0; |
| - // Send a packet on both ipv4 and ipv6. |
| - virtual bool Send(IOBuffer* buffer, unsigned size) = 0; |
| + // Send a packet on both ipv4 and ipv6. Returns a net error code. |
| + virtual int Send(IOBuffer* buffer, unsigned size) = 0; |
| virtual ~MDnsConnection() {} |
| }; |
| @@ -73,7 +74,7 @@ class MDnsClientImpl : public MDnsClient { |
| // Add/remove a listener to the list of listener. May cause network traffic |
| // if listener is active. |
| - void AddListener(MDnsListenerImpl* listener, bool alert_existing_records); |
| + void AddListener(MDnsListenerImpl* listener); |
| void RemoveListener(MDnsListenerImpl* listener); |
| // Query the cache for records of a specific type and name. |
| @@ -83,6 +84,8 @@ class MDnsClientImpl : public MDnsClient { |
| // Parse the response and alert relevant listeners. |
| virtual void HandlePacket(DnsResponse* response, int bytes_read) OVERRIDE; |
| + virtual void OnConnectionError(int error) OVERRIDE; |
| + |
| private: |
| typedef std::pair<uint16, std::string> ListenerKey; |
| typedef std::map<ListenerKey, ObserverList<MDnsListenerImpl>* > |
| @@ -121,23 +124,19 @@ class MDnsClientImpl : public MDnsClient { |
| explicit MDnsClientImpl(MDnsConnectionFactory* connection_factory); |
| virtual ~MDnsClientImpl(); |
| - // Add delegate for RRType |rrtype| and name |name|. |
| - // If |name| is an empty string, listen to all notification of type |
| - // |rrtype|. |
| + // MDnsClient implementation: |
| virtual scoped_ptr<MDnsListener> CreateListener( |
| uint16 rrtype, |
| const std::string& name, |
| - bool active, |
| - bool alert_existing_records, |
| MDnsListener::Delegate* delegate) OVERRIDE; |
| - // 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, |
| + int flags, |
| const MDnsTransaction::ResultCallback& callback) OVERRIDE; |
| + |
| // Functions for testing only. |
| bool IsListeningForTests(); |
| @@ -165,45 +164,26 @@ class MDnsListenerImpl : public MDnsListener, |
| public: |
| MDnsListenerImpl(uint16 rrtype, |
| const std::string& name, |
| - bool active, |
| - bool alert_existing_records, |
| MDnsListener::Delegate* delegate, |
| MDnsClientImpl* client); |
| - // Destroying the listener stops listening. |
| virtual ~MDnsListenerImpl(); |
| - // Start the listener. Returns true on success. |
| + // MDnsListener implementation: |
| virtual bool Start() OVERRIDE; |
| - // Get the host or service name for this query. |
| - // Return an empty string for no name. |
| virtual const std::string& GetName() const OVERRIDE; |
| - // Get the type for this query (SRV, TXT, A, AAA, etc) |
| virtual uint16 GetType() const OVERRIDE; |
| - virtual bool IsActive() const OVERRIDE; |
| - |
| - // 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) OVERRIDE; |
| - |
| - // 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 OVERRIDE; |
| - |
| MDnsListener::Delegate* delegate() { return delegate_; } |
| + // Alert the delegate of a record update. |
| void AlertDelegate(MDnsUpdateType update_type, |
| const RecordParsed* record_parsed); |
| private: |
| uint16 rrtype_; |
| std::string name_; |
| - bool active_; |
| - bool alert_existing_records_; |
| MDnsClientImpl* client_; |
| MDnsListener::Delegate* delegate_; |
| @@ -211,26 +191,33 @@ class MDnsListenerImpl : public MDnsListener, |
| DISALLOW_COPY_AND_ASSIGN(MDnsListenerImpl); |
| }; |
| -class MDnsTransactionImpl : public MDnsTransaction, |
| - public base::SupportsWeakPtr<MDnsTransactionImpl>, |
| +class MDnsTransactionImpl : public base::SupportsWeakPtr<MDnsTransactionImpl>, |
| + public MDnsTransaction, |
| public MDnsListener::Delegate { |
| public: |
| MDnsTransactionImpl(uint16 rrtype, |
| const std::string& name, |
| + int flags, |
| const MDnsTransaction::ResultCallback& callback, |
| MDnsClientImpl* client); |
| virtual ~MDnsTransactionImpl(); |
| - // Start the transaction. Returns true on success. |
| + // MDnsTransaction implementation: |
| virtual bool Start() OVERRIDE; |
| - // MDnsListener::Delegate implementation |
| + virtual const std::string& GetName() const OVERRIDE; |
| + virtual uint16 GetType() const OVERRIDE; |
| + |
| + // MDnsListener::Delegate implementation: |
| virtual void OnRecordUpdate(MDnsUpdateType update, |
| const RecordParsed* record) OVERRIDE; |
| virtual void OnNsecRecord(const std::string& name, unsigned type) OVERRIDE; |
| - virtual const std::string& GetName() const OVERRIDE; |
| - virtual uint16 GetType() const OVERRIDE; |
| + virtual void OnCachePurged() OVERRIDE; |
| + |
| + bool is_active() { return !callback_.is_null(); } |
| + |
| + void Reset(); |
|
szym
2013/06/10 21:58:28
Make it private.
Noam Samuel
2013/06/11 20:35:03
Done.
|
| private: |
| // Trigger the callback and reset all related variables. |
| @@ -240,8 +227,8 @@ class MDnsTransactionImpl : public MDnsTransaction, |
| // Internal callback for when a cache record is found. |
| void CacheRecordFound(const RecordParsed* record); |
| - // Callback for when the transaction times out. |
| - void OnTimedOut(); |
| + // Signal the transactionis over and release all related resources. |
| + void SignalTransactionOver(); |
| uint16 rrtype_; |
| std::string name_; |
| @@ -253,6 +240,7 @@ class MDnsTransactionImpl : public MDnsTransaction, |
| MDnsClientImpl* client_; |
| bool started_; |
| + int flags_; |
| DISALLOW_COPY_AND_ASSIGN(MDnsTransactionImpl); |
| }; |
| @@ -262,31 +250,44 @@ class MDnsTransactionImpl : public MDnsTransaction, |
| class MDnsConnectionImpl : public MDnsConnection { |
| public: |
| explicit MDnsConnectionImpl(MDnsConnection::Delegate* delegate); |
| + MDnsConnectionImpl(DatagramServerSocket* socket_ipv4, |
| + DatagramServerSocket* socket_ipv6, |
| + MDnsConnection::Delegate* delegate); |
| + |
| virtual ~MDnsConnectionImpl(); |
| - virtual bool Init() OVERRIDE; |
| - virtual bool Send(IOBuffer* buffer, unsigned size) OVERRIDE; |
| + virtual int Init() OVERRIDE; |
| + virtual int Send(IOBuffer* buffer, unsigned size) OVERRIDE; |
| private: |
| - class RecvLoop { |
| + class SocketHandler { |
| public: |
| - RecvLoop(DatagramServerSocket* socket, MDnsConnectionImpl* connection); |
| - ~RecvLoop(); |
| - void DoLoop(int rv); |
| + // Pass the socket in. Used for testing. |
| + SocketHandler(DatagramServerSocket* socket, MDnsConnectionImpl* connection, |
| + const IPEndPoint& multicast_addr); |
| + SocketHandler(MDnsConnectionImpl* connection, |
| + const IPEndPoint& multicast_addr); |
| + ~SocketHandler(); |
| + int DoLoop(int rv); |
| + int Start(); |
| + |
| + int Send(IOBuffer* buffer, unsigned size); |
| private: |
| + int BindSocket(); |
| void OnDatagramReceived(int rv); |
| + // Callback for when sending a query has finished. |
| + void SendDone(int sent); |
| + |
| + scoped_ptr<DatagramServerSocket> socket_owned_; |
| DatagramServerSocket* socket_; |
| + |
| MDnsConnectionImpl* connection_; |
| IPEndPoint recv_addr_; |
| scoped_ptr<DnsResponse> response_; |
| + IPEndPoint multicast_addr_; |
| }; |
| - // Bind a socket with a specific address size to a specific multicast group |
| - // and port 5353. |
| - bool BindSocket(DatagramServerSocket* socket, |
| - int addr_size, |
| - const char* multicast_group); |
| // Callback for handling a datagram being recieved on either ipv4 or ipv6. |
| // Responsible for ensuring we request another packet from the network. |
| @@ -295,20 +296,12 @@ class MDnsConnectionImpl : public MDnsConnection { |
| IPEndPoint* recv_addr, |
| int bytes_read); |
| - void OnError(RecvLoop* loop, DatagramServerSocket* socket, int error); |
| - |
| - // Callback for when sending a query has finished. |
| - void SendDone(int sent); |
| - |
| - // The IPEndPoints for sending/recieving packets on IPv4 and IPv6. |
| - IPEndPoint GetIPv4SendEndpoint(); |
| - IPEndPoint GetIPv6SendEndpoint(); |
| + void OnError(SocketHandler* loop, int error); |
| - scoped_ptr<DatagramServerSocket> socket_ipv4_; |
| - scoped_ptr<DatagramServerSocket> socket_ipv6_; |
| + IPEndPoint GetIPEndPoint(const char* address, int port); |
| - RecvLoop loop_ipv4_; |
| - RecvLoop loop_ipv6_; |
| + SocketHandler socket_handler_ipv4_; |
| + SocketHandler socket_handler_ipv6_; |
| MDnsConnection::Delegate* delegate_; |