Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Unified Diff: extensions/common/value_counter.cc

Issue 1227093008: Clear an extension's filtered events when a context is destroyed. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: moar Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « extensions/common/value_counter.h ('k') | extensions/renderer/event_bindings.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: extensions/common/value_counter.cc
diff --git a/extensions/common/value_counter.cc b/extensions/common/value_counter.cc
index 4138f251895981b39568d5eed7cb3fc90b27491f..0266b9ac8f95c93ee2095216849d1d73b1320a8b 100644
--- a/extensions/common/value_counter.cc
+++ b/extensions/common/value_counter.cc
@@ -10,47 +10,44 @@
namespace extensions {
-ValueCounter::ValueCounter() {}
+struct ValueCounter::Entry {
+ explicit Entry(scoped_ptr<base::Value> value)
+ : value(value.Pass()), count(1) {}
-ValueCounter::~ValueCounter() {}
+ scoped_ptr<base::Value> value;
+ int count;
+};
-ValueCounter::Entry::Entry(const base::Value& value)
- : value_(value.DeepCopy()), count_(1) {}
-
-ValueCounter::Entry::~Entry() {}
-
-int ValueCounter::Entry::Increment() { return ++count_; }
+ValueCounter::ValueCounter() {
+}
-int ValueCounter::Entry::Decrement() { return --count_; }
+ValueCounter::~ValueCounter() {
+}
-int ValueCounter::Add(const base::Value& value) { return AddImpl(value, true); }
+bool ValueCounter::Add(const base::Value& value) {
+ for (Entry* entry : entries_) {
+ if (entry->value->Equals(&value)) {
+ ++entry->count;
+ return false;
+ }
+ }
+ entries_.push_back(new Entry(value.CreateDeepCopy()));
+ return true;
+}
-int ValueCounter::Remove(const base::Value& value) {
- for (EntryList::iterator it = entries_.begin(); it != entries_.end(); it++) {
- (*it)->value()->GetType();
- if ((*it)->value()->Equals(&value)) {
- int remaining = (*it)->Decrement();
- if (remaining == 0) {
+bool ValueCounter::Remove(const base::Value& value) {
+ for (ScopedVector<Entry>::iterator it = entries_.begin();
+ it != entries_.end(); ++it) {
+ if ((*it)->value->Equals(&value)) {
+ if (--(*it)->count == 0) {
std::swap(*it, entries_.back());
entries_.pop_back();
+ return true; // Removed the last entry.
}
- return remaining;
+ return false; // Removed, but no the last entry.
}
}
- return 0;
-}
-
-int ValueCounter::AddIfMissing(const base::Value& value) {
- return AddImpl(value, false);
-}
-
-int ValueCounter::AddImpl(const base::Value& value, bool increment) {
- for (EntryList::iterator it = entries_.begin(); it != entries_.end(); it++) {
- if ((*it)->value()->Equals(&value))
- return increment ? (*it)->Increment() : (*it)->count();
- }
- entries_.push_back(linked_ptr<Entry>(new Entry(value)));
- return 1;
+ return false; // Nothing to remove.
}
} // namespace extensions
« no previous file with comments | « extensions/common/value_counter.h ('k') | extensions/renderer/event_bindings.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698