blob: a68c59edf8ad43340884fe501c9896ca5390ea0b [file] [log] [blame]
[email protected]28c3eeb2012-10-15 05:47:531// Copyright (c) 2012 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
[email protected]c2e2b6d2013-01-22 02:23:295#include "apps/app_restore_service.h"
[email protected]28c3eeb2012-10-15 05:47:536
[email protected]4b7111f22013-06-18 14:22:127#include "apps/app_lifetime_monitor_factory.h"
[email protected]a2886e8b2013-06-08 05:15:028#include "apps/app_restore_service_factory.h"
[email protected]24c81d692013-08-07 14:09:489#include "apps/launcher.h"
[email protected]961745f2013-05-25 14:09:2410#include "apps/saved_files_service.h"
[email protected]fdf40f3e2013-07-11 23:55:4611#include "chrome/browser/chrome_notification_types.h"
[email protected]367d9b172013-12-03 00:31:0212#include "chrome/browser/profiles/profile.h"
hashimotoad3c6872014-08-29 09:46:5713#include "extensions/browser/app_window/app_window.h"
[email protected]22401dc2014-03-21 01:38:5714#include "extensions/browser/extension_host.h"
[email protected]489db0842014-01-22 18:20:0315#include "extensions/browser/extension_prefs.h"
[email protected]2d9f2a792014-01-24 12:44:0916#include "extensions/browser/extension_registry.h"
[email protected]e4452d32013-11-15 23:07:4117#include "extensions/common/extension.h"
[email protected]289c44b2013-12-17 03:26:5718#include "extensions/common/extension_set.h"
[email protected]28c3eeb2012-10-15 05:47:5319
[email protected]c2e2b6d2013-01-22 02:23:2920using extensions::Extension;
21using extensions::ExtensionHost;
22using extensions::ExtensionPrefs;
[email protected]2d9f2a792014-01-24 12:44:0923using extensions::ExtensionRegistry;
[email protected]c2e2b6d2013-01-22 02:23:2924
25namespace apps {
[email protected]28c3eeb2012-10-15 05:47:5326
[email protected]24ced7dc02013-04-04 08:32:3927// static
28bool AppRestoreService::ShouldRestoreApps(bool is_browser_restart) {
29 bool should_restore_apps = is_browser_restart;
30#if defined(OS_CHROMEOS)
31 // Chromeos always restarts apps, even if it was a regular shutdown.
32 should_restore_apps = true;
[email protected]24ced7dc02013-04-04 08:32:3933#endif
34 return should_restore_apps;
35}
36
[email protected]28c3eeb2012-10-15 05:47:5337AppRestoreService::AppRestoreService(Profile* profile)
38 : profile_(profile) {
[email protected]4b7111f22013-06-18 14:22:1239 StartObservingAppLifetime();
[email protected]28c3eeb2012-10-15 05:47:5340}
41
[email protected]300ba0c42012-12-06 06:57:1742void AppRestoreService::HandleStartup(bool should_restore_apps) {
[email protected]2d9f2a792014-01-24 12:44:0943 const extensions::ExtensionSet& extensions =
44 ExtensionRegistry::Get(profile_)->enabled_extensions();
45 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_);
[email protected]28c3eeb2012-10-15 05:47:5346
[email protected]2d9f2a792014-01-24 12:44:0947 for (extensions::ExtensionSet::const_iterator it = extensions.begin();
48 it != extensions.end(); ++it) {
[email protected]cadac622013-06-11 16:46:3649 const Extension* extension = it->get();
[email protected]119454622012-10-18 09:48:3250 if (extension_prefs->IsExtensionRunning(extension->id())) {
51 RecordAppStop(extension->id());
[email protected]961745f2013-05-25 14:09:2452 // If we are not restoring apps (e.g., because it is a clean restart), and
53 // the app does not have retain permission, explicitly clear the retained
54 // entries queue.
55 if (should_restore_apps) {
[email protected]cadac622013-06-11 16:46:3656 RestoreApp(it->get());
[email protected]961745f2013-05-25 14:09:2457 } else {
58 SavedFilesService::Get(profile_)->ClearQueueIfNoRetainPermission(
59 extension);
60 }
[email protected]119454622012-10-18 09:48:3261 }
[email protected]28c3eeb2012-10-15 05:47:5362 }
63}
64
[email protected]a2886e8b2013-06-08 05:15:0265bool AppRestoreService::IsAppRestorable(const std::string& extension_id) {
[email protected]2d9f2a792014-01-24 12:44:0966 return ExtensionPrefs::Get(profile_)->IsExtensionRunning(extension_id);
[email protected]a2886e8b2013-06-08 05:15:0267}
68
69// static
70AppRestoreService* AppRestoreService::Get(Profile* profile) {
71 return apps::AppRestoreServiceFactory::GetForProfile(profile);
72}
73
[email protected]4b7111f22013-06-18 14:22:1274void AppRestoreService::OnAppStart(Profile* profile,
75 const std::string& app_id) {
76 RecordAppStart(app_id);
[email protected]28c3eeb2012-10-15 05:47:5377}
78
[email protected]4b7111f22013-06-18 14:22:1279void AppRestoreService::OnAppActivated(Profile* profile,
80 const std::string& app_id) {
81 RecordAppActiveState(app_id, true);
[email protected]771c8d272013-05-17 09:47:4082}
83
[email protected]4b7111f22013-06-18 14:22:1284void AppRestoreService::OnAppDeactivated(Profile* profile,
85 const std::string& app_id) {
86 RecordAppActiveState(app_id, false);
[email protected]771c8d272013-05-17 09:47:4087}
88
[email protected]4b7111f22013-06-18 14:22:1289void AppRestoreService::OnAppStop(Profile* profile, const std::string& app_id) {
90 RecordAppStop(app_id);
91}
92
93void AppRestoreService::OnChromeTerminating() {
94 // We want to preserve the state when the app begins terminating, so stop
95 // listening to app lifetime events.
96 StopObservingAppLifetime();
[email protected]771c8d272013-05-17 09:47:4097}
98
99void AppRestoreService::Shutdown() {
[email protected]4b7111f22013-06-18 14:22:12100 StopObservingAppLifetime();
[email protected]771c8d272013-05-17 09:47:40101}
[email protected]28c3eeb2012-10-15 05:47:53102
103void AppRestoreService::RecordAppStart(const std::string& extension_id) {
[email protected]2d9f2a792014-01-24 12:44:09104 ExtensionPrefs::Get(profile_)->SetExtensionRunning(extension_id, true);
[email protected]28c3eeb2012-10-15 05:47:53105}
106
107void AppRestoreService::RecordAppStop(const std::string& extension_id) {
[email protected]2d9f2a792014-01-24 12:44:09108 ExtensionPrefs::Get(profile_)->SetExtensionRunning(extension_id, false);
[email protected]28c3eeb2012-10-15 05:47:53109}
110
[email protected]4b7111f22013-06-18 14:22:12111void AppRestoreService::RecordAppActiveState(const std::string& id,
112 bool is_active) {
[email protected]2d9f2a792014-01-24 12:44:09113 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_);
[email protected]771c8d272013-05-17 09:47:40114
115 // If the extension isn't running then we will already have recorded whether
[email protected]4b7111f22013-06-18 14:22:12116 // it is active or not.
[email protected]771c8d272013-05-17 09:47:40117 if (!extension_prefs->IsExtensionRunning(id))
118 return;
119
[email protected]4b7111f22013-06-18 14:22:12120 extension_prefs->SetIsActive(id, is_active);
[email protected]771c8d272013-05-17 09:47:40121}
122
[email protected]961745f2013-05-25 14:09:24123void AppRestoreService::RestoreApp(const Extension* extension) {
[email protected]24c81d692013-08-07 14:09:48124 RestartPlatformApp(profile_, extension);
[email protected]28c3eeb2012-10-15 05:47:53125}
126
[email protected]4b7111f22013-06-18 14:22:12127void AppRestoreService::StartObservingAppLifetime() {
128 AppLifetimeMonitor* app_lifetime_monitor =
129 AppLifetimeMonitorFactory::GetForProfile(profile_);
130 DCHECK(app_lifetime_monitor);
131 app_lifetime_monitor->AddObserver(this);
[email protected]771c8d272013-05-17 09:47:40132}
133
[email protected]4b7111f22013-06-18 14:22:12134void AppRestoreService::StopObservingAppLifetime() {
135 AppLifetimeMonitor* app_lifetime_monitor =
136 AppLifetimeMonitorFactory::GetForProfile(profile_);
137 // This might be NULL in tests.
138 if (app_lifetime_monitor)
139 app_lifetime_monitor->RemoveObserver(this);
[email protected]771c8d272013-05-17 09:47:40140}
141
[email protected]c2e2b6d2013-01-22 02:23:29142} // namespace apps