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 // GalleryWatchStateTracker tracks the gallery watchers, updates the
6 // extension state storage and observes the extension registry events.
7 // GalleryWatchStateTracker lives on the UI thread.
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_
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"
28 namespace extensions {
30 class ExtensionRegistry;
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 {
39 explicit GalleryWatchStateTracker(Profile* profile);
40 virtual ~GalleryWatchStateTracker();
42 // Returns the GalleryWatchStateTracker associated with the |profile|.
43 // Returns NULL if GalleryWatchStateTracker does not exists.
44 static GalleryWatchStateTracker* GetForProfile(Profile* profile);
46 // Updates the storage for the given extension on the receipt of gallery
48 void OnGalleryWatchAdded(const std::string& extension_id,
49 MediaGalleryPrefId gallery_id);
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);
56 // Updates the state of the gallery watchers on the receipt of access
57 // permission changed event for the extension specified by the
59 virtual void OnPermissionAdded(MediaGalleriesPreferences* pref,
60 const std::string& extension_id,
61 MediaGalleryPrefId gallery_id) OVERRIDE;
63 virtual void OnPermissionRemoved(MediaGalleriesPreferences* pref,
64 const std::string& extension_id,
65 MediaGalleryPrefId gallery_id) OVERRIDE;
67 virtual void OnGalleryRemoved(MediaGalleriesPreferences* pref,
68 MediaGalleryPrefId gallery_id) OVERRIDE;
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;
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);
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;
88 // Key: Extension identifier.
89 // Value: Map of watched gallery information.
90 typedef std::map<std::string, WatchedGalleriesMap> WatchedExtensionsMap;
92 // extensions::ExtensionRegistryObserver implementation.
93 virtual void OnExtensionLoaded(const Extension* extension) OVERRIDE;
94 virtual void OnExtensionUnloaded(const Extension* extension) OVERRIDE;
96 // Syncs media gallery watch data for the given extension to/from the state
98 void WriteToStorage(const std::string& extension_id);
99 void ReadFromStorage(const std::string& extension_id,
100 scoped_ptr<base::Value> value);
102 // Sets up the gallery watcher on the receipt of granted gallery permission
104 void SetupGalleryWatch(const std::string& extension_id,
105 MediaGalleryPrefId gallery_id,
106 MediaGalleriesPreferences* preferences);
108 // Removes the gallery watcher on the receipt of revoked gallery permission
110 void RemoveGalleryWatch(const std::string& extension_id,
111 MediaGalleryPrefId gallery_id,
112 MediaGalleriesPreferences* preferences);
114 // Returns true if a gallery watcher exists for the extension.
115 // Set |has_active_watcher| to true to find if the gallery watcher is active.
116 bool HasGalleryWatchInfo(const std::string& extension_id,
117 MediaGalleryPrefId gallery_id,
118 bool has_active_watcher);
120 // Handles the setup gallery watch request response. When an extension is
121 // loaded, GalleryWatchStateTracker reads the storage and sends request to
122 // setup gallery watchers for the known watch paths.
123 void HandleSetupGalleryWatchResponse(const std::string& extension_id,
124 MediaGalleryPrefId gallery_id,
127 // Adds the watched |gallery_id| in |watched_extensions_map_| for the
128 // extension specified by the |extension_id|. Returns true if the |gallery_id|
129 // is added into the |watched_extensions_map_| and returns false if the
130 // |gallery_id| already exists in the |watched_extensions_map_|.
131 bool AddWatchedGalleryIdInfoForExtension(const std::string& extension_id,
132 MediaGalleryPrefId gallery_id);
137 ScopedObserver<ExtensionRegistry,
138 ExtensionRegistryObserver> scoped_extension_registry_observer_;
140 // A map of watched gallery details, per extension.
141 WatchedExtensionsMap watched_extensions_map_;
143 DISALLOW_COPY_AND_ASSIGN(GalleryWatchStateTracker);
146 } // namespace extensions
148 #endif // CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_PRIVATE_GALLERY_WATCH_STATE_TRACKER_H_