OLD | NEW |
(Empty) | |
| 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 #include "chrome/browser/media_galleries/fileapi/file_path_watcher_util.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback.h" |
| 9 #include "base/location.h" |
| 10 #include "base/logging.h" |
| 11 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 |
| 14 namespace chrome { |
| 15 |
| 16 namespace { |
| 17 |
| 18 // Bounces |path| and |error| to |callback| from the FILE thread to the media |
| 19 // task runner. |
| 20 void OnFilePathChangedOnFileThread( |
| 21 const base::FilePathWatcher::Callback& callback, |
| 22 const base::FilePath& path, |
| 23 bool error) { |
| 24 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 25 MediaFileSystemBackend::MediaTaskRunner()->PostTask( |
| 26 FROM_HERE, base::Bind(callback, path, error)); |
| 27 } |
| 28 |
| 29 // The watch has to be started on the FILE thread, and the callback called by |
| 30 // the FilePathWatcher also needs to run on the FILE thread. |
| 31 void StartFilePathWatchOnFileThread( |
| 32 const base::FilePath& path, |
| 33 bool recursive, |
| 34 const FileWatchStartedCallback& watch_started_callback, |
| 35 const base::FilePathWatcher::Callback& path_changed_callback) { |
| 36 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 37 // The watcher is created on the FILE thread because it is very difficult |
| 38 // to safely pass an already-created file watcher to a different thread. |
| 39 scoped_ptr<base::FilePathWatcher> watcher(new base::FilePathWatcher); |
| 40 bool success = watcher->Watch( |
| 41 path, |
| 42 recursive, |
| 43 base::Bind(&OnFilePathChangedOnFileThread, path_changed_callback)); |
| 44 if (!success) |
| 45 LOG(ERROR) << "Adding watch for " << path.value() << " failed"; |
| 46 MediaFileSystemBackend::MediaTaskRunner()->PostTask( |
| 47 FROM_HERE, base::Bind(watch_started_callback, base::Passed(&watcher))); |
| 48 } |
| 49 |
| 50 } // namespace |
| 51 |
| 52 void StartFilePathWatchOnMediaTaskRunner( |
| 53 const base::FilePath& path, |
| 54 bool recursive, |
| 55 const FileWatchStartedCallback& watch_started_callback, |
| 56 const base::FilePathWatcher::Callback& path_changed_callback) { |
| 57 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 58 content::BrowserThread::PostTask(content::BrowserThread::FILE, |
| 59 FROM_HERE, |
| 60 base::Bind(&StartFilePathWatchOnFileThread, |
| 61 path, |
| 62 recursive, |
| 63 watch_started_callback, |
| 64 path_changed_callback)); |
| 65 } |
| 66 |
| 67 } // namespace chrome |
OLD | NEW |