Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 // Manages all the gallery file watchers for the associated profile. This | |
| 6 // is temporary and will be moved to a permanent, public place in the near | |
| 7 // future. | |
| 8 | |
| 9 #ifndef CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_PRIVATE_GALLERY_WATCH_MANA GER_H_ | |
| 10 #define CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_PRIVATE_GALLERY_WATCH_MANA GER_H_ | |
| 11 | |
| 12 #include <map> | |
| 13 #include <set> | |
| 14 #include <string> | |
| 15 | |
| 16 #include "base/file_path.h" | |
| 17 #include "base/files/file_path_watcher.h" | |
| 18 #include "base/memory/scoped_ptr.h" | |
| 19 #include "base/memory/weak_ptr.h" | |
| 20 #include "base/threading/thread_checker.h" | |
| 21 #include "base/time.h" | |
| 22 | |
| 23 class Profile; | |
| 24 | |
| 25 namespace extensions { | |
| 26 | |
| 27 // The profile-keyed service that manages the gallery watchers. This class is | |
| 28 // constructed, destructed and operated on the FILE thread. | |
|
Lei Zhang
2012/12/15 01:11:54
You can just say "this class lives on the foo thre
kmadhusu
2012/12/17 23:58:05
Done.
| |
| 29 class GalleryWatchManager { | |
| 30 public: | |
| 31 typedef std::set<std::string> ExtensionIdSet; | |
|
Lei Zhang
2012/12/15 01:11:54
This seems to be used in 1 place inside the .cc fi
kmadhusu
2012/12/17 23:58:05
Removed typedef.
| |
| 32 | |
| 33 explicit GalleryWatchManager(const Profile* profile); | |
| 34 ~GalleryWatchManager(); | |
| 35 | |
| 36 // Initiates a gallery watch operation for the extension specified by | |
| 37 // the |extension_id|. |gallery_id| specifies the gallery identifier and | |
| 38 // |watch_path| specifies the absolute path of the gallery. Returns true, | |
| 39 // if the watch was set successfully. | |
| 40 bool StartGalleryWatch(const std::string& gallery_id, | |
| 41 const FilePath& watch_path, | |
| 42 const std::string& extension_id); | |
| 43 | |
| 44 // Cancels the gallery watch operation for the extension specified by the | |
| 45 // |extension_id|. |watch_path| specifies the gallery absolute path. | |
| 46 void StopGalleryWatch(const FilePath& watch_path, | |
| 47 const std::string& extension_id); | |
| 48 | |
| 49 // Handles the extension unloaded/uninstalled/destroyed event. | |
| 50 void OnExtensionDestroyed(const std::string& extension_id); | |
| 51 | |
| 52 private: | |
| 53 typedef std::map<std::string, int> ExtensionUsageRegistry; | |
|
Lei Zhang
2012/12/15 04:13:21
This map is too wimpy to a registry.
kmadhusu
2012/12/17 23:58:05
ExtensionUsageRegistry => ExtensionWatchCountMap
| |
| 54 | |
| 55 class GalleryFilePathWatcher; | |
| 56 typedef std::map<FilePath, GalleryFilePathWatcher*> WatcherMap; | |
| 57 | |
| 58 // Gallery file path watcher delegate to handle the gallery change | |
|
Lei Zhang
2012/12/15 04:13:21
You should mention this class does recursive watch
kmadhusu
2012/12/17 23:58:05
Done.
| |
| 59 // notifications. This class manages all the extension usage and forwards | |
| 60 // the gallery change notifications to the extensions. | |
| 61 // This class is constructed, destructed and operated on the FILE thread. | |
| 62 // This class is instantiated per gallery watch path. | |
| 63 class GalleryFilePathWatcher { | |
|
Lei Zhang
2012/12/15 04:13:21
Can this go in the .cc file instead?
kmadhusu
2012/12/17 23:58:05
Done.
| |
| 64 public: | |
| 65 GalleryFilePathWatcher(const Profile* profile, | |
| 66 const std::string& gallery_id, | |
| 67 const FilePath& path, | |
| 68 const std::string& extension_id); | |
| 69 | |
| 70 ~GalleryFilePathWatcher(); | |
| 71 | |
| 72 // Adds the extension specified by the |extension_id| to the | |
| 73 // ExtensionUsageRegistry and initiate the watch operation. | |
| 74 void AddExtension(const std::string& extension_id); | |
| 75 | |
| 76 // Cancels the watch for the extension specified by the |extension_id|. | |
| 77 void RemoveExtension(const std::string& extension_id); | |
| 78 | |
| 79 // Handles the extension unloaded/uninstalled/destroyed event. | |
| 80 void OnExtensionDestroyed(const std::string& extension_id); | |
| 81 | |
| 82 // Returns the total number of watchers for the |gallery_path_|. | |
| 83 unsigned int GetRefCount() const; | |
| 84 | |
| 85 // Sets up the watch operation for the specified |gallery_path_|. On | |
| 86 // success, returns true. | |
| 87 bool SetupWatch(); | |
| 88 | |
| 89 private: | |
| 90 // FilePathWatcher callback. | |
| 91 void OnFilePathChanged(const FilePath& path, bool error); | |
| 92 | |
| 93 // Current profile. | |
| 94 const Profile* profile_; | |
| 95 | |
| 96 // The gallery identifier, e.g "1". | |
| 97 const std::string gallery_id_; | |
| 98 | |
| 99 // The gallery file path watcher. | |
| 100 scoped_ptr<base::files::FilePathWatcher> file_watcher_; | |
|
Lei Zhang
2012/12/15 04:13:21
Does this need to be a scoped_ptr?
kmadhusu
2012/12/17 23:58:05
Not needed.
| |
| 101 | |
| 102 // The gallery file path, e.g "C:/My Pictures". | |
|
Lei Zhang
2012/12/15 04:13:21
nit: C:\, not C:/
kmadhusu
2012/12/17 23:58:05
Done.
| |
| 103 FilePath gallery_path_; | |
| 104 | |
| 105 // Map to keep track of the extension and its corresponding watch count. | |
| 106 // Key: Extension identifier, e.g "qoueruoweuroiwueroiwujkshdf". | |
| 107 // Value: Number of watchers in this extension, e.g "3". | |
| 108 ExtensionUsageRegistry extensions_; | |
|
Lei Zhang
2012/12/15 04:13:21
|extensions_watch_count_map_| ?
kmadhusu
2012/12/17 23:58:05
Done.
| |
| 109 | |
| 110 // Total number of watchers. | |
| 111 unsigned int ref_count_; | |
|
Lei Zhang
2012/12/15 04:13:21
Do you have to manually refcount?
kmadhusu
2012/12/17 23:58:05
Fixed. GalleryFilePathWatch inherits from RefCount
| |
| 112 | |
| 113 // Used to manage the gallery changed events. | |
| 114 base::Time last_gallery_changed_event_; | |
|
Lei Zhang
2012/12/15 04:13:21
This should be per-extension.
Extension AA has a
kmadhusu
2012/12/17 23:58:05
Done.
| |
| 115 | |
| 116 // Make sure all the member functions are called on the right thread. | |
| 117 base::ThreadChecker thread_check_; | |
| 118 | |
| 119 // Used to provide a weak pointer to FilePathWatcher callback. | |
| 120 base::WeakPtrFactory<GalleryFilePathWatcher> weak_ptr_factory_; | |
| 121 | |
| 122 DISALLOW_COPY_AND_ASSIGN(GalleryFilePathWatcher); | |
| 123 }; | |
| 124 | |
| 125 // Current profile. | |
| 126 const Profile* profile_; | |
| 127 | |
| 128 // Map to manage the gallery file path watchers. | |
| 129 // Key: Gallery watch path. | |
| 130 // Value: GalleryFilePathWatcher*. | |
| 131 WatcherMap gallery_watchers_; | |
| 132 | |
| 133 // Make sure all the member functions are called on the right thread. | |
| 134 base::ThreadChecker thread_checker_; | |
| 135 | |
| 136 DISALLOW_COPY_AND_ASSIGN(GalleryWatchManager); | |
| 137 }; | |
| 138 | |
| 139 } // namespace extensions | |
| 140 | |
| 141 #endif // CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_PRIVATE_GALLERY_WATCH_M ANAGER_H_ | |
| OLD | NEW |