summaryrefslogtreecommitdiffstats
path: root/chrome/browser/plugin_exceptions_table_model.cc
blob: 81200fe4310512377f980be3e50a5935520a84de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// 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.

#include "chrome/browser/plugin_exceptions_table_model.h"

#include "base/auto_reset.h"
#include "base/sys_string_conversions.h"
#include "base/utf_string_conversions.h"
#include "content/common/notification_service.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/table_model_observer.h"

PluginExceptionsTableModel::PluginExceptionsTableModel(
    HostContentSettingsMap* content_settings_map,
    HostContentSettingsMap* otr_content_settings_map)
    : map_(content_settings_map),
      otr_map_(otr_content_settings_map),
      updates_disabled_(false),
      observer_(NULL) {
  registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED,
                 NotificationService::AllSources());
}

PluginExceptionsTableModel::~PluginExceptionsTableModel() {
}

bool PluginExceptionsTableModel::CanRemoveRows(const Rows& rows) const {
  return !rows.empty();
}

void PluginExceptionsTableModel::RemoveRows(const Rows& rows) {
  AutoReset<bool> tmp(&updates_disabled_, true);
  bool reload_all = false;
  // Iterate over the rows starting with the highest ones so we can delete
  // entries from |settings_| without the other indices shifting.
  for (Rows::const_reverse_iterator it = rows.rbegin();
       it != rows.rend(); ++it) {
    DCHECK_LT(*it, settings_.size());
    SettingsEntry entry = settings_[*it];
    HostContentSettingsMap* map = entry.is_otr ? otr_map_ : map_;
    map->SetContentSetting(entry.pattern,
                           CONTENT_SETTINGS_TYPE_PLUGINS,
                           resources_[entry.plugin_id],
                           CONTENT_SETTING_DEFAULT);
    settings_.erase(settings_.begin() + *it);
    row_counts_[entry.plugin_id]--;
    if (!reload_all) {
      // If we remove the last exception for a plugin, recreate all groups
      // to get correct IDs.
      if (row_counts_[entry.plugin_id] == 0)  {
        reload_all = true;
      } else {
        if (observer_)
          observer_->OnItemsRemoved(*it, 1);
      }
    }
  }
  if (reload_all) {
    // This also notifies the observer.
    ReloadSettings();
  }
}

void PluginExceptionsTableModel::RemoveAll() {
  AutoReset<bool> tmp(&updates_disabled_, true);
  map_->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_PLUGINS);
  if (otr_map_)
    otr_map_->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_PLUGINS);

  ClearSettings();
  if (observer_)
    observer_->OnModelChanged();
}

int PluginExceptionsTableModel::RowCount() {
  return settings_.size();
}

string16 PluginExceptionsTableModel::GetText(int row, int column_id) {
  DCHECK_GE(row, 0);
  DCHECK_LT(row, static_cast<int>(settings_.size()));
  SettingsEntry& entry = settings_[row];
  if (column_id == IDS_EXCEPTIONS_PATTERN_HEADER ||
      column_id == IDS_EXCEPTIONS_HOSTNAME_HEADER) {
    return UTF8ToUTF16(entry.pattern.AsString());
  } else if (column_id == IDS_EXCEPTIONS_ACTION_HEADER) {
    switch (entry.setting) {
      case CONTENT_SETTING_ALLOW:
        return l10n_util::GetStringUTF16(IDS_EXCEPTIONS_ALLOW_BUTTON);
      case CONTENT_SETTING_BLOCK:
        return l10n_util::GetStringUTF16(IDS_EXCEPTIONS_BLOCK_BUTTON);
      default:
        NOTREACHED();
    }
  } else {
    NOTREACHED();
  }
  return string16();
}

bool PluginExceptionsTableModel::HasGroups() {
  return true;
}

void PluginExceptionsTableModel::SetObserver(ui::TableModelObserver* observer) {
  observer_ = observer;
}

ui::TableModel::Groups PluginExceptionsTableModel::GetGroups() {
  return groups_;
}

int PluginExceptionsTableModel::GetGroupID(int row) {
  DCHECK_LT(row, static_cast<int>(settings_.size()));
  return settings_[row].plugin_id;
}

void PluginExceptionsTableModel::Observe(NotificationType type,
                                         const NotificationSource& source,
                                         const NotificationDetails& details) {
  if (!updates_disabled_)
    ReloadSettings();
}

void PluginExceptionsTableModel::ClearSettings() {
  settings_.clear();
  groups_.clear();
  row_counts_.clear();
  resources_.clear();
}

void PluginExceptionsTableModel::GetPlugins(
    std::vector<webkit::npapi::PluginGroup>* plugin_groups) {
  webkit::npapi::PluginList::Singleton()->GetPluginGroups(false, plugin_groups);
}

void PluginExceptionsTableModel::LoadSettings() {
  int group_id = 0;
  std::vector<webkit::npapi::PluginGroup> plugins;
  GetPlugins(&plugins);
  for (size_t i = 0; i < plugins.size(); ++i) {
    std::string plugin = plugins[i].identifier();
    HostContentSettingsMap::SettingsForOneType settings;
    map_->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_PLUGINS,
                                plugin,
                                &settings);
    HostContentSettingsMap::SettingsForOneType otr_settings;
    if (otr_map_) {
      otr_map_->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_PLUGINS,
                                      plugin,
                                      &otr_settings);
    }
    string16 title = plugins[i].GetGroupName();
    for (HostContentSettingsMap::SettingsForOneType::iterator setting_it =
             settings.begin();
         setting_it != settings.end(); ++setting_it) {
      SettingsEntry entry = {
        setting_it->first,
        group_id,
        setting_it->second,
        false
      };
      settings_.push_back(entry);
    }
    for (HostContentSettingsMap::SettingsForOneType::iterator setting_it =
             otr_settings.begin();
         setting_it != otr_settings.end(); ++setting_it) {
      SettingsEntry entry = {
        setting_it->first,
        group_id,
        setting_it->second,
        true
      };
      settings_.push_back(entry);
    }
    int num_plugins = settings.size() + otr_settings.size();
    if (num_plugins > 0) {
      Group group = { title, group_id++ };
      groups_.push_back(group);
      resources_.push_back(plugin);
      row_counts_.push_back(num_plugins);
    }
  }
}

void PluginExceptionsTableModel::ReloadSettings() {
  ClearSettings();
  LoadSettings();

  if (observer_)
    observer_->OnModelChanged();
}