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

Unified 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: silly compiler. 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 side-by-side diff with in-line comments
Download patch
Index: remoting/host/plugin/policy_hack/nat_policy_linux.cc
diff --git a/remoting/host/plugin/policy_hack/nat_policy_linux.cc b/remoting/host/plugin/policy_hack/nat_policy_linux.cc
new file mode 100644
index 0000000000000000000000000000000000000000..137f7689d8625a58ff2419203f6bcfa545003acd
--- /dev/null
+++ b/remoting/host/plugin/policy_hack/nat_policy_linux.cc
@@ -0,0 +1,278 @@
+// Copyright (c) 2011 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/plugin/policy_hack/nat_policy_linux.h"
+
+#include <set>
+
+#include "base/bind.h"
+#include "base/message_loop_proxy.h"
+#include "base/files/file_path_watcher.h"
+#include "base/synchronization/waitable_event.h"
+#include "remoting/host/plugin/policy_hack/json_value_serializer.h"
+
+namespace remoting {
+namespace policy_hack {
+
+namespace {
+
+#if defined(GOOGLE_CHROME_BUILD)
+const FilePath::CharType kPolicyDir[] =
+ FILE_PATH_LITERAL("/etc/opt/chrome/policies/managed");
+#else
+const FilePath::CharType kPolicyDir[] =
+ FILE_PATH_LITERAL("/etc/chromium/policies/managed");
+#endif
+
+// The time interval for rechecking policy. This is our fallback in case the
+// delegate never reports a change to the ReloadObserver.
+static int kFallbackReloadDelayMinutes = 15;
dmac 2011/08/10 21:39:00 no need to mark static in the anonymous namespace.
awong 2011/08/11 01:23:29 consted instead.
+
+// Amount of time we wait for the files on disk to settle before trying to load
+// them. This alleviates the problem of reading partially written files and
+// makes it possible to batch quasi-simultaneous changes.
+const int kSettleIntervalSeconds = 5;
+
+class FileWatcherDelegate : public base::files::FilePathWatcher::Delegate {
+ public:
+ FileWatcherDelegate(base::WeakPtr<NatPolicyLinux> policy_watcher)
+ : message_loop_proxy_(base::MessageLoopProxy::CreateForCurrentThread()),
+ policy_watcher_(policy_watcher) {
+ }
+
+ virtual void OnFilePathChanged(const FilePath& path) {
+ if (!message_loop_proxy_->BelongsToCurrentThread()) {
+ message_loop_proxy_->PostTask(
+ FROM_HERE,
+ base::Bind(&FileWatcherDelegate::OnFilePathChanged, this, path));
+ return;
+ }
+
+ if (policy_watcher_) {
+ policy_watcher_->OnFilePathChanged(path);
+ }
+ }
+
+ virtual void OnFilePathError(const FilePath& path) {
+ if (!message_loop_proxy_->BelongsToCurrentThread()) {
+ message_loop_proxy_->PostTask(
+ FROM_HERE,
+ base::Bind(&FileWatcherDelegate::OnFilePathError, this, path));
+ return;
+ }
+
+ if (policy_watcher_) {
+ policy_watcher_->OnFilePathError(path);
+ }
+ }
+
+ private:
+ scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
+ base::WeakPtr<NatPolicyLinux> policy_watcher_;
+};
+
+} // namespace
+
+NatPolicyLinux::NatPolicyLinux(const FilePath& config_dir,
+ const NatEnabledCallback& nat_enabled_cb)
+ : message_loop_proxy_(base::MessageLoopProxy::CreateForCurrentThread()),
+ config_dir_(config_dir),
+ nat_enabled_cb_(nat_enabled_cb),
+ current_nat_enabled_state_(false),
+ first_state_published_(false),
+ ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
+}
+
+NatPolicyLinux::~NatPolicyLinux() {
+}
+
+void NatPolicyLinux::StartWatching() {
+ if (!message_loop_proxy_->BelongsToCurrentThread()) {
+ message_loop_proxy_->PostTask(FROM_HERE,
+ base::Bind(&NatPolicyLinux::StartWatching,
+ base::Unretained(this)));
+ return;
+ }
+
+ watcher_.reset(new base::files::FilePathWatcher());
+
+ if (!config_dir_.empty() &&
+ !watcher_->Watch(config_dir_,
+ new FileWatcherDelegate(weak_factory_.GetWeakPtr()))) {
dmac 2011/08/10 21:39:00 refcounting the filewatcherdelegate may simplify s
awong 2011/08/11 01:23:29 Went through a few iterations, and decided that in
+ OnFilePathError(config_dir_);
+ }
+
+ // There might have been changes to the directory in the time between
+ // construction of the loader and initialization of the watcher. Call reload
+ // to detect if that is the case.
+ Reload();
+
+ ScheduleFallbackReloadTask();
+}
+
+void NatPolicyLinux::StopWatching(base::WaitableEvent* done) {
+ if (!message_loop_proxy_->BelongsToCurrentThread()) {
+ message_loop_proxy_->PostTask(FROM_HERE,
+ base::Bind(&NatPolicyLinux::StopWatching,
+ base::Unretained(this), done));
+ return;
+ }
+ watcher_.reset();
+ weak_factory_.InvalidateWeakPtrs();
+ done->Signal();
+}
+
+void NatPolicyLinux::ScheduleFallbackReloadTask() {
+ ScheduleReloadTask(base::TimeDelta::FromMinutes(kFallbackReloadDelayMinutes));
+}
+
+void NatPolicyLinux::ScheduleReloadTask(const base::TimeDelta& delay) {
+ DCHECK(message_loop_proxy_->BelongsToCurrentThread());
+ message_loop_proxy_->PostDelayedTask(
+ FROM_HERE,
+ base::Bind(&NatPolicyLinux::Reload, weak_factory_.GetWeakPtr()),
+ delay.InMilliseconds());
+}
+
+void NatPolicyLinux::OnFilePathError(const FilePath& path) {
+ LOG(ERROR) << "NatPolicyLinux on " << path.value()
+ << " failed.";
+}
+
+void NatPolicyLinux::OnFilePathChanged(const FilePath& path) {
+ Reload();
+}
+
+base::Time NatPolicyLinux::GetLastModification() {
+ base::Time last_modification = base::Time();
+ base::PlatformFileInfo file_info;
+
+ // If the path does not exist or points to a directory, it's safe to load.
+ if (!file_util::GetFileInfo(config_dir_, &file_info) ||
+ !file_info.is_directory) {
+ return last_modification;
+ }
+
+ // Enumerate the files and find the most recent modification timestamp.
+ file_util::FileEnumerator file_enumerator(config_dir_,
+ false,
+ file_util::FileEnumerator::FILES);
+ for (FilePath config_file = file_enumerator.Next();
+ !config_file.empty();
+ config_file = file_enumerator.Next()) {
+ if (file_util::GetFileInfo(config_file, &file_info) &&
+ !file_info.is_directory) {
+ last_modification = std::max(last_modification, file_info.last_modified);
+ }
+ }
+
+ return last_modification;
+}
+
+DictionaryValue* NatPolicyLinux::Load() {
+ // Enumerate the files and sort them lexicographically.
+ std::set<FilePath> files;
+ file_util::FileEnumerator file_enumerator(config_dir_, false,
+ file_util::FileEnumerator::FILES);
+ for (FilePath config_file_path = file_enumerator.Next();
+ !config_file_path.empty(); config_file_path = file_enumerator.Next())
+ files.insert(config_file_path);
+
+ // Start with an empty dictionary and merge the files' contents.
+ DictionaryValue* policy = new DictionaryValue;
+ for (std::set<FilePath>::iterator config_file_iter = files.begin();
+ config_file_iter != files.end(); ++config_file_iter) {
+ JSONFileValueSerializer deserializer(*config_file_iter);
+ int error_code = 0;
+ std::string error_msg;
+ scoped_ptr<Value> value(deserializer.Deserialize(&error_code, &error_msg));
+ if (!value.get()) {
+ LOG(WARNING) << "Failed to read configuration file "
+ << config_file_iter->value() << ": " << error_msg;
+ continue;
+ }
+ if (!value->IsType(Value::TYPE_DICTIONARY)) {
+ LOG(WARNING) << "Expected JSON dictionary in configuration file "
+ << config_file_iter->value();
+ continue;
+ }
+ policy->MergeDictionary(static_cast<DictionaryValue*>(value.get()));
+ }
+
+ return policy;
+}
+
+void NatPolicyLinux::Reload() {
+ // Check the directory time in order to see whether a reload is required.
+ base::TimeDelta delay;
+ base::Time now = base::Time::Now();
+ if (!IsSafeToReloadPolicy(now, &delay)) {
+ ScheduleReloadTask(delay);
+ return;
+ }
+
+ // Load the policy definitions.
+ scoped_ptr<DictionaryValue> new_policy(Load());
+
+ // Check again in case the directory has changed while reading it.
+ if (!IsSafeToReloadPolicy(now, &delay)) {
+ ScheduleReloadTask(delay);
+ return;
+ }
+
+ // Read out just the host firewall traversal policy. Name of policy taken
+ // from the generated policy/policy_constants.h file.
+ bool new_nat_enabled_state = false;
+ base::Value* value;
+ if (new_policy->Get("RemoteAccessHostFirewallTraversal", &value) &&
+ value->IsType(base::Value::TYPE_BOOLEAN)) {
+ CHECK(value->GetAsBoolean(&new_nat_enabled_state));
+ }
+
+ if (!first_state_published_ ||
+ (new_nat_enabled_state != current_nat_enabled_state_)) {
+ first_state_published_ = true;
+ current_nat_enabled_state_ = new_nat_enabled_state;
+ nat_enabled_cb_.Run(current_nat_enabled_state_);
+ }
+
+ ScheduleFallbackReloadTask();
+}
+
+bool NatPolicyLinux::IsSafeToReloadPolicy(
+ const base::Time& now,
+ base::TimeDelta* delay) {
+ DCHECK(delay);
+ const base::TimeDelta kSettleInterval =
+ base::TimeDelta::FromSeconds(kSettleIntervalSeconds);
+
+ base::Time last_modification = GetLastModification();
+ if (last_modification.is_null())
+ return true;
+
+ // If there was a change since the last recorded modification, wait some more.
+ if (last_modification != last_modification_file_) {
+ last_modification_file_ = last_modification;
+ last_modification_clock_ = now;
+ *delay = kSettleInterval;
+ return false;
+ }
+
+ // Check whether the settle interval has elapsed.
+ base::TimeDelta age = now - last_modification_clock_;
+ if (age < kSettleInterval) {
+ *delay = kSettleInterval - age;
+ return false;
+ }
+
+ return true;
+}
+
+NatPolicy* NatPolicy::Create(const NatEnabledCallback& nat_enabled_cb) {
+ FilePath policy_dir(kPolicyDir);
+ return new NatPolicyLinux(policy_dir, nat_enabled_cb);
+}
+
+} // namespace policy_hack
+} // namespace remoting

Powered by Google App Engine
This is Rietveld 408576698