Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
|
satorux1
2013/08/06 05:19:31
Hmm, git lost track of this file as too many comme
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_FILE_WATCHER_H_ | |
| 6 #define CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_FILE_WATCHER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/callback_forward.h" | |
| 12 #include "base/files/file_path.h" | |
| 13 #include "base/files/file_path_watcher.h" | |
| 14 #include "base/memory/weak_ptr.h" | |
| 15 | |
| 16 namespace file_manager { | |
| 17 | |
| 18 // This class is used to watch changes in the given virtual path, remember | |
| 19 // what extensions are watching the path. | |
| 20 // | |
| 21 // For local files, the class maintains a FilePathWatcher instance and | |
| 22 // remembers what extensions are watching the path. | |
| 23 // | |
| 24 // For remote files (ex. files on Drive), the class just remembers what | |
| 25 // extensions are watching the path. The actual file watching for remote | |
| 26 // files is handled differently in EventRouter. | |
| 27 class FileWatcher { | |
| 28 public: | |
| 29 typedef std::map<std::string, int> ExtensionUsageRegistry; | |
| 30 typedef base::Callback<void(bool success)> BoolCallback; | |
| 31 | |
| 32 // AddExtension() is internally called for |extension_id|. | |
| 33 // TODO(satorux): Remove |extension_id| and stop calling AddExtension(). | |
| 34 FileWatcher(const base::FilePath& virtual_path, | |
| 35 const std::string& extension_id, | |
| 36 bool is_remote_file_system); | |
| 37 | |
| 38 ~FileWatcher(); | |
| 39 | |
| 40 // Remembers that the extension of |extension_id| is watching the virtual | |
| 41 // path. | |
| 42 // | |
| 43 // If this function is called more than once with the same extension ID, | |
| 44 // the class increments the counter internally, and RemoveExtension() | |
| 45 // decrements the counter, and forgets the extension when the counter | |
| 46 // becomes zero. | |
| 47 void AddExtension(const std::string& extension_id); | |
| 48 | |
| 49 // Forgets that the extension of |extension_id| is watching the virtual path, | |
| 50 // or just decrements the internal counter for the extension ID. See the | |
| 51 // comment at AddExtension() for details. | |
| 52 void RemoveExtension(const std::string& extension_id); | |
| 53 | |
| 54 // Returns IDs of the extensions watching virtual_path. | |
| 55 // TODO(satorux): Should just return a list of extension IDs rather than a | |
| 56 // map. | |
| 57 const ExtensionUsageRegistry& extensions() const { return extensions_; } | |
| 58 | |
| 59 // Returns 0 when no extensions are watching the virtual path. | |
| 60 // TODO(satorux): Should be replaced with extensions().empty(). | |
| 61 int ref_count() const { return ref_count_; } | |
| 62 | |
| 63 // Returns the path being watched. | |
| 64 const base::FilePath& virtual_path() const { return virtual_path_; } | |
| 65 | |
| 66 // Starts watching |local_path|. For a local file, a base::FilePathWatcher | |
| 67 // will be created and |file_watcher_callback| will be called when changes | |
| 68 // are notified. | |
| 69 // | |
| 70 // For a remote file, this function actually does nothing but | |
| 71 // runs |callback| with true. See also the class comment. | |
| 72 // TODO(satorux): This function shouldn't be called for remote files. | |
| 73 // | |
| 74 // |callback| will be called with true, if the file watch is started | |
| 75 // successfully, or false if failed. |callback| must not be null. | |
| 76 void Watch(const base::FilePath& local_path, | |
| 77 const base::FilePathWatcher::Callback& file_watcher_callback, | |
| 78 const BoolCallback& callback); | |
| 79 | |
| 80 private: | |
| 81 // Called when a FilePathWatcher is created and started. | |
| 82 // |file_path_watcher| is NULL, if the watcher wasn't started successfully. | |
| 83 void OnWatcherStarted(const BoolCallback& callback, | |
| 84 base::FilePathWatcher* file_path_watcher); | |
| 85 | |
| 86 base::FilePathWatcher* local_file_watcher_; | |
| 87 base::FilePath local_path_; | |
| 88 base::FilePath virtual_path_; | |
| 89 ExtensionUsageRegistry extensions_; | |
| 90 int ref_count_; | |
| 91 bool is_remote_file_system_; | |
| 92 | |
| 93 // Note: This should remain the last member so it'll be destroyed and | |
| 94 // invalidate the weak pointers before any other members are destroyed. | |
| 95 base::WeakPtrFactory<FileWatcher> weak_ptr_factory_; | |
| 96 }; | |
| 97 | |
| 98 } // namespace file_manager | |
| 99 | |
| 100 #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_FILE_WATCHER_H_ | |
| OLD | NEW |