shill: Convert code to use the newest version of libchrome.
The biggest change is a switch from using the deprecated
Task and CallbackN mechanisms to using the new Callback
mechanism.
Note: Original CL was https://gerrit.chromium.org/gerrit/16156.
This is logically another patch to that CL, but since the
latter was already merged, and is considered closed by
Gerrit, it's necessary to create a new CL.
BUG=chromium-os:15330
TEST=Build shill and run it on a zgb with a modem. Build and
run unit tests.
CQ-DEPEND=I37628863370323d30cac493764ea28f8ffd42637
Change-Id: I3ae78a3aa44ec167b79f2170d07650ece888254f
Reviewed-on: https://gerrit.chromium.org/gerrit/18030
Reviewed-by: Eric Shienbrood <ers@chromium.org>
Tested-by: Eric Shienbrood <ers@chromium.org>
Commit-Ready: Eric Shienbrood <ers@chromium.org>
diff --git a/http_proxy.cc b/http_proxy.cc
index 9e5e83e..5aa1f9e 100644
--- a/http_proxy.cc
+++ b/http_proxy.cc
@@ -13,6 +13,7 @@
#include <string>
#include <vector>
+#include <base/bind.h>
#include <base/logging.h>
#include <base/string_number_conversions.h>
#include <base/string_split.h>
@@ -26,7 +27,9 @@
#include "shill/ip_address.h"
#include "shill/sockets.h"
+using base::Bind;
using base::StringPrintf;
+using base::Unretained;
using std::string;
using std::vector;
@@ -52,15 +55,15 @@
HTTPProxy::HTTPProxy(ConnectionRefPtr connection)
: state_(kStateIdle),
connection_(connection),
- accept_callback_(NewCallback(this, &HTTPProxy::AcceptClient)),
- connect_completion_callback_(
- NewCallback(this, &HTTPProxy::OnConnectCompletion)),
- dns_client_callback_(NewCallback(this, &HTTPProxy::GetDNSResult)),
- read_client_callback_(NewCallback(this, &HTTPProxy::ReadFromClient)),
- read_server_callback_(NewCallback(this, &HTTPProxy::ReadFromServer)),
- write_client_callback_(NewCallback(this, &HTTPProxy::WriteToClient)),
- write_server_callback_(NewCallback(this, &HTTPProxy::WriteToServer)),
- task_factory_(this),
+ weak_ptr_factory_(this),
+ accept_callback_(Bind(&HTTPProxy::AcceptClient, Unretained(this))),
+ connect_completion_callback_(Bind(&HTTPProxy::OnConnectCompletion,
+ Unretained(this))),
+ dns_client_callback_(Bind(&HTTPProxy::GetDNSResult, Unretained(this))),
+ read_client_callback_(Bind(&HTTPProxy::ReadFromClient, Unretained(this))),
+ read_server_callback_(Bind(&HTTPProxy::ReadFromServer, Unretained(this))),
+ write_client_callback_(Bind(&HTTPProxy::WriteToClient, Unretained(this))),
+ write_server_callback_(Bind(&HTTPProxy::WriteToServer, Unretained(this))),
dispatcher_(NULL),
dns_client_(NULL),
proxy_port_(-1),
@@ -70,8 +73,7 @@
client_socket_(-1),
server_port_(kDefaultServerPort),
server_socket_(-1),
- is_route_requested_(false),
- idle_timeout_(NULL) { }
+ is_route_requested_(false) { }
HTTPProxy::~HTTPProxy() {
Stop();
@@ -113,18 +115,18 @@
accept_handler_.reset(
dispatcher->CreateReadyHandler(proxy_socket_, IOHandler::kModeInput,
- accept_callback_.get()));
+ accept_callback_));
dispatcher_ = dispatcher;
dns_client_.reset(new DNSClient(IPAddress::kFamilyIPv4,
connection_->interface_name(),
connection_->dns_servers(),
kDNSTimeoutSeconds * 1000,
dispatcher,
- dns_client_callback_.get()));
+ dns_client_callback_));
proxy_port_ = ntohs(addr.sin_port);
server_async_connection_.reset(
new AsyncConnection(connection_->interface_name(), dispatcher, sockets,
- connect_completion_callback_.get()));
+ connect_completion_callback_));
sockets_ = sockets;
state_ = kStateWaitConnection;
return true;
@@ -169,11 +171,11 @@
sockets_->SetNonBlocking(client_socket_);
read_client_handler_.reset(
dispatcher_->CreateInputHandler(client_socket_,
- read_client_callback_.get()));
+ read_client_callback_));
// Overall transaction timeout.
- dispatcher_->PostDelayedTask(
- task_factory_.NewRunnableMethod(&HTTPProxy::StopClient),
- kTransactionTimeoutSeconds * 1000);
+ dispatcher_->PostDelayedTask(Bind(&HTTPProxy::StopClient,
+ weak_ptr_factory_.GetWeakPtr()),
+ kTransactionTimeoutSeconds * 1000);
state_ = kStateReadClientHeader;
StartIdleTimeout();
@@ -548,13 +550,12 @@
timeout_seconds = kInputTimeoutSeconds;
break;
}
- if (idle_timeout_) {
- idle_timeout_->Cancel();
- idle_timeout_ = NULL;
- }
+ idle_timeout_.Cancel();
if (timeout_seconds != 0) {
- idle_timeout_ = task_factory_.NewRunnableMethod(&HTTPProxy::StopClient);
- dispatcher_->PostDelayedTask(idle_timeout_, timeout_seconds * 1000);
+ idle_timeout_.Reset(Bind(&HTTPProxy::StopClient,
+ weak_ptr_factory_.GetWeakPtr()));
+ dispatcher_->PostDelayedTask(idle_timeout_.callback(),
+ timeout_seconds * 1000);
}
}
@@ -571,7 +572,7 @@
} else {
read_server_handler_.reset(
dispatcher_->CreateInputHandler(server_socket_,
- read_server_callback_.get()));
+ read_server_callback_));
}
} else if (state_ == kStateFlushResponse) {
StopClient();
@@ -591,7 +592,7 @@
write_server_handler_.reset(
dispatcher_->CreateReadyHandler(server_socket_,
IOHandler::kModeOutput,
- write_server_callback_.get()));
+ write_server_callback_));
}
}
if ((state_ == kStateFlushResponse || state_ == kStateTunnelData) &&
@@ -602,7 +603,7 @@
write_client_handler_.reset(
dispatcher_->CreateReadyHandler(client_socket_,
IOHandler::kModeOutput,
- write_client_callback_.get()));
+ write_client_callback_));
}
}
StartIdleTimeout();
@@ -639,8 +640,8 @@
server_data_.Clear();
dns_client_->Stop();
server_async_connection_->Stop();
- task_factory_.RevokeAll();
- idle_timeout_ = NULL;
+ idle_timeout_.Cancel();
+ weak_ptr_factory_.InvalidateWeakPtrs();
accept_handler_->Start();
state_ = kStateWaitConnection;
}