Commit 1f323f2f authored by Brandon Wylie's avatar Brandon Wylie Committed by Commit Bot

[Image Fetcher] Add data url support

Bug: 1015174
Change-Id: I356619e74745a31ebd0aef46f64977a025dd099b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2078978
Commit-Queue: Brandon Wylie <wylieb@chromium.org>
Reviewed-by: default avatarSky Malice <skym@chromium.org>
Cr-Commit-Position: refs/heads/master@{#746143}
parent 3ac88832
...@@ -7,7 +7,9 @@ ...@@ -7,7 +7,9 @@
#include <utility> #include <utility>
#include "base/bind.h" #include "base/bind.h"
#include "base/task/post_task.h"
#include "components/image_fetcher/core/image_fetcher_metrics_reporter.h" #include "components/image_fetcher/core/image_fetcher_metrics_reporter.h"
#include "net/base/data_url.h"
#include "net/base/load_flags.h" #include "net/base/load_flags.h"
#include "net/http/http_response_headers.h" #include "net/http/http_response_headers.h"
#include "net/http/http_status_code.h" #include "net/http/http_status_code.h"
...@@ -16,6 +18,7 @@ ...@@ -16,6 +18,7 @@
#include "services/network/public/cpp/shared_url_loader_factory.h" #include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h" #include "services/network/public/cpp/simple_url_loader.h"
#include "url/gurl.h" #include "url/gurl.h"
#include "url/url_constants.h"
namespace { namespace {
...@@ -103,6 +106,21 @@ void ImageDataFetcher::FetchImageData( ...@@ -103,6 +106,21 @@ void ImageDataFetcher::FetchImageData(
net::URLRequest::ReferrerPolicy referrer_policy, net::URLRequest::ReferrerPolicy referrer_policy,
bool send_cookies) { bool send_cookies) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Handle data urls explicitly since SimpleURLLoader doesn't.
if (image_url.SchemeIs(url::kDataScheme)) {
std::string mime_type, charset, data;
if (!net::DataURL::Parse(image_url, &mime_type, &charset, &data)) {
DVLOG(0) << "Failed to parse data url";
}
// Post a task to maintain our guarantee that the call won't be called
// synchronously.
base::PostTask(FROM_HERE, BindOnce(std::move(callback), std::move(data),
RequestMetadata()));
return;
}
auto request = std::make_unique<network::ResourceRequest>(); auto request = std::make_unique<network::ResourceRequest>();
request->url = image_url; request->url = image_url;
request->referrer_policy = referrer_policy; request->referrer_policy = referrer_policy;
......
...@@ -42,7 +42,8 @@ class ImageDataFetcher { ...@@ -42,7 +42,8 @@ class ImageDataFetcher {
// Fetches the raw image bytes from the given |image_url| and calls the given // Fetches the raw image bytes from the given |image_url| and calls the given
// |callback|. The callback is run even if fetching the URL fails. In case // |callback|. The callback is run even if fetching the URL fails. In case
// of an error an empty string is passed to the callback. // of an error an empty string is passed to the callback. Won't return
// synchronously.
void FetchImageData(const GURL& image_url, void FetchImageData(const GURL& image_url,
ImageDataFetcherCallback callback, ImageDataFetcherCallback callback,
ImageFetcherParams params, ImageFetcherParams params,
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <memory> #include <memory>
#include "base/base64.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/callback.h" #include "base/callback.h"
#include "base/macros.h" #include "base/macros.h"
...@@ -59,7 +60,7 @@ class ImageDataFetcherTest : public testing::Test { ...@@ -59,7 +60,7 @@ class ImageDataFetcherTest : public testing::Test {
void(const std::string&, const RequestMetadata&)); void(const std::string&, const RequestMetadata&));
protected: protected:
base::test::SingleThreadTaskEnvironment task_environment_; base::test::TaskEnvironment task_environment_;
base::HistogramTester histogram_tester_; base::HistogramTester histogram_tester_;
network::TestURLLoaderFactory test_url_loader_factory_; network::TestURLLoaderFactory test_url_loader_factory_;
...@@ -108,6 +109,25 @@ TEST_F(ImageDataFetcherTest, FetchImageData) { ...@@ -108,6 +109,25 @@ TEST_F(ImageDataFetcherTest, FetchImageData) {
histogram_tester().ExpectBucketCount(std::string(kHistogramName), 200, 1); histogram_tester().ExpectBucketCount(std::string(kHistogramName), 200, 1);
} }
TEST_F(ImageDataFetcherTest, FetchImageDataWithDataUrl) {
std::string data =
"iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAFElEQVQYlWNk+M/"
"wn4GBgYGJAQoAHhgCAh6X4CYAAAAASUVORK5CYII=";
std::string data_url = "data:image/png;base64," + data;
image_data_fetcher_.FetchImageData(
GURL(data_url),
base::BindOnce(&ImageDataFetcherTest::OnImageDataFetched,
base::Unretained(this)),
ImageFetcherParams(TRAFFIC_ANNOTATION_FOR_TESTS, kTestUmaClientName));
RequestMetadata expected_metadata;
std::string expected;
base::Base64Decode(data, &expected);
EXPECT_CALL(*this, OnImageDataFetched(expected, expected_metadata));
base::RunLoop().RunUntilIdle();
}
TEST_F(ImageDataFetcherTest, FetchImageDataTrafficAnnotationOnly) { TEST_F(ImageDataFetcherTest, FetchImageDataTrafficAnnotationOnly) {
std::string content = kURLResponseData; std::string content = kURLResponseData;
......
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