Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 #include "content/renderer/p2p/host_address_request.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/message_loop_proxy.h" | |
| 9 #include "content/common/p2p_messages.h" | |
| 10 #include "content/renderer/p2p/socket_dispatcher.h" | |
| 11 | |
| 12 P2PHostAddressRequest::P2PHostAddressRequest(P2PSocketDispatcher* dispatcher) | |
| 13 : dispatcher_(dispatcher), | |
| 14 ipc_message_loop_(dispatcher->message_loop()), | |
| 15 delegate_message_loop_(base::MessageLoopProxy::CreateForCurrentThread()), | |
| 16 state_(STATE_CREATED), | |
| 17 request_id_(0), | |
| 18 registered_(false) { | |
| 19 } | |
| 20 | |
| 21 P2PHostAddressRequest::~P2PHostAddressRequest() { | |
|
Wez
2011/08/08 21:47:18
nit: Check that you're being torn-down on the righ
Sergey Ulanov
2011/08/09 00:41:35
Because this object is refcounted, there are not g
| |
| 22 DCHECK(state_ == STATE_CREATED || state_ == STATE_FINISHED); | |
| 23 } | |
| 24 | |
| 25 void P2PHostAddressRequest::Request(const std::string& host_name, | |
| 26 const DoneCallback& done_callback) { | |
| 27 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | |
| 28 | |
| 29 state_ = STATE_SENT; | |
| 30 ipc_message_loop_->PostTask(FROM_HERE, base::Bind( | |
| 31 &P2PHostAddressRequest::DoSendRequest, this, host_name, done_callback)); | |
|
Wez
2011/08/08 21:47:18
If the IPC message-loop has been torn down, this c
Sergey Ulanov
2011/08/09 00:41:35
The DCHECK in the destructor is supposed to check
| |
| 32 } | |
| 33 | |
| 34 void P2PHostAddressRequest::Cancel() { | |
| 35 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | |
| 36 | |
| 37 if (state_ != STATE_FINISHED) { | |
| 38 state_ = STATE_FINISHED; | |
| 39 ipc_message_loop_->PostTask(FROM_HERE, base::Bind( | |
| 40 &P2PHostAddressRequest::DoUnregister, this)); | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 void P2PHostAddressRequest::DoSendRequest(const std::string& host_name, | |
| 45 const DoneCallback& done_callback) { | |
| 46 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | |
| 47 | |
| 48 done_callback_ = done_callback; | |
| 49 request_id_ = dispatcher_->RegisterHostAddressRequest(this); | |
| 50 registered_ = true; | |
| 51 dispatcher_->SendP2PMessage( | |
| 52 new P2PHostMsg_GetHostAddress(0, host_name, request_id_)); | |
| 53 } | |
| 54 | |
| 55 void P2PHostAddressRequest::DoUnregister() { | |
| 56 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | |
| 57 if (registered_) { | |
| 58 dispatcher_->UnregisterHostAddressRequest(request_id_); | |
| 59 registered_ = false; | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 void P2PHostAddressRequest::OnResponse(const net::IPAddressNumber& address) { | |
| 64 DCHECK(ipc_message_loop_->BelongsToCurrentThread()); | |
| 65 delegate_message_loop_->PostTask(FROM_HERE, base::Bind( | |
| 66 &P2PHostAddressRequest::DeliverResponse, this, address)); | |
| 67 dispatcher_->UnregisterHostAddressRequest(request_id_); | |
| 68 registered_ = false; | |
| 69 } | |
| 70 | |
| 71 void P2PHostAddressRequest::DeliverResponse( | |
| 72 const net::IPAddressNumber& address) { | |
| 73 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | |
| 74 if (state_ == STATE_SENT) { | |
| 75 done_callback_.Run(address); | |
| 76 state_ = STATE_FINISHED; | |
| 77 } | |
| 78 } | |
| OLD | NEW |