381354aebf4f9362249dfbd2c858d21dee633f2f
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / media_galleries_private / gallery_watch_state_tracker.h
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(const Extension* extension) OVERRIDE;
94   virtual void OnExtensionUnloaded(const Extension* extension) OVERRIDE;
95
96   // Syncs media gallery watch data for the given extension to/from the state
97   // storage.
98   void WriteToStorage(const std::string& extension_id);
99   void ReadFromStorage(const std::string& extension_id,
100                        scoped_ptr<base::Value> value);
101
102   // Sets up the gallery watcher on the receipt of granted gallery permission
103   // event.
104   void SetupGalleryWatch(const std::string& extension_id,
105                          MediaGalleryPrefId gallery_id,
106                          MediaGalleriesPreferences* preferences);
107
108   // Removes the gallery watcher on the receipt of revoked gallery permission
109   // event.
110   void RemoveGalleryWatch(const std::string& extension_id,
111                           MediaGalleryPrefId gallery_id,
112                           MediaGalleriesPreferences* preferences);
113
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);
119
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,
125                                        bool success);
126
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);
133
134   // Current profile.
135   Profile* profile_;
136
137   ScopedObserver<ExtensionRegistry,
138                  ExtensionRegistryObserver> scoped_extension_registry_observer_;
139
140   // A map of watched gallery details, per extension.
141   WatchedExtensionsMap watched_extensions_map_;
142
143   DISALLOW_COPY_AND_ASSIGN(GalleryWatchStateTracker);
144 };
145
146 }  // namespace extensions
147
148 #endif  // CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_PRIVATE_GALLERY_WATCH_STATE_TRACKER_H_