Commit 8d36e829 authored by Antonio Gomes's avatar Antonio Gomes Committed by Commit Bot

s13n: Expose an API to get the total of decompressed bytes of a response body

While migrating some classes from URLFetcher to SimpleURLLoader,
it is useful to have the size of the resulting decompressed body.

In case of DownloadToString* calls, this is straightfoward:
the length to the string is the size.
However, in the case of DownloadTo{Temp}File calls, where only the
resulting file path is passed to the completion callback, having
an API to get the decompressed body size is useful. It also avoids
sync calls to access the file data.

This CL adds a SimpleURLLoader::GetContentSize API that provides such
value. When used with SimpleURLLloader::SetAllowPartialResults(true),
the API returns the total decompressed body size even in the case of
errors.

BUG=773295,844972

Cq-Include-Trybots: luci.chromium.try:linux_mojo
Change-Id: Ia8cdb3326b5d5c74276e384f966a7e98f61a8c46
Reviewed-on: https://chromium-review.googlesource.com/1167683Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Commit-Queue: Matt Menke <mmenke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#581952}
parent bef3de4c
......@@ -219,6 +219,7 @@ class SimpleURLLoaderImpl : public SimpleURLLoader,
const ResourceResponseHead* ResponseInfo() const override;
const GURL& GetFinalURL() const override;
bool LoadedFromCache() const override;
int64_t GetContentSize() const override;
// Called by BodyHandler when the BodyHandler body handler is done. If |error|
// is not net::OK, some error occurred reading or consuming the body. If it is
......@@ -1274,6 +1275,12 @@ bool SimpleURLLoaderImpl::LoadedFromCache() const {
return request_state_->loaded_from_cache;
}
int64_t SimpleURLLoaderImpl::GetContentSize() const {
// Should only be called once the request is compelete.
DCHECK(request_state_->finished);
return request_state_->received_body_size;
}
const ResourceResponseHead* SimpleURLLoaderImpl::ResponseInfo() const {
// Should only be called once the request is compelete.
DCHECK(request_state_->finished);
......@@ -1288,6 +1295,12 @@ void SimpleURLLoaderImpl::OnBodyHandlerDone(net::Error error,
// If there's an error, fail request and report it immediately.
if (error != net::OK) {
// When |allow_partial_results_| is true, a valid body|file_path is
// passed to the completion callback even in the case of failures.
// For consistency, it makes sense to also hold the actual decompressed
// body size in case GetContentSize is called.
if (allow_partial_results_)
request_state_->received_body_size = received_body_size;
FinishWithResult(error);
return;
}
......
......@@ -303,6 +303,18 @@ class COMPONENT_EXPORT(NETWORK_CPP) SimpleURLLoader {
// completion.
virtual bool LoadedFromCache() const = 0;
// Indicates the total of decompressed bytes of the response body.
// May only be called once the loader has informed the caller of completion.
//
// The value might be different than the number of bytes actually
// received over the network. This happens, for example, in the case
// of gzipped bodies (Content-Encoding: gzip).
//
// When |SetAllowPartialResults| is set to true and there is an error,
// the method returns the total bytes decompressed bytes until the failure
// occurred.
virtual int64_t GetContentSize() const = 0;
protected:
SimpleURLLoader();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment