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/launcher.h"
11 #include "apps/saved_files_service.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
14 #include "chrome/browser/extensions/extension_host.h"
15 #include "chrome/browser/profiles/profile.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"
22 #include "win8/util/win8_util.h"
25 using extensions::Extension
;
26 using extensions::ExtensionHost
;
27 using extensions::ExtensionPrefs
;
28 using extensions::ExtensionRegistry
;
33 bool AppRestoreService::ShouldRestoreApps(bool is_browser_restart
) {
34 bool should_restore_apps
= is_browser_restart
;
35 #if defined(OS_CHROMEOS)
36 // Chromeos always restarts apps, even if it was a regular shutdown.
37 should_restore_apps
= true;
39 // Packaged apps are not supported in Metro mode, so don't try to start them.
40 if (win8::IsSingleWindowMetroMode())
41 should_restore_apps
= false;
43 return should_restore_apps
;
46 AppRestoreService::AppRestoreService(Profile
* profile
)
48 StartObservingAppLifetime();
51 void AppRestoreService::HandleStartup(bool should_restore_apps
) {
52 const extensions::ExtensionSet
& extensions
=
53 ExtensionRegistry::Get(profile_
)->enabled_extensions();
54 ExtensionPrefs
* extension_prefs
= ExtensionPrefs::Get(profile_
);
56 for (extensions::ExtensionSet::const_iterator it
= extensions
.begin();
57 it
!= extensions
.end(); ++it
) {
58 const Extension
* extension
= it
->get();
59 if (extension_prefs
->IsExtensionRunning(extension
->id())) {
60 RecordAppStop(extension
->id());
61 // If we are not restoring apps (e.g., because it is a clean restart), and
62 // the app does not have retain permission, explicitly clear the retained
64 if (should_restore_apps
) {
65 RestoreApp(it
->get());
67 SavedFilesService::Get(profile_
)->ClearQueueIfNoRetainPermission(
74 bool AppRestoreService::IsAppRestorable(const std::string
& extension_id
) {
75 return ExtensionPrefs::Get(profile_
)->IsExtensionRunning(extension_id
);
79 AppRestoreService
* AppRestoreService::Get(Profile
* profile
) {
80 return apps::AppRestoreServiceFactory::GetForProfile(profile
);
83 void AppRestoreService::OnAppStart(Profile
* profile
,
84 const std::string
& app_id
) {
85 RecordAppStart(app_id
);
88 void AppRestoreService::OnAppActivated(Profile
* profile
,
89 const std::string
& app_id
) {
90 RecordAppActiveState(app_id
, true);
93 void AppRestoreService::OnAppDeactivated(Profile
* profile
,
94 const std::string
& app_id
) {
95 RecordAppActiveState(app_id
, false);
98 void AppRestoreService::OnAppStop(Profile
* profile
, const std::string
& app_id
) {
99 RecordAppStop(app_id
);
102 void AppRestoreService::OnChromeTerminating() {
103 // We want to preserve the state when the app begins terminating, so stop
104 // listening to app lifetime events.
105 StopObservingAppLifetime();
108 void AppRestoreService::Shutdown() {
109 StopObservingAppLifetime();
112 void AppRestoreService::RecordAppStart(const std::string
& extension_id
) {
113 ExtensionPrefs::Get(profile_
)->SetExtensionRunning(extension_id
, true);
116 void AppRestoreService::RecordAppStop(const std::string
& extension_id
) {
117 ExtensionPrefs::Get(profile_
)->SetExtensionRunning(extension_id
, false);
120 void AppRestoreService::RecordAppActiveState(const std::string
& id
,
122 ExtensionPrefs
* extension_prefs
= ExtensionPrefs::Get(profile_
);
124 // If the extension isn't running then we will already have recorded whether
125 // it is active or not.
126 if (!extension_prefs
->IsExtensionRunning(id
))
129 extension_prefs
->SetIsActive(id
, is_active
);
132 void AppRestoreService::RestoreApp(const Extension
* extension
) {
133 RestartPlatformApp(profile_
, extension
);
136 void AppRestoreService::StartObservingAppLifetime() {
137 AppLifetimeMonitor
* app_lifetime_monitor
=
138 AppLifetimeMonitorFactory::GetForProfile(profile_
);
139 DCHECK(app_lifetime_monitor
);
140 app_lifetime_monitor
->AddObserver(this);
143 void AppRestoreService::StopObservingAppLifetime() {
144 AppLifetimeMonitor
* app_lifetime_monitor
=
145 AppLifetimeMonitorFactory::GetForProfile(profile_
);
146 // This might be NULL in tests.
147 if (app_lifetime_monitor
)
148 app_lifetime_monitor
->RemoveObserver(this);