Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(492)

Side by Side Diff: base/files/file_path_watcher_fsevents.cc

Issue 1046353004: Access fields from the appropriate threads in FilePathWatcherFSEvents. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Check callback_ instead of fsevent_stream_ in destructor. Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/files/file_path_watcher_fsevents.h" 5 #include "base/files/file_path_watcher_fsevents.h"
6 6
7 #include <list> 7 #include <list>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 } else { 68 } else {
69 result = current; 69 result = current;
70 } 70 }
71 } 71 }
72 72
73 if (resolve_count >= kMaxLinksToResolve) 73 if (resolve_count >= kMaxLinksToResolve)
74 result.clear(); 74 result.clear();
75 return result; 75 return result;
76 } 76 }
77 77
78 // The callback passed to FSEventStreamCreate(). 78 } // namespace
79 void FSEventsCallback(ConstFSEventStreamRef stream, 79
80 void* event_watcher, size_t num_events, 80 FilePathWatcherFSEvents::FilePathWatcherFSEvents() : fsevent_stream_(NULL) {
81 void* event_paths, const FSEventStreamEventFlags flags[], 81 }
82 const FSEventStreamEventId event_ids[]) { 82
83 bool FilePathWatcherFSEvents::Watch(const FilePath& path,
84 bool recursive,
85 const FilePathWatcher::Callback& callback) {
86 DCHECK(MessageLoopForIO::current());
87 DCHECK(!callback.is_null());
88 DCHECK(callback_.is_null());
89
90 // This class could support non-recursive watches, but that is currently
91 // left to FilePathWatcherKQueue.
92 if (!recursive)
93 return false;
94
95 set_message_loop(MessageLoopProxy::current());
96 callback_ = callback;
97
98 FSEventStreamEventId start_event = FSEventsGetCurrentEventId();
99 g_task_runner.Get().PostTask(
100 FROM_HERE, Bind(&FilePathWatcherFSEvents::StartEventStream, this,
101 start_event, path));
102 return true;
103 }
104
105 void FilePathWatcherFSEvents::Cancel() {
106 set_cancelled();
107 callback_.Reset();
108
109 // Switch to the dispatch queue thread to tear down the event stream.
110 g_task_runner.Get().PostTask(
111 FROM_HERE,
112 Bind(&FilePathWatcherFSEvents::CancelOnMessageLoopThread, this));
113 }
114
115 // static
116 void FilePathWatcherFSEvents::FSEventsCallback(
117 ConstFSEventStreamRef stream,
118 void* event_watcher,
119 size_t num_events,
120 void* event_paths,
121 const FSEventStreamEventFlags flags[],
122 const FSEventStreamEventId event_ids[]) {
83 FilePathWatcherFSEvents* watcher = 123 FilePathWatcherFSEvents* watcher =
84 reinterpret_cast<FilePathWatcherFSEvents*>(event_watcher); 124 reinterpret_cast<FilePathWatcherFSEvents*>(event_watcher);
85 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); 125 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread());
86 126
87 bool root_changed = watcher->ResolveTargetPath(); 127 bool root_changed = watcher->ResolveTargetPath();
88 std::vector<FilePath> paths; 128 std::vector<FilePath> paths;
89 FSEventStreamEventId root_change_at = FSEventStreamGetLatestEventId(stream); 129 FSEventStreamEventId root_change_at = FSEventStreamGetLatestEventId(stream);
90 for (size_t i = 0; i < num_events; i++) { 130 for (size_t i = 0; i < num_events; i++) {
91 if (flags[i] & kFSEventStreamEventFlagRootChanged) 131 if (flags[i] & kFSEventStreamEventFlagRootChanged)
92 root_changed = true; 132 root_changed = true;
(...skipping 11 matching lines...) Expand all
104 // bad file descriptor errors), so post a task to do the reset. 144 // bad file descriptor errors), so post a task to do the reset.
105 g_task_runner.Get().PostTask( 145 g_task_runner.Get().PostTask(
106 FROM_HERE, 146 FROM_HERE,
107 Bind(&FilePathWatcherFSEvents::UpdateEventStream, watcher, 147 Bind(&FilePathWatcherFSEvents::UpdateEventStream, watcher,
108 root_change_at)); 148 root_change_at));
109 } 149 }
110 150
111 watcher->OnFilePathsChanged(paths); 151 watcher->OnFilePathsChanged(paths);
112 } 152 }
113 153
114 } // namespace 154 FilePathWatcherFSEvents::~FilePathWatcherFSEvents() {
115 155 DCHECK(callback_.is_null())
Mattias Nissler (ping if slow) 2015/04/10 09:15:51 Now, if the object gets destroyed on the g_task_ru
Reilly Grant (use Gerrit) 2015/04/10 18:32:46 Reference counting doesn't provide a sufficient me
116 FilePathWatcherFSEvents::FilePathWatcherFSEvents() : fsevent_stream_(NULL) { 156 << "Cancel() must be called before FilePathWatcher is destroyed.";
117 } 157 }
118 158
119 void FilePathWatcherFSEvents::OnFilePathsChanged( 159 void FilePathWatcherFSEvents::OnFilePathsChanged(
120 const std::vector<FilePath>& paths) { 160 const std::vector<FilePath>& paths) {
Mattias Nissler (ping if slow) 2015/04/10 09:15:51 nit: DCHECK(g_task_runner.Get().RunsTasksOnCurrent
Reilly Grant (use Gerrit) 2015/04/10 18:32:46 Done.
121 if (!message_loop()->BelongsToCurrentThread()) { 161 DCHECK(!resolved_target_.empty());
122 message_loop()->PostTask( 162 message_loop()->PostTask(
123 FROM_HERE, 163 FROM_HERE, Bind(&FilePathWatcherFSEvents::DispatchEvents, this, paths,
124 Bind(&FilePathWatcherFSEvents::OnFilePathsChanged, this, paths)); 164 target_, resolved_target_));
165 }
166
167 void FilePathWatcherFSEvents::DispatchEvents(const std::vector<FilePath>& paths,
168 const FilePath& target,
169 const FilePath& resolved_target) {
170 DCHECK(message_loop()->RunsTasksOnCurrentThread());
171
172 // Don't issue callbacks after Cancel() has been called.
173 if (is_cancelled() || callback_.is_null()) {
125 return; 174 return;
126 } 175 }
127 176
128 DCHECK(message_loop()->BelongsToCurrentThread()); 177 for (const FilePath& path : paths) {
129 if (resolved_target_.empty()) 178 if (resolved_target.IsParent(path) || resolved_target == path) {
130 return; 179 callback_.Run(target, false);
131
132 for (size_t i = 0; i < paths.size(); i++) {
133 if (resolved_target_.IsParent(paths[i]) || resolved_target_ == paths[i]) {
134 callback_.Run(target_, false);
135 return; 180 return;
136 } 181 }
137 } 182 }
138 } 183 }
139 184
140 bool FilePathWatcherFSEvents::Watch(const FilePath& path,
141 bool recursive,
142 const FilePathWatcher::Callback& callback) {
143 DCHECK(resolved_target_.empty());
144 DCHECK(MessageLoopForIO::current());
145 DCHECK(!callback.is_null());
146
147 // This class could support non-recursive watches, but that is currently
148 // left to FilePathWatcherKQueue.
149 if (!recursive)
150 return false;
151
152 set_message_loop(MessageLoopProxy::current());
153 callback_ = callback;
154 target_ = path;
155
156 FSEventStreamEventId start_event = FSEventsGetCurrentEventId();
157 g_task_runner.Get().PostTask(
158 FROM_HERE,
159 Bind(&FilePathWatcherFSEvents::StartEventStream, this, start_event));
160 return true;
161 }
162
163 void FilePathWatcherFSEvents::Cancel() {
164 if (callback_.is_null()) {
165 // Watch was never called, so exit.
166 set_cancelled();
167 return;
168 }
169
170 // Switch to the dispatch queue thread if necessary, so we can tear down
171 // the event stream.
172 if (!g_task_runner.Get().RunsTasksOnCurrentThread()) {
173 g_task_runner.Get().PostTask(
174 FROM_HERE,
175 Bind(&FilePathWatcherFSEvents::CancelOnMessageLoopThread, this));
176 } else {
177 CancelOnMessageLoopThread();
178 }
179 }
180
181 void FilePathWatcherFSEvents::CancelOnMessageLoopThread() { 185 void FilePathWatcherFSEvents::CancelOnMessageLoopThread() {
182 // For all other implementations, the "message loop thread" is the IO thread, 186 // For all other implementations, the "message loop thread" is the IO thread,
183 // as returned by message_loop(). This implementation, however, needs to 187 // as returned by message_loop(). This implementation, however, needs to
184 // cancel pending work on the Dipatch Queue thread. 188 // cancel pending work on the Dispatch Queue thread.
185 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); 189 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread());
186 190
187 set_cancelled();
188 if (fsevent_stream_) { 191 if (fsevent_stream_) {
189 DestroyEventStream(); 192 DestroyEventStream();
190 callback_.Reset();
191 target_.clear(); 193 target_.clear();
192 resolved_target_.clear(); 194 resolved_target_.clear();
193 } 195 }
194 } 196 }
195 197
196 void FilePathWatcherFSEvents::UpdateEventStream( 198 void FilePathWatcherFSEvents::UpdateEventStream(
197 FSEventStreamEventId start_event) { 199 FSEventStreamEventId start_event) {
198 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); 200 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread());
199 201
200 // It can happen that the watcher gets canceled while tasks that call this 202 // It can happen that the watcher gets canceled while tasks that call this
201 // function are still in flight, so abort if this situation is detected. 203 // function are still in flight, so abort if this situation is detected.
202 if (is_cancelled() || resolved_target_.empty()) 204 if (resolved_target_.empty())
203 return; 205 return;
204 206
205 if (fsevent_stream_) 207 if (fsevent_stream_)
206 DestroyEventStream(); 208 DestroyEventStream();
207 209
208 ScopedCFTypeRef<CFStringRef> cf_path(CFStringCreateWithCString( 210 ScopedCFTypeRef<CFStringRef> cf_path(CFStringCreateWithCString(
209 NULL, resolved_target_.value().c_str(), kCFStringEncodingMacHFS)); 211 NULL, resolved_target_.value().c_str(), kCFStringEncodingMacHFS));
210 ScopedCFTypeRef<CFStringRef> cf_dir_path(CFStringCreateWithCString( 212 ScopedCFTypeRef<CFStringRef> cf_dir_path(CFStringCreateWithCString(
211 NULL, resolved_target_.DirName().value().c_str(), 213 NULL, resolved_target_.DirName().value().c_str(),
212 kCFStringEncodingMacHFS)); 214 kCFStringEncodingMacHFS));
(...skipping 10 matching lines...) Expand all
223 context.copyDescription = NULL; 225 context.copyDescription = NULL;
224 226
225 fsevent_stream_ = FSEventStreamCreate(NULL, &FSEventsCallback, &context, 227 fsevent_stream_ = FSEventStreamCreate(NULL, &FSEventsCallback, &context,
226 watched_paths, 228 watched_paths,
227 start_event, 229 start_event,
228 kEventLatencySeconds, 230 kEventLatencySeconds,
229 kFSEventStreamCreateFlagWatchRoot); 231 kFSEventStreamCreateFlagWatchRoot);
230 FSEventStreamSetDispatchQueue(fsevent_stream_, 232 FSEventStreamSetDispatchQueue(fsevent_stream_,
231 g_task_runner.Get().GetDispatchQueue()); 233 g_task_runner.Get().GetDispatchQueue());
232 234
233 if (!FSEventStreamStart(fsevent_stream_)) 235 if (!FSEventStreamStart(fsevent_stream_)) {
234 message_loop()->PostTask(FROM_HERE, Bind(callback_, target_, true)); 236 message_loop()->PostTask(
237 FROM_HERE, Bind(&FilePathWatcherFSEvents::ReportError, this, target_));
238 }
235 } 239 }
236 240
237 bool FilePathWatcherFSEvents::ResolveTargetPath() { 241 bool FilePathWatcherFSEvents::ResolveTargetPath() {
238 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); 242 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread());
239 FilePath resolved = ResolvePath(target_).StripTrailingSeparators(); 243 FilePath resolved = ResolvePath(target_).StripTrailingSeparators();
240 bool changed = resolved != resolved_target_; 244 bool changed = resolved != resolved_target_;
241 resolved_target_ = resolved; 245 resolved_target_ = resolved;
242 if (resolved_target_.empty()) 246 if (resolved_target_.empty()) {
243 message_loop()->PostTask(FROM_HERE, Bind(callback_, target_, true)); 247 message_loop()->PostTask(
248 FROM_HERE, Bind(&FilePathWatcherFSEvents::ReportError, this, target_));
249 }
244 return changed; 250 return changed;
245 } 251 }
246 252
253 void FilePathWatcherFSEvents::ReportError(const FilePath& target) {
Mattias Nissler (ping if slow) 2015/04/10 09:15:51 nit: DCHECK(message_loop()->RunsTasksOnCurrentThre
Reilly Grant (use Gerrit) 2015/04/10 18:32:46 Done.
254 if (!callback_.is_null()) {
255 callback_.Run(target, true);
256 }
257 }
258
247 void FilePathWatcherFSEvents::DestroyEventStream() { 259 void FilePathWatcherFSEvents::DestroyEventStream() {
248 FSEventStreamStop(fsevent_stream_); 260 FSEventStreamStop(fsevent_stream_);
249 FSEventStreamInvalidate(fsevent_stream_); 261 FSEventStreamInvalidate(fsevent_stream_);
250 FSEventStreamRelease(fsevent_stream_); 262 FSEventStreamRelease(fsevent_stream_);
251 fsevent_stream_ = NULL; 263 fsevent_stream_ = NULL;
252 } 264 }
253 265
254 void FilePathWatcherFSEvents::StartEventStream( 266 void FilePathWatcherFSEvents::StartEventStream(FSEventStreamEventId start_event,
255 FSEventStreamEventId start_event) { 267 const FilePath& path) {
256 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread()); 268 DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread());
269 DCHECK(resolved_target_.empty());
270
271 target_ = path;
257 ResolveTargetPath(); 272 ResolveTargetPath();
258 UpdateEventStream(start_event); 273 UpdateEventStream(start_event);
259 } 274 }
260 275
261 FilePathWatcherFSEvents::~FilePathWatcherFSEvents() {
262 DCHECK(!fsevent_stream_)
263 << "File path watcher destroyed before event stream.";
264 }
265
266 } // namespace base 276 } // namespace base
OLDNEW
« base/files/file_path_watcher_fsevents.h ('K') | « base/files/file_path_watcher_fsevents.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698