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