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"
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"
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
,
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 once SupervisedUserPrefStore is (partially at
74 // least) a KeyedService. The user_settings_subscription_ must be reset or
75 // destroyed before the SupervisedUserSettingsService is.
76 if (supervised_user_settings_service
->GetProfile() != nullptr){
77 unsubscriber_registrar_
.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED
,
78 content::Source
<Profile
>(
79 supervised_user_settings_service
->GetProfile()));
83 bool SupervisedUserPrefStore::GetValue(const std::string
& key
,
84 const base::Value
** value
) const {
85 // TODO(bauerb): Temporary CHECK to force a clean crash while investigating
86 // https://crbug.com/425785. Remove (or change back to DCHECK) once the bug
89 return prefs_
->GetValue(key
, value
);
92 void SupervisedUserPrefStore::AddObserver(PrefStore::Observer
* observer
) {
93 observers_
.AddObserver(observer
);
96 void SupervisedUserPrefStore::RemoveObserver(PrefStore::Observer
* observer
) {
97 observers_
.RemoveObserver(observer
);
100 bool SupervisedUserPrefStore::HasObservers() const {
101 return observers_
.might_have_observers();
104 bool SupervisedUserPrefStore::IsInitializationComplete() const {
108 SupervisedUserPrefStore::~SupervisedUserPrefStore() {
111 void SupervisedUserPrefStore::OnNewSettingsAvailable(
112 const base::DictionaryValue
* settings
) {
113 scoped_ptr
<PrefValueMap
> old_prefs
= prefs_
.Pass();
114 prefs_
.reset(new PrefValueMap
);
116 // Set hardcoded prefs and defaults.
117 prefs_
->SetBoolean(prefs::kAllowDeletingBrowserHistory
, false);
118 prefs_
->SetInteger(prefs::kDefaultSupervisedUserFilteringBehavior
,
119 SupervisedUserURLFilter::ALLOW
);
120 prefs_
->SetBoolean(prefs::kForceGoogleSafeSearch
, true);
121 prefs_
->SetBoolean(prefs::kForceYouTubeSafetyMode
, true);
122 prefs_
->SetBoolean(prefs::kHideWebStoreIcon
, true);
123 prefs_
->SetInteger(prefs::kIncognitoModeAvailability
,
124 IncognitoModePrefs::DISABLED
);
125 prefs_
->SetBoolean(prefs::kRecordHistory
, true);
126 prefs_
->SetBoolean(prefs::kSigninAllowed
, false);
128 // Copy supervised user settings to prefs.
129 for (const auto& entry
: kSupervisedUserSettingsPrefMapping
) {
130 const base::Value
* value
= NULL
;
131 if (settings
->GetWithoutPathExpansion(entry
.settings_name
, &value
))
132 prefs_
->SetValue(entry
.pref_name
, value
->CreateDeepCopy());
135 // Manually set preferences that aren't direct copies of the settings value.
137 if (settings
->GetBoolean(supervised_users::kRecordHistory
,
139 prefs_
->SetBoolean(prefs::kAllowDeletingBrowserHistory
, !record_history
);
140 prefs_
->SetInteger(prefs::kIncognitoModeAvailability
,
141 record_history
? IncognitoModePrefs::DISABLED
142 : IncognitoModePrefs::ENABLED
);
145 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
146 switches::kEnableSupervisedUserManagedBookmarksFolder
)) {
147 // Reconstruct bookmarks from split settings.
149 bookmarks::prefs::kSupervisedBookmarks
,
150 SupervisedUserBookmarksHandler::BuildBookmarksTree(*settings
).Pass());
155 FOR_EACH_OBSERVER(Observer
, observers_
, OnInitializationCompleted(true));
159 std::vector
<std::string
> changed_prefs
;
160 prefs_
->GetDifferingKeys(old_prefs
.get(), &changed_prefs
);
162 // Send out change notifications.
163 for (const std::string
& pref
: changed_prefs
) {
164 FOR_EACH_OBSERVER(Observer
, observers_
, OnPrefValueChanged(pref
));
168 // Callback to unsubscribe from the supervised user settings service.
169 void SupervisedUserPrefStore::Observe(
171 const content::NotificationSource
& src
,
172 const content::NotificationDetails
& details
) {
173 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_DESTROYED
, type
);
174 user_settings_subscription_
.reset();