OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/notifications/notification_exceptions_table_model.h" | |
6 | |
7 #include <algorithm> | |
8 #include <string> | |
9 | |
10 #include "base/auto_reset.h" | |
11 #include "base/utf_string_conversions.h" | |
12 #include "chrome/browser/content_settings/host_content_settings_map.h" | |
13 #include "chrome/common/chrome_notification_types.h" | |
14 #include "chrome/common/content_settings.h" | |
15 #include "chrome/common/content_settings_pattern.h" | |
16 #include "chrome/common/content_settings_types.h" | |
17 #include "chrome/common/url_constants.h" | |
18 #include "content/common/notification_service.h" | |
19 #include "grit/generated_resources.h" | |
20 #include "ui/base/l10n/l10n_util.h" | |
21 #include "ui/base/models/table_model_observer.h" | |
22 | |
23 struct NotificationExceptionsTableModel::Entry { | |
24 Entry(const ContentSettingsPattern& origin, ContentSetting setting); | |
25 bool operator<(const Entry& b) const; | |
26 | |
27 ContentSettingsPattern origin; | |
28 ContentSetting setting; | |
29 }; | |
30 | |
31 NotificationExceptionsTableModel::NotificationExceptionsTableModel( | |
32 DesktopNotificationService* service) | |
33 : service_(service), | |
34 updates_disabled_(false), | |
35 observer_(NULL) { | |
36 registrar_.Add(this, | |
37 chrome::NOTIFICATION_DESKTOP_NOTIFICATION_SETTINGS_CHANGED, | |
38 Source<DesktopNotificationService>(service)); | |
39 LoadEntries(); | |
40 } | |
41 | |
42 NotificationExceptionsTableModel::~NotificationExceptionsTableModel() {} | |
43 | |
44 bool NotificationExceptionsTableModel::CanRemoveRows( | |
45 const Rows& rows) const { | |
46 return !rows.empty(); | |
47 } | |
48 | |
49 void NotificationExceptionsTableModel::RemoveRows(const Rows& rows) { | |
50 AutoReset<bool> tmp(&updates_disabled_, true); | |
51 // 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) { | |
53 size_t row = *i; | |
54 Entry* entry = &entries_[row]; | |
55 DCHECK(entry->setting == CONTENT_SETTING_ALLOW || | |
56 entry->setting == CONTENT_SETTING_BLOCK); | |
57 service_->ClearSetting(entry->origin); | |
58 entries_.erase(entries_.begin() + row); // Note: |entry| is now garbage. | |
59 if (observer_) | |
60 observer_->OnItemsRemoved(row, 1); | |
61 } | |
62 } | |
63 | |
64 void NotificationExceptionsTableModel::RemoveAll() { | |
65 AutoReset<bool> tmp(&updates_disabled_, true); | |
66 entries_.clear(); | |
67 service_->ResetAllOrigins(); | |
68 if (observer_) | |
69 observer_->OnModelChanged(); | |
70 } | |
71 | |
72 int NotificationExceptionsTableModel::RowCount() { | |
73 return static_cast<int>(entries_.size()); | |
74 } | |
75 | |
76 string16 NotificationExceptionsTableModel::GetText(int row, | |
77 int column_id) { | |
78 const Entry& entry = entries_[row]; | |
79 if (column_id == IDS_EXCEPTIONS_HOSTNAME_HEADER) { | |
80 return UTF8ToUTF16(entry.origin.ToString()); | |
81 } | |
82 | |
83 if (column_id == IDS_EXCEPTIONS_ACTION_HEADER) { | |
84 switch (entry.setting) { | |
85 case CONTENT_SETTING_ALLOW: | |
86 return l10n_util::GetStringUTF16(IDS_EXCEPTIONS_ALLOW_BUTTON); | |
87 case CONTENT_SETTING_BLOCK: | |
88 return l10n_util::GetStringUTF16(IDS_EXCEPTIONS_BLOCK_BUTTON); | |
89 default: | |
90 break; | |
91 } | |
92 } | |
93 | |
94 NOTREACHED(); | |
95 return string16(); | |
96 } | |
97 | |
98 void NotificationExceptionsTableModel::SetObserver( | |
99 ui::TableModelObserver* observer) { | |
100 observer_ = observer; | |
101 } | |
102 | |
103 void NotificationExceptionsTableModel::Observe( | |
104 int type, | |
105 const NotificationSource& source, | |
106 const NotificationDetails& details) { | |
107 if (!updates_disabled_) { | |
108 DCHECK_EQ(type, chrome::NOTIFICATION_DESKTOP_NOTIFICATION_SETTINGS_CHANGED); | |
109 DCHECK_EQ(Source<DesktopNotificationService>(source).ptr(), service_); | |
110 entries_.clear(); | |
111 LoadEntries(); | |
112 | |
113 if (observer_) | |
114 observer_->OnModelChanged(); | |
115 } | |
116 } | |
117 | |
118 void NotificationExceptionsTableModel::LoadEntries() { | |
119 HostContentSettingsMap::SettingsForOneType settings; | |
120 service_->GetNotificationsSettings(&settings); | |
121 | |
122 entries_.reserve(settings.size()); | |
123 for (HostContentSettingsMap::SettingsForOneType::const_iterator i = | |
124 settings.begin(); | |
125 i != settings.end(); | |
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()); | |
131 } | |
132 | |
133 NotificationExceptionsTableModel::Entry::Entry( | |
134 const ContentSettingsPattern& in_origin, | |
135 ContentSetting in_setting) | |
136 : origin(in_origin), | |
137 setting(in_setting) { | |
138 } | |
139 | |
140 bool NotificationExceptionsTableModel::Entry::operator<( | |
141 const NotificationExceptionsTableModel::Entry& b) const { | |
142 DCHECK_NE(origin, b.origin); | |
143 return origin.ToString() < b.origin.ToString(); | |
144 } | |
OLD | NEW |