Home | History | Annotate | Download | only in p2p
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CONTENT_RENDERER_P2P_HOST_ADDRESS_REQUEST_H_
      6 #define CONTENT_RENDERER_P2P_HOST_ADDRESS_REQUEST_H_
      7 
      8 #include <string>
      9 
     10 #include "base/callback.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "content/common/content_export.h"
     13 #include "net/base/net_util.h"
     14 
     15 namespace base {
     16 class MessageLoopProxy;
     17 }  // namespace base
     18 
     19 namespace content {
     20 
     21 class P2PSocketDispatcher;
     22 
     23 // P2PHostAddressRequest performs DNS hostname resolution. It's used
     24 // to resolve addresses of STUN and relay servers.
     25 //
     26 // TODO(sergeyu): Name of this class may be confusing. Rename it to
     27 // something else, e.g. P2PHostnameResolver.
     28 class CONTENT_EXPORT P2PHostAddressRequest
     29     : public base::RefCountedThreadSafe<P2PHostAddressRequest>  {
     30  public:
     31   typedef base::Callback<void(const net::IPAddressNumber&)> DoneCallback;
     32 
     33   P2PHostAddressRequest(P2PSocketDispatcher* dispatcher);
     34 
     35   // Sends host address request.
     36   void Request(const std::string& host_name,
     37                const DoneCallback& done_callback);
     38 
     39   // Cancels the request. The callback passed to Request() will not be
     40   // called after Cancel() is called.
     41   void Cancel();
     42 
     43  private:
     44   enum State {
     45     STATE_CREATED,
     46     STATE_SENT,
     47     STATE_FINISHED,
     48   };
     49 
     50   friend class P2PSocketDispatcher;
     51 
     52   friend class base::RefCountedThreadSafe<P2PHostAddressRequest>;
     53   virtual ~P2PHostAddressRequest();
     54 
     55   void DoSendRequest(const std::string& host_name,
     56                      const DoneCallback& done_callback);
     57   void DoUnregister();
     58   void OnResponse(const net::IPAddressNumber& address);
     59   void DeliverResponse(const net::IPAddressNumber& address);
     60 
     61   P2PSocketDispatcher* dispatcher_;
     62   scoped_refptr<base::MessageLoopProxy> ipc_message_loop_;
     63   scoped_refptr<base::MessageLoopProxy> delegate_message_loop_;
     64   DoneCallback done_callback_;
     65 
     66   // State must be accessed from delegate thread only.
     67   State state_;
     68 
     69   // Accessed on the IPC thread only.
     70   int32 request_id_;
     71   bool registered_;
     72 
     73   DISALLOW_COPY_AND_ASSIGN(P2PHostAddressRequest);
     74 };
     75 
     76 }  // namespace content
     77 
     78 #endif  // CONTENT_RENDERER_P2P_HOST_ADDRESS_REQUEST_H_
     79