Home | History | Annotate | Download | only in media_galleries_private
      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 // GalleryWatchStateTracker tracks the gallery watchers, updates the
      6 // extension state storage and observes the extension registry events.
      7 // GalleryWatchStateTracker lives on the UI thread.
      8 
      9 #ifndef CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_PRIVATE_GALLERY_WATCH_STATE_TRACKER_H_
     10 #define CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_PRIVATE_GALLERY_WATCH_STATE_TRACKER_H_
     11 
     12 #include <map>
     13 #include <set>
     14 #include <string>
     15 
     16 #include "base/memory/scoped_ptr.h"
     17 #include "base/memory/weak_ptr.h"
     18 #include "base/scoped_observer.h"
     19 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
     20 #include "extensions/browser/extension_registry_observer.h"
     21 
     22 class Profile;
     23 
     24 namespace base {
     25 class Value;
     26 }
     27 
     28 namespace extensions {
     29 
     30 class ExtensionRegistry;
     31 
     32 // This class is owned by the MediaGalleriesPrivateAPI, and is created on demand
     33 // along with the MediaGalleriesPrivateEventRouter.
     34 class GalleryWatchStateTracker
     35     : public extensions::ExtensionRegistryObserver,
     36       public base::SupportsWeakPtr<GalleryWatchStateTracker>,
     37       public MediaGalleriesPreferences::GalleryChangeObserver {
     38  public:
     39   explicit GalleryWatchStateTracker(Profile* profile);
     40   virtual ~GalleryWatchStateTracker();
     41 
     42   // Returns the GalleryWatchStateTracker associated with the |profile|.
     43   // Returns NULL if GalleryWatchStateTracker does not exists.
     44   static GalleryWatchStateTracker* GetForProfile(Profile* profile);
     45 
     46   // Updates the storage for the given extension on the receipt of gallery
     47   // watch added event.
     48   void OnGalleryWatchAdded(const std::string& extension_id,
     49                            MediaGalleryPrefId gallery_id);
     50 
     51   // Updates the storage for the given extension on the receipt of gallery
     52   // watch removed event.
     53   void OnGalleryWatchRemoved(const std::string& extension_id,
     54                              MediaGalleryPrefId gallery_id);
     55 
     56   // Updates the state of the gallery watchers on the receipt of access
     57   // permission changed event for the extension specified by the
     58   // |extension_id|.
     59   virtual void OnPermissionAdded(MediaGalleriesPreferences* pref,
     60                                  const std::string& extension_id,
     61                                  MediaGalleryPrefId gallery_id) OVERRIDE;
     62 
     63   virtual void OnPermissionRemoved(MediaGalleriesPreferences* pref,
     64                                    const std::string& extension_id,
     65                                    MediaGalleryPrefId gallery_id) OVERRIDE;
     66 
     67   virtual void OnGalleryRemoved(MediaGalleriesPreferences* pref,
     68                                 MediaGalleryPrefId gallery_id) OVERRIDE;
     69 
     70   // Returns a set of watched gallery identifiers for the extension specified
     71   // by the |extension_id|.
     72   MediaGalleryPrefIdSet GetAllWatchedGalleryIDsForExtension(
     73       const std::string& extension_id) const;
     74 
     75   // Removes all the gallery watchers associated with the extension specified
     76   // by the |extension_id|.
     77   void RemoveAllGalleryWatchersForExtension(
     78       const std::string& extension_id,
     79       MediaGalleriesPreferences* preferences);
     80 
     81  private:
     82   // Key: Gallery identifier.
     83   // Value: True if the watcher is active. Watcher will be active only if
     84   // the extension has access permission to the watched gallery and a watcher
     85   // has been successfully setup.
     86   typedef std::map<MediaGalleryPrefId, bool> WatchedGalleriesMap;
     87 
     88   // Key: Extension identifier.
     89   // Value: Map of watched gallery information.
     90   typedef std::map<std::string, WatchedGalleriesMap> WatchedExtensionsMap;
     91 
     92   // extensions::ExtensionRegistryObserver implementation.
     93   virtual void OnExtensionLoaded(content::BrowserContext* browser_context,
     94                                  const Extension* extension) OVERRIDE;
     95   virtual void OnExtensionUnloaded(content::BrowserContext* browser_context,
     96                                    const Extension* extension,
     97                                    UnloadedExtensionInfo::Reason reason)
     98       OVERRIDE;
     99 
    100   // Syncs media gallery watch data for the given extension to/from the state
    101   // storage.
    102   void WriteToStorage(const std::string& extension_id);
    103   void ReadFromStorage(const std::string& extension_id,
    104                        scoped_ptr<base::Value> value);
    105 
    106   // Sets up the gallery watcher on the receipt of granted gallery permission
    107   // event.
    108   void SetupGalleryWatch(const std::string& extension_id,
    109                          MediaGalleryPrefId gallery_id,
    110                          MediaGalleriesPreferences* preferences);
    111 
    112   // Removes the gallery watcher on the receipt of revoked gallery permission
    113   // event.
    114   void RemoveGalleryWatch(const std::string& extension_id,
    115                           MediaGalleryPrefId gallery_id,
    116                           MediaGalleriesPreferences* preferences);
    117 
    118   // Returns true if a gallery watcher exists for the extension.
    119   // Set |has_active_watcher| to true to find if the gallery watcher is active.
    120   bool HasGalleryWatchInfo(const std::string& extension_id,
    121                            MediaGalleryPrefId gallery_id,
    122                            bool has_active_watcher);
    123 
    124   // Handles the setup gallery watch request response. When an extension is
    125   // loaded, GalleryWatchStateTracker reads the storage and sends request to
    126   // setup gallery watchers for the known watch paths.
    127   void HandleSetupGalleryWatchResponse(const std::string& extension_id,
    128                                        MediaGalleryPrefId gallery_id,
    129                                        bool success);
    130 
    131   // Adds the watched |gallery_id| in |watched_extensions_map_| for the
    132   // extension specified by the |extension_id|. Returns true if the |gallery_id|
    133   // is added into the |watched_extensions_map_| and returns false if the
    134   // |gallery_id| already exists in the |watched_extensions_map_|.
    135   bool AddWatchedGalleryIdInfoForExtension(const std::string& extension_id,
    136                                            MediaGalleryPrefId gallery_id);
    137 
    138   // Current profile.
    139   Profile* profile_;
    140 
    141   ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
    142       extension_registry_observer_;
    143 
    144   // A map of watched gallery details, per extension.
    145   WatchedExtensionsMap watched_extensions_map_;
    146 
    147   DISALLOW_COPY_AND_ASSIGN(GalleryWatchStateTracker);
    148 };
    149 
    150 }  // namespace extensions
    151 
    152 #endif  // CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_PRIVATE_GALLERY_WATCH_STATE_TRACKER_H_
    153