1 // Copyright 2013 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 "chrome/browser/apps/ephemeral_app_service.h"
7 #include "base/command_line.h"
8 #include "chrome/browser/apps/ephemeral_app_service_factory.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/extensions/data_deleter.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/extension_util.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "content/public/browser/notification_service.h"
16 #include "content/public/browser/notification_source.h"
17 #include "content/public/browser/notification_types.h"
18 #include "extensions/browser/extension_prefs.h"
19 #include "extensions/browser/extension_registry.h"
20 #include "extensions/browser/extension_system.h"
21 #include "extensions/browser/extension_util.h"
22 #include "extensions/common/extension.h"
23 #include "extensions/common/extension_set.h"
25 using extensions::Extension
;
26 using extensions::ExtensionInfo
;
27 using extensions::ExtensionPrefs
;
28 using extensions::ExtensionSet
;
29 using extensions::ExtensionSystem
;
30 using extensions::InstalledExtensionInfo
;
34 // The number of seconds after startup before performing garbage collection
36 const int kGarbageCollectAppsStartupDelay
= 60;
38 // The number of seconds after an ephemeral app has been installed before
39 // performing garbage collection.
40 const int kGarbageCollectAppsInstallDelay
= 15;
42 // When the number of ephemeral apps reaches this count, trigger garbage
43 // collection to trim off the least-recently used apps in excess of
44 // kMaxEphemeralAppsCount.
45 const int kGarbageCollectAppsTriggerCount
= 35;
47 // The number of seconds after startup before performing garbage collection
48 // of the data of evicted ephemeral apps.
49 const int kGarbageCollectDataStartupDelay
= 120;
53 const int EphemeralAppService::kAppInactiveThreshold
= 10;
54 const int EphemeralAppService::kAppKeepThreshold
= 1;
55 const int EphemeralAppService::kMaxEphemeralAppsCount
= 30;
56 const int EphemeralAppService::kDataInactiveThreshold
= 90;
59 EphemeralAppService
* EphemeralAppService::Get(Profile
* profile
) {
60 return EphemeralAppServiceFactory::GetForProfile(profile
);
63 EphemeralAppService::EphemeralAppService(Profile
* profile
)
65 ephemeral_app_count_(-1) {
66 if (!CommandLine::ForCurrentProcess()->HasSwitch(
67 switches::kEnableEphemeralApps
))
71 chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED
,
72 content::Source
<Profile
>(profile_
));
73 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED
,
74 content::Source
<Profile
>(profile_
));
75 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY
,
76 content::Source
<Profile
>(profile_
));
77 registrar_
.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED
,
78 content::Source
<Profile
>(profile_
));
81 EphemeralAppService::~EphemeralAppService() {
84 void EphemeralAppService::Observe(
86 const content::NotificationSource
& source
,
87 const content::NotificationDetails
& details
) {
89 case chrome::NOTIFICATION_EXTENSIONS_READY
: {
93 case chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED
: {
94 const Extension
* extension
=
95 content::Details
<const InstalledExtensionInfo
>(details
)->extension
;
97 if (extensions::util::IsEphemeralApp(extension
->id(), profile_
)) {
98 ++ephemeral_app_count_
;
99 if (ephemeral_app_count_
>= kGarbageCollectAppsTriggerCount
)
100 TriggerGarbageCollect(
101 base::TimeDelta::FromSeconds(kGarbageCollectAppsInstallDelay
));
105 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED
: {
106 const Extension
* extension
=
107 content::Details
<const Extension
>(details
).ptr();
109 if (extensions::util::IsEphemeralApp(extension
->id(), profile_
))
110 --ephemeral_app_count_
;
113 case chrome::NOTIFICATION_PROFILE_DESTROYED
: {
114 // Ideally we need to know when the extension system is shutting down.
115 garbage_collect_apps_timer_
.Stop();
123 void EphemeralAppService::Init() {
124 InitEphemeralAppCount();
125 TriggerGarbageCollect(
126 base::TimeDelta::FromSeconds(kGarbageCollectAppsStartupDelay
));
128 garbage_collect_data_timer_
.Start(
130 base::TimeDelta::FromSeconds(kGarbageCollectDataStartupDelay
),
132 &EphemeralAppService::GarbageCollectData
);
135 void EphemeralAppService::InitEphemeralAppCount() {
136 scoped_ptr
<ExtensionSet
> extensions
=
137 extensions::ExtensionRegistry::Get(profile_
)
138 ->GenerateInstalledExtensionsSet();
139 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile_
);
142 ephemeral_app_count_
= 0;
143 for (ExtensionSet::const_iterator it
= extensions
->begin();
144 it
!= extensions
->end(); ++it
) {
145 const Extension
* extension
= *it
;
146 if (prefs
->IsEphemeralApp(extension
->id()))
147 ++ephemeral_app_count_
;
151 void EphemeralAppService::TriggerGarbageCollect(const base::TimeDelta
& delay
) {
152 if (!garbage_collect_apps_timer_
.IsRunning()) {
153 garbage_collect_apps_timer_
.Start(
157 &EphemeralAppService::GarbageCollectApps
);
161 void EphemeralAppService::GarbageCollectApps() {
162 scoped_ptr
<ExtensionSet
> extensions
=
163 extensions::ExtensionRegistry::Get(profile_
)
164 ->GenerateInstalledExtensionsSet();
165 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile_
);
169 LaunchTimeAppMap app_launch_times
;
170 std::set
<std::string
> remove_app_ids
;
172 // Populate a list of ephemeral apps, ordered by their last launch time.
173 for (ExtensionSet::const_iterator it
= extensions
->begin();
174 it
!= extensions
->end(); ++it
) {
175 const Extension
* extension
= *it
;
176 if (!prefs
->IsEphemeralApp(extension
->id()))
181 // Do not remove ephemeral apps that are running.
182 if (!extensions::util::IsExtensionIdle(extension
->id(), profile_
))
185 base::Time last_launch_time
= prefs
->GetLastLaunchTime(extension
->id());
187 // If the last launch time is invalid, this may be because it was just
188 // installed. So use the install time. If this is also null for some reason,
189 // the app will be removed.
190 if (last_launch_time
.is_null())
191 last_launch_time
= prefs
->GetInstallTime(extension
->id());
193 app_launch_times
.insert(std::make_pair(last_launch_time
, extension
->id()));
196 ExtensionService
* service
=
197 ExtensionSystem::Get(profile_
)->extension_service();
199 // Execute the replacement policies and remove apps marked for deletion.
200 if (!app_launch_times
.empty()) {
201 GetAppsToRemove(app_count
, app_launch_times
, &remove_app_ids
);
202 for (std::set
<std::string
>::const_iterator id
= remove_app_ids
.begin();
203 id
!= remove_app_ids
.end(); ++id
) {
204 if (service
->UninstallExtension(*id
, false, NULL
))
209 ephemeral_app_count_
= app_count
;
213 void EphemeralAppService::GetAppsToRemove(
215 const LaunchTimeAppMap
& app_launch_times
,
216 std::set
<std::string
>* remove_app_ids
) {
217 base::Time time_now
= base::Time::Now();
218 const base::Time inactive_threshold
=
219 time_now
- base::TimeDelta::FromDays(kAppInactiveThreshold
);
220 const base::Time keep_threshold
=
221 time_now
- base::TimeDelta::FromDays(kAppKeepThreshold
);
223 // Visit the apps in order of least recently to most recently launched.
224 for (LaunchTimeAppMap::const_iterator it
= app_launch_times
.begin();
225 it
!= app_launch_times
.end(); ++it
) {
226 // Cannot remove apps that have been launched recently. So break when we
227 // reach the new apps.
228 if (it
->first
> keep_threshold
)
231 // Remove ephemeral apps that have been inactive for a while or if the cache
232 // is larger than the desired size.
233 if (it
->first
< inactive_threshold
|| app_count
> kMaxEphemeralAppsCount
) {
234 remove_app_ids
->insert(it
->second
);
242 void EphemeralAppService::GarbageCollectData() {
243 ExtensionService
* service
=
244 ExtensionSystem::Get(profile_
)->extension_service();
246 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile_
);
248 scoped_ptr
<ExtensionPrefs::ExtensionsInfo
> evicted_apps_info(
249 prefs
->GetEvictedEphemeralAppsInfo());
251 base::Time time_now
= base::Time::Now();
252 const base::Time inactive_threshold
=
253 time_now
- base::TimeDelta::FromDays(kDataInactiveThreshold
);
255 for (size_t i
= 0; i
< evicted_apps_info
->size(); ++i
) {
256 ExtensionInfo
* info
= evicted_apps_info
->at(i
).get();
257 base::Time last_launch_time
= prefs
->GetLastLaunchTime(info
->extension_id
);
258 if (last_launch_time
> inactive_threshold
)
261 // Sanity check to ensure the app is not currently installed.
262 if (service
->GetInstalledExtension(info
->extension_id
)) {
267 // Ensure the app is not waiting to be installed.
268 scoped_ptr
<ExtensionInfo
> delayed_install(
269 prefs
->GetDelayedInstallInfo(info
->extension_id
));
270 if (delayed_install
.get())
273 if (info
->extension_manifest
.get()) {
275 scoped_refptr
<const Extension
> extension(Extension::Create(
276 info
->extension_path
,
277 info
->extension_location
,
278 *info
->extension_manifest
,
279 prefs
->GetCreationFlags(info
->extension_id
),
284 extensions::DataDeleter::StartDeleting(profile_
, extension
.get());
287 prefs
->RemoveEvictedEphemeralApp(info
->extension_id
);