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

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
19 const char ManagedModePolicyProvider::kPolicies[] = "policies";
20
21 // static
22 ManagedModePolicyProvider* ManagedModePolicyProvider::Create(Profile* profile) {
23 JsonPrefStore* pref_store =
24 new JsonPrefStore(profile->GetPath().Append(
25 chrome::kManagedModePolicyFilename),
26 BrowserThread::GetMessageLoopProxyForThread(
27 BrowserThread::FILE));
28 return new ManagedModePolicyProvider(pref_store);
29 }
30
31 ManagedModePolicyProvider::ManagedModePolicyProvider(
32 PersistentPrefStore* pref_store)
33 : store_(pref_store) {
34 store_->AddObserver(this);
35 store_->ReadPrefsAsync(NULL);
36 }
37
38 ManagedModePolicyProvider::~ManagedModePolicyProvider() {
39 store_->RemoveObserver(this);
40 }
41
42 const base::Value* ManagedModePolicyProvider::GetPolicy(
43 const std::string& key) const {
44 base::DictionaryValue* dict = GetCachedPolicy();
45 base::Value* value = NULL;
46 dict->GetWithoutPathExpansion(key, &value);
47 return value;
48 }
49
50 void ManagedModePolicyProvider::SetPolicy(const std::string& key,
51 const base::Value* value) {
52 base::DictionaryValue* dict = GetCachedPolicy();
53 dict->SetWithoutPathExpansion(key, value->DeepCopy());
54 store_->ReportValueChanged(kPolicies);
55 UpdatePolicyFromCache();
56 }
57
58 void ManagedModePolicyProvider::RefreshPolicies() {
59 UpdatePolicyFromCache();
60 }
61
62 bool ManagedModePolicyProvider::IsInitializationComplete() const {
63 return store_->IsInitializationComplete();
64 }
65
66 void ManagedModePolicyProvider::OnPrefValueChanged(const std::string& key) {}
67
68 void ManagedModePolicyProvider::OnInitializationCompleted(bool success) {
69 DCHECK(success);
70 UpdatePolicyFromCache();
71 }
72
73 base::DictionaryValue* ManagedModePolicyProvider::GetCachedPolicy() const {
74 base::Value* value = NULL;
75 base::DictionaryValue* dict = NULL;
76 PrefStore::ReadResult result = store_->GetMutableValue(kPolicies, &value);
77 switch (result) {
78 case PrefStore::READ_NO_VALUE: {
79 dict = new base::DictionaryValue;
80 store_->SetValue(kPolicies, dict);
81 break;
82 }
83 case PrefStore::READ_OK: {
84 bool success = value->GetAsDictionary(&dict);
85 DCHECK(success);
86 break;
87 }
88 default:
89 NOTREACHED();
90 }
91
92 return dict;
93 }
94
95 void ManagedModePolicyProvider::UpdatePolicyFromCache() {
96 scoped_ptr<PolicyBundle> policy_bundle(new PolicyBundle);
97 PolicyMap* policy_map =
98 &policy_bundle->Get(POLICY_DOMAIN_CHROME, std::string());
99 policy_map->LoadFrom(GetCachedPolicy(),
100 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER);
101 UpdatePolicy(policy_bundle.Pass());
102 }
103
104 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/managed_mode_policy_provider.h ('k') | chrome/browser/policy/managed_mode_policy_provider_factory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698