Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/content_settings/core/browser/content_settings_pref.h" | 5 #include "components/content_settings/core/browser/content_settings_pref.h" |
| 6 | 6 |
| 7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
| 12 #include "base/time/clock.h" | 12 #include "base/time/clock.h" |
| 13 #include "components/content_settings/core/browser/content_settings_info.h" | 13 #include "components/content_settings/core/browser/content_settings_info.h" |
| 14 #include "components/content_settings/core/browser/content_settings_registry.h" | 14 #include "components/content_settings/core/browser/content_settings_registry.h" |
| 15 #include "components/content_settings/core/browser/content_settings_rule.h" | 15 #include "components/content_settings/core/browser/content_settings_rule.h" |
| 16 #include "components/content_settings/core/browser/content_settings_utils.h" | 16 #include "components/content_settings/core/browser/content_settings_utils.h" |
| 17 #include "components/content_settings/core/browser/host_content_settings_map.h" | 17 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 18 #include "components/content_settings/core/common/content_settings.h" | 18 #include "components/content_settings/core/common/content_settings.h" |
| 19 #include "components/content_settings/core/common/content_settings_pattern.h" | 19 #include "components/content_settings/core/common/content_settings_pattern.h" |
| 20 #include "components/content_settings/core/common/pref_names.h" | 20 #include "components/content_settings/core/common/pref_names.h" |
| 21 #include "components/prefs/scoped_user_pref_update.h" | 21 #include "components/prefs/scoped_user_pref_update.h" |
| 22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 const char kSettingPath[] = "setting"; | 26 const char kSettingPath[] = "setting"; |
| 27 const char kPerResourceIdentifierPrefName[] = "per_resource"; | 27 const char kPerResourceIdentifierPrefName[] = "per_resource"; |
| 28 const char kLastUsed[] = "last_used"; | 28 const char kLastUsed[] = "last_used"; |
| 29 | 29 |
| 30 struct ExceptionUmaStats { | |
| 31 ContentSettingsType settings_type; | |
| 32 const char* allow_exception_metrics_name; | |
| 33 size_t allow_exception_count; | |
| 34 const char* block_exception_metrics_name; | |
| 35 size_t block_exception_count; | |
| 36 const char* ask_exception_metrics_name; | |
| 37 size_t ask_exception_count; | |
| 38 const char* important_content_exception_metrics_name; | |
| 39 size_t important_content_exception_count; | |
| 40 const char* session_only_exception_metrics_name; | |
| 41 size_t session_only_exception_count; | |
| 42 }; | |
| 43 | |
| 44 ExceptionUmaStats ContentSettingExceptionUmaMap[] = { | |
| 45 { | |
| 46 CONTENT_SETTINGS_TYPE_COOKIES, | |
| 47 "ContentSettings.NumberOfAllowCookiesExceptions", 0, | |
| 48 "ContentSettings.NumberOfBlockCookiesExceptions", 0, "", 0, "", 0, | |
| 49 "ContentSettings.NumberOfSessionOnlyCookiesExceptions", 0, | |
| 50 }, | |
| 51 { | |
| 52 CONTENT_SETTINGS_TYPE_IMAGES, | |
| 53 "ContentSettings.NumberOfAllowImagesExceptions", 0, | |
| 54 "ContentSettings.NumberOfBlockImagesExceptions", 0, "", 0, "", 0, "", 0, | |
| 55 }, | |
| 56 { | |
| 57 CONTENT_SETTINGS_TYPE_JAVASCRIPT, | |
| 58 "ContentSettings.NumberOfAllowJavaScriptExceptions", 0, | |
| 59 "ContentSettings.NumberOfBlockJavaScriptExceptions", 0, "", 0, "", 0, | |
| 60 "", 0, | |
| 61 }, | |
| 62 { | |
| 63 CONTENT_SETTINGS_TYPE_PLUGINS, | |
| 64 "ContentSettings.NumberOfAllowPluginsExceptions", 0, | |
| 65 "ContentSettings.NumberOfBlockPluginsExceptions", 0, | |
| 66 "ContentSettings.NumberOfAskPluginsExceptions", 0, | |
| 67 "ContentSettings.NumberOfDetectImportantContentPluginsExceptions", 0, | |
| 68 "", 0, | |
| 69 }, | |
| 70 { | |
| 71 CONTENT_SETTINGS_TYPE_POPUPS, | |
| 72 "ContentSettings.NumberOfAllowPopupsExceptions", 0, | |
| 73 "ContentSettings.NumberOfBlockPopupsExceptions", 0, "", 0, "", 0, "", 0, | |
| 74 }, | |
| 75 { | |
| 76 CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS, | |
| 77 "ContentSettings.NumberOfAllowAutomaticDownloadsExceptions", 0, | |
| 78 "ContentSettings.NumberOfBlockAutomaticDownloadsExceptions", 0, | |
| 79 "ContentSettings.NumberOfAskAutomaticDownloadsExceptions", 0, "", 0, "", | |
| 80 0, | |
| 81 }}; | |
| 82 | |
| 30 // If the given content type supports resource identifiers in user preferences, | 83 // If the given content type supports resource identifiers in user preferences, |
| 31 // returns true and sets |pref_key| to the key in the content settings | 84 // returns true and sets |pref_key| to the key in the content settings |
| 32 // dictionary under which per-resource content settings are stored. | 85 // dictionary under which per-resource content settings are stored. |
| 33 // Otherwise, returns false. | 86 // Otherwise, returns false. |
| 34 bool SupportsResourceIdentifiers(ContentSettingsType content_type) { | 87 bool SupportsResourceIdentifiers(ContentSettingsType content_type) { |
| 35 return content_type == CONTENT_SETTINGS_TYPE_PLUGINS; | 88 return content_type == CONTENT_SETTINGS_TYPE_PLUGINS; |
| 36 } | 89 } |
| 37 | 90 |
| 38 bool IsValueAllowedForType(const base::Value* value, ContentSettingsType type) { | 91 bool IsValueAllowedForType(const base::Value* value, ContentSettingsType type) { |
| 39 const content_settings::ContentSettingsInfo* info = | 92 const content_settings::ContentSettingsInfo* info = |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 273 if (!is_incognito_) { | 326 if (!is_incognito_) { |
| 274 mutable_settings = update.Get(); | 327 mutable_settings = update.Get(); |
| 275 } else { | 328 } else { |
| 276 // Create copy as we do not want to persist anything in OTR prefs. | 329 // Create copy as we do not want to persist anything in OTR prefs. |
| 277 mutable_settings = all_settings_dictionary->DeepCopy(); | 330 mutable_settings = all_settings_dictionary->DeepCopy(); |
| 278 mutable_settings_scope.reset(mutable_settings); | 331 mutable_settings_scope.reset(mutable_settings); |
| 279 } | 332 } |
| 280 // Convert all Unicode patterns into punycode form, then read. | 333 // Convert all Unicode patterns into punycode form, then read. |
| 281 CanonicalizeContentSettingsExceptions(mutable_settings); | 334 CanonicalizeContentSettingsExceptions(mutable_settings); |
| 282 | 335 |
| 283 size_t cookies_block_exception_count = 0; | |
| 284 size_t cookies_allow_exception_count = 0; | |
| 285 size_t cookies_session_only_exception_count = 0; | |
| 286 for (base::DictionaryValue::Iterator i(*mutable_settings); !i.IsAtEnd(); | 336 for (base::DictionaryValue::Iterator i(*mutable_settings); !i.IsAtEnd(); |
| 287 i.Advance()) { | 337 i.Advance()) { |
| 288 const std::string& pattern_str(i.key()); | 338 const std::string& pattern_str(i.key()); |
| 289 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair = | 339 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair = |
| 290 ParsePatternString(pattern_str); | 340 ParsePatternString(pattern_str); |
| 291 if (!pattern_pair.first.IsValid() || | 341 if (!pattern_pair.first.IsValid() || |
| 292 !pattern_pair.second.IsValid()) { | 342 !pattern_pair.second.IsValid()) { |
| 293 // TODO: Change this to DFATAL when crbug.com/132659 is fixed. | 343 // TODO: Change this to DFATAL when crbug.com/132659 is fixed. |
| 294 LOG(ERROR) << "Invalid pattern strings: " << pattern_str; | 344 LOG(ERROR) << "Invalid pattern strings: " << pattern_str; |
| 295 continue; | 345 continue; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 327 const base::Value* value = nullptr; | 377 const base::Value* value = nullptr; |
| 328 settings_dictionary->GetWithoutPathExpansion(kSettingPath, &value); | 378 settings_dictionary->GetWithoutPathExpansion(kSettingPath, &value); |
| 329 | 379 |
| 330 if (value) { | 380 if (value) { |
| 331 DCHECK(IsValueAllowedForType(value, content_type_)); | 381 DCHECK(IsValueAllowedForType(value, content_type_)); |
| 332 value_map_.SetValue(pattern_pair.first, | 382 value_map_.SetValue(pattern_pair.first, |
| 333 pattern_pair.second, | 383 pattern_pair.second, |
| 334 content_type_, | 384 content_type_, |
| 335 ResourceIdentifier(), | 385 ResourceIdentifier(), |
| 336 value->DeepCopy()); | 386 value->DeepCopy()); |
| 337 if (content_type_ == CONTENT_SETTINGS_TYPE_COOKIES) { | 387 |
| 338 ContentSetting s = ValueToContentSetting(value); | 388 for (ExceptionUmaStats stats : ContentSettingExceptionUmaMap) { |
|
raymes
2016/04/07 04:57:59
I think that perhaps we can just measure all types
raymes
2016/04/07 04:57:59
Can we move this code higher up the stack? Perhaps
lshang
2016/04/11 06:31:07
Done.
lshang
2016/04/11 06:31:07
Done.
| |
| 339 switch (s) { | 389 if (stats.settings_type == content_type_) { |
| 340 case CONTENT_SETTING_ALLOW : | 390 ContentSetting s = ValueToContentSetting(value); |
| 341 ++cookies_allow_exception_count; | 391 switch (s) { |
| 342 break; | 392 case CONTENT_SETTING_ALLOW: |
| 343 case CONTENT_SETTING_BLOCK : | 393 ++stats.allow_exception_count; |
| 344 ++cookies_block_exception_count; | 394 break; |
| 345 break; | 395 case CONTENT_SETTING_BLOCK: |
| 346 case CONTENT_SETTING_SESSION_ONLY : | 396 ++stats.block_exception_count; |
|
raymes
2016/04/07 04:57:59
I think we should probably only worry about allow/
lshang
2016/04/11 06:31:07
Done.
| |
| 347 ++cookies_session_only_exception_count; | 397 break; |
| 348 break; | 398 case CONTENT_SETTING_ASK: |
| 349 default: | 399 ++stats.ask_exception_count; |
| 350 NOTREACHED(); | 400 break; |
| 351 break; | 401 case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT: |
| 402 ++stats.important_content_exception_count; | |
| 403 break; | |
| 404 case CONTENT_SETTING_SESSION_ONLY: | |
| 405 ++stats.session_only_exception_count; | |
| 406 break; | |
| 407 default: | |
| 408 NOTREACHED(); | |
| 409 break; | |
| 410 } | |
| 352 } | 411 } |
| 353 } | 412 } |
| 354 } | 413 } |
| 355 | 414 |
| 356 } | 415 } |
| 357 | 416 |
| 358 if (content_type_ == CONTENT_SETTINGS_TYPE_COOKIES) { | 417 for (ExceptionUmaStats stats : ContentSettingExceptionUmaMap) { |
| 359 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfBlockCookiesExceptions", | 418 if (stats.settings_type == content_type_) { |
| 360 cookies_block_exception_count); | 419 UMA_HISTOGRAM_COUNTS(stats.allow_exception_metrics_name, |
| 361 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfAllowCookiesExceptions", | 420 stats.allow_exception_count); |
| 362 cookies_allow_exception_count); | 421 UMA_HISTOGRAM_COUNTS(stats.block_exception_metrics_name, |
| 363 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfSessionOnlyCookiesExceptions", | 422 stats.block_exception_count); |
| 364 cookies_session_only_exception_count); | 423 if (content_type_ == CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS) { |
| 424 UMA_HISTOGRAM_COUNTS(stats.ask_exception_metrics_name, | |
| 425 stats.ask_exception_count); | |
| 426 } else if (content_type_ == CONTENT_SETTINGS_TYPE_PLUGINS) { | |
| 427 UMA_HISTOGRAM_COUNTS(stats.ask_exception_metrics_name, | |
| 428 stats.ask_exception_count); | |
| 429 UMA_HISTOGRAM_COUNTS(stats.important_content_exception_metrics_name, | |
| 430 stats.important_content_exception_count); | |
| 431 } else if (content_type_ == CONTENT_SETTINGS_TYPE_COOKIES) { | |
| 432 UMA_HISTOGRAM_COUNTS(stats.session_only_exception_metrics_name, | |
| 433 stats.session_only_exception_count); | |
| 434 } | |
| 435 } | |
| 365 } | 436 } |
| 366 } | 437 } |
| 367 | 438 |
| 368 void ContentSettingsPref::OnPrefChanged() { | 439 void ContentSettingsPref::OnPrefChanged() { |
| 369 DCHECK(thread_checker_.CalledOnValidThread()); | 440 DCHECK(thread_checker_.CalledOnValidThread()); |
| 370 | 441 |
| 371 if (updating_preferences_) | 442 if (updating_preferences_) |
| 372 return; | 443 return; |
| 373 | 444 |
| 374 ReadContentSettingsFromPref(); | 445 ReadContentSettingsFromPref(); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 508 | 579 |
| 509 void ContentSettingsPref::AssertLockNotHeld() const { | 580 void ContentSettingsPref::AssertLockNotHeld() const { |
| 510 #if !defined(NDEBUG) | 581 #if !defined(NDEBUG) |
| 511 // |Lock::Acquire()| will assert if the lock is held by this thread. | 582 // |Lock::Acquire()| will assert if the lock is held by this thread. |
| 512 lock_.Acquire(); | 583 lock_.Acquire(); |
| 513 lock_.Release(); | 584 lock_.Release(); |
| 514 #endif | 585 #endif |
| 515 } | 586 } |
| 516 | 587 |
| 517 } // namespace content_settings | 588 } // namespace content_settings |
| OLD | NEW |