Commit 93de5377 authored by Sergio Villar Senin's avatar Sergio Villar Senin Committed by Commit Bot

[android] Migrate webapk_installer.cc using SimpleURLLoader

It's currently using URLFetcher. It should use SimpleURLLoader
instead to make it eventually work with the network service.

Bug: 872879
Change-Id: I5ea01677b973991f33eb077e029c7b4d3fd1e7d4
Reviewed-on: https://chromium-review.googlesource.com/1202210Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Commit-Queue: Sergio Villar <svillar@igalia.com>
Cr-Commit-Position: refs/heads/master@{#588811}
parent e1128f2b
......@@ -46,6 +46,9 @@
#include "net/base/load_flags.h"
#include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/resource_response_info.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "ui/gfx/android/java_bitmap.h"
#include "ui/gfx/codec/png_codec.h"
#include "url/gurl.h"
......@@ -97,11 +100,6 @@ class CacheClearer : public content::BrowsingDataRemover::Observer {
DISALLOW_COPY_AND_ASSIGN(CacheClearer);
};
net::URLRequestContextGetter* GetRequestContext(
content::BrowserContext* browser_context) {
return Profile::FromBrowserContext(browser_context)->GetRequestContext();
}
// Returns the WebAPK server URL based on the command line.
GURL GetServerUrl() {
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
......@@ -533,22 +531,23 @@ void WebApkInstaller::OnReadUpdateRequest(
SendRequest(std::move(update_request));
}
void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) {
void WebApkInstaller::OnURLLoaderComplete(
std::unique_ptr<std::string> response_body) {
timer_.Stop();
if (!source->GetStatus().is_success() ||
source->GetResponseCode() != net::HTTP_OK) {
int response_code = -1;
if (loader_->ResponseInfo() && loader_->ResponseInfo()->headers)
response_code = loader_->ResponseInfo()->headers->response_code();
if (!response_body || response_code != net::HTTP_OK) {
LOG(WARNING) << base::StringPrintf(
"WebAPK server returned response code %d.", source->GetResponseCode());
"WebAPK server returned response code %d.", response_code);
OnResult(WebApkInstallResult::FAILURE);
return;
}
std::string response_string;
source->GetResponseAsString(&response_string);
std::unique_ptr<webapk::WebApkResponse> response(new webapk::WebApkResponse);
if (!response->ParseFromString(response_string)) {
if (!response_body || !response->ParseFromString(*response_body)) {
LOG(WARNING) << "WebAPK server did not return proto.";
OnResult(WebApkInstallResult::FAILURE);
return;
......@@ -574,6 +573,13 @@ void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) {
InstallOrUpdateWebApk(response->package_name(), version, token);
}
network::SharedURLLoaderFactory* GetURLLoaderFactory(
content::BrowserContext* browser_context) {
return content::BrowserContext::GetDefaultStoragePartition(browser_context)
->GetURLLoaderFactoryForBrowserProcess()
.get();
}
void WebApkInstaller::OnHaveSufficientSpaceForInstall() {
// We need to take the hash of the bitmap at the icon URL prior to any
// transformations being applied to the bitmap (such as encoding/decoding
......@@ -584,9 +590,7 @@ void WebApkInstaller::OnHaveSufficientSpaceForInstall() {
// We redownload the icon in order to take the Murmur2 hash. The redownload
// should be fast because the icon should be in the HTTP cache.
WebApkIconHasher::DownloadAndComputeMurmur2Hash(
content::BrowserContext::GetDefaultStoragePartition(browser_context_)
->GetURLLoaderFactoryForBrowserProcess()
.get(),
GetURLLoaderFactory(browser_context_),
install_shortcut_info_->best_primary_icon_url,
base::Bind(&WebApkInstaller::OnGotPrimaryIconMurmur2Hash,
weak_ptr_factory_.GetWeakPtr()));
......@@ -604,9 +608,7 @@ void WebApkInstaller::OnGotPrimaryIconMurmur2Hash(
install_shortcut_info_->best_badge_icon_url !=
install_shortcut_info_->best_primary_icon_url) {
WebApkIconHasher::DownloadAndComputeMurmur2Hash(
content::BrowserContext::GetDefaultStoragePartition(browser_context_)
->GetURLLoaderFactoryForBrowserProcess()
.get(),
GetURLLoaderFactory(browser_context_),
install_shortcut_info_->best_badge_icon_url,
base::Bind(&WebApkInstaller::OnGotBadgeIconMurmur2Hash,
weak_ptr_factory_.GetWeakPtr(), true, primary_icon_hash));
......@@ -650,11 +652,16 @@ void WebApkInstaller::SendRequest(
base::Bind(&WebApkInstaller::OnResult, weak_ptr_factory_.GetWeakPtr(),
WebApkInstallResult::FAILURE));
url_fetcher_ =
net::URLFetcher::Create(server_url_, net::URLFetcher::POST, this);
url_fetcher_->SetRequestContext(GetRequestContext(browser_context_));
url_fetcher_->SetUploadData(kProtoMimeType, *serialized_proto);
url_fetcher_->SetLoadFlags(net::LOAD_DISABLE_CACHE);
url_fetcher_->SetAllowCredentials(false);
url_fetcher_->Start();
auto request = std::make_unique<network::ResourceRequest>();
request->url = server_url_;
request->method = "POST";
request->load_flags = net::LOAD_DISABLE_CACHE;
request->allow_credentials = false;
loader_ = network::SimpleURLLoader::Create(std::move(request),
NO_TRAFFIC_ANNOTATION_YET);
loader_->AttachStringForUpload(*serialized_proto, kProtoMimeType);
loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
GetURLLoaderFactory(browser_context_),
base::BindOnce(&WebApkInstaller::OnURLLoaderComplete,
weak_ptr_factory_.GetWeakPtr()));
}
......@@ -19,8 +19,6 @@
#include "chrome/browser/android/shortcut_info.h"
#include "chrome/browser/android/webapk/webapk_install_service.h"
#include "chrome/browser/android/webapk/webapk_types.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "third_party/skia/include/core/SkBitmap.h"
namespace base {
......@@ -32,6 +30,10 @@ namespace content {
class BrowserContext;
}
namespace network {
class SimpleURLLoader;
}
// The enum values are persisted to logs |WebApkInstallSpaceStatus| in
// enums.xml, therefore they should never be reused nor renumbered.
// A Java counterpart will be generated for this enum.
......@@ -46,11 +48,11 @@ enum class SpaceStatus {
// Talks to Chrome WebAPK server to download metadata about a WebAPK and issue
// a request for it to be installed. The native WebApkInstaller owns the Java
// WebApkInstaller counterpart.
class WebApkInstaller : public net::URLFetcherDelegate {
class WebApkInstaller {
public:
using FinishCallback = WebApkInstallService::FinishCallback;
~WebApkInstaller() override;
virtual ~WebApkInstaller();
// Creates a self-owned WebApkInstaller instance and talks to the Chrome
// WebAPK server to generate a WebAPK on the server and locally requests the
......@@ -175,8 +177,7 @@ class WebApkInstaller : public net::URLFetcherDelegate {
// Called with the contents of the update request file.
void OnReadUpdateRequest(std::unique_ptr<std::string> update_request);
// net::URLFetcherDelegate:
void OnURLFetchComplete(const net::URLFetcher* source) override;
void OnURLLoaderComplete(std::unique_ptr<std::string> response_body);
// Called with the computed Murmur2 hash for the primary icon.
void OnGotPrimaryIconMurmur2Hash(const std::string& primary_icon_hash);
......@@ -196,7 +197,7 @@ class WebApkInstaller : public net::URLFetcherDelegate {
content::BrowserContext* browser_context_;
// Sends HTTP request to WebAPK server.
std::unique_ptr<net::URLFetcher> url_fetcher_;
std::unique_ptr<network::SimpleURLLoader> loader_;
// Fails WebApkInstaller if WebAPK server takes too long to respond or if the
// download takes too long.
......
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