Home | History | Annotate | Download | only in background
      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 
     17 class Profile;
     18 
     19 namespace gfx {
     20 class 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).
     26 class 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  private:
     93   // Contains data associated with a background application that is not
     94   // represented by the Extension class.
     95   class Application;
     96 
     97   // Associates extension id strings with Application objects.
     98   typedef std::map<std::string, Application*> ApplicationMap;
     99 
    100   // Identifies and caches data related to the extension.
    101   void AssociateApplicationData(const extensions::Extension* extension);
    102 
    103   // Clears cached data related to |extension|.
    104   void DissociateApplicationData(const extensions::Extension* extension);
    105 
    106   // Returns the Application associated with |extension| or NULL.
    107   const Application* FindApplication(
    108       const extensions::Extension* extension) const;
    109 
    110   // Returns the Application associated with |extension| or NULL.
    111   Application* FindApplication(const extensions::Extension* extension);
    112 
    113   // content::NotificationObserver implementation.
    114   virtual void Observe(int type,
    115                        const content::NotificationSource& source,
    116                        const content::NotificationDetails& details) OVERRIDE;
    117 
    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);
    122 
    123   // Notifies observers that at least one background application has been added
    124   // or removed.
    125   void SendApplicationListChangedNotifications();
    126 
    127   // Invoked by Observe for NOTIFICATION_EXTENSION_LOADED_DEPRECATED.
    128   void OnExtensionLoaded(const extensions::Extension* extension);
    129 
    130   // Invoked by Observe for NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED.
    131   void OnExtensionUnloaded(const extensions::Extension* extension);
    132 
    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);
    138 
    139   // Refresh the list of background applications and generate notifications.
    140   void Update();
    141 
    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);
    148 
    149   ApplicationMap applications_;
    150   extensions::ExtensionList extensions_;
    151   ObserverList<Observer, true> observers_;
    152   Profile* profile_;
    153   content::NotificationRegistrar registrar_;
    154 
    155   DISALLOW_COPY_AND_ASSIGN(BackgroundApplicationListModel);
    156 };
    157 
    158 #endif  // CHROME_BROWSER_BACKGROUND_BACKGROUND_APPLICATION_LIST_MODEL_H_
    159