Home | History | Annotate | Download | only in file_manager
      1 // Copyright 2013 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 #ifndef CHROME_BROWSER_CHROMEOS_FILE_MANAGER_FILE_WATCHER_H_
      6 #define CHROME_BROWSER_CHROMEOS_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 base::Callback<void(bool success)> BoolCallback;
     30 
     31   // Creates a FileWatcher associated with the virtual path.
     32   explicit FileWatcher(const base::FilePath& virtual_path);
     33 
     34   ~FileWatcher();
     35 
     36   // Remembers that the extension of |extension_id| is watching the virtual
     37   // path.
     38   //
     39   // If this function is called more than once with the same extension ID,
     40   // the class increments the counter internally, and RemoveExtension()
     41   // decrements the counter, and forgets the extension when the counter
     42   // becomes zero.
     43   void AddExtension(const std::string& extension_id);
     44 
     45   // Forgets that the extension of |extension_id| is watching the virtual path,
     46   // or just decrements the internal counter for the extension ID. See the
     47   // comment at AddExtension() for details.
     48   void RemoveExtension(const std::string& extension_id);
     49 
     50   // Returns IDs of the extensions watching virtual_path. The returned list
     51   // is sorted in the alphabetical order and contains no duplicates.
     52   std::vector<std::string> GetExtensionIds() const;
     53 
     54   // Returns the virtual path associated with the FileWatcher.
     55   const base::FilePath& virtual_path() const { return virtual_path_; }
     56 
     57   // Starts watching a local file at |local_path|. |file_watcher_callback|
     58   // will be called when changes are notified.
     59   //
     60   // |callback| will be called with true, if the file watch is started
     61   // successfully, or false if failed. |callback| must not be null.
     62   void WatchLocalFile(
     63       const base::FilePath& local_path,
     64       const base::FilePathWatcher::Callback& file_watcher_callback,
     65       const BoolCallback& callback);
     66 
     67  private:
     68   // Called when a FilePathWatcher is created and started.
     69   // |file_path_watcher| is NULL, if the watcher wasn't started successfully.
     70   void OnWatcherStarted(const BoolCallback& callback,
     71                         base::FilePathWatcher* file_path_watcher);
     72 
     73   base::FilePathWatcher* local_file_watcher_;
     74   base::FilePath virtual_path_;
     75   // Map of extension-id to counter. See the comment at AddExtension() for
     76   // why we need to count.
     77   typedef std::map<std::string, int> ExtensionCountMap;
     78   ExtensionCountMap extensions_;
     79 
     80   // Note: This should remain the last member so it'll be destroyed and
     81   // invalidate the weak pointers before any other members are destroyed.
     82   base::WeakPtrFactory<FileWatcher> weak_ptr_factory_;
     83 };
     84 
     85 }  // namespace file_manager
     86 
     87 #endif  // CHROME_BROWSER_CHROMEOS_FILE_MANAGER_FILE_WATCHER_H_
     88