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

Side by Side Diff: chrome/browser/managed_mode/managed_user_settings_service.cc

Issue 23466004: Add ManagedUserSettingsService and a SupervisedUserPrefStore using it. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix Created 7 years, 3 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 2013 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/managed_mode/managed_user_settings_service.h"
6
7 #include "base/callback.h"
8 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h"
10 #include "base/prefs/json_pref_store.h"
11 #include "base/strings/string_util.h"
12 #include "base/threading/sequenced_worker_pool.h"
13 #include "chrome/browser/managed_mode/managed_mode_url_filter.h"
14 #include "chrome/browser/prefs/incognito_mode_prefs.h"
15 #include "chrome/common/chrome_constants.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/user_metrics.h"
18
19 using base::DictionaryValue;
20 using base::Value;
21 using content::BrowserThread;
22 using content::UserMetricsAction;
23
24 const char kAtomicSettings[] = "atomic_settings";
25 const char kManagedUserInternalItemPrefix[] = "X-";
26 const char kQueuedItems[] = "queued_items";
27 const char kSplitSettingKeySeparator = ':';
28 const char kSplitSettings[] = "split_settings";
29
30 namespace {
31
32 bool SettingShouldApplyToPrefs(const std::string& name) {
33 return !StartsWithASCII(name, kManagedUserInternalItemPrefix, false);
34 }
35
36 } // namespace
37
38 ManagedUserSettingsService::ManagedUserSettingsService()
39 : active_(false), local_settings_(new DictionaryValue) {}
40
41 ManagedUserSettingsService::~ManagedUserSettingsService() {}
42
43 void ManagedUserSettingsService::Init(
44 scoped_refptr<PersistentPrefStore> store) {
45 DCHECK(!store_);
46 store_ = store;
47 store_->AddObserver(this);
48 }
49
50 void ManagedUserSettingsService::Subscribe(const SettingsCallback& callback) {
51 if (IsReady()) {
52 scoped_ptr<base::DictionaryValue> settings = GetSettings();
53 callback.Run(settings.get());
54 }
55
56 subscribers_.push_back(callback);
57 }
58
59 void ManagedUserSettingsService::Activate() {
60 active_ = true;
61 InformSubscribers();
62 }
63
64 bool ManagedUserSettingsService::IsReady() {
65 return store_->IsInitializationComplete();
66 }
67
68 void ManagedUserSettingsService::Clear() {
69 store_->RemoveValue(kAtomicSettings);
70 store_->RemoveValue(kSplitSettings);
71 }
72 void ManagedUserSettingsService::SetLocalSettingForTesting(
73 const std::string& key,
74 scoped_ptr<Value> value) {
75 if (value)
76 local_settings_->SetWithoutPathExpansion(key, value.release());
77 else
78 local_settings_->RemoveWithoutPathExpansion(key, NULL);
79
80 InformSubscribers();
81 }
82
83 void ManagedUserSettingsService::Shutdown() {
84 store_->RemoveObserver(this);
85 }
86 void ManagedUserSettingsService::OnPrefValueChanged(const std::string& key) {}
87
88 void ManagedUserSettingsService::OnInitializationCompleted(bool success) {
89 DCHECK(success);
90 DCHECK(IsReady());
91 InformSubscribers();
92 }
93
94 DictionaryValue* ManagedUserSettingsService::GetOrCreateDictionary(
95 const std::string& key) const {
96 Value* value = NULL;
97 DictionaryValue* dict = NULL;
98 if (store_->GetMutableValue(key, &value)) {
99 bool success = value->GetAsDictionary(&dict);
100 DCHECK(success);
101 } else {
102 dict = new base::DictionaryValue;
103 store_->SetValue(key, dict);
104 }
105
106 return dict;
107 }
108
109 DictionaryValue* ManagedUserSettingsService::GetAtomicSettings() const {
110 return GetOrCreateDictionary(kAtomicSettings);
111 }
112
113 DictionaryValue* ManagedUserSettingsService::GetSplitSettings() const {
114 return GetOrCreateDictionary(kSplitSettings);
115 }
116
117 DictionaryValue* ManagedUserSettingsService::GetQueuedItems() const {
118 return GetOrCreateDictionary(kQueuedItems);
119 }
120
121 scoped_ptr<DictionaryValue> ManagedUserSettingsService::GetSettings() {
122 DCHECK(IsReady());
123 if (!active_)
124 return scoped_ptr<base::DictionaryValue>();
125
126 scoped_ptr<DictionaryValue> settings(local_settings_->DeepCopy());
127
128 DictionaryValue* atomic_settings = GetAtomicSettings();
129 for (DictionaryValue::Iterator it(*atomic_settings); !it.IsAtEnd();
130 it.Advance()) {
131 if (!SettingShouldApplyToPrefs(it.key()))
132 continue;
133
134 settings->Set(it.key(), it.value().DeepCopy());
135 }
136
137 DictionaryValue* split_settings = GetSplitSettings();
138 for (DictionaryValue::Iterator it(*split_settings); !it.IsAtEnd();
139 it.Advance()) {
140 if (!SettingShouldApplyToPrefs(it.key()))
141 continue;
142
143 settings->Set(it.key(), it.value().DeepCopy());
144 }
145
146 return settings.Pass();
147 }
148
149 void ManagedUserSettingsService::InformSubscribers() {
150 if (!IsReady())
151 return;
152
153 scoped_ptr<base::DictionaryValue> settings = GetSettings();
154 for (std::vector<SettingsCallback>::iterator it = subscribers_.begin();
155 it != subscribers_.end(); ++it) {
156 it->Run(settings.get());
157 }
158 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698