Commit 1a72e051 authored by Mark Pilgrim's avatar Mark Pilgrim Committed by Commit Bot

Migrate WebstoreDataFetcher to SimpleURLLoader

Bug: 773295
Change-Id: I9cc62ea0aaf4eab4639c68b3d76ec82e17ab0e3c
Reviewed-on: https://chromium-review.googlesource.com/1024077Reviewed-by: default avatarKaran Bhatia <karandeepb@chromium.org>
Reviewed-by: default avatarSatoru Takabayashi <satorux@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Commit-Queue: Mark Pilgrim <pilgrim@chromium.org>
Cr-Commit-Position: refs/heads/master@{#553533}
parent 8d3f7a83
......@@ -370,10 +370,6 @@ void KioskAppData::SetStatus(Status status) {
}
}
net::URLRequestContextGetter* KioskAppData::GetRequestContextGetter() {
return g_browser_process->system_request_context();
}
network::mojom::URLLoaderFactory* KioskAppData::GetURLLoaderFactory() {
return g_browser_process->system_network_context_manager()
->GetURLLoaderFactory();
......@@ -464,10 +460,11 @@ void KioskAppData::StartFetch() {
return;
}
webstore_fetcher_.reset(new extensions::WebstoreDataFetcher(
this, GetRequestContextGetter(), GURL(), app_id()));
webstore_fetcher_.reset(
new extensions::WebstoreDataFetcher(this, GURL(), app_id()));
webstore_fetcher_->set_max_auto_retries(3);
webstore_fetcher_->Start();
webstore_fetcher_->Start(g_browser_process->system_network_context_manager()
->GetURLLoaderFactory());
}
void KioskAppData::OnWebstoreRequestFailure() {
......
......@@ -27,10 +27,6 @@ namespace gfx {
class Image;
}
namespace net {
class URLRequestContextGetter;
}
namespace network {
namespace mojom {
class URLLoaderFactory;
......@@ -102,9 +98,6 @@ class KioskAppData : public KioskAppDataBase,
void SetStatus(Status status);
// Returns URLRequestContextGetter to use for fetching web store data.
net::URLRequestContextGetter* GetRequestContextGetter();
// Returns URLLoaderFactory to use for fetching web store data.
network::mojom::URLLoaderFactory* GetURLLoaderFactory();
......
......@@ -9,6 +9,7 @@
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/webstore_data_fetcher.h"
#include "chrome/browser/extensions/webstore_inline_installer.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
......@@ -82,13 +83,12 @@ ExtensionReenabler::ExtensionReenabler(
// If we have a non-empty referrer, then we have to validate that it's a valid
// url for the extension.
if (!referrer_url_.is_empty()) {
webstore_data_fetcher_.reset(new WebstoreDataFetcher(
this,
content::BrowserContext::GetDefaultStoragePartition(browser_context_)->
GetURLRequestContext(),
referrer_url_,
extension->id()));
webstore_data_fetcher_->Start();
webstore_data_fetcher_.reset(
new WebstoreDataFetcher(this, referrer_url_, extension->id()));
webstore_data_fetcher_->Start(
content::BrowserContext::GetDefaultStoragePartition(browser_context_)
->GetURLLoaderFactoryForBrowserProcess()
.get());
} else {
ExtensionInstallPrompt::PromptType type =
ExtensionInstallPrompt::GetReEnablePromptTypeForExtension(
......
......@@ -358,12 +358,12 @@ ExternalInstallError::ExternalInstallError(
prompt_.reset(new ExtensionInstallPrompt::Prompt(
ExtensionInstallPrompt::EXTERNAL_INSTALL_PROMPT));
webstore_data_fetcher_.reset(new WebstoreDataFetcher(
this,
content::BrowserContext::GetDefaultStoragePartition(browser_context_)->
GetURLRequestContext(),
GURL(), extension_id_));
webstore_data_fetcher_->Start();
webstore_data_fetcher_.reset(
new WebstoreDataFetcher(this, GURL(), extension_id_));
webstore_data_fetcher_->Start(
content::BrowserContext::GetDefaultStoragePartition(browser_context_)
->GetURLLoaderFactoryForBrowserProcess()
.get());
}
ExternalInstallError::~ExternalInstallError() {
......
......@@ -11,6 +11,9 @@
#include "base/values.h"
#include "chrome/browser/extensions/webstore_data_fetcher_delegate.h"
#include "components/safe_browsing/features.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/common/service_manager_connection.h"
#include "extensions/common/extension_urls.h"
#include "net/base/load_flags.h"
......@@ -18,6 +21,9 @@
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_status.h"
#include "services/data_decoder/public/cpp/safe_json_parser.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"
namespace {
......@@ -29,11 +35,9 @@ namespace extensions {
WebstoreDataFetcher::WebstoreDataFetcher(
WebstoreDataFetcherDelegate* delegate,
net::URLRequestContextGetter* request_context,
const GURL& referrer_url,
const std::string webstore_item_id)
: delegate_(delegate),
request_context_(request_context),
referrer_url_(referrer_url),
id_(webstore_item_id),
max_auto_retries_(0) {
......@@ -49,10 +53,9 @@ void WebstoreDataFetcher::SetPostData(const std::string& data) {
post_data_ = data;
}
void WebstoreDataFetcher::Start() {
void WebstoreDataFetcher::Start(
network::mojom::URLLoaderFactory* url_loader_factory) {
GURL webstore_data_url(extension_urls::GetWebstoreItemJsonDataURL(id_));
net::URLFetcher::RequestType request_type =
post_data_.empty() ? net::URLFetcher::GET : net::URLFetcher::POST;
net::NetworkTrafficAnnotationTag traffic_annotation =
net::DefineNetworkTrafficAnnotation("webstore_data_fetcher", R"(
semantics {
......@@ -83,22 +86,29 @@ void WebstoreDataFetcher::Start() {
"triggered if the user uses extensions."
policy_exception_justification: "Not implemented."
})");
webstore_data_url_fetcher_ = net::URLFetcher::Create(
webstore_data_url, request_type, this, traffic_annotation);
webstore_data_url_fetcher_->SetRequestContext(request_context_);
webstore_data_url_fetcher_->SetReferrer(referrer_url_.spec());
webstore_data_url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES |
net::LOAD_DISABLE_CACHE);
auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->url = webstore_data_url;
resource_request->load_flags =
net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_DISABLE_CACHE;
resource_request->referrer = referrer_url_;
resource_request->method = post_data_.empty() ? "GET" : "POST";
simple_url_loader_ = network::SimpleURLLoader::Create(
std::move(resource_request), traffic_annotation);
if (!post_data_.empty())
webstore_data_url_fetcher_->SetUploadData(upload_content_type_, post_data_);
simple_url_loader_->AttachStringForUpload(post_data_, upload_content_type_);
if (max_auto_retries_ > 0) {
webstore_data_url_fetcher_->SetMaxRetriesOn5xx(max_auto_retries_);
webstore_data_url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(
max_auto_retries_);
simple_url_loader_->SetRetryOptions(
max_auto_retries_,
network::SimpleURLLoader::RETRY_ON_5XX |
network::SimpleURLLoader::RETRY_ON_NETWORK_CHANGE);
}
webstore_data_url_fetcher_->Start();
simple_url_loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
url_loader_factory,
base::BindOnce(&WebstoreDataFetcher::OnSimpleLoaderComplete,
base::Unretained(this)));
}
void WebstoreDataFetcher::OnJsonParseSuccess(
......@@ -118,25 +128,17 @@ void WebstoreDataFetcher::OnJsonParseFailure(
delegate_->OnWebstoreResponseParseFailure(error);
}
void WebstoreDataFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
CHECK_EQ(webstore_data_url_fetcher_.get(), source);
std::unique_ptr<net::URLFetcher> fetcher(
std::move(webstore_data_url_fetcher_));
if (!fetcher->GetStatus().is_success() ||
fetcher->GetResponseCode() != 200) {
void WebstoreDataFetcher::OnSimpleLoaderComplete(
std::unique_ptr<std::string> response_body) {
if (!response_body) {
delegate_->OnWebstoreRequestFailure();
return;
}
std::string webstore_json_data;
fetcher->GetResponseAsString(&webstore_json_data);
// The parser will call us back via one of the callbacks.
data_decoder::SafeJsonParser::Parse(
content::ServiceManagerConnection::GetForProcess()->GetConnector(),
webstore_json_data,
*response_body,
base::Bind(&WebstoreDataFetcher::OnJsonParseSuccess, AsWeakPtr()),
base::Bind(&WebstoreDataFetcher::OnJsonParseFailure, AsWeakPtr()));
}
......
......@@ -10,17 +10,18 @@
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "url/gurl.h"
namespace base {
class Value;
}
namespace net {
class URLFetcher;
class URLRequestContextGetter;
}
namespace network {
class SimpleURLLoader;
namespace mojom {
class URLLoaderFactory;
} // namespace mojom
} // namespace network
namespace extensions {
......@@ -28,20 +29,18 @@ class WebstoreDataFetcherDelegate;
// WebstoreDataFetcher fetches web store data and parses it into a
// DictionaryValue.
class WebstoreDataFetcher : public base::SupportsWeakPtr<WebstoreDataFetcher>,
public net::URLFetcherDelegate {
class WebstoreDataFetcher : public base::SupportsWeakPtr<WebstoreDataFetcher> {
public:
WebstoreDataFetcher(WebstoreDataFetcherDelegate* delegate,
net::URLRequestContextGetter* request_context,
const GURL& referrer_url,
const std::string webstore_item_id);
~WebstoreDataFetcher() override;
~WebstoreDataFetcher();
// Makes this request use a POST instead of GET, and sends |data| in the
// body of the request. If |data| is empty, this is a no-op.
void SetPostData(const std::string& data);
void Start();
void Start(network::mojom::URLLoaderFactory* url_loader_factory);
void set_max_auto_retries(int max_retries) {
max_auto_retries_ = max_retries;
......@@ -52,19 +51,16 @@ class WebstoreDataFetcher : public base::SupportsWeakPtr<WebstoreDataFetcher>,
private:
void OnJsonParseSuccess(std::unique_ptr<base::Value> parsed_json);
void OnJsonParseFailure(const std::string& error);
// net::URLFetcherDelegate overrides:
void OnURLFetchComplete(const net::URLFetcher* source) override;
void OnSimpleLoaderComplete(std::unique_ptr<std::string> response_body);
WebstoreDataFetcherDelegate* delegate_;
net::URLRequestContextGetter* request_context_;
GURL referrer_url_;
std::string id_;
std::string post_data_;
std::string upload_content_type_;
// For fetching webstore JSON data.
std::unique_ptr<net::URLFetcher> webstore_data_url_fetcher_;
std::unique_ptr<network::SimpleURLLoader> simple_url_loader_;
// Maximum auto retry times on server 5xx error or ERR_NETWORK_CHANGED.
// Default is 0 which means to use the URLFetcher default behavior.
......
......@@ -63,16 +63,16 @@ void WebstoreStandaloneInstaller::BeginInstall() {
// Use the requesting page as the referrer both since that is more correct
// (it is the page that caused this request to happen) and so that we can
// track top sites that trigger inline install requests.
webstore_data_fetcher_.reset(new WebstoreDataFetcher(
this,
profile_->GetRequestContext(),
GetRequestorURL(),
id_));
webstore_data_fetcher_.reset(
new WebstoreDataFetcher(this, GetRequestorURL(), id_));
webstore_data_fetcher_->SetPostData(
GetPostData(webstore_data_fetcher_->upload_content_type()));
webstore_data_fetcher_->Start();
webstore_data_fetcher_->Start(
content::BrowserContext::GetDefaultStoragePartition(profile_)
->GetURLLoaderFactoryForBrowserProcess()
.get());
}
//
......
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