summaryrefslogtreecommitdiffstats
path: root/chrome/browser/notifications/notification_exceptions_table_model.cc
blob: 31a44d5549a7045f9025be245625dcedd589d804 (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
// 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/notifications/notification_exceptions_table_model.h"

#include "app/l10n_util.h"
#include "app/table_model_observer.h"
#include "base/utf_string_conversions.h"
#include "chrome/common/content_settings.h"
#include "chrome/common/content_settings_helper.h"
#include "chrome/common/content_settings_types.h"
#include "chrome/common/url_constants.h"
#include "grit/generated_resources.h"

struct NotificationExceptionsTableModel::Entry {
  Entry(const GURL& origin, ContentSetting setting);
  bool operator<(const Entry& b) const;

  GURL origin;
  ContentSetting setting;
};

NotificationExceptionsTableModel::NotificationExceptionsTableModel(
    DesktopNotificationService* service)
    : service_(service),
      observer_(NULL) {
  std::vector<GURL> allowed(service_->GetAllowedOrigins());
  std::vector<GURL> blocked(service_->GetBlockedOrigins());
  entries_.reserve(allowed.size() + blocked.size());
  for (size_t i = 0; i < allowed.size(); ++i)
    entries_.push_back(Entry(allowed[i], CONTENT_SETTING_ALLOW));
  for (size_t i = 0; i < blocked.size(); ++i)
    entries_.push_back(Entry(blocked[i], CONTENT_SETTING_BLOCK));
  sort(entries_.begin(), entries_.end());
}

NotificationExceptionsTableModel::~NotificationExceptionsTableModel() {}

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

void NotificationExceptionsTableModel::RemoveRows(const Rows& rows) {
  // This is O(n^2) in rows.size(). Since n is small, that's ok.
  for (Rows::const_reverse_iterator i(rows.rbegin()); i != rows.rend(); ++i) {
    size_t row = *i;
    Entry* entry = &entries_[row];
    if (entry->setting == CONTENT_SETTING_ALLOW) {
      service_->ResetAllowedOrigin(entry->origin);
    } else {
      DCHECK_EQ(entry->setting, CONTENT_SETTING_BLOCK);
      service_->ResetBlockedOrigin(entry->origin);
    }
    entries_.erase(entries_.begin() + row);  // Note: |entry| is now garbage.
    if (observer_)
      observer_->OnItemsRemoved(row, 1);
  }
}

void NotificationExceptionsTableModel::RemoveAll() {
  int old_row_count = RowCount();
  entries_.clear();
  service_->ResetAllOrigins();
  if (observer_)
    observer_->OnItemsRemoved(0, old_row_count);
}

int NotificationExceptionsTableModel::RowCount() {
  return static_cast<int>(entries_.size());
}

std::wstring NotificationExceptionsTableModel::GetText(int row,
                                                       int column_id) {
  const Entry& entry = entries_[row];
  if (column_id == IDS_EXCEPTIONS_HOSTNAME_HEADER) {
    return content_settings_helper::OriginToWString(entry.origin);
  }

  if (column_id == IDS_EXCEPTIONS_ACTION_HEADER) {
    switch (entry.setting) {
      case CONTENT_SETTING_ALLOW:
        return l10n_util::GetString(IDS_EXCEPTIONS_ALLOW_BUTTON);
      case CONTENT_SETTING_BLOCK:
        return l10n_util::GetString(IDS_EXCEPTIONS_BLOCK_BUTTON);
      default:
        break;
    }
  }

  NOTREACHED();
  return std::wstring();
}

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

NotificationExceptionsTableModel::Entry::Entry(
    const GURL& in_origin,
    ContentSetting in_setting)
    : origin(in_origin),
      setting(in_setting) {
}

bool NotificationExceptionsTableModel::Entry::operator<(
    const NotificationExceptionsTableModel::Entry& b) const {
  DCHECK_NE(origin, b.origin);
  return origin < b.origin;
}