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::kSigninAllowed
, prefs::kSigninAllowed
,
55 supervised_users::kUserName
, prefs::kProfileName
,
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
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 {
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
);
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.
123 if (settings
->GetBoolean(supervised_users::kRecordHistory
,
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.
135 bookmarks::prefs::kSupervisedBookmarks
,
136 SupervisedUserBookmarksHandler::BuildBookmarksTree(*settings
)
142 FOR_EACH_OBSERVER(Observer
, observers_
, OnInitializationCompleted(true));
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
));