Commit eb32e8b4 authored by Sergio Villar Senin's avatar Sergio Villar Senin Committed by Commit Bot

[chromeos] Migrate customization_document.cc to SimpleURLoader

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

Bug: 872880
Change-Id: Id192f3aee248334e3a40075b0adb190a799eef60
Reviewed-on: https://chromium-review.googlesource.com/1183229Reviewed-by: default avatarDan Erat <derat@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Commit-Queue: Sergio Villar <svillar@igalia.com>
Cr-Commit-Position: refs/heads/master@{#584844}
parent a7ff1f35
...@@ -48,7 +48,8 @@ ...@@ -48,7 +48,8 @@
#include "net/base/load_flags.h" #include "net/base/load_flags.h"
#include "net/http/http_response_headers.h" #include "net/http/http_response_headers.h"
#include "net/http/http_status_code.h" #include "net/http/http_status_code.h"
#include "net/url_request/url_fetcher.h" #include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
namespace chromeos { namespace chromeos {
namespace { namespace {
...@@ -102,6 +103,10 @@ const char kEmptyServicesCustomizationManifest[] = "{ \"version\": \"1.0\" }"; ...@@ -102,6 +103,10 @@ const char kEmptyServicesCustomizationManifest[] = "{ \"version\": \"1.0\" }";
// Global overrider for ServicesCustomizationDocument for tests. // Global overrider for ServicesCustomizationDocument for tests.
ServicesCustomizationDocument* g_test_services_customization_document = NULL; ServicesCustomizationDocument* g_test_services_customization_document = NULL;
// network::SharedURLLoaderFactory used by tests.
scoped_refptr<network::SharedURLLoaderFactory>
g_shared_url_loader_factory_for_testing = nullptr;
// Services customization document load results reported via the // Services customization document load results reported via the
// "ServicesCustomization.LoadResult" histogram. // "ServicesCustomization.LoadResult" histogram.
// It is append-only enum due to use in a histogram! // It is append-only enum due to use in a histogram!
...@@ -417,14 +422,13 @@ void ServicesCustomizationDocument::ApplyingTask::Finished(bool success) { ...@@ -417,14 +422,13 @@ void ServicesCustomizationDocument::ApplyingTask::Finished(bool success) {
ServicesCustomizationDocument::ServicesCustomizationDocument() ServicesCustomizationDocument::ServicesCustomizationDocument()
: CustomizationDocument(kAcceptedManifestVersion), : CustomizationDocument(kAcceptedManifestVersion),
num_retries_(0), num_retries_(0),
fetch_started_(false), load_started_(false),
network_delay_( network_delay_(
base::TimeDelta::FromMilliseconds(kDefaultNetworkRetryDelayMS)), base::TimeDelta::FromMilliseconds(kDefaultNetworkRetryDelayMS)),
apply_tasks_started_(0), apply_tasks_started_(0),
apply_tasks_finished_(0), apply_tasks_finished_(0),
apply_tasks_success_(0), apply_tasks_success_(0),
weak_ptr_factory_(this) { weak_ptr_factory_(this) {}
}
ServicesCustomizationDocument::ServicesCustomizationDocument( ServicesCustomizationDocument::ServicesCustomizationDocument(
const std::string& manifest) const std::string& manifest)
...@@ -522,7 +526,7 @@ ServicesCustomizationDocument::EnsureCustomizationAppliedClosure() { ...@@ -522,7 +526,7 @@ ServicesCustomizationDocument::EnsureCustomizationAppliedClosure() {
} }
void ServicesCustomizationDocument::StartFetching() { void ServicesCustomizationDocument::StartFetching() {
if (IsReady() || fetch_started_) if (IsReady() || load_started_)
return; return;
if (!url_.is_valid()) { if (!url_.is_valid()) {
...@@ -542,7 +546,7 @@ void ServicesCustomizationDocument::StartFetching() { ...@@ -542,7 +546,7 @@ void ServicesCustomizationDocument::StartFetching() {
} }
if (url_.is_valid()) { if (url_.is_valid()) {
fetch_started_ = true; load_started_ = true;
if (url_.SchemeIsFile()) { if (url_.SchemeIsFile()) {
base::PostTaskWithTraitsAndReplyWithResult( base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE, {base::TaskPriority::BEST_EFFORT, base::MayBlock()}, FROM_HERE, {base::TaskPriority::BEST_EFFORT, base::MayBlock()},
...@@ -560,7 +564,7 @@ void ServicesCustomizationDocument::OnManifestRead( ...@@ -560,7 +564,7 @@ void ServicesCustomizationDocument::OnManifestRead(
if (!manifest.empty()) if (!manifest.empty())
LoadManifestFromString(manifest); LoadManifestFromString(manifest);
fetch_started_ = false; load_started_ = false;
} }
void ServicesCustomizationDocument::StartFileFetch() { void ServicesCustomizationDocument::StartFileFetch() {
...@@ -570,12 +574,21 @@ void ServicesCustomizationDocument::StartFileFetch() { ...@@ -570,12 +574,21 @@ void ServicesCustomizationDocument::StartFileFetch() {
} }
void ServicesCustomizationDocument::DoStartFileFetch() { void ServicesCustomizationDocument::DoStartFileFetch() {
url_fetcher_ = net::URLFetcher::Create(url_, net::URLFetcher::GET, this); auto request = std::make_unique<network::ResourceRequest>();
url_fetcher_->SetRequestContext(g_browser_process->system_request_context()); request->url = url_;
url_fetcher_->AddExtraRequestHeader("Accept: application/json"); request->load_flags = net::LOAD_DISABLE_CACHE;
url_fetcher_->SetLoadFlags(net::LOAD_DISABLE_CACHE); request->allow_credentials = false;
url_fetcher_->SetAllowCredentials(false); request->headers.SetHeader("Accept", "application/json");
url_fetcher_->Start();
url_loader_ = network::SimpleURLLoader::Create(std::move(request),
NO_TRAFFIC_ANNOTATION_YET);
url_loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
g_shared_url_loader_factory_for_testing
? g_shared_url_loader_factory_for_testing.get()
: g_browser_process->shared_url_loader_factory().get(),
base::BindOnce(&ServicesCustomizationDocument::OnSimpleLoaderComplete,
base::Unretained(this)));
} }
bool ServicesCustomizationDocument::LoadManifestFromString( bool ServicesCustomizationDocument::LoadManifestFromString(
...@@ -607,19 +620,20 @@ void ServicesCustomizationDocument::OnManifestLoaded() { ...@@ -607,19 +620,20 @@ void ServicesCustomizationDocument::OnManifestLoaded() {
} }
} }
void ServicesCustomizationDocument::OnURLFetchComplete( void ServicesCustomizationDocument::OnSimpleLoaderComplete(
const net::URLFetcher* source) { std::unique_ptr<std::string> response_body) {
int response_code = -1;
std::string mime_type; std::string mime_type;
std::string data; if (url_loader_->ResponseInfo() && url_loader_->ResponseInfo()->headers) {
if (source->GetStatus().is_success() && response_code = url_loader_->ResponseInfo()->headers->response_code();
source->GetResponseCode() == net::HTTP_OK && url_loader_->ResponseInfo()->headers->GetMimeType(&mime_type);
source->GetResponseHeaders()->GetMimeType(&mime_type) && }
mime_type == "application/json" &&
source->GetResponseAsString(&data)) { if (response_body && mime_type == "application/json") {
LoadManifestFromString(data); LoadManifestFromString(*response_body);
} else if (source->GetResponseCode() == net::HTTP_NOT_FOUND) { } else if (response_code == net::HTTP_NOT_FOUND) {
LOG(ERROR) << "Customization manifest is missing on server: " LOG(ERROR) << "Customization manifest is missing on server: "
<< source->GetURL().spec(); << url_.spec();
OnCustomizationNotFound(); OnCustomizationNotFound();
} else { } else {
if (num_retries_ < kMaxFetchRetries) { if (num_retries_ < kMaxFetchRetries) {
...@@ -634,12 +648,12 @@ void ServicesCustomizationDocument::OnURLFetchComplete( ...@@ -634,12 +648,12 @@ void ServicesCustomizationDocument::OnURLFetchComplete(
} }
// This doesn't stop fetching manifest on next restart. // This doesn't stop fetching manifest on next restart.
LOG(ERROR) << "URL fetch for services customization failed:" LOG(ERROR) << "URL fetch for services customization failed:"
<< " response code = " << source->GetResponseCode() << " response code = " << response_code
<< " URL = " << source->GetURL().spec(); << " URL = " << url_.spec();
LogManifestLoadResult(HISTOGRAM_LOAD_RESULT_RETRIES_FAIL); LogManifestLoadResult(HISTOGRAM_LOAD_RESULT_RETRIES_FAIL);
} }
fetch_started_ = false; load_started_ = false;
} }
bool ServicesCustomizationDocument::ApplyOOBECustomization() { bool ServicesCustomizationDocument::ApplyOOBECustomization() {
...@@ -780,9 +794,11 @@ std::string ServicesCustomizationDocument::GetOemAppsFolderNameImpl( ...@@ -780,9 +794,11 @@ std::string ServicesCustomizationDocument::GetOemAppsFolderNameImpl(
} }
// static // static
void ServicesCustomizationDocument::InitializeForTesting() { void ServicesCustomizationDocument::InitializeForTesting(
scoped_refptr<network::SharedURLLoaderFactory> factory) {
g_test_services_customization_document = new ServicesCustomizationDocument; g_test_services_customization_document = new ServicesCustomizationDocument;
g_test_services_customization_document->network_delay_ = base::TimeDelta(); g_test_services_customization_document->network_delay_ = base::TimeDelta();
g_shared_url_loader_factory_for_testing = std::move(factory);
} }
// static // static
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
#include "base/memory/singleton.h" #include "base/memory/singleton.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/values.h" #include "base/values.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "url/gurl.h" #include "url/gurl.h"
class PrefRegistrySimple; class PrefRegistrySimple;
...@@ -32,8 +31,9 @@ namespace extensions { ...@@ -32,8 +31,9 @@ namespace extensions {
class ExternalLoader; class ExternalLoader;
} }
namespace net { namespace network {
class URLFetcher; class SharedURLLoaderFactory;
class SimpleURLLoader;
} }
namespace user_prefs { namespace user_prefs {
...@@ -141,8 +141,7 @@ class StartupCustomizationDocument : public CustomizationDocument { ...@@ -141,8 +141,7 @@ class StartupCustomizationDocument : public CustomizationDocument {
// outside this class by calling StartFetching() or EnsureCustomizationApplied() // outside this class by calling StartFetching() or EnsureCustomizationApplied()
// methods. // methods.
// User of the file should check IsReady before use it. // User of the file should check IsReady before use it.
class ServicesCustomizationDocument : public CustomizationDocument, class ServicesCustomizationDocument : public CustomizationDocument {
private net::URLFetcherDelegate {
public: public:
static ServicesCustomizationDocument* GetInstance(); static ServicesCustomizationDocument* GetInstance();
...@@ -185,7 +184,8 @@ class ServicesCustomizationDocument : public CustomizationDocument, ...@@ -185,7 +184,8 @@ class ServicesCustomizationDocument : public CustomizationDocument,
// Initialize instance of ServicesCustomizationDocument for tests that will // Initialize instance of ServicesCustomizationDocument for tests that will
// override singleton until ShutdownForTesting is called. // override singleton until ShutdownForTesting is called.
static void InitializeForTesting(); static void InitializeForTesting(
scoped_refptr<network::SharedURLLoaderFactory> factory);
// Remove instance of ServicesCustomizationDocument for tests. // Remove instance of ServicesCustomizationDocument for tests.
static void ShutdownForTesting(); static void ShutdownForTesting();
...@@ -227,8 +227,7 @@ class ServicesCustomizationDocument : public CustomizationDocument, ...@@ -227,8 +227,7 @@ class ServicesCustomizationDocument : public CustomizationDocument,
// Overriden from CustomizationDocument: // Overriden from CustomizationDocument:
bool LoadManifestFromString(const std::string& manifest) override; bool LoadManifestFromString(const std::string& manifest) override;
// Overriden from net::URLFetcherDelegate: void OnSimpleLoaderComplete(std::unique_ptr<std::string> response_body);
void OnURLFetchComplete(const net::URLFetcher* source) override;
// Initiate file fetching. Wait for online status. // Initiate file fetching. Wait for online status.
void StartFileFetch(); void StartFileFetch();
...@@ -293,14 +292,14 @@ class ServicesCustomizationDocument : public CustomizationDocument, ...@@ -293,14 +292,14 @@ class ServicesCustomizationDocument : public CustomizationDocument,
// Services customization manifest URL. // Services customization manifest URL.
GURL url_; GURL url_;
// URLFetcher instance. // SimpleURLLoader instance.
std::unique_ptr<net::URLFetcher> url_fetcher_; std::unique_ptr<network::SimpleURLLoader> url_loader_;
// How many times we already tried to fetch customization manifest file. // How many times we already tried to fetch customization manifest file.
int num_retries_; int num_retries_;
// Manifest fetch is already in progress. // Manifest fetch is already in progress.
bool fetch_started_; bool load_started_;
// Delay between checks for network online state. // Delay between checks for network online state.
base::TimeDelta network_delay_; base::TimeDelta network_delay_;
......
...@@ -34,6 +34,8 @@ ...@@ -34,6 +34,8 @@
#include "net/http/http_status_code.h" #include "net/http/http_status_code.h"
#include "net/url_request/test_url_fetcher_factory.h" #include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_request_status.h" #include "net/url_request/url_request_status.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -159,30 +161,16 @@ TEST(StartupCustomizationDocumentTest, BadManifest) { ...@@ -159,30 +161,16 @@ TEST(StartupCustomizationDocumentTest, BadManifest) {
EXPECT_FALSE(customization.IsReady()); EXPECT_FALSE(customization.IsReady());
} }
class TestURLFetcherCallback { class TestURLLoaderFactoryInterceptor {
public: public:
std::unique_ptr<net::FakeURLFetcher> CreateURLFetcher( explicit TestURLLoaderFactoryInterceptor(
const GURL& url, network::TestURLLoaderFactory& factory) {
net::URLFetcherDelegate* d, factory.SetInterceptor(base::BindRepeating(
const std::string& response_data, &TestURLLoaderFactoryInterceptor::Intercept, base::Unretained(this)));
net::HttpStatusCode response_code,
net::URLRequestStatus::Status status) {
std::unique_ptr<net::FakeURLFetcher> fetcher(
new net::FakeURLFetcher(url, d, response_data, response_code, status));
OnRequestCreate(url, fetcher.get());
return fetcher;
} }
MOCK_METHOD2(OnRequestCreate, MOCK_METHOD1(Intercept, void(const network::ResourceRequest&));
void(const GURL&, net::FakeURLFetcher*));
}; };
void AddMimeHeader(const GURL& url, net::FakeURLFetcher* fetcher) {
scoped_refptr<net::HttpResponseHeaders> download_headers =
new net::HttpResponseHeaders("");
download_headers->AddHeader("Content-Type: application/json");
fetcher->set_response_headers(download_headers);
}
class MockExternalProviderVisitor class MockExternalProviderVisitor
: public extensions::ExternalProviderInterface::VisitorInterface { : public extensions::ExternalProviderInterface::VisitorInterface {
public: public:
...@@ -203,14 +191,13 @@ class MockExternalProviderVisitor ...@@ -203,14 +191,13 @@ class MockExternalProviderVisitor
class ServicesCustomizationDocumentTest : public testing::Test { class ServicesCustomizationDocumentTest : public testing::Test {
protected: protected:
ServicesCustomizationDocumentTest() ServicesCustomizationDocumentTest() = default;
: factory_(nullptr,
base::Bind(&TestURLFetcherCallback::CreateURLFetcher,
base::Unretained(&url_callback_))) {}
// testing::Test: // testing::Test:
void SetUp() override { void SetUp() override {
ServicesCustomizationDocument::InitializeForTesting(); ServicesCustomizationDocument::InitializeForTesting(
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&loader_factory_));
DBusThreadManager::Initialize(); DBusThreadManager::Initialize();
NetworkHandler::Initialize(); NetworkHandler::Initialize();
...@@ -234,6 +221,9 @@ class ServicesCustomizationDocumentTest : public testing::Test { ...@@ -234,6 +221,9 @@ class ServicesCustomizationDocumentTest : public testing::Test {
TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_); TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_);
RegisterLocalState(local_state_.registry()); RegisterLocalState(local_state_.registry());
interceptor_ =
std::make_unique<TestURLLoaderFactoryInterceptor>(loader_factory_);
} }
void TearDown() override { void TearDown() override {
...@@ -241,6 +231,8 @@ class ServicesCustomizationDocumentTest : public testing::Test { ...@@ -241,6 +231,8 @@ class ServicesCustomizationDocumentTest : public testing::Test {
NetworkHandler::Shutdown(); NetworkHandler::Shutdown();
DBusThreadManager::Shutdown(); DBusThreadManager::Shutdown();
network_portal_detector::InitializeForTesting(nullptr); network_portal_detector::InitializeForTesting(nullptr);
loader_factory_.ClearResponses();
interceptor_.reset();
ServicesCustomizationDocument::ShutdownForTesting(); ServicesCustomizationDocument::ShutdownForTesting();
} }
...@@ -258,25 +250,26 @@ class ServicesCustomizationDocumentTest : public testing::Test { ...@@ -258,25 +250,26 @@ class ServicesCustomizationDocumentTest : public testing::Test {
const std::string& manifest) { const std::string& manifest) {
GURL url(base::StringPrintf(ServicesCustomizationDocument::kManifestUrl, GURL url(base::StringPrintf(ServicesCustomizationDocument::kManifestUrl,
id.c_str())); id.c_str()));
factory_.SetFakeResponse(url,
manifest, network::ResourceResponseHead response_head;
net::HTTP_OK, response_head.headers = base::MakeRefCounted<net::HttpResponseHeaders>("");
net::URLRequestStatus::SUCCESS); response_head.headers->AddHeader("Content-Type: application/json");
EXPECT_CALL(url_callback_, OnRequestCreate(url, _)) loader_factory_.AddResponse(url, response_head, manifest,
.Times(Exactly(1)) network::URLLoaderCompletionStatus(net::OK));
.WillRepeatedly(Invoke(AddMimeHeader)); EXPECT_CALL(*interceptor_, Intercept).Times(Exactly(1));
} }
void AddManifestNotFound(const std::string& id) { void AddManifestNotFound(const std::string& id) {
GURL url(base::StringPrintf(ServicesCustomizationDocument::kManifestUrl, GURL url(base::StringPrintf(ServicesCustomizationDocument::kManifestUrl,
id.c_str())); id.c_str()));
factory_.SetFakeResponse(url,
std::string(), network::ResourceResponseHead response_head;
net::HTTP_NOT_FOUND, response_head.headers = base::MakeRefCounted<net::HttpResponseHeaders>("");
net::URLRequestStatus::SUCCESS); response_head.headers->AddHeader("Content-Type: application/json");
EXPECT_CALL(url_callback_, OnRequestCreate(url, _)) response_head.headers->ReplaceStatusLine("HTTP/1.1 404 Not found");
.Times(Exactly(1)) loader_factory_.AddResponse(url, response_head, std::string(),
.WillRepeatedly(Invoke(AddMimeHeader)); network::URLLoaderCompletionStatus(net::OK));
EXPECT_CALL(*interceptor_, Intercept).Times(Exactly(1));
} }
std::unique_ptr<TestingProfile> CreateProfile() { std::unique_ptr<TestingProfile> CreateProfile() {
...@@ -300,8 +293,8 @@ class ServicesCustomizationDocumentTest : public testing::Test { ...@@ -300,8 +293,8 @@ class ServicesCustomizationDocumentTest : public testing::Test {
system::ScopedFakeStatisticsProvider fake_statistics_provider_; system::ScopedFakeStatisticsProvider fake_statistics_provider_;
ScopedCrosSettingsTestHelper scoped_cros_settings_test_helper_; ScopedCrosSettingsTestHelper scoped_cros_settings_test_helper_;
TestingPrefServiceSimple local_state_; TestingPrefServiceSimple local_state_;
TestURLFetcherCallback url_callback_; network::TestURLLoaderFactory loader_factory_;
net::FakeURLFetcherFactory factory_; std::unique_ptr<TestURLLoaderFactoryInterceptor> interceptor_;
NetworkPortalDetectorTestImpl network_portal_detector_; NetworkPortalDetectorTestImpl network_portal_detector_;
}; };
......
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