Chromium Code Reviews

Unified Diff: chrome/browser/policy/asynchronous_policy_loader.cc

Issue 5562002: Refactor FileBasedPolicyProvider, introduce AsynchronousPolicyProvider. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review feedback Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
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..599c446318d7197c1b4ca9d99beb248e7f6d1a62
--- /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()) {}
+
+void AsynchronousPolicyLoader::Init() {
+ policy_.reset(delegate_->Load());
+}
+
+void AsynchronousPolicyLoader::Stop() {
+ DCHECK_EQ(MessageLoop::current(), origin_loop_);
Mattias Nissler (ping if slow) 2010/12/09 09:27:17 Why did you remove this?
danno 2010/12/09 10:23:02 For the singleton policy provider in the real app,
+ delegate_.reset();
+}
+
+void AsynchronousPolicyLoader::SetProvider(
+ AsynchronousPolicyProvider* provider) {
+ provider_ = provider;
+}
+
+// Manages the life cycle of a new policy map during until it's life cycle is
+// taken over by the policy loader.
+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_)
+ provider_->NotifyStoreOfPolicyChange();
+ }
+}
+
+} // namespace policy
« no previous file with comments | « chrome/browser/policy/asynchronous_policy_loader.h ('k') | chrome/browser/policy/asynchronous_policy_loader_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine