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

Side by Side Diff: chrome/browser/file_path_watcher_mac.cc

Issue 5606002: Move:... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 10 years 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/file_path_watcher_inotify.cc ('k') | chrome/browser/file_path_watcher_stub.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2009 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 #include "chrome/browser/file_path_watcher.h"
6
7 #include <CoreServices/CoreServices.h>
8 #include <set>
9
10 #include "base/file_path.h"
11 #include "base/file_util.h"
12 #include "base/logging.h"
13 #include "base/mac/scoped_cftyperef.h"
14 #include "base/singleton.h"
15 #include "base/time.h"
16
17 namespace {
18
19 // The latency parameter passed to FSEventsStreamCreate().
20 const CFAbsoluteTime kEventLatencySeconds = 0.3;
21
22 // Mac-specific file watcher implementation based on the FSEvents API.
23 class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate {
24 public:
25 FilePathWatcherImpl();
26 virtual ~FilePathWatcherImpl() {}
27
28 // Called from the FSEvents callback whenever there is a change to the paths
29 void OnFilePathChanged();
30
31 // (Re-)Initialize the event stream to start reporting events from
32 // |start_event|.
33 void UpdateEventStream(FSEventStreamEventId start_event);
34
35 // FilePathWatcher::PlatformDelegate overrides.
36 virtual bool Watch(const FilePath& path, FilePathWatcher::Delegate* delegate);
37 virtual void Cancel();
38
39 private:
40 // Destroy the event stream.
41 void DestroyEventStream();
42
43 // Delegate to notify upon changes.
44 scoped_refptr<FilePathWatcher::Delegate> delegate_;
45
46 // Target path to watch (passed to delegate).
47 FilePath target_;
48
49 // Keep track of the last modified time of the file. We use nulltime
50 // to represent the file not existing.
51 base::Time last_modified_;
52
53 // The time at which we processed the first notification with the
54 // |last_modified_| time stamp.
55 base::Time first_notification_;
56
57 // Backend stream we receive event callbacks from (strong reference).
58 FSEventStreamRef fsevent_stream_;
59
60 // Used to detect early cancellation.
61 bool canceled_;
62
63 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl);
64 };
65
66 // The callback passed to FSEventStreamCreate().
67 void FSEventsCallback(ConstFSEventStreamRef stream,
68 void* event_watcher, size_t num_events,
69 void* event_paths, const FSEventStreamEventFlags flags[],
70 const FSEventStreamEventId event_ids[]) {
71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
72
73 FilePathWatcherImpl* watcher =
74 reinterpret_cast<FilePathWatcherImpl*>(event_watcher);
75 bool root_changed = false;
76 FSEventStreamEventId root_change_at = FSEventStreamGetLatestEventId(stream);
77 for (size_t i = 0; i < num_events; i++) {
78 if (flags[i] & kFSEventStreamEventFlagRootChanged)
79 root_changed = true;
80 if (event_ids[i])
81 root_change_at = std::min(root_change_at, event_ids[i]);
82 }
83
84 // Reinitialize the event stream if we find changes to the root. This is
85 // necessary since FSEvents doesn't report any events for the subtree after
86 // the directory to be watched gets created.
87 if (root_changed) {
88 // Resetting the event stream from within the callback fails (FSEvents spews
89 // bad file descriptor errors), so post a task to do the reset.
90 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
91 NewRunnableMethod(watcher, &FilePathWatcherImpl::UpdateEventStream,
92 root_change_at));
93 }
94
95 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
96 NewRunnableMethod(watcher, &FilePathWatcherImpl::OnFilePathChanged));
97 }
98
99 // FilePathWatcherImpl implementation:
100
101 FilePathWatcherImpl::FilePathWatcherImpl()
102 : fsevent_stream_(NULL),
103 canceled_(false) {
104 }
105
106 void FilePathWatcherImpl::OnFilePathChanged() {
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
108 DCHECK(!target_.empty());
109
110 base::PlatformFileInfo file_info;
111 bool file_exists = file_util::GetFileInfo(target_, &file_info);
112 if (file_exists && (last_modified_.is_null() ||
113 last_modified_ != file_info.last_modified)) {
114 last_modified_ = file_info.last_modified;
115 first_notification_ = base::Time::Now();
116 delegate_->OnFilePathChanged(target_);
117 } else if (file_exists && !first_notification_.is_null()) {
118 // The target's last modification time is equal to what's on record. This
119 // means that either an unrelated event occurred, or the target changed
120 // again (file modification times only have a resolution of 1s). Comparing
121 // file modification times against the wall clock is not reliable to find
122 // out whether the change is recent, since this code might just run too
123 // late. Moreover, there's no guarantee that file modification time and wall
124 // clock times come from the same source.
125 //
126 // Instead, the time at which the first notification carrying the current
127 // |last_notified_| time stamp is recorded. Later notifications that find
128 // the same file modification time only need to be forwarded until wall
129 // clock has advanced one second from the initial notification. After that
130 // interval, client code is guaranteed to having seen the current revision
131 // of the file.
132 if (base::Time::Now() - first_notification_ >
133 base::TimeDelta::FromSeconds(1)) {
134 // Stop further notifications for this |last_modification_| time stamp.
135 first_notification_ = base::Time();
136 }
137 delegate_->OnFilePathChanged(target_);
138 } else if (!file_exists && !last_modified_.is_null()) {
139 last_modified_ = base::Time();
140 delegate_->OnFilePathChanged(target_);
141 }
142 }
143
144 bool FilePathWatcherImpl::Watch(const FilePath& path,
145 FilePathWatcher::Delegate* delegate) {
146 DCHECK(target_.value().empty());
147
148 target_ = path;
149 delegate_ = delegate;
150
151 FSEventStreamEventId start_event = FSEventsGetCurrentEventId();
152
153 base::PlatformFileInfo file_info;
154 if (file_util::GetFileInfo(target_, &file_info)) {
155 last_modified_ = file_info.last_modified;
156 first_notification_ = base::Time::Now();
157 }
158
159 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
160 NewRunnableMethod(this, &FilePathWatcherImpl::UpdateEventStream,
161 start_event));
162
163 return true;
164 }
165
166 void FilePathWatcherImpl::Cancel() {
167 // Switch to the UI thread if necessary, so we can tear down the event stream.
168 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
169 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
170 NewRunnableMethod(this, &FilePathWatcherImpl::Cancel));
171 return;
172 }
173
174 canceled_ = true;
175 if (fsevent_stream_)
176 DestroyEventStream();
177 }
178
179 void FilePathWatcherImpl::UpdateEventStream(FSEventStreamEventId start_event) {
180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
181
182 // It can happen that the watcher gets canceled while tasks that call this
183 // function are still in flight, so abort if this situation is detected.
184 if (canceled_)
185 return;
186
187 if (fsevent_stream_)
188 DestroyEventStream();
189
190 base::mac::ScopedCFTypeRef<CFStringRef> cf_path(CFStringCreateWithCString(
191 NULL, target_.value().c_str(), kCFStringEncodingMacHFS));
192 base::mac::ScopedCFTypeRef<CFStringRef> cf_dir_path(CFStringCreateWithCString(
193 NULL, target_.DirName().value().c_str(), kCFStringEncodingMacHFS));
194 CFStringRef paths_array[] = { cf_path.get(), cf_dir_path.get() };
195 base::mac::ScopedCFTypeRef<CFArrayRef> watched_paths(CFArrayCreate(
196 NULL, reinterpret_cast<const void**>(paths_array), arraysize(paths_array),
197 &kCFTypeArrayCallBacks));
198
199 FSEventStreamContext context;
200 context.version = 0;
201 context.info = this;
202 context.retain = NULL;
203 context.release = NULL;
204 context.copyDescription = NULL;
205
206 fsevent_stream_ = FSEventStreamCreate(NULL, &FSEventsCallback, &context,
207 watched_paths,
208 start_event,
209 kEventLatencySeconds,
210 kFSEventStreamCreateFlagWatchRoot);
211 FSEventStreamScheduleWithRunLoop(fsevent_stream_, CFRunLoopGetCurrent(),
212 kCFRunLoopDefaultMode);
213 if (!FSEventStreamStart(fsevent_stream_)) {
214 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
215 NewRunnableMethod(delegate_.get(),
216 &FilePathWatcher::Delegate::OnError));
217 }
218 }
219
220 void FilePathWatcherImpl::DestroyEventStream() {
221 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
222 FSEventStreamStop(fsevent_stream_);
223 FSEventStreamInvalidate(fsevent_stream_);
224 FSEventStreamRelease(fsevent_stream_);
225 fsevent_stream_ = NULL;
226 }
227
228 } // namespace
229
230 FilePathWatcher::FilePathWatcher() {
231 impl_ = new FilePathWatcherImpl();
232 }
OLDNEW
« no previous file with comments | « chrome/browser/file_path_watcher_inotify.cc ('k') | chrome/browser/file_path_watcher_stub.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698