Commit 82271d29 authored by Matt Menke's avatar Matt Menke Committed by Commit Bot

SimpleURLLoader: Add download to file support.

The file is written to on another thread, and the data pipe is also
read from off the consumer's thread, so this should be save to use on
named threads without causing significant jank.

Bug: 746977
Change-Id: I8e38945a569e4cd97ff4381ecf43acc73c731cea
Reviewed-on: https://chromium-review.googlesource.com/673043Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Reviewed-by: default avatarRandy Smith <rdsmith@chromium.org>
Commit-Queue: Matt Menke <mmenke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#504880}
parent b42f9674
This diff is collapsed.
...@@ -7,12 +7,17 @@ ...@@ -7,12 +7,17 @@
#include <stdint.h> #include <stdint.h>
#include <limits>
#include <memory> #include <memory>
#include <string> #include <string>
#include "base/callback_forward.h" #include "base/callback_forward.h"
#include "content/common/content_export.h" #include "content/common/content_export.h"
namespace base {
class FilePath;
}
namespace net { namespace net {
struct NetworkTrafficAnnotationTag; struct NetworkTrafficAnnotationTag;
} // namespace net } // namespace net
...@@ -53,6 +58,11 @@ class CONTENT_EXPORT SimpleURLLoader { ...@@ -53,6 +58,11 @@ class CONTENT_EXPORT SimpleURLLoader {
using BodyAsStringCallback = using BodyAsStringCallback =
base::OnceCallback<void(std::unique_ptr<std::string> response_body)>; base::OnceCallback<void(std::unique_ptr<std::string> response_body)>;
// Callback used when download the response body to a file. On failure, |path|
// will be empty.
using DownloadToFileCompleteCallback =
base::OnceCallback<void(const base::FilePath& path)>;
static std::unique_ptr<SimpleURLLoader> Create(); static std::unique_ptr<SimpleURLLoader> Create();
virtual ~SimpleURLLoader(); virtual ~SimpleURLLoader();
...@@ -65,7 +75,9 @@ class CONTENT_EXPORT SimpleURLLoader { ...@@ -65,7 +75,9 @@ class CONTENT_EXPORT SimpleURLLoader {
// or consume the data as it is received. // or consume the data as it is received.
// //
// Whether the request succeeds or fails, or the body exceeds |max_body_size|, // Whether the request succeeds or fails, or the body exceeds |max_body_size|,
// |body_as_string_callback| will be invoked on completion. // |body_as_string_callback| will be invoked on completion. Deleting the
// SimpleURLLoader before the callback is invoked will return in cancelling
// the request, and the callback will not be called.
virtual void DownloadToString( virtual void DownloadToString(
const ResourceRequest& resource_request, const ResourceRequest& resource_request,
mojom::URLLoaderFactory* url_loader_factory, mojom::URLLoaderFactory* url_loader_factory,
...@@ -75,13 +87,36 @@ class CONTENT_EXPORT SimpleURLLoader { ...@@ -75,13 +87,36 @@ class CONTENT_EXPORT SimpleURLLoader {
// Same as DownloadToString, but downloads to a buffer of unbounded size, // Same as DownloadToString, but downloads to a buffer of unbounded size,
// potentially causing a crash if the amount of addressable memory is // potentially causing a crash if the amount of addressable memory is
// exceeded. It's recommended consumers use DownloadToString instead. // exceeded. It's recommended consumers use one of the other download methods
// instead (DownloadToString if the body is expected to be of reasonable
// length, or DownloadToFile otherwise).
virtual void DownloadToStringOfUnboundedSizeUntilCrashAndDie( virtual void DownloadToStringOfUnboundedSizeUntilCrashAndDie(
const ResourceRequest& resource_request, const ResourceRequest& resource_request,
mojom::URLLoaderFactory* url_loader_factory, mojom::URLLoaderFactory* url_loader_factory,
const net::NetworkTrafficAnnotationTag& annotation_tag, const net::NetworkTrafficAnnotationTag& annotation_tag,
BodyAsStringCallback body_as_string_callback) = 0; BodyAsStringCallback body_as_string_callback) = 0;
// SimpleURLLoader will download the entire response to a file at the
// specified path. File I/O will happen on another sequence, so it's safe to
// use this on any sequence.
//
// If there's a file, network, or http error, or the max limit
// is exceeded, the file will be automatically destroyed before the callback
// is invoked and en empty path passed to the callback, unless
// SetAllowPartialResults() and/or SetAllowHttpErrorResults() were used to
// indicate partial results are allowed.
//
// If the SimpleURLLoader is destroyed before it has invoked the callback, the
// downloaded file will be deleted asynchronously and the callback will not be
// invoked, regardless of other settings.
virtual void DownloadToFile(
const ResourceRequest& resource_request,
mojom::URLLoaderFactory* url_loader_factory,
const net::NetworkTrafficAnnotationTag& annotation_tag,
DownloadToFileCompleteCallback download_to_file_complete_callback,
const base::FilePath& file_path,
int64_t max_body_size = std::numeric_limits<int64_t>::max()) = 0;
// Sets whether partially received results are allowed. Defaults to false. // Sets whether partially received results are allowed. Defaults to false.
// When true, if an error is received after reading the body starts or the max // When true, if an error is received after reading the body starts or the max
// allowed body size exceeded, the partial response body that was received // allowed body size exceeded, the partial response body that was received
......
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