Remove unused parameter.
[chromium-blink-merge.git] / extensions / browser / error_map.cc
blob0495ae0841cdf359c0999ffcd5996ecf822da6c2
1 // Copyright 2014 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.
5 #include "extensions/browser/error_map.h"
7 #include "base/lazy_instance.h"
8 #include "base/stl_util.h"
10 namespace extensions {
12 namespace {
14 // The maximum number of errors to be stored per extension.
15 const size_t kMaxErrorsPerExtension = 100;
17 base::LazyInstance<ErrorList> g_empty_error_list = LAZY_INSTANCE_INITIALIZER;
19 // An incrementing counter for the next error id. Overflowing this is very
20 // unlikely, since the number of errors per extension is capped at 100.
21 int kNextErrorId = 1;
23 } // namespace
25 ////////////////////////////////////////////////////////////////////////////////
26 // ErrorMap::Filter
27 ErrorMap::Filter::Filter(const std::string& restrict_to_extension_id,
28 int restrict_to_type,
29 const std::set<int>& restrict_to_ids,
30 bool restrict_to_incognito)
31 : restrict_to_extension_id(restrict_to_extension_id),
32 restrict_to_type(restrict_to_type),
33 restrict_to_ids(restrict_to_ids),
34 restrict_to_incognito(restrict_to_incognito) {
37 ErrorMap::Filter::~Filter() {
40 ErrorMap::Filter ErrorMap::Filter::ErrorsForExtension(
41 const std::string& extension_id) {
42 return Filter(extension_id, -1, std::set<int>(), false);
45 ErrorMap::Filter ErrorMap::Filter::ErrorsForExtensionWithType(
46 const std::string& extension_id,
47 ExtensionError::Type type) {
48 return Filter(extension_id, type, std::set<int>(), false);
51 ErrorMap::Filter ErrorMap::Filter::ErrorsForExtensionWithIds(
52 const std::string& extension_id,
53 const std::set<int>& ids) {
54 return Filter(extension_id, -1, ids, false);
57 ErrorMap::Filter ErrorMap::Filter::ErrorsForExtensionWithTypeAndIds(
58 const std::string& extension_id,
59 ExtensionError::Type type,
60 const std::set<int>& ids) {
61 return Filter(extension_id, type, ids, false);
64 ErrorMap::Filter ErrorMap::Filter::IncognitoErrors() {
65 return Filter(std::string(), -1, std::set<int>(), true);
68 bool ErrorMap::Filter::Matches(const ExtensionError* error) const {
69 if (restrict_to_type != -1 && restrict_to_type != error->type())
70 return false;
71 if (restrict_to_incognito && !error->from_incognito())
72 return false;
73 if (!restrict_to_extension_id.empty() &&
74 error->extension_id() != restrict_to_extension_id)
75 return false;
76 if (!restrict_to_ids.empty() && restrict_to_ids.count(error->id()) == 0)
77 return false;
78 return true;
81 ////////////////////////////////////////////////////////////////////////////////
82 // ErrorMap::ExtensionEntry
83 class ErrorMap::ExtensionEntry {
84 public:
85 ExtensionEntry();
86 ~ExtensionEntry();
88 // Delete any errors in the entry that match the given ids and type, if
89 // provided.
90 void DeleteErrors(const ErrorMap::Filter& filter);
91 // Delete all errors in the entry.
92 void DeleteAllErrors();
94 // Add the error to the list, and return a weak reference.
95 const ExtensionError* AddError(scoped_ptr<ExtensionError> error);
97 const ErrorList* list() const { return &list_; }
99 private:
100 // The list of all errors associated with the extension. The errors are
101 // owned by the Entry (in turn owned by the ErrorMap) and are deleted upon
102 // destruction.
103 ErrorList list_;
105 DISALLOW_COPY_AND_ASSIGN(ExtensionEntry);
108 ErrorMap::ExtensionEntry::ExtensionEntry() {
111 ErrorMap::ExtensionEntry::~ExtensionEntry() {
112 DeleteAllErrors();
115 void ErrorMap::ExtensionEntry::DeleteErrors(const Filter& filter) {
116 for (ErrorList::iterator iter = list_.begin(); iter != list_.end();) {
117 if (filter.Matches(*iter)) {
118 delete *iter;
119 iter = list_.erase(iter);
120 } else {
121 ++iter;
126 void ErrorMap::ExtensionEntry::DeleteAllErrors() {
127 STLDeleteContainerPointers(list_.begin(), list_.end());
128 list_.clear();
131 const ExtensionError* ErrorMap::ExtensionEntry::AddError(
132 scoped_ptr<ExtensionError> error) {
133 for (ErrorList::iterator iter = list_.begin(); iter != list_.end(); ++iter) {
134 // If we find a duplicate error, remove the old error and add the new one,
135 // incrementing the occurrence count of the error. We use the new error
136 // for runtime errors, so we can link to the latest context, inspectable
137 // view, etc.
138 if (error->IsEqual(*iter)) {
139 error->set_occurrences((*iter)->occurrences() + 1);
140 error->set_id((*iter)->id());
141 delete *iter;
142 list_.erase(iter);
143 break;
147 // If there are too many errors for an extension already, limit ourselves to
148 // the most recent ones.
149 if (list_.size() >= kMaxErrorsPerExtension) {
150 delete list_.front();
151 list_.pop_front();
154 if (error->id() == 0)
155 error->set_id(kNextErrorId++);
157 list_.push_back(error.release());
158 return list_.back();
161 ////////////////////////////////////////////////////////////////////////////////
162 // ErrorMap
163 ErrorMap::ErrorMap() {
166 ErrorMap::~ErrorMap() {
167 RemoveAllErrors();
170 const ErrorList& ErrorMap::GetErrorsForExtension(
171 const std::string& extension_id) const {
172 EntryMap::const_iterator iter = map_.find(extension_id);
173 return iter != map_.end() ? *iter->second->list() : g_empty_error_list.Get();
176 const ExtensionError* ErrorMap::AddError(scoped_ptr<ExtensionError> error) {
177 EntryMap::iterator iter = map_.find(error->extension_id());
178 if (iter == map_.end()) {
179 iter = map_.insert(std::pair<std::string, ExtensionEntry*>(
180 error->extension_id(), new ExtensionEntry)).first;
182 return iter->second->AddError(error.Pass());
185 void ErrorMap::RemoveErrors(const Filter& filter) {
186 if (!filter.restrict_to_extension_id.empty()) {
187 EntryMap::iterator iter = map_.find(filter.restrict_to_extension_id);
188 if (iter != map_.end())
189 iter->second->DeleteErrors(filter);
190 } else {
191 for (auto& key_val : map_)
192 key_val.second->DeleteErrors(filter);
196 void ErrorMap::RemoveAllErrors() {
197 for (EntryMap::iterator iter = map_.begin(); iter != map_.end(); ++iter)
198 delete iter->second;
199 map_.clear();
202 } // namespace extensions