This source file includes following definitions.
- weak_factory_
- StartWatchingInternal
- StopWatchingInternal
- OnFilePathChanged
- GetLastModification
- Load
- Reload
- IsSafeToReloadPolicy
- Create
#include <set>
#include "remoting/host/policy_hack/policy_watcher.h"
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/file_util.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_path_watcher.h"
#include "base/json/json_file_value_serializer.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/single_thread_task_runner.h"
#include "base/synchronization/waitable_event.h"
#include "base/time/time.h"
#include "base/values.h"
namespace remoting {
namespace policy_hack {
namespace {
const base::FilePath::CharType kPolicyDir[] =
FILE_PATH_LITERAL("/etc/opt/chrome/policies/managed");
const int kSettleIntervalSeconds = 5;
}
class PolicyWatcherLinux : public PolicyWatcher {
public:
PolicyWatcherLinux(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
const base::FilePath& config_dir)
: PolicyWatcher(task_runner),
config_dir_(config_dir),
weak_factory_(this) {
}
virtual ~PolicyWatcherLinux() {}
protected:
virtual void StartWatchingInternal() OVERRIDE {
DCHECK(OnPolicyWatcherThread());
watcher_.reset(new base::FilePathWatcher());
if (!config_dir_.empty() &&
!watcher_->Watch(
config_dir_, false,
base::Bind(&PolicyWatcherLinux::OnFilePathChanged,
weak_factory_.GetWeakPtr()))) {
OnFilePathChanged(config_dir_, true);
}
Reload();
ScheduleFallbackReloadTask();
}
virtual void StopWatchingInternal() OVERRIDE {
DCHECK(OnPolicyWatcherThread());
watcher_.reset();
weak_factory_.InvalidateWeakPtrs();
}
private:
void OnFilePathChanged(const base::FilePath& path, bool error) {
DCHECK(OnPolicyWatcherThread());
if (!error)
Reload();
else
LOG(ERROR) << "PolicyWatcherLinux on " << path.value() << " failed.";
}
base::Time GetLastModification() {
DCHECK(OnPolicyWatcherThread());
base::Time last_modification = base::Time();
base::File::Info file_info;
if (!base::GetFileInfo(config_dir_, &file_info) ||
!file_info.is_directory) {
return last_modification;
}
base::FileEnumerator file_enumerator(config_dir_,
false,
base::FileEnumerator::FILES);
for (base::FilePath config_file = file_enumerator.Next();
!config_file.empty();
config_file = file_enumerator.Next()) {
if (base::GetFileInfo(config_file, &file_info) &&
!file_info.is_directory) {
last_modification = std::max(last_modification,
file_info.last_modified);
}
}
return last_modification;
}
scoped_ptr<base::DictionaryValue> Load() {
DCHECK(OnPolicyWatcherThread());
std::set<base::FilePath> files;
base::FileEnumerator file_enumerator(config_dir_, false,
base::FileEnumerator::FILES);
for (base::FilePath config_file_path = file_enumerator.Next();
!config_file_path.empty(); config_file_path = file_enumerator.Next())
files.insert(config_file_path);
scoped_ptr<base::DictionaryValue> policy(new base::DictionaryValue());
for (std::set<base::FilePath>::iterator config_file_iter = files.begin();
config_file_iter != files.end(); ++config_file_iter) {
JSONFileValueSerializer deserializer(*config_file_iter);
deserializer.set_allow_trailing_comma(true);
int error_code = 0;
std::string error_msg;
scoped_ptr<base::Value> value(
deserializer.Deserialize(&error_code, &error_msg));
if (!value.get()) {
LOG(WARNING) << "Failed to read configuration file "
<< config_file_iter->value() << ": " << error_msg;
return scoped_ptr<base::DictionaryValue>();
}
if (!value->IsType(base::Value::TYPE_DICTIONARY)) {
LOG(WARNING) << "Expected JSON dictionary in configuration file "
<< config_file_iter->value();
return scoped_ptr<base::DictionaryValue>();
}
policy->MergeDictionary(static_cast<base::DictionaryValue*>(value.get()));
}
return policy.Pass();
}
virtual void Reload() OVERRIDE {
DCHECK(OnPolicyWatcherThread());
base::TimeDelta delay;
base::Time now = base::Time::Now();
if (!IsSafeToReloadPolicy(now, &delay)) {
ScheduleReloadTask(delay);
return;
}
if (!IsSafeToReloadPolicy(now, &delay)) {
ScheduleReloadTask(delay);
return;
}
scoped_ptr<base::DictionaryValue> new_policy = Load();
if (new_policy.get()) {
UpdatePolicies(new_policy.get());
ScheduleFallbackReloadTask();
} else {
ScheduleReloadTask(base::TimeDelta::FromSeconds(kSettleIntervalSeconds));
}
}
bool IsSafeToReloadPolicy(const base::Time& now, base::TimeDelta* delay) {
DCHECK(OnPolicyWatcherThread());
DCHECK(delay);
const base::TimeDelta kSettleInterval =
base::TimeDelta::FromSeconds(kSettleIntervalSeconds);
base::Time last_modification = GetLastModification();
if (last_modification.is_null())
return true;
if (last_modification_file_.is_null())
last_modification_file_ = last_modification;
if (last_modification != last_modification_file_) {
last_modification_file_ = last_modification;
last_modification_clock_ = now;
*delay = kSettleInterval;
return false;
}
base::TimeDelta age = now - last_modification_clock_;
if (age < kSettleInterval) {
*delay = kSettleInterval - age;
return false;
}
return true;
}
scoped_ptr<base::FilePathWatcher> watcher_;
base::Time last_modification_file_;
base::Time last_modification_clock_;
const base::FilePath config_dir_;
base::WeakPtrFactory<PolicyWatcherLinux> weak_factory_;
};
PolicyWatcher* PolicyWatcher::Create(
scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
base::FilePath policy_dir(kPolicyDir);
return new PolicyWatcherLinux(task_runner, policy_dir);
}
}
}