| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/p2p/host_address_request.h" | 5 #include "content/renderer/p2p/host_address_request.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
| 10 #include "content/common/p2p_messages.h" | 10 #include "content/common/p2p_messages.h" |
| 11 #include "content/renderer/p2p/socket_dispatcher.h" | 11 #include "content/renderer/p2p/socket_dispatcher.h" |
| 12 #include "jingle/glue/utils.h" | 12 #include "jingle/glue/utils.h" |
| 13 | 13 |
| 14 namespace content { | 14 namespace content { |
| 15 | 15 |
| 16 P2PAsyncAddressResolver::P2PAsyncAddressResolver( | 16 P2PAsyncAddressResolver::P2PAsyncAddressResolver( |
| 17 P2PSocketDispatcher* dispatcher) | 17 P2PSocketDispatcher* dispatcher) |
| 18 : dispatcher_(dispatcher), | 18 : dispatcher_(dispatcher), |
| 19 ipc_message_loop_(dispatcher->message_loop()), | 19 ipc_task_runner_(dispatcher->task_runner()), |
| 20 delegate_message_loop_(base::MessageLoopProxy::current()), | 20 delegate_message_loop_(base::MessageLoopProxy::current()), |
| 21 state_(STATE_CREATED), | 21 state_(STATE_CREATED), |
| 22 request_id_(0), | 22 request_id_(0), |
| 23 registered_(false) { | 23 registered_(false) { |
| 24 AddRef(); // Balanced in Destroy(). | 24 AddRef(); // Balanced in Destroy(). |
| 25 } | 25 } |
| 26 | 26 |
| 27 P2PAsyncAddressResolver::~P2PAsyncAddressResolver() { | 27 P2PAsyncAddressResolver::~P2PAsyncAddressResolver() { |
| 28 DCHECK(state_ == STATE_CREATED || state_ == STATE_FINISHED); | 28 DCHECK(state_ == STATE_CREATED || state_ == STATE_FINISHED); |
| 29 DCHECK(!registered_); | 29 DCHECK(!registered_); |
| 30 } | 30 } |
| 31 | 31 |
| 32 void P2PAsyncAddressResolver::Start(const rtc::SocketAddress& host_name, | 32 void P2PAsyncAddressResolver::Start(const rtc::SocketAddress& host_name, |
| 33 const DoneCallback& done_callback) { | 33 const DoneCallback& done_callback) { |
| 34 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 34 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 35 DCHECK_EQ(STATE_CREATED, state_); | 35 DCHECK_EQ(STATE_CREATED, state_); |
| 36 | 36 |
| 37 state_ = STATE_SENT; | 37 state_ = STATE_SENT; |
| 38 ipc_message_loop_->PostTask(FROM_HERE, base::Bind( | 38 ipc_task_runner_->PostTask( |
| 39 &P2PAsyncAddressResolver::DoSendRequest, this, host_name, done_callback)); | 39 FROM_HERE, base::Bind(&P2PAsyncAddressResolver::DoSendRequest, this, |
| 40 host_name, done_callback)); |
| 40 } | 41 } |
| 41 | 42 |
| 42 void P2PAsyncAddressResolver::Cancel() { | 43 void P2PAsyncAddressResolver::Cancel() { |
| 43 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 44 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 44 | 45 |
| 45 if (state_ != STATE_FINISHED) { | 46 if (state_ != STATE_FINISHED) { |
| 46 state_ = STATE_FINISHED; | 47 state_ = STATE_FINISHED; |
| 47 ipc_message_loop_->PostTask(FROM_HERE, base::Bind( | 48 ipc_task_runner_->PostTask( |
| 48 &P2PAsyncAddressResolver::DoUnregister, this)); | 49 FROM_HERE, base::Bind(&P2PAsyncAddressResolver::DoUnregister, this)); |
| 49 } | 50 } |
| 50 done_callback_.Reset(); | 51 done_callback_.Reset(); |
| 51 } | 52 } |
| 52 | 53 |
| 53 void P2PAsyncAddressResolver::DoSendRequest( | 54 void P2PAsyncAddressResolver::DoSendRequest( |
| 54 const rtc::SocketAddress& host_name, | 55 const rtc::SocketAddress& host_name, |
| 55 const DoneCallback& done_callback) { | 56 const DoneCallback& done_callback) { |
| 56 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 57 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 57 | 58 |
| 58 done_callback_ = done_callback; | 59 done_callback_ = done_callback; |
| 59 request_id_ = dispatcher_->RegisterHostAddressRequest(this); | 60 request_id_ = dispatcher_->RegisterHostAddressRequest(this); |
| 60 registered_ = true; | 61 registered_ = true; |
| 61 dispatcher_->SendP2PMessage( | 62 dispatcher_->SendP2PMessage( |
| 62 new P2PHostMsg_GetHostAddress(host_name.hostname(), request_id_)); | 63 new P2PHostMsg_GetHostAddress(host_name.hostname(), request_id_)); |
| 63 } | 64 } |
| 64 | 65 |
| 65 void P2PAsyncAddressResolver::DoUnregister() { | 66 void P2PAsyncAddressResolver::DoUnregister() { |
| 66 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 67 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 67 if (registered_) { | 68 if (registered_) { |
| 68 dispatcher_->UnregisterHostAddressRequest(request_id_); | 69 dispatcher_->UnregisterHostAddressRequest(request_id_); |
| 69 registered_ = false; | 70 registered_ = false; |
| 70 } | 71 } |
| 71 } | 72 } |
| 72 | 73 |
| 73 void P2PAsyncAddressResolver::OnResponse(const net::IPAddressList& addresses) { | 74 void P2PAsyncAddressResolver::OnResponse(const net::IPAddressList& addresses) { |
| 74 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | 75 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 75 DCHECK(registered_); | 76 DCHECK(registered_); |
| 76 | 77 |
| 77 dispatcher_->UnregisterHostAddressRequest(request_id_); | 78 dispatcher_->UnregisterHostAddressRequest(request_id_); |
| 78 registered_ = false; | 79 registered_ = false; |
| 79 | 80 |
| 80 delegate_message_loop_->PostTask(FROM_HERE, base::Bind( | 81 delegate_message_loop_->PostTask(FROM_HERE, base::Bind( |
| 81 &P2PAsyncAddressResolver::DeliverResponse, this, addresses)); | 82 &P2PAsyncAddressResolver::DeliverResponse, this, addresses)); |
| 82 } | 83 } |
| 83 | 84 |
| 84 void P2PAsyncAddressResolver::DeliverResponse( | 85 void P2PAsyncAddressResolver::DeliverResponse( |
| 85 const net::IPAddressList& addresses) { | 86 const net::IPAddressList& addresses) { |
| 86 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 87 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
| 87 if (state_ == STATE_SENT) { | 88 if (state_ == STATE_SENT) { |
| 88 state_ = STATE_FINISHED; | 89 state_ = STATE_FINISHED; |
| 89 base::ResetAndReturn(&done_callback_).Run(addresses); | 90 base::ResetAndReturn(&done_callback_).Run(addresses); |
| 90 } | 91 } |
| 91 } | 92 } |
| 92 | 93 |
| 93 } // namespace content | 94 } // namespace content |
| OLD | NEW |