Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / base / files / file_path_watcher_kqueue.h
blob69555a300328d36854d4b255703e56f3b0259207
1 // Copyright 2014 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 #ifndef BASE_FILES_FILE_PATH_WATCHER_KQUEUE_H_
6 #define BASE_FILES_FILE_PATH_WATCHER_KQUEUE_H_
8 #include <sys/event.h>
9 #include <vector>
11 #include "base/files/file_path.h"
12 #include "base/files/file_path_watcher.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/single_thread_task_runner.h"
16 namespace base {
18 // Mac-specific file watcher implementation based on kqueue.
19 // The Linux and Windows versions are able to detect:
20 // - file creation/deletion/modification in a watched directory
21 // - file creation/deletion/modification for a watched file
22 // - modifications to the paths to a watched object that would affect the
23 // object such as renaming/attibute changes etc.
24 // The kqueue implementation will handle all of the items in the list above
25 // except for detecting modifications to files in a watched directory. It will
26 // detect the creation and deletion of files, just not the modification of
27 // files. It does however detect the attribute changes that the FSEvents impl
28 // would miss.
29 class FilePathWatcherKQueue : public FilePathWatcher::PlatformDelegate,
30 public MessageLoopForIO::Watcher,
31 public MessageLoop::DestructionObserver {
32 public:
33 FilePathWatcherKQueue();
35 // MessageLoopForIO::Watcher overrides.
36 void OnFileCanReadWithoutBlocking(int fd) override;
37 void OnFileCanWriteWithoutBlocking(int fd) override;
39 // MessageLoop::DestructionObserver overrides.
40 void WillDestroyCurrentMessageLoop() override;
42 // FilePathWatcher::PlatformDelegate overrides.
43 bool Watch(const FilePath& path,
44 bool recursive,
45 const FilePathWatcher::Callback& callback) override;
46 void Cancel() override;
48 protected:
49 ~FilePathWatcherKQueue() override;
51 private:
52 class EventData {
53 public:
54 EventData(const FilePath& path, const FilePath::StringType& subdir)
55 : path_(path), subdir_(subdir) { }
56 FilePath path_; // Full path to this item.
57 FilePath::StringType subdir_; // Path to any sub item.
60 typedef std::vector<struct kevent> EventVector;
62 // Can only be called on |io_task_runner_|'s thread.
63 void CancelOnMessageLoopThread() override;
65 // Returns true if the kevent values are error free.
66 bool AreKeventValuesValid(struct kevent* kevents, int count);
68 // Respond to a change of attributes of the path component represented by
69 // |event|. Sets |target_file_affected| to true if |target_| is affected.
70 // Sets |update_watches| to true if |events_| need to be updated.
71 void HandleAttributesChange(const EventVector::iterator& event,
72 bool* target_file_affected,
73 bool* update_watches);
75 // Respond to a move or deletion of the path component represented by
76 // |event|. Sets |target_file_affected| to true if |target_| is affected.
77 // Sets |update_watches| to true if |events_| need to be updated.
78 void HandleDeleteOrMoveChange(const EventVector::iterator& event,
79 bool* target_file_affected,
80 bool* update_watches);
82 // Respond to a creation of an item in the path component represented by
83 // |event|. Sets |target_file_affected| to true if |target_| is affected.
84 // Sets |update_watches| to true if |events_| need to be updated.
85 void HandleCreateItemChange(const EventVector::iterator& event,
86 bool* target_file_affected,
87 bool* update_watches);
89 // Update |events_| with the current status of the system.
90 // Sets |target_file_affected| to true if |target_| is affected.
91 // Returns false if an error occurs.
92 bool UpdateWatches(bool* target_file_affected);
94 // Fills |events| with one kevent per component in |path|.
95 // Returns the number of valid events created where a valid event is
96 // defined as one that has a ident (file descriptor) field != -1.
97 static int EventsForPath(FilePath path, EventVector *events);
99 // Release a kevent generated by EventsForPath.
100 static void ReleaseEvent(struct kevent& event);
102 // Returns a file descriptor that will not block the system from deleting
103 // the file it references.
104 static uintptr_t FileDescriptorForPath(const FilePath& path);
106 static const uintptr_t kNoFileDescriptor = static_cast<uintptr_t>(-1);
108 // Closes |*fd| and sets |*fd| to -1.
109 static void CloseFileDescriptor(uintptr_t* fd);
111 // Returns true if kevent has open file descriptor.
112 static bool IsKeventFileDescriptorOpen(const struct kevent& event) {
113 return event.ident != kNoFileDescriptor;
116 static EventData* EventDataForKevent(const struct kevent& event) {
117 return reinterpret_cast<EventData*>(event.udata);
120 EventVector events_;
121 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
122 MessageLoopForIO::FileDescriptorWatcher kqueue_watcher_;
123 FilePathWatcher::Callback callback_;
124 FilePath target_;
125 int kqueue_;
127 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherKQueue);
130 } // namespace base
132 #endif // BASE_FILES_FILE_PATH_WATCHER_KQUEUE_H_