1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/app_mode/startup_app_launcher.h"
#include <memory>
#include <optional>
#include <string>
#include "base/check.h"
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/notreached.h"
#include "base/syslog_logging.h"
#include "base/types/cxx23_to_underlying.h"
#include "chrome/browser/ash/app_mode/kiosk_app_launch_error.h"
#include "chrome/browser/ash/app_mode/kiosk_app_launcher.h"
#include "chrome/browser/ash/app_mode/kiosk_chrome_app_manager.h"
#include "chrome/browser/chromeos/app_mode/chrome_kiosk_app_installer.h"
#include "chrome/browser/chromeos/app_mode/chrome_kiosk_app_launcher.h"
#include "chrome/browser/profiles/profile.h"
#include "components/crx_file/id_util.h"
#include "net/base/backoff_entry.h"
using chromeos::ChromeKioskAppInstaller;
using chromeos::ChromeKioskAppLauncher;
namespace ash {
namespace {
const int kMaxLaunchAttempt = 5;
// Reduced backoff policy for extension downloader while Kiosk is launching.
const net::BackoffEntry::Policy kKioskLaunchExtensionBackoffPolicy = {
.num_errors_to_ignore = 0,
.initial_delay_ms = 2000,
.multiply_factor = 2,
.jitter_factor = 0.1,
.maximum_backoff_ms = 3000,
.entry_lifetime_ms = -1,
.always_use_initial_delay = false,
};
} // namespace
StartupAppLauncher::StartupAppLauncher(
Profile* profile,
const std::string& app_id,
bool should_skip_install,
StartupAppLauncher::NetworkDelegate* network_delegate)
: KioskAppLauncher(network_delegate),
profile_(profile),
app_id_(app_id),
should_skip_install_(should_skip_install) {
CHECK(profile_);
DCHECK(crx_file::id_util::IdIsValid(app_id_));
// Reduce extension downloader retry backoff to avoid waiting on splash screen
// for a long time.
KioskChromeAppManager::Get()->SetExtensionDownloaderBackoffPolicy(
kKioskLaunchExtensionBackoffPolicy);
}
StartupAppLauncher::~StartupAppLauncher() {
// Restore to default extension downloader backoff policy.
KioskChromeAppManager::Get()->SetExtensionDownloaderBackoffPolicy(
std::nullopt);
}
void StartupAppLauncher::AddObserver(KioskAppLauncher::Observer* observer) {
observers_.AddObserver(observer);
}
void StartupAppLauncher::RemoveObserver(KioskAppLauncher::Observer* observer) {
observers_.RemoveObserver(observer);
}
void StartupAppLauncher::Initialize() {
CHECK(state_ != LaunchState::kReadyToLaunch &&
state_ != LaunchState::kWaitingForWindow &&
state_ != LaunchState::kLaunchSucceeded);
if (should_skip_install_) {
OnInstallSuccess();
return;
}
// Update the offline enabled crx cache if the network is ready;
// or just install the app.
if (delegate_->IsNetworkReady()) {
ContinueWithNetworkReady();
} else {
BeginInstall();
}
}
void StartupAppLauncher::ContinueWithNetworkReady() {
SYSLOG(INFO) << "ContinueWithNetworkReady"
<< ", state_=" << base::to_underlying(state_);
if (state_ != LaunchState::kInitializingNetwork &&
state_ != LaunchState::kNotStarted) {
return;
}
if (should_skip_install_) {
OnInstallSuccess();
return;
}
// The network might not be ready when KioskChromeAppManager tries to update
// external cache initially. Update the external cache now that the network
// is ready for sure.
state_ = LaunchState::kWaitingForCache;
kiosk_app_manager_observation_.Observe(KioskChromeAppManager::Get());
KioskChromeAppManager::Get()->UpdateExternalCache();
}
bool StartupAppLauncher::RetryWhenNetworkIsAvailable() {
++launch_attempt_;
if (launch_attempt_ < kMaxLaunchAttempt) {
state_ = LaunchState::kInitializingNetwork;
delegate_->InitializeNetwork();
return true;
}
return false;
}
void StartupAppLauncher::OnKioskExtensionLoadedInCache(
const std::string& app_id) {
OnKioskAppDataLoadStatusChanged(app_id);
}
void StartupAppLauncher::OnKioskExtensionDownloadFailed(
const std::string& app_id) {
OnKioskAppDataLoadStatusChanged(app_id);
}
void StartupAppLauncher::OnKioskAppDataLoadStatusChanged(
const std::string& app_id) {
CHECK_EQ(state_, LaunchState::kWaitingForCache);
if (app_id != app_id_) {
return;
}
kiosk_app_manager_observation_.Reset();
if (KioskChromeAppManager::Get()->HasCachedCrx(app_id_)) {
BeginInstall();
} else {
OnLaunchFailure(KioskAppLaunchError::Error::kUnableToDownload);
}
}
void StartupAppLauncher::BeginInstall() {
state_ = LaunchState::kInstallingApp;
observers_.NotifyAppInstalling();
installer_ = std::make_unique<ChromeKioskAppInstaller>(
profile_,
KioskChromeAppManager::Get()->CreatePrimaryAppInstallData(app_id_));
installer_->BeginInstall(base::BindOnce(
&StartupAppLauncher::OnInstallComplete, weak_ptr_factory_.GetWeakPtr()));
}
void StartupAppLauncher::OnInstallComplete(
ChromeKioskAppInstaller::InstallResult result) {
CHECK_EQ(state_, LaunchState::kInstallingApp);
installer_.reset();
switch (result) {
case ChromeKioskAppInstaller::InstallResult::kSuccess:
OnInstallSuccess();
return;
case ChromeKioskAppInstaller::InstallResult::kPrimaryAppUpdateFailed:
SYSLOG(WARNING) << "Primary app update failed, proceeding anyways";
OnInstallSuccess();
return;
case ChromeKioskAppInstaller::InstallResult::kSecondaryAppUpdateFailed:
SYSLOG(WARNING) << "Secondary app update failed, proceeding anyways";
OnInstallSuccess();
return;
case ChromeKioskAppInstaller::InstallResult::kPrimaryAppInstallFailed:
OnLaunchFailure(KioskAppLaunchError::Error::kUnableToInstall);
return;
case ChromeKioskAppInstaller::InstallResult::kPrimaryAppNotKioskEnabled:
OnLaunchFailure(KioskAppLaunchError::Error::kNotKioskEnabled);
return;
case ChromeKioskAppInstaller::InstallResult::kPrimaryAppNotCached:
case ChromeKioskAppInstaller::InstallResult::kSecondaryAppInstallFailed:
if (!RetryWhenNetworkIsAvailable()) {
OnLaunchFailure(KioskAppLaunchError::Error::kUnableToInstall);
}
return;
case ChromeKioskAppInstaller::InstallResult::kUnknown:
SYSLOG(ERROR) << "Received unknown InstallResult";
OnLaunchFailure(KioskAppLaunchError::Error::kUnableToInstall);
return;
}
}
void StartupAppLauncher::OnInstallSuccess() {
state_ = LaunchState::kReadyToLaunch;
observers_.NotifyAppPrepared();
}
void StartupAppLauncher::LaunchApp() {
if (state_ != LaunchState::kReadyToLaunch) {
SYSLOG(ERROR) << "LaunchApp() called but launcher is not initialized.";
NOTREACHED();
}
launcher_ = std::make_unique<ChromeKioskAppLauncher>(
profile_, app_id_, delegate_->IsNetworkReady());
launcher_->LaunchApp(base::BindOnce(&StartupAppLauncher::OnLaunchComplete,
weak_ptr_factory_.GetWeakPtr()));
}
void StartupAppLauncher::OnLaunchComplete(
ChromeKioskAppLauncher::LaunchResult result) {
CHECK_EQ(state_, LaunchState::kReadyToLaunch);
launcher_.reset();
switch (result) {
case ChromeKioskAppLauncher::LaunchResult::kSuccess:
OnLaunchSuccess();
return;
case ChromeKioskAppLauncher::LaunchResult::kUnableToLaunch:
OnLaunchFailure(KioskAppLaunchError::Error::kUnableToLaunch);
return;
case ChromeKioskAppLauncher::LaunchResult::kNetworkMissing:
if (!RetryWhenNetworkIsAvailable()) {
OnLaunchFailure(KioskAppLaunchError::Error::kUnableToLaunch);
}
return;
case ChromeKioskAppLauncher::LaunchResult::kChromeAppDeprecated:
OnLaunchFailure(KioskAppLaunchError::Error::kChromeAppDeprecated);
return;
case ChromeKioskAppLauncher::LaunchResult::kUnknown:
SYSLOG(ERROR) << "Received unknown LaunchResult";
OnLaunchFailure(KioskAppLaunchError::Error::kUnableToLaunch);
return;
}
}
void StartupAppLauncher::OnLaunchSuccess() {
state_ = LaunchState::kLaunchSucceeded;
observers_.NotifyAppLaunched();
observers_.NotifyAppWindowCreated();
}
void StartupAppLauncher::OnLaunchFailure(KioskAppLaunchError::Error error) {
SYSLOG(ERROR) << "App launch failed, error: " << static_cast<int>(error);
CHECK_NE(KioskAppLaunchError::Error::kNone, error);
observers_.NotifyLaunchFailed(error);
}
} // namespace ash
|