Allow to cancel a proxy resolution request.
After calling GetProxiesForUrl(), there was no way to prevent the
proxy resolver from calling the passed callback once the response is
ready. This implies that the object passed in the callback (normally
as the "data" pointer) must be kept alive until the callback comes
back.
This patch allows to cancel an ongoing request and converts the passed
callback to a base::Callback instead of using a raw pointer.
Bug: 34178297
Test: Added unittests.
Change-Id: Ie544d0230fd0c2dc85c6b9eaca9b5b13702516fa
diff --git a/proxy_resolver.cc b/proxy_resolver.cc
index abd6f76..2ec59db 100644
--- a/proxy_resolver.cc
+++ b/proxy_resolver.cc
@@ -26,6 +26,7 @@
namespace chromeos_update_engine {
const char kNoProxy[] = "direct://";
+const ProxyRequestId kProxyRequestIdNull = brillo::MessageLoop::kTaskIdNull;
DirectProxyResolver::~DirectProxyResolver() {
if (idle_callback_id_ != MessageLoop::kTaskIdNull) {
@@ -39,27 +40,27 @@
}
}
-bool DirectProxyResolver::GetProxiesForUrl(const string& url,
- ProxiesResolvedFn callback,
- void* data) {
+ProxyRequestId DirectProxyResolver::GetProxiesForUrl(
+ const string& url, const ProxiesResolvedFn& callback) {
idle_callback_id_ = MessageLoop::current()->PostTask(
FROM_HERE,
- base::Bind(
- &DirectProxyResolver::ReturnCallback,
- base::Unretained(this),
- callback,
- data));
- return true;
+ base::Bind(&DirectProxyResolver::ReturnCallback,
+ base::Unretained(this),
+ callback));
+ return idle_callback_id_;
}
-void DirectProxyResolver::ReturnCallback(ProxiesResolvedFn callback,
- void* data) {
+bool DirectProxyResolver::CancelProxyRequest(ProxyRequestId request) {
+ return MessageLoop::current()->CancelTask(request);
+}
+
+void DirectProxyResolver::ReturnCallback(const ProxiesResolvedFn& callback) {
idle_callback_id_ = MessageLoop::kTaskIdNull;
// Initialize proxy pool with as many proxies as indicated (all identical).
deque<string> proxies(num_proxies_, kNoProxy);
- (*callback)(proxies, data);
+ callback.Run(proxies);
}