diff options
-rw-r--r-- | remoting/host/config_file_watcher.cc | 88 | ||||
-rw-r--r-- | remoting/host/config_file_watcher.h | 27 | ||||
-rw-r--r-- | remoting/host/config_file_watcher_unittest.cc | 12 | ||||
-rw-r--r-- | remoting/host/config_watcher.h | 42 | ||||
-rw-r--r-- | remoting/host/daemon_process.cc | 20 | ||||
-rw-r--r-- | remoting/host/daemon_process.h | 9 | ||||
-rw-r--r-- | remoting/host/remoting_me2me_host.cc | 11 | ||||
-rw-r--r-- | remoting/remoting.gyp | 2 |
8 files changed, 134 insertions, 77 deletions
diff --git a/remoting/host/config_file_watcher.cc b/remoting/host/config_file_watcher.cc index a261017..29c108c 100644 --- a/remoting/host/config_file_watcher.cc +++ b/remoting/host/config_file_watcher.cc @@ -38,10 +38,11 @@ class ConfigFileWatcherImpl ConfigFileWatcherImpl( scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, - ConfigFileWatcher::Delegate* delegate); + const base::FilePath& config_path); - // Starts watching |config_path|. - void Watch(const base::FilePath& config_path); + + // Notify |delegate| of config changes. + void Watch(ConfigWatcher::Delegate* delegate); // Stops watching the configuration file. void StopWatching(); @@ -52,9 +53,15 @@ class ConfigFileWatcherImpl void FinishStopping(); + void WatchOnIoThread(); + // Called every time the host configuration file is updated. void OnConfigUpdated(const base::FilePath& path, bool error); + // Called to notify the delegate of updates/errors in the main thread. + void NotifyUpdate(const std::string& config); + void NotifyError(); + // Reads the configuration file and passes it to the delegate. void ReloadConfig(); @@ -69,24 +76,22 @@ class ConfigFileWatcherImpl // Monitors the host configuration file. scoped_ptr<base::FilePathWatcher> config_watcher_; - base::WeakPtrFactory<ConfigFileWatcher::Delegate> delegate_weak_factory_; - base::WeakPtr<ConfigFileWatcher::Delegate> delegate_; + ConfigWatcher::Delegate* delegate_; scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; + base::WeakPtrFactory<ConfigFileWatcherImpl> weak_factory_; + DISALLOW_COPY_AND_ASSIGN(ConfigFileWatcherImpl); }; -ConfigFileWatcher::Delegate::~Delegate() { -} - ConfigFileWatcher::ConfigFileWatcher( scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, - Delegate* delegate) + const base::FilePath& config_path) : impl_(new ConfigFileWatcherImpl(main_task_runner, - io_task_runner, delegate)) { + io_task_runner, config_path)) { } ConfigFileWatcher::~ConfigFileWatcher() { @@ -94,31 +99,36 @@ ConfigFileWatcher::~ConfigFileWatcher() { impl_ = NULL; } -void ConfigFileWatcher::Watch(const base::FilePath& config_path) { - impl_->Watch(config_path); +void ConfigFileWatcher::Watch(ConfigWatcher::Delegate* delegate) { + impl_->Watch(delegate); } ConfigFileWatcherImpl::ConfigFileWatcherImpl( scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, - ConfigFileWatcher::Delegate* delegate) - : retries_(0), - delegate_weak_factory_(delegate), - delegate_(delegate_weak_factory_.GetWeakPtr()), + const base::FilePath& config_path) + : config_path_(config_path), + retries_(0), + delegate_(NULL), main_task_runner_(main_task_runner), - io_task_runner_(io_task_runner) { + io_task_runner_(io_task_runner), + weak_factory_(this) { DCHECK(main_task_runner_->BelongsToCurrentThread()); } -void ConfigFileWatcherImpl::Watch(const base::FilePath& config_path) { - if (!io_task_runner_->BelongsToCurrentThread()) { - io_task_runner_->PostTask( - FROM_HERE, - base::Bind(&ConfigFileWatcherImpl::Watch, this, config_path)); - return; - } +void ConfigFileWatcherImpl::Watch(ConfigWatcher::Delegate* delegate) { + DCHECK(main_task_runner_->BelongsToCurrentThread()); + DCHECK(!delegate_); + + delegate_ = delegate; + + io_task_runner_->PostTask( + FROM_HERE, + base::Bind(&ConfigFileWatcherImpl::WatchOnIoThread, this)); +} - DCHECK(config_path_.empty()); +void ConfigFileWatcherImpl::WatchOnIoThread() { + DCHECK(io_task_runner_->BelongsToCurrentThread()); DCHECK(!config_updated_timer_); DCHECK(!config_watcher_); @@ -130,15 +140,14 @@ void ConfigFileWatcherImpl::Watch(const base::FilePath& config_path) { // Start watching the configuration file. config_watcher_.reset(new base::FilePathWatcher()); - config_path_ = config_path; if (!config_watcher_->Watch( config_path_, false, base::Bind(&ConfigFileWatcherImpl::OnConfigUpdated, this))) { PLOG(ERROR) << "Couldn't watch file '" << config_path_.value() << "'"; main_task_runner_->PostTask( FROM_HERE, - base::Bind(&ConfigFileWatcher::Delegate::OnConfigWatcherError, - delegate_)); + base::Bind(&ConfigFileWatcherImpl::NotifyError, + weak_factory_.GetWeakPtr())); return; } @@ -149,7 +158,7 @@ void ConfigFileWatcherImpl::Watch(const base::FilePath& config_path) { void ConfigFileWatcherImpl::StopWatching() { DCHECK(main_task_runner_->BelongsToCurrentThread()); - delegate_weak_factory_.InvalidateWeakPtrs(); + weak_factory_.InvalidateWeakPtrs(); io_task_runner_->PostTask( FROM_HERE, base::Bind(&ConfigFileWatcherImpl::FinishStopping, this)); } @@ -178,6 +187,18 @@ void ConfigFileWatcherImpl::OnConfigUpdated(const base::FilePath& path, config_updated_timer_->Reset(); } +void ConfigFileWatcherImpl::NotifyError() { + DCHECK(main_task_runner_->BelongsToCurrentThread()); + + delegate_->OnConfigWatcherError(); +} + +void ConfigFileWatcherImpl::NotifyUpdate(const std::string& config) { + DCHECK(main_task_runner_->BelongsToCurrentThread()); + + delegate_->OnConfigUpdated(config_); +} + void ConfigFileWatcherImpl::ReloadConfig() { DCHECK(io_task_runner_->BelongsToCurrentThread()); @@ -196,10 +217,11 @@ void ConfigFileWatcherImpl::ReloadConfig() { #endif // defined(OS_WIN) PLOG(ERROR) << "Failed to read '" << config_path_.value() << "'"; + main_task_runner_->PostTask( FROM_HERE, - base::Bind(&ConfigFileWatcher::Delegate::OnConfigWatcherError, - delegate_)); + base::Bind(&ConfigFileWatcherImpl::NotifyError, + weak_factory_.GetWeakPtr())); return; } @@ -210,8 +232,8 @@ void ConfigFileWatcherImpl::ReloadConfig() { config_ = config; main_task_runner_->PostTask( FROM_HERE, - base::Bind(&ConfigFileWatcher::Delegate::OnConfigUpdated, delegate_, - config_)); + base::Bind(&ConfigFileWatcherImpl::NotifyUpdate, + weak_factory_.GetWeakPtr(), config_)); } } diff --git a/remoting/host/config_file_watcher.h b/remoting/host/config_file_watcher.h index f6fe266..163f8b5 100644 --- a/remoting/host/config_file_watcher.h +++ b/remoting/host/config_file_watcher.h @@ -2,13 +2,14 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef REMOTING_HOST_DAEMON_CONFIG_WATCHER_H_ -#define REMOTING_HOST_DAEMON_CONFIG_WATCHER_H_ +#ifndef REMOTING_HOST_CONFIG_FILE_WATCHER_H_ +#define REMOTING_HOST_CONFIG_FILE_WATCHER_H_ #include "base/basictypes.h" #include "base/compiler_specific.h" #include "base/files/file_path.h" #include "base/memory/ref_counted.h" +#include "remoting/host/config_watcher.h" namespace base { class SingleThreadTaskRunner; @@ -21,30 +22,18 @@ extern const base::FilePath::CharType kDefaultHostConfigFile[]; class ConfigFileWatcherImpl; -class ConfigFileWatcher { +class ConfigFileWatcher : public ConfigWatcher { public: - class Delegate { - public: - virtual ~Delegate(); - - // Called once after starting watching the configuration file and every time - // the file changes. - virtual void OnConfigUpdated(const std::string& serialized_config) = 0; - - // Called when the configuration file watcher encountered an error. - virtual void OnConfigWatcherError() = 0; - }; - // Creates a configuration file watcher that lives at the |io_task_runner| // thread but posts config file updates on on |main_task_runner|. ConfigFileWatcher( scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, - Delegate* delegate); + const base::FilePath& config_path); virtual ~ConfigFileWatcher(); - // Starts watching |config_path|. - void Watch(const base::FilePath& config_path); + // Inherited from ConfigWatcher. + virtual void Watch(Delegate* delegate) OVERRIDE; private: scoped_refptr<ConfigFileWatcherImpl> impl_; @@ -54,4 +43,4 @@ class ConfigFileWatcher { } // namespace remoting -#endif // REMOTING_HOST_DAEMON_CONFIG_WATCHER_H_ +#endif // REMOTING_HOST_CONFIG_FILE_WATCHER_H_ diff --git a/remoting/host/config_file_watcher_unittest.cc b/remoting/host/config_file_watcher_unittest.cc index 2eaffd6..29980b4 100644 --- a/remoting/host/config_file_watcher_unittest.cc +++ b/remoting/host/config_file_watcher_unittest.cc @@ -73,6 +73,8 @@ void ConfigFileWatcherTest::StopWatcher() { } void ConfigFileWatcherTest::SetUp() { + EXPECT_TRUE(base::CreateTemporaryFile(&config_file_)); + // Arrange to run |message_loop_| until no components depend on it. scoped_refptr<AutoThreadTaskRunner> task_runner = new AutoThreadTaskRunner( message_loop_.message_loop_proxy(), run_loop_.QuitClosure()); @@ -83,7 +85,7 @@ void ConfigFileWatcherTest::SetUp() { // Create an instance of the config watcher. watcher_.reset( - new ConfigFileWatcher(task_runner, io_task_runner, &delegate_)); + new ConfigFileWatcher(task_runner, io_task_runner, config_file_)); } void ConfigFileWatcherTest::TearDown() { @@ -94,8 +96,6 @@ void ConfigFileWatcherTest::TearDown() { // Verifies that the initial notification is delivered. TEST_F(ConfigFileWatcherTest, Basic) { - EXPECT_TRUE(base::CreateTemporaryFile(&config_file_)); - std::string data("test"); EXPECT_NE(file_util::WriteFile(config_file_, data.c_str(), static_cast<int>(data.size())), -1); @@ -106,7 +106,7 @@ TEST_F(ConfigFileWatcherTest, Basic) { EXPECT_CALL(delegate_, OnConfigWatcherError()) .Times(0); - watcher_->Watch(config_file_); + watcher_->Watch(&delegate_); run_loop_.Run(); } @@ -116,15 +116,13 @@ MATCHER_P(EqualsString, s, "") { // Verifies that an update notification is sent when the file is changed. TEST_F(ConfigFileWatcherTest, Update) { - EXPECT_TRUE(base::CreateTemporaryFile(&config_file_)); - EXPECT_CALL(delegate_, OnConfigUpdated(EqualsString("test"))) .Times(1) .WillOnce(InvokeWithoutArgs(this, &ConfigFileWatcherTest::StopWatcher)); EXPECT_CALL(delegate_, OnConfigWatcherError()) .Times(0); - watcher_->Watch(config_file_); + watcher_->Watch(&delegate_); // Modify the watched file. std::string data("test"); diff --git a/remoting/host/config_watcher.h b/remoting/host/config_watcher.h new file mode 100644 index 0000000..0d608c7 --- /dev/null +++ b/remoting/host/config_watcher.h @@ -0,0 +1,42 @@ +// Copyright 2013 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 REMOTING_HOST_CONFIG_WATCHER_H_ +#define REMOTING_HOST_CONFIG_WATCHER_H_ + +#include <string> + +#include "base/basictypes.h" +#include "base/compiler_specific.h" +#include "base/files/file_path.h" +#include "base/memory/ref_counted.h" + +namespace remoting { + +class ConfigWatcher { + public: + class Delegate { + public: + virtual ~Delegate() {} + + // Called once after starting watching the configuration file and every time + // the file changes. + virtual void OnConfigUpdated(const std::string& serialized_config) = 0; + + // Called when the configuration file watcher encountered an error. + virtual void OnConfigWatcherError() = 0; + }; + + virtual void Watch(Delegate* delegate) = 0; + + ConfigWatcher() {} + + virtual ~ConfigWatcher() {} + private: + DISALLOW_COPY_AND_ASSIGN(ConfigWatcher); +}; + +} // namespace remoting + +#endif // REMOTING_HOST_CONFIG_WATCHER_H_ diff --git a/remoting/host/daemon_process.cc b/remoting/host/daemon_process.cc index f5f0a6d..5ec1e8f 100644 --- a/remoting/host/daemon_process.cc +++ b/remoting/host/daemon_process.cc @@ -4,6 +4,9 @@ #include "remoting/host/daemon_process.h" +#include <algorithm> +#include <string> + #include "base/bind.h" #include "base/bind_helpers.h" #include "base/command_line.h" @@ -13,8 +16,10 @@ #include "base/single_thread_task_runner.h" #include "net/base/net_util.h" #include "remoting/base/auto_thread_task_runner.h" +#include "remoting/base/url_request_context.h" #include "remoting/host/branding.h" #include "remoting/host/chromoting_messages.h" +#include "remoting/host/config_file_watcher.h" #include "remoting/host/desktop_session.h" #include "remoting/host/host_event_logger.h" #include "remoting/host/host_status_observer.h" @@ -254,20 +259,17 @@ void DaemonProcess::CrashNetworkProcess( void DaemonProcess::Initialize() { DCHECK(caller_task_runner()->BelongsToCurrentThread()); + const CommandLine* command_line = CommandLine::ForCurrentProcess(); // Get the name of the host configuration file. base::FilePath default_config_dir = remoting::GetConfigDir(); - base::FilePath config_path = default_config_dir.Append(kDefaultHostConfigFile); - const CommandLine* command_line = CommandLine::ForCurrentProcess(); + base::FilePath config_path = default_config_dir.Append( + kDefaultHostConfigFile); if (command_line->HasSwitch(kHostConfigSwitchName)) { config_path = command_line->GetSwitchValuePath(kHostConfigSwitchName); } - - // Start watching the host configuration file. - config_watcher_.reset(new ConfigFileWatcher(caller_task_runner(), - io_task_runner(), - this)); - config_watcher_->Watch(config_path); - + config_watcher_.reset(new ConfigFileWatcher( + caller_task_runner(), io_task_runner(), config_path)); + config_watcher_->Watch(this); host_event_logger_ = HostEventLogger::Create(weak_factory_.GetWeakPtr(), kApplicationName); diff --git a/remoting/host/daemon_process.h b/remoting/host/daemon_process.h index 9053e42..b74090d 100644 --- a/remoting/host/daemon_process.h +++ b/remoting/host/daemon_process.h @@ -6,6 +6,7 @@ #define REMOTING_HOST_DAEMON_PROCESS_H_ #include <list> +#include <string> #include "base/basictypes.h" #include "base/compiler_specific.h" @@ -17,7 +18,7 @@ #include "ipc/ipc_channel.h" #include "ipc/ipc_channel_proxy.h" #include "ipc/ipc_platform_file.h" -#include "remoting/host/config_file_watcher.h" +#include "remoting/host/config_watcher.h" #include "remoting/host/host_status_monitor.h" #include "remoting/host/worker_process_ipc_delegate.h" @@ -39,7 +40,7 @@ class ScreenResolution; // process running at lower privileges and maintains the list of desktop // sessions. class DaemonProcess - : public ConfigFileWatcher::Delegate, + : public ConfigWatcher::Delegate, public HostStatusMonitor, public WorkerProcessIpcDelegate { public: @@ -56,7 +57,7 @@ class DaemonProcess scoped_refptr<AutoThreadTaskRunner> io_task_runner, const base::Closure& stopped_callback); - // ConfigFileWatcher::Delegate + // ConfigWatcher::Delegate virtual void OnConfigUpdated(const std::string& serialized_config) OVERRIDE; virtual void OnConfigWatcherError() OVERRIDE; @@ -162,7 +163,7 @@ class DaemonProcess // Handles IPC and background I/O tasks. scoped_refptr<AutoThreadTaskRunner> io_task_runner_; - scoped_ptr<ConfigFileWatcher> config_watcher_; + scoped_ptr<ConfigWatcher> config_watcher_; // The configuration file contents. std::string serialized_config_; diff --git a/remoting/host/remoting_me2me_host.cc b/remoting/host/remoting_me2me_host.cc index 9ab0819..ba5fc1b 100644 --- a/remoting/host/remoting_me2me_host.cc +++ b/remoting/host/remoting_me2me_host.cc @@ -42,6 +42,7 @@ #include "remoting/host/chromoting_host_context.h" #include "remoting/host/chromoting_messages.h" #include "remoting/host/config_file_watcher.h" +#include "remoting/host/config_watcher.h" #include "remoting/host/desktop_environment.h" #include "remoting/host/desktop_session_connector.h" #include "remoting/host/dns_blackhole_checker.h" @@ -118,7 +119,7 @@ const char kStdinConfigPath[] = "-"; namespace remoting { class HostProcess - : public ConfigFileWatcher::Delegate, + : public ConfigWatcher::Delegate, public HeartbeatSender::Listener, public HostChangeNotificationListener::Listener, public IPC::Listener, @@ -127,7 +128,7 @@ class HostProcess HostProcess(scoped_ptr<ChromotingHostContext> context, int* exit_code_out); - // ConfigFileWatcher::Delegate interface. + // ConfigWatcher::Delegate interface. virtual void OnConfigUpdated(const std::string& serialized_config) OVERRIDE; virtual void OnConfigWatcherError() OVERRIDE; @@ -254,7 +255,7 @@ class HostProcess // Accessed on the network thread. HostState state_; - scoped_ptr<ConfigFileWatcher> config_watcher_; + scoped_ptr<ConfigWatcher> config_watcher_; std::string host_id_; protocol::SharedSecretHash host_secret_hash_; @@ -472,8 +473,8 @@ void HostProcess::StartOnNetworkThread() { // Start watching the host configuration file. config_watcher_.reset(new ConfigFileWatcher(context_->network_task_runner(), context_->file_task_runner(), - this)); - config_watcher_->Watch(host_config_path_); + host_config_path_)); + config_watcher_->Watch(this); } #endif // !defined(REMOTING_MULTI_PROCESS) diff --git a/remoting/remoting.gyp b/remoting/remoting.gyp index f4beed4..b3facdc 100644 --- a/remoting/remoting.gyp +++ b/remoting/remoting.gyp @@ -313,6 +313,7 @@ 'host/clipboard_x11.cc', 'host/config_file_watcher.cc', 'host/config_file_watcher.h', + 'host/config_watcher.h', 'host/constants_mac.cc', 'host/constants_mac.h', 'host/continue_window.cc', @@ -1592,6 +1593,7 @@ 'host/chromoting_messages.h', 'host/config_file_watcher.cc', 'host/config_file_watcher.h', + 'host/config_watcher.h', 'host/daemon_process.cc', 'host/daemon_process.h', 'host/daemon_process_win.cc', |