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 #ifndef CHROME_BROWSER_BACKGROUND_BACKGROUND_APPLICATION_LIST_MODEL_H_
6 #define CHROME_BROWSER_BACKGROUND_BACKGROUND_APPLICATION_LIST_MODEL_H_
11 #include "base/basictypes.h"
12 #include "base/observer_list.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h"
15 #include "extensions/common/extension.h"
23 // Model for list of Background Applications associated with a Profile (i.e.
24 // extensions with kBackgroundPermission set, or hosted apps with a
25 // BackgroundContents).
26 class BackgroundApplicationListModel
: public content::NotificationObserver
{
28 // Observer is informed of changes to the model. Users of the
29 // BackgroundApplicationListModel should anticipate that associated data,
30 // e. g. the Icon, may exist and yet not be immediately available. When the
31 // data becomes available, OnApplicationDataChanged will be invoked for all
32 // Observers of the model.
35 // Invoked when data that the model associates with the extension, such as
36 // the Icon, has changed.
37 virtual void OnApplicationDataChanged(
38 const extensions::Extension
* extension
,
41 // Invoked when the model detects a previously unknown extension and/or when
42 // it no longer detects a previously known extension.
43 virtual void OnApplicationListChanged(Profile
* profile
);
49 // Create a new model associated with profile.
50 explicit BackgroundApplicationListModel(Profile
* profile
);
52 virtual ~BackgroundApplicationListModel();
54 // Associate observer with this model.
55 void AddObserver(Observer
* observer
);
57 // Return the icon associated with |extension| or NULL. NULL indicates either
58 // that there is no icon associated with the extension, or that a pending
59 // task to retrieve the icon has not completed. See the Observer class above.
61 // NOTE: The model manages the ImageSkia result, that is it "owns" the memory,
62 // releasing it if the associated background application is unloaded.
63 // NOTE: All icons are currently sized as
64 // ExtensionIconSet::EXTENSION_ICON_BITTY.
65 const gfx::ImageSkia
* GetIcon(const extensions::Extension
* extension
);
67 // Return the position of |extension| within this list model.
68 int GetPosition(const extensions::Extension
* extension
) const;
70 // Return the extension at the specified |position| in this list model.
71 const extensions::Extension
* GetExtension(int position
) const;
73 // Returns true if the passed extension is a background app.
74 static bool IsBackgroundApp(const extensions::Extension
& extension
,
77 // Dissociate observer from this model.
78 void RemoveObserver(Observer
* observer
);
80 extensions::ExtensionList::const_iterator
begin() const {
81 return extensions_
.begin();
84 extensions::ExtensionList::const_iterator
end() const {
85 return extensions_
.end();
89 return extensions_
.size();
93 // Contains data associated with a background application that is not
94 // represented by the Extension class.
97 // Associates extension id strings with Application objects.
98 typedef std::map
<std::string
, Application
*> ApplicationMap
;
100 // Identifies and caches data related to the extension.
101 void AssociateApplicationData(const extensions::Extension
* extension
);
103 // Clears cached data related to |extension|.
104 void DissociateApplicationData(const extensions::Extension
* extension
);
106 // Returns the Application associated with |extension| or NULL.
107 const Application
* FindApplication(
108 const extensions::Extension
* extension
) const;
110 // Returns the Application associated with |extension| or NULL.
111 Application
* FindApplication(const extensions::Extension
* extension
);
113 // content::NotificationObserver implementation.
114 virtual void Observe(int type
,
115 const content::NotificationSource
& source
,
116 const content::NotificationDetails
& details
) OVERRIDE
;
118 // Notifies observers that some of the data associated with this background
119 // application, e. g. the Icon, has changed.
120 void SendApplicationDataChangedNotifications(
121 const extensions::Extension
* extension
);
123 // Notifies observers that at least one background application has been added
125 void SendApplicationListChangedNotifications();
127 // Invoked by Observe for NOTIFICATION_EXTENSION_LOADED.
128 void OnExtensionLoaded(const extensions::Extension
* extension
);
130 // Invoked by Observe for NOTIFICATION_EXTENSION_UNLOADED.
131 void OnExtensionUnloaded(const extensions::Extension
* extension
);
133 // Invoked by Observe for NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED.
134 void OnExtensionPermissionsUpdated(
135 const extensions::Extension
* extension
,
136 extensions::UpdatedExtensionPermissionsInfo::Reason reason
,
137 const extensions::PermissionSet
* permissions
);
139 // Refresh the list of background applications and generate notifications.
142 // Determines if the given extension has to be considered a "background app"
143 // due to its use of PushMessaging. Normally every extension that expectes
144 // push messages is classified as "background app", however there are some
145 // rare exceptions, so this function implements a whitelist.
146 static bool RequiresBackgroundModeForPushMessaging(
147 const extensions::Extension
& extension
);
149 ApplicationMap applications_
;
150 extensions::ExtensionList extensions_
;
151 ObserverList
<Observer
> observers_
;
153 content::NotificationRegistrar registrar_
;
155 DISALLOW_COPY_AND_ASSIGN(BackgroundApplicationListModel
);
158 #endif // CHROME_BROWSER_BACKGROUND_BACKGROUND_APPLICATION_LIST_MODEL_H_