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 ~BackgroundApplicationListModel() override
;
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();
92 // Returns true if all startup notifications have already been issued.
93 bool is_ready() const {
98 // Contains data associated with a background application that is not
99 // represented by the Extension class.
102 // Associates extension id strings with Application objects.
103 typedef std::map
<std::string
, Application
*> ApplicationMap
;
105 // Identifies and caches data related to the extension.
106 void AssociateApplicationData(const extensions::Extension
* extension
);
108 // Clears cached data related to |extension|.
109 void DissociateApplicationData(const extensions::Extension
* extension
);
111 // Returns the Application associated with |extension| or NULL.
112 const Application
* FindApplication(
113 const extensions::Extension
* extension
) const;
115 // Returns the Application associated with |extension| or NULL.
116 Application
* FindApplication(const extensions::Extension
* extension
);
118 // content::NotificationObserver implementation.
119 void Observe(int type
,
120 const content::NotificationSource
& source
,
121 const content::NotificationDetails
& details
) override
;
123 // Notifies observers that some of the data associated with this background
124 // application, e. g. the Icon, has changed.
125 void SendApplicationDataChangedNotifications(
126 const extensions::Extension
* extension
);
128 // Notifies observers that at least one background application has been added
130 void SendApplicationListChangedNotifications();
132 // Invoked by Observe for NOTIFICATION_EXTENSION_LOADED_DEPRECATED.
133 void OnExtensionLoaded(const extensions::Extension
* extension
);
135 // Invoked by Observe for NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED.
136 void OnExtensionUnloaded(const extensions::Extension
* extension
);
138 // Invoked by Observe for NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED.
139 void OnExtensionPermissionsUpdated(
140 const extensions::Extension
* extension
,
141 extensions::UpdatedExtensionPermissionsInfo::Reason reason
,
142 const extensions::PermissionSet
* permissions
);
144 // Refresh the list of background applications and generate notifications.
147 ApplicationMap applications_
;
148 extensions::ExtensionList extensions_
;
149 ObserverList
<Observer
, true> observers_
;
151 content::NotificationRegistrar registrar_
;
154 DISALLOW_COPY_AND_ASSIGN(BackgroundApplicationListModel
);
157 #endif // CHROME_BROWSER_BACKGROUND_BACKGROUND_APPLICATION_LIST_MODEL_H_