Add GCMChannelStatusSyncer to schedule requests and enable/disable GCM
[chromium-blink-merge.git] / chrome / browser / supervised_user / supervised_user_pref_store.cc
blob5d0dab4fab865be001d04e005ee4303afa394757
1 // Copyright 2014 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.
5 #include "chrome/browser/supervised_user/supervised_user_pref_store.h"
7 #include "base/bind.h"
8 #include "base/prefs/pref_value_map.h"
9 #include "base/values.h"
10 #include "chrome/browser/prefs/incognito_mode_prefs.h"
11 #include "chrome/browser/supervised_user/supervised_user_constants.h"
12 #include "chrome/browser/supervised_user/supervised_user_settings_service.h"
13 #include "chrome/browser/supervised_user/supervised_user_url_filter.h"
14 #include "chrome/common/pref_names.h"
16 using base::FundamentalValue;
18 namespace {
20 struct SupervisedUserSettingsPrefMappingEntry {
21 const char* settings_name;
22 const char* pref_name;
25 SupervisedUserSettingsPrefMappingEntry kSupervisedUserSettingsPrefMapping[] = {
27 supervised_users::kContentPackDefaultFilteringBehavior,
28 prefs::kDefaultSupervisedUserFilteringBehavior,
31 supervised_users::kContentPackManualBehaviorHosts,
32 prefs::kSupervisedUserManualHosts,
35 supervised_users::kContentPackManualBehaviorURLs,
36 prefs::kSupervisedUserManualURLs,
39 supervised_users::kForceSafeSearch,
40 prefs::kForceSafeSearch,
43 supervised_users::kSigninAllowed,
44 prefs::kSigninAllowed,
47 supervised_users::kUserName,
48 prefs::kProfileName,
52 } // namespace
54 SupervisedUserPrefStore::SupervisedUserPrefStore(
55 SupervisedUserSettingsService* supervised_user_settings_service)
56 : weak_ptr_factory_(this) {
57 supervised_user_settings_service->Subscribe(
58 base::Bind(&SupervisedUserPrefStore::OnNewSettingsAvailable,
59 weak_ptr_factory_.GetWeakPtr()));
62 bool SupervisedUserPrefStore::GetValue(const std::string& key,
63 const base::Value** value) const {
64 DCHECK(prefs_);
65 return prefs_->GetValue(key, value);
68 void SupervisedUserPrefStore::AddObserver(PrefStore::Observer* observer) {
69 observers_.AddObserver(observer);
72 void SupervisedUserPrefStore::RemoveObserver(PrefStore::Observer* observer) {
73 observers_.RemoveObserver(observer);
76 bool SupervisedUserPrefStore::HasObservers() const {
77 return observers_.might_have_observers();
80 bool SupervisedUserPrefStore::IsInitializationComplete() const {
81 return !!prefs_;
84 SupervisedUserPrefStore::~SupervisedUserPrefStore() {
87 void SupervisedUserPrefStore::OnNewSettingsAvailable(
88 const base::DictionaryValue* settings) {
89 scoped_ptr<PrefValueMap> old_prefs = prefs_.Pass();
90 prefs_.reset(new PrefValueMap);
91 if (settings) {
92 // Set hardcoded prefs.
93 prefs_->SetValue(prefs::kAllowDeletingBrowserHistory,
94 new FundamentalValue(false));
95 prefs_->SetValue(prefs::kDefaultSupervisedUserFilteringBehavior,
96 new FundamentalValue(SupervisedUserURLFilter::ALLOW));
97 prefs_->SetValue(prefs::kForceSafeSearch, new FundamentalValue(true));
98 prefs_->SetValue(prefs::kHideWebStoreIcon, new FundamentalValue(true));
99 prefs_->SetValue(prefs::kIncognitoModeAvailability,
100 new FundamentalValue(IncognitoModePrefs::DISABLED));
101 prefs_->SetValue(prefs::kSigninAllowed, new FundamentalValue(false));
103 // Copy supervised user settings to prefs.
104 for (size_t i = 0; i < arraysize(kSupervisedUserSettingsPrefMapping); ++i) {
105 const SupervisedUserSettingsPrefMappingEntry& entry =
106 kSupervisedUserSettingsPrefMapping[i];
107 const base::Value* value = NULL;
108 if (settings->GetWithoutPathExpansion(entry.settings_name, &value))
109 prefs_->SetValue(entry.pref_name, value->DeepCopy());
113 if (!old_prefs) {
114 FOR_EACH_OBSERVER(Observer, observers_, OnInitializationCompleted(true));
115 return;
118 std::vector<std::string> changed_prefs;
119 prefs_->GetDifferingKeys(old_prefs.get(), &changed_prefs);
121 // Send out change notifications.
122 for (std::vector<std::string>::const_iterator pref(changed_prefs.begin());
123 pref != changed_prefs.end();
124 ++pref) {
125 FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(*pref));