1 // Copyright (c) 2015 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/content_settings/content_settings_supervised_provider.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/supervised_user/supervised_user_constants.h"
12 #include "chrome/browser/supervised_user/supervised_user_settings_service.h"
13 #include "content/public/browser/notification_source.h"
17 struct ContentSettingsFromSupervisedSettingsEntry
{
18 const char* setting_name
;
19 ContentSettingsType content_type
;
22 const ContentSettingsFromSupervisedSettingsEntry
23 kContentSettingsFromSupervisedSettingsMap
[] = {
25 supervised_users::kGeolocationDisabled
,
26 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
28 supervised_users::kCameraMicDisabled
,
29 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
,
31 supervised_users::kCameraMicDisabled
,
32 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
,
38 namespace content_settings
{
40 SupervisedProvider::SupervisedProvider(
41 SupervisedUserSettingsService
* supervised_user_settings_service
)
42 : unsubscriber_registrar_(new content::NotificationRegistrar()) {
44 user_settings_subscription_
= supervised_user_settings_service
->Subscribe(
46 &content_settings::SupervisedProvider::OnSupervisedSettingsAvailable
,
47 base::Unretained(this)));
49 // Should only be nullptr in unit tests
50 // TODO(peconn): Remove this notification once HostContentSettingsMap is
52 if (supervised_user_settings_service
->GetProfile() != nullptr){
53 unsubscriber_registrar_
->Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED
,
54 content::Source
<Profile
>(
55 supervised_user_settings_service
->GetProfile()));
59 SupervisedProvider::~SupervisedProvider() {
62 RuleIterator
* SupervisedProvider::GetRuleIterator(
63 ContentSettingsType content_type
,
64 const ResourceIdentifier
& resource_identifier
,
65 bool incognito
) const {
66 scoped_ptr
<base::AutoLock
> auto_lock(new base::AutoLock(lock_
));
67 return value_map_
.GetRuleIterator(content_type
, resource_identifier
,
71 void SupervisedProvider::OnSupervisedSettingsAvailable(
72 const base::DictionaryValue
* settings
) {
73 std::vector
<ContentSettingsType
> to_notify
;
74 // Entering locked scope to update content settings.
76 base::AutoLock
auto_lock(lock_
);
77 for (const auto& entry
: kContentSettingsFromSupervisedSettingsMap
) {
78 bool new_value
= false;
79 if (settings
&& settings
->HasKey(entry
.setting_name
)) {
80 bool is_bool
= settings
->GetBoolean(entry
.setting_name
, &new_value
);
83 bool old_value
= !value_map_
.IsContentSettingEnabled(entry
.content_type
);
84 if (new_value
!= old_value
) {
85 to_notify
.push_back(entry
.content_type
);
86 value_map_
.SetContentSettingDisabled(entry
.content_type
, new_value
);
90 for (const auto& notification
: to_notify
) {
91 NotifyObservers(ContentSettingsPattern(), ContentSettingsPattern(),
92 notification
, std::string());
96 // Since the SupervisedProvider is a read only content settings provider, all
97 // methods of the ProviderInterface that set or delete any settings do nothing.
98 bool SupervisedProvider::SetWebsiteSetting(
99 const ContentSettingsPattern
& primary_pattern
,
100 const ContentSettingsPattern
& secondary_pattern
,
101 ContentSettingsType content_type
,
102 const ResourceIdentifier
& resource_identifier
,
103 base::Value
* value
) {
107 void SupervisedProvider::ClearAllContentSettingsRules(
108 ContentSettingsType content_type
) {
111 void SupervisedProvider::ShutdownOnUIThread() {
112 DCHECK(CalledOnValidThread());
113 RemoveAllObservers();
114 user_settings_subscription_
.reset();
115 unsubscriber_registrar_
.reset();
118 // Callback to unsubscribe from the supervised user settings service.
119 void SupervisedProvider::Observe(
121 const content::NotificationSource
& src
,
122 const content::NotificationDetails
& details
) {
123 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_DESTROYED
, type
);
124 user_settings_subscription_
.reset();
127 } // namespace content_settings