[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 1 | // 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] | c2e2b6d | 2013-01-22 02:23:29 | [diff] [blame] | 5 | #include "apps/app_restore_service.h" |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 6 | |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 7 | #include "apps/app_lifetime_monitor_factory.h" |
[email protected] | a2886e8b | 2013-06-08 05:15:02 | [diff] [blame] | 8 | #include "apps/app_restore_service_factory.h" |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 9 | #include "apps/launcher.h" |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 10 | #include "apps/saved_files_service.h" |
Yuta Hijikata | b607d7b | 2020-11-10 07:24:57 | [diff] [blame] | 11 | #include "build/chromeos_buildflags.h" |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 12 | #include "content/public/browser/browser_context.h" |
hashimoto | ad3c687 | 2014-08-29 09:46:57 | [diff] [blame] | 13 | #include "extensions/browser/app_window/app_window.h" |
[email protected] | 22401dc | 2014-03-21 01:38:57 | [diff] [blame] | 14 | #include "extensions/browser/extension_host.h" |
[email protected] | 489db084 | 2014-01-22 18:20:03 | [diff] [blame] | 15 | #include "extensions/browser/extension_prefs.h" |
[email protected] | 2d9f2a79 | 2014-01-24 12:44:09 | [diff] [blame] | 16 | #include "extensions/browser/extension_registry.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 17 | #include "extensions/common/extension.h" |
[email protected] | 289c44b | 2013-12-17 03:26:57 | [diff] [blame] | 18 | #include "extensions/common/extension_set.h" |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 19 | |
[email protected] | c2e2b6d | 2013-01-22 02:23:29 | [diff] [blame] | 20 | using extensions::Extension; |
| 21 | using extensions::ExtensionHost; |
| 22 | using extensions::ExtensionPrefs; |
[email protected] | 2d9f2a79 | 2014-01-24 12:44:09 | [diff] [blame] | 23 | using extensions::ExtensionRegistry; |
[email protected] | c2e2b6d | 2013-01-22 02:23:29 | [diff] [blame] | 24 | |
| 25 | namespace apps { |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 26 | |
[email protected] | 24ced7dc0 | 2013-04-04 08:32:39 | [diff] [blame] | 27 | // static |
| 28 | bool AppRestoreService::ShouldRestoreApps(bool is_browser_restart) { |
| 29 | bool should_restore_apps = is_browser_restart; |
Yuta Hijikata | b607d7b | 2020-11-10 07:24:57 | [diff] [blame] | 30 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
[email protected] | 24ced7dc0 | 2013-04-04 08:32:39 | [diff] [blame] | 31 | // Chromeos always restarts apps, even if it was a regular shutdown. |
| 32 | should_restore_apps = true; |
[email protected] | 24ced7dc0 | 2013-04-04 08:32:39 | [diff] [blame] | 33 | #endif |
| 34 | return should_restore_apps; |
| 35 | } |
| 36 | |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 37 | AppRestoreService::AppRestoreService(content::BrowserContext* context) |
| 38 | : context_(context) { |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 39 | StartObservingAppLifetime(); |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 40 | } |
| 41 | |
[email protected] | 300ba0c4 | 2012-12-06 06:57:17 | [diff] [blame] | 42 | void AppRestoreService::HandleStartup(bool should_restore_apps) { |
[email protected] | 2d9f2a79 | 2014-01-24 12:44:09 | [diff] [blame] | 43 | const extensions::ExtensionSet& extensions = |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 44 | ExtensionRegistry::Get(context_)->enabled_extensions(); |
| 45 | ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context_); |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 46 | |
[email protected] | 2d9f2a79 | 2014-01-24 12:44:09 | [diff] [blame] | 47 | for (extensions::ExtensionSet::const_iterator it = extensions.begin(); |
| 48 | it != extensions.end(); ++it) { |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 49 | const Extension* extension = it->get(); |
[email protected] | 11945462 | 2012-10-18 09:48:32 | [diff] [blame] | 50 | if (extension_prefs->IsExtensionRunning(extension->id())) { |
| 51 | RecordAppStop(extension->id()); |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 52 | // 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] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 56 | RestoreApp(it->get()); |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 57 | } else { |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 58 | SavedFilesService::Get(context_)->ClearQueueIfNoRetainPermission( |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 59 | extension); |
| 60 | } |
[email protected] | 11945462 | 2012-10-18 09:48:32 | [diff] [blame] | 61 | } |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
[email protected] | a2886e8b | 2013-06-08 05:15:02 | [diff] [blame] | 65 | bool AppRestoreService::IsAppRestorable(const std::string& extension_id) { |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 66 | return ExtensionPrefs::Get(context_)->IsExtensionRunning(extension_id); |
[email protected] | a2886e8b | 2013-06-08 05:15:02 | [diff] [blame] | 67 | } |
| 68 | |
michaelpg | 2fc6af9 | 2017-01-13 19:54:18 | [diff] [blame] | 69 | void AppRestoreService::OnApplicationTerminating() { |
| 70 | // We want to preserve the state when the app begins terminating, so stop |
| 71 | // listening to app lifetime events. |
| 72 | StopObservingAppLifetime(); |
| 73 | } |
| 74 | |
[email protected] | a2886e8b | 2013-06-08 05:15:02 | [diff] [blame] | 75 | // static |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 76 | AppRestoreService* AppRestoreService::Get(content::BrowserContext* context) { |
| 77 | return apps::AppRestoreServiceFactory::GetForBrowserContext(context); |
[email protected] | a2886e8b | 2013-06-08 05:15:02 | [diff] [blame] | 78 | } |
| 79 | |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 80 | void AppRestoreService::OnAppStart(content::BrowserContext* context, |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 81 | const std::string& app_id) { |
| 82 | RecordAppStart(app_id); |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 83 | } |
| 84 | |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 85 | void AppRestoreService::OnAppActivated(content::BrowserContext* context, |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 86 | const std::string& app_id) { |
| 87 | RecordAppActiveState(app_id, true); |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 88 | } |
| 89 | |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 90 | void AppRestoreService::OnAppDeactivated(content::BrowserContext* context, |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 91 | const std::string& app_id) { |
| 92 | RecordAppActiveState(app_id, false); |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 93 | } |
| 94 | |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 95 | void AppRestoreService::OnAppStop(content::BrowserContext* context, |
| 96 | const std::string& app_id) { |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 97 | RecordAppStop(app_id); |
| 98 | } |
| 99 | |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 100 | void AppRestoreService::Shutdown() { |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 101 | StopObservingAppLifetime(); |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 102 | } |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 103 | |
| 104 | void AppRestoreService::RecordAppStart(const std::string& extension_id) { |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 105 | ExtensionPrefs::Get(context_)->SetExtensionRunning(extension_id, true); |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void AppRestoreService::RecordAppStop(const std::string& extension_id) { |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 109 | ExtensionPrefs::Get(context_)->SetExtensionRunning(extension_id, false); |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 110 | } |
| 111 | |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 112 | void AppRestoreService::RecordAppActiveState(const std::string& id, |
| 113 | bool is_active) { |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 114 | ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context_); |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 115 | |
| 116 | // If the extension isn't running then we will already have recorded whether |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 117 | // it is active or not. |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 118 | if (!extension_prefs->IsExtensionRunning(id)) |
| 119 | return; |
| 120 | |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 121 | extension_prefs->SetIsActive(id, is_active); |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 122 | } |
| 123 | |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 124 | void AppRestoreService::RestoreApp(const Extension* extension) { |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 125 | RestartPlatformApp(context_, extension); |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 126 | } |
| 127 | |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 128 | void AppRestoreService::StartObservingAppLifetime() { |
| 129 | AppLifetimeMonitor* app_lifetime_monitor = |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 130 | AppLifetimeMonitorFactory::GetForBrowserContext(context_); |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 131 | DCHECK(app_lifetime_monitor); |
| 132 | app_lifetime_monitor->AddObserver(this); |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 133 | } |
| 134 | |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 135 | void AppRestoreService::StopObservingAppLifetime() { |
| 136 | AppLifetimeMonitor* app_lifetime_monitor = |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 137 | AppLifetimeMonitorFactory::GetForBrowserContext(context_); |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 138 | // This might be NULL in tests. |
| 139 | if (app_lifetime_monitor) |
| 140 | app_lifetime_monitor->RemoveObserver(this); |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | c2e2b6d | 2013-01-22 02:23:29 | [diff] [blame] | 143 | } // namespace apps |