Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(392)

Side by Side Diff: chrome/browser/policy/managed_mode_policy_provider.cc

Issue 10510006: Add ManagedModePolicyProvider and extension API to get and set policies. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/policy/managed_mode_policy_provider.h"
6
7 #include "chrome/browser/policy/policy_bundle.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/common/json_pref_store.h"
10 #include "chrome/common/chrome_constants.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "policy/policy_constants.h"
13
14 using content::BrowserThread;
15
16 namespace policy {
17
18 static const char kPolicies[] = "policies";
19
20 ManagedModePolicyProvider::ManagedModePolicyProvider(Profile* profile)
21 : store_(new JsonPrefStore(profile->GetPath().Append(
22 chrome::kManagedModePolicyFilename),
23 BrowserThread::GetMessageLoopProxyForThread(
24 BrowserThread::FILE))) {
25 store_->AddObserver(this);
26 store_->ReadPrefsAsync(NULL);
27 }
28
29 ManagedModePolicyProvider::~ManagedModePolicyProvider() {
30 store_->RemoveObserver(this);
31 }
32
33 const base::Value* ManagedModePolicyProvider::GetPolicy(
34 const std::string& key) const {
35 base::DictionaryValue* dict = GetCachedPolicy();
36 base::Value* value = NULL;
37 dict->GetWithoutPathExpansion(key, &value);
38 return value;
39 }
40
41 void ManagedModePolicyProvider::SetPolicy(const std::string& key,
42 const base::Value* value) {
43 base::DictionaryValue* dict = GetCachedPolicy();
44 dict->SetWithoutPathExpansion(key, value->DeepCopy());
45 store_->ReportValueChanged(kPolicies);
46 UpdatePolicyFromCache();
47 }
48
49 void ManagedModePolicyProvider::RefreshPolicies() {
50 UpdatePolicyFromCache();
51 }
52
53 bool ManagedModePolicyProvider::IsInitializationComplete() const {
54 return store_->IsInitializationComplete();
55 }
56
57 void ManagedModePolicyProvider::OnPrefValueChanged(const std::string& key) {}
58
59 void ManagedModePolicyProvider::OnInitializationCompleted(bool success) {
60 DCHECK(success);
61 UpdatePolicyFromCache();
62 }
63
64 base::DictionaryValue* ManagedModePolicyProvider::GetCachedPolicy() const {
65 base::Value* value = NULL;
66 base::DictionaryValue* dict = NULL;
67 PrefStore::ReadResult result = store_->GetMutableValue(kPolicies, &value);
68 if (result == PrefStore::READ_NO_VALUE) {
69 dict = new base::DictionaryValue;
70 store_->SetValue(kPolicies, dict);
71 } else if (result == PrefStore::READ_OK) {
72 bool success = value->GetAsDictionary(&dict);
73 DCHECK(success);
74 } else {
75 NOTREACHED();
76 }
77
78 return dict;
79 }
80
81 void ManagedModePolicyProvider::UpdatePolicyFromCache() {
82 const base::DictionaryValue* dict = GetCachedPolicy();
83 scoped_ptr<PolicyBundle> policy_bundle(new PolicyBundle);
84 PolicyMap* policy_map =
85 &policy_bundle->Get(POLICY_DOMAIN_CHROME, std::string());
86 policy_map->LoadFrom(dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER);
87 UpdatePolicy(policy_bundle.Pass());
88 }
89
90 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698