Commit 017c9bd3 authored by Yi Su's avatar Yi Su Committed by Commit Bot

Add allow_credential parameter to ImageDataFetcher and IOSImageDataFetcherWrapper

add an extra boolean arg to API in ImageDataFetcher and IOSImageDataFetcherWrapper
to control cookies usage in requests.

Change-Id: I1142ecf3b595f8ee060d28985183d28e0cdef218
Reviewed-on: https://chromium-review.googlesource.com/1174831Reviewed-by: default avatarMarkus Heintz <markusheintz@chromium.org>
Commit-Queue: Yi Su <mrsuyi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584419}
parent afb1ab4d
...@@ -67,11 +67,12 @@ void ImageDataFetcher::SetImageDownloadLimit( ...@@ -67,11 +67,12 @@ void ImageDataFetcher::SetImageDownloadLimit(
void ImageDataFetcher::FetchImageData( void ImageDataFetcher::FetchImageData(
const GURL& image_url, const GURL& image_url,
ImageDataFetcherCallback callback, ImageDataFetcherCallback callback,
const net::NetworkTrafficAnnotationTag& traffic_annotation) { const net::NetworkTrafficAnnotationTag& traffic_annotation,
bool send_cookies) {
FetchImageData( FetchImageData(
image_url, std::move(callback), /*referrer=*/std::string(), image_url, std::move(callback), /*referrer=*/std::string(),
net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
traffic_annotation); traffic_annotation, send_cookies);
} }
void ImageDataFetcher::FetchImageData( void ImageDataFetcher::FetchImageData(
...@@ -79,15 +80,18 @@ void ImageDataFetcher::FetchImageData( ...@@ -79,15 +80,18 @@ void ImageDataFetcher::FetchImageData(
ImageDataFetcherCallback callback, ImageDataFetcherCallback callback,
const std::string& referrer, const std::string& referrer,
net::URLRequest::ReferrerPolicy referrer_policy, net::URLRequest::ReferrerPolicy referrer_policy,
const net::NetworkTrafficAnnotationTag& traffic_annotation) { const net::NetworkTrafficAnnotationTag& traffic_annotation,
bool send_cookies) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
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;
request->referrer = GURL(referrer); request->referrer = GURL(referrer);
request->load_flags = net::LOAD_DO_NOT_SEND_COOKIES | if (!send_cookies) {
net::LOAD_DO_NOT_SAVE_COOKIES | request->load_flags = net::LOAD_DO_NOT_SEND_COOKIES |
net::LOAD_DO_NOT_SEND_AUTH_DATA; net::LOAD_DO_NOT_SAVE_COOKIES |
net::LOAD_DO_NOT_SEND_AUTH_DATA;
}
// TODO(https://crbug.com/808498) re-add data use measurement once // TODO(https://crbug.com/808498) re-add data use measurement once
// SimpleURLLoader supports it. Parameter: // SimpleURLLoader supports it. Parameter:
......
...@@ -49,7 +49,8 @@ class ImageDataFetcher { ...@@ -49,7 +49,8 @@ class ImageDataFetcher {
void FetchImageData( void FetchImageData(
const GURL& image_url, const GURL& image_url,
ImageDataFetcherCallback callback, ImageDataFetcherCallback callback,
const net::NetworkTrafficAnnotationTag& traffic_annotation); const net::NetworkTrafficAnnotationTag& traffic_annotation,
bool send_cookies = false);
// Like above, but lets the caller set a referrer. // Like above, but lets the caller set a referrer.
void FetchImageData( void FetchImageData(
...@@ -57,7 +58,8 @@ class ImageDataFetcher { ...@@ -57,7 +58,8 @@ class ImageDataFetcher {
ImageDataFetcherCallback callback, ImageDataFetcherCallback callback,
const std::string& referrer, const std::string& referrer,
net::URLRequest::ReferrerPolicy referrer_policy, net::URLRequest::ReferrerPolicy referrer_policy,
const net::NetworkTrafficAnnotationTag& traffic_annotation); const net::NetworkTrafficAnnotationTag& traffic_annotation,
bool send_cookies = false);
// Test-only method to inject a fetch result directly, w/o regard for how the // Test-only method to inject a fetch result directly, w/o regard for how the
// underlying loading is doing. This requires there to be a single pending // underlying loading is doing. This requires there to be a single pending
......
...@@ -97,6 +97,42 @@ TEST_F(ImageDataFetcherTest, FetchImageData) { ...@@ -97,6 +97,42 @@ TEST_F(ImageDataFetcherTest, FetchImageData) {
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
} }
TEST_F(ImageDataFetcherTest, FetchImageDataWithCookies) {
std::string content = kURLResponseData;
image_data_fetcher_.FetchImageData(
GURL(kImageURL),
base::BindOnce(&ImageDataFetcherTest::OnImageDataFetched,
base::Unretained(this)),
TRAFFIC_ANNOTATION_FOR_TESTS, true);
RequestMetadata expected_metadata;
expected_metadata.mime_type = std::string("image/png");
expected_metadata.http_response_code = net::HTTP_OK;
EXPECT_CALL(*this, OnImageDataFetched(content, expected_metadata));
// Check to make sure the request is pending with proper flags, and
// provide a response.
int pending_load_flags = 0;
EXPECT_TRUE(
test_url_loader_factory_.IsPending(kImageURL, &pending_load_flags));
EXPECT_FALSE(pending_load_flags & net::LOAD_DO_NOT_SEND_COOKIES);
EXPECT_FALSE(pending_load_flags & net::LOAD_DO_NOT_SAVE_COOKIES);
EXPECT_FALSE(pending_load_flags & net::LOAD_DO_NOT_SEND_AUTH_DATA);
network::ResourceResponseHead head;
std::string raw_header =
"HTTP/1.1 200 OK\n"
"Content-type: image/png\n\n";
head.headers = new net::HttpResponseHeaders(
net::HttpUtil::AssembleRawHeaders(raw_header.c_str(), raw_header.size()));
head.mime_type = "image/png";
network::URLLoaderCompletionStatus status;
status.decoded_body_length = content.size();
test_url_loader_factory_.AddResponse(GURL(kImageURL), head, content, status);
base::RunLoop().RunUntilIdle();
}
TEST_F(ImageDataFetcherTest, FetchImageData_NotFound) { TEST_F(ImageDataFetcherTest, FetchImageData_NotFound) {
std::string content = kURLResponseData; std::string content = kURLResponseData;
......
...@@ -29,8 +29,9 @@ class IOSImageDataFetcherWrapper { ...@@ -29,8 +29,9 @@ class IOSImageDataFetcherWrapper {
// Helper to start downloading and possibly decoding the image without a // Helper to start downloading and possibly decoding the image without a
// referrer. // referrer.
virtual void FetchImageDataWebpDecoded(const GURL& image_url, void FetchImageDataWebpDecoded(const GURL& image_url,
ImageDataFetcherBlock callback); ImageDataFetcherBlock callback,
bool send_cookies = false);
// Start downloading the image at the given |image_url|. The |callback| will // Start downloading the image at the given |image_url|. The |callback| will
// be called with the downloaded image, or nil if any error happened. If the // be called with the downloaded image, or nil if any error happened. If the
...@@ -42,7 +43,8 @@ class IOSImageDataFetcherWrapper { ...@@ -42,7 +43,8 @@ class IOSImageDataFetcherWrapper {
const GURL& image_url, const GURL& image_url,
ImageDataFetcherBlock callback, ImageDataFetcherBlock callback,
const std::string& referrer, const std::string& referrer,
net::URLRequest::ReferrerPolicy referrer_policy); net::URLRequest::ReferrerPolicy referrer_policy,
bool send_cookies = false);
// Sets a service name against which to track data usage. // Sets a service name against which to track data usage.
void SetDataUseServiceName(DataUseServiceName data_use_service_name); void SetDataUseServiceName(DataUseServiceName data_use_service_name);
......
...@@ -29,22 +29,24 @@ IOSImageDataFetcherWrapper::~IOSImageDataFetcherWrapper() {} ...@@ -29,22 +29,24 @@ IOSImageDataFetcherWrapper::~IOSImageDataFetcherWrapper() {}
void IOSImageDataFetcherWrapper::FetchImageDataWebpDecoded( void IOSImageDataFetcherWrapper::FetchImageDataWebpDecoded(
const GURL& image_url, const GURL& image_url,
ImageDataFetcherBlock callback) { ImageDataFetcherBlock callback,
bool send_cookies) {
image_data_fetcher_.FetchImageData(image_url, image_data_fetcher_.FetchImageData(image_url,
CallbackForImageDataFetcher(callback), CallbackForImageDataFetcher(callback),
NO_TRAFFIC_ANNOTATION_YET); NO_TRAFFIC_ANNOTATION_YET, send_cookies);
} }
void IOSImageDataFetcherWrapper::FetchImageDataWebpDecoded( void IOSImageDataFetcherWrapper::FetchImageDataWebpDecoded(
const GURL& image_url, const GURL& image_url,
ImageDataFetcherBlock callback, ImageDataFetcherBlock callback,
const std::string& referrer, const std::string& referrer,
net::URLRequest::ReferrerPolicy referrer_policy) { net::URLRequest::ReferrerPolicy referrer_policy,
bool send_cookies) {
DCHECK(callback); DCHECK(callback);
image_data_fetcher_.FetchImageData( image_data_fetcher_.FetchImageData(
image_url, CallbackForImageDataFetcher(callback), referrer, image_url, CallbackForImageDataFetcher(callback), referrer,
referrer_policy, NO_TRAFFIC_ANNOTATION_YET); referrer_policy, NO_TRAFFIC_ANNOTATION_YET, send_cookies);
} }
void IOSImageDataFetcherWrapper::SetDataUseServiceName( void IOSImageDataFetcherWrapper::SetDataUseServiceName(
......
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