Index: chrome/browser/policy/asynchronous_policy_loader.cc |
diff --git a/chrome/browser/policy/asynchronous_policy_loader.cc b/chrome/browser/policy/asynchronous_policy_loader.cc |
index e78c9aaf82c26fc01a3e94fb81f0e752bf5660a0..ef1a0aca040be77dfc34d9727aae4a6941ad119d 100644 |
--- a/chrome/browser/policy/asynchronous_policy_loader.cc |
+++ b/chrome/browser/policy/asynchronous_policy_loader.cc |
@@ -6,21 +6,43 @@ |
#include "base/message_loop.h" |
#include "base/task.h" |
+#include "chrome/browser/browser_thread.h" |
namespace policy { |
AsynchronousPolicyLoader::AsynchronousPolicyLoader( |
- AsynchronousPolicyProvider::Delegate* delegate) |
+ AsynchronousPolicyProvider::Delegate* delegate, |
+ int reload_interval_minutes) |
: delegate_(delegate), |
provider_(NULL), |
- origin_loop_(MessageLoop::current()) {} |
+ reload_task_(NULL), |
+ reload_interval_(base::TimeDelta::FromMinutes(reload_interval_minutes)), |
+ origin_loop_(MessageLoop::current()), |
+ stopped_(false) {} |
void AsynchronousPolicyLoader::Init() { |
policy_.reset(delegate_->Load()); |
+ // Initialization can happen early when the file thread is not yet available, |
+ // but the subclass of the loader must do some of their initialization on the |
+ // 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.
|
+ // initialized will cause the task to be forgotten. Instead, post a task to |
+ // the ui thread to delay the remainder of initialization until threading is |
+ // fully initialized. |
+ BrowserThread::PostTask( |
+ 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
|
+ NewRunnableMethod( |
+ this, |
+ &AsynchronousPolicyLoader::InitAfterFileThreadAvailable)); |
} |
void AsynchronousPolicyLoader::Stop() { |
- delegate_.reset(); |
+ if (!stopped_) { |
+ stopped_ = true; |
+ delegate_.reset(); |
+ BrowserThread::PostTask( |
+ BrowserThread::FILE, FROM_HERE, |
+ NewRunnableMethod(this, &AsynchronousPolicyLoader::StopOnFileThread)); |
+ } |
} |
void AsynchronousPolicyLoader::SetProvider( |
@@ -57,6 +79,50 @@ void AsynchronousPolicyLoader::Reload() { |
} |
} |
+void AsynchronousPolicyLoader::CancelReloadTask() { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
+ if (reload_task_) { |
+ reload_task_->Cancel(); |
+ reload_task_ = NULL; |
+ } |
+} |
+ |
+void AsynchronousPolicyLoader::ScheduleReloadTask( |
+ const base::TimeDelta& delay) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
+ |
+ CancelReloadTask(); |
+ |
+ reload_task_ = |
+ NewRunnableMethod(this, &AsynchronousPolicyLoader::ReloadFromTask); |
+ BrowserThread::PostDelayedTask(BrowserThread::FILE, FROM_HERE, reload_task_, |
+ delay.InMilliseconds()); |
+} |
+ |
+void AsynchronousPolicyLoader::ScheduleFallbackReloadTask() { |
+ // As a safeguard in case that the load delegate failed to timely notice a |
+ // change in policy, schedule a reload task that'll make us recheck after a |
+ // reasonable interval. |
+ ScheduleReloadTask(reload_interval_); |
+} |
+ |
+void AsynchronousPolicyLoader::ReloadFromTask() { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
+ |
+ // Drop the reference to the reload task, since the task might be the only |
+ // 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.
|
+ reload_task_ = NULL; |
+ |
+ Reload(); |
+} |
+ |
+void AsynchronousPolicyLoader::InitOnFileThread() { |
+} |
+ |
+void AsynchronousPolicyLoader::StopOnFileThread() { |
+ CancelReloadTask(); |
+} |
+ |
void AsynchronousPolicyLoader::PostUpdatePolicyTask( |
DictionaryValue* new_policy) { |
origin_loop_->PostTask(FROM_HERE, new UpdatePolicyTask(this, new_policy)); |
@@ -75,4 +141,12 @@ void AsynchronousPolicyLoader::UpdatePolicy(DictionaryValue* new_policy_raw) { |
} |
} |
+void AsynchronousPolicyLoader::InitAfterFileThreadAvailable() { |
+ if (!stopped_) { |
+ BrowserThread::PostTask( |
+ BrowserThread::FILE, FROM_HERE, |
+ NewRunnableMethod(this, &AsynchronousPolicyLoader::InitOnFileThread)); |
+ } |
+} |
+ |
} // namespace policy |