Commit 4f71dade authored by Antonio Gomes's avatar Antonio Gomes Committed by Commit Bot

Migrate backdrop_wallpaper_handlers::CollectionInfoFetcher to SimpleURLLoader

BUG=773295
TEST=
  1. <out_chromeos>/chrome --user-data-dir=<profile> --new-wallpaper-picker
  2. settings -> select wallpaper
  3. selection is functional.


Change-Id: Ia8cab4086550b723ce904705db26c5f171277dbf
Reviewed-on: https://chromium-review.googlesource.com/1014222Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Commit-Queue: Antonio Gomes <tonikitoo@igalia.com>
Cr-Commit-Position: refs/heads/master@{#551174}
parent 1d1cc79a
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/extensions/backdrop_wallpaper_handlers/backdrop_wallpaper.pb.h" #include "chrome/browser/chromeos/extensions/backdrop_wallpaper_handlers/backdrop_wallpaper.pb.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/common/extensions/api/wallpaper_private.h" #include "chrome/common/extensions/api/wallpaper_private.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "net/base/load_flags.h" #include "net/base/load_flags.h"
...@@ -40,7 +41,7 @@ CollectionInfoFetcher::CollectionInfoFetcher() { ...@@ -40,7 +41,7 @@ CollectionInfoFetcher::CollectionInfoFetcher() {
CollectionInfoFetcher::~CollectionInfoFetcher() = default; CollectionInfoFetcher::~CollectionInfoFetcher() = default;
void CollectionInfoFetcher::Start(OnCollectionsInfoFetched callback) { void CollectionInfoFetcher::Start(OnCollectionsInfoFetched callback) {
DCHECK(!url_fetcher_ && callback_.is_null()); DCHECK(!simple_loader_ && callback_.is_null());
callback_ = std::move(callback); callback_ = std::move(callback);
net::NetworkTrafficAnnotationTag traffic_annotation = net::NetworkTrafficAnnotationTag traffic_annotation =
...@@ -68,46 +69,62 @@ void CollectionInfoFetcher::Start(OnCollectionsInfoFetched callback) { ...@@ -68,46 +69,62 @@ void CollectionInfoFetcher::Start(OnCollectionsInfoFetched callback) {
"Not implemented, considered not necessary." "Not implemented, considered not necessary."
})"); })");
url_fetcher_ = SystemNetworkContextManager* system_network_context_manager =
net::URLFetcher::Create(GURL(kBackdropCollectionsUrl), g_browser_process->system_network_context_manager();
net::URLFetcher::POST, this, traffic_annotation); // In unit tests, the browser process can return a null context manager
url_fetcher_->SetRequestContext(g_browser_process->system_request_context()); if (!system_network_context_manager)
url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE | return;
net::LOAD_DO_NOT_SAVE_COOKIES |
net::LOAD_DO_NOT_SEND_COOKIES | network::mojom::URLLoaderFactory* loader_factory =
net::LOAD_DO_NOT_SEND_AUTH_DATA); system_network_context_manager->GetURLLoaderFactory();
backdrop::GetCollectionsRequest request; backdrop::GetCollectionsRequest request;
// The language field may include the country code (e.g. "en-US"). // The language field may include the country code (e.g. "en-US").
request.set_language(g_browser_process->GetApplicationLocale()); request.set_language(g_browser_process->GetApplicationLocale());
std::string serialized_proto; std::string serialized_proto;
request.SerializeToString(&serialized_proto); request.SerializeToString(&serialized_proto);
url_fetcher_->SetUploadData(kProtoMimeType, serialized_proto);
url_fetcher_->Start(); auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->url = GURL(kBackdropCollectionsUrl);
resource_request->method = "POST";
resource_request->load_flags =
net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE |
net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_DO_NOT_SEND_COOKIES |
net::LOAD_DO_NOT_SEND_AUTH_DATA;
simple_loader_ = network::SimpleURLLoader::Create(std::move(resource_request),
traffic_annotation);
simple_loader_->AttachStringForUpload(serialized_proto, kProtoMimeType);
simple_loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
loader_factory, base::BindOnce(&CollectionInfoFetcher::OnURLFetchComplete,
base::Unretained(this)));
} }
void CollectionInfoFetcher::OnURLFetchComplete(const net::URLFetcher* source) { void CollectionInfoFetcher::OnURLFetchComplete(
const std::unique_ptr<std::string> response_body) {
std::vector<extensions::api::wallpaper_private::CollectionInfo> std::vector<extensions::api::wallpaper_private::CollectionInfo>
collections_info_list; collections_info_list;
if (!source->GetStatus().is_success() || if (!response_body) {
source->GetResponseCode() != net::HTTP_OK) { int response_code = -1;
if (simple_loader_->ResponseInfo() &&
simple_loader_->ResponseInfo()->headers)
response_code = simple_loader_->ResponseInfo()->headers->response_code();
// TODO(crbug.com/800945): Adds retry mechanism and error handling. // TODO(crbug.com/800945): Adds retry mechanism and error handling.
LOG(ERROR) << "Downloading Backdrop wallpaper proto for collection info " LOG(ERROR) << "Downloading Backdrop wallpaper proto for collection info "
"failed with error code: " "failed with error code: "
<< source->GetResponseCode(); << response_code;
url_fetcher_.reset(); simple_loader_.reset();
std::move(callback_).Run(false /*success=*/, collections_info_list); std::move(callback_).Run(false /*success=*/, collections_info_list);
return; return;
} }
std::string response_string;
source->GetResponseAsString(&response_string);
backdrop::GetCollectionsResponse collections_response; backdrop::GetCollectionsResponse collections_response;
if (!collections_response.ParseFromString(response_string)) { if (!collections_response.ParseFromString(*response_body)) {
LOG(ERROR) << "Deserializing Backdrop wallpaper proto for collection info " LOG(ERROR) << "Deserializing Backdrop wallpaper proto for collection info "
"failed."; "failed.";
url_fetcher_.reset(); simple_loader_.reset();
std::move(callback_).Run(false /*success=*/, collections_info_list); std::move(callback_).Run(false /*success=*/, collections_info_list);
return; return;
} }
...@@ -120,7 +137,7 @@ void CollectionInfoFetcher::OnURLFetchComplete(const net::URLFetcher* source) { ...@@ -120,7 +137,7 @@ void CollectionInfoFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
collections_info_list.push_back(std::move(collection_info)); collections_info_list.push_back(std::move(collection_info));
} }
url_fetcher_.reset(); simple_loader_.reset();
std::move(callback_).Run(true /*success=*/, collections_info_list); std::move(callback_).Run(true /*success=*/, collections_info_list);
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/macros.h" #include "base/macros.h"
#include "net/url_request/url_fetcher_delegate.h" #include "net/url_request/url_fetcher_delegate.h"
#include "services/network/public/cpp/simple_url_loader.h"
namespace extensions { namespace extensions {
namespace api { namespace api {
...@@ -24,7 +25,7 @@ namespace backdrop_wallpaper_handlers { ...@@ -24,7 +25,7 @@ namespace backdrop_wallpaper_handlers {
// Downloads and deserializes the proto for the wallpaper collections info from // Downloads and deserializes the proto for the wallpaper collections info from
// the Backdrop service. // the Backdrop service.
class CollectionInfoFetcher : public net::URLFetcherDelegate { class CollectionInfoFetcher {
public: public:
using OnCollectionsInfoFetched = base::OnceCallback<void( using OnCollectionsInfoFetched = base::OnceCallback<void(
bool success, bool success,
...@@ -32,17 +33,16 @@ class CollectionInfoFetcher : public net::URLFetcherDelegate { ...@@ -32,17 +33,16 @@ class CollectionInfoFetcher : public net::URLFetcherDelegate {
collections_info_list)>; collections_info_list)>;
CollectionInfoFetcher(); CollectionInfoFetcher();
~CollectionInfoFetcher() override; ~CollectionInfoFetcher();
// Triggers the start of the downloading and deserializing of the proto. // Triggers the start of the downloading and deserializing of the proto.
void Start(OnCollectionsInfoFetched callback); void Start(OnCollectionsInfoFetched callback);
// net::URLFetcherDelegate void OnURLFetchComplete(const std::unique_ptr<std::string> response_body);
void OnURLFetchComplete(const net::URLFetcher* source) override;
private: private:
// Used to download the proto from the Backdrop service. // Used to download the proto from the Backdrop service.
std::unique_ptr<net::URLFetcher> url_fetcher_; std::unique_ptr<network::SimpleURLLoader> simple_loader_;
// The callback upon completion of fetching the collections info. // The callback upon completion of fetching the collections info.
OnCollectionsInfoFetched callback_; OnCollectionsInfoFetched callback_;
......
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