OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2010 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/asynchronous_policy_loader.h" | |
6 | |
7 #include "base/message_loop.h" | |
8 #include "base/task.h" | |
9 #include "chrome/browser/browser_thread.h" | |
10 | |
11 namespace policy { | |
12 | |
13 AsynchronousPolicyLoader::AsynchronousPolicyLoader( | |
14 base::WeakPtr<ConfigurationPolicyProvider> provider, | |
15 AsynchronousPolicyProvider::Delegate* delegate, | |
16 int reload_interval_minutes) | |
17 : provider_(provider), | |
18 delegate_(delegate), | |
19 origin_loop_(MessageLoop::current()), | |
20 reload_task_(NULL), | |
21 reload_interval_minutes_(reload_interval_minutes) { | |
22 } | |
23 | |
24 void AsynchronousPolicyLoader::Init() { | |
25 // Force an initial load, so GetPolicy() works. | |
26 policy_.reset(delegate_->Load()); | |
27 DCHECK(policy_.get()); | |
28 ScheduleFallbackReloadTask(); | |
29 } | |
30 | |
31 DictionaryValue* AsynchronousPolicyLoader::GetPolicy() { | |
32 AutoLock lock(lock_); | |
33 return static_cast<DictionaryValue*>(policy_->DeepCopy()); | |
34 } | |
35 | |
36 void AsynchronousPolicyLoader::Reload() { | |
37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
38 // Check the directory time in order to see whether a reload is required. | |
Mattias Nissler (ping if slow)
2010/12/02 18:16:00
Outdated comment?
danno
2010/12/03 17:05:38
Done.
| |
39 base::TimeDelta delay; | |
40 base::Time now = base::Time::Now(); | |
41 if (!delegate_->IsSafeToReloadPolicy(now, &delay)) { | |
Mattias Nissler (ping if slow)
2010/12/02 18:16:00
Does IsSafeToReloadPolicy make sense for group pol
danno
2010/12/03 17:05:38
Done.
| |
42 ScheduleReloadTask(delay); | |
Mattias Nissler (ping if slow)
2010/12/02 18:16:00
Does the periodic reload task make sense for group
danno
2010/12/03 17:05:38
Done.
| |
43 return; | |
44 } | |
45 | |
46 // Load the policy definitions. | |
47 scoped_ptr<DictionaryValue> new_policy(delegate_->Load()); | |
48 | |
49 // Check again in case the directory has changed while reading it. | |
50 if (!delegate_->IsSafeToReloadPolicy(now, &delay)) { | |
51 ScheduleReloadTask(delay); | |
52 return; | |
53 } | |
54 | |
55 // Replace policy definition. | |
56 bool changed = false; | |
57 { | |
58 AutoLock lock(lock_); | |
59 changed = !policy_->Equals(new_policy.get()); | |
60 policy_.reset(new_policy.release()); | |
61 } | |
62 | |
63 // There's a change, report it! | |
64 if (changed) { | |
65 origin_loop_->PostTask(FROM_HERE, | |
66 NewRunnableMethod(this, | |
67 &AsynchronousPolicyLoader::NotifyPolicyChanged)); | |
68 } | |
69 | |
70 ScheduleFallbackReloadTask(); | |
71 } | |
72 | |
73 void AsynchronousPolicyLoader::Stop() { | |
74 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | |
75 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
76 NewRunnableMethod(this, &AsynchronousPolicyLoader::Stop)); | |
77 return; | |
78 } | |
79 | |
80 if (reload_task_) { | |
81 reload_task_->Cancel(); | |
82 reload_task_ = NULL; | |
83 } | |
84 } | |
85 | |
86 void AsynchronousPolicyLoader::ScheduleReloadTask( | |
87 const base::TimeDelta& delay) { | |
88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
89 | |
90 if (reload_task_) | |
91 reload_task_->Cancel(); | |
92 | |
93 reload_task_ = | |
94 NewRunnableMethod(this, &AsynchronousPolicyLoader::ReloadFromTask); | |
95 BrowserThread::PostDelayedTask(BrowserThread::FILE, FROM_HERE, reload_task_, | |
96 delay.InMilliseconds()); | |
97 } | |
98 | |
99 void AsynchronousPolicyLoader::ScheduleFallbackReloadTask() { | |
100 // As a safeguard in case that the load delegate failed to timely notice a | |
101 // change in policy, schedule a reload task that'll make us recheck after a | |
102 // reasonable interval. | |
103 ScheduleReloadTask(base::TimeDelta::FromMinutes(reload_interval_minutes_)); | |
104 } | |
105 | |
106 void AsynchronousPolicyLoader::ReloadFromTask() { | |
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
108 | |
109 // Drop the reference to the reload task, since the task might be the only | |
110 // referer that keeps us alive, so we should not Cancel() it. | |
111 reload_task_ = NULL; | |
112 | |
113 Reload(); | |
114 } | |
115 | |
116 void AsynchronousPolicyLoader::NotifyPolicyChanged() { | |
117 DCHECK_EQ(origin_loop_, MessageLoop::current()); | |
118 if (provider_) | |
119 provider_->NotifyStoreOfPolicyChange(); | |
120 } | |
121 | |
122 } // namespace policy | |
OLD | NEW |