forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reland "Adding network fetcher for fetching images."
This is a reland of 7be16f9 Original change's description: > Adding network fetcher for fetching images. > > Change-Id: If4d19d6ba2367f7770c4e77c8f885767171bebe3 > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2350148 > Commit-Queue: Jonathan Freed <freedjm@chromium.org> > Reviewed-by: Dan H <harringtond@chromium.org> > Reviewed-by: Ian Wells <iwells@chromium.org> > Cr-Commit-Position: refs/heads/master@{#797494} Bug: 1115855 Change-Id: I6376cf4fb830147a093bf5a8ff301b2939c9367f Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2354452 Commit-Queue: Jonathan Freed <freedjm@chromium.org> Reviewed-by: Dan H <harringtond@chromium.org> Reviewed-by: Ian Wells <iwells@chromium.org> Reviewed-by: Martin Šrámek <msramek@chromium.org> Auto-Submit: Jonathan Freed <freedjm@chromium.org> Cr-Commit-Position: refs/heads/master@{#797899}
- Loading branch information
Jonathan Freed
authored and
Commit Bot
committed
Aug 13, 2020
1 parent
ca02dc4
commit 791b266
Showing
12 changed files
with
313 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2020 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "components/feed/core/v2/image_fetcher.h" | ||
|
||
#include "net/http/http_request_headers.h" | ||
#include "services/network/public/cpp/shared_url_loader_factory.h" | ||
#include "services/network/public/cpp/simple_url_loader.h" | ||
|
||
namespace feed { | ||
|
||
ImageFetcher::ImageFetcher( | ||
scoped_refptr<::network::SharedURLLoaderFactory> url_loader_factory) | ||
: url_loader_factory_(url_loader_factory) {} | ||
|
||
ImageFetcher::~ImageFetcher() = default; | ||
|
||
void ImageFetcher::Fetch(const GURL& url, ImageCallback callback) { | ||
net::NetworkTrafficAnnotationTag traffic_annotation = | ||
net::DefineNetworkTrafficAnnotation("interest_feedv2_image_send", R"( | ||
semantics { | ||
sender: "Feed Library" | ||
description: "Images for articles in the feed." | ||
trigger: "Triggered when viewing the feed on the NTP." | ||
data: "Request for an image containing an ID for the image and " | ||
"device specs (e.g. screen size) for resizing images." | ||
destination: GOOGLE_OWNED_SERVICE | ||
} | ||
policy { | ||
cookies_allowed: NO | ||
setting: "This can be disabled from the New Tab Page by collapsing " | ||
"the articles section." | ||
chrome_policy { | ||
NTPContentSuggestionsEnabled { | ||
policy_options {mode: MANDATORY} | ||
NTPContentSuggestionsEnabled: false | ||
} | ||
} | ||
})"); | ||
auto resource_request = std::make_unique<::network::ResourceRequest>(); | ||
resource_request->url = url; | ||
resource_request->method = net::HttpRequestHeaders::kGetMethod; | ||
resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit; | ||
|
||
auto simple_loader = network::SimpleURLLoader::Create( | ||
std::move(resource_request), traffic_annotation); | ||
simple_loader->DownloadToString( | ||
url_loader_factory_.get(), | ||
base::BindOnce(&ImageFetcher::OnFetchComplete, weak_factory_.GetWeakPtr(), | ||
std::move(simple_loader), std::move(callback)), | ||
network::SimpleURLLoader::kMaxBoundedStringDownloadSize); | ||
} | ||
|
||
void ImageFetcher::OnFetchComplete( | ||
std::unique_ptr<network::SimpleURLLoader> simple_loader, | ||
ImageCallback callback, | ||
std::unique_ptr<std::string> response_data) { | ||
std::move(callback).Run(std::move(response_data)); | ||
} | ||
|
||
} // namespace feed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2020 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef COMPONENTS_FEED_CORE_V2_IMAGE_FETCHER_H_ | ||
#define COMPONENTS_FEED_CORE_V2_IMAGE_FETCHER_H_ | ||
|
||
#include "base/callback.h" | ||
#include "base/memory/scoped_refptr.h" | ||
#include "base/memory/weak_ptr.h" | ||
#include "url/gurl.h" | ||
|
||
namespace network { | ||
class SharedURLLoaderFactory; | ||
class SimpleURLLoader; | ||
} // namespace network | ||
|
||
namespace feed { | ||
|
||
// Fetcher object to retrieve an image resource from a URL. | ||
class ImageFetcher { | ||
public: | ||
using ImageCallback = base::OnceCallback<void(std::unique_ptr<std::string>)>; | ||
explicit ImageFetcher( | ||
scoped_refptr<::network::SharedURLLoaderFactory> url_loader_factory); | ||
virtual ~ImageFetcher(); | ||
ImageFetcher(const ImageFetcher&) = delete; | ||
ImageFetcher& operator=(const ImageFetcher&) = delete; | ||
|
||
virtual void Fetch(const GURL& url, ImageCallback callback); | ||
|
||
private: | ||
// Called when fetch request completes. | ||
void OnFetchComplete(std::unique_ptr<network::SimpleURLLoader> simple_loader, | ||
ImageCallback callback, | ||
std::unique_ptr<std::string> response_data); | ||
|
||
const scoped_refptr<::network::SharedURLLoaderFactory> url_loader_factory_; | ||
base::WeakPtrFactory<ImageFetcher> weak_factory_{this}; | ||
}; | ||
|
||
} // namespace feed | ||
|
||
#endif // COMPONENTS_FEED_CORE_V2_IMAGE_FETCHER_H_ |
Oops, something went wrong.