MacViews: Fix WidgetWithDestroyedNativeViewTest.Test
[chromium-blink-merge.git] / apps / app_restore_service.cc
bloba68c59edf8ad43340884fe501c9896ca5390ea0b
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/launcher.h"
10 #include "apps/saved_files_service.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/profiles/profile.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"
20 using extensions::Extension;
21 using extensions::ExtensionHost;
22 using extensions::ExtensionPrefs;
23 using extensions::ExtensionRegistry;
25 namespace apps {
27 // static
28 bool AppRestoreService::ShouldRestoreApps(bool is_browser_restart) {
29 bool should_restore_apps = is_browser_restart;
30 #if defined(OS_CHROMEOS)
31 // Chromeos always restarts apps, even if it was a regular shutdown.
32 should_restore_apps = true;
33 #endif
34 return should_restore_apps;
37 AppRestoreService::AppRestoreService(Profile* profile)
38 : profile_(profile) {
39 StartObservingAppLifetime();
42 void AppRestoreService::HandleStartup(bool should_restore_apps) {
43 const extensions::ExtensionSet& extensions =
44 ExtensionRegistry::Get(profile_)->enabled_extensions();
45 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_);
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(profile_)->ClearQueueIfNoRetainPermission(
59 extension);
65 bool AppRestoreService::IsAppRestorable(const std::string& extension_id) {
66 return ExtensionPrefs::Get(profile_)->IsExtensionRunning(extension_id);
69 // static
70 AppRestoreService* AppRestoreService::Get(Profile* profile) {
71 return apps::AppRestoreServiceFactory::GetForProfile(profile);
74 void AppRestoreService::OnAppStart(Profile* profile,
75 const std::string& app_id) {
76 RecordAppStart(app_id);
79 void AppRestoreService::OnAppActivated(Profile* profile,
80 const std::string& app_id) {
81 RecordAppActiveState(app_id, true);
84 void AppRestoreService::OnAppDeactivated(Profile* profile,
85 const std::string& app_id) {
86 RecordAppActiveState(app_id, false);
89 void AppRestoreService::OnAppStop(Profile* profile, const std::string& app_id) {
90 RecordAppStop(app_id);
93 void AppRestoreService::OnChromeTerminating() {
94 // We want to preserve the state when the app begins terminating, so stop
95 // listening to app lifetime events.
96 StopObservingAppLifetime();
99 void AppRestoreService::Shutdown() {
100 StopObservingAppLifetime();
103 void AppRestoreService::RecordAppStart(const std::string& extension_id) {
104 ExtensionPrefs::Get(profile_)->SetExtensionRunning(extension_id, true);
107 void AppRestoreService::RecordAppStop(const std::string& extension_id) {
108 ExtensionPrefs::Get(profile_)->SetExtensionRunning(extension_id, false);
111 void AppRestoreService::RecordAppActiveState(const std::string& id,
112 bool is_active) {
113 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_);
115 // If the extension isn't running then we will already have recorded whether
116 // it is active or not.
117 if (!extension_prefs->IsExtensionRunning(id))
118 return;
120 extension_prefs->SetIsActive(id, is_active);
123 void AppRestoreService::RestoreApp(const Extension* extension) {
124 RestartPlatformApp(profile_, extension);
127 void AppRestoreService::StartObservingAppLifetime() {
128 AppLifetimeMonitor* app_lifetime_monitor =
129 AppLifetimeMonitorFactory::GetForProfile(profile_);
130 DCHECK(app_lifetime_monitor);
131 app_lifetime_monitor->AddObserver(this);
134 void AppRestoreService::StopObservingAppLifetime() {
135 AppLifetimeMonitor* app_lifetime_monitor =
136 AppLifetimeMonitorFactory::GetForProfile(profile_);
137 // This might be NULL in tests.
138 if (app_lifetime_monitor)
139 app_lifetime_monitor->RemoveObserver(this);
142 } // namespace apps