From 5199d74e9d09b9a0b3ab9f600fcc96a54a5902ba Mon Sep 17 00:00:00 2001 From: "mnissler@chromium.org" Date: Thu, 19 Aug 2010 10:35:46 +0000 Subject: Reland 56341 - Add support for watching directories to FileWatcher. BUG=none TEST=Unit tests in file_watcher_unittest.cc. Review URL: http://codereview.chromium.org/3184010 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@56670 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/browser/file_path_watcher.h | 70 ++++ chrome/browser/file_path_watcher_inotify.cc | 410 +++++++++++++++++++++++ chrome/browser/file_path_watcher_mac.cc | 232 +++++++++++++ chrome/browser/file_path_watcher_stub.cc | 19 ++ chrome/browser/file_path_watcher_unittest.cc | 465 +++++++++++++++++++++++++++ chrome/browser/file_path_watcher_win.cc | 243 ++++++++++++++ chrome/browser/file_watcher.h | 66 ---- chrome/browser/file_watcher_inotify.cc | 302 ----------------- chrome/browser/file_watcher_mac.cc | 157 --------- chrome/browser/file_watcher_stub.cc | 19 -- chrome/browser/file_watcher_unittest.cc | 242 -------------- chrome/browser/file_watcher_win.cc | 111 ------- chrome/browser/user_style_sheet_watcher.cc | 38 +-- chrome/browser/user_style_sheet_watcher.h | 4 +- chrome/chrome_browser.gypi | 12 +- chrome/chrome_tests.gypi | 2 +- 16 files changed, 1467 insertions(+), 925 deletions(-) create mode 100644 chrome/browser/file_path_watcher.h create mode 100644 chrome/browser/file_path_watcher_inotify.cc create mode 100644 chrome/browser/file_path_watcher_mac.cc create mode 100644 chrome/browser/file_path_watcher_stub.cc create mode 100644 chrome/browser/file_path_watcher_unittest.cc create mode 100644 chrome/browser/file_path_watcher_win.cc delete mode 100644 chrome/browser/file_watcher.h delete mode 100644 chrome/browser/file_watcher_inotify.cc delete mode 100644 chrome/browser/file_watcher_mac.cc delete mode 100644 chrome/browser/file_watcher_stub.cc delete mode 100644 chrome/browser/file_watcher_unittest.cc delete mode 100644 chrome/browser/file_watcher_win.cc diff --git a/chrome/browser/file_path_watcher.h b/chrome/browser/file_path_watcher.h new file mode 100644 index 0000000..b8c857f --- /dev/null +++ b/chrome/browser/file_path_watcher.h @@ -0,0 +1,70 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// This module provides a way to monitor a file or directory for changes. + +#ifndef CHROME_BROWSER_FILE_PATH_WATCHER_H_ +#define CHROME_BROWSER_FILE_PATH_WATCHER_H_ +#pragma once + +#include "base/basictypes.h" +#include "base/file_path.h" +#include "base/ref_counted.h" +#include "chrome/browser/chrome_thread.h" + +// This class lets you register interest in changes on a FilePath. +// The delegate will get called whenever the file or directory referenced by the +// FilePath is changed, including created or deleted. Due to limitations in the +// underlying OS APIs, spurious notifications might occur that don't relate to +// an actual change to the watch target. +class FilePathWatcher { + public: + // Declares the callback client code implements to receive notifications. Note + // that implementations of this interface should not keep a reference to the + // corresponding FileWatcher object to prevent a reference cycle. + class Delegate : public base::RefCountedThreadSafe { + public: + virtual ~Delegate() {} + virtual void OnFilePathChanged(const FilePath& path) = 0; + // Called when platform specific code detected an error. The watcher will + // not call OnFilePathChanged for future changes. + virtual void OnError() {} + }; + + FilePathWatcher(); + ~FilePathWatcher() { + impl_->Cancel(); + } + + // Register interest in any changes on |path|. OnPathChanged will be called + // back for each change. Returns true on success. + bool Watch(const FilePath& path, Delegate* delegate) WARN_UNUSED_RESULT { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); + DCHECK(path.IsAbsolute()); + return impl_->Watch(path, delegate); + } + + // Used internally to encapsulate different members on different platforms. + class PlatformDelegate + : public base::RefCountedThreadSafe { + public: + virtual ~PlatformDelegate() {} + + // Start watching for the given |path| and notify |delegate| about changes. + virtual bool Watch(const FilePath& path, Delegate* delegate) + WARN_UNUSED_RESULT = 0; + + // Stop watching. This is called from FilePathWatcher's dtor in order to + // allow to shut down properly while the object is still alive. + virtual void Cancel() {} + }; + + private: + scoped_refptr impl_; + + DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); +}; + +#endif // CHROME_BROWSER_FILE_PATH_WATCHER_H_ diff --git a/chrome/browser/file_path_watcher_inotify.cc b/chrome/browser/file_path_watcher_inotify.cc new file mode 100644 index 0000000..744e9a1 --- /dev/null +++ b/chrome/browser/file_path_watcher_inotify.cc @@ -0,0 +1,410 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/file_path_watcher.h" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "base/eintr_wrapper.h" +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/hash_tables.h" +#include "base/lock.h" +#include "base/logging.h" +#include "base/message_loop.h" +#include "base/scoped_ptr.h" +#include "base/singleton.h" +#include "base/task.h" +#include "base/thread.h" + +namespace { + +class FilePathWatcherImpl; + +// Singleton to manage all inotify watches. +// TODO(tony): It would be nice if this wasn't a singleton. +// http://crbug.com/38174 +class InotifyReader { + public: + typedef int Watch; // Watch descriptor used by AddWatch and RemoveWatch. + static const Watch kInvalidWatch = -1; + + // Watch directory |path| for changes. |watcher| will be notified on each + // change. Returns kInvalidWatch on failure. + Watch AddWatch(const FilePath& path, FilePathWatcherImpl* watcher); + + // Remove |watch|. Returns true on success. + bool RemoveWatch(Watch watch, FilePathWatcherImpl* watcher); + + // Callback for InotifyReaderTask. + void OnInotifyEvent(const inotify_event* event); + + private: + friend struct DefaultSingletonTraits; + + typedef std::set WatcherSet; + + InotifyReader(); + ~InotifyReader(); + + // We keep track of which delegates want to be notified on which watches. + base::hash_map watchers_; + + // Lock to protect watchers_. + Lock lock_; + + // Separate thread on which we run blocking read for inotify events. + base::Thread thread_; + + // File descriptor returned by inotify_init. + const int inotify_fd_; + + // Use self-pipe trick to unblock select during shutdown. + int shutdown_pipe_[2]; + + // Flag set to true when startup was successful. + bool valid_; + + DISALLOW_COPY_AND_ASSIGN(InotifyReader); +}; + +class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate { + public: + FilePathWatcherImpl(); + virtual ~FilePathWatcherImpl() {} + + // Called for each event coming from the watch. |fired_watch| identifies the + // watch that fired, |child| indicates what has changed, and is relative to + // the currently watched path for |fired_watch|. The flag |created| is true if + // the object appears, and |is_directory| is set when the event refers to a + // directory. + void OnFilePathChanged(InotifyReader::Watch fired_watch, + const FilePath::StringType& child, + bool created, + bool is_directory); + + // Start watching |path| for changes and notify |delegate| on each change. + // Returns true if watch for |path| has been added successfully. + virtual bool Watch(const FilePath& path, FilePathWatcher::Delegate* delegate); + + // Cancel the watch. This unregisters the instance with InotifyReader. + virtual void Cancel(); + + private: + // Inotify watches are installed for all directory components of |target_|. A + // WatchEntry instance holds the watch descriptor for a component and the + // subdirectory for that identifies the next component. + struct WatchEntry { + WatchEntry(InotifyReader::Watch watch, const FilePath::StringType& subdir) + : watch_(watch), + subdir_(subdir) {} + + InotifyReader::Watch watch_; + FilePath::StringType subdir_; + }; + typedef std::vector WatchVector; + + // Reconfigure to watch for the most specific parent directory of |target_| + // that exists. Updates |watched_path_|. Returns true on success. + bool UpdateWatches() WARN_UNUSED_RESULT; + + // Delegate to notify upon changes. + scoped_refptr delegate_; + + // The file or directory we're supposed to watch. + FilePath target_; + + // The vector of watches and next component names for all path components, + // starting at the root directory. The last entry corresponds to the watch for + // |target_| and always stores an empty next component name in |subdir_|. + WatchVector watches_; + + DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); +}; + +class InotifyReaderTask : public Task { + public: + InotifyReaderTask(InotifyReader* reader, int inotify_fd, int shutdown_fd) + : reader_(reader), + inotify_fd_(inotify_fd), + shutdown_fd_(shutdown_fd) { + } + + virtual void Run() { + while (true) { + fd_set rfds; + FD_ZERO(&rfds); + FD_SET(inotify_fd_, &rfds); + FD_SET(shutdown_fd_, &rfds); + + // Wait until some inotify events are available. + int select_result = + HANDLE_EINTR(select(std::max(inotify_fd_, shutdown_fd_) + 1, + &rfds, NULL, NULL, NULL)); + if (select_result < 0) { + DPLOG(WARNING) << "select failed"; + return; + } + + if (FD_ISSET(shutdown_fd_, &rfds)) + return; + + // Adjust buffer size to current event queue size. + int buffer_size; + int ioctl_result = HANDLE_EINTR(ioctl(inotify_fd_, FIONREAD, + &buffer_size)); + + if (ioctl_result != 0) { + DPLOG(WARNING) << "ioctl failed"; + return; + } + + std::vector buffer(buffer_size); + + ssize_t bytes_read = HANDLE_EINTR(read(inotify_fd_, &buffer[0], + buffer_size)); + + if (bytes_read < 0) { + DPLOG(WARNING) << "read from inotify fd failed"; + return; + } + + ssize_t i = 0; + while (i < bytes_read) { + inotify_event* event = reinterpret_cast(&buffer[i]); + size_t event_size = sizeof(inotify_event) + event->len; + DCHECK(i + event_size <= static_cast(bytes_read)); + reader_->OnInotifyEvent(event); + i += event_size; + } + } + } + + private: + InotifyReader* reader_; + int inotify_fd_; + int shutdown_fd_; + + DISALLOW_COPY_AND_ASSIGN(InotifyReaderTask); +}; + +InotifyReader::InotifyReader() + : thread_("inotify_reader"), + inotify_fd_(inotify_init()), + valid_(false) { + shutdown_pipe_[0] = -1; + shutdown_pipe_[1] = -1; + if (inotify_fd_ >= 0 && pipe(shutdown_pipe_) == 0 && thread_.Start()) { + thread_.message_loop()->PostTask( + FROM_HERE, new InotifyReaderTask(this, inotify_fd_, shutdown_pipe_[0])); + valid_ = true; + } +} + +InotifyReader::~InotifyReader() { + if (valid_) { + // Write to the self-pipe so that the select call in InotifyReaderTask + // returns. + ssize_t ret = HANDLE_EINTR(write(shutdown_pipe_[1], "", 1)); + DPCHECK(ret > 0); + DCHECK_EQ(ret, 1); + thread_.Stop(); + } + if (inotify_fd_ >= 0) + close(inotify_fd_); + if (shutdown_pipe_[0] >= 0) + close(shutdown_pipe_[0]); + if (shutdown_pipe_[1] >= 0) + close(shutdown_pipe_[1]); +} + +InotifyReader::Watch InotifyReader::AddWatch( + const FilePath& path, FilePathWatcherImpl* watcher) { + if (!valid_) + return kInvalidWatch; + + AutoLock auto_lock(lock_); + + Watch watch = inotify_add_watch(inotify_fd_, path.value().c_str(), + IN_CREATE | IN_DELETE | + IN_CLOSE_WRITE | IN_MOVE | + IN_ONLYDIR); + + if (watch == kInvalidWatch) + return kInvalidWatch; + + watchers_[watch].insert(watcher); + + return watch; +} + +bool InotifyReader::RemoveWatch(Watch watch, + FilePathWatcherImpl* watcher) { + if (!valid_) + return false; + + AutoLock auto_lock(lock_); + + watchers_[watch].erase(watcher); + + if (watchers_[watch].empty()) { + watchers_.erase(watch); + return (inotify_rm_watch(inotify_fd_, watch) == 0); + } + + return true; +} + +void InotifyReader::OnInotifyEvent(const inotify_event* event) { + if (event->mask & IN_IGNORED) + return; + + FilePath::StringType child(event->len ? event->name : FILE_PATH_LITERAL("")); + AutoLock auto_lock(lock_); + + for (WatcherSet::iterator watcher = watchers_[event->wd].begin(); + watcher != watchers_[event->wd].end(); + ++watcher) { + ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, + NewRunnableMethod(*watcher, + &FilePathWatcherImpl::OnFilePathChanged, + event->wd, + child, + event->mask & (IN_CREATE | IN_MOVED_TO), + event->mask & IN_ISDIR)); + } +} + +FilePathWatcherImpl::FilePathWatcherImpl() + : delegate_(NULL) { +} + +void FilePathWatcherImpl::OnFilePathChanged(InotifyReader::Watch fired_watch, + const FilePath::StringType& child, + bool created, + bool is_directory) { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); + + // Find the entry in |watches_| that corresponds to |fired_watch|. + WatchVector::const_iterator watch_entry(watches_.begin()); + for ( ; watch_entry != watches_.end(); ++watch_entry) { + if (fired_watch == watch_entry->watch_) + break; + } + + // If this notification is from a previous generation of watches or the watch + // has been cancelled (|watches_| is empty then), bail out. + if (watch_entry == watches_.end()) + return; + + // Check whether a path component of |target_| changed. + bool change_on_target_path = child.empty() || child == watch_entry->subdir_; + + // Check whether the change references |target_| or a direct child. + DCHECK(watch_entry->subdir_.empty() || (watch_entry + 1) != watches_.end()); + bool target_changed = watch_entry->subdir_.empty() || + (watch_entry->subdir_ == child && (++watch_entry)->subdir_.empty()); + + // Update watches if a directory component of the |target_| path (dis)appears. + if (is_directory && change_on_target_path && !UpdateWatches()) { + delegate_->OnError(); + return; + } + + // Report the following events: + // - The target or a direct child of the target got changed (in case the + // watched path refers to a directory). + // - One of the parent directories got moved or deleted, since the target + // disappears in this case. + // - One of the parent directories appears. The event corresponding to the + // target appearing might have been missed in this case, so recheck. + if (target_changed || + (change_on_target_path && !created) || + (change_on_target_path && file_util::PathExists(target_))) { + delegate_->OnFilePathChanged(target_); + } +} + +bool FilePathWatcherImpl::Watch(const FilePath& path, + FilePathWatcher::Delegate* delegate) { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); + DCHECK(target_.empty()); + + delegate_ = delegate; + target_ = path; + std::vector comps; + target_.GetComponents(&comps); + DCHECK(!comps.empty()); + for (std::vector::const_iterator comp(++comps.begin()); + comp != comps.end(); ++comp) { + watches_.push_back(WatchEntry(InotifyReader::kInvalidWatch, *comp)); + } + watches_.push_back(WatchEntry(InotifyReader::kInvalidWatch, + FilePath::StringType())); + return UpdateWatches(); +} + +void FilePathWatcherImpl::Cancel() { + // Switch to the file thread if necessary so we can access |watches_|. + if (!ChromeThread::CurrentlyOn(ChromeThread::FILE)) { + ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, + NewRunnableMethod(this, &FilePathWatcherImpl::Cancel)); + return; + } + + for (WatchVector::iterator watch_entry(watches_.begin()); + watch_entry != watches_.end(); ++watch_entry) { + if (watch_entry->watch_ != InotifyReader::kInvalidWatch) + Singleton::get()->RemoveWatch(watch_entry->watch_, this); + } + watches_.clear(); + delegate_ = NULL; + target_.clear(); +} + +bool FilePathWatcherImpl::UpdateWatches() { + // Ensure this runs on the file thread exclusively in order to avoid + // concurrency issues. + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); + + // Walk the list of watches and update them as we go. + FilePath path(FILE_PATH_LITERAL("/")); + bool path_valid = true; + for (WatchVector::iterator watch_entry(watches_.begin()); + watch_entry != watches_.end(); ++watch_entry) { + InotifyReader::Watch old_watch = watch_entry->watch_; + if (path_valid) { + watch_entry->watch_ = + Singleton::get()->AddWatch(path, this); + if (watch_entry->watch_ == InotifyReader::kInvalidWatch) { + path_valid = false; + } + } else { + watch_entry->watch_ = InotifyReader::kInvalidWatch; + } + if (old_watch != InotifyReader::kInvalidWatch && + old_watch != watch_entry->watch_) { + Singleton::get()->RemoveWatch(old_watch, this); + } + path = path.Append(watch_entry->subdir_); + } + + return true; +} + +} // namespace + +FilePathWatcher::FilePathWatcher() { + impl_ = new FilePathWatcherImpl(); +} diff --git a/chrome/browser/file_path_watcher_mac.cc b/chrome/browser/file_path_watcher_mac.cc new file mode 100644 index 0000000..91d9c23 --- /dev/null +++ b/chrome/browser/file_path_watcher_mac.cc @@ -0,0 +1,232 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/file_path_watcher.h" + +#include +#include + +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/logging.h" +#include "base/scoped_cftyperef.h" +#include "base/singleton.h" +#include "base/time.h" + +namespace { + +// The latency parameter passed to FSEventsStreamCreate(). +const CFAbsoluteTime kEventLatencySeconds = 0.3; + +// Mac-specific file watcher implementation based on the FSEvents API. +class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate { + public: + FilePathWatcherImpl(); + virtual ~FilePathWatcherImpl() {} + + // Called from the FSEvents callback whenever there is a change to the paths + void OnFilePathChanged(); + + // (Re-)Initialize the event stream to start reporting events from + // |start_event|. + void UpdateEventStream(FSEventStreamEventId start_event); + + // FilePathWatcher::PlatformDelegate overrides. + virtual bool Watch(const FilePath& path, FilePathWatcher::Delegate* delegate); + virtual void Cancel(); + + private: + // Destroy the event stream. + void DestroyEventStream(); + + // Delegate to notify upon changes. + scoped_refptr delegate_; + + // Target path to watch (passed to delegate). + FilePath target_; + + // Keep track of the last modified time of the file. We use nulltime + // to represent the file not existing. + base::Time last_modified_; + + // The time at which we processed the first notification with the + // |last_modified_| time stamp. + base::Time first_notification_; + + // Backend stream we receive event callbacks from (strong reference). + FSEventStreamRef fsevent_stream_; + + // Used to detect early cancellation. + bool canceled_; + + DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); +}; + +// The callback passed to FSEventStreamCreate(). +void FSEventsCallback(ConstFSEventStreamRef stream, + void* event_watcher, size_t num_events, + void* event_paths, const FSEventStreamEventFlags flags[], + const FSEventStreamEventId event_ids[]) { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); + + FilePathWatcherImpl* watcher = + reinterpret_cast(event_watcher); + bool root_changed = false; + FSEventStreamEventId root_change_at = FSEventStreamGetLatestEventId(stream); + for (size_t i = 0; i < num_events; i++) { + if (flags[i] & kFSEventStreamEventFlagRootChanged) + root_changed = true; + if (event_ids[i]) + root_change_at = std::min(root_change_at, event_ids[i]); + } + + // Reinitialize the event stream if we find changes to the root. This is + // necessary since FSEvents doesn't report any events for the subtree after + // the directory to be watched gets created. + if (root_changed) { + // Resetting the event stream from within the callback fails (FSEvents spews + // bad file descriptor errors), so post a task to do the reset. + ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, + NewRunnableMethod(watcher, &FilePathWatcherImpl::UpdateEventStream, + root_change_at)); + } + + ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, + NewRunnableMethod(watcher, &FilePathWatcherImpl::OnFilePathChanged)); +} + +// FilePathWatcherImpl implementation: + +FilePathWatcherImpl::FilePathWatcherImpl() + : fsevent_stream_(NULL), + canceled_(false) { +} + +void FilePathWatcherImpl::OnFilePathChanged() { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); + DCHECK(!target_.empty()); + + file_util::FileInfo file_info; + bool file_exists = file_util::GetFileInfo(target_, &file_info); + if (file_exists && (last_modified_.is_null() || + last_modified_ != file_info.last_modified)) { + last_modified_ = file_info.last_modified; + first_notification_ = base::Time::Now(); + delegate_->OnFilePathChanged(target_); + } else if (file_exists && !first_notification_.is_null()) { + // The target's last modification time is equal to what's on record. This + // means that either an unrelated event occurred, or the target changed + // again (file modification times only have a resolution of 1s). Comparing + // file modification times against the wall clock is not reliable to find + // out whether the change is recent, since this code might just run too + // late. Moreover, there's no guarantee that file modification time and wall + // clock times come from the same source. + // + // Instead, the time at which the first notification carrying the current + // |last_notified_| time stamp is recorded. Later notifications that find + // the same file modification time only need to be forwarded until wall + // clock has advanced one second from the initial notification. After that + // interval, client code is guaranteed to having seen the current revision + // of the file. + if (base::Time::Now() - first_notification_ > + base::TimeDelta::FromSeconds(1)) { + // Stop further notifications for this |last_modification_| time stamp. + first_notification_ = base::Time(); + } + delegate_->OnFilePathChanged(target_); + } else if (!file_exists && !last_modified_.is_null()) { + last_modified_ = base::Time(); + delegate_->OnFilePathChanged(target_); + } +} + +bool FilePathWatcherImpl::Watch(const FilePath& path, + FilePathWatcher::Delegate* delegate) { + DCHECK(target_.value().empty()); + + target_ = path; + delegate_ = delegate; + + FSEventStreamEventId start_event = FSEventsGetCurrentEventId(); + + file_util::FileInfo file_info; + if (file_util::GetFileInfo(target_, &file_info)) { + last_modified_ = file_info.last_modified; + first_notification_ = base::Time::Now(); + } + + ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, + NewRunnableMethod(this, &FilePathWatcherImpl::UpdateEventStream, + start_event)); + + return true; +} + +void FilePathWatcherImpl::Cancel() { + // Switch to the UI thread if necessary, so we can tear down the event stream. + if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { + ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, + NewRunnableMethod(this, &FilePathWatcherImpl::Cancel)); + return; + } + + canceled_ = true; + if (fsevent_stream_) + DestroyEventStream(); +} + +void FilePathWatcherImpl::UpdateEventStream(FSEventStreamEventId start_event) { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); + + // It can happen that the watcher gets canceled while tasks that call this + // function are still in flight, so abort if this situation is detected. + if (canceled_) + return; + + if (fsevent_stream_) + DestroyEventStream(); + + scoped_cftyperef cf_path(CFStringCreateWithCString( + NULL, target_.value().c_str(), kCFStringEncodingMacHFS)); + scoped_cftyperef cf_dir_path(CFStringCreateWithCString( + NULL, target_.DirName().value().c_str(), kCFStringEncodingMacHFS)); + CFStringRef paths_array[] = { cf_path.get(), cf_dir_path.get() }; + scoped_cftyperef watched_paths(CFArrayCreate( + NULL, reinterpret_cast(paths_array), arraysize(paths_array), + &kCFTypeArrayCallBacks)); + + FSEventStreamContext context; + context.version = 0; + context.info = this; + context.retain = NULL; + context.release = NULL; + context.copyDescription = NULL; + + fsevent_stream_ = FSEventStreamCreate(NULL, &FSEventsCallback, &context, + watched_paths, + start_event, + kEventLatencySeconds, + kFSEventStreamCreateFlagWatchRoot); + FSEventStreamScheduleWithRunLoop(fsevent_stream_, CFRunLoopGetCurrent(), + kCFRunLoopDefaultMode); + if (!FSEventStreamStart(fsevent_stream_)) { + ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, + NewRunnableMethod(delegate_.get(), + &FilePathWatcher::Delegate::OnError)); + } +} + +void FilePathWatcherImpl::DestroyEventStream() { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); + FSEventStreamStop(fsevent_stream_); + FSEventStreamInvalidate(fsevent_stream_); + FSEventStreamRelease(fsevent_stream_); + fsevent_stream_ = NULL; +} + +} // namespace + +FilePathWatcher::FilePathWatcher() { + impl_ = new FilePathWatcherImpl(); +} diff --git a/chrome/browser/file_path_watcher_stub.cc b/chrome/browser/file_path_watcher_stub.cc new file mode 100644 index 0000000..5112f48 --- /dev/null +++ b/chrome/browser/file_path_watcher_stub.cc @@ -0,0 +1,19 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// This file exists for Unix systems which don't have the inotify headers, and +// thus cannot build file_watcher_inotify.cc + +#include "chrome/browser/file_path_watcher.h" + +class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate { + public: + virtual bool Watch(const FilePath& path, FileWatcher::Delegate* delegate) { + return false; + } +}; + +FilePathWatcher::FilePathWatcher() { + impl_ = new FilePathWatcherImpl(); +} diff --git a/chrome/browser/file_path_watcher_unittest.cc b/chrome/browser/file_path_watcher_unittest.cc new file mode 100644 index 0000000..102ecf3 --- /dev/null +++ b/chrome/browser/file_path_watcher_unittest.cc @@ -0,0 +1,465 @@ +// Copyright (c) 2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/file_path_watcher.h" + +#include + +#include "base/basictypes.h" +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/lock.h" +#include "base/message_loop.h" +#include "base/path_service.h" +#include "base/platform_thread.h" +#include "base/scoped_temp_dir.h" +#include "base/string_util.h" +#include "base/thread.h" +#include "base/waitable_event.h" +#include "testing/gtest/include/gtest/gtest.h" + +#if defined(OS_MACOSX) +// TODO(tony): Tests are flaky on mac. http://crbug.com/38188 +#define MAYBE(name) FLAKY_ ## name +#else +#define MAYBE(name) name +#endif + +namespace { + +// The time we wait for events to happen. It should be large enough to be +// reasonably sure all notifications sent in response to file system +// modifications made by the test are processed. This must also accomodate for +// the latency interval we pass to the Mac FSEvents API. +const int kWaitForEventTime = 1000; + +// A mock FilePathWatcher::Delegate for testing. I'd rather use gmock, but it's +// not thread safe for setting expectations, so the test code couldn't safely +// reset expectations while the file watcher is running. In order to allow this, +// we implement simple thread safe call counters in TestDelegate. +class TestDelegate : public FilePathWatcher::Delegate { + public: + TestDelegate() + : change_count_(0), + error_count_(0) {} + + virtual void OnFilePathChanged(const FilePath&) { + AutoLock lock(lock_); + ++change_count_; + } + + virtual void OnError() { + AutoLock lock(lock_); + ++error_count_; + } + + void GetCountsAndReset(int* changes, int* errors) { + AutoLock lock(lock_); + *errors = error_count_; + *changes = change_count_; + error_count_ = 0; + change_count_ = 0; + } + + private: + Lock lock_; + int change_count_; + int error_count_; + + DISALLOW_COPY_AND_ASSIGN(TestDelegate); +}; + +// A helper class for setting up watches on the file thread. +class SetupWatchTask : public Task { + public: + SetupWatchTask(const FilePath& target, + FilePathWatcher* watcher, + FilePathWatcher::Delegate* delegate, + bool* result, + base::WaitableEvent* completion) + : target_(target), + watcher_(watcher), + delegate_(delegate), + result_(result), + completion_(completion) {} + + void Run() { + *result_ = watcher_->Watch(target_, delegate_); + completion_->Signal(); + } + + private: + const FilePath target_; + FilePathWatcher* watcher_; + FilePathWatcher::Delegate* delegate_; + bool* result_; + base::WaitableEvent* completion_; + + DISALLOW_COPY_AND_ASSIGN(SetupWatchTask); +}; + +class FilePathWatcherTest : public testing::Test { + public: + // Implementation of FilePathWatcher on Mac requires UI loop. + FilePathWatcherTest() + : loop_(MessageLoop::TYPE_UI), + ui_thread_(ChromeThread::UI, &loop_) { + } + + protected: + virtual void SetUp() { + // Create a separate file thread in order to test proper thread usage. + file_thread_.reset(new ChromeThread(ChromeThread::FILE)); + file_thread_->Start(); + temp_dir_.reset(new ScopedTempDir); + ASSERT_TRUE(temp_dir_->CreateUniqueTempDir()); + } + + virtual void TearDown() { + loop_.RunAllPending(); + file_thread_.reset(); + } + + FilePath test_file() { + return temp_dir_->path().AppendASCII("FilePathWatcherTest"); + } + + // Write |content| to |file|. Returns true on success. + bool WriteFile(const FilePath& file, const std::string& content) { + int write_size = file_util::WriteFile(file, content.c_str(), + content.length()); + SyncIfPOSIX(); + return write_size == static_cast(content.length()); + } + + void SetupWatch(const FilePath& target, + FilePathWatcher* watcher, + FilePathWatcher::Delegate* delegate) { + base::WaitableEvent completion(false, false); + bool result; + ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, + new SetupWatchTask(target, watcher, delegate, &result, &completion)); + completion.Wait(); + ASSERT_TRUE(result); + } + + int WaitForEvents(TestDelegate* delegate) { + // Wait for events and check the expectations of the delegate. + loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask, + kWaitForEventTime); + loop_.Run(); + int errors; + int changes; + delegate->GetCountsAndReset(&changes, &errors); + EXPECT_EQ(0, errors); + return changes; + } + + // We need this function for reliable tests on Mac OS X. FSEvents API + // has a latency interval and can merge multiple events into one, + // and we need a clear distinction between events triggered by test setup code + // and test code. + void SyncIfPOSIX() { +#if defined(OS_POSIX) + sync(); +#endif // defined(OS_POSIX) + } + + MessageLoop loop_; + ChromeThread ui_thread_; + scoped_ptr file_thread_; + scoped_ptr temp_dir_; +}; + +// Basic test: Create the file and verify that we notice. +TEST_F(FilePathWatcherTest, MAYBE(NewFile)) { + FilePathWatcher watcher; + scoped_refptr delegate(new TestDelegate); + SetupWatch(test_file(), &watcher, delegate.get()); + + ASSERT_TRUE(WriteFile(test_file(), "content")); + EXPECT_LE(1, WaitForEvents(delegate.get())); +} + +// Verify that modifying the file is caught. +TEST_F(FilePathWatcherTest, MAYBE(ModifiedFile)) { + ASSERT_TRUE(WriteFile(test_file(), "content")); + + FilePathWatcher watcher; + scoped_refptr delegate(new TestDelegate); + SetupWatch(test_file(), &watcher, delegate.get()); + + // Now make sure we get notified if the file is modified. + ASSERT_TRUE(WriteFile(test_file(), "new content")); + EXPECT_LE(1, WaitForEvents(delegate.get())); +} + +// Verify that moving the file into place is caught. +TEST_F(FilePathWatcherTest, MAYBE(MovedFile)) { + FilePath source_file(temp_dir_->path().AppendASCII("source")); + ASSERT_TRUE(WriteFile(source_file, "content")); + + FilePathWatcher watcher; + scoped_refptr delegate(new TestDelegate); + SetupWatch(test_file(), &watcher, delegate.get()); + + // Now make sure we get notified if the file is modified. + ASSERT_TRUE(file_util::Move(source_file, test_file())); + EXPECT_LE(1, WaitForEvents(delegate.get())); +} + +TEST_F(FilePathWatcherTest, MAYBE(DeletedFile)) { + ASSERT_TRUE(WriteFile(test_file(), "content")); + + FilePathWatcher watcher; + scoped_refptr delegate(new TestDelegate); + SetupWatch(test_file(), &watcher, delegate.get()); + + // Now make sure we get notified if the file is deleted. + file_util::Delete(test_file(), false); + SyncIfPOSIX(); + EXPECT_LE(1, WaitForEvents(delegate.get())); +} + +// Verify that letting the watcher go out of scope stops notifications. +TEST_F(FilePathWatcherTest, MAYBE(Unregister)) { + scoped_refptr delegate(new TestDelegate); + + { + FilePathWatcher watcher; + SetupWatch(test_file(), &watcher, delegate.get()); + + // And then let it fall out of scope, clearing its watch. + } + + // Write a file to the test dir. + ASSERT_TRUE(WriteFile(test_file(), "content")); + EXPECT_EQ(0, WaitForEvents(delegate.get())); +} + +namespace { +// Used by the DeleteDuringNotify test below. +// Deletes the FilePathWatcher when it's notified. +class Deleter : public FilePathWatcher::Delegate { + public: + Deleter(FilePathWatcher* watcher, MessageLoop* loop) + : watcher_(watcher), + loop_(loop) { + } + + virtual void OnFilePathChanged(const FilePath& path) { + watcher_.reset(NULL); + loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); + } + + scoped_ptr watcher_; + MessageLoop* loop_; +}; +} // anonymous namespace + +// Verify that deleting a watcher during the callback doesn't crash. +TEST_F(FilePathWatcherTest, MAYBE(DeleteDuringNotify)) { + FilePathWatcher* watcher = new FilePathWatcher; + // Takes ownership of watcher. + scoped_refptr deleter(new Deleter(watcher, &loop_)); + SetupWatch(test_file(), watcher, deleter.get()); + + ASSERT_TRUE(WriteFile(test_file(), "content")); + loop_.Run(); + + // We win if we haven't crashed yet. + // Might as well double-check it got deleted, too. + ASSERT_TRUE(deleter->watcher_.get() == NULL); +} + +// Verify that deleting the watcher works even if there is a pending +// notification. +TEST_F(FilePathWatcherTest, MAYBE(DestroyWithPendingNotification)) { + scoped_refptr delegate(new TestDelegate); + FilePathWatcher* watcher = new FilePathWatcher; + SetupWatch(test_file(), watcher, delegate.get()); + ASSERT_TRUE(WriteFile(test_file(), "content")); + ChromeThread::DeleteSoon(ChromeThread::FILE, FROM_HERE, watcher); + // Success if there is no crash or DCHECK when running the callback. + WaitForEvents(delegate.get()); +} + +TEST_F(FilePathWatcherTest, MAYBE(MultipleWatchersSingleFile)) { + FilePathWatcher watcher1, watcher2; + scoped_refptr delegate1(new TestDelegate); + scoped_refptr delegate2(new TestDelegate); + SetupWatch(test_file(), &watcher1, delegate1.get()); + SetupWatch(test_file(), &watcher2, delegate2.get()); + + ASSERT_TRUE(WriteFile(test_file(), "content")); + EXPECT_LE(1, WaitForEvents(delegate1.get())); + EXPECT_LE(1, WaitForEvents(delegate2.get())); +} + +// Verify that watching a file whose parent directory doesn't exist yet works if +// the directory and file are created eventually. +TEST_F(FilePathWatcherTest, MAYBE(NonExistentDirectory)) { + FilePathWatcher watcher; + FilePath dir(temp_dir_->path().AppendASCII("dir")); + FilePath file(dir.AppendASCII("file")); + scoped_refptr delegate(new TestDelegate); + SetupWatch(file, &watcher, delegate.get()); + + ASSERT_TRUE(file_util::CreateDirectory(dir)); + EXPECT_EQ(0, WaitForEvents(delegate.get())); + + ASSERT_TRUE(WriteFile(file, "content")); + EXPECT_LE(1, WaitForEvents(delegate.get())); + + ASSERT_TRUE(WriteFile(file, "content v2")); + EXPECT_LE(1, WaitForEvents(delegate.get())); + + ASSERT_TRUE(file_util::Delete(file, false)); + EXPECT_LE(1, WaitForEvents(delegate.get())); +} + +// Exercises watch reconfiguration for the case that directories on the path +// are rapidly created. +TEST_F(FilePathWatcherTest, MAYBE(DirectoryChain)) { + FilePath path(temp_dir_->path()); + std::vector dir_names; + for (int i = 0; i < 20; i++) { + std::string dir(StringPrintf("d%d", i)); + dir_names.push_back(dir); + path = path.AppendASCII(dir); + } + + FilePathWatcher watcher; + FilePath file(path.AppendASCII("file")); + scoped_refptr delegate(new TestDelegate); + SetupWatch(file, &watcher, delegate.get()); + + FilePath sub_path(temp_dir_->path()); + for (std::vector::const_iterator d(dir_names.begin()); + d != dir_names.end(); ++d) { + sub_path = sub_path.AppendASCII(*d); + ASSERT_TRUE(file_util::CreateDirectory(sub_path)); + } + ASSERT_TRUE(WriteFile(file, "content")); + EXPECT_LE(1, WaitForEvents(delegate.get())); + + ASSERT_TRUE(WriteFile(file, "content v2")); + EXPECT_LE(1, WaitForEvents(delegate.get())); +} + +TEST_F(FilePathWatcherTest, MAYBE(DisappearingDirectory)) { + FilePathWatcher watcher; + FilePath dir(temp_dir_->path().AppendASCII("dir")); + FilePath file(dir.AppendASCII("file")); + ASSERT_TRUE(file_util::CreateDirectory(dir)); + ASSERT_TRUE(WriteFile(file, "content")); + scoped_refptr delegate(new TestDelegate); + SetupWatch(file, &watcher, delegate.get()); + + ASSERT_TRUE(file_util::Delete(dir, true)); + EXPECT_LE(1, WaitForEvents(delegate.get())); + + ASSERT_TRUE(file_util::CreateDirectory(dir)); + EXPECT_EQ(0, WaitForEvents(delegate.get())); + + ASSERT_TRUE(WriteFile(file, "content v2")); + EXPECT_LE(1, WaitForEvents(delegate.get())); +} + +// Tests that a file that is deleted and reappears is tracked correctly. +TEST_F(FilePathWatcherTest, MAYBE(DeleteAndRecreate)) { + ASSERT_TRUE(WriteFile(test_file(), "content")); + FilePathWatcher watcher; + scoped_refptr delegate(new TestDelegate); + SetupWatch(test_file(), &watcher, delegate.get()); + + ASSERT_TRUE(file_util::Delete(test_file(), false)); + EXPECT_LE(1, WaitForEvents(delegate.get())); + + ASSERT_TRUE(WriteFile(test_file(), "content")); + EXPECT_LE(1, WaitForEvents(delegate.get())); +} + +TEST_F(FilePathWatcherTest, MAYBE(WatchDirectory)) { + FilePathWatcher watcher; + FilePath dir(temp_dir_->path().AppendASCII("dir")); + FilePath file1(dir.AppendASCII("file1")); + FilePath file2(dir.AppendASCII("file2")); + scoped_refptr delegate(new TestDelegate); + SetupWatch(dir, &watcher, delegate.get()); + + ASSERT_TRUE(file_util::CreateDirectory(dir)); + EXPECT_LE(1, WaitForEvents(delegate.get())); + + ASSERT_TRUE(WriteFile(file1, "content")); + EXPECT_LE(1, WaitForEvents(delegate.get())); + + ASSERT_TRUE(WriteFile(file1, "content v2")); + EXPECT_LE(1, WaitForEvents(delegate.get())); + + ASSERT_TRUE(file_util::Delete(file1, false)); + SyncIfPOSIX(); + EXPECT_LE(1, WaitForEvents(delegate.get())); + + ASSERT_TRUE(WriteFile(file2, "content")); + EXPECT_LE(1, WaitForEvents(delegate.get())); +} + +TEST_F(FilePathWatcherTest, MAYBE(MoveParent)) { + FilePathWatcher file_watcher; + FilePathWatcher subdir_watcher; + FilePath dir(temp_dir_->path().AppendASCII("dir")); + FilePath dest(temp_dir_->path().AppendASCII("dest")); + FilePath subdir(dir.AppendASCII("subdir")); + FilePath file(subdir.AppendASCII("file")); + scoped_refptr file_delegate(new TestDelegate); + SetupWatch(file, &file_watcher, file_delegate.get()); + scoped_refptr subdir_delegate(new TestDelegate); + SetupWatch(subdir, &subdir_watcher, subdir_delegate.get()); + + // Setup a directory hierarchy. + ASSERT_TRUE(file_util::CreateDirectory(subdir)); + ASSERT_TRUE(WriteFile(file, "content")); + EXPECT_LE(1, WaitForEvents(file_delegate.get())); + EXPECT_LE(1, WaitForEvents(subdir_delegate.get())); + + // Move the parent directory. + file_util::Move(dir, dest); + EXPECT_LE(1, WaitForEvents(file_delegate.get())); + EXPECT_LE(1, WaitForEvents(subdir_delegate.get())); + + // Recreate the hierarchy. + ASSERT_TRUE(file_util::CreateDirectory(subdir)); + ASSERT_TRUE(WriteFile(file, "content")); + EXPECT_LE(1, WaitForEvents(file_delegate.get())); + EXPECT_LE(1, WaitForEvents(subdir_delegate.get())); +} + +TEST_F(FilePathWatcherTest, MAYBE(MoveChild)) { + FilePathWatcher file_watcher; + FilePathWatcher subdir_watcher; + FilePath source_dir(temp_dir_->path().AppendASCII("source")); + FilePath source_subdir(source_dir.AppendASCII("subdir")); + FilePath source_file(source_subdir.AppendASCII("file")); + FilePath dest_dir(temp_dir_->path().AppendASCII("dest")); + FilePath dest_subdir(dest_dir.AppendASCII("subdir")); + FilePath dest_file(dest_subdir.AppendASCII("file")); + scoped_refptr file_delegate(new TestDelegate); + SetupWatch(dest_file, &file_watcher, file_delegate.get()); + scoped_refptr subdir_delegate(new TestDelegate); + SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get()); + + // Setup a directory hierarchy. + ASSERT_TRUE(file_util::CreateDirectory(source_subdir)); + ASSERT_TRUE(WriteFile(source_file, "content")); + EXPECT_EQ(0, WaitForEvents(file_delegate.get())); + EXPECT_EQ(0, WaitForEvents(subdir_delegate.get())); + + // Move the directory into place, s.t. the watched file appears. + ASSERT_TRUE(file_util::Move(source_dir, dest_dir)); + EXPECT_LE(1, WaitForEvents(file_delegate.get())); + EXPECT_LE(1, WaitForEvents(subdir_delegate.get())); +} + +} // namespace diff --git a/chrome/browser/file_path_watcher_win.cc b/chrome/browser/file_path_watcher_win.cc new file mode 100644 index 0000000..194e91e --- /dev/null +++ b/chrome/browser/file_path_watcher_win.cc @@ -0,0 +1,243 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/file_path_watcher.h" + +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/logging.h" +#include "base/object_watcher.h" +#include "base/ref_counted.h" +#include "base/time.h" + +namespace { + +class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate, + public base::ObjectWatcher::Delegate { + public: + FilePathWatcherImpl() : delegate_(NULL), handle_(INVALID_HANDLE_VALUE) {} + + virtual bool Watch(const FilePath& path, FilePathWatcher::Delegate* delegate); + virtual void Cancel(); + + // Callback from MessageLoopForIO. + virtual void OnObjectSignaled(HANDLE object); + + private: + virtual ~FilePathWatcherImpl(); + + // Setup a watch handle for directory |dir|. Returns true if no fatal error + // occurs. |handle| will receive the handle value if |dir| is watchable, + // otherwise INVALID_HANDLE_VALUE. + static bool SetupWatchHandle(const FilePath& dir, HANDLE* handle) + WARN_UNUSED_RESULT; + + // (Re-)Initialize the watch handle. + bool UpdateWatch() WARN_UNUSED_RESULT; + + // Destroy the watch handle. + void DestroyWatch(); + + // Delegate to notify upon changes. + scoped_refptr delegate_; + + // Path we're supposed to watch (passed to delegate). + FilePath target_; + + // Handle for FindFirstChangeNotification. + HANDLE handle_; + + // ObjectWatcher to watch handle_ for events. + base::ObjectWatcher watcher_; + + // Keep track of the last modified time of the file. We use nulltime + // to represent the file not existing. + base::Time last_modified_; + + // The time at which we processed the first notification with the + // |last_modified_| time stamp. + base::Time first_notification_; + + DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); +}; + +bool FilePathWatcherImpl::Watch(const FilePath& path, + FilePathWatcher::Delegate* delegate) { + DCHECK(target_.value().empty()); // Can only watch one path. + delegate_ = delegate; + target_ = path; + + if (!UpdateWatch()) + return false; + + watcher_.StartWatching(handle_, this); + + return true; +} + +void FilePathWatcherImpl::Cancel() { + // Switch to the file thread if necessary so we can stop |watcher_|. + if (!ChromeThread::CurrentlyOn(ChromeThread::FILE)) { + ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, + NewRunnableMethod(this, &FilePathWatcherImpl::Cancel)); + return; + } + + if (handle_ != INVALID_HANDLE_VALUE) + DestroyWatch(); +} + +void FilePathWatcherImpl::OnObjectSignaled(HANDLE object) { + DCHECK(object == handle_); + // Make sure we stay alive through the body of this function. + scoped_refptr keep_alive(this); + + if (!UpdateWatch()) { + delegate_->OnError(); + return; + } + + // Check whether the event applies to |target_| and notify the delegate. + file_util::FileInfo file_info; + bool file_exists = file_util::GetFileInfo(target_, &file_info); + if (file_exists && (last_modified_.is_null() || + last_modified_ != file_info.last_modified)) { + last_modified_ = file_info.last_modified; + first_notification_ = base::Time::Now(); + delegate_->OnFilePathChanged(target_); + } else if (file_exists && !first_notification_.is_null()) { + // The target's last modification time is equal to what's on record. This + // means that either an unrelated event occurred, or the target changed + // again (file modification times only have a resolution of 1s). Comparing + // file modification times against the wall clock is not reliable to find + // out whether the change is recent, since this code might just run too + // late. Moreover, there's no guarantee that file modification time and wall + // clock times come from the same source. + // + // Instead, the time at which the first notification carrying the current + // |last_notified_| time stamp is recorded. Later notifications that find + // the same file modification time only need to be forwarded until wall + // clock has advanced one second from the initial notification. After that + // interval, client code is guaranteed to having seen the current revision + // of the file. + if (base::Time::Now() - first_notification_ > + base::TimeDelta::FromSeconds(1)) { + // Stop further notifications for this |last_modification_| time stamp. + first_notification_ = base::Time(); + } + delegate_->OnFilePathChanged(target_); + } else if (!file_exists && !last_modified_.is_null()) { + last_modified_ = base::Time(); + delegate_->OnFilePathChanged(target_); + } + + // The watch may have been cancelled by the callback. + if (handle_ != INVALID_HANDLE_VALUE) + watcher_.StartWatching(handle_, this); +} + +FilePathWatcherImpl::~FilePathWatcherImpl() { + if (handle_ != INVALID_HANDLE_VALUE) + DestroyWatch(); +} + +// static +bool FilePathWatcherImpl::SetupWatchHandle(const FilePath& dir, + HANDLE* handle) { + *handle = FindFirstChangeNotification( + dir.value().c_str(), + false, // Don't watch subtrees + FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_SIZE | + FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_DIR_NAME | + FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SECURITY); + if (*handle != INVALID_HANDLE_VALUE) { + // Make sure the handle we got points to an existing directory. It seems + // that windows sometimes hands out watches to direectories that are + // about to go away, but doesn't sent notifications if that happens. + if (!file_util::DirectoryExists(dir)) { + FindCloseChangeNotification(*handle); + *handle = INVALID_HANDLE_VALUE; + } + return true; + } + + // If FindFirstChangeNotification failed because the target directory + // doesn't exist, access is denied (happens if the file is already gone but + // there are still handles open), or the target is not a directory, try the + // immediate parent directory instead. + DWORD error_code = GetLastError(); + if (error_code != ERROR_FILE_NOT_FOUND && + error_code != ERROR_PATH_NOT_FOUND && + error_code != ERROR_ACCESS_DENIED && + error_code != ERROR_DIRECTORY) { + PLOG(ERROR) << "FindFirstChangeNotification failed for " + << dir.value(); + return false; + } + + return true; +} + +bool FilePathWatcherImpl::UpdateWatch() { + if (handle_ != INVALID_HANDLE_VALUE) + DestroyWatch(); + + file_util::FileInfo file_info; + if (file_util::GetFileInfo(target_, &file_info)) { + last_modified_ = file_info.last_modified; + first_notification_ = base::Time::Now(); + } + + // Start at the target and walk up the directory chain until we succesfully + // create a watch handle in |handle_|. |child_dirs| keeps a stack of child + // directories stripped from target, in reverse order. + std::vector child_dirs; + FilePath watched_path(target_); + while (true) { + if (!SetupWatchHandle(watched_path, &handle_)) + return false; + + // Break if a valid handle is returned. Try the parent directory otherwise. + if (handle_ != INVALID_HANDLE_VALUE) + break; + + // Abort if we hit the root directory. + child_dirs.push_back(watched_path.BaseName()); + FilePath parent(watched_path.DirName()); + if (parent == watched_path) { + LOG(ERROR) << "Reached the root directory"; + return false; + } + watched_path = parent; + } + + // At this point, handle_ is valid. However, the bottom-up search that the + // above code performs races against directory creation. So try to walk back + // down and see whether any children appeared in the mean time. + while (!child_dirs.empty()) { + watched_path = watched_path.Append(child_dirs.back()); + child_dirs.pop_back(); + HANDLE temp_handle = INVALID_HANDLE_VALUE; + if (!SetupWatchHandle(watched_path, &temp_handle)) + return false; + if (temp_handle == INVALID_HANDLE_VALUE) + break; + FindCloseChangeNotification(handle_); + handle_ = temp_handle; + } + + return true; +} + +void FilePathWatcherImpl::DestroyWatch() { + watcher_.StopWatching(); + FindCloseChangeNotification(handle_); + handle_ = INVALID_HANDLE_VALUE; +} + +} // namespace + +FilePathWatcher::FilePathWatcher() { + impl_ = new FilePathWatcherImpl(); +} diff --git a/chrome/browser/file_watcher.h b/chrome/browser/file_watcher.h deleted file mode 100644 index 2d4417b..0000000 --- a/chrome/browser/file_watcher.h +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// This module provides a way to monitor a file for changes. - -#ifndef CHROME_BROWSER_FILE_WATCHER_H_ -#define CHROME_BROWSER_FILE_WATCHER_H_ -#pragma once - -#include "base/basictypes.h" -#include "base/ref_counted.h" -#include "chrome/browser/chrome_thread.h" - -class FilePath; - -// This class lets you register interest in changes on a file. The delegate -// will get called whenever the file is changed, including created or deleted. -// WARNING: To be able to get create/delete notifications and to work cross -// platform, we actually listen for changes to the directory containing -// the file. -// WARNING: On OSX and Windows, the OS API doesn't tell us which file in the -// directory changed. We work around this by watching the file time, but this -// can result in some extra notifications if we get other notifications within -// 2s of the file having changed. -class FileWatcher { - public: - // Declares the callback client code implements to receive notifications. Note - // that implementations of this interface should not keep a reference to the - // corresponding FileWatcher object to prevent a reference cycle. - class Delegate : public base::RefCountedThreadSafe { - public: - virtual ~Delegate() {} - virtual void OnFileChanged(const FilePath& path) = 0; - }; - - FileWatcher(); - ~FileWatcher() {} - - // Register interest in any changes on the file |path|. - // OnFileChanged will be called back for each change to the file. Any - // background operations will be ran on |backend_thread_id|. Note: The - // directory containing |path| must exist before you try to watch the file. - // Returns false if the watch can't be added. - bool Watch(const FilePath& path, Delegate* delegate) { - DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); - return impl_->Watch(path, delegate); - } - - // Used internally to encapsulate different members on different platforms. - class PlatformDelegate - : public base::RefCountedThreadSafe { - public: - virtual ~PlatformDelegate() {} - - virtual bool Watch(const FilePath& path, Delegate* delegate) = 0; - }; - - private: - scoped_refptr impl_; - - DISALLOW_COPY_AND_ASSIGN(FileWatcher); -}; - -#endif // CHROME_BROWSER_FILE_WATCHER_H_ diff --git a/chrome/browser/file_watcher_inotify.cc b/chrome/browser/file_watcher_inotify.cc deleted file mode 100644 index 20321d5..0000000 --- a/chrome/browser/file_watcher_inotify.cc +++ /dev/null @@ -1,302 +0,0 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/file_watcher.h" - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "base/eintr_wrapper.h" -#include "base/file_path.h" -#include "base/file_util.h" -#include "base/hash_tables.h" -#include "base/lock.h" -#include "base/logging.h" -#include "base/message_loop.h" -#include "base/scoped_ptr.h" -#include "base/singleton.h" -#include "base/task.h" -#include "base/thread.h" - -namespace { - -class FileWatcherImpl; - -// Singleton to manage all inotify watches. -// TODO(tony): It would be nice if this wasn't a singleton. -// http://crbug.com/38174 -class InotifyReader { - public: - typedef int Watch; // Watch descriptor used by AddWatch and RemoveWatch. - static const Watch kInvalidWatch = -1; - - // Watch |path| for changes. |watcher| will be notified on each change. - // Returns kInvalidWatch on failure. - Watch AddWatch(const FilePath& path, FileWatcherImpl* watcher); - - // Remove |watch|. Returns true on success. - bool RemoveWatch(Watch watch, FileWatcherImpl* watcher); - - // Callback for InotifyReaderTask. - void OnInotifyEvent(const inotify_event* event); - - private: - friend struct DefaultSingletonTraits; - - typedef std::set WatcherSet; - - InotifyReader(); - ~InotifyReader(); - - // We keep track of which delegates want to be notified on which watches. - base::hash_map watchers_; - - // Lock to protect watchers_. - Lock lock_; - - // Separate thread on which we run blocking read for inotify events. - base::Thread thread_; - - // File descriptor returned by inotify_init. - const int inotify_fd_; - - // Use self-pipe trick to unblock select during shutdown. - int shutdown_pipe_[2]; - - // Flag set to true when startup was successful. - bool valid_; - - DISALLOW_COPY_AND_ASSIGN(InotifyReader); -}; - -class FileWatcherImpl : public FileWatcher::PlatformDelegate { - public: - FileWatcherImpl(); - ~FileWatcherImpl(); - - // Called for each event coming from the watch. - void OnInotifyEvent(const inotify_event* event); - - // Start watching |path| for changes and notify |delegate| on each change. - // Returns true if watch for |path| has been added successfully. - virtual bool Watch(const FilePath& path, FileWatcher::Delegate* delegate); - - private: - // Delegate to notify upon changes. - scoped_refptr delegate_; - - // Watch returned by InotifyReader. - InotifyReader::Watch watch_; - - // The file we're watching. - FilePath path_; - - DISALLOW_COPY_AND_ASSIGN(FileWatcherImpl); -}; - -class InotifyReaderTask : public Task { - public: - InotifyReaderTask(InotifyReader* reader, int inotify_fd, int shutdown_fd) - : reader_(reader), - inotify_fd_(inotify_fd), - shutdown_fd_(shutdown_fd) { - } - - virtual void Run() { - while (true) { - fd_set rfds; - FD_ZERO(&rfds); - FD_SET(inotify_fd_, &rfds); - FD_SET(shutdown_fd_, &rfds); - - // Wait until some inotify events are available. - int select_result = - HANDLE_EINTR(select(std::max(inotify_fd_, shutdown_fd_) + 1, - &rfds, NULL, NULL, NULL)); - if (select_result < 0) { - DPLOG(WARNING) << "select failed"; - return; - } - - if (FD_ISSET(shutdown_fd_, &rfds)) - return; - - // Adjust buffer size to current event queue size. - int buffer_size; - int ioctl_result = HANDLE_EINTR(ioctl(inotify_fd_, FIONREAD, - &buffer_size)); - - if (ioctl_result != 0) { - DPLOG(WARNING) << "ioctl failed"; - return; - } - - std::vector buffer(buffer_size); - - ssize_t bytes_read = HANDLE_EINTR(read(inotify_fd_, &buffer[0], - buffer_size)); - - if (bytes_read < 0) { - DPLOG(WARNING) << "read from inotify fd failed"; - return; - } - - ssize_t i = 0; - while (i < bytes_read) { - inotify_event* event = reinterpret_cast(&buffer[i]); - size_t event_size = sizeof(inotify_event) + event->len; - DCHECK(i + event_size <= static_cast(bytes_read)); - reader_->OnInotifyEvent(event); - i += event_size; - } - } - } - - private: - InotifyReader* reader_; - int inotify_fd_; - int shutdown_fd_; - - DISALLOW_COPY_AND_ASSIGN(InotifyReaderTask); -}; - -InotifyReader::InotifyReader() - : thread_("inotify_reader"), - inotify_fd_(inotify_init()), - valid_(false) { - shutdown_pipe_[0] = -1; - shutdown_pipe_[1] = -1; - if (inotify_fd_ >= 0 && pipe(shutdown_pipe_) == 0 && thread_.Start()) { - thread_.message_loop()->PostTask( - FROM_HERE, new InotifyReaderTask(this, inotify_fd_, shutdown_pipe_[0])); - valid_ = true; - } -} - -InotifyReader::~InotifyReader() { - if (valid_) { - // Write to the self-pipe so that the select call in InotifyReaderTask - // returns. - ssize_t ret = HANDLE_EINTR(write(shutdown_pipe_[1], "", 1)); - DPCHECK(ret > 0); - DCHECK_EQ(ret, 1); - thread_.Stop(); - } - if (inotify_fd_ >= 0) - close(inotify_fd_); - if (shutdown_pipe_[0] >= 0) - close(shutdown_pipe_[0]); - if (shutdown_pipe_[1] >= 0) - close(shutdown_pipe_[1]); -} - -InotifyReader::Watch InotifyReader::AddWatch( - const FilePath& path, FileWatcherImpl* watcher) { - if (!valid_) - return kInvalidWatch; - - AutoLock auto_lock(lock_); - - Watch watch = inotify_add_watch(inotify_fd_, path.value().c_str(), - IN_CREATE | IN_DELETE | - IN_CLOSE_WRITE | IN_MOVE); - - if (watch == kInvalidWatch) - return kInvalidWatch; - - watchers_[watch].insert(watcher); - - return watch; -} - -bool InotifyReader::RemoveWatch(Watch watch, - FileWatcherImpl* watcher) { - if (!valid_) - return false; - - AutoLock auto_lock(lock_); - - watchers_[watch].erase(watcher); - - if (watchers_[watch].empty()) { - watchers_.erase(watch); - return (inotify_rm_watch(inotify_fd_, watch) == 0); - } - - return true; -} - -void InotifyReader::OnInotifyEvent(const inotify_event* event) { - if (event->mask & IN_IGNORED) - return; - - // In case you want to limit the scope of this lock, it's not sufficient - // to just copy things under the lock, and then run the notifications - // without holding the lock. FileWatcherImpl's dtor removes its watches, - // and to do that obtains the lock. After it finishes removing watches, - // it's destroyed. So, if you copy under the lock and notify without the lock, - // it's possible you'll copy the FileWatcherImpl which is being - // destroyed, then it will destroy itself, and then you'll try to notify it. - AutoLock auto_lock(lock_); - - for (WatcherSet::iterator watcher = watchers_[event->wd].begin(); - watcher != watchers_[event->wd].end(); - ++watcher) { - (*watcher)->OnInotifyEvent(event); - } -} - -FileWatcherImpl::FileWatcherImpl() - : delegate_(NULL), - watch_(InotifyReader::kInvalidWatch) { -} - -FileWatcherImpl::~FileWatcherImpl() { - if (watch_ == InotifyReader::kInvalidWatch) - return; - - Singleton::get()->RemoveWatch(watch_, this); -} - -void FileWatcherImpl::OnInotifyEvent(const inotify_event* event) { - // Since we're watching the directory, filter out inotify events - // if it's not related to the file we're watching. - if (path_ != path_.DirName().Append(event->name)) - return; - - ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, - NewRunnableMethod(delegate_.get(), &FileWatcher::Delegate::OnFileChanged, - path_)); -} - -bool FileWatcherImpl::Watch(const FilePath& path, - FileWatcher::Delegate* delegate) { - // Each FileWatcherImpl can only watch one file. - DCHECK(watch_ == InotifyReader::kInvalidWatch); - - // It's not possible to watch a file that doesn't exist, so instead, - // watch the parent directory. - if (!file_util::PathExists(path.DirName())) - return false; - - delegate_ = delegate; - path_ = path; - watch_ = Singleton::get()->AddWatch(path.DirName(), this); - return watch_ != InotifyReader::kInvalidWatch; -} - -} // namespace - -FileWatcher::FileWatcher() { - impl_ = new FileWatcherImpl(); -} diff --git a/chrome/browser/file_watcher_mac.cc b/chrome/browser/file_watcher_mac.cc deleted file mode 100644 index 9bd6d2e2..0000000 --- a/chrome/browser/file_watcher_mac.cc +++ /dev/null @@ -1,157 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/file_watcher.h" - -#include - -#include "base/file_path.h" -#include "base/file_util.h" -#include "base/logging.h" -#include "base/scoped_cftyperef.h" -#include "base/time.h" - -namespace { - -const CFAbsoluteTime kEventLatencySeconds = 0.3; - -class FileWatcherImpl : public FileWatcher::PlatformDelegate { - public: - FileWatcherImpl() {} - ~FileWatcherImpl() { - if (!path_.value().empty()) { - FSEventStreamStop(fsevent_stream_); - FSEventStreamInvalidate(fsevent_stream_); - FSEventStreamRelease(fsevent_stream_); - } - } - - virtual bool Watch(const FilePath& path, FileWatcher::Delegate* delegate) { - FilePath parent_dir = path.DirName(); - if (!file_util::AbsolutePath(&parent_dir)) - return false; - - // Jump back to the UI thread because FSEventStreamScheduleWithRunLoop - // requires a UI thread. - if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { - ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, - NewRunnableMethod(this, &FileWatcherImpl::WatchImpl, path, delegate)); - } else { - LOG(INFO) << "Adding FileWatcher watch."; - // During unittests, there is only one thread and it is both the UI - // thread and the file thread. - WatchImpl(path, delegate); - } - return true; - } - - bool WatchImpl(const FilePath& path, FileWatcher::Delegate* delegate); - - void OnFSEventsCallback(const FilePath& event_path) { - DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); - DCHECK(!path_.value().empty()); - FilePath absolute_event_path = event_path; - if (!file_util::AbsolutePath(&absolute_event_path)) - return; - - file_util::FileInfo file_info; - bool file_exists = file_util::GetFileInfo(path_, &file_info); - if (file_exists && (last_modified_.is_null() || - last_modified_ != file_info.last_modified)) { - last_modified_ = file_info.last_modified; - delegate_->OnFileChanged(path_); - } else if (file_exists && (base::Time::Now() - last_modified_ < - base::TimeDelta::FromSeconds(2))) { - // Since we only have a resolution of 1s, if we get a callback within - // 2s of the file having changed, go ahead and notify our observer. This - // might be from a different file change, but it's better to notify too - // much rather than miss a notification. - delegate_->OnFileChanged(path_); - } else if (!file_exists && !last_modified_.is_null()) { - last_modified_ = base::Time(); - delegate_->OnFileChanged(path_); - } - } - - private: - // Delegate to notify upon changes. - scoped_refptr delegate_; - - // Path we're watching (passed to delegate). - FilePath path_; - - // Backend stream we receive event callbacks from (strong reference). - FSEventStreamRef fsevent_stream_; - - // Keep track of the last modified time of the file. We use nulltime - // to represent the file not existing. - base::Time last_modified_; - - DISALLOW_COPY_AND_ASSIGN(FileWatcherImpl); -}; - -void FSEventsCallback(ConstFSEventStreamRef stream, - void* event_watcher, size_t num_events, - void* event_paths, const FSEventStreamEventFlags flags[], - const FSEventStreamEventId event_ids[]) { - char** paths = reinterpret_cast(event_paths); - FileWatcherImpl* watcher = - reinterpret_cast(event_watcher); - for (size_t i = 0; i < num_events; i++) { - if (!ChromeThread::CurrentlyOn(ChromeThread::FILE)) { - ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, - NewRunnableMethod(watcher, &FileWatcherImpl::OnFSEventsCallback, - FilePath(paths[i]))); - } else { - LOG(INFO) << "FileWatcher event callback for " << paths[i]; - // During unittests, there is only one thread and it is both the UI - // thread and the file thread. - watcher->OnFSEventsCallback(FilePath(paths[i])); - } - } -} - -bool FileWatcherImpl::WatchImpl(const FilePath& path, - FileWatcher::Delegate* delegate) { - DCHECK(path_.value().empty()); // Can only watch one path. - DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); - - file_util::FileInfo file_info; - if (file_util::GetFileInfo(path, &file_info)) - last_modified_ = file_info.last_modified; - - path_ = path; - delegate_ = delegate; - - scoped_cftyperef cf_path(CFStringCreateWithCString( - NULL, path.DirName().value().c_str(), kCFStringEncodingMacHFS)); - CFStringRef path_for_array = cf_path.get(); - scoped_cftyperef watched_paths(CFArrayCreate( - NULL, reinterpret_cast(&path_for_array), 1, - &kCFTypeArrayCallBacks)); - - FSEventStreamContext context; - context.version = 0; - context.info = this; - context.retain = NULL; - context.release = NULL; - context.copyDescription = NULL; - - fsevent_stream_ = FSEventStreamCreate(NULL, &FSEventsCallback, &context, - watched_paths, - kFSEventStreamEventIdSinceNow, - kEventLatencySeconds, - kFSEventStreamCreateFlagNone); - FSEventStreamScheduleWithRunLoop(fsevent_stream_, CFRunLoopGetCurrent(), - kCFRunLoopDefaultMode); - FSEventStreamStart(fsevent_stream_); - - return true; -} - -} // namespace - -FileWatcher::FileWatcher() { - impl_ = new FileWatcherImpl(); -} diff --git a/chrome/browser/file_watcher_stub.cc b/chrome/browser/file_watcher_stub.cc deleted file mode 100644 index 96e24f4..0000000 --- a/chrome/browser/file_watcher_stub.cc +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// This file exists for Linux systems which don't have the inotify headers, and -// thus cannot build file_watcher_inotify.cc - -#include "chrome/browser/file_watcher.h" - -class FileWatcherImpl : public FileWatcher::PlatformDelegate { - public: - virtual bool Watch(const FilePath& path, FileWatcher::Delegate* delegate) { - return false; - } -}; - -FileWatcher::FileWatcher() { - impl_ = new FileWatcherImpl(); -} diff --git a/chrome/browser/file_watcher_unittest.cc b/chrome/browser/file_watcher_unittest.cc deleted file mode 100644 index 525e77a..0000000 --- a/chrome/browser/file_watcher_unittest.cc +++ /dev/null @@ -1,242 +0,0 @@ -// Copyright (c) 2008 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/file_watcher.h" - -#include - -#include "base/basictypes.h" -#include "base/file_path.h" -#include "base/file_util.h" -#include "base/message_loop.h" -#include "base/path_service.h" -#include "base/platform_thread.h" -#include "base/scoped_temp_dir.h" -#include "base/string_util.h" -#include "base/thread.h" -#include "testing/gmock/include/gmock/gmock.h" -#include "testing/gtest/include/gtest/gtest.h" - -using testing::_; -using testing::AnyNumber; -using testing::AtLeast; -using testing::Mock; - -#if defined(OS_MACOSX) -// TODO(tony): Tests are flaky on mac. http://crbug.com/38188 -#define MAYBE(name) FLAKY_ ## name -#else -#define MAYBE(name) name -#endif - -namespace { - -// For tests where we wait a bit to verify nothing happened -const int kWaitForEventTime = 500; - -// Maximum amount of time to wait on a test. -const int kMaxTestTimeMs = 10 * 1000; - -// A mock FileWatcher::Delegate for testing. -class TestDelegate : public FileWatcher::Delegate { - public: - MOCK_METHOD1(OnFileChanged, void(const FilePath&)); -}; - -class FileWatcherTest : public testing::Test { - public: - // Implementation of FileWatcher on Mac requires UI loop. - FileWatcherTest() - : loop_(MessageLoop::TYPE_UI), - ui_thread_(ChromeThread::UI, &loop_), - file_thread_(ChromeThread::FILE, &loop_) { - } - - protected: - virtual void SetUp() { - temp_dir_.reset(new ScopedTempDir); - ASSERT_TRUE(temp_dir_->CreateUniqueTempDir()); - // Make sure that not getting an event doesn't cause the whole - // test suite to hang. - loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask, - kMaxTestTimeMs); - } - - virtual void TearDown() { - loop_.RunAllPending(); - } - - FilePath test_file() { - return temp_dir_->path().AppendASCII("FileWatcherTest"); - } - - // Write |content| to the test file. Returns true on success. - bool WriteTestFile(const std::string& content) { - // Logging to try and figure out why these tests are flaky on mac. - LOG(INFO) << "WriteTestFile"; - int write_size = file_util::WriteFile(test_file(), content.c_str(), - content.length()); - SyncIfPOSIX(); - return write_size == static_cast(content.length()); - } - - void VerifyDelegate(TestDelegate* delegate) { - // Check that we get at least the expected number of notified delegates. - loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask, - kWaitForEventTime); - loop_.Run(); - Mock::VerifyAndClearExpectations(delegate); - } - - // We need this function for reliable tests on Mac OS X. FSEvents API - // has a latency interval and can merge multiple events into one, - // and we need a clear distinction between events triggered by test setup code - // and test code. - void SyncIfPOSIX() { -#if defined(OS_POSIX) - sync(); -#endif // defined(OS_POSIX) - } - - MessageLoop loop_; - ChromeThread ui_thread_; - ChromeThread file_thread_; - scoped_ptr temp_dir_; -}; - -// Basic test: Create the file and verify that we notice. -TEST_F(FileWatcherTest, MAYBE(NewFile)) { - FileWatcher watcher; - scoped_refptr delegate(new TestDelegate); - ASSERT_TRUE(watcher.Watch(test_file(), delegate.get())); - - EXPECT_CALL(*delegate, OnFileChanged(_)).Times(AtLeast(1)); - ASSERT_TRUE(WriteTestFile("content")); - VerifyDelegate(delegate.get()); -} - -// Verify that modifying the file is caught. -TEST_F(FileWatcherTest, MAYBE(ModifiedFile)) { - ASSERT_TRUE(WriteTestFile("content")); - - FileWatcher watcher; - scoped_refptr delegate(new TestDelegate); - ASSERT_TRUE(watcher.Watch(test_file(), delegate.get())); - - // Now make sure we get notified if the file is modified. - EXPECT_CALL(*delegate, OnFileChanged(_)).Times(AtLeast(1)); - ASSERT_TRUE(WriteTestFile("new content")); - VerifyDelegate(delegate.get()); -} - -TEST_F(FileWatcherTest, MAYBE(DeletedFile)) { - ASSERT_TRUE(WriteTestFile("content")); - - FileWatcher watcher; - scoped_refptr delegate(new TestDelegate); - ASSERT_TRUE(watcher.Watch(test_file(), delegate.get())); - - // Now make sure we get notified if the file is deleted. - EXPECT_CALL(*delegate, OnFileChanged(_)).Times(AtLeast(1)); - file_util::Delete(test_file(), false); - SyncIfPOSIX(); - VerifyDelegate(delegate.get()); -} - -// Verify that letting the watcher go out of scope stops notifications. -TEST_F(FileWatcherTest, MAYBE(Unregister)) { - scoped_refptr delegate(new TestDelegate); - - { - FileWatcher watcher; - ASSERT_TRUE(watcher.Watch(test_file(), delegate.get())); - - // And then let it fall out of scope, clearing its watch. - } - - // Write a file to the test dir. - EXPECT_CALL(*delegate, OnFileChanged(_)).Times(0); - ASSERT_TRUE(WriteTestFile("content")); - VerifyDelegate(delegate.get()); -} - -namespace { -// Used by the DeleteDuringNotify test below. -// Deletes the FileWatcher when it's notified. -class Deleter : public FileWatcher::Delegate { - public: - Deleter(FileWatcher* watcher, MessageLoop* loop) - : watcher_(watcher), - loop_(loop) { - } - - virtual void OnFileChanged(const FilePath& path) { - watcher_.reset(NULL); - loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); - } - - scoped_ptr watcher_; - MessageLoop* loop_; -}; -} // anonymous namespace - -// Verify that deleting a watcher during the callback doesn't crash. -TEST_F(FileWatcherTest, MAYBE(DeleteDuringNotify)) { - FileWatcher* watcher = new FileWatcher; - // Takes ownership of watcher. - scoped_refptr deleter(new Deleter(watcher, &loop_)); - ASSERT_TRUE(watcher->Watch(test_file(), deleter.get())); - - ASSERT_TRUE(WriteTestFile("content")); - loop_.Run(); - - // We win if we haven't crashed yet. - // Might as well double-check it got deleted, too. - ASSERT_TRUE(deleter->watcher_.get() == NULL); -} - -// Verify that deleting the watcher works even if there is a pending -// notification. -// -// It's hard to test this, since both a change event and deletion of the file -// watcher must happen before the task that runs the callback executes. The code -// below only triggers the situation with the Linux implementation. Change -// detection runs on a separate thread in the Linux implementation, so we can -// schedule the FileWatcher deletion in advance. For Mac and Windows, the -// DeleteTask runs before the message loop processes the platform-specific -// change notifications, so the whole FileWatcher is destroyed before the -// callback gets scheduled. -TEST_F(FileWatcherTest, MAYBE(DestroyWithPendingNotification)) { - scoped_refptr delegate(new TestDelegate); - EXPECT_CALL(*delegate, OnFileChanged(_)).Times(AnyNumber()); - FileWatcher* watcher = new FileWatcher; - ASSERT_TRUE(watcher->Watch(test_file(), delegate.get())); - ASSERT_TRUE(WriteTestFile("content")); - ChromeThread::DeleteSoon(ChromeThread::FILE, FROM_HERE, watcher); - // Success if there is no crash or DCHECK when running the callback. - VerifyDelegate(delegate.get()); -} - -TEST_F(FileWatcherTest, MAYBE(MultipleWatchersSingleFile)) { - FileWatcher watcher1, watcher2; - scoped_refptr delegate1(new TestDelegate); - scoped_refptr delegate2(new TestDelegate); - ASSERT_TRUE(watcher1.Watch(test_file(), delegate1.get())); - ASSERT_TRUE(watcher2.Watch(test_file(), delegate2.get())); - - EXPECT_CALL(*delegate1, OnFileChanged(_)).Times(AtLeast(1)); - EXPECT_CALL(*delegate2, OnFileChanged(_)).Times(AtLeast(1)); - ASSERT_TRUE(WriteTestFile("content")); - VerifyDelegate(delegate1.get()); - VerifyDelegate(delegate2.get()); -} - -// Verify that watching a file who's parent directory doesn't exist -// fails, but doesn't asssert. -TEST_F(FileWatcherTest, NonExistentDirectory) { - FileWatcher watcher; - ASSERT_FALSE(watcher.Watch(test_file().AppendASCII("FileToWatch"), NULL)); -} - -} // namespace diff --git a/chrome/browser/file_watcher_win.cc b/chrome/browser/file_watcher_win.cc deleted file mode 100644 index d4ec0ed..0000000 --- a/chrome/browser/file_watcher_win.cc +++ /dev/null @@ -1,111 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/file_watcher.h" - -#include "base/file_path.h" -#include "base/file_util.h" -#include "base/logging.h" -#include "base/object_watcher.h" -#include "base/ref_counted.h" -#include "base/time.h" - -namespace { - -class FileWatcherImpl : public FileWatcher::PlatformDelegate, - public base::ObjectWatcher::Delegate { - public: - FileWatcherImpl() : delegate_(NULL), handle_(INVALID_HANDLE_VALUE) {} - - virtual bool Watch(const FilePath& path, FileWatcher::Delegate* delegate); - - // Callback from MessageLoopForIO. - virtual void OnObjectSignaled(HANDLE object); - - private: - virtual ~FileWatcherImpl(); - - // Delegate to notify upon changes. - scoped_refptr delegate_; - - // Path we're watching (passed to delegate). - FilePath path_; - - // Handle for FindFirstChangeNotification. - HANDLE handle_; - - // ObjectWatcher to watch handle_ for events. - base::ObjectWatcher watcher_; - - // Keep track of the last modified time of the file. We use nulltime - // to represent the file not existing. - base::Time last_modified_; - - DISALLOW_COPY_AND_ASSIGN(FileWatcherImpl); -}; - -FileWatcherImpl::~FileWatcherImpl() { - if (handle_ != INVALID_HANDLE_VALUE) { - watcher_.StopWatching(); - FindCloseChangeNotification(handle_); - } -} - -bool FileWatcherImpl::Watch(const FilePath& path, - FileWatcher::Delegate* delegate) { - DCHECK(path_.value().empty()); // Can only watch one path. - file_util::FileInfo file_info; - if (file_util::GetFileInfo(path, &file_info)) - last_modified_ = file_info.last_modified; - - // FindFirstChangeNotification watches directories, so use the parent path. - handle_ = FindFirstChangeNotification( - path.DirName().value().c_str(), - false, // Don't watch subtrees - FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_SIZE | - FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_DIR_NAME); - if (handle_ == INVALID_HANDLE_VALUE) - return false; - - delegate_ = delegate; - path_ = path; - watcher_.StartWatching(handle_, this); - - return true; -} - -void FileWatcherImpl::OnObjectSignaled(HANDLE object) { - DCHECK(object == handle_); - // Make sure we stay alive through the body of this function. - scoped_refptr keep_alive(this); - - file_util::FileInfo file_info; - bool file_exists = file_util::GetFileInfo(path_, &file_info); - if (file_exists && (last_modified_.is_null() || - last_modified_ != file_info.last_modified)) { - last_modified_ = file_info.last_modified; - delegate_->OnFileChanged(path_); - } else if (file_exists && (base::Time::Now() - last_modified_ < - base::TimeDelta::FromSeconds(2))) { - // Since we only have a resolution of 1s, if we get a callback within - // 2s of the file having changed, go ahead and notify our observer. This - // might be from a different file change, but it's better to notify too - // much rather than miss a notification. - delegate_->OnFileChanged(path_); - } else if (!file_exists && !last_modified_.is_null()) { - last_modified_ = base::Time(); - delegate_->OnFileChanged(path_); - } - - // Register for more notifications on file change. - BOOL ok = FindNextChangeNotification(object); - DCHECK(ok); - watcher_.StartWatching(object, this); -} - -} // namespace - -FileWatcher::FileWatcher() { - impl_ = new FileWatcherImpl(); -} diff --git a/chrome/browser/user_style_sheet_watcher.cc b/chrome/browser/user_style_sheet_watcher.cc index 3743820..a8789fd 100644 --- a/chrome/browser/user_style_sheet_watcher.cc +++ b/chrome/browser/user_style_sheet_watcher.cc @@ -22,21 +22,21 @@ const char kUserStyleSheetFile[] = "Custom.css"; // file thread and sends a notification when the style sheet is loaded. It is // a helper to UserStyleSheetWatcher. The reference graph is as follows: // -// .-----------------------. owns .-------------. -// | UserStyleSheetWatcher |----------->| FileWatcher | -// '-----------------------' '-------------' -// | | -// V | -// .----------------------. | -// | UserStyleSheetLoader |<------------------' +// .-----------------------. owns .-----------------. +// | UserStyleSheetWatcher |----------->| FilePathWatcher | +// '-----------------------' '-----------------' +// | | +// V | +// .----------------------. | +// | UserStyleSheetLoader |<--------------------' // '----------------------' // -// FileWatcher's reference to UserStyleSheetLoader is used for delivering the -// change notifications. Since they happen asynchronously, UserStyleSheetWatcher -// and its FileWatcher may be destroyed while a callback to UserStyleSheetLoader -// is in progress, in which case the UserStyleSheetLoader object outlives the -// watchers. -class UserStyleSheetLoader : public FileWatcher::Delegate { +// FilePathWatcher's reference to UserStyleSheetLoader is used for delivering +// the change notifications. Since they happen asynchronously, +// UserStyleSheetWatcher and its FilePathWatcher may be destroyed while a +// callback to UserStyleSheetLoader is in progress, in which case the +// UserStyleSheetLoader object outlives the watchers. +class UserStyleSheetLoader : public FilePathWatcher::Delegate { public: UserStyleSheetLoader(); virtual ~UserStyleSheetLoader() {} @@ -52,8 +52,8 @@ class UserStyleSheetLoader : public FileWatcher::Delegate { // Send out a notification if the stylesheet has already been loaded. void NotifyLoaded(); - // FileWatcher::Delegate interface - virtual void OnFileChanged(const FilePath& path); + // FilePathWatcher::Delegate interface + virtual void OnFilePathChanged(const FilePath& path); private: // Called on the UI thread after the stylesheet has loaded. @@ -81,7 +81,7 @@ void UserStyleSheetLoader::NotifyLoaded() { } } -void UserStyleSheetLoader::OnFileChanged(const FilePath& path) { +void UserStyleSheetLoader::OnFilePathChanged(const FilePath& path) { LoadStyleSheet(path); } @@ -145,11 +145,11 @@ void UserStyleSheetWatcher::Init() { } if (!file_watcher_.get()) { - file_watcher_.reset(new FileWatcher); - file_watcher_->Watch(profile_path_.AppendASCII(kStyleSheetDir) - .AppendASCII(kUserStyleSheetFile), loader_.get()); + file_watcher_.reset(new FilePathWatcher); FilePath style_sheet_file = profile_path_.AppendASCII(kStyleSheetDir) .AppendASCII(kUserStyleSheetFile); + if (!file_watcher_->Watch(style_sheet_file, loader_.get())) + LOG(ERROR) << "Failed to setup watch for " << style_sheet_file.value(); loader_->LoadStyleSheet(style_sheet_file); } } diff --git a/chrome/browser/user_style_sheet_watcher.h b/chrome/browser/user_style_sheet_watcher.h index c4974de..a322837 100644 --- a/chrome/browser/user_style_sheet_watcher.h +++ b/chrome/browser/user_style_sheet_watcher.h @@ -10,7 +10,7 @@ #include "base/ref_counted.h" #include "base/scoped_ptr.h" #include "chrome/browser/chrome_thread.h" -#include "chrome/browser/file_watcher.h" +#include "chrome/browser/file_path_watcher.h" #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" #include "googleurl/src/gurl.h" @@ -44,7 +44,7 @@ class UserStyleSheetWatcher scoped_refptr loader_; // Watches for changes to the css file so we can reload the style sheet. - scoped_ptr file_watcher_; + scoped_ptr file_watcher_; NotificationRegistrar registrar_; diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index c8533ac..747cc69 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1445,10 +1445,10 @@ 'browser/fav_icon_helper.h', 'browser/favicon_service.cc', 'browser/favicon_service.h', - 'browser/file_watcher.h', - 'browser/file_watcher_inotify.cc', - 'browser/file_watcher_mac.cc', - 'browser/file_watcher_win.cc', + 'browser/file_path_watcher.h', + 'browser/file_path_watcher_inotify.cc', + 'browser/file_path_watcher_mac.cc', + 'browser/file_path_watcher_win.cc', 'browser/file_system_proxy.cc', 'browser/file_system_proxy.h', 'browser/find_bar.h', @@ -3107,7 +3107,7 @@ ], }, { # OS != "linux" 'sources!': [ - 'browser/file_watcher_inotify.cc', + 'browser/file_path_watcher_inotify.cc', ], }], ['OS=="freebsd" or OS=="openbsd"', { @@ -3117,7 +3117,7 @@ '../build/linux/system.gyp:nss', ], 'sources': [ - 'browser/file_watcher_stub.cc', + 'browser/file_path_watcher_stub.cc', ], }], ['OS=="mac"', { diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 49ddd9d..6e9ec0c 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1057,7 +1057,7 @@ 'browser/extensions/sandboxed_extension_unpacker_unittest.cc', 'browser/extensions/user_script_listener_unittest.cc', 'browser/extensions/user_script_master_unittest.cc', - 'browser/file_watcher_unittest.cc', + 'browser/file_path_watcher_unittest.cc', 'browser/find_backend_unittest.cc', 'browser/first_run/first_run_unittest.cc', 'browser/geolocation/fake_access_token_store.h', -- cgit v1.1