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 explicit ExtensionNameComparator(icu::Collator
* collator
);
55 bool operator()(const scoped_refptr
<const Extension
>& x
,
56 const scoped_refptr
<const Extension
>& y
);
59 icu::Collator
* collator_
;
62 ExtensionNameComparator::ExtensionNameComparator(icu::Collator
* collator
)
63 : collator_(collator
) {
66 bool ExtensionNameComparator::operator()(
67 const scoped_refptr
<const Extension
>& x
,
68 const scoped_refptr
<const Extension
>& y
) {
69 return l10n_util::StringComparator
<base::string16
>(collator_
)(
70 base::UTF8ToUTF16(x
->name()), base::UTF8ToUTF16(y
->name()));
73 // Background application representation, private to the
74 // BackgroundApplicationListModel class.
75 class BackgroundApplicationListModel::Application
76 : public base::SupportsWeakPtr
<Application
> {
78 Application(BackgroundApplicationListModel
* model
,
79 const Extension
* an_extension
);
81 virtual ~Application();
83 // Invoked when a request icon is available.
84 void OnImageLoaded(const gfx::Image
& image
);
86 // Uses the FILE thread to request this extension's icon, sized
88 void RequestIcon(extension_misc::ExtensionIcons size
);
90 const Extension
* extension_
;
91 scoped_ptr
<gfx::ImageSkia
> icon_
;
92 BackgroundApplicationListModel
* model_
;
96 void GetServiceApplications(ExtensionService
* service
,
97 ExtensionList
* applications_result
) {
98 ExtensionRegistry
* registry
= ExtensionRegistry::Get(service
->profile());
99 const ExtensionSet
& enabled_extensions
= registry
->enabled_extensions();
101 for (ExtensionSet::const_iterator cursor
= enabled_extensions
.begin();
102 cursor
!= enabled_extensions
.end();
104 const Extension
* extension
= cursor
->get();
105 if (BackgroundApplicationListModel::IsBackgroundApp(*extension
,
106 service
->profile())) {
107 applications_result
->push_back(extension
);
111 // Walk the list of terminated extensions also (just because an extension
112 // crashed doesn't mean we should ignore it).
113 const ExtensionSet
& terminated_extensions
= registry
->terminated_extensions();
114 for (ExtensionSet::const_iterator cursor
= terminated_extensions
.begin();
115 cursor
!= terminated_extensions
.end();
117 const Extension
* extension
= cursor
->get();
118 if (BackgroundApplicationListModel::IsBackgroundApp(*extension
,
119 service
->profile())) {
120 applications_result
->push_back(extension
);
124 std::string locale
= g_browser_process
->GetApplicationLocale();
125 icu::Locale
loc(locale
.c_str());
126 UErrorCode error
= U_ZERO_ERROR
;
127 scoped_ptr
<icu::Collator
> collator(icu::Collator::createInstance(loc
, error
));
128 std::sort(applications_result
->begin(), applications_result
->end(),
129 ExtensionNameComparator(collator
.get()));
135 BackgroundApplicationListModel::Observer::OnApplicationDataChanged(
136 const Extension
* extension
, Profile
* profile
) {
140 BackgroundApplicationListModel::Observer::OnApplicationListChanged(
144 BackgroundApplicationListModel::Observer::~Observer() {
147 BackgroundApplicationListModel::Application::~Application() {
150 BackgroundApplicationListModel::Application::Application(
151 BackgroundApplicationListModel
* model
,
152 const Extension
* extension
)
153 : extension_(extension
), model_(model
) {}
155 void BackgroundApplicationListModel::Application::OnImageLoaded(
156 const gfx::Image
& image
) {
159 icon_
.reset(image
.CopyImageSkia());
160 model_
->SendApplicationDataChangedNotifications(extension_
);
163 void BackgroundApplicationListModel::Application::RequestIcon(
164 extension_misc::ExtensionIcons size
) {
165 extensions::ExtensionResource resource
=
166 extensions::IconsInfo::GetIconResource(
167 extension_
, size
, ExtensionIconSet::MATCH_BIGGER
);
168 extensions::ImageLoader::Get(model_
->profile_
)->LoadImageAsync(
169 extension_
, resource
, gfx::Size(size
, size
),
170 base::Bind(&Application::OnImageLoaded
, AsWeakPtr()));
173 BackgroundApplicationListModel::~BackgroundApplicationListModel() {
174 STLDeleteContainerPairSecondPointers(applications_
.begin(),
175 applications_
.end());
178 BackgroundApplicationListModel::BackgroundApplicationListModel(Profile
* profile
)
183 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
,
184 content::Source
<Profile
>(profile
));
186 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED
,
187 content::Source
<Profile
>(profile
));
189 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED
,
190 content::Source
<Profile
>(profile
));
192 extensions::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED
,
193 content::Source
<Profile
>(profile
));
195 chrome::NOTIFICATION_BACKGROUND_CONTENTS_SERVICE_CHANGED
,
196 content::Source
<Profile
>(profile
));
197 ExtensionService
* service
= extensions::ExtensionSystem::Get(profile
)->
199 if (service
&& service
->is_ready()) {
205 void BackgroundApplicationListModel::AddObserver(Observer
* observer
) {
206 observers_
.AddObserver(observer
);
209 void BackgroundApplicationListModel::AssociateApplicationData(
210 const Extension
* extension
) {
211 DCHECK(IsBackgroundApp(*extension
, profile_
));
212 Application
* application
= FindApplication(extension
);
214 // App position is used as a dynamic command and so must be less than any
215 // predefined command id.
216 if (applications_
.size() >= IDC_MinimumLabelValue
) {
217 LOG(ERROR
) << "Background application limit of " << IDC_MinimumLabelValue
218 << " exceeded. Ignoring.";
221 application
= new Application(this, extension
);
222 applications_
[extension
->id()] = application
;
224 application
->RequestIcon(extension_misc::EXTENSION_ICON_BITTY
);
228 void BackgroundApplicationListModel::DissociateApplicationData(
229 const Extension
* extension
) {
230 ApplicationMap::iterator found
= applications_
.find(extension
->id());
231 if (found
!= applications_
.end()) {
232 delete found
->second
;
233 applications_
.erase(found
);
237 const Extension
* BackgroundApplicationListModel::GetExtension(
238 int position
) const {
239 DCHECK(position
>= 0 && static_cast<size_t>(position
) < extensions_
.size());
240 return extensions_
[position
].get();
243 const BackgroundApplicationListModel::Application
*
244 BackgroundApplicationListModel::FindApplication(
245 const Extension
* extension
) const {
246 const std::string
& id
= extension
->id();
247 ApplicationMap::const_iterator found
= applications_
.find(id
);
248 return (found
== applications_
.end()) ? NULL
: found
->second
;
251 BackgroundApplicationListModel::Application
*
252 BackgroundApplicationListModel::FindApplication(
253 const Extension
* extension
) {
254 const std::string
& id
= extension
->id();
255 ApplicationMap::iterator found
= applications_
.find(id
);
256 return (found
== applications_
.end()) ? NULL
: found
->second
;
259 const gfx::ImageSkia
* BackgroundApplicationListModel::GetIcon(
260 const Extension
* extension
) {
261 const Application
* application
= FindApplication(extension
);
263 return application
->icon_
.get();
264 AssociateApplicationData(extension
);
268 int BackgroundApplicationListModel::GetPosition(
269 const Extension
* extension
) const {
271 const std::string
& id
= extension
->id();
272 for (ExtensionList::const_iterator cursor
= extensions_
.begin();
273 cursor
!= extensions_
.end();
274 ++cursor
, ++position
) {
275 if (id
== cursor
->get()->id())
283 bool BackgroundApplicationListModel::RequiresBackgroundModeForPushMessaging(
284 const Extension
& extension
) {
285 // No PushMessaging permission - does not require the background mode.
286 if (!extension
.permissions_data()->HasAPIPermission(
287 APIPermission::kPushMessaging
)) {
291 // If in the whitelist, then does not require background mode even if
292 // uses push messaging.
293 // TODO(dimich): remove this whitelist once we have a better way to keep
294 // listening for GCM. http://crbug.com/311268
295 std::string hexencoded_id_hash
=
296 crx_file::id_util::HashedIdInHex(extension
.id());
297 // The id starting from "9A04..." is a one from unit test.
298 if (hexencoded_id_hash
== "C41AD9DCD670210295614257EF8C9945AD68D86E" ||
299 hexencoded_id_hash
== "9A0417016F345C934A1A88F55CA17C05014EEEBA")
306 bool BackgroundApplicationListModel::IsBackgroundApp(
307 const Extension
& extension
, Profile
* profile
) {
308 // An extension is a "background app" if it has the "background API"
309 // permission, and meets one of the following criteria:
310 // 1) It is an extension (not a hosted app).
311 // 2) It is a hosted app, and has a background contents registered or in the
314 // Ephemeral apps are denied any background activity after their event page
315 // has been destroyed, thus they cannot be background apps.
316 if (extensions::util::IsEphemeralApp(extension
.id(), profile
))
319 // Not a background app if we don't have the background permission or
320 // the push messaging permission
321 if (!extension
.permissions_data()->HasAPIPermission(
322 APIPermission::kBackground
) &&
323 !RequiresBackgroundModeForPushMessaging(extension
))
326 // Extensions and packaged apps with background permission are always treated
327 // as background apps.
328 if (!extension
.is_hosted_app())
331 // Hosted apps with manifest-provided background pages are background apps.
332 if (extensions::BackgroundInfo::HasBackgroundPage(&extension
))
335 BackgroundContentsService
* service
=
336 BackgroundContentsServiceFactory::GetForProfile(profile
);
337 base::string16 app_id
= base::ASCIIToUTF16(extension
.id());
338 // If we have an active or registered background contents for this app, then
339 // it's a background app. This covers the cases where the app has created its
340 // background contents, but it hasn't navigated yet, or the background
341 // contents crashed and hasn't yet been restarted - in both cases we still
342 // want to treat the app as a background app.
343 if (service
->GetAppBackgroundContents(app_id
) ||
344 service
->HasRegisteredBackgroundContents(app_id
)) {
348 // Doesn't meet our criteria, so it's not a background app.
352 void BackgroundApplicationListModel::Observe(
354 const content::NotificationSource
& source
,
355 const content::NotificationDetails
& details
) {
356 if (type
== extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED
) {
361 ExtensionService
* service
= extensions::ExtensionSystem::Get(profile_
)->
363 if (!service
|| !service
->is_ready())
367 case extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
:
368 OnExtensionLoaded(content::Details
<Extension
>(details
).ptr());
370 case extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED
:
372 content::Details
<UnloadedExtensionInfo
>(details
)->extension
);
374 case extensions::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED
:
375 OnExtensionPermissionsUpdated(
376 content::Details
<UpdatedExtensionPermissionsInfo
>(details
)->extension
,
377 content::Details
<UpdatedExtensionPermissionsInfo
>(details
)->reason
,
378 content::Details
<UpdatedExtensionPermissionsInfo
>(details
)->
381 case chrome::NOTIFICATION_BACKGROUND_CONTENTS_SERVICE_CHANGED
:
385 NOTREACHED() << "Received unexpected notification";
389 void BackgroundApplicationListModel::SendApplicationDataChangedNotifications(
390 const Extension
* extension
) {
391 FOR_EACH_OBSERVER(Observer
, observers_
, OnApplicationDataChanged(extension
,
395 void BackgroundApplicationListModel::OnExtensionLoaded(
396 const Extension
* extension
) {
397 // We only care about extensions that are background applications
398 if (!IsBackgroundApp(*extension
, profile_
))
400 AssociateApplicationData(extension
);
403 void BackgroundApplicationListModel::OnExtensionUnloaded(
404 const Extension
* extension
) {
405 if (!IsBackgroundApp(*extension
, profile_
))
408 DissociateApplicationData(extension
);
411 void BackgroundApplicationListModel::OnExtensionPermissionsUpdated(
412 const Extension
* extension
,
413 UpdatedExtensionPermissionsInfo::Reason reason
,
414 const PermissionSet
* permissions
) {
415 if (permissions
->HasAPIPermission(APIPermission::kBackground
)) {
417 case UpdatedExtensionPermissionsInfo::ADDED
:
418 DCHECK(IsBackgroundApp(*extension
, profile_
));
419 OnExtensionLoaded(extension
);
421 case UpdatedExtensionPermissionsInfo::REMOVED
:
422 DCHECK(!IsBackgroundApp(*extension
, profile_
));
424 DissociateApplicationData(extension
);
432 void BackgroundApplicationListModel::RemoveObserver(Observer
* observer
) {
433 observers_
.RemoveObserver(observer
);
436 // Update queries the extensions service of the profile with which the model was
437 // initialized to determine the current set of background applications. If that
438 // differs from the old list, it generates OnApplicationListChanged events for
440 void BackgroundApplicationListModel::Update() {
441 ExtensionService
* service
= extensions::ExtensionSystem::Get(profile_
)->
444 // Discover current background applications, compare with previous list, which
445 // is consistently sorted, and notify observers if they differ.
446 ExtensionList extensions
;
447 GetServiceApplications(service
, &extensions
);
448 ExtensionList::const_iterator old_cursor
= extensions_
.begin();
449 ExtensionList::const_iterator new_cursor
= extensions
.begin();
450 while (old_cursor
!= extensions_
.end() &&
451 new_cursor
!= extensions
.end() &&
452 (*old_cursor
)->name() == (*new_cursor
)->name() &&
453 (*old_cursor
)->id() == (*new_cursor
)->id()) {
457 if (old_cursor
!= extensions_
.end() || new_cursor
!= extensions
.end()) {
458 extensions_
= extensions
;
459 FOR_EACH_OBSERVER(Observer
, observers_
, OnApplicationListChanged(profile_
));