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. 4 5#ifndef CHROME_BROWSER_BACKGROUND_BACKGROUND_APPLICATION_LIST_MODEL_H_ 6#define CHROME_BROWSER_BACKGROUND_BACKGROUND_APPLICATION_LIST_MODEL_H_ 7 8#include <map> 9#include <string> 10 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" 16 17class Profile; 18 19namespace gfx { 20class ImageSkia; 21} 22 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). 26class BackgroundApplicationListModel : public content::NotificationObserver { 27 public: 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. 33 class Observer { 34 public: 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, 39 Profile* profile); 40 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); 44 45 protected: 46 virtual ~Observer(); 47 }; 48 49 // Create a new model associated with profile. 50 explicit BackgroundApplicationListModel(Profile* profile); 51 52 virtual ~BackgroundApplicationListModel(); 53 54 // Associate observer with this model. 55 void AddObserver(Observer* observer); 56 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. 60 // 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); 66 67 // Return the position of |extension| within this list model. 68 int GetPosition(const extensions::Extension* extension) const; 69 70 // Return the extension at the specified |position| in this list model. 71 const extensions::Extension* GetExtension(int position) const; 72 73 // Returns true if the passed extension is a background app. 74 static bool IsBackgroundApp(const extensions::Extension& extension, 75 Profile* profile); 76 77 // Dissociate observer from this model. 78 void RemoveObserver(Observer* observer); 79 80 extensions::ExtensionList::const_iterator begin() const { 81 return extensions_.begin(); 82 } 83 84 extensions::ExtensionList::const_iterator end() const { 85 return extensions_.end(); 86 } 87 88 size_t size() const { 89 return extensions_.size(); 90 } 91 92 // Returns true if all startup notifications have already been issued. 93 bool is_ready() const { 94 return ready_; 95 } 96 97 private: 98 // Contains data associated with a background application that is not 99 // represented by the Extension class. 100 class Application; 101 102 // Associates extension id strings with Application objects. 103 typedef std::map<std::string, Application*> ApplicationMap; 104 105 // Identifies and caches data related to the extension. 106 void AssociateApplicationData(const extensions::Extension* extension); 107 108 // Clears cached data related to |extension|. 109 void DissociateApplicationData(const extensions::Extension* extension); 110 111 // Returns the Application associated with |extension| or NULL. 112 const Application* FindApplication( 113 const extensions::Extension* extension) const; 114 115 // Returns the Application associated with |extension| or NULL. 116 Application* FindApplication(const extensions::Extension* extension); 117 118 // content::NotificationObserver implementation. 119 virtual void Observe(int type, 120 const content::NotificationSource& source, 121 const content::NotificationDetails& details) OVERRIDE; 122 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); 127 128 // Notifies observers that at least one background application has been added 129 // or removed. 130 void SendApplicationListChangedNotifications(); 131 132 // Invoked by Observe for NOTIFICATION_EXTENSION_LOADED_DEPRECATED. 133 void OnExtensionLoaded(const extensions::Extension* extension); 134 135 // Invoked by Observe for NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED. 136 void OnExtensionUnloaded(const extensions::Extension* extension); 137 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); 143 144 // Refresh the list of background applications and generate notifications. 145 void Update(); 146 147 // Determines if the given extension has to be considered a "background app" 148 // due to its use of PushMessaging. Normally every extension that expectes 149 // push messages is classified as "background app", however there are some 150 // rare exceptions, so this function implements a whitelist. 151 static bool RequiresBackgroundModeForPushMessaging( 152 const extensions::Extension& extension); 153 154 ApplicationMap applications_; 155 extensions::ExtensionList extensions_; 156 ObserverList<Observer, true> observers_; 157 Profile* profile_; 158 content::NotificationRegistrar registrar_; 159 bool ready_; 160 161 DISALLOW_COPY_AND_ASSIGN(BackgroundApplicationListModel); 162}; 163 164#endif // CHROME_BROWSER_BACKGROUND_BACKGROUND_APPLICATION_LIST_MODEL_H_ 165