summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/drive/file_write_watcher.h
diff options
context:
space:
mode:
authorkinaba@chromium.org <kinaba@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-07 06:59:56 +0000
committerkinaba@chromium.org <kinaba@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-07 06:59:56 +0000
commit799d53bf220e6b818af66fe7cdb5a5af657e2fab (patch)
treee93d41c322d492931b298dc386a2ca29a4093407 /chrome/browser/chromeos/drive/file_write_watcher.h
parentfab290345ad407611d8e6832773b567a68573664 (diff)
downloadchromium_src-799d53bf220e6b818af66fe7cdb5a5af657e2fab.zip
chromium_src-799d53bf220e6b818af66fe7cdb5a5af657e2fab.tar.gz
chromium_src-799d53bf220e6b818af66fe7cdb5a5af657e2fab.tar.bz2
Add drive::internal::FileWriteWatcher.
This is a part of the effort to enable Drive in the Save-As dialog of Chrome OS for every call site. For that, we need to care about existing callers that just simply write to local file paths returned by the dialog. The new class added in this patch watches specified file path in Drive cache, and notifies the need for uploading when the file content is modified. BUG=140425 R=hashimoto@chromium.org Review URL: https://codereview.chromium.org/22314009 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@216119 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/chromeos/drive/file_write_watcher.h')
-rw-r--r--chrome/browser/chromeos/drive/file_write_watcher.h75
1 files changed, 75 insertions, 0 deletions
diff --git a/chrome/browser/chromeos/drive/file_write_watcher.h b/chrome/browser/chromeos/drive/file_write_watcher.h
new file mode 100644
index 0000000..91c947d
--- /dev/null
+++ b/chrome/browser/chromeos/drive/file_write_watcher.h
@@ -0,0 +1,75 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FILE_WRITE_WATCHER_H_
+#define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_WRITE_WATCHER_H_
+
+#include "base/callback_forward.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/memory/weak_ptr.h"
+#include "chrome/browser/chromeos/drive/file_system_util.h"
+
+namespace base {
+class FilePath;
+} // namespace base
+
+namespace drive {
+
+namespace file_system {
+class OperationObserver;
+} // namespace file_system
+
+namespace internal {
+
+typedef base::Callback<void(bool)> StartWatchCallback;
+
+// The class watches modification to Drive files in the cache directory.
+// This is used for returning a local writable snapshot of Drive files from the
+// Save-As file dialog, so that the callers of the dialog can save to Drive
+// without any special handling about Drive.
+class FileWriteWatcher {
+ public:
+ explicit FileWriteWatcher(file_system::OperationObserver* observer);
+ ~FileWriteWatcher();
+
+ // Starts watching the modification to |path|. When it successfully started
+ // watching, it runs |callback| by passing true as the argument. Or if it
+ // failed, the callback is run with false.
+ // When modification is detected, it is notified to the |observer| passed to
+ // the constructor by calling OnCacheFileUploadNeededByOperation(resource_id).
+ //
+ // Currently, the modification is watched in "one-shot" manner. That is, once
+ // a modification is notified, the watch is deactivated for freeing system
+ // resources. As a heuristic to capture the real end of write operations that
+ // might be done by several chunked writes, the notification is fired after
+ // 5 seconds has passed after the last write operation is detected.
+ //
+ // TODO(kinaba): investigate the possibility to continuously watch the whole
+ // cache directory.
+ void StartWatch(const base::FilePath& path,
+ const std::string& resource_id,
+ const StartWatchCallback& callback);
+
+ // For testing purpose, stops inserting delay between the write detection and
+ // notification to the observer.
+ void DisableDelayForTesting();
+
+ private:
+ // Invoked when a modification is observed.
+ void OnWriteEvent(const std::string& resource_id);
+
+ class FileWriteWatcherImpl;
+ scoped_ptr<FileWriteWatcherImpl, util::DestroyHelper> watcher_impl_;
+ file_system::OperationObserver* operation_observer_;
+
+ // Note: This should remain the last member so it'll be destroyed and
+ // invalidate its weak pointers before any other members are destroyed.
+ base::WeakPtrFactory<FileWriteWatcher> weak_ptr_factory_;
+ DISALLOW_COPY_AND_ASSIGN(FileWriteWatcher);
+};
+
+} // namespace internal
+} // namespace drive
+
+#endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_WRITE_WATCHER_H_