summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/asynchronous_policy_loader.cc
blob: ea27bb767dfd4e5bbd9021c310d7b858a1305ae7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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),
      provider_(NULL),
      origin_loop_(MessageLoop::current()) {}

void AsynchronousPolicyLoader::Init() {
  policy_.reset(delegate_->Load());
}

void AsynchronousPolicyLoader::Stop() {
  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