Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Unified Diff: chrome/browser/media_galleries/fileapi/file_path_watcher_util.cc

Issue 23499006: Media Galleries API Picasa: Add file watch to invalidate database data on disk write. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/media_galleries/fileapi/file_path_watcher_util.cc
diff --git a/chrome/browser/media_galleries/fileapi/file_path_watcher_util.cc b/chrome/browser/media_galleries/fileapi/file_path_watcher_util.cc
new file mode 100644
index 0000000000000000000000000000000000000000..59b372ec62059c6c75bd48f302a803faaab17c01
--- /dev/null
+++ b/chrome/browser/media_galleries/fileapi/file_path_watcher_util.cc
@@ -0,0 +1,67 @@
+// 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.
+
+#include "chrome/browser/media_galleries/fileapi/file_path_watcher_util.h"
+
+#include "base/bind.h"
+#include "base/callback.h"
+#include "base/location.h"
+#include "base/logging.h"
+#include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h"
+#include "content/public/browser/browser_thread.h"
+
+namespace chrome {
+
+namespace {
+
+// Bounces |path| and |error| to |callback| from the FILE thread to the media
+// task runner.
+void OnFilePathChangedOnFileThread(
+ const base::FilePathWatcher::Callback& callback,
+ const base::FilePath& path,
+ bool error) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
+ MediaFileSystemBackend::MediaTaskRunner()->PostTask(
+ FROM_HERE, base::Bind(callback, path, error));
+}
+
+// The watch has to be started on the FILE thread, and the callback called by
+// the FilePathWatcher also needs to run on the FILE thread.
+void StartFilePathWatchOnFileThread(
+ const base::FilePath& path,
+ bool recursive,
+ const FileWatchStartedCallback& watch_started_callback,
+ const base::FilePathWatcher::Callback& path_changed_callback) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
+ // The watcher is created on the FILE thread because it is very difficult
+ // to safely pass an already-created file watcher to a different thread.
+ scoped_ptr<base::FilePathWatcher> watcher(new base::FilePathWatcher);
+ bool success = watcher->Watch(
+ path,
+ recursive,
+ base::Bind(&OnFilePathChangedOnFileThread, path_changed_callback));
+ if (!success)
+ LOG(ERROR) << "Adding watch for " << path.value() << " failed";
+ MediaFileSystemBackend::MediaTaskRunner()->PostTask(
+ FROM_HERE, base::Bind(watch_started_callback, base::Passed(&watcher)));
+}
+
+} // namespace
+
+void StartFilePathWatchOnMediaTaskRunner(
+ const base::FilePath& path,
+ bool recursive,
+ const FileWatchStartedCallback& watch_started_callback,
+ const base::FilePathWatcher::Callback& path_changed_callback) {
+ DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
+ content::BrowserThread::PostTask(content::BrowserThread::FILE,
+ FROM_HERE,
+ base::Bind(&StartFilePathWatchOnFileThread,
+ path,
+ recursive,
+ watch_started_callback,
+ path_changed_callback));
+}
+
+} // namespace chrome

Powered by Google App Engine
This is Rietveld 408576698