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 |
new file mode 100644 |
index 0000000000000000000000000000000000000000..816bde4cd2feb874db58daff01c37f448cd3493b |
--- /dev/null |
+++ b/chrome/browser/policy/asynchronous_policy_loader.cc |
@@ -0,0 +1,75 @@ |
+// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/policy/asynchronous_policy_loader.h" |
+ |
+#include "base/message_loop.h" |
+#include "base/task.h" |
+#include "chrome/browser/browser_thread.h" |
+ |
+namespace policy { |
+ |
+AsynchronousPolicyLoader::AsynchronousPolicyLoader( |
+ AsynchronousPolicyProvider::Delegate* delegate) |
+ : delegate_(delegate), |
+ origin_loop_(MessageLoop::current()) {} |
Mattias Nissler (ping if slow)
2010/12/06 10:26:20
Move closing brace to next line for scanability :)
danno
2010/12/06 14:05:12
Actually, in my readability review, my reviewer su
|
+ |
+void AsynchronousPolicyLoader::Init() { |
+ policy_.reset(delegate_->Load()); |
+} |
+ |
+void AsynchronousPolicyLoader::Stop() { |
+ DCHECK_EQ(MessageLoop::current(), origin_loop_); |
+ delegate_.reset(); |
+} |
+ |
+void AsynchronousPolicyLoader::SetProvider( |
+ base::WeakPtr<AsynchronousPolicyProvider> provider) { |
+ provider_ = provider; |
+} |
+ |
+// Manages the life cycle of a new policy while is pending on the message |
Mattias Nissler (ping if slow)
2010/12/06 10:26:20
What is pending?
danno
2010/12/06 14:05:12
Done.
|
+// loop waiting to be updated. |
+class UpdatePolicyTask : public Task { |
+ public: |
+ UpdatePolicyTask(scoped_refptr<AsynchronousPolicyLoader> loader, |
+ DictionaryValue* new_policy) |
+ : loader_(loader), |
+ new_policy_(new_policy) {} |
+ |
+ virtual void Run() { |
+ loader_->UpdatePolicy(new_policy_.release()); |
+ } |
+ |
+ private: |
+ scoped_refptr<AsynchronousPolicyLoader> loader_; |
+ scoped_ptr<DictionaryValue> new_policy_; |
+ DISALLOW_COPY_AND_ASSIGN(UpdatePolicyTask); |
+}; |
+ |
+void AsynchronousPolicyLoader::Reload() { |
+ if (delegate_.get()) { |
+ DictionaryValue* new_policy = delegate_->Load(); |
+ PostUpdatePolicyTask(new_policy); |
+ } |
+} |
+ |
+void AsynchronousPolicyLoader::PostUpdatePolicyTask( |
+ DictionaryValue* new_policy) { |
+ origin_loop_->PostTask(FROM_HERE, new UpdatePolicyTask(this, new_policy)); |
+} |
+ |
+void AsynchronousPolicyLoader::UpdatePolicy(DictionaryValue* new_policy_raw) { |
+ DCHECK(policy_.get()); |
+ if (!policy_->Equals(new_policy_raw)) { |
+ policy_.reset(new_policy_raw); |
+ // TODO(danno): Change the notification between the provider and the |
+ // PrefStore into a notification mechanism, removing the need for the |
+ // WeakPtr for the provider. |
+ if (provider_.get()) |
+ provider_->NotifyStoreOfPolicyChange(); |
+ } |
+} |
+ |
+} // namespace policy |