Implementation of leveldb-backed PrefStore.
[chromium-blink-merge.git] / extensions / browser / error_map.cc
blobdfcef9fd05acf790b438653409d69a9289eb8857
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"
9 #include "extensions/common/extension.h"
11 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 } // namespace
21 ////////////////////////////////////////////////////////////////////////////////
22 // ErrorMap::ExtensionEntry
23 ErrorMap::ExtensionEntry::ExtensionEntry() {
26 ErrorMap::ExtensionEntry::~ExtensionEntry() {
27 DeleteAllErrors();
30 void ErrorMap::ExtensionEntry::DeleteAllErrors() {
31 STLDeleteContainerPointers(list_.begin(), list_.end());
32 list_.clear();
35 void ErrorMap::ExtensionEntry::DeleteIncognitoErrors() {
36 ErrorList::iterator iter = list_.begin();
37 while (iter != list_.end()) {
38 if ((*iter)->from_incognito()) {
39 delete *iter;
40 iter = list_.erase(iter);
41 } else {
42 ++iter;
47 void ErrorMap::ExtensionEntry::DeleteErrorsOfType(ExtensionError::Type type) {
48 ErrorList::iterator iter = list_.begin();
49 while (iter != list_.end()) {
50 if ((*iter)->type() == type) {
51 delete *iter;
52 iter = list_.erase(iter);
53 } else {
54 ++iter;
59 const ExtensionError* ErrorMap::ExtensionEntry::AddError(
60 scoped_ptr<ExtensionError> error) {
61 for (ErrorList::iterator iter = list_.begin(); iter != list_.end(); ++iter) {
62 // If we find a duplicate error, remove the old error and add the new one,
63 // incrementing the occurrence count of the error. We use the new error
64 // for runtime errors, so we can link to the latest context, inspectable
65 // view, etc.
66 if (error->IsEqual(*iter)) {
67 error->set_occurrences((*iter)->occurrences() + 1);
68 delete *iter;
69 list_.erase(iter);
70 break;
74 // If there are too many errors for an extension already, limit ourselves to
75 // the most recent ones.
76 if (list_.size() >= kMaxErrorsPerExtension) {
77 delete list_.front();
78 list_.pop_front();
81 list_.push_back(error.release());
82 return list_.back();
85 ////////////////////////////////////////////////////////////////////////////////
86 // ErrorMap
87 ErrorMap::ErrorMap() {
90 ErrorMap::~ErrorMap() {
91 RemoveAllErrors();
94 const ErrorList& ErrorMap::GetErrorsForExtension(
95 const std::string& extension_id) const {
96 EntryMap::const_iterator iter = map_.find(extension_id);
97 return iter != map_.end() ? *iter->second->list() : g_empty_error_list.Get();
100 const ExtensionError* ErrorMap::AddError(scoped_ptr<ExtensionError> error) {
101 EntryMap::iterator iter = map_.find(error->extension_id());
102 if (iter == map_.end()) {
103 iter = map_.insert(std::pair<std::string, ExtensionEntry*>(
104 error->extension_id(), new ExtensionEntry)).first;
106 return iter->second->AddError(error.Pass());
109 void ErrorMap::Remove(const std::string& extension_id) {
110 EntryMap::iterator iter = map_.find(extension_id);
111 if (iter == map_.end())
112 return;
114 delete iter->second;
115 map_.erase(iter);
118 void ErrorMap::RemoveErrorsForExtensionOfType(const std::string& extension_id,
119 ExtensionError::Type type) {
120 EntryMap::iterator iter = map_.find(extension_id);
121 if (iter != map_.end())
122 iter->second->DeleteErrorsOfType(type);
125 void ErrorMap::RemoveIncognitoErrors() {
126 for (EntryMap::iterator iter = map_.begin(); iter != map_.end(); ++iter)
127 iter->second->DeleteIncognitoErrors();
130 void ErrorMap::RemoveAllErrors() {
131 for (EntryMap::iterator iter = map_.begin(); iter != map_.end(); ++iter) {
132 iter->second->DeleteAllErrors();
133 delete iter->second;
135 map_.clear();
138 } // namespace extensions