OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2010 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. | |
4 | |
5 #include "chrome/browser/content_settings/policy_content_settings_provider.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "chrome/browser/browser_thread.h" | |
9 #include "chrome/browser/content_settings/content_settings_details.h" | |
10 #include "chrome/browser/content_settings/content_settings_pattern.h" | |
11 #include "chrome/browser/prefs/pref_service.h" | |
12 #include "chrome/browser/prefs/scoped_pref_update.h" | |
13 #include "chrome/browser/profiles/profile.h" | |
14 #include "chrome/common/chrome_switches.h" | |
15 #include "chrome/common/notification_details.h" | |
16 #include "chrome/common/notification_service.h" | |
17 #include "chrome/common/notification_source.h" | |
18 #include "chrome/common/pref_names.h" | |
19 | |
20 namespace { | |
21 | |
22 // Base pref path of the prefs that contain the managed default content | |
23 // settings values. | |
24 const std::string kManagedSettings = | |
25 "profile.managed_default_content_settings"; | |
26 | |
27 // The preferences used to manage ContentSettingsTypes. | |
28 const char* kPrefToManageType[CONTENT_SETTINGS_NUM_TYPES] = { | |
29 prefs::kManagedDefaultCookiesSetting, | |
30 prefs::kManagedDefaultImagesSetting, | |
31 prefs::kManagedDefaultJavaScriptSetting, | |
32 prefs::kManagedDefaultPluginsSetting, | |
33 prefs::kManagedDefaultPopupsSetting, | |
34 NULL, // Not used for Geolocation | |
35 NULL, // Not used for Notifications | |
36 }; | |
37 | |
38 } // namespace | |
39 | |
40 PolicyContentSettingsProvider::PolicyContentSettingsProvider(Profile* profile) | |
41 : profile_(profile), | |
42 is_off_the_record_(profile_->IsOffTheRecord()) { | |
43 PrefService* prefs = profile->GetPrefs(); | |
44 | |
45 // Read global defaults. | |
46 DCHECK_EQ(arraysize(kPrefToManageType), | |
47 static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES)); | |
48 ReadDefaultSettingsFromPrefs(prefs, &managed_default_content_settings_); | |
49 | |
50 pref_change_registrar_.Init(prefs); | |
51 // The following preferences are only used to indicate if a | |
52 // default-content-setting is managed and to hold the managed default-setting | |
53 // value. If the value for any of the following perferences is set then the | |
54 // corresponding default-content-setting is managed. These preferences exist | |
55 // in parallel to the preference default-content-settings. If a | |
56 // default-content-settings-type is managed any user defined excpetions | |
57 // (patterns) for this type are ignored. | |
58 pref_change_registrar_.Add(prefs::kManagedDefaultCookiesSetting, this); | |
59 pref_change_registrar_.Add(prefs::kManagedDefaultImagesSetting, this); | |
60 pref_change_registrar_.Add(prefs::kManagedDefaultJavaScriptSetting, this); | |
61 pref_change_registrar_.Add(prefs::kManagedDefaultPluginsSetting, this); | |
62 pref_change_registrar_.Add(prefs::kManagedDefaultPopupsSetting, this); | |
63 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, | |
64 Source<Profile>(profile_)); | |
65 } | |
66 | |
67 PolicyContentSettingsProvider::~PolicyContentSettingsProvider() { | |
68 UnregisterObservers(); | |
69 } | |
70 | |
71 bool PolicyContentSettingsProvider::CanProvideDefaultSetting( | |
72 ContentSettingsType content_type) const { | |
73 if (managed_default_content_settings_.settings[content_type] != | |
74 CONTENT_SETTING_DEFAULT) { | |
75 return true; | |
76 } else { | |
77 return false; | |
78 } | |
79 } | |
80 | |
81 ContentSetting PolicyContentSettingsProvider::ProvideDefaultSetting( | |
82 ContentSettingsType content_type) const { | |
83 AutoLock auto_lock(lock_); | |
84 return managed_default_content_settings_.settings[content_type]; | |
85 } | |
86 | |
87 void PolicyContentSettingsProvider::UpdateDefaultSetting( | |
88 ContentSettingsType content_type, | |
89 ContentSetting setting) { | |
90 } | |
91 | |
92 bool PolicyContentSettingsProvider::DefaultSettingIsManaged( | |
93 ContentSettingsType content_type) const { | |
94 if (managed_default_content_settings_.settings[content_type] != | |
95 CONTENT_SETTING_DEFAULT) { | |
96 return true; | |
97 } else { | |
98 return false; | |
99 } | |
100 } | |
101 | |
102 void PolicyContentSettingsProvider::ResetToDefaults() { | |
103 } | |
104 | |
105 void PolicyContentSettingsProvider::Observe(NotificationType type, | |
106 const NotificationSource& source, | |
107 const NotificationDetails& details) { | |
108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
109 | |
110 if (NotificationType::PREF_CHANGED == type) { | |
111 std::string* name = Details<std::string>(details).ptr(); | |
gfeher
2010/12/08 13:59:17
Here and everywhere else: please test that the sou
jochen (gone - plz use gerrit)
2010/12/08 14:54:01
If the provider is used for the OTR profile, it'll
gfeher
2010/12/09 10:29:53
This raises an interesting question. When you regi
jochen (gone - plz use gerrit)
2010/12/09 13:18:53
Ok, I've played around with this.
For the PROFILE
| |
112 if (prefs::kManagedDefaultCookiesSetting == *name) { | |
113 UpdateManagedDefaultSetting(profile_->GetPrefs(), | |
114 CONTENT_SETTINGS_TYPE_COOKIES, | |
115 &managed_default_content_settings_); | |
116 } else if (prefs::kManagedDefaultImagesSetting == *name) { | |
117 UpdateManagedDefaultSetting(profile_->GetPrefs(), | |
118 CONTENT_SETTINGS_TYPE_IMAGES, | |
119 &managed_default_content_settings_); | |
120 } else if (prefs::kManagedDefaultJavaScriptSetting == *name) { | |
121 UpdateManagedDefaultSetting(profile_->GetPrefs(), | |
122 CONTENT_SETTINGS_TYPE_JAVASCRIPT, | |
123 &managed_default_content_settings_); | |
124 } else if (prefs::kManagedDefaultPluginsSetting == *name) { | |
125 UpdateManagedDefaultSetting(profile_->GetPrefs(), | |
126 CONTENT_SETTINGS_TYPE_PLUGINS, | |
127 &managed_default_content_settings_); | |
128 } else if (prefs::kManagedDefaultPopupsSetting == *name) { | |
129 UpdateManagedDefaultSetting(profile_->GetPrefs(), | |
130 CONTENT_SETTINGS_TYPE_POPUPS, | |
131 &managed_default_content_settings_); | |
132 } else { | |
133 NOTREACHED() << "Unexpected preference observed"; | |
134 return; | |
135 } | |
136 | |
137 if (!is_off_the_record_) { | |
138 NotifyObservers(ContentSettingsDetails( | |
139 ContentSettingsPattern(), CONTENT_SETTINGS_TYPE_DEFAULT, "")); | |
140 } | |
141 } else if (NotificationType::PROFILE_DESTROYED == type) { | |
142 UnregisterObservers(); | |
143 } else { | |
144 NOTREACHED() << "Unexpected notification"; | |
145 } | |
146 } | |
147 | |
148 void PolicyContentSettingsProvider::UnregisterObservers() { | |
149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
150 if (!profile_) | |
151 return; | |
152 pref_change_registrar_.RemoveAll(); | |
153 notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED, | |
154 Source<Profile>(profile_)); | |
155 profile_ = NULL; | |
156 } | |
157 | |
158 | |
159 void PolicyContentSettingsProvider::NotifyObservers( | |
160 const ContentSettingsDetails& details) { | |
161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
162 if (profile_ == NULL) | |
163 return; | |
164 NotificationService::current()->Notify( | |
165 NotificationType::CONTENT_SETTINGS_CHANGED, | |
166 Source<HostContentSettingsMap>(profile_->GetHostContentSettingsMap()), | |
167 Details<const ContentSettingsDetails>(&details)); | |
168 } | |
169 | |
170 void PolicyContentSettingsProvider::ReadDefaultSettingsFromPrefs( | |
171 const PrefService* prefs, ContentSettings* settings) { | |
172 for (size_t type = 0; type < arraysize(kPrefToManageType); ++type) { | |
173 if (kPrefToManageType[type] == NULL) { | |
174 // TODO(markusheintz): Handle Geolocation and notification separately. | |
175 continue; | |
176 } | |
177 UpdateManagedDefaultSetting(prefs, ContentSettingsType(type), settings); | |
178 } | |
179 } | |
180 | |
181 void PolicyContentSettingsProvider::UpdateManagedDefaultSetting( | |
182 const PrefService* prefs, | |
183 ContentSettingsType type, | |
184 ContentSettings* settings) { | |
185 // If a pref to manage a default-content-setting was not set (NOTICE: | |
186 // "HasPrefPath" returns false if no value was set for a registered pref) then | |
187 // the default value of the preference is used. The default value of a | |
188 // preference to manage a default-content-settings is | |
189 // CONTENT_SETTING_DEFAULT. This indicates that no managed value is set. If a | |
190 // pref was set, than it MUST be managed. | |
191 DCHECK(!prefs->HasPrefPath(kPrefToManageType[type]) || | |
192 prefs->IsManagedPreference(kPrefToManageType[type])); | |
193 AutoLock auto_lock(lock_); | |
194 settings->settings[type] = IntToContentSetting( | |
195 prefs->GetInteger(kPrefToManageType[type])); | |
196 } | |
197 | |
198 // static | |
199 void PolicyContentSettingsProvider::RegisterUserPrefs(PrefService* prefs) { | |
200 // Preferences for default content setting policies. A policy is not set of | |
201 // the corresponding preferences below is set to CONTENT_SETTING_DEFAULT. | |
202 prefs->RegisterIntegerPref(prefs::kManagedDefaultCookiesSetting, | |
203 CONTENT_SETTING_DEFAULT); | |
204 prefs->RegisterIntegerPref(prefs::kManagedDefaultImagesSetting, | |
205 CONTENT_SETTING_DEFAULT); | |
206 prefs->RegisterIntegerPref(prefs::kManagedDefaultJavaScriptSetting, | |
207 CONTENT_SETTING_DEFAULT); | |
208 prefs->RegisterIntegerPref(prefs::kManagedDefaultPluginsSetting, | |
209 CONTENT_SETTING_DEFAULT); | |
210 prefs->RegisterIntegerPref(prefs::kManagedDefaultPopupsSetting, | |
211 CONTENT_SETTING_DEFAULT); | |
212 } | |
OLD | NEW |