Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / base / files / file_path_watcher_kqueue.h
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.h"
12 #include "base/files/file_path_watcher.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/message_loop/message_loop_proxy.h"
15
16 namespace base {
17
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();
34
35   // MessageLoopForIO::Watcher overrides.
36   void OnFileCanReadWithoutBlocking(int fd) override;
37   void OnFileCanWriteWithoutBlocking(int fd) override;
38
39   // MessageLoop::DestructionObserver overrides.
40   void WillDestroyCurrentMessageLoop() override;
41
42   // FilePathWatcher::PlatformDelegate overrides.
43   bool Watch(const FilePath& path,
44              bool recursive,
45              const FilePathWatcher::Callback& callback) override;
46   void Cancel() override;
47
48  protected:
49   ~FilePathWatcherKQueue() override;
50
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.
58   };
59
60   typedef std::vector<struct kevent> EventVector;
61
62   // Can only be called on |io_message_loop_|'s thread.
63   void CancelOnMessageLoopThread() override;
64
65   // Returns true if the kevent values are error free.
66   bool AreKeventValuesValid(struct kevent* kevents, int count);
67
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);
74
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);
81
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);
88
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);
93
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);
98
99   // Release a kevent generated by EventsForPath.
100   static void ReleaseEvent(struct kevent& event);
101
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);
105
106   static const uintptr_t kNoFileDescriptor = static_cast<uintptr_t>(-1);
107
108   // Closes |*fd| and sets |*fd| to -1.
109   static void CloseFileDescriptor(uintptr_t* fd);
110
111   // Returns true if kevent has open file descriptor.
112   static bool IsKeventFileDescriptorOpen(const struct kevent& event) {
113     return event.ident != kNoFileDescriptor;
114   }
115
116   static EventData* EventDataForKevent(const struct kevent& event) {
117     return reinterpret_cast<EventData*>(event.udata);
118   }
119
120   EventVector events_;
121   scoped_refptr<base::MessageLoopProxy> io_message_loop_;
122   MessageLoopForIO::FileDescriptorWatcher kqueue_watcher_;
123   FilePathWatcher::Callback callback_;
124   FilePath target_;
125   int kqueue_;
126
127   DISALLOW_COPY_AND_ASSIGN(FilePathWatcherKQueue);
128 };
129
130 }  // namespace base
131
132 #endif  // BASE_FILES_FILE_PATH_WATCHER_KQUEUE_H_