Stop PasswordStoreMac thread in Shutdown().
[chromium-blink-merge.git] / apps / app_restore_service.cc
blobd3d1302409c46f88ae85fde26707bc25a6b9deb5
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/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 #if defined(OS_WIN)
22 #include "win8/util/win8_util.h"
23 #endif
25 using extensions::Extension;
26 using extensions::ExtensionHost;
27 using extensions::ExtensionPrefs;
28 using extensions::ExtensionRegistry;
30 namespace apps {
32 // static
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;
38 #elif defined(OS_WIN)
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;
42 #endif
43 return should_restore_apps;
46 AppRestoreService::AppRestoreService(Profile* profile)
47 : 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
63 // entries queue.
64 if (should_restore_apps) {
65 RestoreApp(it->get());
66 } else {
67 SavedFilesService::Get(profile_)->ClearQueueIfNoRetainPermission(
68 extension);
74 bool AppRestoreService::IsAppRestorable(const std::string& extension_id) {
75 return ExtensionPrefs::Get(profile_)->IsExtensionRunning(extension_id);
78 // static
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,
121 bool is_active) {
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))
127 return;
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);
151 } // namespace apps