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 the file thread directly before it is initialized |
| 28 // will cause the task to be forgotten. Instead, post a task to the ui thread |
| 29 // to delay the remainder of initialization until threading is fully |
| 30 // initialized. |
| 31 BrowserThread::PostTask( |
| 32 BrowserThread::UI, FROM_HERE, |
| 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 if (reload_task_) { |
| 84 // Only check the thread if there's still a reload task. During |
| 85 // destruction of unit tests, the message loop destruction can |
| 86 // call this method when the file thread is no longer around, |
| 87 // but in that case reload_task_ is NULL. |
| 88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 89 reload_task_->Cancel(); |
| 90 reload_task_ = NULL; |
| 91 } |
| 92 } |
| 93 |
| 94 void AsynchronousPolicyLoader::ScheduleReloadTask( |
| 95 const base::TimeDelta& delay) { |
| 96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 97 |
| 98 CancelReloadTask(); |
| 99 |
| 100 reload_task_ = |
| 101 NewRunnableMethod(this, &AsynchronousPolicyLoader::ReloadFromTask); |
| 102 BrowserThread::PostDelayedTask(BrowserThread::FILE, FROM_HERE, reload_task_, |
| 103 delay.InMilliseconds()); |
| 104 } |
| 105 |
| 106 void AsynchronousPolicyLoader::ScheduleFallbackReloadTask() { |
| 107 // As a safeguard in case that the load delegate failed to timely notice a |
| 108 // change in policy, schedule a reload task that'll make us recheck after a |
| 109 // reasonable interval. |
| 110 ScheduleReloadTask(reload_interval_); |
| 111 } |
| 112 |
| 113 void AsynchronousPolicyLoader::ReloadFromTask() { |
| 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 115 |
| 116 // Drop the reference to the reload task, since the task might be the only |
| 117 // referrer that keeps us alive, so we should not Cancel() it. |
| 118 reload_task_ = NULL; |
| 119 |
| 120 Reload(); |
| 121 } |
| 122 |
| 123 void AsynchronousPolicyLoader::InitOnFileThread() { |
| 124 } |
| 125 |
| 126 void AsynchronousPolicyLoader::StopOnFileThread() { |
| 127 CancelReloadTask(); |
| 128 } |
| 129 |
60 void AsynchronousPolicyLoader::PostUpdatePolicyTask( | 130 void AsynchronousPolicyLoader::PostUpdatePolicyTask( |
61 DictionaryValue* new_policy) { | 131 DictionaryValue* new_policy) { |
62 origin_loop_->PostTask(FROM_HERE, new UpdatePolicyTask(this, new_policy)); | 132 origin_loop_->PostTask(FROM_HERE, new UpdatePolicyTask(this, new_policy)); |
63 } | 133 } |
64 | 134 |
65 void AsynchronousPolicyLoader::UpdatePolicy(DictionaryValue* new_policy_raw) { | 135 void AsynchronousPolicyLoader::UpdatePolicy(DictionaryValue* new_policy_raw) { |
66 scoped_ptr<DictionaryValue> new_policy(new_policy_raw); | 136 scoped_ptr<DictionaryValue> new_policy(new_policy_raw); |
67 DCHECK(policy_.get()); | 137 DCHECK(policy_.get()); |
68 if (!policy_->Equals(new_policy.get())) { | 138 if (!policy_->Equals(new_policy.get())) { |
69 policy_.reset(new_policy.release()); | 139 policy_.reset(new_policy.release()); |
70 // TODO(danno): Change the notification between the provider and the | 140 // TODO(danno): Change the notification between the provider and the |
71 // PrefStore into a notification mechanism, removing the need for the | 141 // PrefStore into a notification mechanism, removing the need for the |
72 // WeakPtr for the provider. | 142 // WeakPtr for the provider. |
73 if (provider_) | 143 if (provider_) |
74 provider_->NotifyStoreOfPolicyChange(); | 144 provider_->NotifyStoreOfPolicyChange(); |
75 } | 145 } |
76 } | 146 } |
77 | 147 |
| 148 void AsynchronousPolicyLoader::InitAfterFileThreadAvailable() { |
| 149 if (!stopped_) { |
| 150 BrowserThread::PostTask( |
| 151 BrowserThread::FILE, FROM_HERE, |
| 152 NewRunnableMethod(this, &AsynchronousPolicyLoader::InitOnFileThread)); |
| 153 } |
| 154 } |
| 155 |
78 } // namespace policy | 156 } // namespace policy |
OLD | NEW |