Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(466)

Side by Side Diff: remoting/host/daemon_config_watcher.cc

Issue 10855249: [Chromoting] The daemon process now starts the networking process and passes the host configuration… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: CR feedback and rebased on top of https://chromiumcodereview.appspot.com/10829467/ Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "remoting/host/daemon_config_watcher.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/files/file_path_watcher.h"
10 #include "base/file_util.h"
11 #include "base/single_thread_task_runner.h"
12
13 namespace remoting {
14
15 // The name of the command-line switch used to specify the host configuration
16 // file to use.
17 const char kHostConfigSwitchName[] = "host-config";
18
19 const FilePath::CharType kDefaultHostConfigFile[] =
20 FILE_PATH_LITERAL("host.json");
21
22 DaemonConfigWatcher::Delegate::~Delegate() {
23 }
24
25 DaemonConfigWatcher::DaemonConfigWatcher(
26 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
27 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
28 const base::WeakPtr<Delegate>& delegate)
29 : delegate_(delegate),
30 main_task_runner_(main_task_runner),
31 io_task_runner_(io_task_runner) {
32 }
33
34 DaemonConfigWatcher::~DaemonConfigWatcher() {
35 }
36
37 void DaemonConfigWatcher::Watch(const FilePath& config_path) {
38 if (!io_task_runner_->BelongsToCurrentThread()) {
39 io_task_runner_->PostTask(FROM_HERE, base::Bind(&DaemonConfigWatcher::Watch,
40 base::Unretained(this),
41 config_path));
42 return;
43 }
44
45 DCHECK(config_path_.empty());
46 DCHECK(config_updated_timer_.get() == NULL);
47 DCHECK(config_watcher_.get() == NULL);
48
49 // Create the timer that will be used for delayed-reading the configuration
50 // file.
51 config_updated_timer_.reset(new base::DelayTimer<DaemonConfigWatcher>(
52 FROM_HERE, base::TimeDelta::FromSeconds(2), this,
53 &DaemonConfigWatcher::ReloadConfig));
54
55 // Start watching the configuration file.
56 config_watcher_.reset(new base::files::FilePathWatcher());
57 config_path_ = config_path;
58 if (!config_watcher_->Watch(config_path_,
59 base::Bind(&DaemonConfigWatcher::OnConfigUpdated,
60 base::Unretained(this)))) {
61 LOG(ERROR) << "Couldn't watch file '" << config_path_.value() << "'";
62 main_task_runner_->PostTask(
63 FROM_HERE,
64 base::Bind(&Delegate::OnConfigWatcherError, delegate_));
65 return;
66 }
67
68 // Force reloading of the configuration file at least once.
69 ReloadConfig();
70 }
71
72 void DaemonConfigWatcher::Stop(scoped_ptr<DaemonConfigWatcher> self) {
73 if (!io_task_runner_->BelongsToCurrentThread()) {
74 io_task_runner_->PostTask(FROM_HERE, base::Bind(&DaemonConfigWatcher::Stop,
75 base::Unretained(this),
76 base::Passed(&self)));
77 return;
78 }
79
80 config_updated_timer_.reset(NULL);
81 config_watcher_.reset(NULL);
82 }
83
84 void DaemonConfigWatcher::OnConfigUpdated(const FilePath& path, bool error) {
85 DCHECK(io_task_runner_->BelongsToCurrentThread());
86
87 // Call ReloadConfig() after a short delay, so that we will not try to read
88 // the updated configuration file before it has been completely written.
89 // If the writer moves the new configuration file into place atomically,
90 // 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
91 if (!error && config_path_ == path)
92 config_updated_timer_->Reset();
93 }
94
95 void DaemonConfigWatcher::ReloadConfig() {
96 DCHECK(io_task_runner_->BelongsToCurrentThread());
97
98 std::string config;
99 if (!file_util::ReadFileToString(config_path_, &config)) {
100 LOG(ERROR) << "Failed to read '" << config_path_.value() << "'";
101 main_task_runner_->PostTask(
102 FROM_HERE,
103 base::Bind(&Delegate::OnConfigWatcherError, delegate_));
104 return;
105 }
106
107 main_task_runner_->PostTask(
108 FROM_HERE,
109 base::Bind(&Delegate::OnConfigUpdated, delegate_, config));
110 }
111
112 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698