OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/config_file_watcher.h" | 5 #include "remoting/host/config_file_watcher.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/files/file_path_watcher.h" | 11 #include "base/files/file_path_watcher.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
16 #include "base/timer.h" | 16 #include "base/timer.h" |
17 | 17 |
18 namespace remoting { | 18 namespace remoting { |
19 | 19 |
20 // The name of the command-line switch used to specify the host configuration | 20 // The name of the command-line switch used to specify the host configuration |
21 // file to use. | 21 // file to use. |
22 const char kHostConfigSwitchName[] = "host-config"; | 22 const char kHostConfigSwitchName[] = "host-config"; |
23 | 23 |
24 const FilePath::CharType kDefaultHostConfigFile[] = | 24 const base::FilePath::CharType kDefaultHostConfigFile[] = |
25 FILE_PATH_LITERAL("host.json"); | 25 FILE_PATH_LITERAL("host.json"); |
26 | 26 |
27 class ConfigFileWatcherImpl | 27 class ConfigFileWatcherImpl |
28 : public base::RefCountedThreadSafe<ConfigFileWatcherImpl> { | 28 : public base::RefCountedThreadSafe<ConfigFileWatcherImpl> { |
29 public: | 29 public: |
30 // Creates a configuration file watcher that lives on the |io_task_runner| | 30 // Creates a configuration file watcher that lives on the |io_task_runner| |
31 // thread but posts config file updates on on |main_task_runner|. | 31 // thread but posts config file updates on on |main_task_runner|. |
32 ConfigFileWatcherImpl( | 32 ConfigFileWatcherImpl( |
33 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, | 33 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
34 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, | 34 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
35 ConfigFileWatcher::Delegate* delegate); | 35 ConfigFileWatcher::Delegate* delegate); |
36 | 36 |
37 // Starts watching |config_path|. | 37 // Starts watching |config_path|. |
38 void Watch(const FilePath& config_path); | 38 void Watch(const base::FilePath& config_path); |
39 | 39 |
40 // Stops watching the configuration file. | 40 // Stops watching the configuration file. |
41 void StopWatching(); | 41 void StopWatching(); |
42 | 42 |
43 private: | 43 private: |
44 friend class base::RefCountedThreadSafe<ConfigFileWatcherImpl>; | 44 friend class base::RefCountedThreadSafe<ConfigFileWatcherImpl>; |
45 virtual ~ConfigFileWatcherImpl(); | 45 virtual ~ConfigFileWatcherImpl(); |
46 | 46 |
47 void FinishStopping(); | 47 void FinishStopping(); |
48 | 48 |
49 // Called every time the host configuration file is updated. | 49 // Called every time the host configuration file is updated. |
50 void OnConfigUpdated(const FilePath& path, bool error); | 50 void OnConfigUpdated(const base::FilePath& path, bool error); |
51 | 51 |
52 // Reads the configuration file and passes it to the delegate. | 52 // Reads the configuration file and passes it to the delegate. |
53 void ReloadConfig(); | 53 void ReloadConfig(); |
54 | 54 |
55 std::string config_; | 55 std::string config_; |
56 FilePath config_path_; | 56 base::FilePath config_path_; |
57 | 57 |
58 scoped_ptr<base::DelayTimer<ConfigFileWatcherImpl> > config_updated_timer_; | 58 scoped_ptr<base::DelayTimer<ConfigFileWatcherImpl> > config_updated_timer_; |
59 | 59 |
60 // Monitors the host configuration file. | 60 // Monitors the host configuration file. |
61 scoped_ptr<base::FilePathWatcher> config_watcher_; | 61 scoped_ptr<base::FilePathWatcher> config_watcher_; |
62 | 62 |
63 base::WeakPtrFactory<ConfigFileWatcher::Delegate> delegate_weak_factory_; | 63 base::WeakPtrFactory<ConfigFileWatcher::Delegate> delegate_weak_factory_; |
64 base::WeakPtr<ConfigFileWatcher::Delegate> delegate_; | 64 base::WeakPtr<ConfigFileWatcher::Delegate> delegate_; |
65 | 65 |
66 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | 66 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
(...skipping 11 matching lines...) Expand all Loading... |
78 Delegate* delegate) | 78 Delegate* delegate) |
79 : impl_(new ConfigFileWatcherImpl(main_task_runner, | 79 : impl_(new ConfigFileWatcherImpl(main_task_runner, |
80 io_task_runner, delegate)) { | 80 io_task_runner, delegate)) { |
81 } | 81 } |
82 | 82 |
83 ConfigFileWatcher::~ConfigFileWatcher() { | 83 ConfigFileWatcher::~ConfigFileWatcher() { |
84 impl_->StopWatching(); | 84 impl_->StopWatching(); |
85 impl_ = NULL; | 85 impl_ = NULL; |
86 } | 86 } |
87 | 87 |
88 void ConfigFileWatcher::Watch(const FilePath& config_path) { | 88 void ConfigFileWatcher::Watch(const base::FilePath& config_path) { |
89 impl_->Watch(config_path); | 89 impl_->Watch(config_path); |
90 } | 90 } |
91 | 91 |
92 ConfigFileWatcherImpl::ConfigFileWatcherImpl( | 92 ConfigFileWatcherImpl::ConfigFileWatcherImpl( |
93 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, | 93 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
94 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, | 94 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
95 ConfigFileWatcher::Delegate* delegate) | 95 ConfigFileWatcher::Delegate* delegate) |
96 : delegate_weak_factory_(delegate), | 96 : delegate_weak_factory_(delegate), |
97 delegate_(delegate_weak_factory_.GetWeakPtr()), | 97 delegate_(delegate_weak_factory_.GetWeakPtr()), |
98 main_task_runner_(main_task_runner), | 98 main_task_runner_(main_task_runner), |
99 io_task_runner_(io_task_runner) { | 99 io_task_runner_(io_task_runner) { |
100 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 100 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
101 } | 101 } |
102 | 102 |
103 void ConfigFileWatcherImpl::Watch(const FilePath& config_path) { | 103 void ConfigFileWatcherImpl::Watch(const base::FilePath& config_path) { |
104 if (!io_task_runner_->BelongsToCurrentThread()) { | 104 if (!io_task_runner_->BelongsToCurrentThread()) { |
105 io_task_runner_->PostTask( | 105 io_task_runner_->PostTask( |
106 FROM_HERE, | 106 FROM_HERE, |
107 base::Bind(&ConfigFileWatcherImpl::Watch, this, config_path)); | 107 base::Bind(&ConfigFileWatcherImpl::Watch, this, config_path)); |
108 return; | 108 return; |
109 } | 109 } |
110 | 110 |
111 DCHECK(config_path_.empty()); | 111 DCHECK(config_path_.empty()); |
112 DCHECK(config_updated_timer_.get() == NULL); | 112 DCHECK(config_updated_timer_.get() == NULL); |
113 DCHECK(config_watcher_.get() == NULL); | 113 DCHECK(config_watcher_.get() == NULL); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 DCHECK(config_watcher_.get() == NULL); | 149 DCHECK(config_watcher_.get() == NULL); |
150 } | 150 } |
151 | 151 |
152 void ConfigFileWatcherImpl::FinishStopping() { | 152 void ConfigFileWatcherImpl::FinishStopping() { |
153 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 153 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
154 | 154 |
155 config_updated_timer_.reset(NULL); | 155 config_updated_timer_.reset(NULL); |
156 config_watcher_.reset(NULL); | 156 config_watcher_.reset(NULL); |
157 } | 157 } |
158 | 158 |
159 void ConfigFileWatcherImpl::OnConfigUpdated(const FilePath& path, bool error) { | 159 void ConfigFileWatcherImpl::OnConfigUpdated(const base::FilePath& path, |
| 160 bool error) { |
160 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 161 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
161 | 162 |
162 // Call ReloadConfig() after a short delay, so that we will not try to read | 163 // Call ReloadConfig() after a short delay, so that we will not try to read |
163 // the updated configuration file before it has been completely written. | 164 // the updated configuration file before it has been completely written. |
164 // If the writer moves the new configuration file into place atomically, | 165 // If the writer moves the new configuration file into place atomically, |
165 // this delay may not be necessary. | 166 // this delay may not be necessary. |
166 if (!error && config_path_ == path) | 167 if (!error && config_path_ == path) |
167 config_updated_timer_->Reset(); | 168 config_updated_timer_->Reset(); |
168 } | 169 } |
169 | 170 |
(...skipping 14 matching lines...) Expand all Loading... |
184 if (config_ != config) { | 185 if (config_ != config) { |
185 config_ = config; | 186 config_ = config; |
186 main_task_runner_->PostTask( | 187 main_task_runner_->PostTask( |
187 FROM_HERE, | 188 FROM_HERE, |
188 base::Bind(&ConfigFileWatcher::Delegate::OnConfigUpdated, delegate_, | 189 base::Bind(&ConfigFileWatcher::Delegate::OnConfigUpdated, delegate_, |
189 config_)); | 190 config_)); |
190 } | 191 } |
191 } | 192 } |
192 | 193 |
193 } // namespace remoting | 194 } // namespace remoting |
OLD | NEW |