Index: chrome/browser/browsing_data/origin_filter_builder.cc |
diff --git a/chrome/browser/browsing_data/origin_filter_builder.cc b/chrome/browser/browsing_data/origin_filter_builder.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2a458eaebed2b2d093d81fe1fce3cc9bb794034a |
--- /dev/null |
+++ b/chrome/browser/browsing_data/origin_filter_builder.cc |
@@ -0,0 +1,108 @@ |
+// Copyright 2016 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/browsing_data/origin_filter_builder.h" |
+ |
+#include <string> |
+#include <vector> |
+ |
+#include "base/bind.h" |
+ |
+OriginFilterBuilder::OriginFilterBuilder(Mode mode) |
+ : mode_(mode), |
+ built_(false) { |
+} |
+ |
+OriginFilterBuilder::~OriginFilterBuilder() { |
+} |
+ |
+OriginFilterBuilder* OriginFilterBuilder::AddOrigin(const url::Origin& origin) { |
+ if (built_) { |
+ NOTREACHED() << "Can not change build parameters after " |
+ "a filter was already built."; |
+ return this; |
Timo Reimann
2016/02/11 22:46:43
This and other usages of "if (condition) { NOTREAC
msramek
2016/02/12 14:24:50
Done.
|
+ } |
+ |
+ // By limiting the filter to non-unique origins, we can guarantee that |
+ // origin1 < origin2 && origin1 > origin2 <=> origin1.isSameOrigin(origin2). |
+ // This means that std::set::find() will use the same semantics for |
+ // origin comparison as Origin::IsSameOriginWith(). Furthermore, this |
+ // means that two filters are equal iff they are equal element-wise. |
+ if (origin.unique()) { |
+ NOTREACHED() << "Invalid origin passed into OriginFilter."; |
+ return this; |
+ } |
+ |
+ // TODO(msramek): All urls with file scheme currently map to the same |
+ // origin. This is currently not a problem, but if it becomes one, |
+ // consider recognizing the URL path. |
+ |
+ origin_list_.insert(origin); |
+ |
+ return this; |
+} |
+ |
+OriginFilterBuilder* OriginFilterBuilder::ChangeMode(Mode mode) { |
+ if (built_) { |
+ NOTREACHED() << "Can not change build parameters after " |
+ "a filter was already built."; |
+ return this; |
+ } |
+ |
+ mode_ = mode; |
+ return this; |
+} |
+ |
+base::Callback<bool(const GURL&)> |
+ OriginFilterBuilder::BuildSameOriginFilter() { |
+ built_ = true; |
+ return base::Bind(&OriginFilterBuilder::MatchesURL, base::Unretained(this)); |
+} |
+ |
+base::Callback<bool(const GURL&)> |
+ OriginFilterBuilder::BuildDomainFilter() { |
+ built_ = true; |
+ return base::Bind(&OriginFilterBuilder::MatchesURLWithSubdomains, |
+ base::Unretained(this)); |
+} |
+ |
+bool OriginFilterBuilder::MatchesURL(const GURL& url) const { |
+ return ((origin_list_.find(url::Origin(url)) != origin_list_.end()) == |
+ (mode_ == WHITELIST)); |
+} |
+ |
+bool OriginFilterBuilder::MatchesURLWithSubdomains(const GURL& url) const { |
+ if (origin_list_.empty()) |
+ return mode_ == BLACKLIST; |
+ |
+ // If there is no concept of subdomains, simply delegate to MatchesURL(). |
+ if (url.HostIsIPAddress()) |
+ return MatchesURL(url); |
+ |
+ // TODO(msramek): We do not expect filters to be particularly large. |
+ // If they are, replace std::set with a trie for faster searching. |
+ int port = url.EffectiveIntPort(); |
+ base::StringPiece host_piece = url.host_piece(); |
+ for (size_t i = 0; i < host_piece.length(); ++i) { |
+ if (i != 0 && host_piece[i - 1] != '.') |
+ continue; |
+ |
+ url::Origin origin_with_removed_subdomain = |
+ url::Origin::UnsafelyCreateOriginWithoutNormalization( |
+ url.scheme_piece(), |
+ host_piece.substr(i), |
+ port); |
+ |
+ // If we recognize the origin, return true for whitelist and false |
+ // for blacklist. |
+ if (origin_list_.find(url::Origin(origin_with_removed_subdomain)) |
+ != origin_list_.end()) { |
+ return mode_ == WHITELIST; |
+ } |
+ } |
+ |
+ // We do not recognize the URL. Return false for whitelist mode and true |
+ // for blacklist mode. |
+ return mode_ == BLACKLIST; |
+} |