summaryrefslogtreecommitdiffstats
path: root/chrome/browser/content_exceptions_table_model.cc
blob: dad02b92bc3a64b767fcd4a0c23cf7ec5c16a1fd (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
// Copyright (c) 2010 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/content_exceptions_table_model.h"

#include "app/l10n_util.h"
#include "app/table_model_observer.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/content_settings/host_content_settings_map.h"
#include "grit/generated_resources.h"

ContentExceptionsTableModel::ContentExceptionsTableModel(
    HostContentSettingsMap* map,
    HostContentSettingsMap* off_the_record_map,
    ContentSettingsType type)
    : map_(map),
      off_the_record_map_(off_the_record_map),
      content_type_(type),
      observer_(NULL) {
  // Load the contents.
  map->GetSettingsForOneType(type, "", &entries_);
  if (off_the_record_map) {
    off_the_record_map->GetSettingsForOneType(type,
                                              "",
                                              &off_the_record_entries_);
  }
}

ContentExceptionsTableModel::~ContentExceptionsTableModel() {}

void ContentExceptionsTableModel::AddException(
    const ContentSettingsPattern& original_pattern,
    ContentSetting setting,
    bool is_off_the_record) {
  DCHECK(!is_off_the_record || off_the_record_map_);

  int insert_position =
      is_off_the_record ? RowCount() : static_cast<int>(entries_.size());

  const ContentSettingsPattern pattern(
      original_pattern.CanonicalizePattern());

  entries(is_off_the_record).push_back(
      HostContentSettingsMap::PatternSettingPair(pattern, setting));
  map(is_off_the_record)->SetContentSetting(pattern,
                                            content_type_,
                                            "",
                                            setting);
  if (observer_)
    observer_->OnItemsAdded(insert_position, 1);
}

void ContentExceptionsTableModel::RemoveException(int row) {
  bool is_off_the_record = entry_is_off_the_record(row);
  int position_to_delete =
      is_off_the_record ? row - static_cast<int>(entries_.size()) : row;
  const HostContentSettingsMap::PatternSettingPair& pair = entry_at(row);

  map(is_off_the_record)->SetContentSetting(
      pair.first, content_type_, "", CONTENT_SETTING_DEFAULT);
  entries(is_off_the_record).erase(
      entries(is_off_the_record).begin() + position_to_delete);
  if (observer_)
    observer_->OnItemsRemoved(row, 1);
}

void ContentExceptionsTableModel::RemoveAll() {
  int old_row_count = RowCount();
  entries_.clear();
  off_the_record_entries_.clear();
  map_->ClearSettingsForOneType(content_type_);
  if (off_the_record_map_)
    off_the_record_map_->ClearSettingsForOneType(content_type_);
  if (observer_)
    observer_->OnItemsRemoved(0, old_row_count);
}

int ContentExceptionsTableModel::IndexOfExceptionByPattern(
    const ContentSettingsPattern& original_pattern,
    bool is_off_the_record) {
  DCHECK(!is_off_the_record || off_the_record_map_);

  int offset =
      is_off_the_record ? static_cast<int>(entries_.size()) : 0;

  const ContentSettingsPattern pattern(
      original_pattern.CanonicalizePattern());

  // This is called on every key type in the editor. Move to a map if we end up
  // with lots of exceptions.
  for (size_t i = 0; i < entries(is_off_the_record).size(); ++i) {
    if (entries(is_off_the_record)[i].first == pattern)
      return offset + static_cast<int>(i);
  }
  return -1;
}

int ContentExceptionsTableModel::RowCount() {
  return static_cast<int>(entries_.size() + off_the_record_entries_.size());
}

std::wstring ContentExceptionsTableModel::GetText(int row, int column_id) {
  HostContentSettingsMap::PatternSettingPair entry = entry_at(row);

  switch (column_id) {
    case IDS_EXCEPTIONS_PATTERN_HEADER:
      return UTF8ToWide(entry.first.AsString());

    case IDS_EXCEPTIONS_ACTION_HEADER:
      switch (entry.second) {
        case CONTENT_SETTING_ALLOW:
          return UTF16ToWideHack(
              l10n_util::GetStringUTF16(IDS_EXCEPTIONS_ALLOW_BUTTON));
        case CONTENT_SETTING_BLOCK:
          return UTF16ToWideHack(
              l10n_util::GetStringUTF16(IDS_EXCEPTIONS_BLOCK_BUTTON));
        case CONTENT_SETTING_ASK:
          return UTF16ToWideHack(
              l10n_util::GetStringUTF16(IDS_EXCEPTIONS_ASK_BUTTON));
        case CONTENT_SETTING_SESSION_ONLY:
          return UTF16ToWideHack(
              l10n_util::GetStringUTF16(IDS_EXCEPTIONS_SESSION_ONLY_BUTTON));
        default:
          NOTREACHED();
      }
      break;

    default:
      NOTREACHED();
  }

  return std::wstring();
}

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