Chromium Code Reviews| Index: remoting/host/daemon_config_watcher.cc |
| diff --git a/remoting/host/daemon_config_watcher.cc b/remoting/host/daemon_config_watcher.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f330eb4621839e642929b5492913b4577803cf6f |
| --- /dev/null |
| +++ b/remoting/host/daemon_config_watcher.cc |
| @@ -0,0 +1,112 @@ |
| +// 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 "remoting/host/daemon_config_watcher.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/files/file_path_watcher.h" |
| +#include "base/file_util.h" |
| +#include "base/single_thread_task_runner.h" |
| + |
| +namespace remoting { |
| + |
| +// The name of the command-line switch used to specify the host configuration |
| +// file to use. |
| +const char kHostConfigSwitchName[] = "host-config"; |
| + |
| +const FilePath::CharType kDefaultHostConfigFile[] = |
| + FILE_PATH_LITERAL("host.json"); |
| + |
| +DaemonConfigWatcher::Delegate::~Delegate() { |
| +} |
| + |
| +DaemonConfigWatcher::DaemonConfigWatcher( |
| + scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
| + scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
| + const base::WeakPtr<Delegate>& delegate) |
| + : delegate_(delegate), |
| + main_task_runner_(main_task_runner), |
| + io_task_runner_(io_task_runner) { |
| +} |
| + |
| +DaemonConfigWatcher::~DaemonConfigWatcher() { |
| +} |
| + |
| +void DaemonConfigWatcher::Watch(const FilePath& config_path) { |
| + if (!io_task_runner_->BelongsToCurrentThread()) { |
| + io_task_runner_->PostTask(FROM_HERE, base::Bind(&DaemonConfigWatcher::Watch, |
| + base::Unretained(this), |
| + config_path)); |
| + return; |
| + } |
| + |
| + DCHECK(config_path_.empty()); |
| + DCHECK(config_updated_timer_.get() == NULL); |
| + DCHECK(config_watcher_.get() == NULL); |
| + |
| + // Create the timer that will be used for delayed-reading the configuration |
| + // file. |
| + config_updated_timer_.reset(new base::DelayTimer<DaemonConfigWatcher>( |
| + FROM_HERE, base::TimeDelta::FromSeconds(2), this, |
| + &DaemonConfigWatcher::ReloadConfig)); |
| + |
| + // Start watching the configuration file. |
| + config_watcher_.reset(new base::files::FilePathWatcher()); |
| + config_path_ = config_path; |
| + if (!config_watcher_->Watch(config_path_, |
| + base::Bind(&DaemonConfigWatcher::OnConfigUpdated, |
| + base::Unretained(this)))) { |
| + LOG(ERROR) << "Couldn't watch file '" << config_path_.value() << "'"; |
| + main_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&Delegate::OnConfigWatcherError, delegate_)); |
| + return; |
| + } |
| + |
| + // Force reloading of the configuration file at least once. |
| + ReloadConfig(); |
| +} |
| + |
| +void DaemonConfigWatcher::Stop(scoped_ptr<DaemonConfigWatcher> self) { |
| + if (!io_task_runner_->BelongsToCurrentThread()) { |
| + io_task_runner_->PostTask(FROM_HERE, base::Bind(&DaemonConfigWatcher::Stop, |
| + base::Unretained(this), |
| + base::Passed(&self))); |
| + return; |
| + } |
| + |
| + config_updated_timer_.reset(NULL); |
| + config_watcher_.reset(NULL); |
| +} |
| + |
| +void DaemonConfigWatcher::OnConfigUpdated(const FilePath& path, bool error) { |
| + DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| + |
| + // Call ReloadConfig() after a short delay, so that we will not try to read |
| + // the updated configuration file before it has been completely written. |
| + // If the writer moves the new configuration file into place atomically, |
| + // this delay may not be necessary. |
|
Wez
2012/08/24 22:20:38
If we fixed our config-writing code to write & ren
alexeypa (please no reviews)
2012/08/27 23:16:41
More or less. I'd keep the delay regardless of the
|
| + if (!error && config_path_ == path) |
| + config_updated_timer_->Reset(); |
| +} |
| + |
| +void DaemonConfigWatcher::ReloadConfig() { |
| + DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| + |
| + std::string config; |
| + if (!file_util::ReadFileToString(config_path_, &config)) { |
| + LOG(ERROR) << "Failed to read '" << config_path_.value() << "'"; |
| + main_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&Delegate::OnConfigWatcherError, delegate_)); |
| + return; |
| + } |
| + |
| + main_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&Delegate::OnConfigUpdated, delegate_, config)); |
| +} |
| + |
| +} // namespace remoting |