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