Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / supervised_user / supervised_user_pref_store.cc
blob7eba2567405bef2000d036e2e42c6c5b087cfbdb
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/prefs/incognito_mode_prefs.h"
14 #include "chrome/browser/supervised_user/supervised_user_bookmarks_handler.h"
15 #include "chrome/browser/supervised_user/supervised_user_constants.h"
16 #include "chrome/browser/supervised_user/supervised_user_settings_service.h"
17 #include "chrome/browser/supervised_user/supervised_user_url_filter.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/pref_names.h"
20 #include "components/bookmarks/common/bookmark_pref_names.h"
22 namespace {
24 struct SupervisedUserSettingsPrefMappingEntry {
25 const char* settings_name;
26 const char* pref_name;
29 SupervisedUserSettingsPrefMappingEntry kSupervisedUserSettingsPrefMapping[] = {
31 supervised_users::kContentPackDefaultFilteringBehavior,
32 prefs::kDefaultSupervisedUserFilteringBehavior,
35 supervised_users::kContentPackManualBehaviorHosts,
36 prefs::kSupervisedUserManualHosts,
39 supervised_users::kContentPackManualBehaviorURLs,
40 prefs::kSupervisedUserManualURLs,
43 supervised_users::kForceSafeSearch, prefs::kForceGoogleSafeSearch,
46 supervised_users::kForceSafeSearch, prefs::kForceYouTubeSafetyMode,
49 supervised_users::kRecordHistory, prefs::kRecordHistory,
52 supervised_users::kSigninAllowed, prefs::kSigninAllowed,
55 supervised_users::kUserName, prefs::kProfileName,
59 } // namespace
61 SupervisedUserPrefStore::SupervisedUserPrefStore(
62 SupervisedUserSettingsService* supervised_user_settings_service)
63 : weak_ptr_factory_(this) {
64 supervised_user_settings_service->Subscribe(
65 base::Bind(&SupervisedUserPrefStore::OnNewSettingsAvailable,
66 weak_ptr_factory_.GetWeakPtr()));
69 bool SupervisedUserPrefStore::GetValue(const std::string& key,
70 const base::Value** value) const {
71 // TODO(bauerb): Temporary CHECK to force a clean crash while investigating
72 // https://crbug.com/425785. Remove (or change back to DCHECK) once the bug
73 // is fixed.
74 CHECK(prefs_);
75 return prefs_->GetValue(key, value);
78 void SupervisedUserPrefStore::AddObserver(PrefStore::Observer* observer) {
79 observers_.AddObserver(observer);
82 void SupervisedUserPrefStore::RemoveObserver(PrefStore::Observer* observer) {
83 observers_.RemoveObserver(observer);
86 bool SupervisedUserPrefStore::HasObservers() const {
87 return observers_.might_have_observers();
90 bool SupervisedUserPrefStore::IsInitializationComplete() const {
91 return !!prefs_;
94 SupervisedUserPrefStore::~SupervisedUserPrefStore() {
97 void SupervisedUserPrefStore::OnNewSettingsAvailable(
98 const base::DictionaryValue* settings) {
99 scoped_ptr<PrefValueMap> old_prefs = prefs_.Pass();
100 prefs_.reset(new PrefValueMap);
101 if (settings) {
102 // Set hardcoded prefs and defaults.
103 prefs_->SetBoolean(prefs::kAllowDeletingBrowserHistory, false);
104 prefs_->SetInteger(prefs::kDefaultSupervisedUserFilteringBehavior,
105 SupervisedUserURLFilter::ALLOW);
106 prefs_->SetBoolean(prefs::kForceGoogleSafeSearch, true);
107 prefs_->SetBoolean(prefs::kForceYouTubeSafetyMode, true);
108 prefs_->SetBoolean(prefs::kHideWebStoreIcon, true);
109 prefs_->SetInteger(prefs::kIncognitoModeAvailability,
110 IncognitoModePrefs::DISABLED);
111 prefs_->SetBoolean(prefs::kRecordHistory, true);
112 prefs_->SetBoolean(prefs::kSigninAllowed, false);
114 // Copy supervised user settings to prefs.
115 for (const auto& entry : kSupervisedUserSettingsPrefMapping) {
116 const base::Value* value = NULL;
117 if (settings->GetWithoutPathExpansion(entry.settings_name, &value))
118 prefs_->SetValue(entry.pref_name, value->DeepCopy());
121 // Manually set preferences that aren't direct copies of the settings value.
122 bool record_history;
123 if (settings->GetBoolean(supervised_users::kRecordHistory,
124 &record_history)) {
125 prefs_->SetBoolean(prefs::kAllowDeletingBrowserHistory, !record_history);
126 prefs_->SetInteger(prefs::kIncognitoModeAvailability,
127 record_history ? IncognitoModePrefs::DISABLED
128 : IncognitoModePrefs::ENABLED);
131 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
132 switches::kEnableSupervisedUserManagedBookmarksFolder)) {
133 // Reconstruct bookmarks from split settings.
134 prefs_->SetValue(
135 bookmarks::prefs::kSupervisedBookmarks,
136 SupervisedUserBookmarksHandler::BuildBookmarksTree(*settings)
137 .release());
141 if (!old_prefs) {
142 FOR_EACH_OBSERVER(Observer, observers_, OnInitializationCompleted(true));
143 return;
146 std::vector<std::string> changed_prefs;
147 prefs_->GetDifferingKeys(old_prefs.get(), &changed_prefs);
149 // Send out change notifications.
150 for (const std::string& pref : changed_prefs) {
151 FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(pref));