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

Side by Side Diff: remoting/host/config_file_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 + rebased on top of https://chromiumcodereview.appspot.com/10829467/ (Patch Set 5) 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/config_file_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 ConfigFileWatcher::Delegate::~Delegate() {
23 }
24
25 ConfigFileWatcher::ConfigFileWatcher(
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 ConfigFileWatcher::~ConfigFileWatcher() {
35 }
36
37 void ConfigFileWatcher::Watch(const FilePath& config_path) {
38 if (!io_task_runner_->BelongsToCurrentThread()) {
39 io_task_runner_->PostTask(FROM_HERE, base::Bind(&ConfigFileWatcher::Watch,
40 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.
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<ConfigFileWatcher>(
52 FROM_HERE, base::TimeDelta::FromSeconds(2), this,
53 &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
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(&ConfigFileWatcher::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 ConfigFileWatcher::StopAndDelete() {
73 if (!io_task_runner_->BelongsToCurrentThread()) {
74 io_task_runner_->PostTask(FROM_HERE,
75 base::Bind(&ConfigFileWatcher::StopAndDelete,
76 base::Unretained(this)));
77 return;
78 }
79
80 config_updated_timer_.reset(NULL);
81 config_watcher_.reset(NULL);
82 delete this;
83 }
84
85 void ConfigFileWatcher::OnConfigUpdated(const FilePath& path, bool error) {
86 DCHECK(io_task_runner_->BelongsToCurrentThread());
87
88 // Call ReloadConfig() after a short delay, so that we will not try to read
89 // the updated configuration file before it has been completely written.
90 // If the writer moves the new configuration file into place atomically,
91 // this delay may not be necessary.
92 if (!error && config_path_ == path)
93 config_updated_timer_->Reset();
94 }
95
96 void ConfigFileWatcher::ReloadConfig() {
97 DCHECK(io_task_runner_->BelongsToCurrentThread());
98
99 std::string config;
100 if (!file_util::ReadFileToString(config_path_, &config)) {
101 LOG(ERROR) << "Failed to read '" << config_path_.value() << "'";
102 main_task_runner_->PostTask(
103 FROM_HERE,
104 base::Bind(&Delegate::OnConfigWatcherError, delegate_));
105 return;
106 }
107
108 main_task_runner_->PostTask(
109 FROM_HERE,
110 base::Bind(&Delegate::OnConfigUpdated, delegate_, config));
111 }
112
113 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698