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 "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" 19 20using extensions::Extension; 21using extensions::ExtensionHost; 22using extensions::ExtensionPrefs; 23using extensions::ExtensionRegistry; 24 25namespace apps { 26 27// static 28bool 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; 35} 36 37AppRestoreService::AppRestoreService(Profile* profile) 38 : profile_(profile) { 39 StartObservingAppLifetime(); 40} 41 42void AppRestoreService::HandleStartup(bool should_restore_apps) { 43 const extensions::ExtensionSet& extensions = 44 ExtensionRegistry::Get(profile_)->enabled_extensions(); 45 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_); 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(profile_)->ClearQueueIfNoRetainPermission( 59 extension); 60 } 61 } 62 } 63} 64 65bool AppRestoreService::IsAppRestorable(const std::string& extension_id) { 66 return ExtensionPrefs::Get(profile_)->IsExtensionRunning(extension_id); 67} 68 69// static 70AppRestoreService* AppRestoreService::Get(Profile* profile) { 71 return apps::AppRestoreServiceFactory::GetForProfile(profile); 72} 73 74void AppRestoreService::OnAppStart(Profile* profile, 75 const std::string& app_id) { 76 RecordAppStart(app_id); 77} 78 79void AppRestoreService::OnAppActivated(Profile* profile, 80 const std::string& app_id) { 81 RecordAppActiveState(app_id, true); 82} 83 84void AppRestoreService::OnAppDeactivated(Profile* profile, 85 const std::string& app_id) { 86 RecordAppActiveState(app_id, false); 87} 88 89void AppRestoreService::OnAppStop(Profile* profile, const std::string& app_id) { 90 RecordAppStop(app_id); 91} 92 93void AppRestoreService::OnChromeTerminating() { 94 // We want to preserve the state when the app begins terminating, so stop 95 // listening to app lifetime events. 96 StopObservingAppLifetime(); 97} 98 99void AppRestoreService::Shutdown() { 100 StopObservingAppLifetime(); 101} 102 103void AppRestoreService::RecordAppStart(const std::string& extension_id) { 104 ExtensionPrefs::Get(profile_)->SetExtensionRunning(extension_id, true); 105} 106 107void AppRestoreService::RecordAppStop(const std::string& extension_id) { 108 ExtensionPrefs::Get(profile_)->SetExtensionRunning(extension_id, false); 109} 110 111void AppRestoreService::RecordAppActiveState(const std::string& id, 112 bool is_active) { 113 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_); 114 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; 119 120 extension_prefs->SetIsActive(id, is_active); 121} 122 123void AppRestoreService::RestoreApp(const Extension* extension) { 124 RestartPlatformApp(profile_, extension); 125} 126 127void AppRestoreService::StartObservingAppLifetime() { 128 AppLifetimeMonitor* app_lifetime_monitor = 129 AppLifetimeMonitorFactory::GetForProfile(profile_); 130 DCHECK(app_lifetime_monitor); 131 app_lifetime_monitor->AddObserver(this); 132} 133 134void 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); 140} 141 142} // namespace apps 143