summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/managed_mode_policy_provider.cc
blob: c55f87f5c46b0bf45eee0cc8c44d31955c4a13d2 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) 2012 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/managed_mode_policy_provider.h"

#include "base/prefs/json_pref_store.h"
#include "base/threading/sequenced_worker_pool.h"
#include "chrome/browser/policy/policy_bundle.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_constants.h"
#include "content/public/browser/browser_thread.h"

using content::BrowserThread;

namespace policy {

// static
const char ManagedModePolicyProvider::kPolicies[] = "policies";

// static
ManagedModePolicyProvider* ManagedModePolicyProvider::Create(Profile* profile) {
  FilePath path = profile->GetPath().Append(chrome::kManagedModePolicyFilename);
  JsonPrefStore* pref_store = new JsonPrefStore(
      path,
      JsonPrefStore::GetTaskRunnerForFile(path,
                                          BrowserThread::GetBlockingPool()));
  return new ManagedModePolicyProvider(pref_store);
}

ManagedModePolicyProvider::ManagedModePolicyProvider(
    PersistentPrefStore* pref_store)
    : store_(pref_store) {
  store_->AddObserver(this);
  store_->ReadPrefsAsync(NULL);
}

ManagedModePolicyProvider::~ManagedModePolicyProvider() {}

const base::Value* ManagedModePolicyProvider::GetPolicy(
    const std::string& key) const {
  base::DictionaryValue* dict = GetCachedPolicy();
  base::Value* value = NULL;

  // Returns NULL if the key doesn't exist.
  dict->GetWithoutPathExpansion(key, &value);
  return value;
}

void ManagedModePolicyProvider::SetPolicy(const std::string& key,
                                          const base::Value* value) {
  base::DictionaryValue* dict = GetCachedPolicy();
  dict->SetWithoutPathExpansion(key, value->DeepCopy());
  store_->ReportValueChanged(kPolicies);
  UpdatePolicyFromCache();
}

void ManagedModePolicyProvider::Shutdown() {
  store_->RemoveObserver(this);
  ConfigurationPolicyProvider::Shutdown();
}

void ManagedModePolicyProvider::RefreshPolicies() {
  UpdatePolicyFromCache();
}

bool ManagedModePolicyProvider::IsInitializationComplete() const {
  return store_->IsInitializationComplete();
}

void ManagedModePolicyProvider::OnPrefValueChanged(const std::string& key) {}

void ManagedModePolicyProvider::OnInitializationCompleted(bool success) {
  DCHECK(success);
  UpdatePolicyFromCache();
}

base::DictionaryValue* ManagedModePolicyProvider::GetCachedPolicy() const {
  base::Value* value = NULL;
  base::DictionaryValue* dict = NULL;
  PrefStore::ReadResult result = store_->GetMutableValue(kPolicies, &value);
  switch (result) {
    case PrefStore::READ_NO_VALUE: {
      dict = new base::DictionaryValue;
      store_->SetValue(kPolicies, dict);
      break;
    }
    case PrefStore::READ_OK: {
      bool success = value->GetAsDictionary(&dict);
      DCHECK(success);
      break;
    }
    default:
      NOTREACHED();
  }

  return dict;
}

void ManagedModePolicyProvider::UpdatePolicyFromCache() {
  scoped_ptr<PolicyBundle> policy_bundle(new PolicyBundle);
  PolicyMap* policy_map =
      &policy_bundle->Get(POLICY_DOMAIN_CHROME, std::string());
  policy_map->LoadFrom(GetCachedPolicy(),
                       POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER);
  UpdatePolicy(policy_bundle.Pass());
}

}  // namespace policy