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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/apps/ephemeral_app_service.h"
#include "apps/app_lifetime_monitor_factory.h"
#include "base/command_line.h"
#include "base/message_loop/message_loop.h"
#include "chrome/browser/apps/ephemeral_app_service_factory.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_switches.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/extension_util.h"
#include "extensions/browser/notification_types.h"
#include "extensions/browser/uninstall_reason.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_set.h"
using extensions::Extension;
using extensions::ExtensionPrefs;
using extensions::ExtensionRegistry;
using extensions::ExtensionSet;
using extensions::ExtensionSystem;
namespace {
// The number of seconds after startup before performing garbage collection
// of ephemeral apps.
const int kGarbageCollectAppsStartupDelay = 60;
// The number of seconds after an ephemeral app has been installed before
// performing garbage collection.
const int kGarbageCollectAppsInstallDelay = 15;
// When the number of ephemeral apps reaches this count, trigger garbage
// collection to trim off the least-recently used apps in excess of
// kMaxEphemeralAppsCount.
const int kGarbageCollectAppsTriggerCount = 35;
// The number of seconds after an app has stopped running before it will be
// disabled.
const int kDefaultDisableAppDelay = 1;
// The number of seconds after startup before disabling inactive ephemeral apps.
const int kDisableAppsOnStartupDelay = 5;
} // namespace
const int EphemeralAppService::kAppInactiveThreshold = 10;
const int EphemeralAppService::kAppKeepThreshold = 1;
const int EphemeralAppService::kMaxEphemeralAppsCount = 30;
// static
EphemeralAppService* EphemeralAppService::Get(Profile* profile) {
return EphemeralAppServiceFactory::GetForProfile(profile);
}
EphemeralAppService::EphemeralAppService(Profile* profile)
: profile_(profile),
extension_registry_observer_(this),
app_lifetime_monitor_observer_(this),
ephemeral_app_count_(-1),
disable_idle_app_delay_(kDefaultDisableAppDelay),
weak_ptr_factory_(this) {
registrar_.Add(this,
extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
content::Source<Profile>(profile_));
}
EphemeralAppService::~EphemeralAppService() {
}
void EphemeralAppService::ClearCachedApps() {
// Cancel any pending garbage collects.
garbage_collect_apps_timer_.Stop();
ExtensionRegistry* registry = ExtensionRegistry::Get(profile_);
DCHECK(registry);
ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_);
DCHECK(prefs);
ExtensionService* service =
ExtensionSystem::Get(profile_)->extension_service();
DCHECK(service);
scoped_ptr<ExtensionSet> extensions =
registry->GenerateInstalledExtensionsSet();
for (ExtensionSet::const_iterator it = extensions->begin();
it != extensions->end();
++it) {
std::string extension_id = (*it)->id();
if (!prefs->IsEphemeralApp(extension_id))
continue;
// Do not remove apps that are running.
if (!extensions::util::IsExtensionIdle(extension_id, profile_))
continue;
DCHECK(registry->GetExtensionById(extension_id,
ExtensionRegistry::EVERYTHING));
service->UninstallExtension(
extension_id,
extensions::UNINSTALL_REASON_ORPHANED_EPHEMERAL_EXTENSION,
base::Bind(&base::DoNothing),
NULL);
}
}
void EphemeralAppService::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED: {
Init();
break;
}
default:
NOTREACHED();
}
}
void EphemeralAppService::OnExtensionWillBeInstalled(
content::BrowserContext* browser_context,
const extensions::Extension* extension,
bool is_update,
bool from_ephemeral,
const std::string& old_name) {
if (from_ephemeral) {
// An ephemeral app was just promoted to a regular installed app.
--ephemeral_app_count_;
DCHECK_GE(ephemeral_app_count_, 0);
HandleEphemeralAppPromoted(extension);
} else if (!is_update &&
extensions::util::IsEphemeralApp(extension->id(), profile_)) {
// A new ephemeral app was launched.
++ephemeral_app_count_;
if (ephemeral_app_count_ >= kGarbageCollectAppsTriggerCount) {
TriggerGarbageCollect(
base::TimeDelta::FromSeconds(kGarbageCollectAppsInstallDelay));
}
}
}
void EphemeralAppService::OnExtensionUninstalled(
content::BrowserContext* browser_context,
const extensions::Extension* extension,
extensions::UninstallReason reason) {
if (extensions::util::IsEphemeralApp(extension->id(), profile_)) {
--ephemeral_app_count_;
DCHECK_GE(ephemeral_app_count_, 0);
}
}
void EphemeralAppService::OnAppStop(Profile* profile,
const std::string& app_id) {
if (!extensions::util::IsEphemeralApp(app_id, profile_))
return;
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&EphemeralAppService::DisableEphemeralApp,
weak_ptr_factory_.GetWeakPtr(),
app_id),
base::TimeDelta::FromSeconds(disable_idle_app_delay_));
}
void EphemeralAppService::OnChromeTerminating() {
garbage_collect_apps_timer_.Stop();
extension_registry_observer_.RemoveAll();
app_lifetime_monitor_observer_.RemoveAll();
}
void EphemeralAppService::Init() {
InitEphemeralAppCount();
// Start observing.
extension_registry_observer_.Add(ExtensionRegistry::Get(profile_));
app_lifetime_monitor_observer_.Add(
apps::AppLifetimeMonitorFactory::GetForProfile(profile_));
// Execute startup clean up tasks (except during tests).
if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType))
return;
TriggerGarbageCollect(
base::TimeDelta::FromSeconds(kGarbageCollectAppsStartupDelay));
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&EphemeralAppService::DisableEphemeralAppsOnStartup,
weak_ptr_factory_.GetWeakPtr()),
base::TimeDelta::FromSeconds(kDisableAppsOnStartupDelay));
}
void EphemeralAppService::InitEphemeralAppCount() {
scoped_ptr<ExtensionSet> extensions =
ExtensionRegistry::Get(profile_)->GenerateInstalledExtensionsSet();
ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_);
DCHECK(prefs);
ephemeral_app_count_ = 0;
for (ExtensionSet::const_iterator it = extensions->begin();
it != extensions->end(); ++it) {
const Extension* extension = it->get();
if (prefs->IsEphemeralApp(extension->id()))
++ephemeral_app_count_;
}
}
void EphemeralAppService::DisableEphemeralApp(const std::string& app_id) {
if (!extensions::util::IsEphemeralApp(app_id, profile_) ||
!extensions::util::IsExtensionIdle(app_id, profile_)) {
return;
}
// After an ephemeral app has stopped running, unload it from extension
// system and disable it to prevent all background activity.
ExtensionService* service =
ExtensionSystem::Get(profile_)->extension_service();
DCHECK(service);
service->DisableExtension(app_id, Extension::DISABLE_INACTIVE_EPHEMERAL_APP);
}
void EphemeralAppService::DisableEphemeralAppsOnStartup() {
ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_);
DCHECK(prefs);
ExtensionService* service =
ExtensionSystem::Get(profile_)->extension_service();
DCHECK(service);
// Ensure that all inactive ephemeral apps are disabled to prevent all
// background activity. This is done on startup to catch any apps that escaped
// being disabled on shutdown.
scoped_ptr<ExtensionSet> extensions =
ExtensionRegistry::Get(profile_)->GenerateInstalledExtensionsSet();
for (ExtensionSet::const_iterator it = extensions->begin();
it != extensions->end();
++it) {
const Extension* extension = it->get();
if (!prefs->IsEphemeralApp(extension->id()))
continue;
// Only V2 apps are installed ephemerally. Remove other ephemeral app types
// that were cached before this policy was introduced.
if (!extension->is_platform_app()) {
service->UninstallExtension(
extension->id(),
extensions::UNINSTALL_REASON_ORPHANED_EPHEMERAL_EXTENSION,
base::Bind(&base::DoNothing),
NULL);
continue;
}
if (!prefs->HasDisableReason(extension->id(),
Extension::DISABLE_INACTIVE_EPHEMERAL_APP) &&
!prefs->IsExtensionRunning(extension->id()) &&
extensions::util::IsExtensionIdle(extension->id(), profile_)) {
service->DisableExtension(extension->id(),
Extension::DISABLE_INACTIVE_EPHEMERAL_APP);
}
}
}
void EphemeralAppService::HandleEphemeralAppPromoted(const Extension* app) {
// When ephemeral apps are promoted to regular install apps, remove the
// DISABLE_INACTIVE_EPHEMERAL_APP reason and enable the app if there are no
// other reasons.
ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_);
DCHECK(prefs);
int disable_reasons = prefs->GetDisableReasons(app->id());
if (disable_reasons & Extension::DISABLE_INACTIVE_EPHEMERAL_APP) {
prefs->RemoveDisableReason(app->id(),
Extension::DISABLE_INACTIVE_EPHEMERAL_APP);
if (disable_reasons == Extension::DISABLE_INACTIVE_EPHEMERAL_APP)
prefs->SetExtensionState(app->id(), Extension::ENABLED);
}
}
void EphemeralAppService::TriggerGarbageCollect(const base::TimeDelta& delay) {
if (!garbage_collect_apps_timer_.IsRunning()) {
garbage_collect_apps_timer_.Start(
FROM_HERE,
delay,
this,
&EphemeralAppService::GarbageCollectApps);
}
}
void EphemeralAppService::GarbageCollectApps() {
ExtensionRegistry* registry = ExtensionRegistry::Get(profile_);
DCHECK(registry);
ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_);
DCHECK(prefs);
scoped_ptr<ExtensionSet> extensions =
registry->GenerateInstalledExtensionsSet();
int app_count = 0;
LaunchTimeAppMap app_launch_times;
std::set<std::string> remove_app_ids;
// Populate a list of ephemeral apps, ordered by their last launch time.
for (ExtensionSet::const_iterator it = extensions->begin();
it != extensions->end(); ++it) {
const Extension* extension = it->get();
if (!prefs->IsEphemeralApp(extension->id()))
continue;
++app_count;
// Do not remove ephemeral apps that are running.
if (!extensions::util::IsExtensionIdle(extension->id(), profile_))
continue;
base::Time last_launch_time = prefs->GetLastLaunchTime(extension->id());
// If the last launch time is invalid, this may be because it was just
// installed. So use the install time. If this is also null for some reason,
// the app will be removed.
if (last_launch_time.is_null())
last_launch_time = prefs->GetInstallTime(extension->id());
app_launch_times.insert(std::make_pair(last_launch_time, extension->id()));
}
ExtensionService* service =
ExtensionSystem::Get(profile_)->extension_service();
DCHECK(service);
// Execute the eviction policies and remove apps marked for deletion.
if (!app_launch_times.empty()) {
GetAppsToRemove(app_count, app_launch_times, &remove_app_ids);
for (std::set<std::string>::const_iterator id = remove_app_ids.begin();
id != remove_app_ids.end(); ++id) {
// Protect against cascading uninstalls.
if (!registry->GetExtensionById(*id, ExtensionRegistry::EVERYTHING))
continue;
service->UninstallExtension(
*id,
extensions::UNINSTALL_REASON_ORPHANED_EPHEMERAL_EXTENSION,
base::Bind(&base::DoNothing),
NULL);
}
}
}
// static
void EphemeralAppService::GetAppsToRemove(
int app_count,
const LaunchTimeAppMap& app_launch_times,
std::set<std::string>* remove_app_ids) {
base::Time time_now = base::Time::Now();
const base::Time inactive_threshold =
time_now - base::TimeDelta::FromDays(kAppInactiveThreshold);
const base::Time keep_threshold =
time_now - base::TimeDelta::FromDays(kAppKeepThreshold);
// Visit the apps in order of least recently to most recently launched.
for (LaunchTimeAppMap::const_iterator it = app_launch_times.begin();
it != app_launch_times.end(); ++it) {
// Cannot remove apps that have been launched recently. So break when we
// reach the new apps.
if (it->first > keep_threshold)
break;
// Remove ephemeral apps that have been inactive for a while or if the cache
// is larger than the desired size.
if (it->first < inactive_threshold || app_count > kMaxEphemeralAppsCount) {
remove_app_ids->insert(it->second);
--app_count;
} else {
break;
}
}
}
|