summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/value_counter.h
diff options
context:
space:
mode:
authorkoz@chromium.org <koz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-25 11:39:00 +0000
committerkoz@chromium.org <koz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-25 11:39:00 +0000
commit6ca5ff2de3f35d5c657bfe6c4e7e0dff33204f7b (patch)
treee2a9afd13d21d4d5ec07ca4e5c14aa460de6fb5c /chrome/common/extensions/value_counter.h
parent1f1c0a19ad71c0e279e5f207c7f5f525550c2e33 (diff)
downloadchromium_src-6ca5ff2de3f35d5c657bfe6c4e7e0dff33204f7b.zip
chromium_src-6ca5ff2de3f35d5c657bfe6c4e7e0dff33204f7b.tar.gz
chromium_src-6ca5ff2de3f35d5c657bfe6c4e7e0dff33204f7b.tar.bz2
Filtered events.
Makes web_navigation events support filters, eg: chrome.webNavigation.onBeforeCommitted.addListener(callback, {url: [{hostSuffix: 'google.com'}]}); Now callback will only be called when the event has a URL with a host suffix of google.com. BUG=121479 Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=143872 Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=143874 Review URL: https://chromiumcodereview.appspot.com/10514013 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@143896 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/extensions/value_counter.h')
-rw-r--r--chrome/common/extensions/value_counter.h72
1 files changed, 72 insertions, 0 deletions
diff --git a/chrome/common/extensions/value_counter.h b/chrome/common/extensions/value_counter.h
new file mode 100644
index 0000000..52c719c
--- /dev/null
+++ b/chrome/common/extensions/value_counter.h
@@ -0,0 +1,72 @@
+// Copyright (c) 2012 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.
+
+#ifndef CHROME_COMMON_EXTENSIONS_VALUE_COUNTER_H_
+#define CHROME_COMMON_EXTENSIONS_VALUE_COUNTER_H_
+#pragma once
+
+#include "base/memory/linked_ptr.h"
+
+#include <vector>
+
+namespace base {
+class Value;
+}
+
+namespace extensions {
+
+// Keeps a running count of Values, like map<Value, int>. Adding / removing
+// values increments / decrements the count associated with a given Value.
+//
+// Add() and Remove() are linear in the number of Values in the ValueCounter,
+// because there is no operator<() defined on Value, so we must iterate to find
+// whether a Value is equal to an existing one.
+class ValueCounter {
+ public:
+ ValueCounter();
+ ~ValueCounter();
+
+ // Adds |value| to the set and returns how many equal values are in the set
+ // after. Does not take ownership of |value|. In the case where a Value equal
+ // to |value| doesn't already exist in this map, this function makes a
+ // DeepCopy() of |value|.
+ int Add(const base::Value& value);
+
+ // Removes |value| from the set and returns how many equal values are in
+ // the set after.
+ int Remove(const base::Value& value);
+
+ // Same as Add() but only performs the add if the value isn't present.
+ int AddIfMissing(const base::Value& value);
+
+ private:
+ class Entry {
+ public:
+ explicit Entry(const base::Value& value);
+ ~Entry();
+
+ int Increment();
+ int Decrement();
+
+ const base::Value* value() const { return value_.get(); }
+ int count() const { return count_; }
+
+ private:
+ linked_ptr<base::Value> value_;
+ int count_;
+
+ DISALLOW_COPY_AND_ASSIGN(Entry);
+ };
+ typedef std::vector<linked_ptr<Entry> > EntryList;
+
+ int AddImpl(const base::Value& value, bool increment);
+
+ EntryList entries_;
+
+ DISALLOW_COPY_AND_ASSIGN(ValueCounter);
+};
+
+} // namespace extensions
+
+#endif // CHROME_COMMON_EXTENSIONS_VALUE_COUNTER_H_