1 // Copyright (c) 2011 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_APPLICATION_LIST_MODEL_H_ 6 #define CHROME_BROWSER_BACKGROUND_APPLICATION_LIST_MODEL_H_ 7 #pragma once 8 9 #include <map> 10 #include <string> 11 12 #include "base/basictypes.h" 13 #include "base/observer_list.h" 14 #include "chrome/common/extensions/extension.h" 15 #include "content/common/notification_observer.h" 16 #include "content/common/notification_registrar.h" 17 18 class ExtensionService; 19 class PrefService; 20 class Profile; 21 22 // Model for list of Background Applications, that is, Extensions with 23 // kBackgroundPermission set, associated with a Profile. 24 class BackgroundApplicationListModel : public NotificationObserver { 25 public: 26 // Observer is informed of changes to the model. Users of the 27 // BackgroundApplicationListModel should anticipate that associated data, 28 // e. g. the Icon, may exist and yet not be immediately available. When the 29 // data becomes available, OnApplicationDataChanged will be invoked for all 30 // Observers of the model. 31 class Observer { 32 public: 33 // Invoked when data that the model associates with the extension, such as 34 // the Icon, has changed. 35 virtual void OnApplicationDataChanged(const Extension* extension); 36 37 // Invoked when the model detects a previously unknown extension and/or when 38 // it no longer detects a previously known extension. 39 virtual void OnApplicationListChanged(); 40 41 protected: 42 virtual ~Observer(); 43 }; 44 45 // Create a new model associated with profile. 46 explicit BackgroundApplicationListModel(Profile* profile); 47 48 ~BackgroundApplicationListModel(); 49 50 // Associate observer with this model. 51 void AddObserver(Observer* observer); 52 53 // Return the icon associated with |extension| or NULL. NULL indicates either 54 // that there is no icon associated with the extension, or that a pending 55 // task to retrieve the icon has not completed. See the Observer class above. 56 // 57 // NOTE: The model manages the SkBitmap result, that is it "owns" the memory, 58 // releasing it if the associated background application is unloaded. 59 // NOTE: All icons are currently sized as Extension::EXTENSION_ICON_BITTY. 60 const SkBitmap* GetIcon(const Extension* extension); 61 62 // Return the position of |extension| within this list model. 63 int GetPosition(const Extension* extension) const; 64 65 // Return the extension at the specified |position| in this list model. 66 const Extension* GetExtension(int position) const; 67 68 // Returns true if the passed extension is a background app. 69 static bool IsBackgroundApp(const Extension& extension); 70 71 // Dissociate observer from this model. 72 void RemoveObserver(Observer* observer); 73 74 ExtensionList::const_iterator begin() const { 75 return extensions_.begin(); 76 } 77 78 ExtensionList::const_iterator end() const { 79 return extensions_.end(); 80 } 81 82 size_t size() const { 83 return extensions_.size(); 84 } 85 86 private: 87 // Contains data associated with a background application that is not 88 // represented by the Extension class. 89 class Application; 90 91 // Associates extension id strings with Application objects. 92 typedef std::map<std::string, Application*> ApplicationMap; 93 94 // Identifies and caches data related to the extension. 95 void AssociateApplicationData(const Extension* extension); 96 97 // Clears cached data related to |extension|. 98 void DissociateApplicationData(const Extension* extension); 99 100 // Returns the Application associated with |extension| or NULL. 101 const Application* FindApplication(const Extension* extension) const; 102 103 // Returns the Application associated with |extension| or NULL. 104 Application* FindApplication(const Extension* extension); 105 106 // NotificationObserver implementation. 107 virtual void Observe(NotificationType type, 108 const NotificationSource& source, 109 const NotificationDetails& details); 110 111 // Notifies observers that some of the data associated with this background 112 // application, e. g. the Icon, has changed. 113 void OnApplicationDataChanged(const Extension* extension); 114 115 // Notifies observers that at least one background application has been added 116 // or removed. 117 void OnApplicationListChanged(); 118 119 // Invoked by Observe for EXTENSION_LOADED notifications. 120 void OnExtensionLoaded(Extension* extension); 121 122 // Invoked by Observe for EXTENSION_UNLOADED notifications. 123 void OnExtensionUnloaded(const Extension* extension); 124 125 // Refresh the list of background applications and generate notifications. 126 void Update(); 127 128 ApplicationMap applications_; 129 ExtensionList extensions_; 130 ObserverList<Observer> observers_; 131 Profile* profile_; 132 NotificationRegistrar registrar_; 133 134 DISALLOW_COPY_AND_ASSIGN(BackgroundApplicationListModel); 135 }; 136 137 #endif // CHROME_BROWSER_BACKGROUND_APPLICATION_LIST_MODEL_H_ 138