| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "apps/app_restore_service.h" | 5 #include "apps/app_restore_service.h" |
| 6 | 6 |
| 7 #include "apps/app_lifetime_monitor_factory.h" | 7 #include "apps/app_lifetime_monitor_factory.h" |
| 8 #include "apps/app_restore_service_factory.h" | 8 #include "apps/app_restore_service_factory.h" |
| 9 #include "apps/launcher.h" | 9 #include "apps/launcher.h" |
| 10 #include "apps/saved_files_service.h" | 10 #include "apps/saved_files_service.h" |
| 11 #include "chrome/browser/chrome_notification_types.h" | 11 #include "content/public/browser/browser_context.h" |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 #include "extensions/browser/app_window/app_window.h" | 12 #include "extensions/browser/app_window/app_window.h" |
| 14 #include "extensions/browser/extension_host.h" | 13 #include "extensions/browser/extension_host.h" |
| 15 #include "extensions/browser/extension_prefs.h" | 14 #include "extensions/browser/extension_prefs.h" |
| 16 #include "extensions/browser/extension_registry.h" | 15 #include "extensions/browser/extension_registry.h" |
| 17 #include "extensions/common/extension.h" | 16 #include "extensions/common/extension.h" |
| 18 #include "extensions/common/extension_set.h" | 17 #include "extensions/common/extension_set.h" |
| 19 | 18 |
| 20 using extensions::Extension; | 19 using extensions::Extension; |
| 21 using extensions::ExtensionHost; | 20 using extensions::ExtensionHost; |
| 22 using extensions::ExtensionPrefs; | 21 using extensions::ExtensionPrefs; |
| 23 using extensions::ExtensionRegistry; | 22 using extensions::ExtensionRegistry; |
| 24 | 23 |
| 25 namespace apps { | 24 namespace apps { |
| 26 | 25 |
| 27 // static | 26 // static |
| 28 bool AppRestoreService::ShouldRestoreApps(bool is_browser_restart) { | 27 bool AppRestoreService::ShouldRestoreApps(bool is_browser_restart) { |
| 29 bool should_restore_apps = is_browser_restart; | 28 bool should_restore_apps = is_browser_restart; |
| 30 #if defined(OS_CHROMEOS) | 29 #if defined(OS_CHROMEOS) |
| 31 // Chromeos always restarts apps, even if it was a regular shutdown. | 30 // Chromeos always restarts apps, even if it was a regular shutdown. |
| 32 should_restore_apps = true; | 31 should_restore_apps = true; |
| 33 #endif | 32 #endif |
| 34 return should_restore_apps; | 33 return should_restore_apps; |
| 35 } | 34 } |
| 36 | 35 |
| 37 AppRestoreService::AppRestoreService(Profile* profile) | 36 AppRestoreService::AppRestoreService(content::BrowserContext* context) |
| 38 : profile_(profile) { | 37 : context_(context) { |
| 39 StartObservingAppLifetime(); | 38 StartObservingAppLifetime(); |
| 40 } | 39 } |
| 41 | 40 |
| 42 void AppRestoreService::HandleStartup(bool should_restore_apps) { | 41 void AppRestoreService::HandleStartup(bool should_restore_apps) { |
| 43 const extensions::ExtensionSet& extensions = | 42 const extensions::ExtensionSet& extensions = |
| 44 ExtensionRegistry::Get(profile_)->enabled_extensions(); | 43 ExtensionRegistry::Get(context_)->enabled_extensions(); |
| 45 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_); | 44 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context_); |
| 46 | 45 |
| 47 for (extensions::ExtensionSet::const_iterator it = extensions.begin(); | 46 for (extensions::ExtensionSet::const_iterator it = extensions.begin(); |
| 48 it != extensions.end(); ++it) { | 47 it != extensions.end(); ++it) { |
| 49 const Extension* extension = it->get(); | 48 const Extension* extension = it->get(); |
| 50 if (extension_prefs->IsExtensionRunning(extension->id())) { | 49 if (extension_prefs->IsExtensionRunning(extension->id())) { |
| 51 RecordAppStop(extension->id()); | 50 RecordAppStop(extension->id()); |
| 52 // If we are not restoring apps (e.g., because it is a clean restart), and | 51 // 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 | 52 // the app does not have retain permission, explicitly clear the retained |
| 54 // entries queue. | 53 // entries queue. |
| 55 if (should_restore_apps) { | 54 if (should_restore_apps) { |
| 56 RestoreApp(it->get()); | 55 RestoreApp(it->get()); |
| 57 } else { | 56 } else { |
| 58 SavedFilesService::Get(profile_)->ClearQueueIfNoRetainPermission( | 57 SavedFilesService::Get(context_)->ClearQueueIfNoRetainPermission( |
| 59 extension); | 58 extension); |
| 60 } | 59 } |
| 61 } | 60 } |
| 62 } | 61 } |
| 63 } | 62 } |
| 64 | 63 |
| 65 bool AppRestoreService::IsAppRestorable(const std::string& extension_id) { | 64 bool AppRestoreService::IsAppRestorable(const std::string& extension_id) { |
| 66 return ExtensionPrefs::Get(profile_)->IsExtensionRunning(extension_id); | 65 return ExtensionPrefs::Get(context_)->IsExtensionRunning(extension_id); |
| 67 } | 66 } |
| 68 | 67 |
| 69 void AppRestoreService::OnApplicationTerminating() { | 68 void AppRestoreService::OnApplicationTerminating() { |
| 70 // We want to preserve the state when the app begins terminating, so stop | 69 // We want to preserve the state when the app begins terminating, so stop |
| 71 // listening to app lifetime events. | 70 // listening to app lifetime events. |
| 72 StopObservingAppLifetime(); | 71 StopObservingAppLifetime(); |
| 73 } | 72 } |
| 74 | 73 |
| 75 // static | 74 // static |
| 76 AppRestoreService* AppRestoreService::Get(Profile* profile) { | 75 AppRestoreService* AppRestoreService::Get(content::BrowserContext* context) { |
| 77 return apps::AppRestoreServiceFactory::GetForProfile(profile); | 76 return apps::AppRestoreServiceFactory::GetForBrowserContext(context); |
| 78 } | 77 } |
| 79 | 78 |
| 80 void AppRestoreService::OnAppStart(Profile* profile, | 79 void AppRestoreService::OnAppStart(content::BrowserContext* context, |
| 81 const std::string& app_id) { | 80 const std::string& app_id) { |
| 82 RecordAppStart(app_id); | 81 RecordAppStart(app_id); |
| 83 } | 82 } |
| 84 | 83 |
| 85 void AppRestoreService::OnAppActivated(Profile* profile, | 84 void AppRestoreService::OnAppActivated(content::BrowserContext* context, |
| 86 const std::string& app_id) { | 85 const std::string& app_id) { |
| 87 RecordAppActiveState(app_id, true); | 86 RecordAppActiveState(app_id, true); |
| 88 } | 87 } |
| 89 | 88 |
| 90 void AppRestoreService::OnAppDeactivated(Profile* profile, | 89 void AppRestoreService::OnAppDeactivated(content::BrowserContext* context, |
| 91 const std::string& app_id) { | 90 const std::string& app_id) { |
| 92 RecordAppActiveState(app_id, false); | 91 RecordAppActiveState(app_id, false); |
| 93 } | 92 } |
| 94 | 93 |
| 95 void AppRestoreService::OnAppStop(Profile* profile, const std::string& app_id) { | 94 void AppRestoreService::OnAppStop(content::BrowserContext* context, |
| 95 const std::string& app_id) { |
| 96 RecordAppStop(app_id); | 96 RecordAppStop(app_id); |
| 97 } | 97 } |
| 98 | 98 |
| 99 void AppRestoreService::Shutdown() { | 99 void AppRestoreService::Shutdown() { |
| 100 StopObservingAppLifetime(); | 100 StopObservingAppLifetime(); |
| 101 } | 101 } |
| 102 | 102 |
| 103 void AppRestoreService::RecordAppStart(const std::string& extension_id) { | 103 void AppRestoreService::RecordAppStart(const std::string& extension_id) { |
| 104 ExtensionPrefs::Get(profile_)->SetExtensionRunning(extension_id, true); | 104 ExtensionPrefs::Get(context_)->SetExtensionRunning(extension_id, true); |
| 105 } | 105 } |
| 106 | 106 |
| 107 void AppRestoreService::RecordAppStop(const std::string& extension_id) { | 107 void AppRestoreService::RecordAppStop(const std::string& extension_id) { |
| 108 ExtensionPrefs::Get(profile_)->SetExtensionRunning(extension_id, false); | 108 ExtensionPrefs::Get(context_)->SetExtensionRunning(extension_id, false); |
| 109 } | 109 } |
| 110 | 110 |
| 111 void AppRestoreService::RecordAppActiveState(const std::string& id, | 111 void AppRestoreService::RecordAppActiveState(const std::string& id, |
| 112 bool is_active) { | 112 bool is_active) { |
| 113 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_); | 113 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context_); |
| 114 | 114 |
| 115 // If the extension isn't running then we will already have recorded whether | 115 // If the extension isn't running then we will already have recorded whether |
| 116 // it is active or not. | 116 // it is active or not. |
| 117 if (!extension_prefs->IsExtensionRunning(id)) | 117 if (!extension_prefs->IsExtensionRunning(id)) |
| 118 return; | 118 return; |
| 119 | 119 |
| 120 extension_prefs->SetIsActive(id, is_active); | 120 extension_prefs->SetIsActive(id, is_active); |
| 121 } | 121 } |
| 122 | 122 |
| 123 void AppRestoreService::RestoreApp(const Extension* extension) { | 123 void AppRestoreService::RestoreApp(const Extension* extension) { |
| 124 RestartPlatformApp(profile_, extension); | 124 RestartPlatformApp(context_, extension); |
| 125 } | 125 } |
| 126 | 126 |
| 127 void AppRestoreService::StartObservingAppLifetime() { | 127 void AppRestoreService::StartObservingAppLifetime() { |
| 128 AppLifetimeMonitor* app_lifetime_monitor = | 128 AppLifetimeMonitor* app_lifetime_monitor = |
| 129 AppLifetimeMonitorFactory::GetForProfile(profile_); | 129 AppLifetimeMonitorFactory::GetForBrowserContext(context_); |
| 130 DCHECK(app_lifetime_monitor); | 130 DCHECK(app_lifetime_monitor); |
| 131 app_lifetime_monitor->AddObserver(this); | 131 app_lifetime_monitor->AddObserver(this); |
| 132 } | 132 } |
| 133 | 133 |
| 134 void AppRestoreService::StopObservingAppLifetime() { | 134 void AppRestoreService::StopObservingAppLifetime() { |
| 135 AppLifetimeMonitor* app_lifetime_monitor = | 135 AppLifetimeMonitor* app_lifetime_monitor = |
| 136 AppLifetimeMonitorFactory::GetForProfile(profile_); | 136 AppLifetimeMonitorFactory::GetForBrowserContext(context_); |
| 137 // This might be NULL in tests. | 137 // This might be NULL in tests. |
| 138 if (app_lifetime_monitor) | 138 if (app_lifetime_monitor) |
| 139 app_lifetime_monitor->RemoveObserver(this); | 139 app_lifetime_monitor->RemoveObserver(this); |
| 140 } | 140 } |
| 141 | 141 |
| 142 } // namespace apps | 142 } // namespace apps |
| OLD | NEW |