Commit 1f16cd8d authored by Antonio Gomes's avatar Antonio Gomes Committed by Commit Bot

[ios] Migrate OmahaService to SimpleURLLoader

URLFetcher will stop working with advent of Network Service, and
SimpleURLLoader is the replacement API for most clients.
This CL migrates iOS' OmahaService and its respective
unittests away from URLFetcher.

BUG=879774

Cq-Include-Trybots: luci.chromium.try:ios-simulator-cronet;luci.chromium.try:ios-simulator-full-configs
Change-Id: I60339c7599c6174951056c5a8b867aa6df1438da
Reviewed-on: https://chromium-review.googlesource.com/1227191Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Commit-Queue: Antonio Gomes <tonikitoo@igalia.com>
Cr-Commit-Position: refs/heads/master@{#592006}
parent 621b28fb
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "ios/chrome/browser/reading_list/reading_list_download_service_factory.h" #include "ios/chrome/browser/reading_list/reading_list_download_service_factory.h"
#import "ios/chrome/browser/ui/main/browser_view_information.h" #import "ios/chrome/browser/ui/main/browser_view_information.h"
#import "ios/chrome/browser/upgrade/upgrade_center.h" #import "ios/chrome/browser/upgrade/upgrade_center.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support." #error "This file requires ARC support."
...@@ -63,9 +64,7 @@ NSString* const kStartProfileStartupTaskRunners = ...@@ -63,9 +64,7 @@ NSString* const kStartProfileStartupTaskRunners =
return; return;
// Start omaha service. We only do this on official builds. // Start omaha service. We only do this on official builds.
OmahaService::Start( OmahaService::Start(
GetApplicationContext() GetApplicationContext()->GetSharedURLLoaderFactory()->Clone(),
->GetIOSChromeIOThread()
->system_url_request_context_getter(),
base::BindRepeating(^(const UpgradeRecommendedDetails& details) { base::BindRepeating(^(const UpgradeRecommendedDetails& details) {
[[UpgradeCenter sharedInstance] upgradeNotificationDidOccur:details]; [[UpgradeCenter sharedInstance] upgradeNotificationDidOccur:details];
})); }));
......
...@@ -51,6 +51,7 @@ source_set("unit_tests") { ...@@ -51,6 +51,7 @@ source_set("unit_tests") {
"//ios/web", "//ios/web",
"//ios/web/public/test", "//ios/web/public/test",
"//net:test_support", "//net:test_support",
"//services/network:test_support",
"//testing/gtest", "//testing/gtest",
] ]
} }
...@@ -14,16 +14,16 @@ ...@@ -14,16 +14,16 @@
#include "base/scoped_observer.h" #include "base/scoped_observer.h"
#include "base/timer/timer.h" #include "base/timer/timer.h"
#include "base/version.h" #include "base/version.h"
#include "net/url_request/url_fetcher_delegate.h"
namespace base { namespace base {
class DictionaryValue; class DictionaryValue;
} }
namespace net { namespace network {
class URLFetcher; class SharedURLLoaderFactory;
class URLRequestContextGetter; class SharedURLLoaderFactoryInfo;
} class SimpleURLLoader;
} // namespace network
struct UpgradeRecommendedDetails; struct UpgradeRecommendedDetails;
...@@ -31,15 +31,16 @@ struct UpgradeRecommendedDetails; ...@@ -31,15 +31,16 @@ struct UpgradeRecommendedDetails;
// handles all the scheduling necessary to contact the server regularly. // handles all the scheduling necessary to contact the server regularly.
// All methods, but the constructor, |GetInstance| and |Start| methods, must be // All methods, but the constructor, |GetInstance| and |Start| methods, must be
// called from the IO thread. // called from the IO thread.
class OmahaService : public net::URLFetcherDelegate { class OmahaService {
public: public:
// Called when an upgrade is recommended. // Called when an upgrade is recommended.
using UpgradeRecommendedCallback = using UpgradeRecommendedCallback =
base::Callback<void(const UpgradeRecommendedDetails&)>; base::Callback<void(const UpgradeRecommendedDetails&)>;
// Starts the service. Also set the |URLRequestContextGetter| necessary to // Starts the service. Also set the |URLLoaderFactory| necessary to
// access the Omaha server. This method should only be called once. // access the Omaha server. This method should only be called once.
static void Start(net::URLRequestContextGetter* request_context_getter, static void Start(std::unique_ptr<network::SharedURLLoaderFactoryInfo>
url_loader_factory_info,
const UpgradeRecommendedCallback& callback); const UpgradeRecommendedCallback& callback);
// Returns debug information about the omaha service. // Returns debug information about the omaha service.
...@@ -78,8 +79,7 @@ class OmahaService : public net::URLFetcherDelegate { ...@@ -78,8 +79,7 @@ class OmahaService : public net::URLFetcherDelegate {
// Initialize the timer. Used on startup. // Initialize the timer. Used on startup.
void Initialize(); void Initialize();
// net::URLFetcherDelegate void OnURLLoadComplete(std::unique_ptr<std::string> response_body);
void OnURLFetchComplete(const net::URLFetcher* fetcher) override;
// Raw GetInstance method. Necessary for using singletons. // Raw GetInstance method. Necessary for using singletons.
static OmahaService* GetInstance(); static OmahaService* GetInstance();
...@@ -88,7 +88,7 @@ class OmahaService : public net::URLFetcherDelegate { ...@@ -88,7 +88,7 @@ class OmahaService : public net::URLFetcherDelegate {
OmahaService(); OmahaService();
// Private constructor, only used for tests. // Private constructor, only used for tests.
explicit OmahaService(bool schedule); explicit OmahaService(bool schedule);
~OmahaService() override; ~OmahaService();
// Returns the time to wait before next attempt. // Returns the time to wait before next attempt.
static base::TimeDelta GetBackOff(uint8_t number_of_tries); static base::TimeDelta GetBackOff(uint8_t number_of_tries);
...@@ -146,12 +146,17 @@ class OmahaService : public net::URLFetcherDelegate { ...@@ -146,12 +146,17 @@ class OmahaService : public net::URLFetcherDelegate {
// called after a successful installation/update ping. // called after a successful installation/update ping.
void ClearInstallRetryRequestId(); void ClearInstallRetryRequestId();
// Initialize the URLLoaderFactory instance (mostly needed for tests).
void InitializeURLLoaderFactory(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
// Clears the all persistent state. Should only be used for testing. // Clears the all persistent state. Should only be used for testing.
static void ClearPersistentStateForTests(); static void ClearPersistentStateForTests();
// To communicate with the Omaha server. // To communicate with the Omaha server.
std::unique_ptr<net::URLFetcher> fetcher_; std::unique_ptr<network::SimpleURLLoader> url_loader_;
net::URLRequestContextGetter* request_context_getter_; std::unique_ptr<network::SharedURLLoaderFactoryInfo> url_loader_factory_info_;
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
// The timer that call this object back when needed. // The timer that call this object back when needed.
base::OneShotTimer timer_; base::OneShotTimer timer_;
......
...@@ -42,6 +42,8 @@ ...@@ -42,6 +42,8 @@
#include "net/base/backoff_entry.h" #include "net/base/backoff_entry.h"
#include "net/base/load_flags.h" #include "net/base/load_flags.h"
#include "net/url_request/url_fetcher.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"
#include "url/gurl.h" #include "url/gurl.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
...@@ -294,15 +296,16 @@ OmahaService* OmahaService::GetInstance() { ...@@ -294,15 +296,16 @@ OmahaService* OmahaService::GetInstance() {
} }
// static // static
void OmahaService::Start(net::URLRequestContextGetter* request_context_getter, void OmahaService::Start(std::unique_ptr<network::SharedURLLoaderFactoryInfo>
url_loader_factory_info,
const UpgradeRecommendedCallback& callback) { const UpgradeRecommendedCallback& callback) {
DCHECK(request_context_getter); DCHECK(url_loader_factory_info);
DCHECK(!callback.is_null()); DCHECK(!callback.is_null());
OmahaService* result = GetInstance(); OmahaService* result = GetInstance();
result->set_upgrade_recommended_callback(callback); result->set_upgrade_recommended_callback(callback);
// This should only be called once. // This should only be called once.
DCHECK(!result->request_context_getter_); DCHECK(!result->url_loader_factory_info_ || !result->url_loader_factory_);
result->request_context_getter_ = request_context_getter; result->url_loader_factory_info_ = std::move(url_loader_factory_info);
result->locale_lang_ = GetApplicationContext()->GetApplicationLocale(); result->locale_lang_ = GetApplicationContext()->GetApplicationLocale();
base::PostTaskWithTraits(FROM_HERE, {web::WebThread::IO}, base::PostTaskWithTraits(FROM_HERE, {web::WebThread::IO},
base::Bind(&OmahaService::SendOrScheduleNextPing, base::Bind(&OmahaService::SendOrScheduleNextPing,
...@@ -310,16 +313,14 @@ void OmahaService::Start(net::URLRequestContextGetter* request_context_getter, ...@@ -310,16 +313,14 @@ void OmahaService::Start(net::URLRequestContextGetter* request_context_getter,
} }
OmahaService::OmahaService() OmahaService::OmahaService()
: request_context_getter_(NULL), : schedule_(true),
schedule_(true),
application_install_date_(0), application_install_date_(0),
sending_install_event_(false) { sending_install_event_(false) {
Initialize(); Initialize();
} }
OmahaService::OmahaService(bool schedule) OmahaService::OmahaService(bool schedule)
: request_context_getter_(NULL), : schedule_(schedule),
schedule_(schedule),
application_install_date_(0), application_install_date_(0),
sending_install_event_(false) { sending_install_event_(false) {
Initialize(); Initialize();
...@@ -524,7 +525,7 @@ std::string OmahaService::GetCurrentPingContent() { ...@@ -524,7 +525,7 @@ std::string OmahaService::GetCurrentPingContent() {
void OmahaService::SendPing() { void OmahaService::SendPing() {
// Check that no request is in progress. // Check that no request is in progress.
DCHECK(!fetcher_); DCHECK(!url_loader_);
GURL url(ios::GetChromeBrowserProvider() GURL url(ios::GetChromeBrowserProvider()
->GetOmahaServiceProvider() ->GetOmahaServiceProvider()
...@@ -533,17 +534,30 @@ void OmahaService::SendPing() { ...@@ -533,17 +534,30 @@ void OmahaService::SendPing() {
return; return;
} }
fetcher_ = net::URLFetcher::Create(0, url, net::URLFetcher::POST, this); // There are 2 situations here:
fetcher_->SetRequestContext(request_context_getter_); // 1) production code, where |url_loader_factory_info_| is used.
fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | // 2) testing code, where the |url_loader_factory_| creation is triggered by
net::LOAD_DO_NOT_SAVE_COOKIES); // the test.
fetcher_->SetUploadData("text/xml", GetCurrentPingContent()); if (url_loader_factory_info_) {
DCHECK(!url_loader_factory_);
url_loader_factory_ = network::SharedURLLoaderFactory::Create(
std::move(url_loader_factory_info_));
}
DCHECK(url_loader_factory_);
auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->url = url;
resource_request->method = "POST";
resource_request->load_flags =
net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES;
// If this is not the first try, notify the omaha server. // If this is not the first try, notify the omaha server.
if (number_of_tries_ && IsNextPingInstallRetry()) { if (number_of_tries_ && IsNextPingInstallRetry()) {
fetcher_->SetExtraRequestHeaders(base::StringPrintf( resource_request->headers.SetHeader(
"X-RequestAge: %lld", "X-RequestAge",
(base::Time::Now() - current_ping_time_).InSeconds())); base::StringPrintf(
"%lld", (base::Time::Now() - current_ping_time_).InSeconds()));
} }
// Update last fail time and number of tries, so that if anything fails // Update last fail time and number of tries, so that if anything fails
...@@ -553,7 +567,12 @@ void OmahaService::SendPing() { ...@@ -553,7 +567,12 @@ void OmahaService::SendPing() {
next_tries_time_ = base::Time::Now() + GetBackOff(number_of_tries_); next_tries_time_ = base::Time::Now() + GetBackOff(number_of_tries_);
PersistStates(); PersistStates();
fetcher_->Start(); url_loader_ = network::SimpleURLLoader::Create(std::move(resource_request),
NO_TRAFFIC_ANNOTATION_YET);
url_loader_->AttachStringForUpload(GetCurrentPingContent(), "text/xml");
url_loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
url_loader_factory_.get(),
base::BindOnce(&OmahaService::OnURLLoadComplete, base::Unretained(this)));
} }
void OmahaService::SendOrScheduleNextPing() { void OmahaService::SendOrScheduleNextPing() {
...@@ -563,8 +582,9 @@ void OmahaService::SendOrScheduleNextPing() { ...@@ -563,8 +582,9 @@ void OmahaService::SendOrScheduleNextPing() {
return; return;
} }
if (schedule_) { if (schedule_) {
timer_.Start(FROM_HERE, next_tries_time_ - now, this, timer_.Start(
&OmahaService::SendPing); FROM_HERE, next_tries_time_ - now,
base::BindOnce(&OmahaService::SendPing, base::Unretained(this)));
} }
} }
...@@ -585,21 +605,19 @@ void OmahaService::PersistStates() { ...@@ -585,21 +605,19 @@ void OmahaService::PersistStates() {
[defaults synchronize]; [defaults synchronize];
} }
void OmahaService::OnURLFetchComplete(const net::URLFetcher* fetcher) { void OmahaService::OnURLLoadComplete(
DCHECK(fetcher_.get() == fetcher); std::unique_ptr<std::string> response_body) {
// Transfer the ownership of fetcher_ to this method. // Reset the loader.
std::unique_ptr<net::URLFetcher> local_fetcher = std::move(fetcher_); url_loader_.reset();
if (fetcher->GetResponseCode() != 200) { if (!response_body) {
DLOG(WARNING) << "Error contacting the Omaha server"; DLOG(WARNING) << "Error contacting the Omaha server";
SendOrScheduleNextPing(); SendOrScheduleNextPing();
return; return;
} }
std::string response; NSData* xml = [NSData dataWithBytes:response_body->data()
bool result = fetcher->GetResponseAsString(&response); length:response_body->length()];
DCHECK(result);
NSData* xml = [NSData dataWithBytes:response.data() length:response.length()];
NSXMLParser* parser = [[NSXMLParser alloc] initWithData:xml]; NSXMLParser* parser = [[NSXMLParser alloc] initWithData:xml];
const std::string application_id = ios::GetChromeBrowserProvider() const std::string application_id = ios::GetChromeBrowserProvider()
->GetOmahaServiceProvider() ->GetOmahaServiceProvider()
...@@ -701,6 +719,11 @@ void OmahaService::ClearInstallRetryRequestId() { ...@@ -701,6 +719,11 @@ void OmahaService::ClearInstallRetryRequestId() {
[defaults synchronize]; [defaults synchronize];
} }
void OmahaService::InitializeURLLoaderFactory(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) {
url_loader_factory_ = url_loader_factory;
}
void OmahaService::ClearPersistentStateForTests() { void OmahaService::ClearPersistentStateForTests() {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:kNextTriesTimesKey]; [defaults removeObjectForKey:kNextTriesTimesKey];
......
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