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
parent 757f2505
// 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_
......@@ -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);
......
......@@ -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
......@@ -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(
......
......@@ -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,
......
......@@ -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);
......
......@@ -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();
......
......@@ -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();
}
......@@ -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());
}
......@@ -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"
......
// 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));
}
}
// 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_
// 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);
}
}
......@@ -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);
......
......@@ -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);
DCHECK(content_setting == CONTENT_SETTING_ALLOW ||
content_setting == CONTENT_SETTING_BLOCK);
DesktopNotificationServiceFactory::GetForProfile(profile)->
ResetBlockedOrigin(GURL(origin));
}
ClearSetting(ContentSettingsPattern::FromString(origin));
} else {
std::string mode;
bool rv = args->GetString(arg_i++, &mode);
......
......@@ -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',
......
......@@ -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',
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment