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.
5 #include "apps/app_restore_service.h"
7 #include "apps/app_lifetime_monitor_factory.h"
8 #include "apps/app_restore_service_factory.h"
9 #include "apps/app_window.h"
10 #include "apps/browser/api/app_runtime/app_runtime_api.h"
11 #include "apps/launcher.h"
12 #include "apps/saved_files_service.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "extensions/browser/extension_host.h"
16 #include "extensions/browser/extension_prefs.h"
17 #include "extensions/browser/extension_registry.h"
18 #include "extensions/common/extension.h"
19 #include "extensions/common/extension_set.h"
21 using extensions::Extension
;
22 using extensions::ExtensionHost
;
23 using extensions::ExtensionPrefs
;
24 using extensions::ExtensionRegistry
;
29 bool AppRestoreService::ShouldRestoreApps(bool is_browser_restart
) {
30 bool should_restore_apps
= is_browser_restart
;
31 #if defined(OS_CHROMEOS)
32 // Chromeos always restarts apps, even if it was a regular shutdown.
33 should_restore_apps
= true;
35 return should_restore_apps
;
38 AppRestoreService::AppRestoreService(Profile
* profile
)
40 StartObservingAppLifetime();
43 void AppRestoreService::HandleStartup(bool should_restore_apps
) {
44 const extensions::ExtensionSet
& extensions
=
45 ExtensionRegistry::Get(profile_
)->enabled_extensions();
46 ExtensionPrefs
* extension_prefs
= ExtensionPrefs::Get(profile_
);
48 for (extensions::ExtensionSet::const_iterator it
= extensions
.begin();
49 it
!= extensions
.end(); ++it
) {
50 const Extension
* extension
= it
->get();
51 if (extension_prefs
->IsExtensionRunning(extension
->id())) {
52 RecordAppStop(extension
->id());
53 // If we are not restoring apps (e.g., because it is a clean restart), and
54 // the app does not have retain permission, explicitly clear the retained
56 if (should_restore_apps
) {
57 RestoreApp(it
->get());
59 SavedFilesService::Get(profile_
)->ClearQueueIfNoRetainPermission(
66 bool AppRestoreService::IsAppRestorable(const std::string
& extension_id
) {
67 return ExtensionPrefs::Get(profile_
)->IsExtensionRunning(extension_id
);
71 AppRestoreService
* AppRestoreService::Get(Profile
* profile
) {
72 return apps::AppRestoreServiceFactory::GetForProfile(profile
);
75 void AppRestoreService::OnAppStart(Profile
* profile
,
76 const std::string
& app_id
) {
77 RecordAppStart(app_id
);
80 void AppRestoreService::OnAppActivated(Profile
* profile
,
81 const std::string
& app_id
) {
82 RecordAppActiveState(app_id
, true);
85 void AppRestoreService::OnAppDeactivated(Profile
* profile
,
86 const std::string
& app_id
) {
87 RecordAppActiveState(app_id
, false);
90 void AppRestoreService::OnAppStop(Profile
* profile
, const std::string
& app_id
) {
91 RecordAppStop(app_id
);
94 void AppRestoreService::OnChromeTerminating() {
95 // We want to preserve the state when the app begins terminating, so stop
96 // listening to app lifetime events.
97 StopObservingAppLifetime();
100 void AppRestoreService::Shutdown() {
101 StopObservingAppLifetime();
104 void AppRestoreService::RecordAppStart(const std::string
& extension_id
) {
105 ExtensionPrefs::Get(profile_
)->SetExtensionRunning(extension_id
, true);
108 void AppRestoreService::RecordAppStop(const std::string
& extension_id
) {
109 ExtensionPrefs::Get(profile_
)->SetExtensionRunning(extension_id
, false);
112 void AppRestoreService::RecordAppActiveState(const std::string
& id
,
114 ExtensionPrefs
* extension_prefs
= ExtensionPrefs::Get(profile_
);
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
))
121 extension_prefs
->SetIsActive(id
, is_active
);
124 void AppRestoreService::RestoreApp(const Extension
* extension
) {
125 RestartPlatformApp(profile_
, extension
);
128 void AppRestoreService::StartObservingAppLifetime() {
129 AppLifetimeMonitor
* app_lifetime_monitor
=
130 AppLifetimeMonitorFactory::GetForProfile(profile_
);
131 DCHECK(app_lifetime_monitor
);
132 app_lifetime_monitor
->AddObserver(this);
135 void AppRestoreService::StopObservingAppLifetime() {
136 AppLifetimeMonitor
* app_lifetime_monitor
=
137 AppLifetimeMonitorFactory::GetForProfile(profile_
);
138 // This might be NULL in tests.
139 if (app_lifetime_monitor
)
140 app_lifetime_monitor
->RemoveObserver(this);