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 index used to determine the group id attributed to the user |
| 23 // in order to report the behaviour of the save password prompt. When |
| 24 // |g_random_group_index| is equal to |kGroupsPerDomain| the index is |
| 25 // generated randomly in range [0, |kGroupsPerDomain|). The user keeps his |
| 26 // group index till the end of each session. |
| 27 size_t g_random_group_index = kGroupsPerDomain; |
| 28 |
| 29 // Contains a monitored domain name and all the group IDs which contain |
| 30 // domain name. |
| 31 struct DomainGroupsPair { |
| 32 const char* const domain_name; |
| 33 const size_t group_ids[kGroupsPerDomain]; |
| 34 }; |
| 35 |
| 36 // Generate a group index in range [0, |kGroupsPerDomain|). |
| 37 // If the |g_random_group_index| is not equal to its initial value |
| 38 // (|kGroupsPerDomain|), the value of |g_random_group_index| is not changed. |
| 39 void GenerateRandomIndex() { |
| 40 // For each session, the user uses only one group for each website. |
| 41 if (g_random_group_index == kGroupsPerDomain) |
| 42 g_random_group_index = base::RandGenerator(kGroupsPerDomain); |
| 43 } |
| 44 |
| 45 } // 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 GenerateRandomIndex(); |
| 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_index]; |
| 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 SetRandomIndexForTesting(size_t value) { |
| 104 g_random_group_index = 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 |