Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / media_galleries / gallery_watch_manager.h
bloba87a2e2ae8afd8025bc2ca067f119e93c901efb7
1 // Copyright 2014 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 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_GALLERY_WATCH_MANAGER_H_
6 #define CHROME_BROWSER_MEDIA_GALLERIES_GALLERY_WATCH_MANAGER_H_
8 #include <map>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/callback_forward.h"
13 #include "base/containers/scoped_ptr_map.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_path_watcher.h"
16 #include "base/memory/linked_ptr.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/time/time.h"
20 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
21 #include "components/keyed_service/core/keyed_service_shutdown_notifier.h"
22 #include "components/storage_monitor/removable_storage_observer.h"
24 class GalleryWatchManagerObserver;
26 namespace content {
27 class BrowserContext;
30 namespace extensions {
31 class Extension;
34 // The GalleryWatchManager is owned by MediaFileSystemRegistry, which is global.
35 // This class manages all watches on media galleries, regardless of profile.
36 // It tracks outstanding watch requests and creates one FilePathWatcher per
37 // watched directory. This class lives and is called on the UI thread.
38 class GalleryWatchManager
39 : public MediaGalleriesPreferences::GalleryChangeObserver,
40 public storage_monitor::RemovableStorageObserver {
41 public:
42 // On success, |error| is empty.
43 typedef base::Callback<void(const std::string& /* error */)> ResultCallback;
45 static const char kInvalidGalleryIDError[];
46 static const char kNoPermissionError[];
47 static const char kCouldNotWatchGalleryError[];
49 GalleryWatchManager();
50 ~GalleryWatchManager() override;
52 // Add or remove observer of change events - this is the only way to
53 // get the result of the file watches. There can only be one observer per
54 // browser context.
55 void AddObserver(content::BrowserContext* browser_context,
56 GalleryWatchManagerObserver* observer);
57 void RemoveObserver(content::BrowserContext* browser_context);
59 // Must be called when |browser_context| is shut down.
60 void ShutdownBrowserContext(content::BrowserContext* browser_context);
62 // Add a watch for |gallery_id|.
63 void AddWatch(content::BrowserContext* browser_context,
64 const extensions::Extension* extension,
65 MediaGalleryPrefId gallery_id,
66 const ResultCallback& callback);
68 // Remove the watch for |gallery_id|. It is valid to call this method on
69 // non-existent watches.
70 void RemoveWatch(content::BrowserContext* browser_context,
71 const std::string& extension_id,
72 MediaGalleryPrefId gallery_id);
74 // Remove all the watches for |extension_id|.
75 void RemoveAllWatches(content::BrowserContext* browser_context,
76 const std::string& extension_id);
78 // Return the set of galleries being watched for |extension_id|.
79 MediaGalleryPrefIdSet GetWatchSet(content::BrowserContext* browser_context,
80 const std::string& extension_id);
82 private:
83 class FileWatchManager;
85 // Used to track the gallery watches connected to a specific path.
86 struct WatchOwner {
87 WatchOwner(content::BrowserContext* browser_context,
88 const std::string& extension_id,
89 MediaGalleryPrefId gallery_id);
91 content::BrowserContext* browser_context;
92 const std::string extension_id;
93 MediaGalleryPrefId gallery_id;
95 // Needed to support storage in STL set, as well as usage as map key.
96 bool operator<(const WatchOwner& other) const;
99 struct NotificationInfo {
100 NotificationInfo();
101 ~NotificationInfo();
103 std::set<WatchOwner> owners;
104 base::Time last_notify_time;
105 bool delayed_notification_pending;
108 typedef std::map<WatchOwner, base::FilePath> WatchesMap;
109 typedef std::map<base::FilePath, NotificationInfo> WatchedPaths;
110 typedef std::map<content::BrowserContext*, GalleryWatchManagerObserver*>
111 ObserverMap;
112 typedef base::ScopedPtrMap<
113 content::BrowserContext*,
114 scoped_ptr<KeyedServiceShutdownNotifier::Subscription>>
115 BrowserContextSubscriptionMap;
117 // Ensure there is a subscription to shutdown notifications for
118 // |browser_context|.
119 void EnsureBrowserContextSubscription(
120 content::BrowserContext* browser_context);
122 // Stop the FilePathWatcher for |path|. Updates |watched_paths_| but not
123 // |registered_watches_|.
124 void DeactivateFileWatch(const WatchOwner& owner, const base::FilePath& path);
126 // Called by FilePathWatcher on the UI thread to respond to a request to
127 // watch the path.
128 void OnFileWatchActivated(const WatchOwner& owner,
129 const base::FilePath& path,
130 const ResultCallback& callback,
131 bool success);
133 // Called by FilePathWatcher on the UI thread on a change event for |path|.
134 void OnFilePathChanged(const base::FilePath& path, bool error);
136 // MediaGalleriesPreferences::GalleryChangeObserver implementation.
137 void OnPermissionRemoved(MediaGalleriesPreferences* pref,
138 const std::string& extension_id,
139 MediaGalleryPrefId pref_id) override;
140 void OnGalleryRemoved(MediaGalleriesPreferences* pref,
141 MediaGalleryPrefId pref_id) override;
143 // storage_monitor::RemovableStorageObserver implementation.
144 void OnRemovableStorageDetached(
145 const storage_monitor::StorageInfo& info) override;
147 // True if the we are already observing the storage monitor.
148 bool storage_monitor_observed_;
150 // MediaGalleriesPreferences we are currently observing.
151 std::set<MediaGalleriesPreferences*> observed_preferences_;
153 // All registered watches, keyed by WatchOwner.
154 WatchesMap watches_;
156 // Reverse mapping of watched paths to the set of owning WatchOwners.
157 WatchedPaths watched_paths_;
159 // Things that want to hear about gallery changes.
160 ObserverMap observers_;
162 // Helper that does the watches on the FILE thread.
163 scoped_ptr<FileWatchManager> watch_manager_;
165 // Removes watches when a browser context is shut down as watches contain raw
166 // pointers.
167 BrowserContextSubscriptionMap browser_context_subscription_map_;
169 base::WeakPtrFactory<GalleryWatchManager> weak_factory_;
171 DISALLOW_COPY_AND_ASSIGN(GalleryWatchManager);
174 #endif // CHROME_BROWSER_MEDIA_GALLERIES_GALLERY_WATCH_MANAGER_H_