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

Side by Side 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 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/base/stale_while_revalidate_experiment_domains.h"
6
7 #include <stddef.h>
8
9 #include "base/logging.h"
10 #include "net/base/lookup_string_in_fixed_set.h"
11
12 namespace net {
13
14 namespace {
15
16 #include "net/base/stale_while_revalidate_experiment_domains-inc.cc"
17
18 // The maximum number of dots in any domain in the list. This is used to ignore
19 // parts of the host name that are irrelevant, and so must be correct.
20 const int kMaxDots = 2;
21
22 // The minimum number of dots in a host necessary for it to be considered a
23 // match.
24 const int kMinDots = 1;
25
26 const size_t npos = base::StringPiece::npos;
27
28 bool LookupTail(const base::StringPiece& host, size_t pos) {
29 DCHECK_LT(pos, host.size());
30 return LookupStringInFixedSet(kDafsa, sizeof(kDafsa), host.data() + pos,
31 host.size() - pos) == 0;
32 }
33
34 bool LookupTrimmedHost(const base::StringPiece& trimmed) {
35 // |trimmed| contains at least one non-dot. The maximum number of dots we want
36 // to look for is kMaxInterestingDots; any dots before that will not affect
37 // the outcome of the match.
38 const int kMaxInterestingDots = kMaxDots + 1;
39
40 // Scan |trimmed| from the right for up to kMaxInterestingDots dots, checking
41 // for a domain match at each position.
42 int found_dots = 0;
43 size_t pos = npos;
44 while (found_dots < kMaxInterestingDots) {
45 pos = trimmed.find_last_of('.', pos);
46 if (pos == npos)
47 break;
48 ++found_dots;
49 if (found_dots > kMinDots && LookupTail(trimmed, pos + 1))
50 return true;
51 if (pos == 0)
52 return false;
53 --pos;
54 }
55
56 if (found_dots >= kMinDots && found_dots <= kMaxDots) {
57 // We might have an exact match.
58 return LookupTail(trimmed, 0);
59 }
60
61 return false;
62 }
63
64 } // namespace
65
66 bool IsHostInStaleWhileRevalidateExperimentDomain(
67 const base::StringPiece& host) {
68 // Ignore trailing dots.
69 size_t last_interesting_pos = host.find_last_not_of('.');
70 if (last_interesting_pos == npos)
71 return false;
72
73 return LookupTrimmedHost(host.substr(0, last_interesting_pos + 1));
74 }
75
76 } // namespace net
OLDNEW
« 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