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