Skip to content

Commit

Permalink
ios: Call OmahaService:CheckNow from IO thread.
Browse files Browse the repository at this point in the history
Also adds more DCHECKs for the IO thread.

(cherry picked from commit 26a0204)

Bug: 1400214
Change-Id: I6987fa3aa6169272c896302299e21cf60e84fb1b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4194695
Commit-Queue: Justin Cohen <justincohen@chromium.org>
Reviewed-by: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: Rohit Rao <rohitrao@chromium.org>
Auto-Submit: Justin Cohen <justincohen@chromium.org>
Cr-Original-Commit-Position: refs/heads/main@{#1097560}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4219693
Commit-Queue: Harry Souders <harrysouders@google.com>
Owners-Override: Harry Souders <harrysouders@google.com>
Reviewed-by: Harry Souders <harrysouders@google.com>
Cr-Commit-Position: refs/branch-heads/5481@{#899}
Cr-Branched-From: 130f3e4-refs/heads/main@{#1084008}
  • Loading branch information
Justin Cohen authored and Chromium LUCI CQ committed Feb 2, 2023
1 parent 41bf7e9 commit c59edec
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
2 changes: 0 additions & 2 deletions ios/chrome/app/main_controller.mm
Original file line number Diff line number Diff line change
Expand Up @@ -816,8 +816,6 @@ - (void)activateFirstUserActionRecorderWithBackgroundTime:
}

- (void)stopChromeMain {
OmahaService::Stop();

[_spotlightManager shutdown];
_spotlightManager = nil;

Expand Down
11 changes: 6 additions & 5 deletions ios/chrome/browser/omaha/omaha_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,10 @@ class OmahaService {
OmahaService(const OmahaService&) = delete;
OmahaService& operator=(const OmahaService&) = delete;

// Performs an immediate check to see if the device is up to date. Start must
// have been previously called.
// Posts to CheckNowOnIOThread on IO thread to perform an immediate check
// if the device is up to date.
static void CheckNow(OneOffCallback callback);

// Stops the service in preparation for browser shutdown.
static void Stop();

// Returns debug information about the omaha service.
static void GetDebugInformation(
base::OnceCallback<void(base::Value::Dict)> callback);
Expand Down Expand Up @@ -150,6 +147,10 @@ class OmahaService {
// server. Use the current state of the service to compute the right message.
std::string GetCurrentPingContent();

// Performs an immediate check to see if the device is up to date. Start must
// have been previously called.
void CheckNowOnIOThread(OneOffCallback callback);

// Computes debugging information and fill `result`.
void GetDebugInformationOnIOThread(
base::OnceCallback<void(base::Value::Dict)> callback);
Expand Down
53 changes: 30 additions & 23 deletions ios/chrome/browser/omaha/omaha_service.mm
Original file line number Diff line number Diff line change
Expand Up @@ -380,30 +380,30 @@ - (void)parser:(NSXMLParser*)parser
void OmahaService::CheckNow(OneOffCallback callback) {
DCHECK(!callback.is_null());

OmahaService* service = GetInstance();
DCHECK(service->started_);
if (OmahaService::IsEnabled()) {
OmahaService* service = GetInstance();
web::GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&OmahaService::CheckNowOnIOThread,
base::Unretained(service), std::move(callback)));
}
}

void OmahaService::CheckNowOnIOThread(OneOffCallback callback) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
DCHECK(!callback.is_null());

DCHECK(service->one_off_check_callback_.is_null());
service->one_off_check_callback_ = std::move(callback);
DCHECK(one_off_check_callback_.is_null());
one_off_check_callback_ = std::move(callback);

// If there is not an ongoing ping, send one.
if (!service->url_loader_) {
service->SendPing();
if (!url_loader_) {
SendPing();
} else {
// The one off ping is taking the scheduled one, so the scheduled ping is
// now "canceled".
service->scheduled_ping_canceled_ = true;
}
}

// static
void OmahaService::Stop() {
if (!OmahaService::IsEnabled()) {
return;
scheduled_ping_canceled_ = true;
}

OmahaService* service = GetInstance();
service->StopInternal();
}

OmahaService::OmahaService() : OmahaService(/*schedule=*/true) {}
Expand Down Expand Up @@ -481,12 +481,6 @@ - (void)parser:(NSXMLParser*)parser
PersistStates();
}

void OmahaService::StopInternal() {
if (!started_) {
return;
}
}

// static
void OmahaService::GetDebugInformation(
base::OnceCallback<void(base::Value::Dict)> callback) {
Expand All @@ -506,6 +500,7 @@ - (void)parser:(NSXMLParser*)parser

// static
base::TimeDelta OmahaService::GetBackOff(uint8_t number_of_tries) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
// Configuration for the service exponential backoff
static net::BackoffEntry::Policy kBackoffPolicy = {
0, // num_errors_to_ignore
Expand All @@ -531,6 +526,7 @@ - (void)parser:(NSXMLParser*)parser
const std::string& channelName,
const base::Time& installationTime,
PingContent pingContent) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
XmlWrapper xml_wrapper;

{
Expand Down Expand Up @@ -619,6 +615,7 @@ - (void)parser:(NSXMLParser*)parser
}

std::string OmahaService::GetCurrentPingContent() {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
const base::Version& current_version = version_info::GetVersion();
sending_install_event_ = last_sent_version_ < current_version;
PingContent ping_content =
Expand All @@ -634,6 +631,7 @@ - (void)parser:(NSXMLParser*)parser
}

void OmahaService::SendPing() {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
// If a scheduled ping comes during a one off, drop it.
if (url_loader_ && !one_off_check_callback_.is_null()) {
scheduled_ping_canceled_ = true;
Expand Down Expand Up @@ -689,6 +687,7 @@ - (void)parser:(NSXMLParser*)parser
}

void OmahaService::SendOrScheduleNextPing() {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
base::Time now = base::Time::Now();
if (next_tries_time_ <= now) {
SendPing();
Expand Down Expand Up @@ -724,6 +723,7 @@ - (void)parser:(NSXMLParser*)parser

void OmahaService::OnURLLoadComplete(
std::unique_ptr<std::string> response_body) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
// Reset the loader.
url_loader_.reset();

Expand Down Expand Up @@ -789,6 +789,7 @@ - (void)parser:(NSXMLParser*)parser

void OmahaService::GetDebugInformationOnIOThread(
base::OnceCallback<void(base::Value::Dict)> callback) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
base::Value::Dict result;

result.Set("message", GetCurrentPingContent());
Expand All @@ -814,11 +815,13 @@ - (void)parser:(NSXMLParser*)parser
}

bool OmahaService::IsNextPingInstallRetry() {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
return [[NSUserDefaults standardUserDefaults]
stringForKey:kRetryRequestIdKey] != nil;
}

std::string OmahaService::GetNextPingRequestId(PingContent ping_content) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
NSString* stored_id =
[[NSUserDefaults standardUserDefaults] stringForKey:kRetryRequestIdKey];
if (stored_id) {
Expand All @@ -833,6 +836,7 @@ - (void)parser:(NSXMLParser*)parser
}

void OmahaService::SetInstallRetryRequestId(const std::string& request_id) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:base::SysUTF8ToNSString(request_id)
forKey:kRetryRequestIdKey];
Expand All @@ -841,6 +845,7 @@ - (void)parser:(NSXMLParser*)parser
}

void OmahaService::ClearInstallRetryRequestId() {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:kRetryRequestIdKey];
// Clear critical state information for usage reporting.
Expand All @@ -849,10 +854,12 @@ - (void)parser:(NSXMLParser*)parser

void OmahaService::InitializeURLLoaderFactory(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
url_loader_factory_ = url_loader_factory;
}

void OmahaService::ClearPersistentStateForTests() {
DCHECK_CURRENTLY_ON(web::WebThread::IO);
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:kNextTriesTimesKey];
[defaults removeObjectForKey:kCurrentPingKey];
Expand Down

0 comments on commit c59edec

Please sign in to comment.