diff options
author | markusheintz@chromium.org <markusheintz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-31 08:21:05 +0000 |
---|---|---|
committer | markusheintz@chromium.org <markusheintz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-31 08:21:05 +0000 |
commit | f67a10be22371bd4cd9e8727e72d3f132faa6fbd (patch) | |
tree | 828a3ec8e48810975d84ab1416941daf6b15554d /chrome | |
parent | 757f2505873dd7f05eb807d3f4f789d1cd5c0369 (diff) | |
download | chromium_src-f67a10be22371bd4cd9e8727e72d3f132faa6fbd.zip chromium_src-f67a10be22371bd4cd9e8727e72d3f132faa6fbd.tar.gz chromium_src-f67a10be22371bd4cd9e8727e72d3f132faa6fbd.tar.bz2 |
Migrate Obsolete NotificationsSettings and remove content_settings::NotificationsProvider.
BUG=63656
TEST=host_content_settings_map_unittest.cc,
content_settings_pref_provider.cc,
desktop_notifications_service_unittest.cc
Review URL: http://codereview.chromium.org/7655019
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@98938 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
22 files changed, 504 insertions, 1223 deletions
diff --git a/chrome/browser/content_settings/content_settings_notification_provider.cc b/chrome/browser/content_settings/content_settings_notification_provider.cc deleted file mode 100644 index 98893be..0000000 --- a/chrome/browser/content_settings/content_settings_notification_provider.cc +++ /dev/null @@ -1,368 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - - -#include "chrome/browser/content_settings/content_settings_notification_provider.h" - -#include "base/string_util.h" -#include "chrome/browser/notifications/desktop_notification_service_factory.h" -#include "chrome/browser/notifications/notification.h" -#include "chrome/browser/notifications/notifications_prefs_cache.h" -#include "chrome/browser/notifications/notification_ui_manager.h" -#include "chrome/browser/prefs/pref_service.h" -#include "chrome/browser/prefs/scoped_user_pref_update.h" -#include "chrome/browser/profiles/profile.h" -#include "chrome/common/chrome_notification_types.h" -#include "chrome/common/content_settings_types.h" -#include "chrome/common/pref_names.h" -#include "chrome/common/url_constants.h" -#include "content/common/notification_service.h" -#include "googleurl/src/gurl.h" - -namespace { - -const ContentSetting kDefaultSetting = CONTENT_SETTING_ASK; - -} // namespace - -namespace content_settings { - -// //////////////////////////////////////////////////////////////////////////// -// NotificationProvider -// - -// static -void NotificationProvider::RegisterUserPrefs(PrefService* user_prefs) { - if (!user_prefs->FindPreference(prefs::kDesktopNotificationAllowedOrigins)) - user_prefs->RegisterListPref(prefs::kDesktopNotificationAllowedOrigins, - PrefService::SYNCABLE_PREF); - if (!user_prefs->FindPreference(prefs::kDesktopNotificationDeniedOrigins)) - user_prefs->RegisterListPref(prefs::kDesktopNotificationDeniedOrigins, - PrefService::SYNCABLE_PREF); -} - -// TODO(markusheintz): Re-factoring in progress. Do not move or touch the -// following two static methods as you might cause trouble. Thanks! - -// static -ContentSettingsPattern NotificationProvider::ToContentSettingsPattern( - const GURL& origin) { - // Fix empty GURLs. - if (origin.spec().empty()) { - std::string pattern_spec(chrome::kFileScheme); - pattern_spec += chrome::kStandardSchemeSeparator; - return ContentSettingsPattern::FromString(pattern_spec); - } - return ContentSettingsPattern::FromURLNoWildcard(origin); -} - -// static -GURL NotificationProvider::ToGURL(const ContentSettingsPattern& pattern) { - std::string pattern_spec(pattern.ToString()); - if (!pattern.IsValid() || - (pattern_spec.find(std::string( - ContentSettingsPattern::kDomainWildcard)) != std::string::npos)) { - NOTREACHED(); - } - return GURL(pattern_spec); -} - -NotificationProvider::NotificationProvider( - Profile* profile) - : profile_(profile) { - prefs_registrar_.Init(profile_->GetPrefs()); - StartObserving(); -} - -NotificationProvider::~NotificationProvider() { - StopObserving(); -} - -ContentSetting NotificationProvider::GetContentSetting( - const GURL& requesting_url, - const GURL& embedding_url, - ContentSettingsType content_type, - const ResourceIdentifier& resource_identifier) const { - if (content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS) - return CONTENT_SETTING_DEFAULT; - - return GetContentSetting(requesting_url); -} - -void NotificationProvider::SetContentSetting( - const ContentSettingsPattern& requesting_url_pattern, - const ContentSettingsPattern& embedding_url_pattern, - ContentSettingsType content_type, - const ResourceIdentifier& resource_identifier, - ContentSetting content_setting) { - if (content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS) - return; - - GURL origin = ToGURL(requesting_url_pattern); - if (CONTENT_SETTING_ALLOW == content_setting) { - GrantPermission(origin); - } else if (CONTENT_SETTING_BLOCK == content_setting) { - DenyPermission(origin); - } else if (CONTENT_SETTING_DEFAULT == content_setting) { - ContentSetting current_setting = GetContentSetting(origin); - if (CONTENT_SETTING_ALLOW == current_setting) { - ResetAllowedOrigin(origin); - } else if (CONTENT_SETTING_BLOCK == current_setting) { - ResetBlockedOrigin(origin); - } else { - NOTREACHED(); - } - } else { - NOTREACHED(); - } -} - -void NotificationProvider::GetAllContentSettingsRules( - ContentSettingsType content_type, - const ResourceIdentifier& resource_identifier, - Rules* content_setting_rules) const { - if (content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS) - return; - - std::vector<GURL> allowed_origins = GetAllowedOrigins(); - std::vector<GURL> denied_origins = GetBlockedOrigins(); - - for (std::vector<GURL>::iterator url = allowed_origins.begin(); - url != allowed_origins.end(); - ++url) { - ContentSettingsPattern pattern = - ContentSettingsPattern::FromURLNoWildcard(*url); - content_setting_rules->push_back(Rule( - pattern, - pattern, - CONTENT_SETTING_ALLOW)); - } - for (std::vector<GURL>::iterator url = denied_origins.begin(); - url != denied_origins.end(); - ++url) { - ContentSettingsPattern pattern = - ContentSettingsPattern::FromURLNoWildcard(*url); - content_setting_rules->push_back(Rule( - pattern, - pattern, - CONTENT_SETTING_BLOCK)); - } -} - -void NotificationProvider::ClearAllContentSettingsRules( - ContentSettingsType content_type) { - if (content_type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS) - ResetAllOrigins(); -} - -void NotificationProvider::ShutdownOnUIThread() { -} - -void NotificationProvider::Observe(int type, - const NotificationSource& source, - const NotificationDetails& details) { - if (chrome::NOTIFICATION_PREF_CHANGED == type) { - const std::string& name = *Details<std::string>(details).ptr(); - OnPrefsChanged(name); - } else if (chrome::NOTIFICATION_PROFILE_DESTROYED == type) { - StopObserving(); - } -} - -///////////////////////////////////////////////////////////////////// -// Private -// - -void NotificationProvider::StartObserving() { - if (!profile_->IsOffTheRecord()) { - prefs_registrar_.Add(prefs::kDesktopNotificationAllowedOrigins, this); - prefs_registrar_.Add(prefs::kDesktopNotificationDeniedOrigins, this); - - notification_registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, - NotificationService::AllSources()); - } - - notification_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, - Source<Profile>(profile_)); -} - -void NotificationProvider::StopObserving() { - if (!profile_->IsOffTheRecord()) { - prefs_registrar_.RemoveAll(); - } - notification_registrar_.RemoveAll(); -} - -void NotificationProvider::OnPrefsChanged(const std::string& pref_name) { - if (pref_name == prefs::kDesktopNotificationAllowedOrigins) { - NotifySettingsChange(); - } else if (pref_name == prefs::kDesktopNotificationDeniedOrigins) { - NotifySettingsChange(); - } -} - -void NotificationProvider::NotifySettingsChange() { - // TODO(markusheintz): Re-factoring work in progress: Replace the - // DESKTOP_NOTIFICATION_SETTINGS_CHANGED with a CONTENT_SETTINGS_CHANGED - // notification, and use the HostContentSettingsMap as source once this - // content settings provider in integrated in the HostContentSetttingsMap. - NotificationService::current()->Notify( - chrome::NOTIFICATION_DESKTOP_NOTIFICATION_SETTINGS_CHANGED, - Source<DesktopNotificationService>( - DesktopNotificationServiceFactory::GetForProfile(profile_)), - NotificationService::NoDetails()); -} - -std::vector<GURL> NotificationProvider::GetAllowedOrigins() const { - std::vector<GURL> allowed_origins; - PrefService* prefs = profile_->GetPrefs(); - const ListValue* allowed_sites = - prefs->GetList(prefs::kDesktopNotificationAllowedOrigins); - if (allowed_sites) { - // TODO(markusheintz): Remove dependency to PrefsCache - NotificationsPrefsCache::ListValueToGurlVector(*allowed_sites, - &allowed_origins); - } - return allowed_origins; -} - -std::vector<GURL> NotificationProvider::GetBlockedOrigins() const { - std::vector<GURL> denied_origins; - PrefService* prefs = profile_->GetPrefs(); - const ListValue* denied_sites = - prefs->GetList(prefs::kDesktopNotificationDeniedOrigins); - if (denied_sites) { - // TODO(markusheintz): Remove dependency to PrefsCache - NotificationsPrefsCache::ListValueToGurlVector(*denied_sites, - &denied_origins); - } - return denied_origins; -} - -void NotificationProvider::GrantPermission(const GURL& origin) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - PersistPermissionChange(origin, true); - NotifySettingsChange(); -} - -void NotificationProvider::DenyPermission(const GURL& origin) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - PersistPermissionChange(origin, false); - NotifySettingsChange(); -} - -void NotificationProvider::PersistPermissionChange( - const GURL& origin, bool is_allowed) { - // Don't persist changes when incognito. - if (profile_->IsOffTheRecord()) - return; - PrefService* prefs = profile_->GetPrefs(); - - // |Observe()| updates the whole permission set in the cache, but only a - // single origin has changed. Hence, callers of this method manually - // schedule a task to update the prefs cache, and the prefs observer is - // disabled while the update runs. - StopObserving(); - - bool allowed_changed = false; - bool denied_changed = false; - - { - ListPrefUpdate update_allowed_sites( - prefs, prefs::kDesktopNotificationAllowedOrigins); - ListPrefUpdate update_denied_sites( - prefs, prefs::kDesktopNotificationDeniedOrigins); - ListValue* allowed_sites = update_allowed_sites.Get(); - ListValue* denied_sites = update_denied_sites.Get(); - // |value| is passed to the preferences list, or deleted. - StringValue* value = new StringValue(origin.spec()); - - // Remove from one list and add to the other. - if (is_allowed) { - // Remove from the denied list. - if (denied_sites->Remove(*value, NULL)) - denied_changed = true; - - // Add to the allowed list. - if (allowed_sites->AppendIfNotPresent(value)) - allowed_changed = true; - } else { - // Remove from the allowed list. - if (allowed_sites->Remove(*value, NULL)) - allowed_changed = true; - - // Add to the denied list. - if (denied_sites->AppendIfNotPresent(value)) - denied_changed = true; - } - } - - // Persist the pref if anthing changed, but only send updates for the - // list that changed. - if (allowed_changed || denied_changed) - prefs->ScheduleSavePersistentPrefs(); - StartObserving(); -} - -ContentSetting NotificationProvider::GetContentSetting( - const GURL& origin) const { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - - if (profile_->IsOffTheRecord()) - return kDefaultSetting; - - std::vector<GURL> allowed_origins(GetAllowedOrigins()); - if (std::find(allowed_origins.begin(), allowed_origins.end(), origin) != - allowed_origins.end()) - return CONTENT_SETTING_ALLOW; - - std::vector<GURL> denied_origins(GetBlockedOrigins()); - if (std::find(denied_origins.begin(), denied_origins.end(), origin) != - denied_origins.end()) - return CONTENT_SETTING_BLOCK; - - return CONTENT_SETTING_DEFAULT; -} - -void NotificationProvider::ResetAllowedOrigin(const GURL& origin) { - if (profile_->IsOffTheRecord()) - return; - - // Since this isn't called often, let the normal observer behavior update the - // cache in this case. - PrefService* prefs = profile_->GetPrefs(); - { - ListPrefUpdate update(prefs, prefs::kDesktopNotificationAllowedOrigins); - ListValue* allowed_sites = update.Get(); - StringValue value(origin.spec()); - bool removed = allowed_sites->Remove(value, NULL); - DCHECK_NE(false, removed) << origin << " was not allowed"; - } - prefs->ScheduleSavePersistentPrefs(); -} - -void NotificationProvider::ResetBlockedOrigin(const GURL& origin) { - if (profile_->IsOffTheRecord()) - return; - - // Since this isn't called often, let the normal observer behavior update the - // cache in this case. - PrefService* prefs = profile_->GetPrefs(); - { - ListPrefUpdate update(prefs, prefs::kDesktopNotificationDeniedOrigins); - ListValue* denied_sites = update.Get(); - StringValue value(origin.spec()); - bool removed = denied_sites->Remove(value, NULL); - DCHECK_NE(false, removed) << origin << " was not blocked"; - } - prefs->ScheduleSavePersistentPrefs(); -} - -void NotificationProvider::ResetAllOrigins() { - PrefService* prefs = profile_->GetPrefs(); - prefs->ClearPref(prefs::kDesktopNotificationAllowedOrigins); - prefs->ClearPref(prefs::kDesktopNotificationDeniedOrigins); -} - -} // namespace content_settings diff --git a/chrome/browser/content_settings/content_settings_notification_provider.h b/chrome/browser/content_settings/content_settings_notification_provider.h deleted file mode 100644 index 1f9caf7..0000000 --- a/chrome/browser/content_settings/content_settings_notification_provider.h +++ /dev/null @@ -1,106 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_NOTIFICATION_PROVIDER_H_ -#define CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_NOTIFICATION_PROVIDER_H_ - -#include <string> -#include <vector> - -#include "base/basictypes.h" -#include "base/memory/ref_counted.h" -#include "chrome/browser/content_settings/content_settings_provider.h" -#include "chrome/browser/prefs/pref_change_registrar.h" -#include "content/common/notification_observer.h" -#include "content/common/notification_registrar.h" - -class GURL; -class PrefService; -class Profile; - -namespace content_settings { - -class NotificationProvider : public ProviderInterface, - public NotificationObserver { - public: - static void RegisterUserPrefs(PrefService* user_prefs); - - static ContentSettingsPattern ToContentSettingsPattern(const GURL& origin); - - static GURL ToGURL(const ContentSettingsPattern& pattern); - - explicit NotificationProvider(Profile* profile); - - virtual ~NotificationProvider(); - - virtual ContentSetting GetContentSetting( - const GURL& requesting_url, - const GURL& embedding_url, - ContentSettingsType content_type, - const ResourceIdentifier& resource_identifier) const; - - virtual void SetContentSetting( - const ContentSettingsPattern& requesting_url_pattern, - const ContentSettingsPattern& embedding_url_pattern, - ContentSettingsType content_type, - const ResourceIdentifier& resource_identifier, - ContentSetting content_setting); - - virtual void GetAllContentSettingsRules( - ContentSettingsType content_type, - const ResourceIdentifier& resource_identifier, - Rules* content_setting_rules) const; - - virtual void ClearAllContentSettingsRules( - ContentSettingsType content_type); - - virtual void ShutdownOnUIThread(); - - // NotificationObserver implementation. - virtual void Observe(int type, - const NotificationSource& source, - const NotificationDetails& details); - private: - void StartObserving(); - void StopObserving(); - - void OnPrefsChanged(const std::string& pref_name); - - // Notifies the observers when permissions settings change. - void NotifySettingsChange(); - - // Returns all origins that explicitly have been allowed. - std::vector<GURL> GetAllowedOrigins() const; - - // Returns all origins that explicitly have been denied. - std::vector<GURL> GetBlockedOrigins() const; - - // Methods to setup and modify permission preferences. - void GrantPermission(const GURL& origin); - void DenyPermission(const GURL& origin); - - void PersistPermissionChange(const GURL& origin, bool is_allowed); - - ContentSetting GetContentSetting(const GURL& origin) const; - - // Removes an origin from the "explicitly allowed" set. - void ResetAllowedOrigin(const GURL& origin); - - // Removes an origin from the "explicitly denied" set. - void ResetBlockedOrigin(const GURL& origin); - - // Clears the sets of explicitly allowed and denied origins. - void ResetAllOrigins(); - - Profile* profile_; - - PrefChangeRegistrar prefs_registrar_; - NotificationRegistrar notification_registrar_; - - DISALLOW_COPY_AND_ASSIGN(NotificationProvider); -}; - -} // namespace content_settings - -#endif // CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_NOTIFICATION_PROVIDER_H_ diff --git a/chrome/browser/content_settings/content_settings_pref_provider.cc b/chrome/browser/content_settings/content_settings_pref_provider.cc index 37f60c1..1a68486 100644 --- a/chrome/browser/content_settings/content_settings_pref_provider.cc +++ b/chrome/browser/content_settings/content_settings_pref_provider.cc @@ -43,8 +43,8 @@ const char* kResourceTypeNames[] = { "per_plugin", NULL, NULL, - NULL, // Not used for Notifications - NULL, // Not used for Intents. + NULL, + NULL, NULL, }; COMPILE_ASSERT(arraysize(kResourceTypeNames) == CONTENT_SETTINGS_NUM_TYPES, @@ -73,9 +73,7 @@ const char* kTypeNames[] = { "plugins", "popups", "geolocation", - // TODO(markusheintz): Refactoring in progress. Content settings exceptions - // for notifications added next. - "notifications", // Only used for default Notifications settings. + "notifications", "intents", "auto-select-certificate" }; @@ -123,6 +121,24 @@ ContentSetting FixObsoleteCookiePromptMode(ContentSettingsType content_type, return setting; } +// Clears all settings for the given |type| in the given |pattern_pairs| +// dictionary. +void ClearSettings(ContentSettingsType type, + DictionaryValue* pattern_pairs) { + std::string type_name(kTypeNames[type]); + for (DictionaryValue::key_iterator i = pattern_pairs->begin_keys(); + i != pattern_pairs->end_keys(); + ++i) { + const std::string& pattern_pair(*i); + + DictionaryValue* settings = NULL; + pattern_pairs->GetDictionaryWithoutPathExpansion( + pattern_pair, &settings); + + settings->RemoveWithoutPathExpansion(type_name, NULL); + } +} + } // namespace namespace content_settings { @@ -371,6 +387,10 @@ void PrefProvider::RegisterUserPrefs(PrefService* prefs) { PrefService::SYNCABLE_PREF); prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns, PrefService::SYNCABLE_PREF); + prefs->RegisterListPref(prefs::kDesktopNotificationAllowedOrigins, + PrefService::SYNCABLE_PREF); + prefs->RegisterListPref(prefs::kDesktopNotificationDeniedOrigins, + PrefService::SYNCABLE_PREF); prefs->RegisterListPref(prefs::kPopupWhitelistedHosts, PrefService::UNSYNCABLE_PREF); prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings, @@ -389,6 +409,7 @@ PrefProvider::PrefProvider(PrefService* prefs, MigrateObsoletePopupsPref(); MigrateObsoleteContentSettingsPatternPref(); MigrateObsoleteGeolocationPref(); + MigrateObsoleteNotificationsPrefs(); } // Verify preferences version. @@ -413,6 +434,8 @@ PrefProvider::PrefProvider(PrefService* prefs, pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this); pref_change_registrar_.Add(prefs::kContentSettingsPatternPairs, this); pref_change_registrar_.Add(prefs::kGeolocationContentSettings, this); + pref_change_registrar_.Add(prefs::kDesktopNotificationAllowedOrigins, this); + pref_change_registrar_.Add(prefs::kDesktopNotificationDeniedOrigins, this); } ContentSetting PrefProvider::GetContentSetting( @@ -511,6 +534,7 @@ void PrefProvider::SetContentSetting( content_type, resource_identifier, setting); + prefs_->ScheduleSavePersistentPrefs(); } NotifyObservers( @@ -545,7 +569,7 @@ void PrefProvider::ClearAllContentSettingsRules( } } } - + prefs_->ScheduleSavePersistentPrefs(); NotifyObservers(ContentSettingsPattern(), ContentSettingsPattern(), content_type, @@ -568,16 +592,20 @@ void PrefProvider::Observe( std::string* name = Details<std::string>(details).ptr(); if (*name == prefs::kContentSettingsPatternPairs) { SyncObsoletePatternPref(); - SyncObsoleteGeolocationPref(); + SyncObsoletePrefs(); } else if (*name == prefs::kContentSettingsPatterns) { MigrateObsoleteContentSettingsPatternPref(); } else if (*name == prefs::kGeolocationContentSettings) { MigrateObsoleteGeolocationPref(); + } else if (*name == prefs::kDesktopNotificationAllowedOrigins || + *name == prefs::kDesktopNotificationDeniedOrigins) { + MigrateObsoleteNotificationsPrefs(); } else { NOTREACHED() << "Unexpected preference observed"; return; } } + prefs_->ScheduleSavePersistentPrefs(); ReadContentSettingsFromPref(true); NotifyObservers(ContentSettingsPattern(), @@ -603,11 +631,17 @@ void PrefProvider::UpdatePref( const ResourceIdentifier& resource_identifier, ContentSetting setting) { AutoReset<bool> auto_reset(&updating_preferences_, true); - UpdatePatternPairsPref(primary_pattern, - secondary_pattern, - content_type, - resource_identifier, - setting); + { + DictionaryPrefUpdate update(prefs_, + prefs::kContentSettingsPatternPairs); + DictionaryValue* pattern_pairs_settings = update.Get(); + UpdatePatternPairsSettings(primary_pattern, + secondary_pattern, + content_type, + resource_identifier, + setting, + pattern_pairs_settings); + } if (content_type != CONTENT_SETTINGS_TYPE_GEOLOCATION && content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS) { UpdateObsoletePatternsPref(primary_pattern, @@ -615,9 +649,18 @@ void PrefProvider::UpdatePref( content_type, resource_identifier, setting); - } - if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION) { + } else if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION) { UpdateObsoleteGeolocationPref(primary_pattern, secondary_pattern, setting); + } else if (content_type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS) { + ListPrefUpdate update_allowed_sites( + prefs_, prefs::kDesktopNotificationAllowedOrigins); + ListPrefUpdate update_denied_sites( + prefs_, prefs::kDesktopNotificationDeniedOrigins); + UpdateObsoleteNotificationsSettings(primary_pattern, + secondary_pattern, + setting, + update_allowed_sites.Get(), + update_denied_sites.Get()); } } @@ -779,26 +822,23 @@ void PrefProvider::UpdateObsoletePatternsPref( } } -void PrefProvider::UpdatePatternPairsPref( +void PrefProvider::UpdatePatternPairsSettings( const ContentSettingsPattern& primary_pattern, const ContentSettingsPattern& secondary_pattern, ContentSettingsType content_type, const ResourceIdentifier& resource_identifier, - ContentSetting setting) { - DictionaryPrefUpdate update(prefs_, - prefs::kContentSettingsPatternPairs); - DictionaryValue* all_settings_dictionary = update.Get(); - + ContentSetting setting, + DictionaryValue* pattern_pairs_settings) { // Get settings dictionary for the given patterns. std::string pattern_str(CreatePatternString(primary_pattern, secondary_pattern)); DictionaryValue* settings_dictionary = NULL; - bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( + bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion( pattern_str, &settings_dictionary); if (!found && (setting != CONTENT_SETTING_DEFAULT)) { settings_dictionary = new DictionaryValue; - all_settings_dictionary->SetWithoutPathExpansion( + pattern_pairs_settings->SetWithoutPathExpansion( pattern_str, settings_dictionary); } @@ -840,7 +880,7 @@ void PrefProvider::UpdatePatternPairsPref( } // Remove the settings dictionary if it is empty. if (settings_dictionary->empty()) { - all_settings_dictionary->RemoveWithoutPathExpansion( + pattern_pairs_settings->RemoveWithoutPathExpansion( pattern_str, NULL); } } @@ -883,6 +923,31 @@ void PrefProvider::UpdateObsoleteGeolocationPref( } } +void PrefProvider::UpdateObsoleteNotificationsSettings( + const ContentSettingsPattern& primary_pattern, + const ContentSettingsPattern& secondary_pattern, + ContentSetting setting, + ListValue* allowed_sites, + ListValue* denied_sites) { + DCHECK_EQ(secondary_pattern, ContentSettingsPattern::Wildcard()); + GURL origin(primary_pattern.ToString()); + DCHECK(origin.is_valid()); + scoped_ptr<StringValue> value(new StringValue(origin.spec())); + if (setting == CONTENT_SETTING_ALLOW) { + denied_sites->Remove(*value, NULL); + allowed_sites->AppendIfNotPresent(value.release()); + } else if (setting == CONTENT_SETTING_BLOCK) { + allowed_sites->Remove(*value, NULL); + denied_sites->AppendIfNotPresent(value.release()); + } else if (setting == CONTENT_SETTING_DEFAULT) { + denied_sites->Remove(*value, NULL); + allowed_sites->Remove(*value, NULL); + } else { + NOTREACHED() << "Setting value: " << setting + << " is not supported for notifications"; + } +} + // static void PrefProvider::CanonicalizeContentSettingsExceptions( DictionaryValue* all_settings_dictionary) { @@ -1160,6 +1225,10 @@ void PrefProvider::MigrateObsoleteGeolocationPref() { if (!prefs_->HasPrefPath(prefs::kGeolocationContentSettings)) return; + DictionaryPrefUpdate update(prefs_, + prefs::kContentSettingsPatternPairs); + DictionaryValue* pattern_pairs_settings = update.Get(); + const DictionaryValue* geolocation_settings = prefs_->GetDictionary(prefs::kGeolocationContentSettings); for (DictionaryValue::key_iterator i = @@ -1194,22 +1263,78 @@ void PrefProvider::MigrateObsoleteGeolocationPref() { ContentSettingsPattern::FromURLNoWildcard(secondary_url); DCHECK(primary_pattern.IsValid() && secondary_pattern.IsValid()); - UpdatePatternPairsPref(primary_pattern, - secondary_pattern, - CONTENT_SETTINGS_TYPE_GEOLOCATION, - std::string(), - IntToContentSetting(setting_value)); + UpdatePatternPairsSettings(primary_pattern, + secondary_pattern, + CONTENT_SETTINGS_TYPE_GEOLOCATION, + std::string(), + IntToContentSetting(setting_value), + pattern_pairs_settings); } } } -void PrefProvider::SyncObsoleteGeolocationPref() { +void PrefProvider::MigrateObsoleteNotificationsPrefs() { + // The notifications settings in the preferences + // prefs::kContentSettingsPatternPairs do not contain the latest + // notifications settings. So all notification settings are cleared and + // migrated from the obsolete preferences for notifications settings that + // contain the latest settings. + DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs); + DictionaryValue* pattern_pairs_settings = update.Get(); + ClearSettings(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, pattern_pairs_settings); + + const ListValue* allowed_origins = + prefs_->GetList(prefs::kDesktopNotificationAllowedOrigins); + for (size_t i = 0; i < allowed_origins->GetSize(); ++i) { + std::string url_string; + bool status = allowed_origins->GetString(i, &url_string); + DCHECK(status); + ContentSettingsPattern primary_pattern = + ContentSettingsPattern::FromURLNoWildcard(GURL(url_string)); + DCHECK(primary_pattern.IsValid()); + UpdatePatternPairsSettings(primary_pattern, + ContentSettingsPattern::Wildcard(), + CONTENT_SETTINGS_TYPE_NOTIFICATIONS, + std::string(), + CONTENT_SETTING_ALLOW, + pattern_pairs_settings); + } + + const ListValue* denied_origins = + prefs_->GetList(prefs::kDesktopNotificationDeniedOrigins); + for (size_t i = 0; i < denied_origins->GetSize(); ++i) { + std::string url_string; + bool status = denied_origins->GetString(i, &url_string); + DCHECK(status); + ContentSettingsPattern primary_pattern = + ContentSettingsPattern::FromURLNoWildcard(GURL(url_string)); + DCHECK(primary_pattern.IsValid()); + UpdatePatternPairsSettings(primary_pattern, + ContentSettingsPattern::Wildcard(), + CONTENT_SETTINGS_TYPE_NOTIFICATIONS, + std::string(), + CONTENT_SETTING_BLOCK, + pattern_pairs_settings); + } +} + +void PrefProvider::SyncObsoletePrefs() { DCHECK(prefs_); DCHECK(prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs)); - // Clear the obsolete preference for geolocation settings. Then copy all - // geolocation settings from the new preference to the obsolete one. + // Clear obsolete preferences first. Then copy the settings from the new + // preference to the obsolete ones. prefs_->ClearPref(prefs::kGeolocationContentSettings); + prefs_->ClearPref(prefs::kDesktopNotificationAllowedOrigins); + prefs_->ClearPref(prefs::kDesktopNotificationDeniedOrigins); + + ListPrefUpdate update_allowed_origins( + prefs_, prefs::kDesktopNotificationAllowedOrigins); + ListPrefUpdate update_denied_origins( + prefs_, prefs::kDesktopNotificationDeniedOrigins); + ListValue* allowed_origins = update_allowed_origins.Get(); + ListValue* denied_origins = update_denied_origins.Get(); + const DictionaryValue* pattern_pairs_dictionary = prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); for (DictionaryValue::key_iterator i = @@ -1222,16 +1347,22 @@ void PrefProvider::SyncObsoleteGeolocationPref() { DCHECK(pattern_pair.first.IsValid() && pattern_pair.second.IsValid()); DictionaryValue* settings_dictionary = NULL; - bool found = pattern_pairs_dictionary->GetDictionaryWithoutPathExpansion( + pattern_pairs_dictionary->GetDictionaryWithoutPathExpansion( key, &settings_dictionary); - DCHECK(found); - if (settings_dictionary->HasKey( - kTypeNames[CONTENT_SETTINGS_TYPE_GEOLOCATION])) { - int setting_value; - settings_dictionary->GetInteger( - kTypeNames[CONTENT_SETTINGS_TYPE_GEOLOCATION], &setting_value); + int setting_value = 0; + if (settings_dictionary->GetInteger( + kTypeNames[CONTENT_SETTINGS_TYPE_NOTIFICATIONS], &setting_value)) { + UpdateObsoleteNotificationsSettings(pattern_pair.first, + pattern_pair.second, + ContentSetting(setting_value), + allowed_origins, + denied_origins); + } + setting_value = 0; + if (settings_dictionary->GetInteger( + kTypeNames[CONTENT_SETTINGS_TYPE_GEOLOCATION], &setting_value)) { UpdateObsoleteGeolocationPref(pattern_pair.first, pattern_pair.second, ContentSetting(setting_value)); diff --git a/chrome/browser/content_settings/content_settings_pref_provider.h b/chrome/browser/content_settings/content_settings_pref_provider.h index 3cb3b64..13895f5 100644 --- a/chrome/browser/content_settings/content_settings_pref_provider.h +++ b/chrome/browser/content_settings/content_settings_pref_provider.h @@ -148,15 +148,14 @@ class PrefProvider : public ObservableProvider, const ResourceIdentifier& resource_identifier, ContentSetting setting); - // Update the preference prefs::kContentSettingsPatternPairs, which is used to - // persist content settings exceptions and supposed to replace the preferences - // prefs::kContentSettingsPatterns and prefs::kGeolocationContentSettings. - void UpdatePatternPairsPref( + // Updates the given |pattern_pairs_settings| dictionary value. + void UpdatePatternPairsSettings( const ContentSettingsPattern& primary_pattern, const ContentSettingsPattern& secondary_pattern, ContentSettingsType content_type, const ResourceIdentifier& resource_identifier, - ContentSetting setting); + ContentSetting setting, + DictionaryValue* pattern_pairs_settings); // Updates the preferences prefs::kContentSettingsPatterns. This preferences // is obsolete and only used for compatibility reasons. @@ -167,7 +166,7 @@ class PrefProvider : public ObservableProvider, const ResourceIdentifier& resource_identifier, ContentSetting setting); - // Updates the preferences prefs::kGeolocationContentSettings. This preference + // Updates the preference prefs::kGeolocationContentSettings. This preference // is obsolete and only used to keep sync working with older chrome versions // that do not know about the new preference. void UpdateObsoleteGeolocationPref( @@ -175,12 +174,22 @@ class PrefProvider : public ObservableProvider, const ContentSettingsPattern& secondary_pattern, ContentSetting setting); + // Updates the obsolete notifications settings in the passed list values + // |allowed_sites| and |denied_sites|. + void UpdateObsoleteNotificationsSettings( + const ContentSettingsPattern& primary_pattern, + const ContentSettingsPattern& secondary_pattern, + ContentSetting setting, + ListValue* allowed_sites, + ListValue* denied_sites); + // Various migration methods (old cookie, popup and per-host data gets // migrated to the new format). void MigrateObsoletePerhostPref(); void MigrateObsoletePopupsPref(); void MigrateObsoleteContentSettingsPatternPref(); void MigrateObsoleteGeolocationPref(); + void MigrateObsoleteNotificationsPrefs(); // Copies the value of the preference that stores the content settings // exceptions to the obsolete preference for content settings exceptions. This @@ -188,12 +197,13 @@ class PrefProvider : public ObservableProvider, // versions of chrome that only use the obsolete preference. void SyncObsoletePatternPref(); - // Copies the geolocation content settings exceptions from the preference that - // stores the content settings exceptions to the obsolete preference for - // geolocation content settings exceptions. This is necessary to allow - // geolocation content settings exceptions being synced to older versions of - // chrome that only use the obsolete preference. - void SyncObsoleteGeolocationPref(); + // Copies the notifications and geolocation content settings exceptions from + // the preference that stores the content settings exceptions to the obsolete + // preference for notification and geolocation content settings exceptions. + // This is necessary to allow notifications and geolocation content settings + // exceptions being synced to older versions of chrome that only use the + // obsolete preference. + void SyncObsoletePrefs(); static void CanonicalizeContentSettingsExceptions( base::DictionaryValue* all_settings_dictionary); diff --git a/chrome/browser/content_settings/content_settings_pref_provider_unittest.cc b/chrome/browser/content_settings/content_settings_pref_provider_unittest.cc index 01177c1..e15d4fd 100644 --- a/chrome/browser/content_settings/content_settings_pref_provider_unittest.cc +++ b/chrome/browser/content_settings/content_settings_pref_provider_unittest.cc @@ -6,6 +6,7 @@ #include "base/auto_reset.h" #include "base/command_line.h" +#include "base/memory/scoped_ptr.h" #include "chrome/browser/content_settings/content_settings_mock_observer.h" #include "chrome/browser/prefs/browser_prefs.h" #include "chrome/browser/prefs/default_pref_store.h" @@ -677,7 +678,7 @@ TEST_F(PrefProviderTest, SyncObsoleteGeolocationPref) { key, settings_dictionary->DeepCopy()); key = std::string( - primary_pattern_2.ToString()+ "," + + primary_pattern_2.ToString() + "," + secondary_pattern.ToString()); all_settings_dictionary->SetWithoutPathExpansion( key, settings_dictionary->DeepCopy()); @@ -734,4 +735,140 @@ TEST_F(PrefProviderTest, AutoSubmitCertificateContentSetting) { provider.ShutdownOnUIThread(); } +TEST_F(PrefProviderTest, MigrateObsoleteNotificationsPref) { + TestingProfile profile; + PrefService* prefs = profile.GetPrefs(); + GURL allowed_url("http://www.foo.com"); + GURL allowed_url2("http://www.example.com"); + GURL denied_url("http://www.bar.com"); + + // Set obsolete preference. + scoped_ptr<ListValue> allowed_origin_list(new ListValue()); + allowed_origin_list->AppendIfNotPresent( + Value::CreateStringValue(allowed_url.spec())); + prefs->Set(prefs::kDesktopNotificationAllowedOrigins, + *allowed_origin_list); + + scoped_ptr<ListValue> denied_origin_list(new ListValue()); + denied_origin_list->AppendIfNotPresent( + Value::CreateStringValue(denied_url.spec())); + prefs->Set(prefs::kDesktopNotificationDeniedOrigins, + *denied_origin_list); + + content_settings::PrefProvider provider(prefs, false); + + // Test if the migrated settings are loaded and available. + EXPECT_EQ(CONTENT_SETTING_ALLOW, provider.GetContentSetting( + allowed_url, + allowed_url, + CONTENT_SETTINGS_TYPE_NOTIFICATIONS, + "")); + EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( + denied_url, + denied_url, + CONTENT_SETTINGS_TYPE_NOTIFICATIONS, + "")); + EXPECT_EQ(CONTENT_SETTING_DEFAULT, provider.GetContentSetting( + allowed_url2, + allowed_url2, + CONTENT_SETTINGS_TYPE_NOTIFICATIONS, + "")); + // Check if the settings where migrated correctly. + const DictionaryValue* const_all_settings_dictionary = + prefs->GetDictionary(prefs::kContentSettingsPatternPairs); + EXPECT_EQ(2U, const_all_settings_dictionary->size()); + EXPECT_TRUE(const_all_settings_dictionary->HasKey( + ContentSettingsPattern::FromURLNoWildcard(allowed_url).ToString() + "," + + ContentSettingsPattern::Wildcard().ToString())); + EXPECT_TRUE(const_all_settings_dictionary->HasKey( + ContentSettingsPattern::FromURLNoWildcard(denied_url).ToString() + "," + + ContentSettingsPattern::Wildcard().ToString())); + + // Check that notifications settings were not synced to the obsolete content + // settings pattern preference. + const DictionaryValue* const_obsolete_patterns_dictionary = + prefs->GetDictionary(prefs::kContentSettingsPatterns); + EXPECT_TRUE(const_obsolete_patterns_dictionary->empty()); + + // Change obsolete preference. This could be triggered by sync if sync is used + // with an old version of chrome. + allowed_origin_list.reset(new ListValue()); + allowed_origin_list->AppendIfNotPresent( + Value::CreateStringValue(allowed_url2.spec())); + prefs->Set(prefs::kDesktopNotificationAllowedOrigins, + *allowed_origin_list); + + // Test if the changed obsolete preference was migrated correctly. + EXPECT_EQ(CONTENT_SETTING_ALLOW, provider.GetContentSetting( + allowed_url2, + allowed_url2, + CONTENT_SETTINGS_TYPE_NOTIFICATIONS, + "")); + EXPECT_EQ(CONTENT_SETTING_DEFAULT, provider.GetContentSetting( + allowed_url, + allowed_url, + CONTENT_SETTINGS_TYPE_NOTIFICATIONS, + "")); + EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( + denied_url, + denied_url, + CONTENT_SETTINGS_TYPE_NOTIFICATIONS, + "")); + // Check that geolocation settings were not synced to the obsolete content + // settings pattern preference. + const_obsolete_patterns_dictionary = + prefs->GetDictionary(prefs::kContentSettingsPatterns); + EXPECT_TRUE(const_obsolete_patterns_dictionary->empty()); + + provider.ShutdownOnUIThread(); +} + +TEST_F(PrefProviderTest, SyncObsoleteNotificationsPref) { + TestingProfile profile; + PrefService* prefs = profile.GetPrefs(); + + content_settings::PrefProvider provider(prefs, false); + + // Changing the preferences prefs::kContentSettingsPatternPairs. + ContentSettingsPattern primary_pattern= + ContentSettingsPattern::FromString("http://www.bar.com"); + ContentSettingsPattern primary_pattern_2 = + ContentSettingsPattern::FromString("http://www.example.com"); + ContentSettingsPattern secondary_pattern = + ContentSettingsPattern::Wildcard(); + GURL primary_url("http://www.bar.com"); + GURL primary_url_2("http://www.example.com"); + + { + DictionaryPrefUpdate update(prefs, + prefs::kContentSettingsPatternPairs); + DictionaryValue* all_settings_dictionary = update.Get(); + + scoped_ptr<DictionaryValue> settings_dictionary(new DictionaryValue()); + settings_dictionary->SetInteger("notifications", CONTENT_SETTING_BLOCK); + std::string key( + primary_pattern.ToString() + "," + + secondary_pattern.ToString()); + all_settings_dictionary->SetWithoutPathExpansion( + key, settings_dictionary->DeepCopy()); + + settings_dictionary.reset(new DictionaryValue()); + settings_dictionary->SetInteger("notifications", CONTENT_SETTING_ALLOW); + key = primary_pattern_2.ToString() + "," + secondary_pattern.ToString(); + all_settings_dictionary->SetWithoutPathExpansion( + key, settings_dictionary->DeepCopy()); + } + + // Test if the obsolete notifications preference is kept in sync if the new + // preference is changed by a sync. + const ListValue* denied_origin_list = + prefs->GetList(prefs::kDesktopNotificationAllowedOrigins); + EXPECT_EQ(1U, denied_origin_list->GetSize()); + const ListValue* allowed_origin_list = + prefs->GetList(prefs::kDesktopNotificationDeniedOrigins); + EXPECT_EQ(1U, allowed_origin_list->GetSize()); + + provider.ShutdownOnUIThread(); +} + } // namespace content_settings diff --git a/chrome/browser/content_settings/host_content_settings_map.cc b/chrome/browser/content_settings/host_content_settings_map.cc index 3d15073..16ad3a2 100644 --- a/chrome/browser/content_settings/host_content_settings_map.cc +++ b/chrome/browser/content_settings/host_content_settings_map.cc @@ -38,7 +38,10 @@ namespace { // Returns true if we should allow all content types for this URL. This is // true for various internal objects like chrome:// URLs, so UI and other // things users think of as "not webpages" don't break. -static bool ShouldAllowAllContent(const GURL& url) { +static bool ShouldAllowAllContent(const GURL& url, + ContentSettingsType content_type) { + if (content_type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS) + return false; return url.SchemeIs(chrome::kChromeDevToolsScheme) || url.SchemeIs(chrome::kChromeInternalScheme) || url.SchemeIs(chrome::kChromeUIScheme) || @@ -184,7 +187,7 @@ ContentSetting HostContentSettingsMap::GetCookieContentSetting( const GURL& url, const GURL& first_party_url, bool setting_cookie) const { - if (ShouldAllowAllContent(first_party_url)) + if (ShouldAllowAllContent(first_party_url, CONTENT_SETTINGS_TYPE_COOKIES)) return CONTENT_SETTING_ALLOW; // First get any host-specific settings. @@ -245,7 +248,7 @@ ContentSetting HostContentSettingsMap::GetNonDefaultContentSetting( const GURL& secondary_url, ContentSettingsType content_type, const std::string& resource_identifier) const { - if (ShouldAllowAllContent(secondary_url)) + if (ShouldAllowAllContent(secondary_url, content_type)) return CONTENT_SETTING_ALLOW; // Iterate through the list of providers and break when the first non default @@ -284,9 +287,6 @@ ContentSettings HostContentSettingsMap::GetContentSettings( ContentSettings HostContentSettingsMap::GetNonDefaultContentSettings( const GURL& primary_url, const GURL& secondary_url) const { - if (ShouldAllowAllContent(secondary_url)) - return ContentSettings(CONTENT_SETTING_ALLOW); - ContentSettings output(CONTENT_SETTING_DEFAULT); for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { output.settings[j] = GetNonDefaultContentSetting( diff --git a/chrome/browser/content_settings/host_content_settings_map.h b/chrome/browser/content_settings/host_content_settings_map.h index fcb566b..cc7150a8 100644 --- a/chrome/browser/content_settings/host_content_settings_map.h +++ b/chrome/browser/content_settings/host_content_settings_map.h @@ -42,6 +42,8 @@ class HostContentSettingsMap public NotificationObserver, public base::RefCountedThreadSafe<HostContentSettingsMap> { public: + // TODO(markusheintz): I sold my soul to the devil on order to add this tuple. + // I really want my soul back, so I really will change this ASAP. typedef Tuple4<ContentSettingsPattern, ContentSettingsPattern, ContentSetting, diff --git a/chrome/browser/notifications/desktop_notification_service.cc b/chrome/browser/notifications/desktop_notification_service.cc index 53dce9d..bd46686 100644 --- a/chrome/browser/notifications/desktop_notification_service.cc +++ b/chrome/browser/notifications/desktop_notification_service.cc @@ -7,6 +7,8 @@ #include "base/metrics/histogram.h" #include "base/threading/thread.h" #include "base/utf_string_conversions.h" +#include "chrome/browser/content_settings/content_settings_details.h" +#include "chrome/browser/content_settings/content_settings_pattern.h" #include "chrome/browser/content_settings/content_settings_provider.h" #include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/browser/extensions/extension_service.h" @@ -14,7 +16,6 @@ #include "chrome/browser/notifications/notification.h" #include "chrome/browser/notifications/notification_object_proxy.h" #include "chrome/browser/notifications/notification_ui_manager.h" -#include "chrome/browser/notifications/notifications_prefs_cache.h" #include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/prefs/scoped_user_pref_update.h" #include "chrome/browser/profiles/profile.h" @@ -45,45 +46,6 @@ using WebKit::WebTextDirection; const ContentSetting kDefaultSetting = CONTENT_SETTING_ASK; -namespace { - -typedef content_settings::ProviderInterface::Rules Rules; - -void GetOriginsWithSettingFromContentSettingsRules( - const Rules& content_setting_rules, - ContentSetting setting, - std::vector<GURL>* origins) { - origins->clear(); - - for (Rules::const_iterator rule = content_setting_rules.begin(); - rule != content_setting_rules.end(); - ++rule) { - if (setting == rule->content_setting) { - std::string url_str = rule->primary_pattern.ToString(); - if (!rule->primary_pattern.IsValid()) { - // TODO(markusheintz): This will be removed in one of the next - // refactoring steps as this entire function will disapear. - LOG(DFATAL) << "Ignoring invalid content settings pattern: " - << url_str; - } else if (url_str.find(ContentSettingsPattern::kDomainWildcard) == 0) { - // TODO(markusheintz): This must be changed once the UI code is - // refactored and content_settings patterns are fully supported for - // notifications settings. - LOG(DFATAL) << "Ignoring unsupported content settings pattern: " - << url_str << ". Content settings patterns other than " - << "hostnames (e.g. wildcard patterns) are not supported " - << "for notification content settings yet."; - } else { - origins->push_back( - content_settings::NotificationProvider::ToGURL( - rule->primary_pattern)); - } - } - } -} - -} // namespace - // NotificationPermissionInfoBarDelegate -------------------------------------- // The delegate for the infobar shown when an origin requests notification @@ -252,8 +214,6 @@ DesktopNotificationService::DesktopNotificationService(Profile* profile, NotificationUIManager* ui_manager) : profile_(profile), ui_manager_(ui_manager) { - prefs_registrar_.Init(profile_->GetPrefs()); - InitPrefs(); StartObserving(); } @@ -261,114 +221,45 @@ DesktopNotificationService::~DesktopNotificationService() { StopObserving(); } -void DesktopNotificationService::RegisterUserPrefs(PrefService* user_prefs) { - content_settings::NotificationProvider::RegisterUserPrefs(user_prefs); -} - -// Initialize the cache with the allowed and denied origins, or -// create the preferences if they don't exist yet. -void DesktopNotificationService::InitPrefs() { - provider_.reset(new content_settings::NotificationProvider(profile_)); - - std::vector<GURL> allowed_origins; - std::vector<GURL> denied_origins; - ContentSetting default_content_setting = CONTENT_SETTING_DEFAULT; - - if (!profile_->IsOffTheRecord()) { - default_content_setting = - profile_->GetHostContentSettingsMap()->GetDefaultContentSetting( - CONTENT_SETTINGS_TYPE_NOTIFICATIONS); - allowed_origins = GetAllowedOrigins(); - denied_origins = GetBlockedOrigins(); - } - - prefs_cache_ = new NotificationsPrefsCache(); - prefs_cache_->SetCacheDefaultContentSetting(default_content_setting); - prefs_cache_->SetCacheAllowedOrigins(allowed_origins); - prefs_cache_->SetCacheDeniedOrigins(denied_origins); - prefs_cache_->set_is_initialized(true); -} - void DesktopNotificationService::StartObserving() { if (!profile_->IsOffTheRecord()) { - prefs_registrar_.Add(prefs::kDesktopNotificationAllowedOrigins, this); - prefs_registrar_.Add(prefs::kDesktopNotificationDeniedOrigins, this); notification_registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, NotificationService::AllSources()); - notification_registrar_.Add( - this, - chrome::NOTIFICATION_CONTENT_SETTINGS_CHANGED, - Source<HostContentSettingsMap>(profile_->GetHostContentSettingsMap())); } notification_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, Source<Profile>(profile_)); } void DesktopNotificationService::StopObserving() { - if (!profile_->IsOffTheRecord()) { - prefs_registrar_.RemoveAll(); - } notification_registrar_.RemoveAll(); } void DesktopNotificationService::GrantPermission(const GURL& origin) { - ContentSettingsPattern pattern = - content_settings::NotificationProvider::ToContentSettingsPattern(origin); - provider_->SetContentSetting( - pattern, - pattern, + ContentSettingsPattern primary_pattern = + ContentSettingsPattern::FromURLNoWildcard(origin); + profile_->GetHostContentSettingsMap()->SetContentSetting( + primary_pattern, + ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS, NO_RESOURCE_IDENTIFIER, CONTENT_SETTING_ALLOW); - - // Schedule a cache update on the IO thread. - BrowserThread::PostTask( - BrowserThread::IO, FROM_HERE, - NewRunnableMethod( - prefs_cache_.get(), - &NotificationsPrefsCache::CacheAllowedOrigin, - origin)); } void DesktopNotificationService::DenyPermission(const GURL& origin) { - // Update content settings - ContentSettingsPattern pattern = - content_settings::NotificationProvider::ToContentSettingsPattern(origin); - provider_->SetContentSetting( - pattern, - pattern, + ContentSettingsPattern primary_pattern = + ContentSettingsPattern::FromURLNoWildcard(origin); + profile_->GetHostContentSettingsMap()->SetContentSetting( + primary_pattern, + ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS, NO_RESOURCE_IDENTIFIER, CONTENT_SETTING_BLOCK); - - // Schedule a cache update on the IO thread. - BrowserThread::PostTask( - BrowserThread::IO, FROM_HERE, - NewRunnableMethod( - prefs_cache_.get(), - &NotificationsPrefsCache::CacheDeniedOrigin, - origin)); } void DesktopNotificationService::Observe(int type, const NotificationSource& source, const NotificationDetails& details) { - if (chrome::NOTIFICATION_PREF_CHANGED == type) { - const std::string& name = *Details<std::string>(details).ptr(); - OnPrefsChanged(name); - } else if (chrome::NOTIFICATION_CONTENT_SETTINGS_CHANGED == type) { - // TODO(markusheintz): Check if content settings type default was changed; - const ContentSetting default_content_setting = - profile_->GetHostContentSettingsMap()->GetDefaultContentSetting( - CONTENT_SETTINGS_TYPE_NOTIFICATIONS); - // Schedule a cache update on the IO thread. - BrowserThread::PostTask( - BrowserThread::IO, FROM_HERE, - NewRunnableMethod( - prefs_cache_.get(), - &NotificationsPrefsCache::SetCacheDefaultContentSetting, - default_content_setting)); - } else if (chrome::NOTIFICATION_EXTENSION_UNLOADED == type) { + if (chrome::NOTIFICATION_EXTENSION_UNLOADED == type) { // Remove all notifications currently shown or queued by the extension // which was unloaded. const Extension* extension = @@ -380,30 +271,6 @@ void DesktopNotificationService::Observe(int type, } } -void DesktopNotificationService::OnPrefsChanged(const std::string& pref_name) { - if (pref_name == prefs::kDesktopNotificationAllowedOrigins) { - // Schedule a cache update on the IO thread. - std::vector<GURL> allowed_origins(GetAllowedOrigins()); - BrowserThread::PostTask( - BrowserThread::IO, FROM_HERE, - NewRunnableMethod( - prefs_cache_.get(), - &NotificationsPrefsCache::SetCacheAllowedOrigins, - allowed_origins)); - } else if (pref_name == prefs::kDesktopNotificationDeniedOrigins) { - // Schedule a cache update on the IO thread. - std::vector<GURL> denied_origins(GetBlockedOrigins()); - BrowserThread::PostTask( - BrowserThread::IO, FROM_HERE, - NewRunnableMethod( - prefs_cache_.get(), - &NotificationsPrefsCache::SetCacheDeniedOrigins, - denied_origins)); - } else { - NOTREACHED(); - } -} - ContentSetting DesktopNotificationService::GetDefaultContentSetting() { return profile_->GetHostContentSettingsMap()->GetDefaultContentSetting( CONTENT_SETTINGS_TYPE_NOTIFICATIONS); @@ -425,70 +292,36 @@ void DesktopNotificationService::ResetToDefaultContentSetting() { CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_DEFAULT); } -std::vector<GURL> DesktopNotificationService::GetAllowedOrigins() { - content_settings::ProviderInterface::Rules content_setting_rules; - provider_->GetAllContentSettingsRules( - CONTENT_SETTINGS_TYPE_NOTIFICATIONS, - NO_RESOURCE_IDENTIFIER, - &content_setting_rules); - std::vector<GURL> allowed_origins; - - GetOriginsWithSettingFromContentSettingsRules( - content_setting_rules, CONTENT_SETTING_ALLOW, &allowed_origins); - - return allowed_origins; -} - -std::vector<GURL> DesktopNotificationService::GetBlockedOrigins() { - content_settings::ProviderInterface::Rules content_settings_rules; - provider_->GetAllContentSettingsRules( - CONTENT_SETTINGS_TYPE_NOTIFICATIONS, - NO_RESOURCE_IDENTIFIER, - &content_settings_rules); - std::vector<GURL> denied_origins; - - GetOriginsWithSettingFromContentSettingsRules( - content_settings_rules, CONTENT_SETTING_BLOCK, &denied_origins); - - return denied_origins; -} - -void DesktopNotificationService::ResetAllowedOrigin(const GURL& origin) { - ContentSettingsPattern pattern = - ContentSettingsPattern::FromURLNoWildcard(origin); - provider_->SetContentSetting( - pattern, - pattern, +void DesktopNotificationService::GetNotificationsSettings( + HostContentSettingsMap::SettingsForOneType* settings) { + profile_->GetHostContentSettingsMap()->GetSettingsForOneType( CONTENT_SETTINGS_TYPE_NOTIFICATIONS, NO_RESOURCE_IDENTIFIER, - CONTENT_SETTING_DEFAULT); + settings); } -void DesktopNotificationService::ResetBlockedOrigin(const GURL& origin) { - ContentSettingsPattern pattern = - ContentSettingsPattern::FromURLNoWildcard(origin); - provider_->SetContentSetting( - pattern, +void DesktopNotificationService::ClearSetting( + const ContentSettingsPattern& pattern) { + profile_->GetHostContentSettingsMap()->SetContentSetting( pattern, + ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS, NO_RESOURCE_IDENTIFIER, CONTENT_SETTING_DEFAULT); } void DesktopNotificationService::ResetAllOrigins() { - provider_->ClearAllContentSettingsRules(CONTENT_SETTINGS_TYPE_NOTIFICATIONS); + profile_->GetHostContentSettingsMap()->ClearSettingsForOneType( + CONTENT_SETTINGS_TYPE_NOTIFICATIONS); } ContentSetting DesktopNotificationService::GetContentSetting( const GURL& origin) { - ContentSetting provided_setting = provider_->GetContentSetting( + return profile_->GetHostContentSettingsMap()->GetContentSetting( origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, NO_RESOURCE_IDENTIFIER); - if (CONTENT_SETTING_DEFAULT == provided_setting) - return GetDefaultContentSetting(); - return provided_setting; } void DesktopNotificationService::RequestPermission( @@ -539,7 +372,6 @@ bool DesktopNotificationService::CancelDesktopNotification( return ui_manager_->CancelById(proxy->id()); } - bool DesktopNotificationService::ShowDesktopNotification( const DesktopNotificationHostMsg_Show_Params& params, int process_id, int route_id, DesktopNotificationSource source) { @@ -589,5 +421,20 @@ void DesktopNotificationService::NotifySettingsChange() { WebKit::WebNotificationPresenter::Permission DesktopNotificationService::HasPermission(const GURL& origin) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); - return prefs_cache()->HasPermission(origin.GetOrigin()); + HostContentSettingsMap* host_content_settings_map = + profile_->GetHostContentSettingsMap(); + ContentSetting setting = host_content_settings_map->GetContentSetting( + origin, + origin, + CONTENT_SETTINGS_TYPE_NOTIFICATIONS, + NO_RESOURCE_IDENTIFIER); + + if (setting == CONTENT_SETTING_ALLOW) + return WebKit::WebNotificationPresenter::PermissionAllowed; + if (setting == CONTENT_SETTING_BLOCK) + return WebKit::WebNotificationPresenter::PermissionDenied; + if (setting == CONTENT_SETTING_ASK) + return WebKit::WebNotificationPresenter::PermissionNotAllowed; + NOTREACHED() << "Invalid notifications settings value: " << setting; + return WebKit::WebNotificationPresenter::PermissionNotAllowed; } diff --git a/chrome/browser/notifications/desktop_notification_service.h b/chrome/browser/notifications/desktop_notification_service.h index 4ef69fd..93b3a4c 100644 --- a/chrome/browser/notifications/desktop_notification_service.h +++ b/chrome/browser/notifications/desktop_notification_service.h @@ -13,9 +13,8 @@ #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" #include "base/string16.h" -#include "chrome/browser/content_settings/content_settings_notification_provider.h" #include "chrome/browser/content_settings/content_settings_provider.h" -#include "chrome/browser/prefs/pref_change_registrar.h" +#include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/browser/profiles/profile_keyed_service.h" #include "chrome/common/content_settings.h" #include "content/common/notification_observer.h" @@ -24,11 +23,11 @@ #include "third_party/WebKit/Source/WebKit/chromium/public/WebNotificationPresenter.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextDirection.h" +class ContentSettingsPattern; class Extension; class Notification; class NotificationDelegate; class NotificationUIManager; -class NotificationsPrefsCache; class PrefService; class Profile; class TabContents; @@ -83,8 +82,6 @@ class DesktopNotificationService : public NotificationObserver, const NotificationSource& source, const NotificationDetails& details); - NotificationsPrefsCache* prefs_cache() { return prefs_cache_; } - // Creates a data:xxxx URL which contains the full HTML for a notification // using supplied icon, title, and text, run through a template which contains // the standard formatting for notifications. @@ -108,23 +105,17 @@ class DesktopNotificationService : public NotificationObserver, // NOTE: This should only be called on the UI thread. void ResetToDefaultContentSetting(); - // Returns all origins that explicitly have been allowed. - std::vector<GURL> GetAllowedOrigins(); - - // Returns all origins that explicitly have been denied. - std::vector<GURL> GetBlockedOrigins(); + // Returns all notifications settings. |settings| is cleared before + // notifications setting are passed to it. + void GetNotificationsSettings( + HostContentSettingsMap::SettingsForOneType* settings); - // Removes an origin from the "explicitly allowed" set. - void ResetAllowedOrigin(const GURL& origin); - - // Removes an origin from the "explicitly denied" set. - void ResetBlockedOrigin(const GURL& origin); + // Clears the notifications setting for the given pattern. + void ClearSetting(const ContentSettingsPattern& pattern); // Clears the sets of explicitly allowed and denied origins. void ResetAllOrigins(); - static void RegisterUserPrefs(PrefService* user_prefs); - ContentSetting GetContentSetting(const GURL& origin); // Checks to see if a given origin has permission to create desktop @@ -133,12 +124,9 @@ class DesktopNotificationService : public NotificationObserver, HasPermission(const GURL& origin); private: - void InitPrefs(); void StartObserving(); void StopObserving(); - void OnPrefsChanged(const std::string& pref_name); - // Takes a notification object and shows it in the UI. void ShowNotification(const Notification& notification); @@ -153,17 +141,10 @@ class DesktopNotificationService : public NotificationObserver, // The profile which owns this object. Profile* profile_; - // A cache of preferences which is accessible only on the IO thread - // to service synchronous IPCs. - scoped_refptr<NotificationsPrefsCache> prefs_cache_; - // Non-owned pointer to the notification manager which manages the // UI for desktop toasts. NotificationUIManager* ui_manager_; - scoped_ptr<content_settings::NotificationProvider> provider_; - - PrefChangeRegistrar prefs_registrar_; NotificationRegistrar notification_registrar_; DISALLOW_COPY_AND_ASSIGN(DesktopNotificationService); diff --git a/chrome/browser/notifications/desktop_notification_service_unittest.cc b/chrome/browser/notifications/desktop_notification_service_unittest.cc index 17e8f1c..84d1b06 100644 --- a/chrome/browser/notifications/desktop_notification_service_unittest.cc +++ b/chrome/browser/notifications/desktop_notification_service_unittest.cc @@ -4,31 +4,27 @@ #include "chrome/browser/notifications/desktop_notification_service.h" -#include "base/memory/ref_counted.h" +#include "base/memory/scoped_ptr.h" #include "base/message_loop.h" #include "base/synchronization/waitable_event.h" +#include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/browser/notifications/desktop_notification_service_factory.h" -#include "chrome/browser/notifications/notifications_prefs_cache.h" -#include "chrome/browser/prefs/pref_service.h" -#include "chrome/browser/prefs/scoped_user_pref_update.h" -#include "chrome/common/pref_names.h" #include "chrome/test/base/testing_profile.h" #include "content/browser/browser_thread.h" #include "content/browser/renderer_host/test_render_view_host.h" -#include "grit/generated_resources.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebNotificationPresenter.h" namespace { -// NotificationsPrefsCache wants to be called on the IO thread. This class -// routes calls to the cache on the IO thread. +// The HasPermission method of the DesktopNotificationService wants to be called +// on the IO thread. This class routes calls to the cache on the IO thread. class ThreadProxy : public base::RefCountedThreadSafe<ThreadProxy> { public: ThreadProxy() : io_event_(false, false), ui_event_(false, false), - permission_(0) { + permission_(WebKit::WebNotificationPresenter::PermissionAllowed) { // The current message loop was already initalized by the test superclass. ui_thread_.reset( new BrowserThread(BrowserThread::UI, MessageLoop::current())); @@ -41,11 +37,15 @@ class ThreadProxy : public base::RefCountedThreadSafe<ThreadProxy> { // could complete before the constructor is done, deleting |this|. } - int CacheHasPermission(NotificationsPrefsCache* cache, const GURL& url) { + // Call the HasPermission method of the DesktopNotificationService on the IO + // thread and returns the permission setting. + WebKit::WebNotificationPresenter::Permission ServiceHasPermission( + DesktopNotificationService* service, + const GURL& url) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, - NewRunnableMethod(this, &ThreadProxy::CacheHasPermissionIO, - make_scoped_refptr(cache), url)); + NewRunnableMethod(this, &ThreadProxy::ServiceHasPermissionIO, + service, url)); io_event_.Signal(); ui_event_.Wait(); // Wait for IO thread to be done. BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, @@ -76,9 +76,10 @@ class ThreadProxy : public base::RefCountedThreadSafe<ThreadProxy> { io_event_.Wait(); } - void CacheHasPermissionIO(NotificationsPrefsCache* cache, const GURL& url) { + void ServiceHasPermissionIO(DesktopNotificationService* service, + const GURL& url) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); - permission_ = cache->HasPermission(url); + permission_ = service->HasPermission(url); ui_event_.Signal(); } @@ -87,9 +88,10 @@ class ThreadProxy : public base::RefCountedThreadSafe<ThreadProxy> { scoped_ptr<BrowserThread> ui_thread_; scoped_ptr<BrowserThread> io_thread_; - int permission_; + WebKit::WebNotificationPresenter::Permission permission_; }; +} // namespace class DesktopNotificationServiceTest : public RenderViewHostTestHarness { public: @@ -101,10 +103,8 @@ class DesktopNotificationServiceTest : public RenderViewHostTestHarness { proxy_ = new ThreadProxy; proxy_->PauseIOThread(); - // Creates the service, calls InitPrefs() on it which loads data from the - // profile into the cache and then puts the cache in io thread mode. + // Creates the destop notification service. service_ = DesktopNotificationServiceFactory::GetForProfile(profile()); - cache_ = service_->prefs_cache(); } virtual void TearDown() { @@ -115,170 +115,75 @@ class DesktopNotificationServiceTest : public RenderViewHostTestHarness { } DesktopNotificationService* service_; - NotificationsPrefsCache* cache_; scoped_refptr<ThreadProxy> proxy_; }; -TEST_F(DesktopNotificationServiceTest, DefaultContentSettingSentToCache) { - // The default pref registered in DesktopNotificationService is "ask", - // and that's what sent to the cache. - EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting()); - - // Change the default content setting. This will post a task on the IO thread - // to update the cache. - service_->SetDefaultContentSetting(CONTENT_SETTING_BLOCK); - - // The updated pref shouldn't be sent to the cache immediately. - EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting()); - - // Run IO thread tasks. - proxy_->DrainIOThread(); - - // Now that IO thread events have been processed, it should be there. - EXPECT_EQ(CONTENT_SETTING_BLOCK, cache_->CachedDefaultContentSetting()); -} - TEST_F(DesktopNotificationServiceTest, SettingsForSchemes) { GURL url("file:///html/test.html"); - EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting()); + EXPECT_EQ(CONTENT_SETTING_ASK, + service_->GetDefaultContentSetting()); EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, - proxy_->CacheHasPermission(cache_, url)); + proxy_->ServiceHasPermission(service_, url)); service_->GrantPermission(url); EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, - proxy_->CacheHasPermission(cache_, url)); + proxy_->ServiceHasPermission(service_, url)); service_->DenyPermission(url); EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, - proxy_->CacheHasPermission(cache_, url)); + proxy_->ServiceHasPermission(service_, url)); GURL https_url("https://testurl"); GURL http_url("http://testurl"); - EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting()); + EXPECT_EQ(CONTENT_SETTING_ASK, + service_->GetDefaultContentSetting()); EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, - proxy_->CacheHasPermission(cache_, http_url)); + proxy_->ServiceHasPermission(service_, http_url)); EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, - proxy_->CacheHasPermission(cache_, https_url)); + proxy_->ServiceHasPermission(service_, https_url)); service_->GrantPermission(https_url); - EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, - proxy_->CacheHasPermission(cache_, https_url)); EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, - proxy_->CacheHasPermission(cache_, http_url)); + proxy_->ServiceHasPermission(service_, http_url)); + EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, + proxy_->ServiceHasPermission(service_, https_url)); service_->DenyPermission(http_url); EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, - proxy_->CacheHasPermission(cache_, http_url)); + proxy_->ServiceHasPermission(service_, http_url)); EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, - proxy_->CacheHasPermission(cache_, https_url)); + proxy_->ServiceHasPermission(service_, https_url)); } -TEST_F(DesktopNotificationServiceTest, GrantPermissionSentToCache) { - GURL url("http://allowed.com"); - EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, - proxy_->CacheHasPermission(cache_, url)); - - service_->GrantPermission(url); - - EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, - proxy_->CacheHasPermission(cache_, url)); -} - -TEST_F(DesktopNotificationServiceTest, DenyPermissionSentToCache) { - GURL url("http://denied.com"); - EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, - proxy_->CacheHasPermission(cache_, url)); - - service_->DenyPermission(url); - - EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, - proxy_->CacheHasPermission(cache_, url)); -} - -TEST_F(DesktopNotificationServiceTest, PrefChangesSentToCache) { - PrefService* prefs = profile()->GetPrefs(); - - { - ListPrefUpdate update_allowed_origins( - prefs, prefs::kDesktopNotificationAllowedOrigins); - ListValue* allowed_origins = update_allowed_origins.Get(); - allowed_origins->Append(new StringValue(GURL("http://allowed.com").spec())); - } - - { - ListPrefUpdate update_denied_origins( - prefs, prefs::kDesktopNotificationDeniedOrigins); - ListValue* denied_origins = update_denied_origins.Get(); - denied_origins->Append(new StringValue(GURL("http://denied.com").spec())); - } - - EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, - proxy_->CacheHasPermission(cache_, GURL("http://allowed.com"))); - EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, - proxy_->CacheHasPermission(cache_, GURL("http://denied.com"))); -} - -TEST_F(DesktopNotificationServiceTest, GetAllowedOrigins) { +TEST_F(DesktopNotificationServiceTest, GetNotificationsSettings) { service_->GrantPermission(GURL("http://allowed2.com")); service_->GrantPermission(GURL("http://allowed.com")); - - std::vector<GURL> allowed_origins(service_->GetAllowedOrigins()); - ASSERT_EQ(2u, allowed_origins.size()); - EXPECT_EQ(GURL("http://allowed2.com"), allowed_origins[0]); - EXPECT_EQ(GURL("http://allowed.com"), allowed_origins[1]); -} - -TEST_F(DesktopNotificationServiceTest, GetBlockedOrigins) { service_->DenyPermission(GURL("http://denied2.com")); service_->DenyPermission(GURL("http://denied.com")); - std::vector<GURL> denied_origins(service_->GetBlockedOrigins()); - ASSERT_EQ(2u, denied_origins.size()); - EXPECT_EQ(GURL("http://denied2.com"), denied_origins[0]); - EXPECT_EQ(GURL("http://denied.com"), denied_origins[1]); -} - -TEST_F(DesktopNotificationServiceTest, ResetAllSentToCache) { - GURL allowed_url("http://allowed.com"); - service_->GrantPermission(allowed_url); - EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, - proxy_->CacheHasPermission(cache_, allowed_url)); - GURL denied_url("http://denied.com"); - service_->DenyPermission(denied_url); - EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, - proxy_->CacheHasPermission(cache_, denied_url)); - - service_->ResetAllOrigins(); - - EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, - proxy_->CacheHasPermission(cache_, allowed_url)); - EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, - proxy_->CacheHasPermission(cache_, denied_url)); -} - -TEST_F(DesktopNotificationServiceTest, ResetAllowedSentToCache) { - GURL allowed_url("http://allowed.com"); - service_->GrantPermission(allowed_url); - EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, - proxy_->CacheHasPermission(cache_, allowed_url)); - - service_->ResetAllowedOrigin(allowed_url); - - EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, - proxy_->CacheHasPermission(cache_, allowed_url)); -} - -TEST_F(DesktopNotificationServiceTest, ResetBlockedSentToCache) { - GURL denied_url("http://denied.com"); - service_->DenyPermission(denied_url); - EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, - proxy_->CacheHasPermission(cache_, denied_url)); - - service_->ResetBlockedOrigin(denied_url); - - EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, - proxy_->CacheHasPermission(cache_, denied_url)); + HostContentSettingsMap::SettingsForOneType settings; + service_->GetNotificationsSettings(&settings); + ASSERT_EQ(4u, settings.size()); + + EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard( + GURL("http://allowed.com")), + settings[0].a); + EXPECT_EQ(CONTENT_SETTING_ALLOW, + settings[0].c); + EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard( + GURL("http://allowed2.com")), + settings[1].a); + EXPECT_EQ(CONTENT_SETTING_ALLOW, + settings[1].c); + EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard( + GURL("http://denied.com")), + settings[2].a); + EXPECT_EQ(CONTENT_SETTING_BLOCK, + settings[2].c); + EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard( + GURL("http://denied2.com")), + settings[3].a); + EXPECT_EQ(CONTENT_SETTING_BLOCK, + settings[3].c); } - -} // namespace diff --git a/chrome/browser/notifications/desktop_notifications_unittest.h b/chrome/browser/notifications/desktop_notifications_unittest.h index f2f2eb2a..1c71ce2 100644 --- a/chrome/browser/notifications/desktop_notifications_unittest.h +++ b/chrome/browser/notifications/desktop_notifications_unittest.h @@ -15,7 +15,6 @@ #include "chrome/browser/notifications/notification.h" #include "chrome/browser/notifications/notification_test_util.h" #include "chrome/browser/notifications/notification_ui_manager.h" -#include "chrome/browser/notifications/notifications_prefs_cache.h" #include "chrome/test/base/testing_pref_service.h" #include "chrome/test/base/testing_profile.h" #include "content/browser/browser_thread.h" @@ -91,10 +90,6 @@ class DesktopNotificationsTest : public testing::Test { service_->DenyPermission(origin); } - int HasPermission(const GURL& origin) { - return service_->prefs_cache()->HasPermission(origin); - } - // Constructs a notification parameter structure for use in tests. DesktopNotificationHostMsg_Show_Params StandardTestNotification(); diff --git a/chrome/browser/notifications/notification_exceptions_table_model.cc b/chrome/browser/notifications/notification_exceptions_table_model.cc index 71ff312..943672b 100644 --- a/chrome/browser/notifications/notification_exceptions_table_model.cc +++ b/chrome/browser/notifications/notification_exceptions_table_model.cc @@ -4,10 +4,15 @@ #include "chrome/browser/notifications/notification_exceptions_table_model.h" +#include <algorithm> +#include <string> + #include "base/auto_reset.h" +#include "base/utf_string_conversions.h" +#include "chrome/browser/content_settings/content_settings_pattern.h" +#include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/common/chrome_notification_types.h" #include "chrome/common/content_settings.h" -#include "chrome/common/content_settings_helper.h" #include "chrome/common/content_settings_types.h" #include "chrome/common/url_constants.h" #include "content/common/notification_service.h" @@ -16,10 +21,10 @@ #include "ui/base/models/table_model_observer.h" struct NotificationExceptionsTableModel::Entry { - Entry(const GURL& origin, ContentSetting setting); + Entry(const ContentSettingsPattern& origin, ContentSetting setting); bool operator<(const Entry& b) const; - GURL origin; + ContentSettingsPattern origin; ContentSetting setting; }; @@ -47,12 +52,9 @@ void NotificationExceptionsTableModel::RemoveRows(const Rows& rows) { for (Rows::const_reverse_iterator i(rows.rbegin()); i != rows.rend(); ++i) { size_t row = *i; Entry* entry = &entries_[row]; - if (entry->setting == CONTENT_SETTING_ALLOW) { - service_->ResetAllowedOrigin(entry->origin); - } else { - DCHECK_EQ(entry->setting, CONTENT_SETTING_BLOCK); - service_->ResetBlockedOrigin(entry->origin); - } + DCHECK(entry->setting == CONTENT_SETTING_ALLOW || + entry->setting == CONTENT_SETTING_BLOCK); + service_->ClearSetting(entry->origin); entries_.erase(entries_.begin() + row); // Note: |entry| is now garbage. if (observer_) observer_->OnItemsRemoved(row, 1); @@ -75,7 +77,7 @@ string16 NotificationExceptionsTableModel::GetText(int row, int column_id) { const Entry& entry = entries_[row]; if (column_id == IDS_EXCEPTIONS_HOSTNAME_HEADER) { - return content_settings_helper::OriginToString16(entry.origin); + return UTF8ToUTF16(entry.origin.ToString()); } if (column_id == IDS_EXCEPTIONS_ACTION_HEADER) { @@ -114,18 +116,22 @@ void NotificationExceptionsTableModel::Observe( } void NotificationExceptionsTableModel::LoadEntries() { - std::vector<GURL> allowed(service_->GetAllowedOrigins()); - std::vector<GURL> blocked(service_->GetBlockedOrigins()); - entries_.reserve(allowed.size() + blocked.size()); - for (size_t i = 0; i < allowed.size(); ++i) - entries_.push_back(Entry(allowed[i], CONTENT_SETTING_ALLOW)); - for (size_t i = 0; i < blocked.size(); ++i) - entries_.push_back(Entry(blocked[i], CONTENT_SETTING_BLOCK)); + HostContentSettingsMap::SettingsForOneType settings; + service_->GetNotificationsSettings(&settings); + + entries_.reserve(settings.size()); + for (HostContentSettingsMap::SettingsForOneType::const_iterator i = + settings.begin(); + i != settings.end(); + ++i) { + const HostContentSettingsMap::PatternSettingSourceTuple& tuple(*i); + entries_.push_back(Entry(tuple.a, tuple.c)); + } std::sort(entries_.begin(), entries_.end()); } NotificationExceptionsTableModel::Entry::Entry( - const GURL& in_origin, + const ContentSettingsPattern& in_origin, ContentSetting in_setting) : origin(in_origin), setting(in_setting) { @@ -134,5 +140,5 @@ NotificationExceptionsTableModel::Entry::Entry( bool NotificationExceptionsTableModel::Entry::operator<( const NotificationExceptionsTableModel::Entry& b) const { DCHECK_NE(origin, b.origin); - return origin < b.origin; + return origin.ToString() < b.origin.ToString(); } diff --git a/chrome/browser/notifications/notification_exceptions_table_model_unittest.cc b/chrome/browser/notifications/notification_exceptions_table_model_unittest.cc index 956d702..3650cbd 100644 --- a/chrome/browser/notifications/notification_exceptions_table_model_unittest.cc +++ b/chrome/browser/notifications/notification_exceptions_table_model_unittest.cc @@ -60,42 +60,43 @@ TEST_F(NotificationExceptionsTableModelTest, CanCreate) { TEST_F(NotificationExceptionsTableModelTest, RemoveAll) { FillData(); - EXPECT_EQ(2u, service_->GetAllowedOrigins().size()); - EXPECT_EQ(3u, service_->GetBlockedOrigins().size()); + HostContentSettingsMap::SettingsForOneType settings; + service_->GetNotificationsSettings(&settings); + EXPECT_EQ(5u, settings.size()); EXPECT_EQ(5, model_->RowCount()); model_->RemoveAll(); EXPECT_EQ(0, model_->RowCount()); - EXPECT_EQ(0u, service_->GetAllowedOrigins().size()); - EXPECT_EQ(0u, service_->GetBlockedOrigins().size()); + service_->GetNotificationsSettings(&settings); + EXPECT_EQ(0u, settings.size()); } TEST_F(NotificationExceptionsTableModelTest, AlphabeticalOrder) { FillData(); EXPECT_EQ(5, model_->RowCount()); - EXPECT_EQ(ASCIIToUTF16("allowed.com"), + EXPECT_EQ(ASCIIToUTF16("http://allowed.com:80"), model_->GetText(0, IDS_EXCEPTIONS_HOSTNAME_HEADER)); EXPECT_EQ(l10n_util::GetStringUTF16(IDS_EXCEPTIONS_ALLOW_BUTTON), model_->GetText(0, IDS_EXCEPTIONS_ACTION_HEADER)); - EXPECT_EQ(ASCIIToUTF16("denied.com"), + EXPECT_EQ(ASCIIToUTF16("http://denied.com:80"), model_->GetText(1, IDS_EXCEPTIONS_HOSTNAME_HEADER)); EXPECT_EQ(l10n_util::GetStringUTF16(IDS_EXCEPTIONS_BLOCK_BUTTON), model_->GetText(1, IDS_EXCEPTIONS_ACTION_HEADER)); - EXPECT_EQ(ASCIIToUTF16("denied2.com"), + EXPECT_EQ(ASCIIToUTF16("http://denied2.com:80"), model_->GetText(2, IDS_EXCEPTIONS_HOSTNAME_HEADER)); EXPECT_EQ(l10n_util::GetStringUTF16(IDS_EXCEPTIONS_BLOCK_BUTTON), model_->GetText(2, IDS_EXCEPTIONS_ACTION_HEADER)); - EXPECT_EQ(ASCIIToUTF16("e-allowed2.com"), + EXPECT_EQ(ASCIIToUTF16("http://e-allowed2.com:80"), model_->GetText(3, IDS_EXCEPTIONS_HOSTNAME_HEADER)); EXPECT_EQ(l10n_util::GetStringUTF16(IDS_EXCEPTIONS_ALLOW_BUTTON), model_->GetText(3, IDS_EXCEPTIONS_ACTION_HEADER)); - EXPECT_EQ(ASCIIToUTF16("f-denied3.com"), + EXPECT_EQ(ASCIIToUTF16("http://f-denied3.com:80"), model_->GetText(4, IDS_EXCEPTIONS_HOSTNAME_HEADER)); EXPECT_EQ(l10n_util::GetStringUTF16(IDS_EXCEPTIONS_BLOCK_BUTTON), model_->GetText(4, IDS_EXCEPTIONS_ACTION_HEADER)); @@ -112,8 +113,10 @@ TEST_F(NotificationExceptionsTableModelTest, RemoveRows) { model_->RemoveRows(rows); } EXPECT_EQ(3, model_->RowCount()); - EXPECT_EQ(0u, service_->GetAllowedOrigins().size()); - EXPECT_EQ(3u, service_->GetBlockedOrigins().size()); + + HostContentSettingsMap::SettingsForOneType settings; + service_->GetNotificationsSettings(&settings); + EXPECT_EQ(3u, settings.size()); { RemoveRowsTableModel::Rows rows; @@ -123,6 +126,6 @@ TEST_F(NotificationExceptionsTableModelTest, RemoveRows) { model_->RemoveRows(rows); } EXPECT_EQ(0, model_->RowCount()); - EXPECT_EQ(0u, service_->GetAllowedOrigins().size()); - EXPECT_EQ(0u, service_->GetBlockedOrigins().size()); + service_->GetNotificationsSettings(&settings); + EXPECT_EQ(0u, settings.size()); } diff --git a/chrome/browser/notifications/notification_options_menu_model.cc b/chrome/browser/notifications/notification_options_menu_model.cc index bce7b9c..0ccb66b 100644 --- a/chrome/browser/notifications/notification_options_menu_model.cc +++ b/chrome/browser/notifications/notification_options_menu_model.cc @@ -4,6 +4,8 @@ #include "chrome/browser/notifications/notification_options_menu_model.h" +#include <string> + #include "base/compiler_specific.h" #include "base/logging.h" #include "base/utf_string_conversions.h" @@ -14,7 +16,6 @@ #include "chrome/browser/notifications/desktop_notification_service_factory.h" #include "chrome/browser/notifications/notification.h" #include "chrome/browser/notifications/notification_ui_manager.h" -#include "chrome/browser/notifications/notifications_prefs_cache.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser_list.h" #include "chrome/common/chrome_switches.h" diff --git a/chrome/browser/notifications/notifications_prefs_cache.cc b/chrome/browser/notifications/notifications_prefs_cache.cc deleted file mode 100644 index cacf136..0000000 --- a/chrome/browser/notifications/notifications_prefs_cache.cc +++ /dev/null @@ -1,104 +0,0 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/notifications/notifications_prefs_cache.h" - -#include <string> - -#include "base/string_util.h" -#include "base/values.h" -#include "base/utf_string_conversions.h" -#include "content/browser/browser_thread.h" - -NotificationsPrefsCache::NotificationsPrefsCache() - : default_content_setting_(CONTENT_SETTING_DEFAULT), - is_initialized_(false) { -} - -void NotificationsPrefsCache::CacheAllowedOrigin( - const GURL& origin) { - CheckThreadAccess(); - std::set<GURL>::iterator iter; - allowed_origins_.insert(origin); - if ((iter = denied_origins_.find(origin)) != denied_origins_.end()) - denied_origins_.erase(iter); -} - -void NotificationsPrefsCache::CacheDeniedOrigin( - const GURL& origin) { - CheckThreadAccess(); - std::set<GURL>::iterator iter; - denied_origins_.insert(origin); - if ((iter = allowed_origins_.find(origin)) != allowed_origins_.end()) - allowed_origins_.erase(iter); -} - -void NotificationsPrefsCache::SetCacheAllowedOrigins( - const std::vector<GURL>& allowed) { - allowed_origins_.clear(); - allowed_origins_.insert(allowed.begin(), allowed.end()); -} - -void NotificationsPrefsCache::SetCacheDeniedOrigins( - const std::vector<GURL>& denied) { - denied_origins_.clear(); - denied_origins_.insert(denied.begin(), denied.end()); -} - -void NotificationsPrefsCache::SetCacheDefaultContentSetting( - ContentSetting setting) { - default_content_setting_ = setting; -} - -// static -void NotificationsPrefsCache::ListValueToGurlVector( - const ListValue& origin_list, - std::vector<GURL>* origin_vector) { - ListValue::const_iterator i; - std::string origin; - for (i = origin_list.begin(); i != origin_list.end(); ++i) { - (*i)->GetAsString(&origin); - origin_vector->push_back(GURL(origin)); - } -} - -WebKit::WebNotificationPresenter::Permission - NotificationsPrefsCache::HasPermission(const GURL& origin) { - if (IsOriginAllowed(origin)) - return WebKit::WebNotificationPresenter::PermissionAllowed; - if (IsOriginDenied(origin)) - return WebKit::WebNotificationPresenter::PermissionDenied; - switch (default_content_setting_) { - case CONTENT_SETTING_ALLOW: - return WebKit::WebNotificationPresenter::PermissionAllowed; - case CONTENT_SETTING_BLOCK: - return WebKit::WebNotificationPresenter::PermissionDenied; - case CONTENT_SETTING_ASK: - case CONTENT_SETTING_DEFAULT: - default: // Make gcc happy. - return WebKit::WebNotificationPresenter::PermissionNotAllowed; - } -} - -NotificationsPrefsCache::~NotificationsPrefsCache() {} - -bool NotificationsPrefsCache::IsOriginAllowed( - const GURL& origin) { - CheckThreadAccess(); - return allowed_origins_.find(origin) != allowed_origins_.end(); -} - -bool NotificationsPrefsCache::IsOriginDenied( - const GURL& origin) { - CheckThreadAccess(); - return denied_origins_.find(origin) != denied_origins_.end(); -} - -void NotificationsPrefsCache::CheckThreadAccess() { - if (is_initialized_) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); - } else { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - } -} diff --git a/chrome/browser/notifications/notifications_prefs_cache.h b/chrome/browser/notifications/notifications_prefs_cache.h deleted file mode 100644 index de65a3e..0000000 --- a/chrome/browser/notifications/notifications_prefs_cache.h +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_NOTIFICATIONS_NOTIFICATIONS_PREFS_CACHE_H_ -#define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATIONS_PREFS_CACHE_H_ -#pragma once - -#include <set> -#include <vector> - -#include "base/memory/ref_counted.h" -#include "chrome/common/content_settings.h" -#include "googleurl/src/gurl.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/WebNotificationPresenter.h" - -namespace base { -class ListValue; -} - -// Class which caches notification preferences. -// Construction occurs on the UI thread when the contents -// of the profile preferences are initialized. Once is_initialized() is set, -// access can only be done from the IO thread. -class NotificationsPrefsCache - : public base::RefCountedThreadSafe<NotificationsPrefsCache> { - public: - NotificationsPrefsCache(); - - // Once is_initialized() is set, all accesses must happen on the IO thread. - // Before that, all accesses need to happen on the UI thread. - void set_is_initialized(bool val) { is_initialized_ = val; } - bool is_initialized() { return is_initialized_; } - - // Checks to see if a given origin has permission to create desktop - // notifications. - WebKit::WebNotificationPresenter::Permission - HasPermission(const GURL& origin); - - // Updates the cache with a new origin allowed or denied. - void CacheAllowedOrigin(const GURL& origin); - void CacheDeniedOrigin(const GURL& origin); - - // Set the cache to the supplied values. This clears the current - // contents of the cache. - void SetCacheAllowedOrigins(const std::vector<GURL>& allowed); - void SetCacheDeniedOrigins(const std::vector<GURL>& denied); - void SetCacheDefaultContentSetting(ContentSetting setting); - - static void ListValueToGurlVector(const base::ListValue& origin_list, - std::vector<GURL>* origin_vector); - - // Exposed for testing. - ContentSetting CachedDefaultContentSetting() { - return default_content_setting_; - } - - private: - friend class base::RefCountedThreadSafe<NotificationsPrefsCache>; - - virtual ~NotificationsPrefsCache(); - - // Helper functions which read preferences. - bool IsOriginAllowed(const GURL& origin); - bool IsOriginDenied(const GURL& origin); - - // Helper that ensures we are running on the expected thread. - void CheckThreadAccess(); - - // Storage of the actual preferences. - std::set<GURL> allowed_origins_; - std::set<GURL> denied_origins_; - - // The default setting, used for origins that are neither in - // |allowed_origins_| nor |denied_origins_|. - ContentSetting default_content_setting_; - - // Set to true once the initial cached settings have been completely read. - // Once this is done, the class can no longer be accessed on the UI thread. - bool is_initialized_; - - DISALLOW_COPY_AND_ASSIGN(NotificationsPrefsCache); -}; - -#endif // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATIONS_PREFS_CACHE_H_ diff --git a/chrome/browser/notifications/notifications_prefs_cache_unittest.cc b/chrome/browser/notifications/notifications_prefs_cache_unittest.cc deleted file mode 100644 index 2b27aba..0000000 --- a/chrome/browser/notifications/notifications_prefs_cache_unittest.cc +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/notifications/notifications_prefs_cache.h" - -#include "base/message_loop.h" -#include "content/browser/browser_thread.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/WebNotificationPresenter.h" - -TEST(NotificationsPrefsCacheTest, CanCreate) { - scoped_refptr<NotificationsPrefsCache> cache(new NotificationsPrefsCache()); - std::vector<GURL> allowed_origins; - allowed_origins.push_back(GURL("http://allowed.com")); - std::vector<GURL> denied_origins; - denied_origins.push_back(GURL("http://denied.com")); - - { - MessageLoop loop; - BrowserThread ui_thread(BrowserThread::UI, &loop); - - cache->SetCacheAllowedOrigins(allowed_origins); - cache->SetCacheDeniedOrigins(denied_origins); - cache->SetCacheDefaultContentSetting(CONTENT_SETTING_DEFAULT); - } - - cache->set_is_initialized(true); - - { - MessageLoop loop; - BrowserThread io_thread(BrowserThread::IO, &loop); - - cache->CacheAllowedOrigin(GURL("http://allowed2.com")); - cache->CacheDeniedOrigin(GURL("http://denied2.com")); - - EXPECT_EQ(cache->HasPermission(GURL("http://allowed.com")), - WebKit::WebNotificationPresenter::PermissionAllowed); - EXPECT_EQ(cache->HasPermission(GURL("http://allowed2.com")), - WebKit::WebNotificationPresenter::PermissionAllowed); - - EXPECT_EQ(cache->HasPermission(GURL("http://denied.com")), - WebKit::WebNotificationPresenter::PermissionDenied); - EXPECT_EQ(cache->HasPermission(GURL("http://denied2.com")), - WebKit::WebNotificationPresenter::PermissionDenied); - - EXPECT_EQ(cache->HasPermission(GURL("http://unkown.com")), - WebKit::WebNotificationPresenter::PermissionNotAllowed); - - cache->SetCacheDefaultContentSetting(CONTENT_SETTING_ASK); - EXPECT_EQ(cache->HasPermission(GURL("http://unkown.com")), - WebKit::WebNotificationPresenter::PermissionNotAllowed); - - cache->SetCacheDefaultContentSetting(CONTENT_SETTING_ALLOW); - EXPECT_EQ(cache->HasPermission(GURL("http://unkown.com")), - WebKit::WebNotificationPresenter::PermissionAllowed); - - cache->SetCacheDefaultContentSetting(CONTENT_SETTING_BLOCK); - EXPECT_EQ(cache->HasPermission(GURL("http://unkown.com")), - WebKit::WebNotificationPresenter::PermissionDenied); - } -} - diff --git a/chrome/browser/prefs/browser_prefs.cc b/chrome/browser/prefs/browser_prefs.cc index 3d26106..2381332 100644 --- a/chrome/browser/prefs/browser_prefs.cc +++ b/chrome/browser/prefs/browser_prefs.cc @@ -164,7 +164,6 @@ void RegisterUserPrefs(PrefService* user_prefs) { PinnedTabCodec::RegisterUserPrefs(user_prefs); ExtensionPrefs::RegisterUserPrefs(user_prefs); TranslatePrefs::RegisterUserPrefs(user_prefs); - DesktopNotificationService::RegisterUserPrefs(user_prefs); PrefProxyConfigService::RegisterPrefs(user_prefs); #if defined(TOOLKIT_VIEWS) BrowserActionsContainer::RegisterUserPrefs(user_prefs); diff --git a/chrome/browser/ui/webui/options/content_settings_handler.cc b/chrome/browser/ui/webui/options/content_settings_handler.cc index 4b80c94..365b07d 100644 --- a/chrome/browser/ui/webui/options/content_settings_handler.cc +++ b/chrome/browser/ui/webui/options/content_settings_handler.cc @@ -24,7 +24,6 @@ #include "chrome/browser/ui/browser_list.h" #include "chrome/common/chrome_notification_types.h" #include "chrome/common/chrome_switches.h" -#include "chrome/common/content_settings_helper.h" #include "chrome/common/pref_names.h" #include "chrome/common/url_constants.h" #include "content/browser/tab_contents/tab_contents.h" @@ -168,13 +167,12 @@ DictionaryValue* GetGeolocationExceptionForPage( // in the desktop notifications exceptions table. Ownership of the pointer is // passed to the caller. DictionaryValue* GetNotificationExceptionForPage( - const GURL& url, + const ContentSettingsPattern& pattern, ContentSetting setting) { DictionaryValue* exception = new DictionaryValue(); - exception->SetString(kDisplayPattern, - content_settings_helper::OriginToString(url)); + exception->SetString(kDisplayPattern, pattern.ToString()); exception->SetString(kSetting, ContentSettingToString(setting)); - exception->SetString(kOrigin, url.spec()); + exception->SetString(kOrigin, pattern.ToString()); return exception; } @@ -510,17 +508,17 @@ void ContentSettingsHandler::UpdateNotificationExceptionsView() { DesktopNotificationService* service = DesktopNotificationServiceFactory::GetForProfile(profile); - std::vector<GURL> allowed(service->GetAllowedOrigins()); - std::vector<GURL> blocked(service->GetBlockedOrigins()); + HostContentSettingsMap::SettingsForOneType settings; + service->GetNotificationsSettings(&settings); ListValue exceptions; - for (size_t i = 0; i < allowed.size(); ++i) { - exceptions.Append( - GetNotificationExceptionForPage(allowed[i], CONTENT_SETTING_ALLOW)); - } - for (size_t i = 0; i < blocked.size(); ++i) { + for (HostContentSettingsMap::SettingsForOneType::const_iterator i = + settings.begin(); + i != settings.end(); + ++i) { + const HostContentSettingsMap::PatternSettingSourceTuple& tuple(*i); exceptions.Append( - GetNotificationExceptionForPage(blocked[i], CONTENT_SETTING_BLOCK)); + GetNotificationExceptionForPage(tuple.a, tuple.c)); } StringValue type_string( @@ -651,14 +649,11 @@ void ContentSettingsHandler::RemoveException(const ListValue* args) { rv = args->GetString(arg_i++, &setting); DCHECK(rv); ContentSetting content_setting = ContentSettingFromString(setting); - if (content_setting == CONTENT_SETTING_ALLOW) { - DesktopNotificationServiceFactory::GetForProfile(profile)-> - ResetAllowedOrigin(GURL(origin)); - } else { - DCHECK_EQ(content_setting, CONTENT_SETTING_BLOCK); - DesktopNotificationServiceFactory::GetForProfile(profile)-> - ResetBlockedOrigin(GURL(origin)); - } + + DCHECK(content_setting == CONTENT_SETTING_ALLOW || + content_setting == CONTENT_SETTING_BLOCK); + DesktopNotificationServiceFactory::GetForProfile(profile)-> + ClearSetting(ContentSettingsPattern::FromString(origin)); } else { std::string mode; bool rv = args->GetString(arg_i++, &mode); diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 81cca7c..424d4bd 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -793,8 +793,6 @@ 'browser/content_settings/content_settings_details.h', 'browser/content_settings/content_settings_extension_provider.cc', 'browser/content_settings/content_settings_extension_provider.h', - 'browser/content_settings/content_settings_notification_provider.cc', - 'browser/content_settings/content_settings_notification_provider.h', 'browser/content_settings/content_settings_observable_provider.cc', 'browser/content_settings/content_settings_observable_provider.h', 'browser/content_settings/content_settings_observer.h', @@ -1564,8 +1562,6 @@ 'browser/notifications/notification_options_menu_model.h', 'browser/notifications/notification_ui_manager.cc', 'browser/notifications/notification_ui_manager.h', - 'browser/notifications/notifications_prefs_cache.cc', - 'browser/notifications/notifications_prefs_cache.h', 'browser/ntp_background_util.cc', 'browser/ntp_background_util.h', 'browser/omnibox_search_hint.cc', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 0bff73f..766302c 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1513,7 +1513,6 @@ 'browser/net/url_info_unittest.cc', 'browser/notifications/desktop_notification_service_unittest.cc', 'browser/notifications/notification_exceptions_table_model_unittest.cc', - 'browser/notifications/notifications_prefs_cache_unittest.cc', 'browser/parsers/metadata_parser_filebase_unittest.cc', 'browser/password_manager/encryptor_password_mac_unittest.cc', 'browser/password_manager/encryptor_unittest.cc', diff --git a/chrome/common/pref_names.h b/chrome/common/pref_names.h index dc0e7cf..0c302fc 100644 --- a/chrome/common/pref_names.h +++ b/chrome/common/pref_names.h @@ -261,9 +261,9 @@ extern const char kShowUpdatePromotionInfoBar[]; #endif extern const char kUseCustomChromeFrame[]; extern const char kShowOmniboxSearchHint[]; -extern const char kDesktopNotificationDefaultContentSetting[]; -extern const char kDesktopNotificationAllowedOrigins[]; -extern const char kDesktopNotificationDeniedOrigins[]; +extern const char kDesktopNotificationDefaultContentSetting[]; // OBSOLETE +extern const char kDesktopNotificationAllowedOrigins[]; // OBSOLETE +extern const char kDesktopNotificationDeniedOrigins[]; // OBSOLETE extern const char kDesktopNotificationPosition[]; extern const char kDefaultContentSettings[]; extern const char kPerHostContentSettings[]; // OBSOLETE |