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

Side by Side Diff: chrome/browser/policy/async_policy_loader.cc

Issue 56623005: Policy providers all get a SchemaRegistry to work with. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@chrome-policy-schema-9-purge-with-callback
Patch Set: rebase Created 7 years, 1 month 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/policy/async_policy_loader.h" 5 #include "chrome/browser/policy/async_policy_loader.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/sequenced_task_runner.h" 9 #include "base/sequenced_task_runner.h"
10 #include "chrome/browser/policy/policy_bundle.h" 10 #include "chrome/browser/policy/policy_bundle.h"
11 #include "chrome/browser/policy/policy_domain_descriptor.h"
12 11
13 using base::Time; 12 using base::Time;
14 using base::TimeDelta; 13 using base::TimeDelta;
15 14
16 namespace policy { 15 namespace policy {
17 16
18 namespace { 17 namespace {
19 18
20 // Amount of time to wait for the files on disk to settle before trying to load 19 // 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 20 // them. This alleviates the problem of reading partially written files and
22 // makes it possible to batch quasi-simultaneous changes. 21 // makes it possible to batch quasi-simultaneous changes.
23 const int kSettleIntervalSeconds = 5; 22 const int kSettleIntervalSeconds = 5;
24 23
25 // The time interval for rechecking policy. This is the fallback in case the 24 // The time interval for rechecking policy. This is the fallback in case the
26 // implementation never detects changes. 25 // implementation never detects changes.
27 const int kReloadIntervalSeconds = 15 * 60; 26 const int kReloadIntervalSeconds = 15 * 60;
28 27
29 } // namespace 28 } // namespace
30 29
31 AsyncPolicyLoader::AsyncPolicyLoader( 30 AsyncPolicyLoader::AsyncPolicyLoader(
32 scoped_refptr<base::SequencedTaskRunner> task_runner) 31 scoped_refptr<base::SequencedTaskRunner> task_runner)
33 : task_runner_(task_runner), 32 : task_runner_(task_runner),
34 weak_factory_(this) {} 33 weak_factory_(this) {}
35 34
36 AsyncPolicyLoader::~AsyncPolicyLoader() {} 35 AsyncPolicyLoader::~AsyncPolicyLoader() {}
37 36
38 base::Time AsyncPolicyLoader::LastModificationTime() { 37 Time AsyncPolicyLoader::LastModificationTime() {
39 return base::Time(); 38 return Time();
40 } 39 }
41 40
42 void AsyncPolicyLoader::Reload(bool force) { 41 void AsyncPolicyLoader::Reload(bool force) {
43 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 42 DCHECK(task_runner_->RunsTasksOnCurrentThread());
44 43
45 TimeDelta delay; 44 TimeDelta delay;
46 Time now = Time::Now(); 45 Time now = Time::Now();
47 // Check if there was a recent modification to the underlying files. 46 // Check if there was a recent modification to the underlying files.
48 if (!force && !IsSafeToReload(now, &delay)) { 47 if (!force && !IsSafeToReload(now, &delay)) {
49 ScheduleNextReload(delay); 48 ScheduleNextReload(delay);
50 return; 49 return;
51 } 50 }
52 51
53 scoped_ptr<PolicyBundle> bundle(Load()); 52 scoped_ptr<PolicyBundle> bundle(Load());
54 53
55 // Check if there was a modification while reading. 54 // Check if there was a modification while reading.
56 if (!force && !IsSafeToReload(now, &delay)) { 55 if (!force && !IsSafeToReload(now, &delay)) {
57 ScheduleNextReload(delay); 56 ScheduleNextReload(delay);
58 return; 57 return;
59 } 58 }
60 59
61 // Filter out mismatching policies. 60 // Filter out mismatching policies.
62 for (DescriptorMap::iterator it = descriptor_map_.begin(); 61 schema_map_->FilterBundle(bundle.get());
63 it != descriptor_map_.end(); ++it) {
64 it->second->FilterBundle(bundle.get());
65 }
66 62
67 update_callback_.Run(bundle.Pass()); 63 update_callback_.Run(bundle.Pass());
68 ScheduleNextReload(TimeDelta::FromSeconds(kReloadIntervalSeconds)); 64 ScheduleNextReload(TimeDelta::FromSeconds(kReloadIntervalSeconds));
69 } 65 }
70 66
71 void AsyncPolicyLoader::RegisterPolicyDomain( 67 scoped_ptr<PolicyBundle> AsyncPolicyLoader::InitialLoad(
72 scoped_refptr<const PolicyDomainDescriptor> descriptor) { 68 const scoped_refptr<SchemaMap>& schema_map) {
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 69 // This is the first load, early during startup. Use this to record the
81 // initial |last_modification_time_|, so that potential changes made before 70 // initial |last_modification_time_|, so that potential changes made before
82 // installing the watches can be detected. 71 // installing the watches can be detected.
83 last_modification_time_ = LastModificationTime(); 72 last_modification_time_ = LastModificationTime();
84 return Load(); 73 schema_map_ = schema_map;
74 scoped_ptr<PolicyBundle> bundle(Load());
75 // Filter out mismatching policies.
76 schema_map_->FilterBundle(bundle.get());
77 return bundle.Pass();
85 } 78 }
86 79
87 void AsyncPolicyLoader::Init(const UpdateCallback& update_callback) { 80 void AsyncPolicyLoader::Init(const UpdateCallback& update_callback) {
88 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 81 DCHECK(task_runner_->RunsTasksOnCurrentThread());
89 DCHECK(update_callback_.is_null()); 82 DCHECK(update_callback_.is_null());
90 DCHECK(!update_callback.is_null()); 83 DCHECK(!update_callback.is_null());
91 update_callback_ = update_callback; 84 update_callback_ = update_callback;
92 85
93 InitOnBackgroundThread(); 86 InitOnBackgroundThread();
94 87
95 // There might have been changes to the underlying files since the initial 88 // There might have been changes to the underlying files since the initial
96 // load and before the watchers have been created. 89 // load and before the watchers have been created.
97 if (LastModificationTime() != last_modification_time_) 90 if (LastModificationTime() != last_modification_time_)
98 Reload(false); 91 Reload(false);
99 92
100 // Start periodic refreshes. 93 // Start periodic refreshes.
101 ScheduleNextReload(TimeDelta::FromSeconds(kReloadIntervalSeconds)); 94 ScheduleNextReload(TimeDelta::FromSeconds(kReloadIntervalSeconds));
102 } 95 }
103 96
97 void AsyncPolicyLoader::RefreshPolicies(scoped_refptr<SchemaMap> schema_map) {
98 DCHECK(task_runner_->RunsTasksOnCurrentThread());
99 schema_map_ = schema_map;
100 Reload(true);
101 }
102
104 void AsyncPolicyLoader::ScheduleNextReload(TimeDelta delay) { 103 void AsyncPolicyLoader::ScheduleNextReload(TimeDelta delay) {
105 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 104 DCHECK(task_runner_->RunsTasksOnCurrentThread());
106 weak_factory_.InvalidateWeakPtrs(); 105 weak_factory_.InvalidateWeakPtrs();
107 task_runner_->PostDelayedTask(FROM_HERE, 106 task_runner_->PostDelayedTask(FROM_HERE,
108 base::Bind(&AsyncPolicyLoader::Reload, 107 base::Bind(&AsyncPolicyLoader::Reload,
109 weak_factory_.GetWeakPtr(), 108 weak_factory_.GetWeakPtr(),
110 false /* force */), 109 false /* force */),
111 delay); 110 delay);
112 } 111 }
113 112
114 bool AsyncPolicyLoader::IsSafeToReload(const Time& now, TimeDelta* delay) { 113 bool AsyncPolicyLoader::IsSafeToReload(const Time& now, TimeDelta* delay) {
115 Time last_modification = LastModificationTime(); 114 Time last_modification = LastModificationTime();
116 if (last_modification.is_null()) 115 if (last_modification.is_null())
117 return true; 116 return true;
118 117
119 // If there was a change since the last recorded modification, wait some more. 118 // If there was a change since the last recorded modification, wait some more.
120 const TimeDelta kSettleInterval( 119 const TimeDelta kSettleInterval(
121 TimeDelta::FromSeconds(kSettleIntervalSeconds)); 120 TimeDelta::FromSeconds(kSettleIntervalSeconds));
122 if (last_modification != last_modification_time_) { 121 if (last_modification != last_modification_time_) {
123 last_modification_time_ = last_modification; 122 last_modification_time_ = last_modification;
124 last_modification_clock_ = now; 123 last_modification_clock_ = now;
125 *delay = kSettleInterval; 124 *delay = kSettleInterval;
126 return false; 125 return false;
127 } 126 }
128 127
129 // Check whether the settle interval has elapsed. 128 // Check whether the settle interval has elapsed.
130 const base::TimeDelta age = now - last_modification_clock_; 129 const TimeDelta age = now - last_modification_clock_;
131 if (age < kSettleInterval) { 130 if (age < kSettleInterval) {
132 *delay = kSettleInterval - age; 131 *delay = kSettleInterval - age;
133 return false; 132 return false;
134 } 133 }
135 134
136 return true; 135 return true;
137 } 136 }
138 137
139 } // namespace policy 138 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/async_policy_loader.h ('k') | chrome/browser/policy/async_policy_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698