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_DRIVE_FILE_WRITE_WATCHER_H_ 6 #define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_WRITE_WATCHER_H_ 7 8 #include "base/callback_forward.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/weak_ptr.h" 11 #include "chrome/browser/chromeos/drive/file_system_util.h" 12 13 namespace base { 14 class FilePath; 15 } // namespace base 16 17 namespace drive { 18 19 namespace file_system { 20 class OperationObserver; 21 } // namespace file_system 22 23 namespace internal { 24 25 typedef base::Callback<void(bool)> StartWatchCallback; 26 27 // The class watches modification to Drive files in the cache directory. 28 // This is used for returning a local writable snapshot of Drive files from the 29 // Save-As file dialog, so that the callers of the dialog can save to Drive 30 // without any special handling about Drive. 31 class FileWriteWatcher { 32 public: 33 explicit FileWriteWatcher(file_system::OperationObserver* observer); 34 ~FileWriteWatcher(); 35 36 // Starts watching the modification to |path|. When it successfully started 37 // watching, it runs |callback| by passing true as the argument. Or if it 38 // failed, the callback is run with false. 39 // When modification is detected, it is notified to the |observer| passed to 40 // the constructor by calling OnCacheFileUploadNeededByOperation(resource_id). 41 // 42 // Currently, the modification is watched in "one-shot" manner. That is, once 43 // a modification is notified, the watch is deactivated for freeing system 44 // resources. As a heuristic to capture the real end of write operations that 45 // might be done by several chunked writes, the notification is fired after 46 // 5 seconds has passed after the last write operation is detected. 47 // 48 // TODO(kinaba): investigate the possibility to continuously watch the whole 49 // cache directory. http://crbug.com/269424 50 void StartWatch(const base::FilePath& path, 51 const std::string& resource_id, 52 const StartWatchCallback& callback); 53 54 // For testing purpose, stops inserting delay between the write detection and 55 // notification to the observer. 56 void DisableDelayForTesting(); 57 58 private: 59 // Invoked when a modification is observed. 60 void OnWriteEvent(const std::string& resource_id); 61 62 class FileWriteWatcherImpl; 63 scoped_ptr<FileWriteWatcherImpl, util::DestroyHelper> watcher_impl_; 64 file_system::OperationObserver* operation_observer_; 65 66 // Note: This should remain the last member so it'll be destroyed and 67 // invalidate its weak pointers before any other members are destroyed. 68 base::WeakPtrFactory<FileWriteWatcher> weak_ptr_factory_; 69 DISALLOW_COPY_AND_ASSIGN(FileWriteWatcher); 70 }; 71 72 } // namespace internal 73 } // namespace drive 74 75 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_WRITE_WATCHER_H_ 76