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/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"
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::kSafeSitesEnabled
, prefs::kSupervisedUserSafeSites
,
55 supervised_users::kSigninAllowed
, prefs::kSigninAllowed
,
58 supervised_users::kUserName
, prefs::kProfileName
,
64 SupervisedUserPrefStore::SupervisedUserPrefStore(
65 SupervisedUserSettingsService
* supervised_user_settings_service
)
66 : weak_ptr_factory_(this) {
67 supervised_user_settings_service
->Subscribe(
68 base::Bind(&SupervisedUserPrefStore::OnNewSettingsAvailable
,
69 weak_ptr_factory_
.GetWeakPtr()));
72 bool SupervisedUserPrefStore::GetValue(const std::string
& key
,
73 const base::Value
** value
) const {
74 // TODO(bauerb): Temporary CHECK to force a clean crash while investigating
75 // https://crbug.com/425785. Remove (or change back to DCHECK) once the bug
78 return prefs_
->GetValue(key
, value
);
81 void SupervisedUserPrefStore::AddObserver(PrefStore::Observer
* observer
) {
82 observers_
.AddObserver(observer
);
85 void SupervisedUserPrefStore::RemoveObserver(PrefStore::Observer
* observer
) {
86 observers_
.RemoveObserver(observer
);
89 bool SupervisedUserPrefStore::HasObservers() const {
90 return observers_
.might_have_observers();
93 bool SupervisedUserPrefStore::IsInitializationComplete() const {
97 SupervisedUserPrefStore::~SupervisedUserPrefStore() {
100 void SupervisedUserPrefStore::OnNewSettingsAvailable(
101 const base::DictionaryValue
* settings
) {
102 scoped_ptr
<PrefValueMap
> old_prefs
= prefs_
.Pass();
103 prefs_
.reset(new PrefValueMap
);
105 // Set hardcoded prefs and defaults.
106 prefs_
->SetBoolean(prefs::kAllowDeletingBrowserHistory
, false);
107 prefs_
->SetInteger(prefs::kDefaultSupervisedUserFilteringBehavior
,
108 SupervisedUserURLFilter::ALLOW
);
109 prefs_
->SetBoolean(prefs::kForceGoogleSafeSearch
, true);
110 prefs_
->SetBoolean(prefs::kForceYouTubeSafetyMode
, true);
111 prefs_
->SetBoolean(prefs::kHideWebStoreIcon
, true);
112 prefs_
->SetInteger(prefs::kIncognitoModeAvailability
,
113 IncognitoModePrefs::DISABLED
);
114 prefs_
->SetBoolean(prefs::kRecordHistory
, true);
115 prefs_
->SetBoolean(prefs::kSigninAllowed
, false);
117 // Copy supervised user settings to prefs.
118 for (const auto& entry
: kSupervisedUserSettingsPrefMapping
) {
119 const base::Value
* value
= NULL
;
120 if (settings
->GetWithoutPathExpansion(entry
.settings_name
, &value
))
121 prefs_
->SetValue(entry
.pref_name
, value
->CreateDeepCopy());
124 // Manually set preferences that aren't direct copies of the settings value.
126 if (settings
->GetBoolean(supervised_users::kRecordHistory
,
128 prefs_
->SetBoolean(prefs::kAllowDeletingBrowserHistory
, !record_history
);
129 prefs_
->SetInteger(prefs::kIncognitoModeAvailability
,
130 record_history
? IncognitoModePrefs::DISABLED
131 : IncognitoModePrefs::ENABLED
);
134 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
135 switches::kEnableSupervisedUserManagedBookmarksFolder
)) {
136 // Reconstruct bookmarks from split settings.
138 bookmarks::prefs::kSupervisedBookmarks
,
139 SupervisedUserBookmarksHandler::BuildBookmarksTree(*settings
).Pass());
144 FOR_EACH_OBSERVER(Observer
, observers_
, OnInitializationCompleted(true));
148 std::vector
<std::string
> changed_prefs
;
149 prefs_
->GetDifferingKeys(old_prefs
.get(), &changed_prefs
);
151 // Send out change notifications.
152 for (const std::string
& pref
: changed_prefs
) {
153 FOR_EACH_OBSERVER(Observer
, observers_
, OnPrefValueChanged(pref
));