Chromium Code Reviews| Index: remoting/host/config_file_watcher.cc |
| diff --git a/remoting/host/config_file_watcher.cc b/remoting/host/config_file_watcher.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..80223953fcac7d60e5616c06f30aa4d6ab216cf3 |
| --- /dev/null |
| +++ b/remoting/host/config_file_watcher.cc |
| @@ -0,0 +1,113 @@ |
| +// 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/config_file_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"); |
| + |
| +ConfigFileWatcher::Delegate::~Delegate() { |
| +} |
| + |
| +ConfigFileWatcher::ConfigFileWatcher( |
| + 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) { |
| +} |
| + |
| +ConfigFileWatcher::~ConfigFileWatcher() { |
| +} |
| + |
| +void ConfigFileWatcher::Watch(const FilePath& config_path) { |
| + if (!io_task_runner_->BelongsToCurrentThread()) { |
| + io_task_runner_->PostTask(FROM_HERE, base::Bind(&ConfigFileWatcher::Watch, |
| + base::Unretained(this), |
|
Wez
2012/08/30 20:36:04
nit: Indentation; either align w/ base::Bind brack
alexeypa (please no reviews)
2012/08/30 23:15:13
Done.
|
| + 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<ConfigFileWatcher>( |
| + FROM_HERE, base::TimeDelta::FromSeconds(2), this, |
| + &ConfigFileWatcher::ReloadConfig)); |
|
Wez
2012/08/30 20:36:04
nit: Could you use a raw timer here, and base::Bin
alexeypa (please no reviews)
2012/08/30 23:15:13
|config_path_| is needed because of Linux we get n
|
| + |
| + // Start watching the configuration file. |
| + config_watcher_.reset(new base::files::FilePathWatcher()); |
| + config_path_ = config_path; |
| + if (!config_watcher_->Watch(config_path_, |
| + base::Bind(&ConfigFileWatcher::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 ConfigFileWatcher::StopAndDelete() { |
| + if (!io_task_runner_->BelongsToCurrentThread()) { |
| + io_task_runner_->PostTask(FROM_HERE, |
| + base::Bind(&ConfigFileWatcher::StopAndDelete, |
| + base::Unretained(this))); |
| + return; |
| + } |
| + |
| + config_updated_timer_.reset(NULL); |
| + config_watcher_.reset(NULL); |
| + delete this; |
| +} |
| + |
| +void ConfigFileWatcher::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. |
| + if (!error && config_path_ == path) |
| + config_updated_timer_->Reset(); |
| +} |
| + |
| +void ConfigFileWatcher::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 |