Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / app_mode / kiosk_app_update_service.cc
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/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"
22
23 namespace chromeos {
24
25 namespace {
26
27 // How low to wait after an update is available before we force a restart.
28 const int kForceRestartWaitTimeMs = 24 * 3600 * 1000;  // 24 hours.
29
30 }  // namespace
31
32 KioskAppUpdateService::KioskAppUpdateService(
33     Profile* profile,
34     system::AutomaticRebootManager* automatic_reboot_manager)
35     : profile_(profile),
36       automatic_reboot_manager_(automatic_reboot_manager) {
37   ExtensionService* service =
38       extensions::ExtensionSystem::Get(profile_)->extension_service();
39   if (service)
40     service->AddUpdateObserver(this);
41
42   if (automatic_reboot_manager_)
43     automatic_reboot_manager_->AddObserver(this);
44 }
45
46 KioskAppUpdateService::~KioskAppUpdateService() {
47 }
48
49 void KioskAppUpdateService::StartAppUpdateRestartTimer() {
50   if (restart_timer_.IsRunning())
51     return;
52
53   // Setup timer to force restart once the wait period expires.
54   restart_timer_.Start(
55       FROM_HERE, base::TimeDelta::FromMilliseconds(kForceRestartWaitTimeMs),
56       this, &KioskAppUpdateService::ForceAppUpdateRestart);
57 }
58
59 void KioskAppUpdateService::ForceAppUpdateRestart() {
60   // Force a chrome restart (not a logout or reboot) by closing all browsers.
61   LOG(WARNING) << "Force closing all browsers to update kiosk app.";
62   chrome::CloseAllBrowsersAndQuit();
63 }
64
65 void KioskAppUpdateService::Shutdown() {
66   ExtensionService* service =
67       extensions::ExtensionSystem::Get(profile_)->extension_service();
68   if (service)
69     service->RemoveUpdateObserver(this);
70 }
71
72 void KioskAppUpdateService::OnAppUpdateAvailable(
73     const extensions::Extension* extension) {
74   if (extension->id() != app_id_)
75     return;
76
77   // Clears cached app data so that it will be reloaded if update from app
78   // does not finish in this run.
79   KioskAppManager::Get()->ClearAppData(app_id_);
80   KioskAppManager::Get()->UpdateAppDataFromProfile(
81       app_id_, profile_, extension);
82
83   extensions::RuntimeEventRouter::DispatchOnRestartRequiredEvent(
84       profile_,
85       app_id_,
86       extensions::core_api::runtime::OnRestartRequired::REASON_APP_UPDATE);
87
88   StartAppUpdateRestartTimer();
89 }
90
91 void KioskAppUpdateService::OnRebootScheduled(Reason reason) {
92   extensions::core_api::runtime::OnRestartRequired::Reason restart_reason =
93       extensions::core_api::runtime::OnRestartRequired::REASON_NONE;
94   switch (reason) {
95     case REBOOT_REASON_OS_UPDATE:
96       restart_reason =
97           extensions::core_api::runtime::OnRestartRequired::REASON_OS_UPDATE;
98       break;
99     case REBOOT_REASON_PERIODIC:
100       restart_reason =
101           extensions::core_api::runtime::OnRestartRequired::REASON_PERIODIC;
102       break;
103     default:
104       NOTREACHED() << "Unknown reboot reason=" << reason;
105       return;
106   }
107
108   extensions::RuntimeEventRouter::DispatchOnRestartRequiredEvent(
109       profile_, app_id_, restart_reason);
110 }
111
112 void KioskAppUpdateService::WillDestroyAutomaticRebootManager() {
113   automatic_reboot_manager_->RemoveObserver(this);
114   automatic_reboot_manager_ = NULL;
115 }
116
117 KioskAppUpdateServiceFactory::KioskAppUpdateServiceFactory()
118     : BrowserContextKeyedServiceFactory(
119         "KioskAppUpdateService",
120         BrowserContextDependencyManager::GetInstance()) {
121   DependsOn(
122       extensions::ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
123 }
124
125 KioskAppUpdateServiceFactory::~KioskAppUpdateServiceFactory() {
126 }
127
128 // static
129 KioskAppUpdateService* KioskAppUpdateServiceFactory::GetForProfile(
130     Profile* profile) {
131   // This should never be called unless we are running in forced app mode.
132   DCHECK(chrome::IsRunningInForcedAppMode());
133   if (!chrome::IsRunningInForcedAppMode())
134     return NULL;
135
136   return static_cast<KioskAppUpdateService*>(
137       GetInstance()->GetServiceForBrowserContext(profile, true));
138 }
139
140 // static
141 KioskAppUpdateServiceFactory* KioskAppUpdateServiceFactory::GetInstance() {
142   return Singleton<KioskAppUpdateServiceFactory>::get();
143 }
144
145 KeyedService* KioskAppUpdateServiceFactory::BuildServiceInstanceFor(
146     content::BrowserContext* context) const {
147   return new KioskAppUpdateService(
148       Profile::FromBrowserContext(context),
149       g_browser_process->platform_part()->automatic_reboot_manager());
150 }
151
152 }  // namespace chromeos