blob: faf0e54d19045c8d9835d7db30b5a6fe577ce671 [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"
Yuta Hijikatab607d7b2020-11-10 07:24:5711#include "build/chromeos_buildflags.h"
michaelpg4d80e562017-04-04 01:48:1412#include "content/public/browser/browser_context.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;
Yuta Hijikatab607d7b2020-11-10 07:24:5730#if BUILDFLAG(IS_CHROMEOS_ASH)
[email protected]24ced7dc02013-04-04 08:32:3931 // 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
michaelpg4d80e562017-04-04 01:48:1437AppRestoreService::AppRestoreService(content::BrowserContext* context)
38 : context_(context) {
[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 =
michaelpg4d80e562017-04-04 01:48:1444 ExtensionRegistry::Get(context_)->enabled_extensions();
45 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context_);
[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 {
michaelpg4d80e562017-04-04 01:48:1458 SavedFilesService::Get(context_)->ClearQueueIfNoRetainPermission(
[email protected]961745f2013-05-25 14:09:2459 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) {
michaelpg4d80e562017-04-04 01:48:1466 return ExtensionPrefs::Get(context_)->IsExtensionRunning(extension_id);
[email protected]a2886e8b2013-06-08 05:15:0267}
68
michaelpg2fc6af92017-01-13 19:54:1869void 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]a2886e8b2013-06-08 05:15:0275// static
michaelpg4d80e562017-04-04 01:48:1476AppRestoreService* AppRestoreService::Get(content::BrowserContext* context) {
77 return apps::AppRestoreServiceFactory::GetForBrowserContext(context);
[email protected]a2886e8b2013-06-08 05:15:0278}
79
michaelpg4d80e562017-04-04 01:48:1480void AppRestoreService::OnAppStart(content::BrowserContext* context,
[email protected]4b7111f22013-06-18 14:22:1281 const std::string& app_id) {
82 RecordAppStart(app_id);
[email protected]28c3eeb2012-10-15 05:47:5383}
84
michaelpg4d80e562017-04-04 01:48:1485void AppRestoreService::OnAppActivated(content::BrowserContext* context,
[email protected]4b7111f22013-06-18 14:22:1286 const std::string& app_id) {
87 RecordAppActiveState(app_id, true);
[email protected]771c8d272013-05-17 09:47:4088}
89
michaelpg4d80e562017-04-04 01:48:1490void AppRestoreService::OnAppDeactivated(content::BrowserContext* context,
[email protected]4b7111f22013-06-18 14:22:1291 const std::string& app_id) {
92 RecordAppActiveState(app_id, false);
[email protected]771c8d272013-05-17 09:47:4093}
94
michaelpg4d80e562017-04-04 01:48:1495void AppRestoreService::OnAppStop(content::BrowserContext* context,
96 const std::string& app_id) {
[email protected]4b7111f22013-06-18 14:22:1297 RecordAppStop(app_id);
98}
99
[email protected]771c8d272013-05-17 09:47:40100void AppRestoreService::Shutdown() {
[email protected]4b7111f22013-06-18 14:22:12101 StopObservingAppLifetime();
[email protected]771c8d272013-05-17 09:47:40102}
[email protected]28c3eeb2012-10-15 05:47:53103
104void AppRestoreService::RecordAppStart(const std::string& extension_id) {
michaelpg4d80e562017-04-04 01:48:14105 ExtensionPrefs::Get(context_)->SetExtensionRunning(extension_id, true);
[email protected]28c3eeb2012-10-15 05:47:53106}
107
108void AppRestoreService::RecordAppStop(const std::string& extension_id) {
michaelpg4d80e562017-04-04 01:48:14109 ExtensionPrefs::Get(context_)->SetExtensionRunning(extension_id, false);
[email protected]28c3eeb2012-10-15 05:47:53110}
111
[email protected]4b7111f22013-06-18 14:22:12112void AppRestoreService::RecordAppActiveState(const std::string& id,
113 bool is_active) {
michaelpg4d80e562017-04-04 01:48:14114 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context_);
[email protected]771c8d272013-05-17 09:47:40115
116 // If the extension isn't running then we will already have recorded whether
[email protected]4b7111f22013-06-18 14:22:12117 // it is active or not.
[email protected]771c8d272013-05-17 09:47:40118 if (!extension_prefs->IsExtensionRunning(id))
119 return;
120
[email protected]4b7111f22013-06-18 14:22:12121 extension_prefs->SetIsActive(id, is_active);
[email protected]771c8d272013-05-17 09:47:40122}
123
[email protected]961745f2013-05-25 14:09:24124void AppRestoreService::RestoreApp(const Extension* extension) {
michaelpg4d80e562017-04-04 01:48:14125 RestartPlatformApp(context_, extension);
[email protected]28c3eeb2012-10-15 05:47:53126}
127
[email protected]4b7111f22013-06-18 14:22:12128void AppRestoreService::StartObservingAppLifetime() {
129 AppLifetimeMonitor* app_lifetime_monitor =
michaelpg4d80e562017-04-04 01:48:14130 AppLifetimeMonitorFactory::GetForBrowserContext(context_);
[email protected]4b7111f22013-06-18 14:22:12131 DCHECK(app_lifetime_monitor);
132 app_lifetime_monitor->AddObserver(this);
[email protected]771c8d272013-05-17 09:47:40133}
134
[email protected]4b7111f22013-06-18 14:22:12135void AppRestoreService::StopObservingAppLifetime() {
136 AppLifetimeMonitor* app_lifetime_monitor =
michaelpg4d80e562017-04-04 01:48:14137 AppLifetimeMonitorFactory::GetForBrowserContext(context_);
[email protected]4b7111f22013-06-18 14:22:12138 // This might be NULL in tests.
139 if (app_lifetime_monitor)
140 app_lifetime_monitor->RemoveObserver(this);
[email protected]771c8d272013-05-17 09:47:40141}
142
[email protected]c2e2b6d2013-01-22 02:23:29143} // namespace apps