OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 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 "chrome/browser/password_manager/password_manager_metrics_util.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/metrics/histogram.h" | |
9 #include "base/rand_util.h" | |
10 #include "base/strings/string_number_conversions.h" | |
11 #include "base/strings/string_util.h" | |
12 #include "base/time/time.h" | |
13 #include "url/gurl.h" | |
14 | |
15 namespace password_manager_metrics_util { | |
16 | |
17 namespace { | |
18 | |
19 // The number of domain groups. | |
20 const size_t kNumGroups = 2 * kGroupsPerDomain; | |
21 | |
22 // The group id attributed to the user in order to report the behaviour of | |
23 // the save password prompt. When |g_random_group_id| is equal to | |
24 // |kGroupsPerDomain| the id is generated randomly in range | |
25 // [0, |kGroupsPerDomain|). The user keeps his group id till the end of | |
26 // each session. | |
Ilya Sherman
2013/09/14 03:33:25
This isn't really a group id, but rather an index
| |
27 size_t g_random_group_id = kGroupsPerDomain; | |
28 | |
29 // Generate an id in range [0, |kGroupsPerDomain|). | |
30 // If the |random_group_id| is equal to its initial value | |
Ilya Sherman
2013/09/14 03:33:25
random_group_id -> g_random_group_id
| |
31 // (|kGroupsPerDomain|), the value of |random_id| is not changed. | |
32 void GenerateRandomId() { | |
33 // For each session, the user uses only one group for each website. | |
34 if (g_random_group_id == kGroupsPerDomain) | |
35 g_random_group_id = base::RandGenerator(kGroupsPerDomain); | |
36 } | |
37 | |
38 } // namespace | |
39 | |
40 // Contains a monitored domain name and all the group IDs which contain | |
41 // domain name. | |
42 struct DomainGroupsPair { | |
43 const char* const domain_name; | |
44 const size_t group_ids[kGroupsPerDomain]; | |
45 }; | |
Ilya Sherman
2013/09/14 03:33:25
nit: Please move this into the anonymous namespace
| |
46 | |
47 size_t MonitoredDomainGroupId(const std::string& url_host) { | |
48 // This array contains each monitored website together with all ids of | |
49 // groups which contain the website. Each website appears in | |
50 // |kGroupsPerDomain| groups, and each group includes an equal number of | |
51 // websites, so that no two websites have the same set of groups that they | |
52 // belong to. All ids are in the range [1, |kNumGroups|]. | |
53 // For more information about the algorithm used see http://goo.gl/vUuFd5. | |
54 static const DomainGroupsPair kDomainMapping[] = { | |
55 {"google.com", {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}, | |
56 {"yahoo.com", {1, 2, 3, 4, 5, 11, 12, 13, 14, 15}}, | |
57 {"baidu.com", {1, 2, 3, 4, 6, 7, 11, 12, 16, 17}}, | |
58 {"wikipedia.org", {1, 2, 3, 4, 5, 6, 11, 12, 16, 18}}, | |
59 {"linkedin.com", {1, 6, 8, 11, 13, 14, 15, 16, 17, 19}}, | |
60 {"twitter.com", {5, 6, 7, 8, 9, 11, 13, 17, 19, 20}}, | |
61 {"live.com", {7, 8, 9, 10, 13, 14, 16, 17, 18, 20}}, | |
62 {"amazon.com", {2, 5, 9, 10, 12, 14, 15, 18, 19, 20}}, | |
63 {"ebay.com", {3, 7, 9, 10, 14, 15, 17, 18, 19, 20}}, | |
64 {"tumblr.com", {4, 8, 10, 12, 13, 15, 16, 18, 19, 20}}, | |
65 }; | |
66 const size_t kDomainMappingLength = arraysize(kDomainMapping); | |
67 | |
68 GenerateRandomId(); | |
69 | |
70 GURL url(url_host); | |
71 for (size_t i = 0; i < kDomainMappingLength; ++i) { | |
72 if (url.DomainIs(kDomainMapping[i].domain_name)) | |
73 return kDomainMapping[i].group_ids[g_random_group_id]; | |
74 } | |
75 return 0; | |
76 } | |
77 | |
78 void LogUMAHistogramEnumeration(const std::string& name, | |
79 int sample, | |
80 int boundary_value) { | |
81 DCHECK_LT(sample, boundary_value); | |
82 | |
83 // Note: This leaks memory, which is expected behavior. | |
84 base::HistogramBase* histogram = | |
85 base::LinearHistogram::FactoryGet( | |
86 name, | |
87 1, | |
88 boundary_value, | |
89 boundary_value + 1, | |
90 base::HistogramBase::kUmaTargetedHistogramFlag); | |
91 histogram->Add(sample); | |
92 } | |
93 | |
94 void LogUMAHistogramBoolean(const std::string& name, bool sample) { | |
95 // Note: This leaks memory, which is expected behavior. | |
96 base::HistogramBase* histogram = | |
97 base::BooleanHistogram::FactoryGet( | |
98 name, | |
99 base::Histogram::kNoFlags); | |
100 histogram->AddBoolean(sample); | |
101 } | |
102 | |
103 void SetRandomIdForTesting(size_t value) { | |
104 g_random_group_id = value; | |
105 } | |
106 | |
107 std::string GroupIdToString(size_t group_id) { | |
108 DCHECK_LE(group_id, kNumGroups); | |
109 if (group_id > 0) | |
110 return "group_" + base::IntToString(group_id); | |
111 return std::string(); | |
112 } | |
113 | |
114 } // namespace password_manager_metrics_util | |
OLD | NEW |