summaryrefslogtreecommitdiffstats
path: root/content
diff options
context:
space:
mode:
authorcraig.schlenter@chromium.org <craig.schlenter@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-04-14 16:56:01 +0000
committercraig.schlenter@chromium.org <craig.schlenter@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-04-14 16:56:01 +0000
commit493c8001f5011b3f0aacf06a57df7f523b09b23c (patch)
tree7cd7d409b12a85fb9f36c2bca0a54488d551b800 /content
parentbb8890e9cee0c997257873e9c978eead7d028568 (diff)
downloadchromium_src-493c8001f5011b3f0aacf06a57df7f523b09b23c.zip
chromium_src-493c8001f5011b3f0aacf06a57df7f523b09b23c.tar.gz
chromium_src-493c8001f5011b3f0aacf06a57df7f523b09b23c.tar.bz2
Move FilePathWatcher to base/files.
FilePathWatcher is going to be used in future for monitoring changes to /etc/resolv.conf so it needs live somewhere where various consumers can get at it without incurring the wrath of the layering gods. TEST=existing tests BUG=67734 Review URL: http://codereview.chromium.org/6793020 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@81606 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content')
-rw-r--r--content/browser/plugin_service.cc6
-rw-r--r--content/browser/plugin_service.h8
-rw-r--r--content/common/file_path_watcher/OWNERS2
-rw-r--r--content/common/file_path_watcher/file_path_watcher.cc28
-rw-r--r--content/common/file_path_watcher/file_path_watcher.h122
-rw-r--r--content/common/file_path_watcher/file_path_watcher_browsertest.cc616
-rw-r--r--content/common/file_path_watcher/file_path_watcher_inotify.cc456
-rw-r--r--content/common/file_path_watcher/file_path_watcher_mac.cc487
-rw-r--r--content/common/file_path_watcher/file_path_watcher_stub.cc21
-rw-r--r--content/common/file_path_watcher/file_path_watcher_win.cc274
-rw-r--r--content/content_common.gypi15
11 files changed, 9 insertions, 2026 deletions
diff --git a/content/browser/plugin_service.cc b/content/browser/plugin_service.cc
index 7ba4fa5..7bcde62 100644
--- a/content/browser/plugin_service.cc
+++ b/content/browser/plugin_service.cc
@@ -38,6 +38,10 @@
#include "chrome/browser/chromeos/plugin_selection_policy.h"
#endif
+#if defined(OS_LINUX)
+using ::base::files::FilePathWatcher;
+#endif
+
#if defined(OS_MACOSX)
static void NotifyPluginsOfActivation() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
@@ -147,7 +151,6 @@ PluginService::PluginService()
&plugin_dirs);
for (size_t i = 0; i < plugin_dirs.size(); ++i) {
- FilePathWatcher* watcher = new FilePathWatcher();
// FilePathWatcher can not handle non-absolute paths under windows.
// We don't watch for file changes in windows now but if this should ever
// be extended to Windows these lines might save some time of debugging.
@@ -155,6 +158,7 @@ PluginService::PluginService()
if (!plugin_dirs[i].IsAbsolute())
continue;
#endif
+ FilePathWatcher* watcher = new FilePathWatcher();
VLOG(1) << "Watching for changes in: " << plugin_dirs[i].value();
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
diff --git a/content/browser/plugin_service.h b/content/browser/plugin_service.h
index 5c37c01..d5c5004 100644
--- a/content/browser/plugin_service.h
+++ b/content/browser/plugin_service.h
@@ -34,7 +34,7 @@
#endif
#if defined(OS_LINUX)
-#include "content/common/file_path_watcher/file_path_watcher.h"
+#include "base/files/file_path_watcher.h"
#endif
#if defined(OS_CHROMEOS)
@@ -177,9 +177,9 @@ class PluginService
#if defined(OS_LINUX)
// Registers a new FilePathWatcher for a given path.
static void RegisterFilePathWatcher(
- FilePathWatcher* watcher,
+ base::files::FilePathWatcher* watcher,
const FilePath& path,
- FilePathWatcher::Delegate* delegate);
+ base::files::FilePathWatcher::Delegate* delegate);
#endif
// The main thread's message loop.
@@ -210,7 +210,7 @@ class PluginService
#endif
#if defined(OS_LINUX)
- ScopedVector<FilePathWatcher> file_watchers_;
+ ScopedVector<base::files::FilePathWatcher> file_watchers_;
scoped_refptr<PluginDirWatcherDelegate> file_watcher_delegate_;
#endif
diff --git a/content/common/file_path_watcher/OWNERS b/content/common/file_path_watcher/OWNERS
deleted file mode 100644
index e83a27d..0000000
--- a/content/common/file_path_watcher/OWNERS
+++ /dev/null
@@ -1,2 +0,0 @@
-mnissler@chromium.org
-dmaclach@chromium.org
diff --git a/content/common/file_path_watcher/file_path_watcher.cc b/content/common/file_path_watcher/file_path_watcher.cc
deleted file mode 100644
index 41f3e2b..0000000
--- a/content/common/file_path_watcher/file_path_watcher.cc
+++ /dev/null
@@ -1,28 +0,0 @@
-// Copyright (c) 2011 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.
-
-// Cross platform methods for FilePathWatcher. See the various platform
-// specific implementation files, too.
-
-#include "content/common/file_path_watcher/file_path_watcher.h"
-
-#include "base/logging.h"
-#include "base/message_loop.h"
-
-FilePathWatcher::~FilePathWatcher() {
- impl_->Cancel();
-}
-
-bool FilePathWatcher::Watch(const FilePath& path, Delegate* delegate) {
- DCHECK(path.IsAbsolute());
- return impl_->Watch(path, delegate);
-}
-
-FilePathWatcher::PlatformDelegate::PlatformDelegate(): cancelled_(false) {
-}
-
-FilePathWatcher::PlatformDelegate::~PlatformDelegate() {
- DCHECK(is_cancelled());
-}
-
diff --git a/content/common/file_path_watcher/file_path_watcher.h b/content/common/file_path_watcher/file_path_watcher.h
deleted file mode 100644
index 094b04f..0000000
--- a/content/common/file_path_watcher/file_path_watcher.h
+++ /dev/null
@@ -1,122 +0,0 @@
-// Copyright (c) 2011 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 CONTENT_COMMON_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_
-#define CONTENT_COMMON_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_
-#pragma once
-
-#include "base/basictypes.h"
-#include "base/file_path.h"
-#include "base/memory/ref_counted.h"
-#include "base/message_loop_proxy.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, FilePathWatcher has slightly different semantics on OS X
-// than on Windows or Linux. FilePathWatcher on Linux and Windows will detect
-// modifications to files in a watched directory. FilePathWatcher on Mac will
-// detect the creation and deletion of files in a watched directory, but will
-// not detect modifications to those files. See file_path_watcher_mac.cc for
-// details.
-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<Delegate> {
- 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 OnFilePathError(const FilePath& path) {}
- };
-
- FilePathWatcher();
- ~FilePathWatcher();
-
- // Register interest in any changes on |path|. OnPathChanged will be called
- // back for each change. Returns true on success.
- // OnFilePathChanged() will be called on the same thread as Watch() is called,
- // which should have a MessageLoop of TYPE_IO.
- bool Watch(const FilePath& path, Delegate* delegate) WARN_UNUSED_RESULT;
-
- class PlatformDelegate;
-
- // A custom Task that always cleans up the PlatformDelegate, either when
- // executed or when deleted without having been executed at all, as can
- // happen during shutdown.
- class CancelTask : public Task {
- public:
- CancelTask(PlatformDelegate* delegate): delegate_(delegate) {}
- virtual ~CancelTask() {
- delegate_->CancelOnMessageLoopThread();
- }
-
- virtual void Run() {
- delegate_->CancelOnMessageLoopThread();
- }
- private:
- scoped_refptr<PlatformDelegate> delegate_;
-
- DISALLOW_COPY_AND_ASSIGN(CancelTask);
- };
-
- // Used internally to encapsulate different members on different platforms.
- class PlatformDelegate : public base::RefCountedThreadSafe<PlatformDelegate> {
- public:
- 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.
- // It can be called from any thread.
- virtual void Cancel() = 0;
-
- protected:
- virtual ~PlatformDelegate();
-
- // Stop watching. This is only called on the thread of the appropriate
- // message loop. Since it can also be called more than once, it should
- // check |is_cancelled()| to avoid duplicate work.
- virtual void CancelOnMessageLoopThread() = 0;
-
- scoped_refptr<base::MessageLoopProxy> message_loop() const {
- return message_loop_;
- }
-
- void set_message_loop(base::MessageLoopProxy* loop) {
- message_loop_ = loop;
- }
-
- // Must be called before the PlatformDelegate is deleted.
- void set_cancelled() {
- cancelled_ = true;
- }
-
- bool is_cancelled() const {
- return cancelled_;
- }
-
- private:
- friend class base::RefCountedThreadSafe<PlatformDelegate>;
- friend class CancelTask;
-
- scoped_refptr<base::MessageLoopProxy> message_loop_;
- bool cancelled_;
- };
-
- private:
- scoped_refptr<PlatformDelegate> impl_;
-
- DISALLOW_COPY_AND_ASSIGN(FilePathWatcher);
-};
-
-#endif // CONTENT_COMMON_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_
diff --git a/content/common/file_path_watcher/file_path_watcher_browsertest.cc b/content/common/file_path_watcher/file_path_watcher_browsertest.cc
deleted file mode 100644
index b847ca9..0000000
--- a/content/common/file_path_watcher/file_path_watcher_browsertest.cc
+++ /dev/null
@@ -1,616 +0,0 @@
-// Copyright (c) 2011 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 "content/common/file_path_watcher/file_path_watcher.h"
-
-#include <set>
-
-#if defined(OS_WIN)
-#include <windows.h>
-#include <aclapi.h>
-#elif defined(OS_POSIX)
-#include <sys/stat.h>
-#endif
-
-#include "base/basictypes.h"
-#include "base/compiler_specific.h"
-#include "base/file_path.h"
-#include "base/file_util.h"
-#include "base/memory/scoped_temp_dir.h"
-#include "base/message_loop.h"
-#include "base/message_loop_proxy.h"
-#include "base/path_service.h"
-#include "base/string_util.h"
-#include "base/stl_util-inl.h"
-#include "base/synchronization/waitable_event.h"
-#include "base/test/test_timeouts.h"
-#include "base/threading/thread.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-namespace {
-
-class TestDelegate;
-
-// Aggregates notifications from the test delegates and breaks the message loop
-// the test thread is waiting on once they all came in.
-class NotificationCollector
- : public base::RefCountedThreadSafe<NotificationCollector> {
- public:
- NotificationCollector()
- : loop_(base::MessageLoopProxy::CreateForCurrentThread()) {}
-
- // Called from the file thread by the delegates.
- void OnChange(TestDelegate* delegate) {
- loop_->PostTask(FROM_HERE,
- NewRunnableMethod(this,
- &NotificationCollector::RecordChange,
- make_scoped_refptr(delegate)));
- }
-
- void Register(TestDelegate* delegate) {
- delegates_.insert(delegate);
- }
-
- void Reset() {
- signaled_.clear();
- }
-
- bool Success() {
- return signaled_ == delegates_;
- }
-
- private:
- void RecordChange(TestDelegate* delegate) {
- ASSERT_TRUE(loop_->BelongsToCurrentThread());
- ASSERT_TRUE(delegates_.count(delegate));
- signaled_.insert(delegate);
-
- // Check whether all delegates have been signaled.
- if (signaled_ == delegates_)
- loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
- }
-
- // Set of registered delegates.
- std::set<TestDelegate*> delegates_;
-
- // Set of signaled delegates.
- std::set<TestDelegate*> signaled_;
-
- // The loop we should break after all delegates signaled.
- scoped_refptr<base::MessageLoopProxy> loop_;
-};
-
-// 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 keep simple thread safe status flags in TestDelegate.
-class TestDelegate : public FilePathWatcher::Delegate {
- public:
- // The message loop specified by |loop| will be quit if a notification is
- // received while the delegate is |armed_|. Note that the testing code must
- // guarantee |loop| outlives the file thread on which OnFilePathChanged runs.
- explicit TestDelegate(NotificationCollector* collector)
- : collector_(collector) {
- collector_->Register(this);
- }
-
- virtual void OnFilePathChanged(const FilePath&) {
- collector_->OnChange(this);
- }
-
- virtual void OnFilePathError(const FilePath& path) {
- ADD_FAILURE() << "Error " << path.value();
- }
-
- private:
- scoped_refptr<NotificationCollector> collector_;
-
- 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:
- FilePathWatcherTest()
- : file_thread_("FilePathWatcherTest") {}
-
- virtual ~FilePathWatcherTest() {}
-
- protected:
- virtual void SetUp() {
- // Create a separate file thread in order to test proper thread usage.
- base::Thread::Options options(MessageLoop::TYPE_IO, 0);
- ASSERT_TRUE(file_thread_.StartWithOptions(options));
- ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
- collector_ = new NotificationCollector();
- }
-
- virtual void TearDown() {
- loop_.RunAllPending();
- }
-
- 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());
- return write_size == static_cast<int>(content.length());
- }
-
- bool SetupWatch(const FilePath& target,
- FilePathWatcher* watcher,
- FilePathWatcher::Delegate* delegate) WARN_UNUSED_RESULT {
- base::WaitableEvent completion(false, false);
- bool result;
- file_thread_.message_loop_proxy()->PostTask(FROM_HERE,
- new SetupWatchTask(target,
- watcher,
- delegate,
- &result,
- &completion));
- completion.Wait();
- return result;
- }
-
- bool WaitForEvents() WARN_UNUSED_RESULT {
- collector_->Reset();
- loop_.Run();
- return collector_->Success();
- }
-
- NotificationCollector* collector() { return collector_.get(); }
-
- MessageLoop loop_;
- base::Thread file_thread_;
- ScopedTempDir temp_dir_;
- scoped_refptr<NotificationCollector> collector_;
-};
-
-// Basic test: Create the file and verify that we notice.
-TEST_F(FilePathWatcherTest, NewFile) {
- FilePathWatcher watcher;
- scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
- ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
-
- ASSERT_TRUE(WriteFile(test_file(), "content"));
- ASSERT_TRUE(WaitForEvents());
-}
-
-// Verify that modifying the file is caught.
-TEST_F(FilePathWatcherTest, ModifiedFile) {
- ASSERT_TRUE(WriteFile(test_file(), "content"));
-
- FilePathWatcher watcher;
- scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
- ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
-
- // Now make sure we get notified if the file is modified.
- ASSERT_TRUE(WriteFile(test_file(), "new content"));
- ASSERT_TRUE(WaitForEvents());
-}
-
-// Verify that moving the file into place is caught.
-TEST_F(FilePathWatcherTest, MovedFile) {
- FilePath source_file(temp_dir_.path().AppendASCII("source"));
- ASSERT_TRUE(WriteFile(source_file, "content"));
-
- FilePathWatcher watcher;
- scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
- ASSERT_TRUE(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()));
- ASSERT_TRUE(WaitForEvents());
-}
-
-TEST_F(FilePathWatcherTest, DeletedFile) {
- ASSERT_TRUE(WriteFile(test_file(), "content"));
-
- FilePathWatcher watcher;
- scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
- ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
-
- // Now make sure we get notified if the file is deleted.
- file_util::Delete(test_file(), false);
- ASSERT_TRUE(WaitForEvents());
-}
-
-// 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();
- loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
- }
-
- scoped_ptr<FilePathWatcher> watcher_;
- MessageLoop* loop_;
-};
-
-// Verify that deleting a watcher during the callback doesn't crash.
-TEST_F(FilePathWatcherTest, DeleteDuringNotify) {
- FilePathWatcher* watcher = new FilePathWatcher;
- // Takes ownership of watcher.
- scoped_refptr<Deleter> deleter(new Deleter(watcher, &loop_));
- ASSERT_TRUE(SetupWatch(test_file(), watcher, deleter.get()));
-
- ASSERT_TRUE(WriteFile(test_file(), "content"));
- ASSERT_TRUE(WaitForEvents());
-
- // 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, DestroyWithPendingNotification) {
- scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
- FilePathWatcher* watcher = new FilePathWatcher;
- ASSERT_TRUE(SetupWatch(test_file(), watcher, delegate.get()));
- ASSERT_TRUE(WriteFile(test_file(), "content"));
- file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, watcher);
-}
-
-TEST_F(FilePathWatcherTest, MultipleWatchersSingleFile) {
- FilePathWatcher watcher1, watcher2;
- scoped_refptr<TestDelegate> delegate1(new TestDelegate(collector()));
- scoped_refptr<TestDelegate> delegate2(new TestDelegate(collector()));
- ASSERT_TRUE(SetupWatch(test_file(), &watcher1, delegate1.get()));
- ASSERT_TRUE(SetupWatch(test_file(), &watcher2, delegate2.get()));
-
- ASSERT_TRUE(WriteFile(test_file(), "content"));
- ASSERT_TRUE(WaitForEvents());
-}
-
-// Verify that watching a file whose parent directory doesn't exist yet works if
-// the directory and file are created eventually.
-TEST_F(FilePathWatcherTest, NonExistentDirectory) {
- FilePathWatcher watcher;
- FilePath dir(temp_dir_.path().AppendASCII("dir"));
- FilePath file(dir.AppendASCII("file"));
- scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
- ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get()));
-
- ASSERT_TRUE(file_util::CreateDirectory(dir));
-
- ASSERT_TRUE(WriteFile(file, "content"));
-
- VLOG(1) << "Waiting for file creation";
- ASSERT_TRUE(WaitForEvents());
-
- ASSERT_TRUE(WriteFile(file, "content v2"));
- VLOG(1) << "Waiting for file change";
- ASSERT_TRUE(WaitForEvents());
-
- ASSERT_TRUE(file_util::Delete(file, false));
- VLOG(1) << "Waiting for file deletion";
- ASSERT_TRUE(WaitForEvents());
-}
-
-// Exercises watch reconfiguration for the case that directories on the path
-// are rapidly created.
-TEST_F(FilePathWatcherTest, DirectoryChain) {
- FilePath path(temp_dir_.path());
- std::vector<std::string> 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<TestDelegate> delegate(new TestDelegate(collector()));
- ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get()));
-
- FilePath sub_path(temp_dir_.path());
- for (std::vector<std::string>::const_iterator d(dir_names.begin());
- d != dir_names.end(); ++d) {
- sub_path = sub_path.AppendASCII(*d);
- ASSERT_TRUE(file_util::CreateDirectory(sub_path));
- }
- VLOG(1) << "Create File";
- ASSERT_TRUE(WriteFile(file, "content"));
- VLOG(1) << "Waiting for file creation";
- ASSERT_TRUE(WaitForEvents());
-
- ASSERT_TRUE(WriteFile(file, "content v2"));
- VLOG(1) << "Waiting for file modification";
- ASSERT_TRUE(WaitForEvents());
-}
-
-TEST_F(FilePathWatcherTest, 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<TestDelegate> delegate(new TestDelegate(collector()));
- ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get()));
-
- ASSERT_TRUE(file_util::Delete(dir, true));
- ASSERT_TRUE(WaitForEvents());
-}
-
-// Tests that a file that is deleted and reappears is tracked correctly.
-TEST_F(FilePathWatcherTest, DeleteAndRecreate) {
- ASSERT_TRUE(WriteFile(test_file(), "content"));
- FilePathWatcher watcher;
- scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
- ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
-
- ASSERT_TRUE(file_util::Delete(test_file(), false));
- VLOG(1) << "Waiting for file deletion";
- ASSERT_TRUE(WaitForEvents());
-
- ASSERT_TRUE(WriteFile(test_file(), "content"));
- VLOG(1) << "Waiting for file creation";
- ASSERT_TRUE(WaitForEvents());
-}
-
-TEST_F(FilePathWatcherTest, WatchDirectory) {
- FilePathWatcher watcher;
- FilePath dir(temp_dir_.path().AppendASCII("dir"));
- FilePath file1(dir.AppendASCII("file1"));
- FilePath file2(dir.AppendASCII("file2"));
- scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
- ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get()));
-
- ASSERT_TRUE(file_util::CreateDirectory(dir));
- VLOG(1) << "Waiting for directory creation";
- ASSERT_TRUE(WaitForEvents());
-
- ASSERT_TRUE(WriteFile(file1, "content"));
- VLOG(1) << "Waiting for file1 creation";
- ASSERT_TRUE(WaitForEvents());
-
-#if !defined(OS_MACOSX)
- // Mac implementation does not detect files modified in a directory.
- ASSERT_TRUE(WriteFile(file1, "content v2"));
- VLOG(1) << "Waiting for file1 modification";
- ASSERT_TRUE(WaitForEvents());
-#endif // !OS_MACOSX
-
- ASSERT_TRUE(file_util::Delete(file1, false));
- VLOG(1) << "Waiting for file1 deletion";
- ASSERT_TRUE(WaitForEvents());
-
- ASSERT_TRUE(WriteFile(file2, "content"));
- VLOG(1) << "Waiting for file2 creation";
- ASSERT_TRUE(WaitForEvents());
-}
-
-TEST_F(FilePathWatcherTest, 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<TestDelegate> file_delegate(new TestDelegate(collector()));
- ASSERT_TRUE(SetupWatch(file, &file_watcher, file_delegate.get()));
- scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector()));
- ASSERT_TRUE(SetupWatch(subdir, &subdir_watcher, subdir_delegate.get()));
-
- // Setup a directory hierarchy.
- ASSERT_TRUE(file_util::CreateDirectory(subdir));
- ASSERT_TRUE(WriteFile(file, "content"));
- VLOG(1) << "Waiting for file creation";
- ASSERT_TRUE(WaitForEvents());
-
- // Move the parent directory.
- file_util::Move(dir, dest);
- VLOG(1) << "Waiting for directory move";
- ASSERT_TRUE(WaitForEvents());
-}
-
-TEST_F(FilePathWatcherTest, 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"));
-
- // Setup a directory hierarchy.
- ASSERT_TRUE(file_util::CreateDirectory(source_subdir));
- ASSERT_TRUE(WriteFile(source_file, "content"));
-
- scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector()));
- ASSERT_TRUE(SetupWatch(dest_file, &file_watcher, file_delegate.get()));
- scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector()));
- ASSERT_TRUE(SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get()));
-
- // Move the directory into place, s.t. the watched file appears.
- ASSERT_TRUE(file_util::Move(source_dir, dest_dir));
- ASSERT_TRUE(WaitForEvents());
-}
-
-#if !defined(OS_LINUX)
-// Linux implementation of FilePathWatcher doesn't catch attribute changes.
-// http://crbug.com/78043
-
-// Verify that changing attributes on a file is caught
-TEST_F(FilePathWatcherTest, FileAttributesChanged) {
- ASSERT_TRUE(WriteFile(test_file(), "content"));
- FilePathWatcher watcher;
- scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
- ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
-
- // Now make sure we get notified if the file is modified.
- ASSERT_TRUE(file_util::MakeFileUnreadable(test_file()));
- ASSERT_TRUE(WaitForEvents());
-}
-
-#endif // !OS_LINUX
-
-enum Permission {
- Read,
- Write,
- Execute
-};
-
-bool ChangeFilePermissions(const FilePath& path, Permission perm, bool allow) {
-#if defined(OS_POSIX)
- struct stat stat_buf;
-
- if (stat(path.value().c_str(), &stat_buf) != 0)
- return false;
-
- mode_t mode = 0;
- switch (perm) {
- case Read:
- mode = S_IRUSR | S_IRGRP | S_IROTH;
- break;
- case Write:
- mode = S_IWUSR | S_IWGRP | S_IWOTH;
- break;
- case Execute:
- mode = S_IXUSR | S_IXGRP | S_IXOTH;
- break;
- default:
- ADD_FAILURE() << "unknown perm " << perm;
- return false;
- }
- if (allow) {
- stat_buf.st_mode |= mode;
- } else {
- stat_buf.st_mode &= ~mode;
- }
- return chmod(path.value().c_str(), stat_buf.st_mode) == 0;
-
-#elif defined(OS_WIN)
- PACL old_dacl;
- PSECURITY_DESCRIPTOR security_descriptor;
- if (GetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()),
- SE_FILE_OBJECT,
- DACL_SECURITY_INFORMATION, NULL, NULL, &old_dacl,
- NULL, &security_descriptor) != ERROR_SUCCESS)
- return false;
-
- DWORD mode = 0;
- switch (perm) {
- case Read:
- mode = GENERIC_READ;
- break;
- case Write:
- mode = GENERIC_WRITE;
- break;
- case Execute:
- mode = GENERIC_EXECUTE;
- break;
- default:
- ADD_FAILURE() << "unknown perm " << perm;
- return false;
- }
-
- // Deny Read access for the current user.
- EXPLICIT_ACCESS change;
- change.grfAccessPermissions = mode;
- change.grfAccessMode = allow ? GRANT_ACCESS : DENY_ACCESS;
- change.grfInheritance = 0;
- change.Trustee.pMultipleTrustee = NULL;
- change.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
- change.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
- change.Trustee.TrusteeType = TRUSTEE_IS_USER;
- change.Trustee.ptstrName = L"CURRENT_USER";
-
- PACL new_dacl;
- if (SetEntriesInAcl(1, &change, old_dacl, &new_dacl) != ERROR_SUCCESS) {
- LocalFree(security_descriptor);
- return false;
- }
-
- DWORD rc = SetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()),
- SE_FILE_OBJECT, DACL_SECURITY_INFORMATION,
- NULL, NULL, new_dacl, NULL);
- LocalFree(security_descriptor);
- LocalFree(new_dacl);
-
- return rc == ERROR_SUCCESS;
-#else
- NOTIMPLEMENTED();
- return false;
-#endif
-}
-
-#if defined(OS_MACOSX)
-// Linux implementation of FilePathWatcher doesn't catch attribute changes.
-// http://crbug.com/78043
-// Windows implementation of FilePathWatcher catches attribute changes that
-// don't affect the path being watched.
-// http://crbug.com/78045
-
-// Verify that changing attributes on a directory works.
-TEST_F(FilePathWatcherTest, DirAttributesChanged) {
- FilePath test_dir1(temp_dir_.path().AppendASCII("DirAttributesChangedDir1"));
- FilePath test_dir2(test_dir1.AppendASCII("DirAttributesChangedDir2"));
- FilePath test_file(test_dir2.AppendASCII("DirAttributesChangedFile"));
- // Setup a directory hierarchy.
- ASSERT_TRUE(file_util::CreateDirectory(test_dir1));
- ASSERT_TRUE(file_util::CreateDirectory(test_dir2));
- ASSERT_TRUE(WriteFile(test_file, "content"));
-
- FilePathWatcher watcher;
- scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
- ASSERT_TRUE(SetupWatch(test_file, &watcher, delegate.get()));
-
- // We should not get notified in this case as it hasn't affected our ability
- // to access the file.
- ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, false));
- loop_.PostDelayedTask(FROM_HERE,
- new MessageLoop::QuitTask,
- TestTimeouts::tiny_timeout_ms());
- ASSERT_FALSE(WaitForEvents());
- ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, true));
-
- // We should get notified in this case because filepathwatcher can no
- // longer access the file
- ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false));
- ASSERT_TRUE(WaitForEvents());
- ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true));
-}
-
-#endif // OS_MACOSX
-} // namespace
diff --git a/content/common/file_path_watcher/file_path_watcher_inotify.cc b/content/common/file_path_watcher/file_path_watcher_inotify.cc
deleted file mode 100644
index f91756f..0000000
--- a/content/common/file_path_watcher/file_path_watcher_inotify.cc
+++ /dev/null
@@ -1,456 +0,0 @@
-// Copyright (c) 2011 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 "content/common/file_path_watcher/file_path_watcher.h"
-
-#include <errno.h>
-#include <string.h>
-#include <sys/inotify.h>
-#include <sys/ioctl.h>
-#include <sys/select.h>
-#include <unistd.h>
-
-#include <algorithm>
-#include <set>
-#include <utility>
-#include <vector>
-
-#include "base/eintr_wrapper.h"
-#include "base/file_path.h"
-#include "base/file_util.h"
-#include "base/hash_tables.h"
-#include "base/lazy_instance.h"
-#include "base/logging.h"
-#include "base/memory/scoped_ptr.h"
-#include "base/message_loop.h"
-#include "base/message_loop_proxy.h"
-#include "base/synchronization/lock.h"
-#include "base/task.h"
-#include "base/threading/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 ::base::DefaultLazyInstanceTraits<InotifyReader>;
-
- typedef std::set<FilePathWatcherImpl*> WatcherSet;
-
- InotifyReader();
- ~InotifyReader();
-
- // We keep track of which delegates want to be notified on which watches.
- base::hash_map<Watch, WatcherSet> watchers_;
-
- // Lock to protect watchers_.
- base::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 MessageLoop::DestructionObserver {
- public:
- 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) OVERRIDE;
-
- // Cancel the watch. This unregisters the instance with InotifyReader.
- virtual void Cancel() OVERRIDE;
-
- // Deletion of the FilePathWatcher will call Cancel() to dispose of this
- // object in the right thread. This also observes destruction of the required
- // cleanup thread, in case it quits before Cancel() is called.
- virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
-
- private:
- virtual ~FilePathWatcherImpl() {}
-
- // Cleans up and stops observing the |message_loop_| thread.
- void CancelOnMessageLoopThread() OVERRIDE;
-
- // 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<WatchEntry> 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<FilePathWatcher::Delegate> 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<char> 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<inotify_event*>(&buffer[i]);
- size_t event_size = sizeof(inotify_event) + event->len;
- DCHECK(i + event_size <= static_cast<size_t>(bytes_read));
- reader_->OnInotifyEvent(event);
- i += event_size;
- }
- }
- }
-
- private:
- InotifyReader* reader_;
- int inotify_fd_;
- int shutdown_fd_;
-
- DISALLOW_COPY_AND_ASSIGN(InotifyReaderTask);
-};
-
-static base::LazyInstance<InotifyReader> g_inotify_reader(
- base::LINKER_INITIALIZED);
-
-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;
-
- base::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;
-
- base::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(""));
- base::AutoLock auto_lock(lock_);
-
- for (WatcherSet::iterator watcher = watchers_[event->wd].begin();
- watcher != watchers_[event->wd].end();
- ++watcher) {
- (*watcher)->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) {
-
- if (!message_loop()->BelongsToCurrentThread()) {
- // Switch to message_loop_ to access watches_ safely.
- message_loop()->PostTask(FROM_HERE,
- NewRunnableMethod(this,
- &FilePathWatcherImpl::OnFilePathChanged,
- fired_watch,
- child,
- created,
- is_directory));
- return;
- }
-
- DCHECK(MessageLoopForIO::current());
-
- // 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_->OnFilePathError(target_);
- 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(target_.empty());
- DCHECK(MessageLoopForIO::current());
-
- set_message_loop(base::MessageLoopProxy::CreateForCurrentThread());
- delegate_ = delegate;
- target_ = path;
- MessageLoop::current()->AddDestructionObserver(this);
-
- std::vector<FilePath::StringType> comps;
- target_.GetComponents(&comps);
- DCHECK(!comps.empty());
- for (std::vector<FilePath::StringType>::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() {
- if (!delegate_) {
- // Watch was never called, or the |message_loop_| thread is already gone.
- set_cancelled();
- return;
- }
-
- // Switch to the message_loop_ if necessary so we can access |watches_|.
- if (!message_loop()->BelongsToCurrentThread()) {
- message_loop()->PostTask(FROM_HERE,
- new FilePathWatcher::CancelTask(this));
- } else {
- CancelOnMessageLoopThread();
- }
-}
-
-void FilePathWatcherImpl::CancelOnMessageLoopThread() {
- if (!is_cancelled()) {
- set_cancelled();
- MessageLoop::current()->RemoveDestructionObserver(this);
-
- for (WatchVector::iterator watch_entry(watches_.begin());
- watch_entry != watches_.end(); ++watch_entry) {
- if (watch_entry->watch_ != InotifyReader::kInvalidWatch)
- g_inotify_reader.Get().RemoveWatch(watch_entry->watch_, this);
- }
- watches_.clear();
- delegate_ = NULL;
- target_.clear();
- }
-}
-
-void FilePathWatcherImpl::WillDestroyCurrentMessageLoop() {
- CancelOnMessageLoopThread();
-}
-
-bool FilePathWatcherImpl::UpdateWatches() {
- // Ensure this runs on the message_loop_ exclusively in order to avoid
- // concurrency issues.
- DCHECK(message_loop()->BelongsToCurrentThread());
-
- // 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_ = g_inotify_reader.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_) {
- g_inotify_reader.Get().RemoveWatch(old_watch, this);
- }
- path = path.Append(watch_entry->subdir_);
- }
-
- return true;
-}
-
-} // namespace
-
-FilePathWatcher::FilePathWatcher() {
- impl_ = new FilePathWatcherImpl();
-}
diff --git a/content/common/file_path_watcher/file_path_watcher_mac.cc b/content/common/file_path_watcher/file_path_watcher_mac.cc
deleted file mode 100644
index eb72b94..0000000
--- a/content/common/file_path_watcher/file_path_watcher_mac.cc
+++ /dev/null
@@ -1,487 +0,0 @@
-// Copyright (c) 2011 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 "content/common/file_path_watcher/file_path_watcher.h"
-
-#include <fcntl.h>
-#include <sys/event.h>
-#include <sys/param.h>
-
-#include <vector>
-
-#include "base/file_util.h"
-#include "base/message_loop.h"
-#include "base/message_loop_proxy.h"
-#include "base/stringprintf.h"
-
-namespace {
-
-// Mac-specific file watcher implementation based on kqueue.
-// Originally it was based on FSEvents so that the semantics were equivalent
-// on Linux, OSX and Windows where it was able to detect:
-// - file creation/deletion/modification in a watched directory
-// - file creation/deletion/modification for a watched file
-// - modifications to the paths to a watched object that would affect the
-// object such as renaming/attibute changes etc.
-// The FSEvents version did all of the above except handling attribute changes
-// to path components. Unfortunately FSEvents appears to have an issue where the
-// current implementation (Mac OS X 10.6.7) sometimes drops events and doesn't
-// send notifications. See
-// http://code.google.com/p/chromium/issues/detail?id=54822#c31 for source that
-// will reproduce the problem. FSEvents also required having a CFRunLoop
-// backing the thread that it was running on, that caused added complexity
-// in the interfaces.
-// The kqueue implementation will handle all of the items in the list above
-// except for detecting modifications to files in a watched directory. It will
-// detect the creation and deletion of files, just not the modification of
-// files. It does however detect the attribute changes that the FSEvents impl
-// would miss.
-class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate,
- public MessageLoopForIO::Watcher,
- public MessageLoop::DestructionObserver {
- public:
- FilePathWatcherImpl() : kqueue_(-1) {}
- virtual ~FilePathWatcherImpl() {}
-
- // MessageLoopForIO::Watcher overrides.
- virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
- virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
-
- // MessageLoop::DestructionObserver overrides.
- virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
-
- // FilePathWatcher::PlatformDelegate overrides.
- virtual bool Watch(const FilePath& path,
- FilePathWatcher::Delegate* delegate) OVERRIDE;
- virtual void Cancel() OVERRIDE;
-
- private:
- class EventData {
- public:
- EventData(const FilePath& path, const FilePath::StringType& subdir)
- : path_(path), subdir_(subdir) { }
- FilePath path_; // Full path to this item.
- FilePath::StringType subdir_; // Path to any sub item.
- };
- typedef std::vector<struct kevent> EventVector;
-
- // Can only be called on |io_message_loop_|'s thread.
- virtual void CancelOnMessageLoopThread() OVERRIDE;
-
- // Returns true if the kevent values are error free.
- bool AreKeventValuesValid(struct kevent* kevents, int count);
-
- // Respond to a change of attributes of the path component represented by
- // |event|. Sets |target_file_affected| to true if |target_| is affected.
- // Sets |update_watches| to true if |events_| need to be updated.
- void HandleAttributesChange(const EventVector::iterator& event,
- bool* target_file_affected,
- bool* update_watches);
-
- // Respond to a move of deletion of the path component represented by
- // |event|. Sets |target_file_affected| to true if |target_| is affected.
- // Sets |update_watches| to true if |events_| need to be updated.
- void HandleDeleteOrMoveChange(const EventVector::iterator& event,
- bool* target_file_affected,
- bool* update_watches);
-
- // Respond to a creation of an item in the path component represented by
- // |event|. Sets |target_file_affected| to true if |target_| is affected.
- // Sets |update_watches| to true if |events_| need to be updated.
- void HandleCreateItemChange(const EventVector::iterator& event,
- bool* target_file_affected,
- bool* update_watches);
-
- // Update |events_| with the current status of the system.
- // Sets |target_file_affected| to true if |target_| is affected.
- // Returns false if an error occurs.
- bool UpdateWatches(bool* target_file_affected);
-
- // Fills |events| with one kevent per component in |path|.
- // Returns the number of valid events created where a valid event is
- // defined as one that has a ident (file descriptor) field != -1.
- static int EventsForPath(FilePath path, EventVector *events);
-
- // Release a kevent generated by EventsForPath.
- static void ReleaseEvent(struct kevent& event);
-
- // Returns a file descriptor that will not block the system from deleting
- // the file it references.
- static int FileDescriptorForPath(const FilePath& path);
-
- // Closes |*fd| and sets |*fd| to -1.
- static void CloseFileDescriptor(int* fd);
-
- // Returns true if kevent has open file descriptor.
- static bool IsKeventFileDescriptorOpen(const struct kevent& event) {
- return event.ident != static_cast<uintptr_t>(-1);
- }
-
- static EventData* EventDataForKevent(const struct kevent& event) {
- return reinterpret_cast<EventData*>(event.udata);
- }
-
- EventVector events_;
- scoped_refptr<base::MessageLoopProxy> io_message_loop_;
- MessageLoopForIO::FileDescriptorWatcher kqueue_watcher_;
- scoped_refptr<FilePathWatcher::Delegate> delegate_;
- FilePath target_;
- int kqueue_;
-
- DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl);
-};
-
-void FilePathWatcherImpl::ReleaseEvent(struct kevent& event) {
- CloseFileDescriptor(reinterpret_cast<int*>(&event.ident));
- EventData* entry = EventDataForKevent(event);
- delete entry;
- event.udata = NULL;
-}
-
-int FilePathWatcherImpl::EventsForPath(FilePath path, EventVector* events) {
- DCHECK(MessageLoopForIO::current());
- // Make sure that we are working with a clean slate.
- DCHECK(events->empty());
-
- std::vector<FilePath::StringType> components;
- path.GetComponents(&components);
-
- if (components.size() < 1) {
- return -1;
- }
-
- int last_existing_entry = 0;
- FilePath built_path;
- bool path_still_exists = true;
- for(std::vector<FilePath::StringType>::iterator i = components.begin();
- i != components.end(); ++i) {
- if (i == components.begin()) {
- built_path = FilePath(*i);
- } else {
- built_path = built_path.Append(*i);
- }
- int fd = -1;
- if (path_still_exists) {
- fd = FileDescriptorForPath(built_path);
- if (fd == -1) {
- path_still_exists = false;
- } else {
- ++last_existing_entry;
- }
- }
- FilePath::StringType subdir = (i != (components.end() - 1)) ? *(i + 1) : "";
- EventData* data = new EventData(built_path, subdir);
- struct kevent event;
- EV_SET(&event, fd, EVFILT_VNODE, (EV_ADD | EV_CLEAR | EV_RECEIPT),
- (NOTE_DELETE | NOTE_WRITE | NOTE_ATTRIB |
- NOTE_RENAME | NOTE_REVOKE | NOTE_EXTEND), 0, data);
- events->push_back(event);
- }
- return last_existing_entry;
-}
-
-int FilePathWatcherImpl::FileDescriptorForPath(const FilePath& path) {
- return HANDLE_EINTR(open(path.value().c_str(), O_EVTONLY));
-}
-
-void FilePathWatcherImpl::CloseFileDescriptor(int *fd) {
- if (*fd == -1) {
- return;
- }
-
- if (HANDLE_EINTR(close(*fd)) != 0) {
- PLOG(ERROR) << "close";
- }
- *fd = -1;
-}
-
-bool FilePathWatcherImpl::AreKeventValuesValid(struct kevent* kevents,
- int count) {
- if (count < 0) {
- PLOG(ERROR) << "kevent";
- return false;
- }
- bool valid = true;
- for (int i = 0; i < count; ++i) {
- if (kevents[i].flags & EV_ERROR && kevents[i].data) {
- // Find the kevent in |events_| that matches the kevent with the error.
- EventVector::iterator event = events_.begin();
- for (; event != events_.end(); ++event) {
- if (event->ident == kevents[i].ident) {
- break;
- }
- }
- std::string path_name;
- if (event != events_.end()) {
- EventData* event_data = EventDataForKevent(*event);
- if (event_data != NULL) {
- path_name = event_data->path_.value();
- }
- }
- if (path_name.empty()) {
- path_name = base::StringPrintf(
- "fd %d", *reinterpret_cast<int*>(&kevents[i].ident));
- }
- LOG(ERROR) << "Error: " << kevents[i].data << " for " << path_name;
- valid = false;
- }
- }
- return valid;
-}
-
-void FilePathWatcherImpl::HandleAttributesChange(
- const EventVector::iterator& event,
- bool* target_file_affected,
- bool* update_watches) {
- EventVector::iterator next_event = event + 1;
- EventData* next_event_data = EventDataForKevent(*next_event);
- // Check to see if the next item in path is still accessible.
- int have_access = FileDescriptorForPath(next_event_data->path_);
- if (have_access == -1) {
- *target_file_affected = true;
- *update_watches = true;
- EventVector::iterator local_event(event);
- for (; local_event != events_.end(); ++local_event) {
- // Close all nodes from the event down. This has the side effect of
- // potentially rendering other events in |updates| invalid.
- // There is no need to remove the events from |kqueue_| because this
- // happens as a side effect of closing the file descriptor.
- CloseFileDescriptor(reinterpret_cast<int*>(&local_event->ident));
- }
- } else {
- CloseFileDescriptor(&have_access);
- }
-}
-
-void FilePathWatcherImpl::HandleDeleteOrMoveChange(
- const EventVector::iterator& event,
- bool* target_file_affected,
- bool* update_watches) {
- *target_file_affected = true;
- *update_watches = true;
- EventVector::iterator local_event(event);
- for (; local_event != events_.end(); ++local_event) {
- // Close all nodes from the event down. This has the side effect of
- // potentially rendering other events in |updates| invalid.
- // There is no need to remove the events from |kqueue_| because this
- // happens as a side effect of closing the file descriptor.
- CloseFileDescriptor(reinterpret_cast<int*>(&local_event->ident));
- }
-}
-
-void FilePathWatcherImpl::HandleCreateItemChange(
- const EventVector::iterator& event,
- bool* target_file_affected,
- bool* update_watches) {
- // Get the next item in the path.
- EventVector::iterator next_event = event + 1;
- EventData* next_event_data = EventDataForKevent(*next_event);
-
- // Check to see if it already has a valid file descriptor.
- if (!IsKeventFileDescriptorOpen(*next_event)) {
- // If not, attempt to open a file descriptor for it.
- next_event->ident = FileDescriptorForPath(next_event_data->path_);
- if (IsKeventFileDescriptorOpen(*next_event)) {
- *update_watches = true;
- if (next_event_data->subdir_.empty()) {
- *target_file_affected = true;
- }
- }
- }
-}
-
-bool FilePathWatcherImpl::UpdateWatches(bool* target_file_affected) {
- // Iterate over events adding kevents for items that exist to the kqueue.
- // Then check to see if new components in the path have been created.
- // Repeat until no new components in the path are detected.
- // This is to get around races in directory creation in a watched path.
- bool update_watches = true;
- while (update_watches) {
- size_t valid;
- for (valid = 0; valid < events_.size(); ++valid) {
- if (!IsKeventFileDescriptorOpen(events_[valid])) {
- break;
- }
- }
- if (valid == 0) {
- // The root of the file path is inaccessible?
- return false;
- }
-
- EventVector updates(valid);
- int count = HANDLE_EINTR(kevent(kqueue_, &events_[0], valid, &updates[0],
- valid, NULL));
- if (!AreKeventValuesValid(&updates[0], count)) {
- return false;
- }
- update_watches = false;
- for (; valid < events_.size(); ++valid) {
- EventData* event_data = EventDataForKevent(events_[valid]);
- events_[valid].ident = FileDescriptorForPath(event_data->path_);
- if (IsKeventFileDescriptorOpen(events_[valid])) {
- update_watches = true;
- if (event_data->subdir_.empty()) {
- *target_file_affected = true;
- }
- } else {
- break;
- }
- }
- }
- return true;
-}
-
-void FilePathWatcherImpl::OnFileCanReadWithoutBlocking(int fd) {
- DCHECK(MessageLoopForIO::current());
- CHECK_EQ(fd, kqueue_);
- CHECK(events_.size());
-
- // Request the file system update notifications that have occurred and return
- // them in |updates|. |count| will contain the number of updates that have
- // occurred.
- EventVector updates(events_.size());
- struct timespec timeout = {0, 0};
- int count = HANDLE_EINTR(kevent(kqueue_, NULL, 0, &updates[0], updates.size(),
- &timeout));
-
- // Error values are stored within updates, so check to make sure that no
- // errors occurred.
- if (!AreKeventValuesValid(&updates[0], count)) {
- delegate_->OnFilePathError(target_);
- Cancel();
- return;
- }
-
- bool update_watches = false;
- bool send_notification = false;
-
- // Iterate through each of the updates and react to them.
- for (int i = 0; i < count; ++i) {
- // Find our kevent record that matches the update notification.
- EventVector::iterator event = events_.begin();
- for (; event != events_.end(); ++event) {
- if (!IsKeventFileDescriptorOpen(*event) ||
- event->ident == updates[i].ident) {
- break;
- }
- }
- if (!IsKeventFileDescriptorOpen(*event) || event == events_.end()) {
- // The event may no longer exist in |events_| because another event
- // modified |events_| in such a way to make it invalid. For example if
- // the path is /foo/bar/bam and foo is deleted, NOTE_DELETE events for
- // foo, bar and bam will be sent. If foo is processed first, then
- // the file descriptors for bar and bam will already be closed and set
- // to -1 before they get a chance to be processed.
- continue;
- }
-
- EventData* event_data = EventDataForKevent(*event);
-
- // If the subdir is empty, this is the last item on the path and is the
- // target file.
- bool target_file_affected = event_data->subdir_.empty();
- if ((updates[i].fflags & NOTE_ATTRIB) && !target_file_affected) {
- HandleAttributesChange(event, &target_file_affected, &update_watches);
- }
- if (updates[i].fflags & (NOTE_DELETE | NOTE_REVOKE | NOTE_RENAME)) {
- HandleDeleteOrMoveChange(event, &target_file_affected, &update_watches);
- }
- if ((updates[i].fflags & NOTE_WRITE) && !target_file_affected) {
- HandleCreateItemChange(event, &target_file_affected, &update_watches);
- }
- send_notification |= target_file_affected;
- }
-
- if (update_watches) {
- if (!UpdateWatches(&send_notification)) {
- delegate_->OnFilePathError(target_);
- Cancel();
- }
- }
-
- if (send_notification) {
- delegate_->OnFilePathChanged(target_);
- }
-}
-
-void FilePathWatcherImpl::OnFileCanWriteWithoutBlocking(int fd) {
- NOTREACHED();
-}
-
-void FilePathWatcherImpl::WillDestroyCurrentMessageLoop() {
- CancelOnMessageLoopThread();
-}
-
-bool FilePathWatcherImpl::Watch(const FilePath& path,
- FilePathWatcher::Delegate* delegate) {
- DCHECK(MessageLoopForIO::current());
- DCHECK(target_.value().empty()); // Can only watch one path.
- DCHECK(delegate);
- DCHECK_EQ(kqueue_, -1);
-
- delegate_ = delegate;
- target_ = path;
-
- MessageLoop::current()->AddDestructionObserver(this);
- io_message_loop_ = base::MessageLoopProxy::CreateForCurrentThread();
-
- kqueue_ = kqueue();
- if (kqueue_ == -1) {
- PLOG(ERROR) << "kqueue";
- return false;
- }
-
- int last_entry = EventsForPath(target_, &events_);
- CHECK_NE(last_entry, 0);
-
- EventVector responses(last_entry);
-
- int count = HANDLE_EINTR(kevent(kqueue_, &events_[0], last_entry,
- &responses[0], last_entry, NULL));
- if (!AreKeventValuesValid(&responses[0], count)) {
- // Calling Cancel() here to close any file descriptors that were opened.
- // This would happen in the destructor anyways, but FilePathWatchers tend to
- // be long lived, and if an error has occurred, there is no reason to waste
- // the file descriptors.
- Cancel();
- return false;
- }
-
- return MessageLoopForIO::current()->WatchFileDescriptor(
- kqueue_, true, MessageLoopForIO::WATCH_READ, &kqueue_watcher_, this);
-}
-
-void FilePathWatcherImpl::Cancel() {
- base::MessageLoopProxy* proxy = io_message_loop_.get();
- if (!proxy) {
- set_cancelled();
- return;
- }
- if (!proxy->BelongsToCurrentThread()) {
- proxy->PostTask(FROM_HERE,
- NewRunnableMethod(this, &FilePathWatcherImpl::Cancel));
- return;
- }
- CancelOnMessageLoopThread();
-}
-
-void FilePathWatcherImpl::CancelOnMessageLoopThread() {
- DCHECK(MessageLoopForIO::current());
- if (!is_cancelled()) {
- set_cancelled();
- kqueue_watcher_.StopWatchingFileDescriptor();
- CloseFileDescriptor(&kqueue_);
- std::for_each(events_.begin(), events_.end(), ReleaseEvent);
- events_.clear();
- io_message_loop_ = NULL;
- MessageLoop::current()->RemoveDestructionObserver(this);
- delegate_ = NULL;
- }
-}
-
-} // namespace
-
-FilePathWatcher::FilePathWatcher() {
- impl_ = new FilePathWatcherImpl();
-}
diff --git a/content/common/file_path_watcher/file_path_watcher_stub.cc b/content/common/file_path_watcher/file_path_watcher_stub.cc
deleted file mode 100644
index 42b6ec6..0000000
--- a/content/common/file_path_watcher/file_path_watcher_stub.cc
+++ /dev/null
@@ -1,21 +0,0 @@
-// Copyright (c) 2011 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/common/file_path_watcher/file_path_watcher.h"
-
-class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate {
- public:
- virtual bool Watch(const FilePath& path,
- FileWatcher::Delegate* delegate,
- base::MessageLoopProxy*) OVERRIDE {
- return false;
- }
-};
-
-FilePathWatcher::FilePathWatcher() {
- impl_ = new FilePathWatcherImpl();
-}
diff --git a/content/common/file_path_watcher/file_path_watcher_win.cc b/content/common/file_path_watcher/file_path_watcher_win.cc
deleted file mode 100644
index bf24e18..0000000
--- a/content/common/file_path_watcher/file_path_watcher_win.cc
+++ /dev/null
@@ -1,274 +0,0 @@
-// Copyright (c) 2011 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 "content/common/file_path_watcher/file_path_watcher.h"
-
-#include "base/file_path.h"
-#include "base/file_util.h"
-#include "base/logging.h"
-#include "base/memory/ref_counted.h"
-#include "base/message_loop_proxy.h"
-#include "base/time.h"
-#include "base/win/object_watcher.h"
-
-namespace {
-
-class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate,
- public base::win::ObjectWatcher::Delegate,
- public MessageLoop::DestructionObserver {
- public:
- FilePathWatcherImpl() : delegate_(NULL), handle_(INVALID_HANDLE_VALUE) {}
-
- // FilePathWatcher::PlatformDelegate overrides.
- virtual bool Watch(const FilePath& path,
- FilePathWatcher::Delegate* delegate) OVERRIDE;
- virtual void Cancel() OVERRIDE;
-
- // Deletion of the FilePathWatcher will call Cancel() to dispose of this
- // object in the right thread. This also observes destruction of the required
- // cleanup thread, in case it quits before Cancel() is called.
- virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
-
- // 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();
-
- // Cleans up and stops observing the |message_loop_| thread.
- void CancelOnMessageLoopThread() OVERRIDE;
-
- // Delegate to notify upon changes.
- scoped_refptr<FilePathWatcher::Delegate> delegate_;
-
- // Path we're supposed to watch (passed to delegate).
- FilePath target_;
-
- // Handle for FindFirstChangeNotification.
- HANDLE handle_;
-
- // ObjectWatcher to watch handle_ for events.
- base::win::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.
-
- set_message_loop(base::MessageLoopProxy::CreateForCurrentThread());
- delegate_ = delegate;
- target_ = path;
- MessageLoop::current()->AddDestructionObserver(this);
-
- if (!UpdateWatch())
- return false;
-
- watcher_.StartWatching(handle_, this);
-
- return true;
-}
-
-void FilePathWatcherImpl::Cancel() {
- if (!delegate_) {
- // Watch was never called, or the |message_loop_| has already quit.
- set_cancelled();
- return;
- }
-
- // Switch to the file thread if necessary so we can stop |watcher_|.
- if (!message_loop()->BelongsToCurrentThread()) {
- message_loop()->PostTask(FROM_HERE,
- new FilePathWatcher::CancelTask(this));
- } else {
- CancelOnMessageLoopThread();
- }
-}
-
-void FilePathWatcherImpl::CancelOnMessageLoopThread() {
- set_cancelled();
-
- if (handle_ != INVALID_HANDLE_VALUE)
- DestroyWatch();
-
- if (delegate_) {
- MessageLoop::current()->RemoveDestructionObserver(this);
- delegate_ = NULL;
- }
-}
-
-void FilePathWatcherImpl::WillDestroyCurrentMessageLoop() {
- CancelOnMessageLoopThread();
-}
-
-void FilePathWatcherImpl::OnObjectSignaled(HANDLE object) {
- DCHECK(object == handle_);
- // Make sure we stay alive through the body of this function.
- scoped_refptr<FilePathWatcherImpl> keep_alive(this);
-
- if (!UpdateWatch()) {
- delegate_->OnFilePathError(target_);
- return;
- }
-
- // Check whether the event applies to |target_| and notify the delegate.
- base::PlatformFileInfo 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);
-}
-
-// 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_SHARING_VIOLATION &&
- error_code != ERROR_DIRECTORY) {
- PLOG(ERROR) << "FindFirstChangeNotification failed for "
- << dir.value();
- return false;
- }
-
- return true;
-}
-
-bool FilePathWatcherImpl::UpdateWatch() {
- if (handle_ != INVALID_HANDLE_VALUE)
- DestroyWatch();
-
- base::PlatformFileInfo 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<FilePath> 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/content/content_common.gypi b/content/content_common.gypi
index f0e16a1..1047715 100644
--- a/content/content_common.gypi
+++ b/content/content_common.gypi
@@ -73,11 +73,6 @@
'common/dx_diag_node.cc',
'common/dx_diag_node.h',
'common/edit_command.h',
- 'common/file_path_watcher/file_path_watcher.cc',
- 'common/file_path_watcher/file_path_watcher.h',
- 'common/file_path_watcher/file_path_watcher_inotify.cc',
- 'common/file_path_watcher/file_path_watcher_mac.cc',
- 'common/file_path_watcher/file_path_watcher_win.cc',
'common/file_system/file_system_dispatcher.cc',
'common/file_system/file_system_dispatcher.h',
'common/file_system/webfilesystem_callback_dispatcher.cc',
@@ -234,16 +229,6 @@
'../third_party/angle/src/build_angle.gyp:libGLESv2',
],
}],
- ['OS!="linux"', {
- 'sources!': [
- 'common/file_path_watcher/file_path_watcher_inotify.cc',
- ],
- }],
- ['OS=="freebsd" or OS=="openbsd"', {
- 'sources': [
- 'common/file_path_watcher/file_path_watcher_stub.cc',
- ],
- }],
['OS=="mac"', {
'sources!': [
'common/process_watcher_posix.cc',