summaryrefslogtreecommitdiffstats
path: root/content/renderer/p2p/host_address_request.h
blob: 9a3de71844ead568af1456f87191d2e962bf3a53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) 2011 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 CONTENT_RENDERER_P2P_HOST_ADDRESS_REQUEST_H_
#define CONTENT_RENDERER_P2P_HOST_ADDRESS_REQUEST_H_

#include <string>

#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "content/common/content_export.h"
#include "net/base/net_util.h"

namespace base {
class MessageLoopProxy;
}  // namespace base

namespace content {

class P2PSocketDispatcher;

// P2PHostAddressRequest performs DNS hostname resolution. It's used
// to resolve addresses of STUN and relay servers.
//
// TODO(sergeyu): Name of this class may be confusing. Rename it to
// something else, e.g. P2PHostnameResolver.
class CONTENT_EXPORT P2PHostAddressRequest
    : public base::RefCountedThreadSafe<P2PHostAddressRequest>  {
 public:
  typedef base::Callback<void(const net::IPAddressNumber&)> DoneCallback;

  P2PHostAddressRequest(P2PSocketDispatcher* dispatcher);

  // Sends host address request.
  void Request(const std::string& host_name,
               const DoneCallback& done_callback);

  // Cancels the request. The callback passed to Request() will not be
  // called after Cancel() is called.
  void Cancel();

 private:
  enum State {
    STATE_CREATED,
    STATE_SENT,
    STATE_FINISHED,
  };

  friend class P2PSocketDispatcher;

  friend class base::RefCountedThreadSafe<P2PHostAddressRequest>;
  virtual ~P2PHostAddressRequest();

  void DoSendRequest(const std::string& host_name,
                     const DoneCallback& done_callback);
  void DoUnregister();
  void OnResponse(const net::IPAddressNumber& address);
  void DeliverResponse(const net::IPAddressNumber& address);

  P2PSocketDispatcher* dispatcher_;
  scoped_refptr<base::MessageLoopProxy> ipc_message_loop_;
  scoped_refptr<base::MessageLoopProxy> delegate_message_loop_;
  DoneCallback done_callback_;

  // State must be accessed from delegate thread only.
  State state_;

  // Accessed on the IPC thread only.
  int32 request_id_;
  bool registered_;

  DISALLOW_COPY_AND_ASSIGN(P2PHostAddressRequest);
};

}  // namespace content

#endif  // CONTENT_RENDERER_P2P_HOST_ADDRESS_REQUEST_H_