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/extension_service.h"
14 #include "chrome/browser/lifetime/application_lifetime.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "components/keyed_service/content/browser_context_dependency_manager.h"
17 #include "extensions/browser/api/runtime/runtime_api.h"
18 #include "extensions/browser/extension_system.h"
19 #include "extensions/browser/extension_system_provider.h"
20 #include "extensions/browser/extensions_browser_client.h"
21 #include "extensions/common/extension.h"
27 // How low to wait after an update is available before we force a restart.
28 const int kForceRestartWaitTimeMs
= 24 * 3600 * 1000; // 24 hours.
32 KioskAppUpdateService::KioskAppUpdateService(
34 system::AutomaticRebootManager
* automatic_reboot_manager
)
36 automatic_reboot_manager_(automatic_reboot_manager
) {
39 KioskAppUpdateService::~KioskAppUpdateService() {
42 void KioskAppUpdateService::Init(const std::string
& app_id
) {
43 DCHECK(app_id_
.empty());
46 ExtensionService
* service
=
47 extensions::ExtensionSystem::Get(profile_
)->extension_service();
49 service
->AddUpdateObserver(this);
51 if (automatic_reboot_manager_
)
52 automatic_reboot_manager_
->AddObserver(this);
54 if (KioskAppManager::Get())
55 KioskAppManager::Get()->AddObserver(this);
57 if (automatic_reboot_manager_
->reboot_requested())
58 OnRebootRequested(automatic_reboot_manager_
->reboot_reason());
61 void KioskAppUpdateService::StartAppUpdateRestartTimer() {
62 if (restart_timer_
.IsRunning())
65 // Setup timer to force restart once the wait period expires.
67 FROM_HERE
, base::TimeDelta::FromMilliseconds(kForceRestartWaitTimeMs
),
68 this, &KioskAppUpdateService::ForceAppUpdateRestart
);
71 void KioskAppUpdateService::ForceAppUpdateRestart() {
72 // Force a chrome restart (not a logout or reboot) by closing all browsers.
73 LOG(WARNING
) << "Force closing all browsers to update kiosk app.";
74 chrome::CloseAllBrowsersAndQuit();
77 void KioskAppUpdateService::Shutdown() {
78 ExtensionService
* service
=
79 extensions::ExtensionSystem::Get(profile_
)->extension_service();
81 service
->RemoveUpdateObserver(this);
82 if (KioskAppManager::Get())
83 KioskAppManager::Get()->RemoveObserver(this);
84 if (automatic_reboot_manager_
)
85 automatic_reboot_manager_
->RemoveObserver(this);
88 void KioskAppUpdateService::OnAppUpdateAvailable(
89 const extensions::Extension
* extension
) {
90 if (extension
->id() != app_id_
)
93 // Clears cached app data so that it will be reloaded if update from app
94 // does not finish in this run.
95 KioskAppManager::Get()->ClearAppData(app_id_
);
96 KioskAppManager::Get()->UpdateAppDataFromProfile(
97 app_id_
, profile_
, extension
);
99 extensions::RuntimeEventRouter::DispatchOnRestartRequiredEvent(
101 extensions::api::runtime::ON_RESTART_REQUIRED_REASON_APP_UPDATE
);
103 StartAppUpdateRestartTimer();
106 void KioskAppUpdateService::OnRebootRequested(Reason reason
) {
107 extensions::api::runtime::OnRestartRequiredReason restart_reason
=
108 extensions::api::runtime::ON_RESTART_REQUIRED_REASON_NONE
;
110 case REBOOT_REASON_OS_UPDATE
:
112 extensions::api::runtime::ON_RESTART_REQUIRED_REASON_OS_UPDATE
;
114 case REBOOT_REASON_PERIODIC
:
116 extensions::api::runtime::ON_RESTART_REQUIRED_REASON_PERIODIC
;
119 NOTREACHED() << "Unknown reboot reason=" << reason
;
123 extensions::RuntimeEventRouter::DispatchOnRestartRequiredEvent(
124 profile_
, app_id_
, restart_reason
);
127 void KioskAppUpdateService::WillDestroyAutomaticRebootManager() {
128 automatic_reboot_manager_
->RemoveObserver(this);
129 automatic_reboot_manager_
= NULL
;
132 void KioskAppUpdateService::OnKioskAppCacheUpdated(const std::string
& app_id
) {
133 if (app_id
!= app_id_
)
136 extensions::RuntimeEventRouter::DispatchOnRestartRequiredEvent(
138 extensions::api::runtime::ON_RESTART_REQUIRED_REASON_APP_UPDATE
);
140 StartAppUpdateRestartTimer();
143 KioskAppUpdateServiceFactory::KioskAppUpdateServiceFactory()
144 : BrowserContextKeyedServiceFactory(
145 "KioskAppUpdateService",
146 BrowserContextDependencyManager::GetInstance()) {
148 extensions::ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
151 KioskAppUpdateServiceFactory::~KioskAppUpdateServiceFactory() {
155 KioskAppUpdateService
* KioskAppUpdateServiceFactory::GetForProfile(
157 // This should never be called unless we are running in forced app mode.
158 DCHECK(chrome::IsRunningInForcedAppMode());
159 if (!chrome::IsRunningInForcedAppMode())
162 return static_cast<KioskAppUpdateService
*>(
163 GetInstance()->GetServiceForBrowserContext(profile
, true));
167 KioskAppUpdateServiceFactory
* KioskAppUpdateServiceFactory::GetInstance() {
168 return Singleton
<KioskAppUpdateServiceFactory
>::get();
171 KeyedService
* KioskAppUpdateServiceFactory::BuildServiceInstanceFor(
172 content::BrowserContext
* context
) const {
173 return new KioskAppUpdateService(
174 Profile::FromBrowserContext(context
),
175 g_browser_process
->platform_part()->automatic_reboot_manager());
178 } // namespace chromeos