1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/app_mode/kiosk_app_update_service.h"
7 #include "base/logging.h"
8 #include "chrome/browser/app_mode/app_mode_utils.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/browser_process_platform_part_chromeos.h"
11 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
12 #include "chrome/browser/chromeos/system/automatic_reboot_manager.h"
13 #include "chrome/browser/extensions/api/runtime/runtime_api.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/extension_system.h"
16 #include "chrome/browser/extensions/extension_system_factory.h"
17 #include "chrome/browser/lifetime/application_lifetime.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
20 #include "extensions/common/extension.h"
26 // How low to wait after an update is available before we force a restart.
27 const int kForceRestartWaitTimeMs
= 24 * 3600 * 1000; // 24 hours.
31 KioskAppUpdateService::KioskAppUpdateService(
33 system::AutomaticRebootManager
* automatic_reboot_manager
)
35 automatic_reboot_manager_(automatic_reboot_manager
) {
36 ExtensionService
* service
=
37 extensions::ExtensionSystem::Get(profile_
)->extension_service();
39 service
->AddUpdateObserver(this);
41 if (automatic_reboot_manager_
)
42 automatic_reboot_manager_
->AddObserver(this);
45 KioskAppUpdateService::~KioskAppUpdateService() {
48 void KioskAppUpdateService::StartAppUpdateRestartTimer() {
49 if (restart_timer_
.IsRunning())
52 // Setup timer to force restart once the wait period expires.
54 FROM_HERE
, base::TimeDelta::FromMilliseconds(kForceRestartWaitTimeMs
),
55 this, &KioskAppUpdateService::ForceAppUpdateRestart
);
58 void KioskAppUpdateService::ForceAppUpdateRestart() {
59 // Force a chrome restart (not a logout or reboot) by closing all browsers.
60 LOG(WARNING
) << "Force closing all browsers to update kiosk app.";
61 chrome::CloseAllBrowsersAndQuit();
64 void KioskAppUpdateService::Shutdown() {
65 ExtensionService
* service
= profile_
->GetExtensionService();
67 service
->RemoveUpdateObserver(this);
70 void KioskAppUpdateService::OnAppUpdateAvailable(
71 const extensions::Extension
* extension
) {
72 if (extension
->id() != app_id_
)
75 // Clears cached app data so that it will be reloaded if update from app
76 // does not finish in this run.
77 KioskAppManager::Get()->ClearAppData(app_id_
);
78 KioskAppManager::Get()->UpdateAppDataFromProfile(
79 app_id_
, profile_
, extension
);
81 extensions::RuntimeEventRouter::DispatchOnRestartRequiredEvent(
84 extensions::api::runtime::OnRestartRequired::REASON_APP_UPDATE
);
86 StartAppUpdateRestartTimer();
89 void KioskAppUpdateService::OnRebootScheduled(Reason reason
) {
90 extensions::api::runtime::OnRestartRequired::Reason restart_reason
=
91 extensions::api::runtime::OnRestartRequired::REASON_NONE
;
93 case REBOOT_REASON_OS_UPDATE
:
95 extensions::api::runtime::OnRestartRequired::REASON_OS_UPDATE
;
97 case REBOOT_REASON_PERIODIC
:
99 extensions::api::runtime::OnRestartRequired::REASON_PERIODIC
;
102 NOTREACHED() << "Unknown reboot reason=" << reason
;
106 extensions::RuntimeEventRouter::DispatchOnRestartRequiredEvent(
107 profile_
, app_id_
, restart_reason
);
110 void KioskAppUpdateService::WillDestroyAutomaticRebootManager() {
111 automatic_reboot_manager_
->RemoveObserver(this);
112 automatic_reboot_manager_
= NULL
;
115 KioskAppUpdateServiceFactory::KioskAppUpdateServiceFactory()
116 : BrowserContextKeyedServiceFactory(
117 "KioskAppUpdateService",
118 BrowserContextDependencyManager::GetInstance()) {
119 DependsOn(extensions::ExtensionSystemFactory::GetInstance());
122 KioskAppUpdateServiceFactory::~KioskAppUpdateServiceFactory() {
126 KioskAppUpdateService
* KioskAppUpdateServiceFactory::GetForProfile(
128 // This should never be called unless we are running in forced app mode.
129 DCHECK(chrome::IsRunningInForcedAppMode());
130 if (!chrome::IsRunningInForcedAppMode())
133 return static_cast<KioskAppUpdateService
*>(
134 GetInstance()->GetServiceForBrowserContext(profile
, true));
138 KioskAppUpdateServiceFactory
* KioskAppUpdateServiceFactory::GetInstance() {
139 return Singleton
<KioskAppUpdateServiceFactory
>::get();
142 BrowserContextKeyedService
*
143 KioskAppUpdateServiceFactory::BuildServiceInstanceFor(
144 content::BrowserContext
* context
) const {
145 return new KioskAppUpdateService(
146 Profile::FromBrowserContext(context
),
147 g_browser_process
->platform_part()->automatic_reboot_manager());
150 } // namespace chromeos