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
3 // found in the LICENSE file.
5 #include "chrome/browser/background/background_application_list_model.h"
10 #include "base/stl_util.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/app/chrome_command_ids.h"
14 #include "chrome/browser/background/background_contents_service.h"
15 #include "chrome/browser/background/background_contents_service_factory.h"
16 #include "chrome/browser/background/background_mode_manager.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/chrome_notification_types.h"
19 #include "chrome/browser/extensions/extension_service.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/common/extensions/extension_constants.h"
22 #include "components/crx_file/id_util.h"
23 #include "content/public/browser/notification_details.h"
24 #include "content/public/browser/notification_source.h"
25 #include "extensions/browser/extension_prefs.h"
26 #include "extensions/browser/extension_registry.h"
27 #include "extensions/browser/extension_system.h"
28 #include "extensions/browser/extension_util.h"
29 #include "extensions/browser/image_loader.h"
30 #include "extensions/browser/notification_types.h"
31 #include "extensions/common/extension.h"
32 #include "extensions/common/extension_icon_set.h"
33 #include "extensions/common/extension_resource.h"
34 #include "extensions/common/extension_set.h"
35 #include "extensions/common/manifest_handlers/background_info.h"
36 #include "extensions/common/manifest_handlers/icons_handler.h"
37 #include "extensions/common/permissions/permission_set.h"
38 #include "extensions/common/permissions/permissions_data.h"
39 #include "ui/base/l10n/l10n_util_collator.h"
40 #include "ui/gfx/image/image.h"
41 #include "ui/gfx/image/image_skia.h"
43 using extensions::APIPermission
;
44 using extensions::Extension
;
45 using extensions::ExtensionList
;
46 using extensions::ExtensionRegistry
;
47 using extensions::ExtensionSet
;
48 using extensions::PermissionSet
;
49 using extensions::UnloadedExtensionInfo
;
50 using extensions::UpdatedExtensionPermissionsInfo
;
52 class ExtensionNameComparator
{
54 bool operator()(const scoped_refptr
<const Extension
>& x
,
55 const scoped_refptr
<const Extension
>& y
) {
56 return x
->name() < y
->name();
60 // Background application representation, private to the
61 // BackgroundApplicationListModel class.
62 class BackgroundApplicationListModel::Application
63 : public base::SupportsWeakPtr
<Application
> {
65 Application(BackgroundApplicationListModel
* model
,
66 const Extension
* an_extension
);
68 virtual ~Application();
70 // Invoked when a request icon is available.
71 void OnImageLoaded(const gfx::Image
& image
);
73 // Uses the FILE thread to request this extension's icon, sized
75 void RequestIcon(extension_misc::ExtensionIcons size
);
77 const Extension
* extension_
;
78 scoped_ptr
<gfx::ImageSkia
> icon_
;
79 BackgroundApplicationListModel
* model_
;
83 void GetServiceApplications(ExtensionService
* service
,
84 ExtensionList
* applications_result
) {
85 ExtensionRegistry
* registry
= ExtensionRegistry::Get(service
->profile());
86 const ExtensionSet
& enabled_extensions
= registry
->enabled_extensions();
88 for (ExtensionSet::const_iterator cursor
= enabled_extensions
.begin();
89 cursor
!= enabled_extensions
.end();
91 const Extension
* extension
= cursor
->get();
92 if (BackgroundApplicationListModel::IsBackgroundApp(*extension
,
93 service
->profile())) {
94 applications_result
->push_back(extension
);
98 // Walk the list of terminated extensions also (just because an extension
99 // crashed doesn't mean we should ignore it).
100 const ExtensionSet
& terminated_extensions
= registry
->terminated_extensions();
101 for (ExtensionSet::const_iterator cursor
= terminated_extensions
.begin();
102 cursor
!= terminated_extensions
.end();
104 const Extension
* extension
= cursor
->get();
105 if (BackgroundApplicationListModel::IsBackgroundApp(*extension
,
106 service
->profile())) {
107 applications_result
->push_back(extension
);
111 std::sort(applications_result
->begin(), applications_result
->end(),
112 ExtensionNameComparator());
118 BackgroundApplicationListModel::Observer::OnApplicationDataChanged(
119 const Extension
* extension
, Profile
* profile
) {
123 BackgroundApplicationListModel::Observer::OnApplicationListChanged(
127 BackgroundApplicationListModel::Observer::~Observer() {
130 BackgroundApplicationListModel::Application::~Application() {
133 BackgroundApplicationListModel::Application::Application(
134 BackgroundApplicationListModel
* model
,
135 const Extension
* extension
)
136 : extension_(extension
), model_(model
) {}
138 void BackgroundApplicationListModel::Application::OnImageLoaded(
139 const gfx::Image
& image
) {
142 icon_
.reset(image
.CopyImageSkia());
143 model_
->SendApplicationDataChangedNotifications(extension_
);
146 void BackgroundApplicationListModel::Application::RequestIcon(
147 extension_misc::ExtensionIcons size
) {
148 extensions::ExtensionResource resource
=
149 extensions::IconsInfo::GetIconResource(
150 extension_
, size
, ExtensionIconSet::MATCH_BIGGER
);
151 extensions::ImageLoader::Get(model_
->profile_
)->LoadImageAsync(
152 extension_
, resource
, gfx::Size(size
, size
),
153 base::Bind(&Application::OnImageLoaded
, AsWeakPtr()));
156 BackgroundApplicationListModel::~BackgroundApplicationListModel() {
157 STLDeleteContainerPairSecondPointers(applications_
.begin(),
158 applications_
.end());
161 BackgroundApplicationListModel::BackgroundApplicationListModel(Profile
* profile
)
166 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
,
167 content::Source
<Profile
>(profile
));
169 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED
,
170 content::Source
<Profile
>(profile
));
172 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED
,
173 content::Source
<Profile
>(profile
));
175 extensions::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED
,
176 content::Source
<Profile
>(profile
));
178 chrome::NOTIFICATION_BACKGROUND_CONTENTS_SERVICE_CHANGED
,
179 content::Source
<Profile
>(profile
));
180 ExtensionService
* service
= extensions::ExtensionSystem::Get(profile
)->
182 if (service
&& service
->is_ready()) {
188 void BackgroundApplicationListModel::AddObserver(Observer
* observer
) {
189 observers_
.AddObserver(observer
);
192 void BackgroundApplicationListModel::AssociateApplicationData(
193 const Extension
* extension
) {
194 DCHECK(IsBackgroundApp(*extension
, profile_
));
195 Application
* application
= FindApplication(extension
);
197 // App position is used as a dynamic command and so must be less than any
198 // predefined command id.
199 if (applications_
.size() >= IDC_MinimumLabelValue
) {
200 LOG(ERROR
) << "Background application limit of " << IDC_MinimumLabelValue
201 << " exceeded. Ignoring.";
204 application
= new Application(this, extension
);
205 applications_
[extension
->id()] = application
;
207 application
->RequestIcon(extension_misc::EXTENSION_ICON_BITTY
);
211 void BackgroundApplicationListModel::DissociateApplicationData(
212 const Extension
* extension
) {
213 ApplicationMap::iterator found
= applications_
.find(extension
->id());
214 if (found
!= applications_
.end()) {
215 delete found
->second
;
216 applications_
.erase(found
);
220 const Extension
* BackgroundApplicationListModel::GetExtension(
221 int position
) const {
222 DCHECK(position
>= 0 && static_cast<size_t>(position
) < extensions_
.size());
223 return extensions_
[position
].get();
226 const BackgroundApplicationListModel::Application
*
227 BackgroundApplicationListModel::FindApplication(
228 const Extension
* extension
) const {
229 const std::string
& id
= extension
->id();
230 ApplicationMap::const_iterator found
= applications_
.find(id
);
231 return (found
== applications_
.end()) ? NULL
: found
->second
;
234 BackgroundApplicationListModel::Application
*
235 BackgroundApplicationListModel::FindApplication(
236 const Extension
* extension
) {
237 const std::string
& id
= extension
->id();
238 ApplicationMap::iterator found
= applications_
.find(id
);
239 return (found
== applications_
.end()) ? NULL
: found
->second
;
242 const gfx::ImageSkia
* BackgroundApplicationListModel::GetIcon(
243 const Extension
* extension
) {
244 const Application
* application
= FindApplication(extension
);
246 return application
->icon_
.get();
247 AssociateApplicationData(extension
);
251 int BackgroundApplicationListModel::GetPosition(
252 const Extension
* extension
) const {
254 const std::string
& id
= extension
->id();
255 for (ExtensionList::const_iterator cursor
= extensions_
.begin();
256 cursor
!= extensions_
.end();
257 ++cursor
, ++position
) {
258 if (id
== cursor
->get()->id())
266 bool BackgroundApplicationListModel::IsBackgroundApp(
267 const Extension
& extension
, Profile
* profile
) {
268 // An extension is a "background app" if it has the "background API"
269 // permission, and meets one of the following criteria:
270 // 1) It is an extension (not a hosted app).
271 // 2) It is a hosted app, and has a background contents registered or in the
274 // Ephemeral apps are denied any background activity after their event page
275 // has been destroyed, thus they cannot be background apps.
276 if (extensions::util::IsEphemeralApp(extension
.id(), profile
))
279 // Not a background app if we don't have the background permission.
280 if (!extension
.permissions_data()->HasAPIPermission(
281 APIPermission::kBackground
)) {
285 // Extensions and packaged apps with background permission are always treated
286 // as background apps.
287 if (!extension
.is_hosted_app())
290 // Hosted apps with manifest-provided background pages are background apps.
291 if (extensions::BackgroundInfo::HasBackgroundPage(&extension
))
294 BackgroundContentsService
* service
=
295 BackgroundContentsServiceFactory::GetForProfile(profile
);
296 base::string16 app_id
= base::ASCIIToUTF16(extension
.id());
297 // If we have an active or registered background contents for this app, then
298 // it's a background app. This covers the cases where the app has created its
299 // background contents, but it hasn't navigated yet, or the background
300 // contents crashed and hasn't yet been restarted - in both cases we still
301 // want to treat the app as a background app.
302 if (service
->GetAppBackgroundContents(app_id
) ||
303 service
->HasRegisteredBackgroundContents(app_id
)) {
307 // Doesn't meet our criteria, so it's not a background app.
311 void BackgroundApplicationListModel::Observe(
313 const content::NotificationSource
& source
,
314 const content::NotificationDetails
& details
) {
315 if (type
== extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED
) {
320 ExtensionService
* service
= extensions::ExtensionSystem::Get(profile_
)->
322 if (!service
|| !service
->is_ready())
326 case extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
:
327 OnExtensionLoaded(content::Details
<Extension
>(details
).ptr());
329 case extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED
:
331 content::Details
<UnloadedExtensionInfo
>(details
)->extension
);
333 case extensions::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED
:
334 OnExtensionPermissionsUpdated(
335 content::Details
<UpdatedExtensionPermissionsInfo
>(details
)->extension
,
336 content::Details
<UpdatedExtensionPermissionsInfo
>(details
)->reason
,
337 content::Details
<UpdatedExtensionPermissionsInfo
>(details
)->
340 case chrome::NOTIFICATION_BACKGROUND_CONTENTS_SERVICE_CHANGED
:
344 NOTREACHED() << "Received unexpected notification";
348 void BackgroundApplicationListModel::SendApplicationDataChangedNotifications(
349 const Extension
* extension
) {
350 FOR_EACH_OBSERVER(Observer
, observers_
, OnApplicationDataChanged(extension
,
354 void BackgroundApplicationListModel::OnExtensionLoaded(
355 const Extension
* extension
) {
356 // We only care about extensions that are background applications
357 if (!IsBackgroundApp(*extension
, profile_
))
359 AssociateApplicationData(extension
);
362 void BackgroundApplicationListModel::OnExtensionUnloaded(
363 const Extension
* extension
) {
364 if (!IsBackgroundApp(*extension
, profile_
))
367 DissociateApplicationData(extension
);
370 void BackgroundApplicationListModel::OnExtensionPermissionsUpdated(
371 const Extension
* extension
,
372 UpdatedExtensionPermissionsInfo::Reason reason
,
373 const PermissionSet
* permissions
) {
374 if (permissions
->HasAPIPermission(APIPermission::kBackground
)) {
376 case UpdatedExtensionPermissionsInfo::ADDED
:
377 DCHECK(IsBackgroundApp(*extension
, profile_
));
378 OnExtensionLoaded(extension
);
380 case UpdatedExtensionPermissionsInfo::REMOVED
:
381 DCHECK(!IsBackgroundApp(*extension
, profile_
));
383 DissociateApplicationData(extension
);
391 void BackgroundApplicationListModel::RemoveObserver(Observer
* observer
) {
392 observers_
.RemoveObserver(observer
);
395 // Update queries the extensions service of the profile with which the model was
396 // initialized to determine the current set of background applications. If that
397 // differs from the old list, it generates OnApplicationListChanged events for
399 void BackgroundApplicationListModel::Update() {
400 ExtensionService
* service
= extensions::ExtensionSystem::Get(profile_
)->
403 // Discover current background applications, compare with previous list, which
404 // is consistently sorted, and notify observers if they differ.
405 ExtensionList extensions
;
406 GetServiceApplications(service
, &extensions
);
407 ExtensionList::const_iterator old_cursor
= extensions_
.begin();
408 ExtensionList::const_iterator new_cursor
= extensions
.begin();
409 while (old_cursor
!= extensions_
.end() &&
410 new_cursor
!= extensions
.end() &&
411 (*old_cursor
)->name() == (*new_cursor
)->name() &&
412 (*old_cursor
)->id() == (*new_cursor
)->id()) {
416 if (old_cursor
!= extensions_
.end() || new_cursor
!= extensions
.end()) {
417 extensions_
= extensions
;
418 FOR_EACH_OBSERVER(Observer
, observers_
, OnApplicationListChanged(profile_
));