Index: base/files/file_path_watcher_fsevents.cc |
diff --git a/base/files/file_path_watcher_fsevents.cc b/base/files/file_path_watcher_fsevents.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ff24e075e9cf24c093f8e09a4110a9714e332908 |
--- /dev/null |
+++ b/base/files/file_path_watcher_fsevents.cc |
@@ -0,0 +1,255 @@ |
+// Copyright 2014 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 "base/files/file_path_watcher_fsevents.h" |
+ |
+#include <list> |
+ |
+#include "base/bind.h" |
+#include "base/file_util.h" |
+#include "base/lazy_instance.h" |
+#include "base/logging.h" |
+#include "base/mac/libdispatch_task_runner.h" |
+#include "base/mac/scoped_cftyperef.h" |
+#include "base/message_loop/message_loop.h" |
+ |
+namespace base { |
+ |
+namespace { |
+ |
+// The latency parameter passed to FSEventsStreamCreate(). |
+const CFAbsoluteTime kEventLatencySeconds = 0.3; |
+ |
+class FSEventsTaskRunner : public mac::LibDispatchTaskRunner { |
+ public: |
+ FSEventsTaskRunner() |
+ : mac::LibDispatchTaskRunner("chromium.org.FilePathWatcherFSEvents") { |
+ } |
+ |
+ protected: |
+ virtual ~FSEventsTaskRunner() {} |
+}; |
+ |
+static LazyInstance<FSEventsTaskRunner>::Leaky g_task_runner = |
+ LAZY_INSTANCE_INITIALIZER; |
+ |
+// Resolve any symlinks in the path. |
+base::FilePath ResolvePath(const base::FilePath& path) { |
+ const unsigned kMaxLinksToResolve = 255; |
+ |
+ std::vector<FilePath::StringType> component_array; |
Mattias Nissler (ping if slow)
2014/05/28 08:08:32
FilePath vs. base::FilePath inconsistency here.
Mattias Nissler (ping if slow)
2014/05/28 08:08:32
nit: this should be |component_vector|.
vandebo (ex-Chrome)
2014/05/28 18:54:52
Fixed.
vandebo (ex-Chrome)
2014/05/28 18:54:52
Done.
|
+ path.GetComponents(&component_array); |
+ std::list<base::FilePath> components; |
Mattias Nissler (ping if slow)
2014/05/28 08:08:32
Shouldn't this rather be std::list<base::FilePath:
vandebo (ex-Chrome)
2014/05/28 18:54:52
Indeed.
|
+ for (size_t i = 0; i < component_array.size(); i++) |
+ components.push_back(base::FilePath(component_array[i])); |
+ |
+ base::FilePath result; |
+ unsigned resolve_count = 0; |
+ while (resolve_count < kMaxLinksToResolve && !components.empty()) { |
+ base::FilePath current = result.Append(*components.begin()); |
+ components.pop_front(); |
+ |
+ base::FilePath target; |
+ if (base::ReadSymbolicLink(current, &target)) { |
+ if (target.IsAbsolute()) |
+ result.clear(); |
+ std::vector<FilePath::StringType> target_components; |
+ target.GetComponents(&target_components); |
+ for (std::vector<FilePath::StringType>::const_reverse_iterator it = |
+ target_components.rbegin(); |
+ it != target_components.rend(); |
+ ++it) { |
+ components.push_front(base::FilePath(*it)); |
+ } |
Mattias Nissler (ping if slow)
2014/05/28 08:08:32
instead of the loop, you could also do
std::copy(
vandebo (ex-Chrome)
2014/05/28 18:54:52
Yup, I had target_components.insert(...) before bu
|
+ resolve_count++; |
+ } else { |
+ result = current; |
+ } |
+ } |
+ |
+ if (resolve_count >= kMaxLinksToResolve) |
+ result.clear(); |
+ return result; |
+} |
+ |
+// The callback passed to FSEventStreamCreate(). |
+void FSEventsCallback(ConstFSEventStreamRef stream, |
+ void* event_watcher, size_t num_events, |
+ void* event_paths, const FSEventStreamEventFlags flags[], |
+ const FSEventStreamEventId event_ids[]) { |
+ base::FilePathWatcherFSEvents* watcher = |
+ reinterpret_cast<base::FilePathWatcherFSEvents*>(event_watcher); |
+ DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); |
+ |
+ bool root_changed = false; |
+ std::vector<FilePath> paths; |
+ FSEventStreamEventId root_change_at = FSEventStreamGetLatestEventId(stream); |
+ for (size_t i = 0; i < num_events; i++) { |
+ if (flags[i] & kFSEventStreamEventFlagRootChanged) |
+ root_changed = true; |
+ if (event_ids[i]) |
+ root_change_at = std::min(root_change_at, event_ids[i]); |
+ paths.push_back(FilePath( |
+ reinterpret_cast<char**>(event_paths)[i]).StripTrailingSeparators()); |
+ } |
+ |
+ // Reinitialize the event stream if we find changes to the root. This is |
+ // necessary since FSEvents doesn't report any events for the subtree after |
+ // the directory to be watched gets created. |
+ if (root_changed) { |
+ // Resetting the event stream from within the callback fails (FSEvents spews |
+ // bad file descriptor errors), so post a task to do the reset. |
+ g_task_runner.Get().PostTask( |
+ FROM_HERE, |
+ base::Bind(&base::FilePathWatcherFSEvents::UpdateEventStream, watcher, |
+ root_change_at)); |
+ } |
+ |
+ watcher->OnFilePathsChanged(paths); |
+} |
+ |
+} // namespace |
+ |
+FilePathWatcherFSEvents::FilePathWatcherFSEvents() : fsevent_stream_(NULL) { |
+} |
+ |
+void FilePathWatcherFSEvents::OnFilePathsChanged( |
+ const std::vector<FilePath>& paths) { |
+ if (!message_loop()->BelongsToCurrentThread()) { |
+ message_loop()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&FilePathWatcherFSEvents::OnFilePathsChanged, this, paths)); |
+ return; |
+ } |
+ |
+ DCHECK(message_loop()->BelongsToCurrentThread()); |
+ DCHECK(!resolved_target_.empty()); |
+ |
+ for (size_t i = 0; i < paths.size(); i++) { |
+ if (resolved_target_.IsParent(paths[i]) || resolved_target_ == paths[i]) { |
+ callback_.Run(target_, false); |
+ return; |
+ } |
+ } |
+} |
+ |
+bool FilePathWatcherFSEvents::Watch(const FilePath& path, |
+ bool recursive, |
+ const FilePathWatcher::Callback& callback) { |
+ DCHECK(resolved_target_.empty()); |
+ DCHECK(MessageLoopForIO::current()); |
+ DCHECK(!callback.is_null()); |
+ |
+ // This class could support non-recursive watches, but that is currently |
+ // left to FilePathWatcherKQueue. |
+ if (!recursive) |
+ return false; |
+ |
+ set_message_loop(base::MessageLoopProxy::current()); |
+ callback_ = callback; |
+ target_ = path; |
+ |
+ FSEventStreamEventId start_event = FSEventsGetCurrentEventId(); |
+ g_task_runner.Get().PostTask( |
+ FROM_HERE, |
+ base::Bind(&FilePathWatcherFSEvents::StartEventStream, this, |
+ start_event)); |
+ return true; |
+} |
+ |
+void FilePathWatcherFSEvents::Cancel() { |
+ if (callback_.is_null()) { |
+ // Watch was never called, so exit. |
+ set_cancelled(); |
+ return; |
+ } |
+ |
+ // Switch to the dispatch queue thread if necessary, so we can tear down |
+ // the event stream. |
+ if (!g_task_runner.Get().RunsTasksOnCurrentThread()) { |
+ g_task_runner.Get().PostTask( |
+ FROM_HERE, |
+ base::Bind(&FilePathWatcherFSEvents::CancelOnMessageLoopThread, this)); |
+ } else { |
+ CancelOnMessageLoopThread(); |
+ } |
+} |
+ |
+void FilePathWatcherFSEvents::CancelOnMessageLoopThread() { |
+ // For all other implementations, the "message loop thread" is the IO thread, |
+ // as returned by message_loop(). This implementation, however, needs to |
+ // cancel pending work on the Dipatch Queue thread. |
+ DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); |
+ |
+ set_cancelled(); |
+ if (fsevent_stream_) { |
+ DestroyEventStream(); |
+ callback_.Reset(); |
+ target_.clear(); |
+ resolved_target_.clear(); |
+ } |
+} |
+ |
+void FilePathWatcherFSEvents::UpdateEventStream( |
+ FSEventStreamEventId start_event) { |
+ DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); |
+ |
+ // It can happen that the watcher gets canceled while tasks that call this |
+ // function are still in flight, so abort if this situation is detected. |
+ if (is_cancelled()) |
+ return; |
+ |
+ if (fsevent_stream_) |
+ DestroyEventStream(); |
+ |
+ base::ScopedCFTypeRef<CFStringRef> cf_path(CFStringCreateWithCString( |
+ NULL, resolved_target_.value().c_str(), kCFStringEncodingMacHFS)); |
+ base::ScopedCFTypeRef<CFStringRef> cf_dir_path(CFStringCreateWithCString( |
+ NULL, resolved_target_.DirName().value().c_str(), |
+ kCFStringEncodingMacHFS)); |
+ CFStringRef paths_array[] = { cf_path.get(), cf_dir_path.get() }; |
+ base::ScopedCFTypeRef<CFArrayRef> watched_paths(CFArrayCreate( |
+ NULL, reinterpret_cast<const void**>(paths_array), arraysize(paths_array), |
+ &kCFTypeArrayCallBacks)); |
+ |
+ FSEventStreamContext context; |
+ context.version = 0; |
+ context.info = this; |
+ context.retain = NULL; |
+ context.release = NULL; |
+ context.copyDescription = NULL; |
+ |
+ fsevent_stream_ = FSEventStreamCreate(NULL, &FSEventsCallback, &context, |
+ watched_paths, |
+ start_event, |
+ kEventLatencySeconds, |
+ kFSEventStreamCreateFlagWatchRoot); |
+ FSEventStreamSetDispatchQueue(fsevent_stream_, |
+ g_task_runner.Get().GetDispatchQueue()); |
+ |
+ if (!FSEventStreamStart(fsevent_stream_)) |
+ message_loop()->PostTask(FROM_HERE, base::Bind(callback_, target_, true)); |
+} |
+ |
+void FilePathWatcherFSEvents::DestroyEventStream() { |
+ FSEventStreamStop(fsevent_stream_); |
+ FSEventStreamInvalidate(fsevent_stream_); |
+ FSEventStreamRelease(fsevent_stream_); |
+ fsevent_stream_ = NULL; |
+} |
+ |
+void FilePathWatcherFSEvents::StartEventStream( |
+ FSEventStreamEventId start_event) { |
+ DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); |
+ resolved_target_ = ResolvePath(target_).StripTrailingSeparators(); |
Mattias Nissler (ping if slow)
2014/05/28 08:08:32
If we only resolve when starting the watcher, we'l
vandebo (ex-Chrome)
2014/05/28 18:54:52
Yup, Fixed.
|
+ if (resolved_target_.empty()) { |
+ message_loop()->PostTask(FROM_HERE, base::Bind(callback_, target_, true)); |
+ return; |
+ } |
+ UpdateEventStream(start_event); |
+} |
+ |
+FilePathWatcherFSEvents::~FilePathWatcherFSEvents() {} |
+ |
+} // namespace base |