Fix terminating a transfer while resolving proxies.
Calling TerminateTransfer() on an HttpFetcher should cancel the current
transfer regardless of where it is sitting. If TerminateTransfer() was
called right after BeginTransfer(), the fetcher would be waiting for
the proxy resolution callback which will kick the download. This patch
fixes this case by releasing the proxy callback when terminating a
transfer.
Bug: 34178297
Test: Added a unittest to trigger this case.
Change-Id: I282d04995bd0d03f9a469c80c1e263f9902e4be2
diff --git a/common/http_fetcher.cc b/common/http_fetcher.cc
index e592cc5..4fd1082 100644
--- a/common/http_fetcher.cc
+++ b/common/http_fetcher.cc
@@ -26,10 +26,7 @@
namespace chromeos_update_engine {
HttpFetcher::~HttpFetcher() {
- if (no_resolver_idle_id_ != MessageLoop::kTaskIdNull) {
- MessageLoop::current()->CancelTask(no_resolver_idle_id_);
- no_resolver_idle_id_ = MessageLoop::kTaskIdNull;
- }
+ CancelProxyResolution();
}
void HttpFetcher::SetPostData(const void* data, size_t size,
@@ -59,23 +56,42 @@
base::Unretained(this)));
return true;
}
- return proxy_resolver_->GetProxiesForUrl(
+ proxy_request_ = proxy_resolver_->GetProxiesForUrl(
url, base::Bind(&HttpFetcher::ProxiesResolved, base::Unretained(this)));
+ if (proxy_request_ == kProxyRequestIdNull) {
+ callback_.reset();
+ return false;
+ }
+ return true;
}
void HttpFetcher::NoProxyResolverCallback() {
+ no_resolver_idle_id_ = MessageLoop::kTaskIdNull;
ProxiesResolved(deque<string>());
}
void HttpFetcher::ProxiesResolved(const deque<string>& proxies) {
- no_resolver_idle_id_ = MessageLoop::kTaskIdNull;
+ proxy_request_ = kProxyRequestIdNull;
if (!proxies.empty())
SetProxies(proxies);
- CHECK_NE(static_cast<Closure*>(nullptr), callback_.get());
+ CHECK(callback_.get()) << "ProxiesResolved but none pending.";
Closure* callback = callback_.release();
// This may indirectly call back into ResolveProxiesForUrl():
callback->Run();
delete callback;
}
+bool HttpFetcher::CancelProxyResolution() {
+ bool ret = false;
+ if (no_resolver_idle_id_ != MessageLoop::kTaskIdNull) {
+ ret = MessageLoop::current()->CancelTask(no_resolver_idle_id_);
+ no_resolver_idle_id_ = MessageLoop::kTaskIdNull;
+ }
+ if (proxy_request_ && proxy_resolver_) {
+ ret = proxy_resolver_->CancelProxyRequest(proxy_request_) || ret;
+ proxy_request_ = kProxyRequestIdNull;
+ }
+ return ret;
+}
+
} // namespace chromeos_update_engine