Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / managed_mode / supervised_user_pref_store.cc
blob4d21f75b3c5182684f95f47b72a1d0553e0a9d20
1 // Copyright 2013 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/managed_mode/supervised_user_pref_store.h"
7 #include "base/bind.h"
8 #include "base/prefs/pref_value_map.h"
9 #include "base/values.h"
10 #include "chrome/browser/managed_mode/managed_mode_url_filter.h"
11 #include "chrome/browser/managed_mode/managed_user_constants.h"
12 #include "chrome/browser/managed_mode/managed_user_settings_service.h"
13 #include "chrome/browser/prefs/incognito_mode_prefs.h"
14 #include "chrome/common/pref_names.h"
16 using base::FundamentalValue;
18 namespace {
20 struct ManagedUserSettingsPrefMappingEntry {
21 const char* settings_name;
22 const char* pref_name;
25 ManagedUserSettingsPrefMappingEntry kManagedUserSettingsPrefMapping[] = {
27 managed_users::kContentPackDefaultFilteringBehavior,
28 prefs::kDefaultManagedModeFilteringBehavior,
31 managed_users::kContentPackManualBehaviorHosts,
32 prefs::kManagedModeManualHosts,
35 managed_users::kContentPackManualBehaviorURLs,
36 prefs::kManagedModeManualURLs,
39 managed_users::kForceSafeSearch,
40 prefs::kForceSafeSearch,
43 managed_users::kUserName,
44 prefs::kProfileName,
48 } // namespace
50 SupervisedUserPrefStore::SupervisedUserPrefStore(
51 ManagedUserSettingsService* managed_user_settings_service)
52 : weak_ptr_factory_(this) {
53 managed_user_settings_service->Subscribe(
54 base::Bind(&SupervisedUserPrefStore::OnNewSettingsAvailable,
55 weak_ptr_factory_.GetWeakPtr()));
58 bool SupervisedUserPrefStore::GetValue(const std::string& key,
59 const base::Value** value) const {
60 DCHECK(prefs_);
61 return prefs_->GetValue(key, value);
64 void SupervisedUserPrefStore::AddObserver(PrefStore::Observer* observer) {
65 observers_.AddObserver(observer);
68 void SupervisedUserPrefStore::RemoveObserver(PrefStore::Observer* observer) {
69 observers_.RemoveObserver(observer);
72 bool SupervisedUserPrefStore::HasObservers() const {
73 return observers_.might_have_observers();
76 bool SupervisedUserPrefStore::IsInitializationComplete() const {
77 return !!prefs_;
80 SupervisedUserPrefStore::~SupervisedUserPrefStore() {
83 void SupervisedUserPrefStore::OnNewSettingsAvailable(
84 const base::DictionaryValue* settings) {
85 scoped_ptr<PrefValueMap> old_prefs = prefs_.Pass();
86 prefs_.reset(new PrefValueMap);
87 if (settings) {
88 // Set hardcoded prefs.
89 prefs_->SetValue(prefs::kAllowDeletingBrowserHistory,
90 new FundamentalValue(false));
91 prefs_->SetValue(prefs::kDefaultManagedModeFilteringBehavior,
92 new FundamentalValue(ManagedModeURLFilter::ALLOW));
93 prefs_->SetValue(prefs::kForceSafeSearch, new FundamentalValue(true));
94 prefs_->SetValue(prefs::kHideWebStoreIcon, new FundamentalValue(true));
95 prefs_->SetValue(prefs::kIncognitoModeAvailability,
96 new FundamentalValue(IncognitoModePrefs::DISABLED));
97 prefs_->SetValue(prefs::kSigninAllowed, new FundamentalValue(false));
99 // Copy managed user settings to prefs.
100 for (size_t i = 0; i < arraysize(kManagedUserSettingsPrefMapping); ++i) {
101 const ManagedUserSettingsPrefMappingEntry& entry =
102 kManagedUserSettingsPrefMapping[i];
103 const base::Value* value = NULL;
104 if (settings->GetWithoutPathExpansion(entry.settings_name, &value))
105 prefs_->SetValue(entry.pref_name, value->DeepCopy());
109 if (!old_prefs) {
110 FOR_EACH_OBSERVER(Observer, observers_, OnInitializationCompleted(true));
111 return;
114 std::vector<std::string> changed_prefs;
115 prefs_->GetDifferingKeys(old_prefs.get(), &changed_prefs);
117 // Send out change notifications.
118 for (std::vector<std::string>::const_iterator pref(changed_prefs.begin());
119 pref != changed_prefs.end();
120 ++pref) {
121 FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(*pref));