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
3 // found in the LICENSE file.
5 #include "chrome/browser/policy/async_policy_loader.h"
8 #include "base/location.h"
9 #include "base/sequenced_task_runner.h"
10 #include "chrome/browser/policy/policy_bundle.h"
11 #include "chrome/browser/policy/policy_domain_descriptor.h"
14 using base::TimeDelta;
20 // Amount of time to wait for the files on disk to settle before trying to load
21 // them. This alleviates the problem of reading partially written files and
22 // makes it possible to batch quasi-simultaneous changes.
23 const int kSettleIntervalSeconds = 5;
25 // The time interval for rechecking policy. This is the fallback in case the
26 // implementation never detects changes.
27 const int kReloadIntervalSeconds = 15 * 60;
31 AsyncPolicyLoader::AsyncPolicyLoader(
32 scoped_refptr<base::SequencedTaskRunner> task_runner)
33 : task_runner_(task_runner),
34 weak_factory_(this) {}
36 AsyncPolicyLoader::~AsyncPolicyLoader() {}
38 base::Time AsyncPolicyLoader::LastModificationTime() {
42 void AsyncPolicyLoader::Reload(bool force) {
43 DCHECK(task_runner_->RunsTasksOnCurrentThread());
46 Time now = Time::Now();
47 // Check if there was a recent modification to the underlying files.
48 if (!force && !IsSafeToReload(now, &delay)) {
49 ScheduleNextReload(delay);
53 scoped_ptr<PolicyBundle> bundle(Load());
55 // Check if there was a modification while reading.
56 if (!force && !IsSafeToReload(now, &delay)) {
57 ScheduleNextReload(delay);
61 // Filter out mismatching policies.
62 for (DescriptorMap::iterator it = descriptor_map_.begin();
63 it != descriptor_map_.end(); ++it) {
64 it->second->FilterBundle(bundle.get());
67 update_callback_.Run(bundle.Pass());
68 ScheduleNextReload(TimeDelta::FromSeconds(kReloadIntervalSeconds));
71 void AsyncPolicyLoader::RegisterPolicyDomain(
72 scoped_refptr<const PolicyDomainDescriptor> descriptor) {
73 if (descriptor->domain() != POLICY_DOMAIN_CHROME) {
74 descriptor_map_[descriptor->domain()] = descriptor;
79 scoped_ptr<PolicyBundle> AsyncPolicyLoader::InitialLoad() {
80 // This is the first load, early during startup. Use this to record the
81 // initial |last_modification_time_|, so that potential changes made before
82 // installing the watches can be detected.
83 last_modification_time_ = LastModificationTime();
87 void AsyncPolicyLoader::Init(const UpdateCallback& update_callback) {
88 DCHECK(task_runner_->RunsTasksOnCurrentThread());
89 DCHECK(update_callback_.is_null());
90 DCHECK(!update_callback.is_null());
91 update_callback_ = update_callback;
93 InitOnBackgroundThread();
95 // There might have been changes to the underlying files since the initial
96 // load and before the watchers have been created.
97 if (LastModificationTime() != last_modification_time_)
100 // Start periodic refreshes.
101 ScheduleNextReload(TimeDelta::FromSeconds(kReloadIntervalSeconds));
104 void AsyncPolicyLoader::ScheduleNextReload(TimeDelta delay) {
105 DCHECK(task_runner_->RunsTasksOnCurrentThread());
106 weak_factory_.InvalidateWeakPtrs();
107 task_runner_->PostDelayedTask(FROM_HERE,
108 base::Bind(&AsyncPolicyLoader::Reload,
109 weak_factory_.GetWeakPtr(),
114 bool AsyncPolicyLoader::IsSafeToReload(const Time& now, TimeDelta* delay) {
115 Time last_modification = LastModificationTime();
116 if (last_modification.is_null())
119 // If there was a change since the last recorded modification, wait some more.
120 const TimeDelta kSettleInterval(
121 TimeDelta::FromSeconds(kSettleIntervalSeconds));
122 if (last_modification != last_modification_time_) {
123 last_modification_time_ = last_modification;
124 last_modification_clock_ = now;
125 *delay = kSettleInterval;
129 // Check whether the settle interval has elapsed.
130 const base::TimeDelta age = now - last_modification_clock_;
131 if (age < kSettleInterval) {
132 *delay = kSettleInterval - age;
139 } // namespace policy