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

Unified Diff: net/base/stale_while_revalidate_experiment_domains.cc

Issue 1433893002: Add StaleWhileRevalidateExperiment histograms. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@s-w-r-dafsa
Patch Set: Fix compile error. Created 5 years, 1 month 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: net/base/stale_while_revalidate_experiment_domains.cc
diff --git a/net/base/stale_while_revalidate_experiment_domains.cc b/net/base/stale_while_revalidate_experiment_domains.cc
new file mode 100644
index 0000000000000000000000000000000000000000..6e901843bbcc9919119ff4253838d6033a551def
--- /dev/null
+++ b/net/base/stale_while_revalidate_experiment_domains.cc
@@ -0,0 +1,76 @@
+// Copyright 2015 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 "net/base/stale_while_revalidate_experiment_domains.h"
+
+#include <stddef.h>
+
+#include "base/logging.h"
+#include "net/base/lookup_string_in_fixed_set.h"
+
+namespace net {
+
+namespace {
+
+#include "net/base/stale_while_revalidate_experiment_domains-inc.cc"
+
+// The maximum number of dots in any domain in the list. This is used to ignore
+// parts of the host name that are irrelevant, and so must be correct.
+const int kMaxDots = 2;
+
+// The minimum number of dots in a host necessary for it to be considered a
+// match.
+const int kMinDots = 1;
+
+const size_t npos = base::StringPiece::npos;
+
+bool LookupTail(const base::StringPiece& host, size_t pos) {
+ DCHECK_LT(pos, host.size());
+ return LookupStringInFixedSet(kDafsa, sizeof(kDafsa), host.data() + pos,
+ host.size() - pos) == 0;
+}
+
+bool LookupTrimmedHost(const base::StringPiece& trimmed) {
+ // |trimmed| contains at least one non-dot. The maximum number of dots we want
+ // to look for is kMaxInterestingDots; any dots before that will not affect
+ // the outcome of the match.
+ const int kMaxInterestingDots = kMaxDots + 1;
+
+ // Scan |trimmed| from the right for up to kMaxInterestingDots dots, checking
+ // for a domain match at each position.
+ int found_dots = 0;
+ size_t pos = npos;
+ while (found_dots < kMaxInterestingDots) {
+ pos = trimmed.find_last_of('.', pos);
+ if (pos == npos)
+ break;
+ ++found_dots;
+ if (found_dots > kMinDots && LookupTail(trimmed, pos + 1))
+ return true;
+ if (pos == 0)
+ return false;
+ --pos;
+ }
+
+ if (found_dots >= kMinDots && found_dots <= kMaxDots) {
+ // We might have an exact match.
+ return LookupTail(trimmed, 0);
+ }
+
+ return false;
+}
+
+} // namespace
+
+bool IsHostInStaleWhileRevalidateExperimentDomain(
+ const base::StringPiece& host) {
+ // Ignore trailing dots.
+ size_t last_interesting_pos = host.find_last_not_of('.');
+ if (last_interesting_pos == npos)
+ return false;
+
+ return LookupTrimmedHost(host.substr(0, last_interesting_pos + 1));
+}
+
+} // namespace net
« no previous file with comments | « net/base/stale_while_revalidate_experiment_domains.h ('k') | net/base/stale_while_revalidate_experiment_domains.gperf » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698