OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
10 | 10 |
11 using content::BrowserThread; | 11 using content::BrowserThread; |
12 | 12 |
13 namespace policy { | 13 namespace policy { |
14 | 14 |
15 AsynchronousPolicyLoader::AsynchronousPolicyLoader( | 15 AsynchronousPolicyLoader::AsynchronousPolicyLoader( |
16 AsynchronousPolicyProvider::Delegate* delegate, | 16 AsynchronousPolicyProvider::Delegate* delegate, |
17 int reload_interval_minutes) | 17 int reload_interval_minutes) |
18 : delegate_(delegate), | 18 : delegate_(delegate), |
19 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), | 19 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), |
20 reload_interval_(base::TimeDelta::FromMinutes(reload_interval_minutes)), | 20 reload_interval_(base::TimeDelta::FromMinutes(reload_interval_minutes)), |
21 origin_loop_(MessageLoop::current()), | 21 origin_loop_(MessageLoop::current()), |
22 stopped_(false) {} | 22 stopped_(false) {} |
23 | 23 |
24 void AsynchronousPolicyLoader::Init(const base::Closure& callback) { | 24 void AsynchronousPolicyLoader::Init(const base::Closure& default_callback) { |
25 updates_callback_ = callback; | 25 default_callback_ = default_callback; |
26 policy_.reset(delegate_->Load()); | 26 policy_.reset(delegate_->Load()); |
27 // Initialization can happen early when the file thread is not yet available, | 27 // Initialization can happen early when the file thread is not yet available, |
28 // but the subclass of the loader must do some of their initialization on the | 28 // but the subclass of the loader must do some of their initialization on the |
29 // file thread. Posting to the file thread directly before it is initialized | 29 // file thread. Posting to the file thread directly before it is initialized |
30 // will cause the task to be forgotten. Instead, post a task to the ui thread | 30 // will cause the task to be forgotten. Instead, post a task to the ui thread |
31 // to delay the remainder of initialization until threading is fully | 31 // to delay the remainder of initialization until threading is fully |
32 // initialized. | 32 // initialized. |
33 BrowserThread::PostTask( | 33 BrowserThread::PostTask( |
34 BrowserThread::UI, FROM_HERE, | 34 BrowserThread::UI, FROM_HERE, |
35 base::Bind(&AsynchronousPolicyLoader::InitAfterFileThreadAvailable, | 35 base::Bind(&AsynchronousPolicyLoader::InitAfterFileThreadAvailable, |
36 this)); | 36 this)); |
37 } | 37 } |
38 | 38 |
39 void AsynchronousPolicyLoader::Stop() { | 39 void AsynchronousPolicyLoader::Stop() { |
40 if (!stopped_) { | 40 if (!stopped_) { |
41 stopped_ = true; | 41 stopped_ = true; |
42 BrowserThread::PostTask( | 42 BrowserThread::PostTask( |
43 BrowserThread::FILE, FROM_HERE, | 43 BrowserThread::FILE, FROM_HERE, |
44 base::Bind(&AsynchronousPolicyLoader::StopOnFileThread, this)); | 44 base::Bind(&AsynchronousPolicyLoader::StopOnFileThread, this)); |
45 } | 45 } |
46 } | 46 } |
47 | 47 |
48 AsynchronousPolicyLoader::~AsynchronousPolicyLoader() { | 48 AsynchronousPolicyLoader::~AsynchronousPolicyLoader() { |
49 } | 49 } |
50 | 50 |
51 void AsynchronousPolicyLoader::Reload(bool force) { | 51 void AsynchronousPolicyLoader::Reload(const base::Closure& callback, |
| 52 bool force) { |
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
53 if (delegate_.get()) { | 54 if (delegate_.get()) { |
54 PostUpdatePolicyTask(delegate_->Load()); | 55 PostUpdatePolicyTask(callback, delegate_->Load()); |
| 56 } else { |
| 57 PostUpdatePolicyTask(callback, NULL); |
55 } | 58 } |
56 } | 59 } |
57 | 60 |
58 void AsynchronousPolicyLoader::CancelReloadTask() { | 61 void AsynchronousPolicyLoader::CancelReloadTask() { |
59 weak_ptr_factory_.InvalidateWeakPtrs(); | 62 weak_ptr_factory_.InvalidateWeakPtrs(); |
60 } | 63 } |
61 | 64 |
62 void AsynchronousPolicyLoader::ScheduleReloadTask( | 65 void AsynchronousPolicyLoader::ScheduleReloadTask( |
| 66 const base::Closure& callback, |
63 const base::TimeDelta& delay) { | 67 const base::TimeDelta& delay) { |
64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
65 | 69 |
66 CancelReloadTask(); | 70 CancelReloadTask(); |
67 | 71 |
68 BrowserThread::PostDelayedTask( | 72 BrowserThread::PostDelayedTask( |
69 BrowserThread::FILE, FROM_HERE, | 73 BrowserThread::FILE, FROM_HERE, |
70 base::Bind(&AsynchronousPolicyLoader::ReloadFromTask, | 74 base::Bind(&AsynchronousPolicyLoader::Reload, |
71 weak_ptr_factory_.GetWeakPtr()), | 75 weak_ptr_factory_.GetWeakPtr(), |
| 76 callback, |
| 77 false), |
72 delay.InMilliseconds()); | 78 delay.InMilliseconds()); |
73 } | 79 } |
74 | 80 |
75 void AsynchronousPolicyLoader::ScheduleFallbackReloadTask() { | 81 void AsynchronousPolicyLoader::ScheduleFallbackReloadTask() { |
76 // As a safeguard in case that the load delegate failed to timely notice a | 82 // As a safeguard in case that the load delegate failed to timely notice a |
77 // change in policy, schedule a reload task that'll make us recheck after a | 83 // change in policy, schedule a reload task that'll make us recheck after a |
78 // reasonable interval. | 84 // reasonable interval. |
79 ScheduleReloadTask(reload_interval_); | 85 ScheduleReloadTask(default_callback_, reload_interval_); |
80 } | |
81 | |
82 void AsynchronousPolicyLoader::ReloadFromTask() { | |
83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
84 Reload(false); | |
85 } | 86 } |
86 | 87 |
87 void AsynchronousPolicyLoader::InitOnFileThread() { | 88 void AsynchronousPolicyLoader::InitOnFileThread() { |
88 } | 89 } |
89 | 90 |
90 void AsynchronousPolicyLoader::StopOnFileThread() { | 91 void AsynchronousPolicyLoader::StopOnFileThread() { |
91 delegate_.reset(); | 92 delegate_.reset(); |
92 CancelReloadTask(); | 93 CancelReloadTask(); |
93 } | 94 } |
94 | 95 |
95 void AsynchronousPolicyLoader::PostUpdatePolicyTask( | 96 void AsynchronousPolicyLoader::PostUpdatePolicyTask( |
| 97 const base::Closure& callback, |
96 DictionaryValue* new_policy) { | 98 DictionaryValue* new_policy) { |
97 // TODO(joaodasilva): make the callback own |new_policy|. | 99 // TODO(joaodasilva): make the callback own |new_policy|. |
98 origin_loop_->PostTask( | 100 origin_loop_->PostTask( |
99 FROM_HERE, | 101 FROM_HERE, |
100 base::Bind(&AsynchronousPolicyLoader::UpdatePolicy, this, new_policy)); | 102 base::Bind(&AsynchronousPolicyLoader::UpdatePolicy, |
| 103 this, |
| 104 callback, |
| 105 new_policy)); |
101 } | 106 } |
102 | 107 |
103 void AsynchronousPolicyLoader::UpdatePolicy(DictionaryValue* new_policy_raw) { | 108 void AsynchronousPolicyLoader::UpdatePolicy(const base::Closure& callback, |
104 scoped_ptr<DictionaryValue> new_policy(new_policy_raw); | 109 DictionaryValue* new_policy) { |
105 DCHECK(policy_.get()); | 110 policy_.reset(new_policy); |
106 policy_.swap(new_policy); | |
107 if (!stopped_) | 111 if (!stopped_) |
108 updates_callback_.Run(); | 112 callback.Run(); |
109 } | 113 } |
110 | 114 |
111 void AsynchronousPolicyLoader::InitAfterFileThreadAvailable() { | 115 void AsynchronousPolicyLoader::InitAfterFileThreadAvailable() { |
112 if (!stopped_) { | 116 if (!stopped_) { |
113 BrowserThread::PostTask( | 117 BrowserThread::PostTask( |
114 BrowserThread::FILE, FROM_HERE, | 118 BrowserThread::FILE, FROM_HERE, |
115 base::Bind(&AsynchronousPolicyLoader::InitOnFileThread, this)); | 119 base::Bind(&AsynchronousPolicyLoader::InitOnFileThread, this)); |
116 } | 120 } |
117 } | 121 } |
118 | 122 |
119 } // namespace policy | 123 } // namespace policy |
OLD | NEW |