[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" |
[email protected] | fdf40f3e | 2013-07-11 23:55:46 | [diff] [blame] | 11 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 367d9b17 | 2013-12-03 00:31:02 | [diff] [blame] | 12 | #include "chrome/browser/profiles/profile.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; |
| 30 | #if defined(OS_CHROMEOS) |
| 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 | |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 37 | AppRestoreService::AppRestoreService(Profile* profile) |
| 38 | : profile_(profile) { |
[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 = |
| 44 | ExtensionRegistry::Get(profile_)->enabled_extensions(); |
| 45 | ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_); |
[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 { |
| 58 | SavedFilesService::Get(profile_)->ClearQueueIfNoRetainPermission( |
| 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) { |
[email protected] | 2d9f2a79 | 2014-01-24 12:44:09 | [diff] [blame] | 66 | return ExtensionPrefs::Get(profile_)->IsExtensionRunning(extension_id); |
[email protected] | a2886e8b | 2013-06-08 05:15:02 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // static |
| 70 | AppRestoreService* AppRestoreService::Get(Profile* profile) { |
| 71 | return apps::AppRestoreServiceFactory::GetForProfile(profile); |
| 72 | } |
| 73 | |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 74 | void AppRestoreService::OnAppStart(Profile* profile, |
| 75 | const std::string& app_id) { |
| 76 | RecordAppStart(app_id); |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 77 | } |
| 78 | |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 79 | void AppRestoreService::OnAppActivated(Profile* profile, |
| 80 | const std::string& app_id) { |
| 81 | RecordAppActiveState(app_id, true); |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 82 | } |
| 83 | |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 84 | void AppRestoreService::OnAppDeactivated(Profile* profile, |
| 85 | const std::string& app_id) { |
| 86 | RecordAppActiveState(app_id, false); |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 87 | } |
| 88 | |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 89 | void AppRestoreService::OnAppStop(Profile* profile, const std::string& app_id) { |
| 90 | RecordAppStop(app_id); |
| 91 | } |
| 92 | |
| 93 | void 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] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void AppRestoreService::Shutdown() { |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 100 | StopObservingAppLifetime(); |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 101 | } |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 102 | |
| 103 | void AppRestoreService::RecordAppStart(const std::string& extension_id) { |
[email protected] | 2d9f2a79 | 2014-01-24 12:44:09 | [diff] [blame] | 104 | ExtensionPrefs::Get(profile_)->SetExtensionRunning(extension_id, true); |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void AppRestoreService::RecordAppStop(const std::string& extension_id) { |
[email protected] | 2d9f2a79 | 2014-01-24 12:44:09 | [diff] [blame] | 108 | ExtensionPrefs::Get(profile_)->SetExtensionRunning(extension_id, false); |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 111 | void AppRestoreService::RecordAppActiveState(const std::string& id, |
| 112 | bool is_active) { |
[email protected] | 2d9f2a79 | 2014-01-24 12:44:09 | [diff] [blame] | 113 | ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_); |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 114 | |
| 115 | // If the extension isn't running then we will already have recorded whether |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 116 | // it is active or not. |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 117 | if (!extension_prefs->IsExtensionRunning(id)) |
| 118 | return; |
| 119 | |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 120 | extension_prefs->SetIsActive(id, is_active); |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 123 | void AppRestoreService::RestoreApp(const Extension* extension) { |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 124 | RestartPlatformApp(profile_, extension); |
[email protected] | 28c3eeb | 2012-10-15 05:47:53 | [diff] [blame] | 125 | } |
| 126 | |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 127 | void AppRestoreService::StartObservingAppLifetime() { |
| 128 | AppLifetimeMonitor* app_lifetime_monitor = |
| 129 | AppLifetimeMonitorFactory::GetForProfile(profile_); |
| 130 | DCHECK(app_lifetime_monitor); |
| 131 | app_lifetime_monitor->AddObserver(this); |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 132 | } |
| 133 | |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 134 | void 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] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 140 | } |
| 141 | |
[email protected] | c2e2b6d | 2013-01-22 02:23:29 | [diff] [blame] | 142 | } // namespace apps |