diff options
author | szym@chromium.org <szym@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-05 19:40:12 +0000 |
---|---|---|
committer | szym@chromium.org <szym@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-05 19:40:12 +0000 |
commit | 8ea5ac1520ec4b30b7b9f831e2c18ebc14834a56 (patch) | |
tree | e0f6792e4c7858b9979476a46f3df3e0dff75e85 /net/dns | |
parent | a8ce4de87a7f9c766e25d2b8b9a75c0fcce9a4b8 (diff) | |
download | chromium_src-8ea5ac1520ec4b30b7b9f831e2c18ebc14834a56.zip chromium_src-8ea5ac1520ec4b30b7b9f831e2c18ebc14834a56.tar.gz chromium_src-8ea5ac1520ec4b30b7b9f831e2c18ebc14834a56.tar.bz2 |
[net/dns] Remove FilePathWatcherWrapper after refactoring of FilePathWatcher.
BUG=130980
TEST=chrome --enable-async-dns
Review URL: https://chromiumcodereview.appspot.com/10511016
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@140584 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/dns')
-rw-r--r-- | net/dns/dns_config_service_posix.cc | 1 | ||||
-rw-r--r-- | net/dns/dns_config_watcher_posix.cc | 26 | ||||
-rw-r--r-- | net/dns/dns_config_watcher_win.cc | 18 | ||||
-rw-r--r-- | net/dns/file_path_watcher_wrapper.cc | 99 | ||||
-rw-r--r-- | net/dns/file_path_watcher_wrapper.h | 66 | ||||
-rw-r--r-- | net/dns/file_path_watcher_wrapper_unittest.cc | 142 |
6 files changed, 27 insertions, 325 deletions
diff --git a/net/dns/dns_config_service_posix.cc b/net/dns/dns_config_service_posix.cc index 11a48a5..a0571912 100644 --- a/net/dns/dns_config_service_posix.cc +++ b/net/dns/dns_config_service_posix.cc @@ -13,7 +13,6 @@ #include "base/memory/scoped_ptr.h" #include "net/base/ip_endpoint.h" #include "net/base/net_util.h" -#include "net/dns/file_path_watcher_wrapper.h" #include "net/dns/serial_worker.h" namespace net { diff --git a/net/dns/dns_config_watcher_posix.cc b/net/dns/dns_config_watcher_posix.cc index ebebf51..3c0d118 100644 --- a/net/dns/dns_config_watcher_posix.cc +++ b/net/dns/dns_config_watcher_posix.cc @@ -9,11 +9,11 @@ #include "base/basictypes.h" #include "base/bind.h" #include "base/callback.h" +#include "base/files/file_path_watcher.h" #include "base/file_path.h" #include "base/logging.h" #include "base/memory/scoped_ptr.h" #include "net/base/network_change_notifier.h" -#include "net/dns/file_path_watcher_wrapper.h" #if defined(OS_MACOSX) #include "net/dns/notify_watcher_mac.h" @@ -36,6 +36,7 @@ class ConfigWatcher { bool Watch(const base::Callback<void(bool succeeded)>& callback) { return watcher_.Watch(kDnsNotifyKey, callback); } + private: NotifyWatcherMac watcher_; }; @@ -50,11 +51,22 @@ static const FilePath::CharType* kFilePathConfig = class ConfigWatcher { public: - bool Watch(const base::Callback<void(bool succeeded)>& callback) { - return watcher_.Watch(FilePath(kFilePathConfig), callback); + typedef base::Callback<void(bool succeeded)> CallbackType; + + bool Watch(const CallbackType& callback) { + callback_ = callback; + return watcher_.Watch(FilePath(kFilePathConfig), + base::Bind(&ConfigWatcher::OnCallback, + base::Unretained(this))); } + private: - FilePathWatcherWrapper watcher_; + void OnCallback(const FilePath& path, bool error) { + callback_.Run(!error); + } + + base::files::FilePathWatcher watcher_; + CallbackType callback_; }; #endif @@ -93,8 +105,8 @@ class DnsConfigWatcher::Core { } } - void OnHostsChanged(bool succeeded) { - if (succeeded) { + void OnHostsChanged(const FilePath& path, bool error) { + if (!error) { NetworkChangeNotifier::NotifyObserversOfDNSChange( NetworkChangeNotifier::CHANGE_DNS_HOSTS); } else { @@ -105,7 +117,7 @@ class DnsConfigWatcher::Core { } ConfigWatcher config_watcher_; - FilePathWatcherWrapper hosts_watcher_; + base::files::FilePathWatcher hosts_watcher_; DISALLOW_COPY_AND_ASSIGN(Core); }; diff --git a/net/dns/dns_config_watcher_win.cc b/net/dns/dns_config_watcher_win.cc index 47a12d0..2cd41e9 100644 --- a/net/dns/dns_config_watcher_win.cc +++ b/net/dns/dns_config_watcher_win.cc @@ -11,6 +11,7 @@ #include "base/bind.h" #include "base/callback.h" #include "base/compiler_specific.h" +#include "base/files/file_path_watcher.h" #include "base/file_path.h" #include "base/logging.h" #include "base/memory/scoped_ptr.h" @@ -21,7 +22,6 @@ #include "net/base/net_util.h" #include "net/base/network_change_notifier.h" #include "net/dns/dns_config_service_win.h" -#include "net/dns/file_path_watcher_wrapper.h" namespace net { namespace internal { @@ -53,12 +53,10 @@ class RegistryWatcher : public base::win::ObjectWatcher::Delegate, DCHECK(CalledOnValidThread()); bool succeeded = (key_.StartWatching() == ERROR_SUCCESS) && watcher_.StartWatching(key_.watch_event(), this); - if (!succeeded) { - if (key_.Valid()) { - watcher_.StopWatching(); - key_.StopWatching(); - key_.Close(); - } + if (!succeeded && key_.Valid()) { + watcher_.StopWatching(); + key_.StopWatching(); + key_.Close(); } if (!callback_.is_null()) callback_.Run(succeeded); @@ -125,8 +123,8 @@ class DnsConfigWatcher::Core { } } - void OnHostsChanged(bool succeeded) { - if (succeeded) { + void OnHostsChanged(const FilePath& path, bool error) { + if (!error) { NetworkChangeNotifier::NotifyObserversOfDNSChange( NetworkChangeNotifier::CHANGE_DNS_HOSTS); } else { @@ -140,7 +138,7 @@ class DnsConfigWatcher::Core { RegistryWatcher tcpip6_watcher_; RegistryWatcher dnscache_watcher_; RegistryWatcher policy_watcher_; - FilePathWatcherWrapper hosts_watcher_; + base::files::FilePathWatcher hosts_watcher_; DISALLOW_COPY_AND_ASSIGN(Core); }; diff --git a/net/dns/file_path_watcher_wrapper.cc b/net/dns/file_path_watcher_wrapper.cc deleted file mode 100644 index 75879db..0000000 --- a/net/dns/file_path_watcher_wrapper.cc +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright (c) 2012 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 "net/dns/file_path_watcher_wrapper.h" - -#include "base/callback.h" -#include "base/files/file_path_watcher.h" -#include "base/logging.h" - -namespace net { - -// A FilePathWatcher::Delegate that forwards calls to FilePathWatcherCallback. -class FilePathWatcherWrapper::WatcherDelegate - : public base::files::FilePathWatcher::Delegate { - public: - explicit WatcherDelegate(FilePathWatcherWrapper* watcher) - : watcher_(watcher) {} - - void Cancel() { - watcher_ = NULL; - } - - void OnFilePathChanged(const FilePath& path) OVERRIDE { - if (watcher_) - watcher_->OnFilePathChanged(); - } - - void OnFilePathError(const FilePath& path) OVERRIDE { - if (watcher_) - watcher_->OnFilePathError(); - } - - private: - virtual ~WatcherDelegate() {} - FilePathWatcherWrapper* watcher_; -}; - -FilePathWatcherWrapper::FilePathWatcherWrapper() { - // No need to restrict the thread until the state is created in Watch. - DetachFromThread(); -} - -FilePathWatcherWrapper::~FilePathWatcherWrapper() { - Cancel(); -} - -void FilePathWatcherWrapper::Cancel() { - DCHECK(CalledOnValidThread()); - if (delegate_) { - delegate_->Cancel(); - delegate_ = NULL; - } - watcher_.reset(NULL); - // All state is gone so it's okay to detach from thread. - DetachFromThread(); -} - -bool FilePathWatcherWrapper::Watch( - const FilePath& path, - const base::Callback<void(bool succeeded)>& callback) { - DCHECK(CalledOnValidThread()); - if (delegate_) - delegate_->Cancel(); - callback_ = callback; - watcher_ = this->CreateFilePathWatcher(); - delegate_ = new WatcherDelegate(this); - if (watcher_->Watch(path, delegate_.get())) - return true; - - Cancel(); - return false; -} - -bool FilePathWatcherWrapper::IsWatching() const { - DCHECK(CalledOnValidThread()); - return watcher_.get() != NULL; -} - -scoped_ptr<base::files::FilePathWatcher> - FilePathWatcherWrapper::CreateFilePathWatcher() { - return scoped_ptr<base::files::FilePathWatcher>( - new base::files::FilePathWatcher()); -} - -void FilePathWatcherWrapper::OnFilePathChanged() { - DCHECK(CalledOnValidThread()); - callback_.Run(true); -} - -void FilePathWatcherWrapper::OnFilePathError() { - DCHECK(CalledOnValidThread()); - base::Callback<void(bool succeeded)> callback = callback_; - Cancel(); - callback.Run(false); -} - -} // namespace net - diff --git a/net/dns/file_path_watcher_wrapper.h b/net/dns/file_path_watcher_wrapper.h deleted file mode 100644 index b960c45..0000000 --- a/net/dns/file_path_watcher_wrapper.h +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright (c) 2012 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. - -#ifndef NET_DNS_FILE_PATH_WATCHER_WRAPPER_H_ -#define NET_DNS_FILE_PATH_WATCHER_WRAPPER_H_ -#pragma once - -#include <string> - -#include "base/basictypes.h" -#include "base/callback.h" -#include "base/file_path.h" -#include "base/memory/ref_counted.h" -#include "base/memory/scoped_ptr.h" -#include "base/threading/non_thread_safe.h" -#include "net/base/net_export.h" - -namespace base { -namespace files { -class FilePathWatcher; -} -} - -namespace net { - -// Convenience wrapper which holds a FilePathWatcher and a -// FilePathWatcher::Delegate which forwards its calls to a Callback. -class NET_EXPORT FilePathWatcherWrapper - : NON_EXPORTED_BASE(public base::NonThreadSafe) { - public: - FilePathWatcherWrapper(); - // When deleted, automatically cancels the FilePathWatcher. - virtual ~FilePathWatcherWrapper(); - - // Starts watching the file at |path|. Returns true if Watch succeeds. - // If so, |callback| will be called with true on change and false on error. - // After failure the watch is cancelled and will have to be restarted. - bool Watch(const FilePath& path, - const base::Callback<void(bool succeeded)>& callback); - - bool IsWatching() const; - - // Cancels the current watch. - void Cancel(); - - protected: - // Creates a FilePathWatcher. Override to provide a different implementation. - virtual scoped_ptr<base::files::FilePathWatcher> CreateFilePathWatcher(); - - private: - class WatcherDelegate; - - void OnFilePathChanged(); - void OnFilePathError(); - - base::Callback<void(bool succeeded)> callback_; - scoped_ptr<base::files::FilePathWatcher> watcher_; - scoped_refptr<WatcherDelegate> delegate_; - DISALLOW_COPY_AND_ASSIGN(FilePathWatcherWrapper); -}; - -} // namespace net - -#endif // NET_DNS_FILE_PATH_WATCHER_WRAPPER_H_ - diff --git a/net/dns/file_path_watcher_wrapper_unittest.cc b/net/dns/file_path_watcher_wrapper_unittest.cc deleted file mode 100644 index e925e27..0000000 --- a/net/dns/file_path_watcher_wrapper_unittest.cc +++ /dev/null @@ -1,142 +0,0 @@ -// Copyright (c) 2012 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 "net/dns/file_path_watcher_wrapper.h" - -#include "base/bind.h" -#include "base/files/file_path_watcher.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace net { - -namespace { - -class FilePathWatcherWrapperTest : public testing::Test { - protected: - class MockFilePathWatcher : public base::files::FilePathWatcher { - public: - virtual bool Watch(const FilePath& path, Delegate* delegate) OVERRIDE { - DCHECK(!delegate_); - DCHECK(delegate); - path_ = path; - delegate_ = delegate; - return true; - } - - void OnFilePathChanged() { - delegate_->OnFilePathChanged(path_); - } - - void OnFilePathError() { - delegate_->OnFilePathError(path_); - } - - private: - FilePath path_; - scoped_refptr<Delegate> delegate_; - }; - - class FailingFilePathWatcher : public MockFilePathWatcher { - public: - virtual bool Watch(const FilePath& path, Delegate* delegate) OVERRIDE { - MockFilePathWatcher::Watch(path, delegate); - return false; - } - }; - - // Class under test. - class TestFilePathWatcherWrapper : public FilePathWatcherWrapper { - public: - TestFilePathWatcherWrapper() : fail_on_watch_(false) {} - - MockFilePathWatcher* file_path_watcher() { - return test_watcher_; - } - - void set_fail_on_watch(bool value) { - fail_on_watch_ = value; - } - - private: - virtual scoped_ptr<base::files::FilePathWatcher> - CreateFilePathWatcher() OVERRIDE { - test_watcher_ = fail_on_watch_ ? new FailingFilePathWatcher() - : new MockFilePathWatcher(); - return scoped_ptr<base::files::FilePathWatcher>(test_watcher_); - } - - MockFilePathWatcher* test_watcher_; - bool fail_on_watch_; - }; - - virtual void SetUp() OVERRIDE { - watcher_wrapper_.reset(new TestFilePathWatcherWrapper()); - got_change_ = false; - } - - bool Watch() { - return watcher_wrapper_->Watch( - FilePath(FILE_PATH_LITERAL("some_file_name")), - base::Bind(&FilePathWatcherWrapperTest::OnChange, - base::Unretained(this))); - } - - void OnChange(bool succeeded) { - got_change_ = true; - last_result_ = succeeded; - } - - scoped_ptr<TestFilePathWatcherWrapper> watcher_wrapper_; - bool got_change_; - bool last_result_; -}; - -TEST_F(FilePathWatcherWrapperTest, Basic) { - watcher_wrapper_->set_fail_on_watch(true); - EXPECT_FALSE(Watch()); - EXPECT_FALSE(watcher_wrapper_->IsWatching()); - EXPECT_FALSE(got_change_); - - watcher_wrapper_->set_fail_on_watch(false); - EXPECT_TRUE(Watch()); - EXPECT_TRUE(watcher_wrapper_->IsWatching()); - EXPECT_FALSE(got_change_); - - EXPECT_TRUE(Watch()); - EXPECT_TRUE(watcher_wrapper_->IsWatching()); - EXPECT_FALSE(got_change_); - - watcher_wrapper_->file_path_watcher()->OnFilePathChanged(); - EXPECT_TRUE(watcher_wrapper_->IsWatching()); - EXPECT_TRUE(got_change_); - EXPECT_TRUE(last_result_); - - got_change_ = false; - watcher_wrapper_->file_path_watcher()->OnFilePathError(); - EXPECT_FALSE(watcher_wrapper_->IsWatching()); - EXPECT_TRUE(got_change_); - EXPECT_FALSE(last_result_); - - got_change_ = false; - EXPECT_TRUE(Watch()); - EXPECT_TRUE(watcher_wrapper_->IsWatching()); - EXPECT_FALSE(got_change_); -} - -TEST_F(FilePathWatcherWrapperTest, Cancel) { - EXPECT_TRUE(Watch()); - EXPECT_TRUE(watcher_wrapper_->IsWatching()); - - watcher_wrapper_->Cancel(); - EXPECT_FALSE(watcher_wrapper_->IsWatching()); - EXPECT_FALSE(got_change_); - - EXPECT_TRUE(Watch()); - EXPECT_TRUE(watcher_wrapper_->IsWatching()); -} - -} // namespace - -} // namespace net - |