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

Side by Side Diff: remoting/host/plugin/policy_hack/nat_policy_linux.cc

Issue 7599017: Framework to allow Chromoting host to respect NAT traversal policy in linux. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge with Sergey's MessageLoopProxy change Created 9 years, 4 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) 2011 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 // Most of this code is copied from various classes in
6 // src/chrome/browser/policy. In partiuclar, look at
7 //
8 // asynchronous_policy_loader.{h,cc}
9 // file_based_policy_loader.{h,cc}
10 // config_dir_policy_provider.{h,cc}
11 //
12 // This is a reduction of the functionality in those classes.
13
14 #include <set>
15
16 #include "remoting/host/plugin/policy_hack/nat_policy.h"
17
18 #include "base/bind.h"
19 #include "base/compiler_specific.h"
20 #include "base/file_path.h"
21 #include "base/file_util.h"
22 #include "base/files/file_path_watcher.h"
23 #include "base/memory/weak_ptr.h"
24 #include "base/message_loop_proxy.h"
25 #include "base/scoped_ptr.h"
26 #include "base/synchronization/waitable_event.h"
27 #include "base/time.h"
28 #include "base/values.h"
29 #include "content/common/json_value_serializer.h"
30
31 namespace remoting {
32 namespace policy_hack {
33
34 namespace {
35
36 #if defined(GOOGLE_CHROME_BUILD)
37 const FilePath::CharType kPolicyDir[] =
38 FILE_PATH_LITERAL("/etc/opt/chrome/policies/managed");
39 #else
40 const FilePath::CharType kPolicyDir[] =
41 FILE_PATH_LITERAL("/etc/chromium/policies/managed");
42 #endif
43
44 // The time interval for rechecking policy. This is our fallback in case the
45 // delegate never reports a change to the ReloadObserver.
46 const int kFallbackReloadDelayMinutes = 15;
47
48 // Amount of time we wait for the files on disk to settle before trying to load
49 // them. This alleviates the problem of reading partially written files and
50 // makes it possible to batch quasi-simultaneous changes.
51 const int kSettleIntervalSeconds = 5;
52
53 } // namespace
54
55 class NatPolicyLinux : public NatPolicy {
56 public:
57 NatPolicyLinux(base::MessageLoopProxy* message_loop_proxy,
58 const FilePath& config_dir)
59 : message_loop_proxy_(message_loop_proxy),
60 config_dir_(config_dir),
61 current_nat_enabled_state_(false),
62 first_state_published_(false),
63 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
64 // Detach the factory because we ensure that only the |message_loop_proxy_|
65 // thread ever calls methods on this. Also, the API contract of having
66 // to call StopWatching() (which signals completion) after StopWatching()
67 // before this object can be destructed ensures there are no users of
68 // this object before it is destructed.
69 weak_factory_.DetachFromThread();
70 }
71
72 virtual ~NatPolicyLinux() {}
73
74 virtual void StartWatching(const NatEnabledCallback& nat_enabled_cb)
75 OVERRIDE {
76 if (!message_loop_proxy_->BelongsToCurrentThread()) {
77 message_loop_proxy_->PostTask(FROM_HERE,
78 base::Bind(&NatPolicyLinux::StartWatching,
79 base::Unretained(this),
80 nat_enabled_cb));
81 return;
82 }
83
84 nat_enabled_cb_ = nat_enabled_cb;
85 watcher_.reset(new base::files::FilePathWatcher());
86
87 if (!config_dir_.empty() &&
88 !watcher_->Watch(
89 config_dir_,
90 new FilePathWatcherDelegate(weak_factory_.GetWeakPtr()))) {
91 OnFilePathError(config_dir_);
92 }
93
94 // There might have been changes to the directory in the time between
95 // construction of the loader and initialization of the watcher. Call reload
96 // to detect if that is the case.
97 Reload();
98
99 ScheduleFallbackReloadTask();
100 }
101
102 virtual void StopWatching(base::WaitableEvent* done) OVERRIDE {
103 if (!message_loop_proxy_->BelongsToCurrentThread()) {
104 message_loop_proxy_->PostTask(FROM_HERE,
105 base::Bind(&NatPolicyLinux::StopWatching,
106 base::Unretained(this), done));
107 return;
108 }
109
110 // Cancel any inflight requests.
111 weak_factory_.InvalidateWeakPtrs();
112 watcher_.reset();
113 nat_enabled_cb_.Reset();
114
115 done->Signal();
116 }
117
118 // Called by FilePathWatcherDelegate.
119 virtual void OnFilePathError(const FilePath& path) {
120 LOG(ERROR) << "NatPolicyLinux on " << path.value()
121 << " failed.";
122 }
123
124 // Called by FilePathWatcherDelegate.
125 virtual void OnFilePathChanged(const FilePath& path) {
126 DCHECK(message_loop_proxy_->BelongsToCurrentThread());
127
128 Reload();
129 }
130
131 private:
132 // Needed to avoid refcounting NatPolicyLinux.
133 class FilePathWatcherDelegate :
134 public base::files::FilePathWatcher::Delegate {
135 public:
136 FilePathWatcherDelegate(base::WeakPtr<NatPolicyLinux> policy_watcher)
137 : policy_watcher_(policy_watcher) {
138 }
139
140 virtual void OnFilePathError(const FilePath& path) {
141 if (policy_watcher_) {
142 policy_watcher_->OnFilePathError(path);
143 }
144 }
145
146 virtual void OnFilePathChanged(const FilePath& path) {
147 if (policy_watcher_) {
148 policy_watcher_->OnFilePathChanged(path);
149 }
150 }
151
152 private:
153 base::WeakPtr<NatPolicyLinux> policy_watcher_;
154 };
155
156 void ScheduleFallbackReloadTask() {
157 DCHECK(message_loop_proxy_->BelongsToCurrentThread());
158 ScheduleReloadTask(
159 base::TimeDelta::FromMinutes(kFallbackReloadDelayMinutes));
160 }
161
162 void ScheduleReloadTask(const base::TimeDelta& delay) {
163 DCHECK(message_loop_proxy_->BelongsToCurrentThread());
164 message_loop_proxy_->PostDelayedTask(
165 FROM_HERE,
166 base::Bind(&NatPolicyLinux::Reload, weak_factory_.GetWeakPtr()),
167 delay.InMilliseconds());
168 }
169
170 base::Time GetLastModification() {
171 DCHECK(message_loop_proxy_->BelongsToCurrentThread());
172 base::Time last_modification = base::Time();
173 base::PlatformFileInfo file_info;
174
175 // If the path does not exist or points to a directory, it's safe to load.
176 if (!file_util::GetFileInfo(config_dir_, &file_info) ||
177 !file_info.is_directory) {
178 return last_modification;
179 }
180
181 // Enumerate the files and find the most recent modification timestamp.
182 file_util::FileEnumerator file_enumerator(config_dir_,
183 false,
184 file_util::FileEnumerator::FILES);
185 for (FilePath config_file = file_enumerator.Next();
186 !config_file.empty();
187 config_file = file_enumerator.Next()) {
188 if (file_util::GetFileInfo(config_file, &file_info) &&
189 !file_info.is_directory) {
190 last_modification = std::max(last_modification,
191 file_info.last_modified);
192 }
193 }
194
195 return last_modification;
196 }
197
198 // Caller owns the value.
199 DictionaryValue* Load() {
200 DCHECK(message_loop_proxy_->BelongsToCurrentThread());
201 // Enumerate the files and sort them lexicographically.
202 std::set<FilePath> files;
203 file_util::FileEnumerator file_enumerator(config_dir_, false,
204 file_util::FileEnumerator::FILES);
205 for (FilePath config_file_path = file_enumerator.Next();
206 !config_file_path.empty(); config_file_path = file_enumerator.Next())
207 files.insert(config_file_path);
208
209 // Start with an empty dictionary and merge the files' contents.
210 DictionaryValue* policy = new DictionaryValue;
211 for (std::set<FilePath>::iterator config_file_iter = files.begin();
212 config_file_iter != files.end(); ++config_file_iter) {
213 JSONFileValueSerializer deserializer(*config_file_iter);
214 int error_code = 0;
215 std::string error_msg;
216 scoped_ptr<Value> value(
217 deserializer.Deserialize(&error_code, &error_msg));
218 if (!value.get()) {
219 LOG(WARNING) << "Failed to read configuration file "
220 << config_file_iter->value() << ": " << error_msg;
221 continue;
222 }
223 if (!value->IsType(Value::TYPE_DICTIONARY)) {
224 LOG(WARNING) << "Expected JSON dictionary in configuration file "
225 << config_file_iter->value();
226 continue;
227 }
228 policy->MergeDictionary(static_cast<DictionaryValue*>(value.get()));
229 }
230
231 return policy;
232 }
233
234 void Reload() {
235 DCHECK(message_loop_proxy_->BelongsToCurrentThread());
236 // Check the directory time in order to see whether a reload is required.
237 base::TimeDelta delay;
238 base::Time now = base::Time::Now();
239 if (!IsSafeToReloadPolicy(now, &delay)) {
240 ScheduleReloadTask(delay);
241 return;
242 }
243
244 // Load the policy definitions.
245 scoped_ptr<DictionaryValue> new_policy(Load());
246
247 // Check again in case the directory has changed while reading it.
248 if (!IsSafeToReloadPolicy(now, &delay)) {
249 ScheduleReloadTask(delay);
250 return;
251 }
252
253 // Read out just the host firewall traversal policy. Name of policy taken
254 // from the generated policy/policy_constants.h file.
255 bool new_nat_enabled_state = false;
256 if (!new_policy->HasKey("RemoteAccessHostFirewallTraversal")) {
257 // If unspecified, the default value of this policy is true.
258 //
259 // TODO(ajwong): Current default to false until we have policy
260 // implemented and verified in all 3 platforms.
261 new_nat_enabled_state = false;
262 } else {
263 // Otherwise, try to parse the value and only change from false if we get
264 // a successful read.
265 base::Value* value;
266 if (new_policy->Get("RemoteAccessHostFirewallTraversal", &value) &&
267 value->IsType(base::Value::TYPE_BOOLEAN)) {
268 CHECK(value->GetAsBoolean(&new_nat_enabled_state));
269 }
270 }
271
272 if (!first_state_published_ ||
273 (new_nat_enabled_state != current_nat_enabled_state_)) {
274 first_state_published_ = true;
275 current_nat_enabled_state_ = new_nat_enabled_state;
276 nat_enabled_cb_.Run(current_nat_enabled_state_);
277 }
278
279 ScheduleFallbackReloadTask();
280 }
281
282 bool IsSafeToReloadPolicy(const base::Time& now, base::TimeDelta* delay) {
283 DCHECK(message_loop_proxy_->BelongsToCurrentThread());
284 DCHECK(delay);
285 const base::TimeDelta kSettleInterval =
286 base::TimeDelta::FromSeconds(kSettleIntervalSeconds);
287
288 base::Time last_modification = GetLastModification();
289 if (last_modification.is_null())
290 return true;
291
292 // If there was a change since the last recorded modification, wait some
293 // more.
294 if (last_modification != last_modification_file_) {
295 last_modification_file_ = last_modification;
296 last_modification_clock_ = now;
297 *delay = kSettleInterval;
298 return false;
299 }
300
301 // Check whether the settle interval has elapsed.
302 base::TimeDelta age = now - last_modification_clock_;
303 if (age < kSettleInterval) {
304 *delay = kSettleInterval - age;
305 return false;
306 }
307
308 return true;
309 }
310
311 // Managed with a scoped_ptr rather than being declared as an inline member to
312 // decouple the watcher's life cycle from the NatPolicyLinux. This decoupling
313 // makes it possible to destroy the watcher before the loader's destructor is
314 // called (e.g. during Stop), since |watcher_| internally holds a reference to
315 // the loader and keeps it alive.
316 scoped_ptr<base::files::FilePathWatcher> watcher_;
317
318 // Records last known modification timestamp of |config_dir_|.
319 base::Time last_modification_file_;
320
321 // The wall clock time at which the last modification timestamp was
322 // recorded. It's better to not assume the file notification time and the
323 // wall clock times come from the same source, just in case there is some
324 // non-local filesystem involved.
325 base::Time last_modification_clock_;
326
327 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
328 const FilePath config_dir_;
329 NatEnabledCallback nat_enabled_cb_;
330
331 bool current_nat_enabled_state_;
332 bool first_state_published_;
333
334 // Allows us to cancel any inflight FileWatcher events or scheduled reloads.
335 base::WeakPtrFactory<NatPolicyLinux> weak_factory_;
336 };
337
338 NatPolicy* NatPolicy::Create(base::MessageLoopProxy* message_loop_proxy) {
339 FilePath policy_dir(kPolicyDir);
340 return new NatPolicyLinux(message_loop_proxy, policy_dir);
341 }
342
343 } // namespace policy_hack
344 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/plugin/policy_hack/nat_policy.h ('k') | remoting/host/plugin/policy_hack/nat_policy_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698