diff options
author | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-18 20:47:12 +0000 |
---|---|---|
committer | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-18 20:47:12 +0000 |
commit | a90e6d8e47a8d13c82d8501210f8aa629ff587e8 (patch) | |
tree | 0f7c07127a3b1820612bd6335f621cab41a3876d /chrome/browser/notifications | |
parent | ae04f59cd4978fa308f9521664dcaea8ef221d0f (diff) | |
download | chromium_src-a90e6d8e47a8d13c82d8501210f8aa629ff587e8.zip chromium_src-a90e6d8e47a8d13c82d8501210f8aa629ff587e8.tar.gz chromium_src-a90e6d8e47a8d13c82d8501210f8aa629ff587e8.tar.bz2 |
notifications: Update the 'Notification Exceptions' dialog when the permission settings changes.
This update the view when the model changes.
BUG=59299
TEST=see bug
Review URL: http://codereview.chromium.org/4562003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@66669 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/notifications')
4 files changed, 64 insertions, 22 deletions
diff --git a/chrome/browser/notifications/desktop_notification_service.cc b/chrome/browser/notifications/desktop_notification_service.cc index e7530e4..08d25db 100644 --- a/chrome/browser/notifications/desktop_notification_service.cc +++ b/chrome/browser/notifications/desktop_notification_service.cc @@ -282,6 +282,8 @@ void DesktopNotificationService::GrantPermission(const GURL& origin) { NewRunnableMethod( prefs_cache_.get(), &NotificationsPrefsCache::CacheAllowedOrigin, origin)); + + NotifySettingsChange(); } void DesktopNotificationService::DenyPermission(const GURL& origin) { @@ -294,6 +296,8 @@ void DesktopNotificationService::DenyPermission(const GURL& origin) { NewRunnableMethod( prefs_cache_.get(), &NotificationsPrefsCache::CacheDeniedOrigin, origin)); + + NotifySettingsChange(); } void DesktopNotificationService::Observe(NotificationType type, @@ -304,10 +308,7 @@ void DesktopNotificationService::Observe(NotificationType type, const std::string& name = *Details<std::string>(details).ptr(); if (name == prefs::kDesktopNotificationAllowedOrigins) { - NotificationService::current()->Notify( - NotificationType::DESKTOP_NOTIFICATION_SETTINGS_CHANGED, - Source<DesktopNotificationService>(this), - NotificationService::NoDetails()); + NotifySettingsChange(); std::vector<GURL> allowed_origins(GetAllowedOrigins()); // Schedule a cache update on the IO thread. @@ -318,10 +319,7 @@ void DesktopNotificationService::Observe(NotificationType type, &NotificationsPrefsCache::SetCacheAllowedOrigins, allowed_origins)); } else if (name == prefs::kDesktopNotificationDeniedOrigins) { - NotificationService::current()->Notify( - NotificationType::DESKTOP_NOTIFICATION_SETTINGS_CHANGED, - Source<DesktopNotificationService>(this), - NotificationService::NoDetails()); + NotifySettingsChange(); std::vector<GURL> denied_origins(GetBlockedOrigins()); // Schedule a cache update on the IO thread. @@ -607,3 +605,10 @@ string16 DesktopNotificationService::DisplayNameForOrigin( } return UTF8ToUTF16(origin.host()); } + +void DesktopNotificationService::NotifySettingsChange() { + NotificationService::current()->Notify( + NotificationType::DESKTOP_NOTIFICATION_SETTINGS_CHANGED, + Source<DesktopNotificationService>(this), + NotificationService::NoDetails()); +} diff --git a/chrome/browser/notifications/desktop_notification_service.h b/chrome/browser/notifications/desktop_notification_service.h index d92d7e6..51e3493 100644 --- a/chrome/browser/notifications/desktop_notification_service.h +++ b/chrome/browser/notifications/desktop_notification_service.h @@ -132,6 +132,9 @@ class DesktopNotificationService : public NotificationObserver { // itself when dealing with extensions. string16 DisplayNameForOrigin(const GURL& origin); + // Notifies the observers when permissions settings change. + void NotifySettingsChange(); + // The profile which owns this object. Profile* profile_; diff --git a/chrome/browser/notifications/notification_exceptions_table_model.cc b/chrome/browser/notifications/notification_exceptions_table_model.cc index 31a44d5..1f8b228 100644 --- a/chrome/browser/notifications/notification_exceptions_table_model.cc +++ b/chrome/browser/notifications/notification_exceptions_table_model.cc @@ -6,6 +6,7 @@ #include "app/l10n_util.h" #include "app/table_model_observer.h" +#include "base/auto_reset.h" #include "base/utf_string_conversions.h" #include "chrome/common/content_settings.h" #include "chrome/common/content_settings_helper.h" @@ -24,15 +25,11 @@ struct NotificationExceptionsTableModel::Entry { NotificationExceptionsTableModel::NotificationExceptionsTableModel( DesktopNotificationService* service) : service_(service), + updates_disabled_(false), observer_(NULL) { - 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)); - sort(entries_.begin(), entries_.end()); + registrar_.Add(this, NotificationType::DESKTOP_NOTIFICATION_SETTINGS_CHANGED, + NotificationService::AllSources()); + LoadEntries(); } NotificationExceptionsTableModel::~NotificationExceptionsTableModel() {} @@ -43,6 +40,7 @@ bool NotificationExceptionsTableModel::CanRemoveRows( } void NotificationExceptionsTableModel::RemoveRows(const Rows& rows) { + AutoReset<bool> tmp(&updates_disabled_, true); // This is O(n^2) in rows.size(). Since n is small, that's ok. for (Rows::const_reverse_iterator i(rows.rbegin()); i != rows.rend(); ++i) { size_t row = *i; @@ -60,11 +58,11 @@ void NotificationExceptionsTableModel::RemoveRows(const Rows& rows) { } void NotificationExceptionsTableModel::RemoveAll() { - int old_row_count = RowCount(); + AutoReset<bool> tmp(&updates_disabled_, true); entries_.clear(); service_->ResetAllOrigins(); if (observer_) - observer_->OnItemsRemoved(0, old_row_count); + observer_->OnModelChanged(); } int NotificationExceptionsTableModel::RowCount() { @@ -98,6 +96,31 @@ void NotificationExceptionsTableModel::SetObserver( observer_ = observer; } +void NotificationExceptionsTableModel::Observe( + NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + if (!updates_disabled_) { + DCHECK(type == NotificationType::DESKTOP_NOTIFICATION_SETTINGS_CHANGED); + entries_.clear(); + LoadEntries(); + + if (observer_) + observer_->OnModelChanged(); + } +} + +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)); + std::sort(entries_.begin(), entries_.end()); +} + NotificationExceptionsTableModel::Entry::Entry( const GURL& in_origin, ContentSetting in_setting) diff --git a/chrome/browser/notifications/notification_exceptions_table_model.h b/chrome/browser/notifications/notification_exceptions_table_model.h index 0fcf335..107b2ec 100644 --- a/chrome/browser/notifications/notification_exceptions_table_model.h +++ b/chrome/browser/notifications/notification_exceptions_table_model.h @@ -11,35 +11,46 @@ #include "chrome/browser/notifications/desktop_notification_service.h" #include "chrome/browser/remove_rows_table_model.h" +#include "chrome/common/notification_observer.h" -class NotificationExceptionsTableModel : public RemoveRowsTableModel { +class NotificationExceptionsTableModel : public RemoveRowsTableModel, + public NotificationObserver { public: explicit NotificationExceptionsTableModel( DesktopNotificationService* service); virtual ~NotificationExceptionsTableModel(); - // RemoveRowsTableModel overrides: + // Overridden from RemoveRowsTableModel: virtual bool CanRemoveRows(const Rows& rows) const; virtual void RemoveRows(const Rows& rows); virtual void RemoveAll(); - // TableModel overrides: + // Overridden from TableModel: virtual int RowCount(); virtual std::wstring GetText(int row, int column_id); virtual void SetObserver(TableModelObserver* observer); + // Overridden from NotificationObserver: + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details); private: struct Entry; + void LoadEntries(); + DesktopNotificationService* service_; typedef std::vector<Entry> EntriesVector; EntriesVector entries_; + // We use this variable to prevent ourselves from handling further changes + // that we ourselves caused. + bool updates_disabled_; + NotificationRegistrar registrar_; TableModelObserver* observer_; DISALLOW_COPY_AND_ASSIGN(NotificationExceptionsTableModel); }; #endif // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_EXCEPTIONS_TABLE_MODEL_H_ - |