OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/apps/ephemeral_app_service.h" | 5 #include "chrome/browser/apps/ephemeral_app_service.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "chrome/browser/apps/ephemeral_app_service_factory.h" | 8 #include "chrome/browser/apps/ephemeral_app_service_factory.h" |
9 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
10 #include "chrome/browser/extensions/data_deleter.h" | 10 #include "chrome/browser/extensions/data_deleter.h" |
11 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
12 #include "chrome/browser/extensions/extension_util.h" | 12 #include "chrome/browser/extensions/extension_util.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
15 #include "content/public/browser/notification_service.h" | 15 #include "content/public/browser/notification_service.h" |
16 #include "content/public/browser/notification_source.h" | 16 #include "content/public/browser/notification_source.h" |
17 #include "content/public/browser/notification_types.h" | 17 #include "content/public/browser/notification_types.h" |
18 #include "extensions/browser/extension_prefs.h" | 18 #include "extensions/browser/extension_prefs.h" |
19 #include "extensions/browser/extension_registry.h" | 19 #include "extensions/browser/extension_registry.h" |
20 #include "extensions/browser/extension_system.h" | 20 #include "extensions/browser/extension_system.h" |
| 21 #include "extensions/browser/extension_util.h" |
21 #include "extensions/common/extension.h" | 22 #include "extensions/common/extension.h" |
22 #include "extensions/common/extension_set.h" | 23 #include "extensions/common/extension_set.h" |
23 | 24 |
24 using extensions::Extension; | 25 using extensions::Extension; |
25 using extensions::ExtensionInfo; | 26 using extensions::ExtensionInfo; |
26 using extensions::ExtensionPrefs; | 27 using extensions::ExtensionPrefs; |
27 using extensions::ExtensionSet; | 28 using extensions::ExtensionSet; |
28 using extensions::ExtensionSystem; | 29 using extensions::ExtensionSystem; |
29 using extensions::InstalledExtensionInfo; | 30 using extensions::InstalledExtensionInfo; |
30 | 31 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 const content::NotificationDetails& details) { | 86 const content::NotificationDetails& details) { |
86 switch (type) { | 87 switch (type) { |
87 case chrome::NOTIFICATION_EXTENSIONS_READY: { | 88 case chrome::NOTIFICATION_EXTENSIONS_READY: { |
88 Init(); | 89 Init(); |
89 break; | 90 break; |
90 } | 91 } |
91 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { | 92 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { |
92 const Extension* extension = | 93 const Extension* extension = |
93 content::Details<const InstalledExtensionInfo>(details)->extension; | 94 content::Details<const InstalledExtensionInfo>(details)->extension; |
94 DCHECK(extension); | 95 DCHECK(extension); |
95 if (extension->is_ephemeral()) { | 96 if (extensions::util::IsEphemeralApp(extension->id(), profile_)) { |
96 ++ephemeral_app_count_; | 97 ++ephemeral_app_count_; |
97 if (ephemeral_app_count_ >= kGarbageCollectAppsTriggerCount) | 98 if (ephemeral_app_count_ >= kGarbageCollectAppsTriggerCount) |
98 TriggerGarbageCollect( | 99 TriggerGarbageCollect( |
99 base::TimeDelta::FromSeconds(kGarbageCollectAppsInstallDelay)); | 100 base::TimeDelta::FromSeconds(kGarbageCollectAppsInstallDelay)); |
100 } | 101 } |
101 break; | 102 break; |
102 } | 103 } |
103 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { | 104 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { |
104 const Extension* extension = | 105 const Extension* extension = |
105 content::Details<const Extension>(details).ptr(); | 106 content::Details<const Extension>(details).ptr(); |
106 DCHECK(extension); | 107 DCHECK(extension); |
107 if (extension->is_ephemeral()) | 108 if (extensions::util::IsEphemeralApp(extension->id(), profile_)) |
108 --ephemeral_app_count_; | 109 --ephemeral_app_count_; |
109 break; | 110 break; |
110 } | 111 } |
111 case chrome::NOTIFICATION_PROFILE_DESTROYED: { | 112 case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
112 // Ideally we need to know when the extension system is shutting down. | 113 // Ideally we need to know when the extension system is shutting down. |
113 garbage_collect_apps_timer_.Stop(); | 114 garbage_collect_apps_timer_.Stop(); |
114 break; | 115 break; |
115 } | 116 } |
116 default: | 117 default: |
117 NOTREACHED(); | 118 NOTREACHED(); |
118 }; | 119 } |
119 } | 120 } |
120 | 121 |
121 void EphemeralAppService::Init() { | 122 void EphemeralAppService::Init() { |
122 InitEphemeralAppCount(); | 123 InitEphemeralAppCount(); |
123 TriggerGarbageCollect( | 124 TriggerGarbageCollect( |
124 base::TimeDelta::FromSeconds(kGarbageCollectAppsStartupDelay)); | 125 base::TimeDelta::FromSeconds(kGarbageCollectAppsStartupDelay)); |
125 | 126 |
126 garbage_collect_data_timer_.Start( | 127 garbage_collect_data_timer_.Start( |
127 FROM_HERE, | 128 FROM_HERE, |
128 base::TimeDelta::FromSeconds(kGarbageCollectDataStartupDelay), | 129 base::TimeDelta::FromSeconds(kGarbageCollectDataStartupDelay), |
129 this, | 130 this, |
130 &EphemeralAppService::GarbageCollectData); | 131 &EphemeralAppService::GarbageCollectData); |
131 } | 132 } |
132 | 133 |
133 void EphemeralAppService::InitEphemeralAppCount() { | 134 void EphemeralAppService::InitEphemeralAppCount() { |
134 scoped_ptr<ExtensionSet> extensions = | 135 scoped_ptr<ExtensionSet> extensions = |
135 extensions::ExtensionRegistry::Get(profile_) | 136 extensions::ExtensionRegistry::Get(profile_) |
136 ->GenerateInstalledExtensionsSet(); | 137 ->GenerateInstalledExtensionsSet(); |
| 138 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); |
| 139 DCHECK(prefs); |
137 | 140 |
138 ephemeral_app_count_ = 0; | 141 ephemeral_app_count_ = 0; |
139 for (ExtensionSet::const_iterator it = extensions->begin(); | 142 for (ExtensionSet::const_iterator it = extensions->begin(); |
140 it != extensions->end(); ++it) { | 143 it != extensions->end(); ++it) { |
141 const Extension* extension = *it; | 144 const Extension* extension = *it; |
142 if (extension->is_ephemeral()) | 145 if (prefs->IsEphemeralApp(extension->id())) |
143 ++ephemeral_app_count_; | 146 ++ephemeral_app_count_; |
144 } | 147 } |
145 } | 148 } |
146 | 149 |
147 void EphemeralAppService::TriggerGarbageCollect(const base::TimeDelta& delay) { | 150 void EphemeralAppService::TriggerGarbageCollect(const base::TimeDelta& delay) { |
148 if (!garbage_collect_apps_timer_.IsRunning()) { | 151 if (!garbage_collect_apps_timer_.IsRunning()) { |
149 garbage_collect_apps_timer_.Start( | 152 garbage_collect_apps_timer_.Start( |
150 FROM_HERE, | 153 FROM_HERE, |
151 delay, | 154 delay, |
152 this, | 155 this, |
153 &EphemeralAppService::GarbageCollectApps); | 156 &EphemeralAppService::GarbageCollectApps); |
154 } | 157 } |
155 } | 158 } |
156 | 159 |
157 void EphemeralAppService::GarbageCollectApps() { | 160 void EphemeralAppService::GarbageCollectApps() { |
158 scoped_ptr<ExtensionSet> extensions = | 161 scoped_ptr<ExtensionSet> extensions = |
159 extensions::ExtensionRegistry::Get(profile_) | 162 extensions::ExtensionRegistry::Get(profile_) |
160 ->GenerateInstalledExtensionsSet(); | 163 ->GenerateInstalledExtensionsSet(); |
161 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); | 164 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); |
| 165 DCHECK(prefs); |
162 | 166 |
163 int app_count = 0; | 167 int app_count = 0; |
164 LaunchTimeAppMap app_launch_times; | 168 LaunchTimeAppMap app_launch_times; |
165 std::set<std::string> remove_app_ids; | 169 std::set<std::string> remove_app_ids; |
166 | 170 |
167 // Populate a list of ephemeral apps, ordered by their last launch time. | 171 // Populate a list of ephemeral apps, ordered by their last launch time. |
168 for (ExtensionSet::const_iterator it = extensions->begin(); | 172 for (ExtensionSet::const_iterator it = extensions->begin(); |
169 it != extensions->end(); ++it) { | 173 it != extensions->end(); ++it) { |
170 const Extension* extension = *it; | 174 const Extension* extension = *it; |
171 if (!extension->is_ephemeral()) | 175 if (!prefs->IsEphemeralApp(extension->id())) |
172 continue; | 176 continue; |
173 | 177 |
174 ++app_count; | 178 ++app_count; |
175 | 179 |
176 // Do not remove ephemeral apps that are running. | 180 // Do not remove ephemeral apps that are running. |
177 if (!extensions::util::IsExtensionIdle(extension->id(), profile_)) | 181 if (!extensions::util::IsExtensionIdle(extension->id(), profile_)) |
178 continue; | 182 continue; |
179 | 183 |
180 base::Time last_launch_time = prefs->GetLastLaunchTime(extension->id()); | 184 base::Time last_launch_time = prefs->GetLastLaunchTime(extension->id()); |
181 | 185 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 info->extension_id, | 279 info->extension_id, |
276 &error)); | 280 &error)); |
277 | 281 |
278 if (extension.get()) | 282 if (extension.get()) |
279 extensions::DataDeleter::StartDeleting(profile_, extension.get()); | 283 extensions::DataDeleter::StartDeleting(profile_, extension.get()); |
280 } | 284 } |
281 | 285 |
282 prefs->RemoveEvictedEphemeralApp(info->extension_id); | 286 prefs->RemoveEvictedEphemeralApp(info->extension_id); |
283 } | 287 } |
284 } | 288 } |
OLD | NEW |