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..277bb4e98cfe08512cb3a29d8ca8f089d6d40f99 |
--- /dev/null |
+++ b/chrome/browser/browsing_data/origin_filter_builder.cc |
@@ -0,0 +1,112 @@ |
+// 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" |
+ |
+namespace { |
+ |
+bool EmptyFilter(const GURL& url) { |
+ return true; |
+} |
+ |
+} // namespace |
+ |
+OriginFilterBuilder::OriginFilterBuilder(Mode mode) |
+ : mode_(mode), |
+ built_(false) { |
+} |
+ |
+OriginFilterBuilder::~OriginFilterBuilder() { |
+} |
+ |
+OriginFilterBuilder* OriginFilterBuilder::AddOrigin(const url::Origin& origin) { |
+ DCHECK (!built_) << "Can not change build parameters after a filter was " |
+ "already built."; |
+ |
+ // 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. |
+ DCHECK(!origin.unique()) << "Invalid origin passed into OriginFilter."; |
+ |
+ // 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) { |
+ DCHECK(!built_) << "Can not change build parameters after a filter was " |
+ "already built."; |
+ |
+ mode_ = mode; |
+ return this; |
+} |
+ |
+base::Callback<bool(const GURL&)> |
+ OriginFilterBuilder::BuildSameOriginFilter() { |
+ built_ = true; |
+ return base::Bind(&OriginFilterBuilder::MatchesURL, base::Unretained(this)); |
Timo Reimann
2016/02/17 14:59:23
As discussed with Martin, the current approach req
msramek
2016/02/18 12:48:43
Done. Changed this to base::Owned, the callback no
|
+} |
+ |
+base::Callback<bool(const GURL&)> |
+ OriginFilterBuilder::BuildDomainFilter() { |
+ built_ = true; |
+ return base::Bind(&OriginFilterBuilder::MatchesURLWithSubdomains, |
+ base::Unretained(this)); |
Mike West
2016/02/17 15:22:10
Why is Unretained safe here?
msramek
2016/02/18 12:48:43
It's not :) See my other comments.
|
+} |
+ |
+// static |
+base::Callback<bool(const GURL&)> OriginFilterBuilder::BuildEmptyFilter() { |
Mike West
2016/02/17 15:22:10
Perhaps "NoopFilter"? An empty BLACKLIST has a dif
msramek
2016/02/18 12:48:42
Done. Yes, I agree.
|
+ return base::Bind(&EmptyFilter); |
+} |
+ |
+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; |
+} |