| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/notifications/notification_exceptions_table_model.h" | 5 #include "chrome/browser/notifications/notification_exceptions_table_model.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 11 #include "base/utf_string_conversions.h" | |
| 12 #include "chrome/browser/content_settings/content_settings_pattern.h" | |
| 13 #include "chrome/browser/content_settings/host_content_settings_map.h" | |
| 14 #include "chrome/common/chrome_notification_types.h" | 8 #include "chrome/common/chrome_notification_types.h" |
| 15 #include "chrome/common/content_settings.h" | 9 #include "chrome/common/content_settings.h" |
| 10 #include "chrome/common/content_settings_helper.h" |
| 16 #include "chrome/common/content_settings_types.h" | 11 #include "chrome/common/content_settings_types.h" |
| 17 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
| 18 #include "content/common/notification_service.h" | 13 #include "content/common/notification_service.h" |
| 19 #include "grit/generated_resources.h" | 14 #include "grit/generated_resources.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | 15 #include "ui/base/l10n/l10n_util.h" |
| 21 #include "ui/base/models/table_model_observer.h" | 16 #include "ui/base/models/table_model_observer.h" |
| 22 | 17 |
| 23 struct NotificationExceptionsTableModel::Entry { | 18 struct NotificationExceptionsTableModel::Entry { |
| 24 Entry(const ContentSettingsPattern& origin, ContentSetting setting); | 19 Entry(const GURL& origin, ContentSetting setting); |
| 25 bool operator<(const Entry& b) const; | 20 bool operator<(const Entry& b) const; |
| 26 | 21 |
| 27 ContentSettingsPattern origin; | 22 GURL origin; |
| 28 ContentSetting setting; | 23 ContentSetting setting; |
| 29 }; | 24 }; |
| 30 | 25 |
| 31 NotificationExceptionsTableModel::NotificationExceptionsTableModel( | 26 NotificationExceptionsTableModel::NotificationExceptionsTableModel( |
| 32 DesktopNotificationService* service) | 27 DesktopNotificationService* service) |
| 33 : service_(service), | 28 : service_(service), |
| 34 updates_disabled_(false), | 29 updates_disabled_(false), |
| 35 observer_(NULL) { | 30 observer_(NULL) { |
| 36 registrar_.Add(this, | 31 registrar_.Add(this, |
| 37 chrome::NOTIFICATION_DESKTOP_NOTIFICATION_SETTINGS_CHANGED, | 32 chrome::NOTIFICATION_DESKTOP_NOTIFICATION_SETTINGS_CHANGED, |
| 38 Source<DesktopNotificationService>(service)); | 33 Source<DesktopNotificationService>(service)); |
| 39 LoadEntries(); | 34 LoadEntries(); |
| 40 } | 35 } |
| 41 | 36 |
| 42 NotificationExceptionsTableModel::~NotificationExceptionsTableModel() {} | 37 NotificationExceptionsTableModel::~NotificationExceptionsTableModel() {} |
| 43 | 38 |
| 44 bool NotificationExceptionsTableModel::CanRemoveRows( | 39 bool NotificationExceptionsTableModel::CanRemoveRows( |
| 45 const Rows& rows) const { | 40 const Rows& rows) const { |
| 46 return !rows.empty(); | 41 return !rows.empty(); |
| 47 } | 42 } |
| 48 | 43 |
| 49 void NotificationExceptionsTableModel::RemoveRows(const Rows& rows) { | 44 void NotificationExceptionsTableModel::RemoveRows(const Rows& rows) { |
| 50 AutoReset<bool> tmp(&updates_disabled_, true); | 45 AutoReset<bool> tmp(&updates_disabled_, true); |
| 51 // This is O(n^2) in rows.size(). Since n is small, that's ok. | 46 // This is O(n^2) in rows.size(). Since n is small, that's ok. |
| 52 for (Rows::const_reverse_iterator i(rows.rbegin()); i != rows.rend(); ++i) { | 47 for (Rows::const_reverse_iterator i(rows.rbegin()); i != rows.rend(); ++i) { |
| 53 size_t row = *i; | 48 size_t row = *i; |
| 54 Entry* entry = &entries_[row]; | 49 Entry* entry = &entries_[row]; |
| 55 DCHECK(entry->setting == CONTENT_SETTING_ALLOW || | 50 if (entry->setting == CONTENT_SETTING_ALLOW) { |
| 56 entry->setting == CONTENT_SETTING_BLOCK); | 51 service_->ResetAllowedOrigin(entry->origin); |
| 57 service_->ClearSetting(entry->origin); | 52 } else { |
| 53 DCHECK_EQ(entry->setting, CONTENT_SETTING_BLOCK); |
| 54 service_->ResetBlockedOrigin(entry->origin); |
| 55 } |
| 58 entries_.erase(entries_.begin() + row); // Note: |entry| is now garbage. | 56 entries_.erase(entries_.begin() + row); // Note: |entry| is now garbage. |
| 59 if (observer_) | 57 if (observer_) |
| 60 observer_->OnItemsRemoved(row, 1); | 58 observer_->OnItemsRemoved(row, 1); |
| 61 } | 59 } |
| 62 } | 60 } |
| 63 | 61 |
| 64 void NotificationExceptionsTableModel::RemoveAll() { | 62 void NotificationExceptionsTableModel::RemoveAll() { |
| 65 AutoReset<bool> tmp(&updates_disabled_, true); | 63 AutoReset<bool> tmp(&updates_disabled_, true); |
| 66 entries_.clear(); | 64 entries_.clear(); |
| 67 service_->ResetAllOrigins(); | 65 service_->ResetAllOrigins(); |
| 68 if (observer_) | 66 if (observer_) |
| 69 observer_->OnModelChanged(); | 67 observer_->OnModelChanged(); |
| 70 } | 68 } |
| 71 | 69 |
| 72 int NotificationExceptionsTableModel::RowCount() { | 70 int NotificationExceptionsTableModel::RowCount() { |
| 73 return static_cast<int>(entries_.size()); | 71 return static_cast<int>(entries_.size()); |
| 74 } | 72 } |
| 75 | 73 |
| 76 string16 NotificationExceptionsTableModel::GetText(int row, | 74 string16 NotificationExceptionsTableModel::GetText(int row, |
| 77 int column_id) { | 75 int column_id) { |
| 78 const Entry& entry = entries_[row]; | 76 const Entry& entry = entries_[row]; |
| 79 if (column_id == IDS_EXCEPTIONS_HOSTNAME_HEADER) { | 77 if (column_id == IDS_EXCEPTIONS_HOSTNAME_HEADER) { |
| 80 return UTF8ToUTF16(entry.origin.ToString()); | 78 return content_settings_helper::OriginToString16(entry.origin); |
| 81 } | 79 } |
| 82 | 80 |
| 83 if (column_id == IDS_EXCEPTIONS_ACTION_HEADER) { | 81 if (column_id == IDS_EXCEPTIONS_ACTION_HEADER) { |
| 84 switch (entry.setting) { | 82 switch (entry.setting) { |
| 85 case CONTENT_SETTING_ALLOW: | 83 case CONTENT_SETTING_ALLOW: |
| 86 return l10n_util::GetStringUTF16(IDS_EXCEPTIONS_ALLOW_BUTTON); | 84 return l10n_util::GetStringUTF16(IDS_EXCEPTIONS_ALLOW_BUTTON); |
| 87 case CONTENT_SETTING_BLOCK: | 85 case CONTENT_SETTING_BLOCK: |
| 88 return l10n_util::GetStringUTF16(IDS_EXCEPTIONS_BLOCK_BUTTON); | 86 return l10n_util::GetStringUTF16(IDS_EXCEPTIONS_BLOCK_BUTTON); |
| 89 default: | 87 default: |
| 90 break; | 88 break; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 109 DCHECK_EQ(Source<DesktopNotificationService>(source).ptr(), service_); | 107 DCHECK_EQ(Source<DesktopNotificationService>(source).ptr(), service_); |
| 110 entries_.clear(); | 108 entries_.clear(); |
| 111 LoadEntries(); | 109 LoadEntries(); |
| 112 | 110 |
| 113 if (observer_) | 111 if (observer_) |
| 114 observer_->OnModelChanged(); | 112 observer_->OnModelChanged(); |
| 115 } | 113 } |
| 116 } | 114 } |
| 117 | 115 |
| 118 void NotificationExceptionsTableModel::LoadEntries() { | 116 void NotificationExceptionsTableModel::LoadEntries() { |
| 119 HostContentSettingsMap::SettingsForOneType settings; | 117 std::vector<GURL> allowed(service_->GetAllowedOrigins()); |
| 120 service_->GetNotificationsSettings(&settings); | 118 std::vector<GURL> blocked(service_->GetBlockedOrigins()); |
| 121 | 119 entries_.reserve(allowed.size() + blocked.size()); |
| 122 entries_.reserve(settings.size()); | 120 for (size_t i = 0; i < allowed.size(); ++i) |
| 123 for (HostContentSettingsMap::SettingsForOneType::const_iterator i = | 121 entries_.push_back(Entry(allowed[i], CONTENT_SETTING_ALLOW)); |
| 124 settings.begin(); | 122 for (size_t i = 0; i < blocked.size(); ++i) |
| 125 i != settings.end(); | 123 entries_.push_back(Entry(blocked[i], CONTENT_SETTING_BLOCK)); |
| 126 ++i) { | |
| 127 const HostContentSettingsMap::PatternSettingSourceTuple& tuple(*i); | |
| 128 entries_.push_back(Entry(tuple.a, tuple.c)); | |
| 129 } | |
| 130 std::sort(entries_.begin(), entries_.end()); | 124 std::sort(entries_.begin(), entries_.end()); |
| 131 } | 125 } |
| 132 | 126 |
| 133 NotificationExceptionsTableModel::Entry::Entry( | 127 NotificationExceptionsTableModel::Entry::Entry( |
| 134 const ContentSettingsPattern& in_origin, | 128 const GURL& in_origin, |
| 135 ContentSetting in_setting) | 129 ContentSetting in_setting) |
| 136 : origin(in_origin), | 130 : origin(in_origin), |
| 137 setting(in_setting) { | 131 setting(in_setting) { |
| 138 } | 132 } |
| 139 | 133 |
| 140 bool NotificationExceptionsTableModel::Entry::operator<( | 134 bool NotificationExceptionsTableModel::Entry::operator<( |
| 141 const NotificationExceptionsTableModel::Entry& b) const { | 135 const NotificationExceptionsTableModel::Entry& b) const { |
| 142 DCHECK_NE(origin, b.origin); | 136 DCHECK_NE(origin, b.origin); |
| 143 return origin.ToString() < b.origin.ToString(); | 137 return origin < b.origin; |
| 144 } | 138 } |
| OLD | NEW |