| 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/plugin_exceptions_table_model.h" | |
| 6 | |
| 7 #include "base/auto_reset.h" | |
| 8 #include "base/sys_string_conversions.h" | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "chrome/common/chrome_notification_types.h" | |
| 11 #include "content/common/notification_service.h" | |
| 12 #include "grit/generated_resources.h" | |
| 13 #include "ui/base/l10n/l10n_util.h" | |
| 14 #include "ui/base/models/table_model_observer.h" | |
| 15 | |
| 16 PluginExceptionsTableModel::PluginExceptionsTableModel( | |
| 17 HostContentSettingsMap* content_settings_map, | |
| 18 HostContentSettingsMap* otr_content_settings_map) | |
| 19 : map_(content_settings_map), | |
| 20 otr_map_(otr_content_settings_map), | |
| 21 updates_disabled_(false), | |
| 22 observer_(NULL) { | |
| 23 registrar_.Add(this, chrome::NOTIFICATION_CONTENT_SETTINGS_CHANGED, | |
| 24 Source<HostContentSettingsMap>(map_)); | |
| 25 if (otr_map_) { | |
| 26 registrar_.Add(this, chrome::NOTIFICATION_CONTENT_SETTINGS_CHANGED, | |
| 27 Source<HostContentSettingsMap>(otr_map_)); | |
| 28 } | |
| 29 } | |
| 30 | |
| 31 PluginExceptionsTableModel::~PluginExceptionsTableModel() { | |
| 32 } | |
| 33 | |
| 34 bool PluginExceptionsTableModel::CanRemoveRows(const Rows& rows) const { | |
| 35 return !rows.empty(); | |
| 36 } | |
| 37 | |
| 38 void PluginExceptionsTableModel::RemoveRows(const Rows& rows) { | |
| 39 AutoReset<bool> tmp(&updates_disabled_, true); | |
| 40 bool reload_all = false; | |
| 41 // Iterate over the rows starting with the highest ones so we can delete | |
| 42 // entries from |settings_| without the other indices shifting. | |
| 43 for (Rows::const_reverse_iterator it = rows.rbegin(); | |
| 44 it != rows.rend(); ++it) { | |
| 45 DCHECK_LT(*it, settings_.size()); | |
| 46 SettingsEntry entry = settings_[*it]; | |
| 47 HostContentSettingsMap* map = entry.is_otr ? otr_map_ : map_; | |
| 48 map->SetContentSetting(entry.pattern, | |
| 49 ContentSettingsPattern::Wildcard(), | |
| 50 CONTENT_SETTINGS_TYPE_PLUGINS, | |
| 51 resources_[entry.plugin_id], | |
| 52 CONTENT_SETTING_DEFAULT); | |
| 53 settings_.erase(settings_.begin() + *it); | |
| 54 row_counts_[entry.plugin_id]--; | |
| 55 if (!reload_all) { | |
| 56 // If we remove the last exception for a plugin, recreate all groups | |
| 57 // to get correct IDs. | |
| 58 if (row_counts_[entry.plugin_id] == 0) { | |
| 59 reload_all = true; | |
| 60 } else { | |
| 61 if (observer_) | |
| 62 observer_->OnItemsRemoved(*it, 1); | |
| 63 } | |
| 64 } | |
| 65 } | |
| 66 if (reload_all) { | |
| 67 // This also notifies the observer. | |
| 68 ReloadSettings(); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 void PluginExceptionsTableModel::RemoveAll() { | |
| 73 AutoReset<bool> tmp(&updates_disabled_, true); | |
| 74 map_->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_PLUGINS); | |
| 75 if (otr_map_) | |
| 76 otr_map_->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_PLUGINS); | |
| 77 | |
| 78 ClearSettings(); | |
| 79 if (observer_) | |
| 80 observer_->OnModelChanged(); | |
| 81 } | |
| 82 | |
| 83 int PluginExceptionsTableModel::RowCount() { | |
| 84 return settings_.size(); | |
| 85 } | |
| 86 | |
| 87 string16 PluginExceptionsTableModel::GetText(int row, int column_id) { | |
| 88 DCHECK_GE(row, 0); | |
| 89 DCHECK_LT(row, static_cast<int>(settings_.size())); | |
| 90 SettingsEntry& entry = settings_[row]; | |
| 91 if (column_id == IDS_EXCEPTIONS_PATTERN_HEADER || | |
| 92 column_id == IDS_EXCEPTIONS_HOSTNAME_HEADER) { | |
| 93 return UTF8ToUTF16(entry.pattern.ToString()); | |
| 94 } else if (column_id == IDS_EXCEPTIONS_ACTION_HEADER) { | |
| 95 switch (entry.setting) { | |
| 96 case CONTENT_SETTING_ALLOW: | |
| 97 return l10n_util::GetStringUTF16(IDS_EXCEPTIONS_ALLOW_BUTTON); | |
| 98 case CONTENT_SETTING_BLOCK: | |
| 99 return l10n_util::GetStringUTF16(IDS_EXCEPTIONS_BLOCK_BUTTON); | |
| 100 default: | |
| 101 NOTREACHED(); | |
| 102 } | |
| 103 } else { | |
| 104 NOTREACHED(); | |
| 105 } | |
| 106 return string16(); | |
| 107 } | |
| 108 | |
| 109 bool PluginExceptionsTableModel::HasGroups() { | |
| 110 return true; | |
| 111 } | |
| 112 | |
| 113 void PluginExceptionsTableModel::SetObserver(ui::TableModelObserver* observer) { | |
| 114 observer_ = observer; | |
| 115 } | |
| 116 | |
| 117 ui::TableModel::Groups PluginExceptionsTableModel::GetGroups() { | |
| 118 return groups_; | |
| 119 } | |
| 120 | |
| 121 int PluginExceptionsTableModel::GetGroupID(int row) { | |
| 122 DCHECK_LT(row, static_cast<int>(settings_.size())); | |
| 123 return settings_[row].plugin_id; | |
| 124 } | |
| 125 | |
| 126 void PluginExceptionsTableModel::Observe(int type, | |
| 127 const NotificationSource& source, | |
| 128 const NotificationDetails& details) { | |
| 129 if (!updates_disabled_) | |
| 130 ReloadSettings(); | |
| 131 } | |
| 132 | |
| 133 void PluginExceptionsTableModel::ClearSettings() { | |
| 134 settings_.clear(); | |
| 135 groups_.clear(); | |
| 136 row_counts_.clear(); | |
| 137 resources_.clear(); | |
| 138 } | |
| 139 | |
| 140 void PluginExceptionsTableModel::GetPlugins( | |
| 141 std::vector<webkit::npapi::PluginGroup>* plugin_groups) { | |
| 142 webkit::npapi::PluginList::Singleton()->GetPluginGroups(false, plugin_groups); | |
| 143 } | |
| 144 | |
| 145 void PluginExceptionsTableModel::LoadSettings() { | |
| 146 int group_id = 0; | |
| 147 std::vector<webkit::npapi::PluginGroup> plugins; | |
| 148 GetPlugins(&plugins); | |
| 149 for (size_t i = 0; i < plugins.size(); ++i) { | |
| 150 std::string plugin = plugins[i].identifier(); | |
| 151 HostContentSettingsMap::SettingsForOneType settings; | |
| 152 map_->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_PLUGINS, | |
| 153 plugin, | |
| 154 &settings); | |
| 155 HostContentSettingsMap::SettingsForOneType otr_settings; | |
| 156 if (otr_map_) { | |
| 157 otr_map_->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_PLUGINS, | |
| 158 plugin, | |
| 159 &otr_settings); | |
| 160 } | |
| 161 string16 title = plugins[i].GetGroupName(); | |
| 162 for (HostContentSettingsMap::SettingsForOneType::iterator setting_it = | |
| 163 settings.begin(); | |
| 164 setting_it != settings.end(); ++setting_it) { | |
| 165 SettingsEntry entry = { | |
| 166 setting_it->a, | |
| 167 group_id, | |
| 168 setting_it->c, | |
| 169 false | |
| 170 }; | |
| 171 settings_.push_back(entry); | |
| 172 } | |
| 173 for (HostContentSettingsMap::SettingsForOneType::iterator setting_it = | |
| 174 otr_settings.begin(); | |
| 175 setting_it != otr_settings.end(); ++setting_it) { | |
| 176 SettingsEntry entry = { | |
| 177 setting_it->a, | |
| 178 group_id, | |
| 179 setting_it->c, | |
| 180 true | |
| 181 }; | |
| 182 settings_.push_back(entry); | |
| 183 } | |
| 184 int num_plugins = settings.size() + otr_settings.size(); | |
| 185 if (num_plugins > 0) { | |
| 186 Group group = { title, group_id++ }; | |
| 187 groups_.push_back(group); | |
| 188 resources_.push_back(plugin); | |
| 189 row_counts_.push_back(num_plugins); | |
| 190 } | |
| 191 } | |
| 192 } | |
| 193 | |
| 194 void PluginExceptionsTableModel::ReloadSettings() { | |
| 195 ClearSettings(); | |
| 196 LoadSettings(); | |
| 197 | |
| 198 if (observer_) | |
| 199 observer_->OnModelChanged(); | |
| 200 } | |
| OLD | NEW |