Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / renderer / p2p / host_address_request.cc
blob5c7a891def99638140cc2b82e0ab85854b7e7f72
1 // Copyright (c) 2012 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.
5 #include "content/renderer/p2p/host_address_request.h"
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/location.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "content/common/p2p_messages.h"
13 #include "content/renderer/p2p/socket_dispatcher.h"
14 #include "jingle/glue/utils.h"
16 namespace content {
18 P2PAsyncAddressResolver::P2PAsyncAddressResolver(
19 P2PSocketDispatcher* dispatcher)
20 : dispatcher_(dispatcher),
21 ipc_task_runner_(dispatcher->task_runner()),
22 delegate_task_runner_(base::ThreadTaskRunnerHandle::Get()),
23 state_(STATE_CREATED),
24 request_id_(0),
25 registered_(false) {
26 AddRef(); // Balanced in Destroy().
29 P2PAsyncAddressResolver::~P2PAsyncAddressResolver() {
30 DCHECK(state_ == STATE_CREATED || state_ == STATE_FINISHED);
31 DCHECK(!registered_);
34 void P2PAsyncAddressResolver::Start(const rtc::SocketAddress& host_name,
35 const DoneCallback& done_callback) {
36 DCHECK(delegate_task_runner_->BelongsToCurrentThread());
37 DCHECK_EQ(STATE_CREATED, state_);
39 state_ = STATE_SENT;
40 ipc_task_runner_->PostTask(
41 FROM_HERE, base::Bind(&P2PAsyncAddressResolver::DoSendRequest, this,
42 host_name, done_callback));
45 void P2PAsyncAddressResolver::Cancel() {
46 DCHECK(delegate_task_runner_->BelongsToCurrentThread());
48 if (state_ != STATE_FINISHED) {
49 state_ = STATE_FINISHED;
50 ipc_task_runner_->PostTask(
51 FROM_HERE, base::Bind(&P2PAsyncAddressResolver::DoUnregister, this));
53 done_callback_.Reset();
56 void P2PAsyncAddressResolver::DoSendRequest(
57 const rtc::SocketAddress& host_name,
58 const DoneCallback& done_callback) {
59 DCHECK(ipc_task_runner_->BelongsToCurrentThread());
61 done_callback_ = done_callback;
62 request_id_ = dispatcher_->RegisterHostAddressRequest(this);
63 registered_ = true;
64 dispatcher_->SendP2PMessage(
65 new P2PHostMsg_GetHostAddress(host_name.hostname(), request_id_));
68 void P2PAsyncAddressResolver::DoUnregister() {
69 DCHECK(ipc_task_runner_->BelongsToCurrentThread());
70 if (registered_) {
71 dispatcher_->UnregisterHostAddressRequest(request_id_);
72 registered_ = false;
76 void P2PAsyncAddressResolver::OnResponse(const net::IPAddressList& addresses) {
77 DCHECK(ipc_task_runner_->BelongsToCurrentThread());
78 DCHECK(registered_);
80 dispatcher_->UnregisterHostAddressRequest(request_id_);
81 registered_ = false;
83 delegate_task_runner_->PostTask(
84 FROM_HERE,
85 base::Bind(&P2PAsyncAddressResolver::DeliverResponse, this, addresses));
88 void P2PAsyncAddressResolver::DeliverResponse(
89 const net::IPAddressList& addresses) {
90 DCHECK(delegate_task_runner_->BelongsToCurrentThread());
91 if (state_ == STATE_SENT) {
92 state_ = STATE_FINISHED;
93 base::ResetAndReturn(&done_callback_).Run(addresses);
97 } // namespace content