Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / extensions / value_counter.cc
blob156e3adef00263f9265dfeb90a7477ba517bbef0
1 // Copyright (c) 2012 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 "chrome/common/extensions/value_counter.h"
7 #include "base/values.h"
9 #include <algorithm>
11 namespace extensions {
13 ValueCounter::ValueCounter() {
16 ValueCounter::~ValueCounter() {
19 ValueCounter::Entry::Entry(const base::Value& value)
20 : value_(value.DeepCopy()),
21 count_(1) {
24 ValueCounter::Entry::~Entry() {
27 int ValueCounter::Entry::Increment() {
28 return ++count_;
31 int ValueCounter::Entry::Decrement() {
32 return --count_;
35 int ValueCounter::Add(const base::Value& value) {
36 return AddImpl(value, true);
39 int ValueCounter::Remove(const base::Value& value) {
40 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); it++) {
41 (*it)->value()->GetType();
42 if ((*it)->value()->Equals(&value)) {
43 int remaining = (*it)->Decrement();
44 if (remaining == 0) {
45 std::swap(*it, entries_.back());
46 entries_.pop_back();
48 return remaining;
51 return 0;
54 int ValueCounter::AddIfMissing(const base::Value& value) {
55 return AddImpl(value, false);
58 int ValueCounter::AddImpl(const base::Value& value, bool increment) {
59 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); it++) {
60 if ((*it)->value()->Equals(&value))
61 return increment ? (*it)->Increment() : (*it)->count();
63 entries_.push_back(linked_ptr<Entry>(new Entry(value)));
64 return 1;
67 } // namespace extensions