Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / media_galleries / fileapi / file_path_watcher_util.cc
blob8470ba0c2c4b611eb6deb55c47b711c018882428
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.
5 #include "chrome/browser/media_galleries/fileapi/file_path_watcher_util.h"
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"
14 namespace {
16 // Bounces |path| and |error| to |callback| from the FILE thread to the media
17 // task runner.
18 void OnFilePathChangedOnFileThread(
19 const base::FilePathWatcher::Callback& callback,
20 const base::FilePath& path,
21 bool error) {
22 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
23 MediaFileSystemBackend::MediaTaskRunner()->PostTask(
24 FROM_HERE, base::Bind(callback, path, error));
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.
29 void StartFilePathWatchOnFileThread(
30 const base::FilePath& path,
31 const FileWatchStartedCallback& watch_started_callback,
32 const base::FilePathWatcher::Callback& path_changed_callback) {
33 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
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.
36 scoped_ptr<base::FilePathWatcher> watcher(new base::FilePathWatcher);
37 bool success = watcher->Watch(
38 path,
39 false /* recursive */,
40 base::Bind(&OnFilePathChangedOnFileThread, path_changed_callback));
41 if (!success)
42 LOG(ERROR) << "Adding watch for " << path.value() << " failed";
43 MediaFileSystemBackend::MediaTaskRunner()->PostTask(
44 FROM_HERE, base::Bind(watch_started_callback, base::Passed(&watcher)));
47 } // namespace
49 void StartFilePathWatchOnMediaTaskRunner(
50 const base::FilePath& path,
51 const FileWatchStartedCallback& watch_started_callback,
52 const base::FilePathWatcher::Callback& path_changed_callback) {
53 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
54 content::BrowserThread::PostTask(content::BrowserThread::FILE,
55 FROM_HERE,
56 base::Bind(&StartFilePathWatchOnFileThread,
57 path,
58 watch_started_callback,
59 path_changed_callback));