• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/chromeos/app_mode/kiosk_app_update_service.h"
6 
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"
21 
22 namespace chromeos {
23 
24 namespace {
25 
26 // How low to wait after an update is available before we force a restart.
27 const int kForceRestartWaitTimeMs = 24 * 3600 * 1000;  // 24 hours.
28 
29 }  // namespace
30 
KioskAppUpdateService(Profile * profile,system::AutomaticRebootManager * automatic_reboot_manager)31 KioskAppUpdateService::KioskAppUpdateService(
32     Profile* profile,
33     system::AutomaticRebootManager* automatic_reboot_manager)
34     : profile_(profile),
35       automatic_reboot_manager_(automatic_reboot_manager) {
36   ExtensionService* service =
37       extensions::ExtensionSystem::Get(profile_)->extension_service();
38   if (service)
39     service->AddUpdateObserver(this);
40 
41   if (automatic_reboot_manager_)
42     automatic_reboot_manager_->AddObserver(this);
43 }
44 
~KioskAppUpdateService()45 KioskAppUpdateService::~KioskAppUpdateService() {
46 }
47 
StartAppUpdateRestartTimer()48 void KioskAppUpdateService::StartAppUpdateRestartTimer() {
49   if (restart_timer_.IsRunning())
50     return;
51 
52   // Setup timer to force restart once the wait period expires.
53   restart_timer_.Start(
54       FROM_HERE, base::TimeDelta::FromMilliseconds(kForceRestartWaitTimeMs),
55       this, &KioskAppUpdateService::ForceAppUpdateRestart);
56 }
57 
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();
62 }
63 
Shutdown()64 void KioskAppUpdateService::Shutdown() {
65   ExtensionService* service = profile_->GetExtensionService();
66   if (service)
67     service->RemoveUpdateObserver(this);
68 }
69 
OnAppUpdateAvailable(const extensions::Extension * extension)70 void KioskAppUpdateService::OnAppUpdateAvailable(
71     const extensions::Extension* extension) {
72   if (extension->id() != app_id_)
73     return;
74 
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);
80 
81   extensions::RuntimeEventRouter::DispatchOnRestartRequiredEvent(
82       profile_,
83       app_id_,
84       extensions::api::runtime::OnRestartRequired::REASON_APP_UPDATE);
85 
86   StartAppUpdateRestartTimer();
87 }
88 
OnRebootScheduled(Reason reason)89 void KioskAppUpdateService::OnRebootScheduled(Reason reason) {
90   extensions::api::runtime::OnRestartRequired::Reason restart_reason =
91       extensions::api::runtime::OnRestartRequired::REASON_NONE;
92   switch (reason) {
93     case REBOOT_REASON_OS_UPDATE:
94       restart_reason =
95           extensions::api::runtime::OnRestartRequired::REASON_OS_UPDATE;
96       break;
97     case REBOOT_REASON_PERIODIC:
98       restart_reason =
99           extensions::api::runtime::OnRestartRequired::REASON_PERIODIC;
100       break;
101     default:
102       NOTREACHED() << "Unknown reboot reason=" << reason;
103       return;
104   }
105 
106   extensions::RuntimeEventRouter::DispatchOnRestartRequiredEvent(
107       profile_, app_id_, restart_reason);
108 }
109 
WillDestroyAutomaticRebootManager()110 void KioskAppUpdateService::WillDestroyAutomaticRebootManager() {
111   automatic_reboot_manager_->RemoveObserver(this);
112   automatic_reboot_manager_ = NULL;
113 }
114 
KioskAppUpdateServiceFactory()115 KioskAppUpdateServiceFactory::KioskAppUpdateServiceFactory()
116     : BrowserContextKeyedServiceFactory(
117         "KioskAppUpdateService",
118         BrowserContextDependencyManager::GetInstance()) {
119   DependsOn(extensions::ExtensionSystemFactory::GetInstance());
120 }
121 
~KioskAppUpdateServiceFactory()122 KioskAppUpdateServiceFactory::~KioskAppUpdateServiceFactory() {
123 }
124 
125 // static
GetForProfile(Profile * profile)126 KioskAppUpdateService* KioskAppUpdateServiceFactory::GetForProfile(
127     Profile* profile) {
128   // This should never be called unless we are running in forced app mode.
129   DCHECK(chrome::IsRunningInForcedAppMode());
130   if (!chrome::IsRunningInForcedAppMode())
131     return NULL;
132 
133   return static_cast<KioskAppUpdateService*>(
134       GetInstance()->GetServiceForBrowserContext(profile, true));
135 }
136 
137 // static
GetInstance()138 KioskAppUpdateServiceFactory* KioskAppUpdateServiceFactory::GetInstance() {
139   return Singleton<KioskAppUpdateServiceFactory>::get();
140 }
141 
142 BrowserContextKeyedService*
BuildServiceInstanceFor(content::BrowserContext * context) const143 KioskAppUpdateServiceFactory::BuildServiceInstanceFor(
144     content::BrowserContext* context) const {
145   return new KioskAppUpdateService(
146       Profile::FromBrowserContext(context),
147       g_browser_process->platform_part()->automatic_reboot_manager());
148 }
149 
150 }  // namespace chromeos
151