Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / supervised_user / supervised_user_pref_store.cc
blob088f2bc03e61b51ddf49a50f68605ab88520a40e
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 <vector>
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "base/prefs/pref_value_map.h"
12 #include "base/values.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/prefs/incognito_mode_prefs.h"
15 #include "chrome/browser/supervised_user/supervised_user_bookmarks_handler.h"
16 #include "chrome/browser/supervised_user/supervised_user_constants.h"
17 #include "chrome/browser/supervised_user/supervised_user_settings_service.h"
18 #include "chrome/browser/supervised_user/supervised_user_url_filter.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/pref_names.h"
21 #include "components/bookmarks/common/bookmark_pref_names.h"
22 #include "content/public/browser/notification_source.h"
24 namespace {
26 struct SupervisedUserSettingsPrefMappingEntry {
27 const char* settings_name;
28 const char* pref_name;
31 SupervisedUserSettingsPrefMappingEntry kSupervisedUserSettingsPrefMapping[] = {
33 supervised_users::kContentPackDefaultFilteringBehavior,
34 prefs::kDefaultSupervisedUserFilteringBehavior,
37 supervised_users::kContentPackManualBehaviorHosts,
38 prefs::kSupervisedUserManualHosts,
41 supervised_users::kContentPackManualBehaviorURLs,
42 prefs::kSupervisedUserManualURLs,
45 supervised_users::kForceSafeSearch, prefs::kForceGoogleSafeSearch,
48 supervised_users::kForceSafeSearch, prefs::kForceYouTubeSafetyMode,
51 supervised_users::kRecordHistory, prefs::kRecordHistory,
54 supervised_users::kSafeSitesEnabled, prefs::kSupervisedUserSafeSites,
57 supervised_users::kSigninAllowed, prefs::kSigninAllowed,
60 supervised_users::kUserName, prefs::kProfileName,
64 } // namespace
66 SupervisedUserPrefStore::SupervisedUserPrefStore(
67 SupervisedUserSettingsService* supervised_user_settings_service) {
68 user_settings_subscription_ = supervised_user_settings_service->Subscribe(
69 base::Bind(&SupervisedUserPrefStore::OnNewSettingsAvailable,
70 base::Unretained(this)));
72 // Should only be nullptr in unit tests
73 // TODO(peconn): Remove this notification once HostContentSettingsMap is
74 // a KeyedService.
75 if (supervised_user_settings_service->GetProfile() != nullptr){
76 unsubscriber_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
77 content::Source<Profile>(
78 supervised_user_settings_service->GetProfile()));
82 bool SupervisedUserPrefStore::GetValue(const std::string& key,
83 const base::Value** value) const {
84 // TODO(bauerb): Temporary CHECK to force a clean crash while investigating
85 // https://crbug.com/425785. Remove (or change back to DCHECK) once the bug
86 // is fixed.
87 CHECK(prefs_);
88 return prefs_->GetValue(key, value);
91 void SupervisedUserPrefStore::AddObserver(PrefStore::Observer* observer) {
92 observers_.AddObserver(observer);
95 void SupervisedUserPrefStore::RemoveObserver(PrefStore::Observer* observer) {
96 observers_.RemoveObserver(observer);
99 bool SupervisedUserPrefStore::HasObservers() const {
100 return observers_.might_have_observers();
103 bool SupervisedUserPrefStore::IsInitializationComplete() const {
104 return !!prefs_;
107 SupervisedUserPrefStore::~SupervisedUserPrefStore() {
110 void SupervisedUserPrefStore::OnNewSettingsAvailable(
111 const base::DictionaryValue* settings) {
112 scoped_ptr<PrefValueMap> old_prefs = prefs_.Pass();
113 prefs_.reset(new PrefValueMap);
114 if (settings) {
115 // Set hardcoded prefs and defaults.
116 prefs_->SetBoolean(prefs::kAllowDeletingBrowserHistory, false);
117 prefs_->SetInteger(prefs::kDefaultSupervisedUserFilteringBehavior,
118 SupervisedUserURLFilter::ALLOW);
119 prefs_->SetBoolean(prefs::kForceGoogleSafeSearch, true);
120 prefs_->SetBoolean(prefs::kForceYouTubeSafetyMode, true);
121 prefs_->SetBoolean(prefs::kHideWebStoreIcon, true);
122 prefs_->SetInteger(prefs::kIncognitoModeAvailability,
123 IncognitoModePrefs::DISABLED);
124 prefs_->SetBoolean(prefs::kRecordHistory, true);
125 prefs_->SetBoolean(prefs::kSigninAllowed, false);
127 // Copy supervised user settings to prefs.
128 for (const auto& entry : kSupervisedUserSettingsPrefMapping) {
129 const base::Value* value = NULL;
130 if (settings->GetWithoutPathExpansion(entry.settings_name, &value))
131 prefs_->SetValue(entry.pref_name, value->CreateDeepCopy());
134 // Manually set preferences that aren't direct copies of the settings value.
135 bool record_history;
136 if (settings->GetBoolean(supervised_users::kRecordHistory,
137 &record_history)) {
138 prefs_->SetBoolean(prefs::kAllowDeletingBrowserHistory, !record_history);
139 prefs_->SetInteger(prefs::kIncognitoModeAvailability,
140 record_history ? IncognitoModePrefs::DISABLED
141 : IncognitoModePrefs::ENABLED);
144 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
145 switches::kEnableSupervisedUserManagedBookmarksFolder)) {
146 // Reconstruct bookmarks from split settings.
147 prefs_->SetValue(
148 bookmarks::prefs::kSupervisedBookmarks,
149 SupervisedUserBookmarksHandler::BuildBookmarksTree(*settings).Pass());
153 if (!old_prefs) {
154 FOR_EACH_OBSERVER(Observer, observers_, OnInitializationCompleted(true));
155 return;
158 std::vector<std::string> changed_prefs;
159 prefs_->GetDifferingKeys(old_prefs.get(), &changed_prefs);
161 // Send out change notifications.
162 for (const std::string& pref : changed_prefs) {
163 FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(pref));
167 // Callback to unsubscribe from the supervised user settings service.
168 void SupervisedUserPrefStore::Observe(
169 int type,
170 const content::NotificationSource& src,
171 const content::NotificationDetails& details) {
172 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_DESTROYED, type);
173 user_settings_subscription_.reset();