OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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/asynchronous_policy_loader.h" | 5 #include "chrome/browser/policy/asynchronous_policy_loader.h" |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/task.h" | 8 #include "base/task.h" |
9 #include "chrome/browser/browser_thread.h" | |
9 | 10 |
10 namespace policy { | 11 namespace policy { |
11 | 12 |
12 AsynchronousPolicyLoader::AsynchronousPolicyLoader( | 13 AsynchronousPolicyLoader::AsynchronousPolicyLoader( |
13 AsynchronousPolicyProvider::Delegate* delegate) | 14 AsynchronousPolicyProvider::Delegate* delegate, |
15 int reload_interval_minutes) | |
14 : delegate_(delegate), | 16 : delegate_(delegate), |
15 provider_(NULL), | 17 provider_(NULL), |
16 origin_loop_(MessageLoop::current()) {} | 18 reload_task_(NULL), |
19 reload_interval_(base::TimeDelta::FromMinutes(reload_interval_minutes)), | |
20 origin_loop_(MessageLoop::current()), | |
21 stopped_(false) {} | |
17 | 22 |
18 void AsynchronousPolicyLoader::Init() { | 23 void AsynchronousPolicyLoader::Init() { |
19 policy_.reset(delegate_->Load()); | 24 policy_.reset(delegate_->Load()); |
25 // Initialization can happen early when the file thread is not yet available, | |
26 // but the subclass of the loader must do some of their initialization on the | |
27 // file thread. Posting to a to the file directly before the file thread is | |
Jakob Kummerow
2010/12/21 16:56:10
nit: "Posting a task to the file thread before the
danno
2010/12/22 11:02:17
Done.
| |
28 // initialized will cause the task to be forgotten. Instead, post a task to | |
29 // the ui thread to delay the remainder of initialization until threading is | |
30 // fully initialized. | |
31 BrowserThread::PostTask( | |
32 BrowserThread::FILE, FROM_HERE, | |
Mattias Nissler (ping if slow)
2010/12/21 16:42:24
UI!
danno
2010/12/22 11:02:17
Done, hadn't merged with my fix yet.
On 2010/12/21
| |
33 NewRunnableMethod( | |
34 this, | |
35 &AsynchronousPolicyLoader::InitAfterFileThreadAvailable)); | |
20 } | 36 } |
21 | 37 |
22 void AsynchronousPolicyLoader::Stop() { | 38 void AsynchronousPolicyLoader::Stop() { |
23 delegate_.reset(); | 39 if (!stopped_) { |
40 stopped_ = true; | |
41 delegate_.reset(); | |
42 BrowserThread::PostTask( | |
43 BrowserThread::FILE, FROM_HERE, | |
44 NewRunnableMethod(this, &AsynchronousPolicyLoader::StopOnFileThread)); | |
45 } | |
24 } | 46 } |
25 | 47 |
26 void AsynchronousPolicyLoader::SetProvider( | 48 void AsynchronousPolicyLoader::SetProvider( |
27 AsynchronousPolicyProvider* provider) { | 49 AsynchronousPolicyProvider* provider) { |
28 provider_ = provider; | 50 provider_ = provider; |
29 } | 51 } |
30 | 52 |
31 AsynchronousPolicyLoader::~AsynchronousPolicyLoader() { | 53 AsynchronousPolicyLoader::~AsynchronousPolicyLoader() { |
32 } | 54 } |
33 | 55 |
(...skipping 16 matching lines...) Expand all Loading... | |
50 DISALLOW_COPY_AND_ASSIGN(UpdatePolicyTask); | 72 DISALLOW_COPY_AND_ASSIGN(UpdatePolicyTask); |
51 }; | 73 }; |
52 | 74 |
53 void AsynchronousPolicyLoader::Reload() { | 75 void AsynchronousPolicyLoader::Reload() { |
54 if (delegate_.get()) { | 76 if (delegate_.get()) { |
55 DictionaryValue* new_policy = delegate_->Load(); | 77 DictionaryValue* new_policy = delegate_->Load(); |
56 PostUpdatePolicyTask(new_policy); | 78 PostUpdatePolicyTask(new_policy); |
57 } | 79 } |
58 } | 80 } |
59 | 81 |
82 void AsynchronousPolicyLoader::CancelReloadTask() { | |
83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
84 if (reload_task_) { | |
85 reload_task_->Cancel(); | |
86 reload_task_ = NULL; | |
87 } | |
88 } | |
89 | |
90 void AsynchronousPolicyLoader::ScheduleReloadTask( | |
91 const base::TimeDelta& delay) { | |
92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
93 | |
94 CancelReloadTask(); | |
95 | |
96 reload_task_ = | |
97 NewRunnableMethod(this, &AsynchronousPolicyLoader::ReloadFromTask); | |
98 BrowserThread::PostDelayedTask(BrowserThread::FILE, FROM_HERE, reload_task_, | |
99 delay.InMilliseconds()); | |
100 } | |
101 | |
102 void AsynchronousPolicyLoader::ScheduleFallbackReloadTask() { | |
103 // As a safeguard in case that the load delegate failed to timely notice a | |
104 // change in policy, schedule a reload task that'll make us recheck after a | |
105 // reasonable interval. | |
106 ScheduleReloadTask(reload_interval_); | |
107 } | |
108 | |
109 void AsynchronousPolicyLoader::ReloadFromTask() { | |
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
111 | |
112 // Drop the reference to the reload task, since the task might be the only | |
113 // referer that keeps us alive, so we should not Cancel() it. | |
Jakob Kummerow
2010/12/21 16:56:10
nit: referrer
danno
2010/12/22 11:02:17
Done.
| |
114 reload_task_ = NULL; | |
115 | |
116 Reload(); | |
117 } | |
118 | |
119 void AsynchronousPolicyLoader::InitOnFileThread() { | |
120 } | |
121 | |
122 void AsynchronousPolicyLoader::StopOnFileThread() { | |
123 CancelReloadTask(); | |
124 } | |
125 | |
60 void AsynchronousPolicyLoader::PostUpdatePolicyTask( | 126 void AsynchronousPolicyLoader::PostUpdatePolicyTask( |
61 DictionaryValue* new_policy) { | 127 DictionaryValue* new_policy) { |
62 origin_loop_->PostTask(FROM_HERE, new UpdatePolicyTask(this, new_policy)); | 128 origin_loop_->PostTask(FROM_HERE, new UpdatePolicyTask(this, new_policy)); |
63 } | 129 } |
64 | 130 |
65 void AsynchronousPolicyLoader::UpdatePolicy(DictionaryValue* new_policy_raw) { | 131 void AsynchronousPolicyLoader::UpdatePolicy(DictionaryValue* new_policy_raw) { |
66 scoped_ptr<DictionaryValue> new_policy(new_policy_raw); | 132 scoped_ptr<DictionaryValue> new_policy(new_policy_raw); |
67 DCHECK(policy_.get()); | 133 DCHECK(policy_.get()); |
68 if (!policy_->Equals(new_policy.get())) { | 134 if (!policy_->Equals(new_policy.get())) { |
69 policy_.reset(new_policy.release()); | 135 policy_.reset(new_policy.release()); |
70 // TODO(danno): Change the notification between the provider and the | 136 // TODO(danno): Change the notification between the provider and the |
71 // PrefStore into a notification mechanism, removing the need for the | 137 // PrefStore into a notification mechanism, removing the need for the |
72 // WeakPtr for the provider. | 138 // WeakPtr for the provider. |
73 if (provider_) | 139 if (provider_) |
74 provider_->NotifyStoreOfPolicyChange(); | 140 provider_->NotifyStoreOfPolicyChange(); |
75 } | 141 } |
76 } | 142 } |
77 | 143 |
144 void AsynchronousPolicyLoader::InitAfterFileThreadAvailable() { | |
145 if (!stopped_) { | |
146 BrowserThread::PostTask( | |
147 BrowserThread::FILE, FROM_HERE, | |
148 NewRunnableMethod(this, &AsynchronousPolicyLoader::InitOnFileThread)); | |
149 } | |
150 } | |
151 | |
78 } // namespace policy | 152 } // namespace policy |
OLD | NEW |