OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/media_galleries/fileapi/file_path_watcher_util.h" | 5 #include "chrome/browser/media_galleries/fileapi/file_path_watcher_util.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 | 26 |
27 // The watch has to be started on the FILE thread, and the callback called by | 27 // The watch has to be started on the FILE thread, and the callback called by |
28 // the FilePathWatcher also needs to run on the FILE thread. | 28 // the FilePathWatcher also needs to run on the FILE thread. |
29 void StartFilePathWatchOnFileThread( | 29 void StartFilePathWatchOnFileThread( |
30 const base::FilePath& path, | 30 const base::FilePath& path, |
31 const FileWatchStartedCallback& watch_started_callback, | 31 const FileWatchStartedCallback& watch_started_callback, |
32 const base::FilePathWatcher::Callback& path_changed_callback) { | 32 const base::FilePathWatcher::Callback& path_changed_callback) { |
33 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); | 33 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); |
34 // The watcher is created on the FILE thread because it is very difficult | 34 // The watcher is created on the FILE thread because it is very difficult |
35 // to safely pass an already-created file watcher to a different thread. | 35 // to safely pass an already-created file watcher to a different thread. |
36 std::unique_ptr<base::FilePathWatcher> watcher(new base::FilePathWatcher); | 36 MediaFilePathWatcherUniquePtr watcher(new base::FilePathWatcher); |
37 bool success = watcher->Watch( | 37 bool success = watcher->Watch( |
38 path, | 38 path, |
39 false /* recursive */, | 39 false /* recursive */, |
40 base::Bind(&OnFilePathChangedOnFileThread, path_changed_callback)); | 40 base::Bind(&OnFilePathChangedOnFileThread, path_changed_callback)); |
41 if (!success) | 41 if (!success) |
42 LOG(ERROR) << "Adding watch for " << path.value() << " failed"; | 42 LOG(ERROR) << "Adding watch for " << path.value() << " failed"; |
43 MediaFileSystemBackend::MediaTaskRunner()->PostTask( | 43 MediaFileSystemBackend::MediaTaskRunner()->PostTask( |
44 FROM_HERE, base::Bind(watch_started_callback, base::Passed(&watcher))); | 44 FROM_HERE, base::Bind(watch_started_callback, base::Passed(&watcher))); |
45 } | 45 } |
46 | 46 |
47 } // namespace | 47 } // namespace |
48 | 48 |
49 void StartFilePathWatchOnMediaTaskRunner( | 49 void StartFilePathWatchOnMediaTaskRunner( |
50 const base::FilePath& path, | 50 const base::FilePath& path, |
51 const FileWatchStartedCallback& watch_started_callback, | 51 const FileWatchStartedCallback& watch_started_callback, |
52 const base::FilePathWatcher::Callback& path_changed_callback) { | 52 const base::FilePathWatcher::Callback& path_changed_callback) { |
53 MediaFileSystemBackend::AssertCurrentlyOnMediaSequence(); | 53 MediaFileSystemBackend::AssertCurrentlyOnMediaSequence(); |
54 content::BrowserThread::PostTask(content::BrowserThread::FILE, | 54 content::BrowserThread::PostTask(content::BrowserThread::FILE, |
55 FROM_HERE, | 55 FROM_HERE, |
56 base::Bind(&StartFilePathWatchOnFileThread, | 56 base::Bind(&StartFilePathWatchOnFileThread, |
57 path, | 57 path, |
58 watch_started_callback, | 58 watch_started_callback, |
59 path_changed_callback)); | 59 path_changed_callback)); |
60 } | 60 } |
61 | |
62 void StopFilePathWatchOnMediaTaskRunner( | |
63 std::unique_ptr<base::FilePathWatcher> watcher) { | |
64 MediaFileSystemBackend::AssertCurrentlyOnMediaSequence(); | |
65 if (watcher) { | |
66 const bool task_posted = content::BrowserThread::DeleteSoon( | |
67 content::BrowserThread::FILE, FROM_HERE, watcher.release()); | |
68 | |
69 // This will fail if the FILE thread has been stopped. | |
70 DCHECK(task_posted); | |
71 } | |
72 } | |
OLD | NEW |