- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / policy / async_policy_loader.cc
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.
4
5 #include "chrome/browser/policy/async_policy_loader.h"
6
7 #include "base/bind.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"
12
13 using base::Time;
14 using base::TimeDelta;
15
16 namespace policy {
17
18 namespace {
19
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;
24
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;
28
29 }  // namespace
30
31 AsyncPolicyLoader::AsyncPolicyLoader(
32     scoped_refptr<base::SequencedTaskRunner> task_runner)
33     : task_runner_(task_runner),
34       weak_factory_(this) {}
35
36 AsyncPolicyLoader::~AsyncPolicyLoader() {}
37
38 base::Time AsyncPolicyLoader::LastModificationTime() {
39   return base::Time();
40 }
41
42 void AsyncPolicyLoader::Reload(bool force) {
43   DCHECK(task_runner_->RunsTasksOnCurrentThread());
44
45   TimeDelta delay;
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);
50     return;
51   }
52
53   scoped_ptr<PolicyBundle> bundle(Load());
54
55   // Check if there was a modification while reading.
56   if (!force && !IsSafeToReload(now, &delay)) {
57     ScheduleNextReload(delay);
58     return;
59   }
60
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());
65   }
66
67   update_callback_.Run(bundle.Pass());
68   ScheduleNextReload(TimeDelta::FromSeconds(kReloadIntervalSeconds));
69 }
70
71 void AsyncPolicyLoader::RegisterPolicyDomain(
72     scoped_refptr<const PolicyDomainDescriptor> descriptor) {
73   if (descriptor->domain() != POLICY_DOMAIN_CHROME) {
74     descriptor_map_[descriptor->domain()] = descriptor;
75     Reload(true);
76   }
77 }
78
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();
84   return Load();
85 }
86
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;
92
93   InitOnBackgroundThread();
94
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_)
98     Reload(false);
99
100   // Start periodic refreshes.
101   ScheduleNextReload(TimeDelta::FromSeconds(kReloadIntervalSeconds));
102 }
103
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(),
110                                            false /* force */),
111                                 delay);
112 }
113
114 bool AsyncPolicyLoader::IsSafeToReload(const Time& now, TimeDelta* delay) {
115   Time last_modification = LastModificationTime();
116   if (last_modification.is_null())
117     return true;
118
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;
126     return false;
127   }
128
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;
133     return false;
134   }
135
136   return true;
137 }
138
139 }  // namespace policy