| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/site_settings_helper.h" | 5 #include "chrome/browser/ui/webui/site_settings_helper.h" |
| 6 | 6 |
| 7 #include <functional> | 7 #include <functional> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/permissions/chooser_context_base.h" | 12 #include "chrome/browser/permissions/chooser_context_base.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/usb/usb_chooser_context_factory.h" | 14 #include "chrome/browser/usb/usb_chooser_context_factory.h" |
| 15 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 16 #include "components/content_settings/core/browser/host_content_settings_map.h" | 16 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 17 #include "components/prefs/pref_service.h" | 17 #include "components/prefs/pref_service.h" |
| 18 #include "extensions/browser/extension_registry.h" |
| 18 | 19 |
| 19 namespace site_settings { | 20 namespace site_settings { |
| 20 | 21 |
| 21 const char kAppName[] = "appName"; | 22 const char kAppName[] = "appName"; |
| 22 const char kAppId[] = "appId"; | 23 const char kAppId[] = "appId"; |
| 23 const char kSetting[] = "setting"; | 24 const char kSetting[] = "setting"; |
| 24 const char kOrigin[] = "origin"; | 25 const char kOrigin[] = "origin"; |
| 26 const char kDisplayName[] = "displayName"; |
| 27 const char kOriginForFavicon[] = "originForFavicon"; |
| 25 const char kPolicyProviderId[] = "policy"; | 28 const char kPolicyProviderId[] = "policy"; |
| 26 const char kSource[] = "source"; | 29 const char kSource[] = "source"; |
| 27 const char kIncognito[] = "incognito"; | 30 const char kIncognito[] = "incognito"; |
| 28 const char kEmbeddingOrigin[] = "embeddingOrigin"; | 31 const char kEmbeddingOrigin[] = "embeddingOrigin"; |
| 29 const char kPreferencesSource[] = "preference"; | 32 const char kPreferencesSource[] = "preference"; |
| 30 const char kObject[] = "object"; | 33 const char kObject[] = "object"; |
| 31 const char kObjectName[] = "objectName"; | 34 const char kObjectName[] = "objectName"; |
| 32 | 35 |
| 33 const char kGroupTypeUsb[] = "usb-devices"; | 36 const char kGroupTypeUsb[] = "usb-devices"; |
| 34 | 37 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 exception->SetString(kAppName, app.name()); | 124 exception->SetString(kAppName, app.name()); |
| 122 exception->SetString(kAppId, app.id()); | 125 exception->SetString(kAppId, app.id()); |
| 123 exceptions->Append(std::move(exception)); | 126 exceptions->Append(std::move(exception)); |
| 124 } | 127 } |
| 125 | 128 |
| 126 // Create a DictionaryValue* that will act as a data source for a single row | 129 // Create a DictionaryValue* that will act as a data source for a single row |
| 127 // in a HostContentSettingsMap-controlled exceptions table (e.g., cookies). | 130 // in a HostContentSettingsMap-controlled exceptions table (e.g., cookies). |
| 128 std::unique_ptr<base::DictionaryValue> GetExceptionForPage( | 131 std::unique_ptr<base::DictionaryValue> GetExceptionForPage( |
| 129 const ContentSettingsPattern& pattern, | 132 const ContentSettingsPattern& pattern, |
| 130 const ContentSettingsPattern& secondary_pattern, | 133 const ContentSettingsPattern& secondary_pattern, |
| 134 const std::string& display_name, |
| 131 const ContentSetting& setting, | 135 const ContentSetting& setting, |
| 132 const std::string& provider_name, | 136 const std::string& provider_name, |
| 133 bool incognito) { | 137 bool incognito) { |
| 134 base::DictionaryValue* exception = new base::DictionaryValue(); | 138 base::DictionaryValue* exception = new base::DictionaryValue(); |
| 135 exception->SetString(kOrigin, pattern.ToString()); | 139 exception->SetString(kOrigin, pattern.ToString()); |
| 140 exception->SetString(kDisplayName, display_name); |
| 136 exception->SetString(kEmbeddingOrigin, | 141 exception->SetString(kEmbeddingOrigin, |
| 137 secondary_pattern == ContentSettingsPattern::Wildcard() ? | 142 secondary_pattern == ContentSettingsPattern::Wildcard() ? |
| 138 std::string() : | 143 std::string() : |
| 139 secondary_pattern.ToString()); | 144 secondary_pattern.ToString()); |
| 140 | 145 |
| 141 std::string setting_string = | 146 std::string setting_string = |
| 142 content_settings::ContentSettingToString(setting); | 147 content_settings::ContentSettingToString(setting); |
| 143 DCHECK(!setting_string.empty()); | 148 DCHECK(!setting_string.empty()); |
| 144 | 149 |
| 145 exception->SetString(kSetting, setting_string); | 150 exception->SetString(kSetting, setting_string); |
| 146 exception->SetString(kSource, provider_name); | 151 exception->SetString(kSource, provider_name); |
| 147 exception->SetBoolean(kIncognito, incognito); | 152 exception->SetBoolean(kIncognito, incognito); |
| 148 return base::WrapUnique(exception); | 153 return base::WrapUnique(exception); |
| 149 } | 154 } |
| 150 | 155 |
| 151 void GetExceptionsFromHostContentSettingsMap(const HostContentSettingsMap* map, | 156 std::string GetDisplayName( |
| 152 ContentSettingsType type, | 157 const ContentSettingsPattern& pattern, |
| 153 content::WebUI* web_ui, | 158 const extensions::ExtensionRegistry* extension_registry) { |
| 154 bool incognito, | 159 if (extension_registry && |
| 155 const std::string* filter, | 160 pattern.GetScheme() == ContentSettingsPattern::SCHEME_CHROMEEXTENSION) { |
| 156 base::ListValue* exceptions) { | 161 GURL url(pattern.ToString()); |
| 162 // For the extension scheme, the pattern must be a valid URL. |
| 163 DCHECK(url.is_valid()); |
| 164 const extensions::Extension* extension = |
| 165 extension_registry->GetExtensionById( |
| 166 url.host(), extensions::ExtensionRegistry::EVERYTHING); |
| 167 if (extension) |
| 168 return extension->name(); |
| 169 } |
| 170 return pattern.ToString(); |
| 171 } |
| 172 |
| 173 void GetExceptionsFromHostContentSettingsMap( |
| 174 const HostContentSettingsMap* map, |
| 175 ContentSettingsType type, |
| 176 const extensions::ExtensionRegistry* extension_registry, |
| 177 content::WebUI* web_ui, |
| 178 bool incognito, |
| 179 const std::string* filter, |
| 180 base::ListValue* exceptions) { |
| 157 ContentSettingsForOneType entries; | 181 ContentSettingsForOneType entries; |
| 158 map->GetSettingsForOneType(type, std::string(), &entries); | 182 map->GetSettingsForOneType(type, std::string(), &entries); |
| 159 // Group settings by primary_pattern. | 183 // Group settings by primary_pattern. |
| 160 AllPatternsSettings all_patterns_settings; | 184 AllPatternsSettings all_patterns_settings; |
| 161 for (ContentSettingsForOneType::iterator i = entries.begin(); | 185 for (ContentSettingsForOneType::iterator i = entries.begin(); |
| 162 i != entries.end(); ++i) { | 186 i != entries.end(); ++i) { |
| 163 // Don't add default settings. | 187 // Don't add default settings. |
| 164 if (i->primary_pattern == ContentSettingsPattern::Wildcard() && | 188 if (i->primary_pattern == ContentSettingsPattern::Wildcard() && |
| 165 i->secondary_pattern == ContentSettingsPattern::Wildcard() && | 189 i->secondary_pattern == ContentSettingsPattern::Wildcard() && |
| 166 i->source != kPreferencesSource) { | 190 i->source != kPreferencesSource) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 187 | 211 |
| 188 // |all_patterns_settings| is sorted from the lowest precedence pattern to | 212 // |all_patterns_settings| is sorted from the lowest precedence pattern to |
| 189 // the highest (see operator< in ContentSettingsPattern), so traverse it in | 213 // the highest (see operator< in ContentSettingsPattern), so traverse it in |
| 190 // reverse to show the patterns with the highest precedence (the more specific | 214 // reverse to show the patterns with the highest precedence (the more specific |
| 191 // ones) on the top. | 215 // ones) on the top. |
| 192 for (AllPatternsSettings::reverse_iterator i = all_patterns_settings.rbegin(); | 216 for (AllPatternsSettings::reverse_iterator i = all_patterns_settings.rbegin(); |
| 193 i != all_patterns_settings.rend(); | 217 i != all_patterns_settings.rend(); |
| 194 ++i) { | 218 ++i) { |
| 195 const ContentSettingsPattern& primary_pattern = i->first.first; | 219 const ContentSettingsPattern& primary_pattern = i->first.first; |
| 196 const OnePatternSettings& one_settings = i->second; | 220 const OnePatternSettings& one_settings = i->second; |
| 221 const std::string display_name = |
| 222 GetDisplayName(primary_pattern, extension_registry); |
| 197 | 223 |
| 198 // The "parent" entry either has an identical primary and secondary pattern, | 224 // The "parent" entry either has an identical primary and secondary pattern, |
| 199 // or has a wildcard secondary. The two cases are indistinguishable in the | 225 // or has a wildcard secondary. The two cases are indistinguishable in the |
| 200 // UI. | 226 // UI. |
| 201 OnePatternSettings::const_iterator parent = | 227 OnePatternSettings::const_iterator parent = |
| 202 one_settings.find(primary_pattern); | 228 one_settings.find(primary_pattern); |
| 203 if (parent == one_settings.end()) | 229 if (parent == one_settings.end()) |
| 204 parent = one_settings.find(ContentSettingsPattern::Wildcard()); | 230 parent = one_settings.find(ContentSettingsPattern::Wildcard()); |
| 205 | 231 |
| 206 const std::string& source = i->first.second; | 232 const std::string& source = i->first.second; |
| 207 auto& this_provider_exceptions = all_provider_exceptions | 233 auto& this_provider_exceptions = all_provider_exceptions |
| 208 [HostContentSettingsMap::GetProviderTypeFromSource(source)]; | 234 [HostContentSettingsMap::GetProviderTypeFromSource(source)]; |
| 209 | 235 |
| 210 // Add the "parent" entry for the non-embedded setting. | 236 // Add the "parent" entry for the non-embedded setting. |
| 211 ContentSetting parent_setting = | 237 ContentSetting parent_setting = |
| 212 parent == one_settings.end() ? CONTENT_SETTING_DEFAULT : parent->second; | 238 parent == one_settings.end() ? CONTENT_SETTING_DEFAULT : parent->second; |
| 213 const ContentSettingsPattern& secondary_pattern = | 239 const ContentSettingsPattern& secondary_pattern = |
| 214 parent == one_settings.end() ? primary_pattern : parent->first; | 240 parent == one_settings.end() ? primary_pattern : parent->first; |
| 215 this_provider_exceptions.push_back(GetExceptionForPage( | 241 this_provider_exceptions.push_back( |
| 216 primary_pattern, secondary_pattern, parent_setting, source, incognito)); | 242 GetExceptionForPage(primary_pattern, secondary_pattern, display_name, |
| 243 parent_setting, source, incognito)); |
| 217 | 244 |
| 218 // Add the "children" for any embedded settings. | 245 // Add the "children" for any embedded settings. |
| 219 for (OnePatternSettings::const_iterator j = one_settings.begin(); | 246 for (OnePatternSettings::const_iterator j = one_settings.begin(); |
| 220 j != one_settings.end(); ++j) { | 247 j != one_settings.end(); ++j) { |
| 221 // Skip the non-embedded setting which we already added above. | 248 // Skip the non-embedded setting which we already added above. |
| 222 if (j == parent) | 249 if (j == parent) |
| 223 continue; | 250 continue; |
| 224 | 251 |
| 225 ContentSetting content_setting = j->second; | 252 ContentSetting content_setting = j->second; |
| 226 this_provider_exceptions.push_back(GetExceptionForPage( | 253 this_provider_exceptions.push_back( |
| 227 primary_pattern, j->first, content_setting, source, incognito)); | 254 GetExceptionForPage(primary_pattern, j->first, display_name, |
| 255 content_setting, source, incognito)); |
| 228 } | 256 } |
| 229 } | 257 } |
| 230 | 258 |
| 231 // For camera and microphone, we do not have policy exceptions, but we do have | 259 // For camera and microphone, we do not have policy exceptions, but we do have |
| 232 // the policy-set allowed URLs, which should be displayed in the same manner. | 260 // the policy-set allowed URLs, which should be displayed in the same manner. |
| 233 if (type == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC || | 261 if (type == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC || |
| 234 type == CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA) { | 262 type == CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA) { |
| 235 auto& policy_exceptions = all_provider_exceptions | 263 auto& policy_exceptions = all_provider_exceptions |
| 236 [HostContentSettingsMap::GetProviderTypeFromSource(kPolicyProviderId)]; | 264 [HostContentSettingsMap::GetProviderTypeFromSource(kPolicyProviderId)]; |
| 237 DCHECK(policy_exceptions.empty()); | 265 DCHECK(policy_exceptions.empty()); |
| 238 GetPolicyAllowedUrls(type, &policy_exceptions, web_ui, incognito); | 266 GetPolicyAllowedUrls(type, &policy_exceptions, extension_registry, web_ui, |
| 267 incognito); |
| 239 } | 268 } |
| 240 | 269 |
| 241 for (auto& one_provider_exceptions : all_provider_exceptions) { | 270 for (auto& one_provider_exceptions : all_provider_exceptions) { |
| 242 for (auto& exception : one_provider_exceptions) | 271 for (auto& exception : one_provider_exceptions) |
| 243 exceptions->Append(std::move(exception)); | 272 exceptions->Append(std::move(exception)); |
| 244 } | 273 } |
| 245 } | 274 } |
| 246 | 275 |
| 247 void GetPolicyAllowedUrls( | 276 void GetPolicyAllowedUrls( |
| 248 ContentSettingsType type, | 277 ContentSettingsType type, |
| 249 std::vector<std::unique_ptr<base::DictionaryValue>>* exceptions, | 278 std::vector<std::unique_ptr<base::DictionaryValue>>* exceptions, |
| 279 const extensions::ExtensionRegistry* extension_registry, |
| 250 content::WebUI* web_ui, | 280 content::WebUI* web_ui, |
| 251 bool incognito) { | 281 bool incognito) { |
| 252 DCHECK(type == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC || | 282 DCHECK(type == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC || |
| 253 type == CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA); | 283 type == CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA); |
| 254 | 284 |
| 255 PrefService* prefs = Profile::FromWebUI(web_ui)->GetPrefs(); | 285 PrefService* prefs = Profile::FromWebUI(web_ui)->GetPrefs(); |
| 256 const base::ListValue* policy_urls = prefs->GetList( | 286 const base::ListValue* policy_urls = prefs->GetList( |
| 257 type == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC | 287 type == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC |
| 258 ? prefs::kAudioCaptureAllowedUrls | 288 ? prefs::kAudioCaptureAllowedUrls |
| 259 : prefs::kVideoCaptureAllowedUrls); | 289 : prefs::kVideoCaptureAllowedUrls); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 272 | 302 |
| 273 patterns.push_back(pattern); | 303 patterns.push_back(pattern); |
| 274 } | 304 } |
| 275 | 305 |
| 276 // The patterns are shown in the UI in a reverse order defined by | 306 // The patterns are shown in the UI in a reverse order defined by |
| 277 // |ContentSettingsPattern::operator<|. | 307 // |ContentSettingsPattern::operator<|. |
| 278 std::sort( | 308 std::sort( |
| 279 patterns.begin(), patterns.end(), std::greater<ContentSettingsPattern>()); | 309 patterns.begin(), patterns.end(), std::greater<ContentSettingsPattern>()); |
| 280 | 310 |
| 281 for (const ContentSettingsPattern& pattern : patterns) { | 311 for (const ContentSettingsPattern& pattern : patterns) { |
| 282 exceptions->push_back(GetExceptionForPage(pattern, ContentSettingsPattern(), | 312 std::string display_name = GetDisplayName(pattern, extension_registry); |
| 283 CONTENT_SETTING_ALLOW, | 313 exceptions->push_back(GetExceptionForPage( |
| 284 kPolicyProviderId, incognito)); | 314 pattern, ContentSettingsPattern(), display_name, CONTENT_SETTING_ALLOW, |
| 315 kPolicyProviderId, incognito)); |
| 285 } | 316 } |
| 286 } | 317 } |
| 287 | 318 |
| 288 const ChooserTypeNameEntry* ChooserTypeFromGroupName(const std::string& name) { | 319 const ChooserTypeNameEntry* ChooserTypeFromGroupName(const std::string& name) { |
| 289 for (const auto& chooser_type : kChooserTypeGroupNames) { | 320 for (const auto& chooser_type : kChooserTypeGroupNames) { |
| 290 if (chooser_type.name == name) | 321 if (chooser_type.name == name) |
| 291 return &chooser_type; | 322 return &chooser_type; |
| 292 } | 323 } |
| 293 return nullptr; | 324 return nullptr; |
| 294 } | 325 } |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 } | 438 } |
| 408 } | 439 } |
| 409 | 440 |
| 410 for (auto& one_provider_exceptions : all_provider_exceptions) { | 441 for (auto& one_provider_exceptions : all_provider_exceptions) { |
| 411 for (auto& exception : one_provider_exceptions) | 442 for (auto& exception : one_provider_exceptions) |
| 412 exceptions->Append(std::move(exception)); | 443 exceptions->Append(std::move(exception)); |
| 413 } | 444 } |
| 414 } | 445 } |
| 415 | 446 |
| 416 } // namespace site_settings | 447 } // namespace site_settings |
| OLD | NEW |