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