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

Unified Diff: chrome/browser/extensions/api/declarative/substring_set_matcher.cc

Issue 9390018: Implementation of a Matching strategy for URLs in the Declarative WebRequest API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: MSVC does not support EXPECT_NE on iterators Created 8 years, 10 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
Index: chrome/browser/extensions/api/declarative/substring_set_matcher.cc
diff --git a/chrome/browser/extensions/api/declarative/substring_set_matcher.cc b/chrome/browser/extensions/api/declarative/substring_set_matcher.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1b02a6697559407e9854417aaed9155b04d54fcc
--- /dev/null
+++ b/chrome/browser/extensions/api/declarative/substring_set_matcher.cc
@@ -0,0 +1,77 @@
+// 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.
+
+#include "chrome/browser/extensions/api/declarative/substring_set_matcher.h"
+
+#include "base/logging.h"
+#include "base/stl_util.h"
+
+namespace extensions {
+
+//
+// SubstringPattern
+//
+
+SubstringPattern::SubstringPattern(const std::string& pattern,
+ SubstringPattern::ID id)
+ : pattern_(pattern), id_(id) {}
+
+SubstringPattern::~SubstringPattern() {}
+
+bool SubstringPattern::operator<(const SubstringPattern& rhs) const {
+ if (id_ < rhs.id_) return true;
+ if (id_ > rhs.id_) return false;
+ return pattern_ < rhs.pattern_;
+}
+
+//
+// SubstringSetMatcher
+//
+
+SubstringSetMatcher::SubstringSetMatcher() {}
+
+SubstringSetMatcher::~SubstringSetMatcher() {}
+
+void SubstringSetMatcher::RegisterPatterns(
+ const std::vector<const SubstringPattern*>& rules) {
+ for (std::vector<const SubstringPattern*>::const_iterator i =
+ rules.begin(); i != rules.end(); ++i) {
+ DCHECK(patterns_.find((*i)->id()) == patterns_.end());
+ patterns_[(*i)->id()] = *i;
+ }
+}
+
+void SubstringSetMatcher::UnregisterPatterns(
+ const std::vector<const SubstringPattern*>& patterns) {
+ for (std::vector<const SubstringPattern*>::const_iterator i =
+ patterns.begin(); i != patterns.end(); ++i) {
+ patterns_.erase((*i)->id());
+ }
+}
+
+void SubstringSetMatcher::RegisterAndUnregisterPatterns(
+ const std::vector<const SubstringPattern*>& to_register,
+ const std::vector<const SubstringPattern*>& to_unregister) {
+ // In the Aho-Corasick implementation this will change, the main
+ // implementation will be here, and RegisterPatterns/UnregisterPatterns
+ // will delegate to this version.
+ RegisterPatterns(to_register);
+ UnregisterPatterns(to_unregister);
+}
+
+bool SubstringSetMatcher::Match(const std::string& text,
+ std::set<SubstringPattern::ID>* matches) const {
+ for (SubstringPatternSet::const_iterator i = patterns_.begin();
+ i != patterns_.end(); ++i) {
+ if (text.find(i->second->pattern()) != std::string::npos) {
+ if (matches)
+ matches->insert(i->second->id());
+ else
+ return true;
+ }
+ }
+ return matches && !matches->empty();
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698