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/chromeos/file_manager/file_watcher.h"
8 #include "base/message_loop/message_loop_proxy.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "google_apis/drive/task_util.h"
12 using content::BrowserThread
;
14 namespace file_manager
{
17 // Creates a base::FilePathWatcher and starts watching at |watch_path| with
18 // |callback|. Returns NULL on failure.
19 base::FilePathWatcher
* CreateAndStartFilePathWatcher(
20 const base::FilePath
& watch_path
,
21 const base::FilePathWatcher::Callback
& callback
) {
22 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
23 DCHECK(!callback
.is_null());
25 scoped_ptr
<base::FilePathWatcher
> watcher(new base::FilePathWatcher
);
26 if (!watcher
->Watch(watch_path
, false /* recursive */, callback
))
29 return watcher
.release();
34 FileWatcher::FileWatcher(const base::FilePath
& virtual_path
)
35 : local_file_watcher_(NULL
),
36 virtual_path_(virtual_path
),
37 weak_ptr_factory_(this) {
38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
41 FileWatcher::~FileWatcher() {
42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
44 BrowserThread::DeleteSoon(BrowserThread::FILE,
49 void FileWatcher::AddExtension(const std::string
& extension_id
) {
50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
52 extensions_
[extension_id
]++;
55 void FileWatcher::RemoveExtension(const std::string
& extension_id
) {
56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
58 ExtensionCountMap::iterator it
= extensions_
.find(extension_id
);
59 if (it
== extensions_
.end()) {
60 LOG(ERROR
) << " Extension [" << extension_id
61 << "] tries to unsubscribe from folder ["
62 << virtual_path_
.value()
63 << "] it isn't subscribed";
67 // If entry found - decrease it's count and remove if necessary
70 extensions_
.erase(it
);
73 std::vector
<std::string
> FileWatcher::GetExtensionIds() const {
74 std::vector
<std::string
> extension_ids
;
75 for (ExtensionCountMap::const_iterator iter
= extensions_
.begin();
76 iter
!= extensions_
.end(); ++iter
) {
77 extension_ids
.push_back(iter
->first
);
82 void FileWatcher::WatchLocalFile(
83 const base::FilePath
& local_path
,
84 const base::FilePathWatcher::Callback
& file_watcher_callback
,
85 const BoolCallback
& callback
) {
86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
87 DCHECK(!callback
.is_null());
88 DCHECK(!local_file_watcher_
);
90 BrowserThread::PostTaskAndReplyWithResult(
93 base::Bind(&CreateAndStartFilePathWatcher
,
95 google_apis::CreateRelayCallback(file_watcher_callback
)),
96 base::Bind(&FileWatcher::OnWatcherStarted
,
97 weak_ptr_factory_
.GetWeakPtr(),
101 void FileWatcher::OnWatcherStarted(
102 const BoolCallback
& callback
,
103 base::FilePathWatcher
* file_watcher
) {
104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
105 DCHECK(!callback
.is_null());
106 DCHECK(!local_file_watcher_
);
109 local_file_watcher_
= file_watcher
;
116 } // namespace file_manager