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
|
// 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/kiosk_app_update_service.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "chrome/browser/app_mode/app_mode_utils.h"
#include "chrome/browser/ash/app_mode/kiosk_chrome_app_manager.h"
#include "chrome/browser/ash/system/automatic_reboot_manager.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part_ash.h"
#include "chrome/browser/extensions/updater/extension_updater.h"
#include "chrome/browser/extensions/updater/extension_updater_factory.h"
#include "chrome/browser/lifetime/application_lifetime_desktop.h"
#include "chrome/browser/profiles/profile.h"
#include "extensions/browser/api/runtime/runtime_api.h"
#include "extensions/browser/extension_system_provider.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/common/extension.h"
namespace ash {
namespace {
// How low to wait after an update is available before we force a restart.
const int kForceRestartWaitTimeMs = 24 * 3600 * 1000; // 24 hours.
} // namespace
const char kKioskPrimaryAppInSessionUpdateHistogram[] =
"Kiosk.ChromeApp.PrimaryAppInSessionUpdate";
KioskAppUpdateService::KioskAppUpdateService(
Profile* profile,
system::AutomaticRebootManager* automatic_reboot_manager)
: profile_(profile), automatic_reboot_manager_(automatic_reboot_manager) {}
KioskAppUpdateService::~KioskAppUpdateService() = default;
void KioskAppUpdateService::Init(const std::string& app_id) {
DCHECK(app_id_.empty());
app_id_ = app_id;
update_observation_.Observe(extensions::ExtensionUpdater::Get(profile_));
if (automatic_reboot_manager_) {
automatic_reboot_manager_->AddObserver(this);
}
if (KioskChromeAppManager::IsInitialized()) {
KioskChromeAppManager::Get()->AddObserver(this);
}
if (automatic_reboot_manager_->reboot_requested()) {
OnRebootRequested(automatic_reboot_manager_->reboot_reason());
}
}
void KioskAppUpdateService::StartAppUpdateRestartTimer() {
base::UmaHistogramCounts100(kKioskPrimaryAppInSessionUpdateHistogram, 1);
if (restart_timer_.IsRunning()) {
return;
}
// Setup timer to force restart once the wait period expires.
restart_timer_.Start(FROM_HERE, base::Milliseconds(kForceRestartWaitTimeMs),
this, &KioskAppUpdateService::ForceAppUpdateRestart);
}
void KioskAppUpdateService::ForceAppUpdateRestart() {
// Force a chrome restart (not a logout or reboot) by closing all browsers.
LOG(WARNING) << "Force closing all browsers to update kiosk app.";
chrome::CloseAllBrowsersAndQuit();
}
void KioskAppUpdateService::Shutdown() {
update_observation_.Reset();
if (KioskChromeAppManager::IsInitialized()) {
KioskChromeAppManager::Get()->RemoveObserver(this);
}
if (automatic_reboot_manager_) {
automatic_reboot_manager_->RemoveObserver(this);
}
}
void KioskAppUpdateService::OnAppUpdateAvailable(
const extensions::Extension& extension) {
if (extension.id() != app_id_) {
return;
}
// Clears cached app data so that it will be reloaded if update from app
// does not finish in this run.
KioskChromeAppManager::Get()->ClearAppData(app_id_);
KioskChromeAppManager::Get()->UpdateAppDataFromProfile(app_id_, profile_,
&extension);
extensions::RuntimeEventRouter::DispatchOnRestartRequiredEvent(
profile_, app_id_,
extensions::api::runtime::OnRestartRequiredReason::kAppUpdate);
StartAppUpdateRestartTimer();
}
void KioskAppUpdateService::OnRebootRequested(Reason reason) {
extensions::api::runtime::OnRestartRequiredReason restart_reason =
extensions::api::runtime::OnRestartRequiredReason::kNone;
switch (reason) {
case REBOOT_REASON_OS_UPDATE:
restart_reason =
extensions::api::runtime::OnRestartRequiredReason::kOsUpdate;
break;
case REBOOT_REASON_PERIODIC:
restart_reason =
extensions::api::runtime::OnRestartRequiredReason::kPeriodic;
break;
default:
NOTREACHED() << "Unknown reboot reason=" << reason;
}
extensions::RuntimeEventRouter::DispatchOnRestartRequiredEvent(
profile_, app_id_, restart_reason);
}
void KioskAppUpdateService::WillDestroyAutomaticRebootManager() {
automatic_reboot_manager_->RemoveObserver(this);
automatic_reboot_manager_ = nullptr;
}
void KioskAppUpdateService::OnKioskAppCacheUpdated(const std::string& app_id) {
if (app_id != app_id_) {
return;
}
extensions::RuntimeEventRouter::DispatchOnRestartRequiredEvent(
profile_, app_id_,
extensions::api::runtime::OnRestartRequiredReason::kAppUpdate);
StartAppUpdateRestartTimer();
}
KioskAppUpdateServiceFactory::KioskAppUpdateServiceFactory()
: ProfileKeyedServiceFactory(
"KioskAppUpdateService",
ProfileSelections::Builder()
.WithRegular(ProfileSelection::kOriginalOnly)
// TODO(crbug.com/40257657): Check if this service is needed in
// Guest mode.
.WithGuest(ProfileSelection::kOriginalOnly)
// TODO(crbug.com/41488885): Check if this service is needed for
// Ash Internals.
.WithAshInternals(ProfileSelection::kOriginalOnly)
.Build()) {
DependsOn(extensions::ExtensionUpdaterFactory::GetInstance());
}
KioskAppUpdateServiceFactory::~KioskAppUpdateServiceFactory() = default;
// static
KioskAppUpdateService* KioskAppUpdateServiceFactory::GetForProfile(
Profile* profile) {
// This should never be called unless we are running in forced app mode.
DCHECK(IsRunningInForcedAppMode());
if (!IsRunningInForcedAppMode()) {
return nullptr;
}
return static_cast<KioskAppUpdateService*>(
GetInstance()->GetServiceForBrowserContext(profile, true));
}
// static
KioskAppUpdateServiceFactory* KioskAppUpdateServiceFactory::GetInstance() {
return base::Singleton<KioskAppUpdateServiceFactory>::get();
}
std::unique_ptr<KeyedService>
KioskAppUpdateServiceFactory::BuildServiceInstanceForBrowserContext(
content::BrowserContext* context) const {
return std::make_unique<KioskAppUpdateService>(
Profile::FromBrowserContext(context),
g_browser_process->platform_part()->automatic_reboot_manager());
}
} // namespace ash
|