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/common/http_fetcher.cc b/common/http_fetcher.cc
index 400b43c..e592cc5 100644
--- a/common/http_fetcher.cc
+++ b/common/http_fetcher.cc
@@ -59,9 +59,8 @@
base::Unretained(this)));
return true;
}
- return proxy_resolver_->GetProxiesForUrl(url,
- &HttpFetcher::StaticProxiesResolved,
- this);
+ return proxy_resolver_->GetProxiesForUrl(
+ url, base::Bind(&HttpFetcher::ProxiesResolved, base::Unretained(this)));
}
void HttpFetcher::NoProxyResolverCallback() {
diff --git a/common/http_fetcher.h b/common/http_fetcher.h
index d2499eb..9f81879 100644
--- a/common/http_fetcher.h
+++ b/common/http_fetcher.h
@@ -165,10 +165,6 @@
private:
// Callback from the proxy resolver
void ProxiesResolved(const std::deque<std::string>& proxies);
- static void StaticProxiesResolved(const std::deque<std::string>& proxies,
- void* data) {
- reinterpret_cast<HttpFetcher*>(data)->ProxiesResolved(proxies);
- }
// Callback used to run the proxy resolver callback when there is no
// |proxy_resolver_|.
diff --git a/common/http_fetcher_unittest.cc b/common/http_fetcher_unittest.cc
index 0f34475..11a20e9 100644
--- a/common/http_fetcher_unittest.cc
+++ b/common/http_fetcher_unittest.cc
@@ -622,12 +622,9 @@
unique_ptr<HttpFetcher> fetcher(this->test_.NewLargeFetcher(&mock_resolver));
// Saved arguments from the proxy call.
- ProxiesResolvedFn proxy_callback = nullptr;
- void* proxy_data = nullptr;
-
- EXPECT_CALL(mock_resolver, GetProxiesForUrl("http://fake_url", _, _))
- .WillOnce(DoAll(
- SaveArg<1>(&proxy_callback), SaveArg<2>(&proxy_data), Return(true)));
+ ProxiesResolvedFn proxy_callback;
+ EXPECT_CALL(mock_resolver, GetProxiesForUrl("http://fake_url", _))
+ .WillOnce(DoAll(SaveArg<1>(&proxy_callback), Return(true)));
fetcher->BeginTransfer("http://fake_url");
testing::Mock::VerifyAndClearExpectations(&mock_resolver);
@@ -637,7 +634,7 @@
fetcher->Pause();
// Proxy resolver comes back after we paused the fetcher.
ASSERT_TRUE(proxy_callback);
- (*proxy_callback)({1, kNoProxy}, proxy_data);
+ proxy_callback.Run({1, kNoProxy});
}
namespace {