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 
5 #include "apps/app_restore_service.h"
6 
7 #include "apps/app_lifetime_monitor_factory.h"
8 #include "apps/app_restore_service_factory.h"
9 #include "apps/launcher.h"
10 #include "apps/saved_files_service.h"
11 #include "build/chromeos_buildflags.h"
12 #include "content/public/browser/browser_context.h"
13 #include "extensions/browser/app_window/app_window.h"
14 #include "extensions/browser/extension_host.h"
15 #include "extensions/browser/extension_prefs.h"
16 #include "extensions/browser/extension_registry.h"
17 #include "extensions/common/extension.h"
18 #include "extensions/common/extension_set.h"
19 
20 using extensions::Extension;
21 using extensions::ExtensionHost;
22 using extensions::ExtensionPrefs;
23 using extensions::ExtensionRegistry;
24 
25 namespace apps {
26 
27 // static
ShouldRestoreApps(bool is_browser_restart)28 bool AppRestoreService::ShouldRestoreApps(bool is_browser_restart) {
29   bool should_restore_apps = is_browser_restart;
30 #if BUILDFLAG(IS_CHROMEOS_ASH)
31   // Chromeos always restarts apps, even if it was a regular shutdown.
32   should_restore_apps = true;
33 #endif
34   return should_restore_apps;
35 }
36 
AppRestoreService(content::BrowserContext * context)37 AppRestoreService::AppRestoreService(content::BrowserContext* context)
38     : context_(context) {
39   StartObservingAppLifetime();
40 }
41 
HandleStartup(bool should_restore_apps)42 void AppRestoreService::HandleStartup(bool should_restore_apps) {
43   const extensions::ExtensionSet& extensions =
44       ExtensionRegistry::Get(context_)->enabled_extensions();
45   ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context_);
46 
47   for (extensions::ExtensionSet::const_iterator it = extensions.begin();
48       it != extensions.end(); ++it) {
49     const Extension* extension = it->get();
50     if (extension_prefs->IsExtensionRunning(extension->id())) {
51       RecordAppStop(extension->id());
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) {
56         RestoreApp(it->get());
57       } else {
58         SavedFilesService::Get(context_)->ClearQueueIfNoRetainPermission(
59             extension);
60       }
61     }
62   }
63 }
64 
IsAppRestorable(const std::string & extension_id)65 bool AppRestoreService::IsAppRestorable(const std::string& extension_id) {
66   return ExtensionPrefs::Get(context_)->IsExtensionRunning(extension_id);
67 }
68 
OnApplicationTerminating()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 
75 // static
Get(content::BrowserContext * context)76 AppRestoreService* AppRestoreService::Get(content::BrowserContext* context) {
77   return apps::AppRestoreServiceFactory::GetForBrowserContext(context);
78 }
79 
OnAppStart(content::BrowserContext * context,const std::string & app_id)80 void AppRestoreService::OnAppStart(content::BrowserContext* context,
81                                    const std::string& app_id) {
82   RecordAppStart(app_id);
83 }
84 
OnAppActivated(content::BrowserContext * context,const std::string & app_id)85 void AppRestoreService::OnAppActivated(content::BrowserContext* context,
86                                        const std::string& app_id) {
87   RecordAppActiveState(app_id, true);
88 }
89 
OnAppDeactivated(content::BrowserContext * context,const std::string & app_id)90 void AppRestoreService::OnAppDeactivated(content::BrowserContext* context,
91                                          const std::string& app_id) {
92   RecordAppActiveState(app_id, false);
93 }
94 
OnAppStop(content::BrowserContext * context,const std::string & app_id)95 void AppRestoreService::OnAppStop(content::BrowserContext* context,
96                                   const std::string& app_id) {
97   RecordAppStop(app_id);
98 }
99 
Shutdown()100 void AppRestoreService::Shutdown() {
101   StopObservingAppLifetime();
102 }
103 
RecordAppStart(const std::string & extension_id)104 void AppRestoreService::RecordAppStart(const std::string& extension_id) {
105   ExtensionPrefs::Get(context_)->SetExtensionRunning(extension_id, true);
106 }
107 
RecordAppStop(const std::string & extension_id)108 void AppRestoreService::RecordAppStop(const std::string& extension_id) {
109   ExtensionPrefs::Get(context_)->SetExtensionRunning(extension_id, false);
110 }
111 
RecordAppActiveState(const std::string & id,bool is_active)112 void AppRestoreService::RecordAppActiveState(const std::string& id,
113                                              bool is_active) {
114   ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context_);
115 
116   // If the extension isn't running then we will already have recorded whether
117   // it is active or not.
118   if (!extension_prefs->IsExtensionRunning(id))
119     return;
120 
121   extension_prefs->SetIsActive(id, is_active);
122 }
123 
RestoreApp(const Extension * extension)124 void AppRestoreService::RestoreApp(const Extension* extension) {
125   RestartPlatformApp(context_, extension);
126 }
127 
StartObservingAppLifetime()128 void AppRestoreService::StartObservingAppLifetime() {
129   AppLifetimeMonitor* app_lifetime_monitor =
130       AppLifetimeMonitorFactory::GetForBrowserContext(context_);
131   DCHECK(app_lifetime_monitor);
132   app_lifetime_monitor->AddObserver(this);
133 }
134 
StopObservingAppLifetime()135 void AppRestoreService::StopObservingAppLifetime() {
136   AppLifetimeMonitor* app_lifetime_monitor =
137       AppLifetimeMonitorFactory::GetForBrowserContext(context_);
138   // This might be NULL in tests.
139   if (app_lifetime_monitor)
140     app_lifetime_monitor->RemoveObserver(this);
141 }
142 
143 }  // namespace apps
144