Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / disk_cache / blockfile / webfonts_histogram.cc
bloba92d94224ec11b8a479f5f0b5699e8dd7db74ea7
1 // Copyright 2014 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.
5 #include "net/disk_cache/blockfile/webfonts_histogram.h"
7 #include "base/strings/string_piece.h"
8 #include "base/strings/stringprintf.h"
9 #include "net/disk_cache/blockfile/entry_impl.h"
10 #include "net/disk_cache/blockfile/histogram_macros.h"
12 namespace {
14 enum WebFontDiskCacheEventType {
15 CACHE_EVENT_MISS,
16 CACHE_EVENT_HIT,
17 CACHE_EVENT_EVICTED_ENTRY,
18 CACHE_EVENT_MAX
21 // Tests if the substring of str that begins at pos starts with substr. If so,
22 // returns true and advances pos by the length of substr.
23 bool Consume(const std::string& str, const base::StringPiece& substr,
24 std::string::size_type* pos) {
25 if (!str.compare(*pos, substr.length(), substr.data())) {
26 *pos += substr.length();
27 return true;
29 return false;
32 const char kRoboto[] = "roboto";
33 const char kOpenSans[] = "opensans";
34 const char kOthers[] = "others";
36 // Check if the given string is a URL for a font resource of Google Fonts.
37 // If so, returns a label for UMA histogram ("roboto", "opensans" or "others").
38 const char* HistogramLabel(const std::string& str) {
39 std::string::size_type pos = 0;
40 if (Consume(str, "http://", &pos) || Consume(str, "https://", &pos)) {
41 if (Consume(str, "themes.googleusercontent.com/static/fonts/", &pos) ||
42 Consume(str, "ssl.gstatic.com/fonts/", &pos) ||
43 Consume(str, "fonts.gstatic.com/s/", &pos)) {
44 if (Consume(str, kRoboto, &pos))
45 return kRoboto;
46 if (Consume(str, kOpenSans, &pos))
47 return kOpenSans;
48 return kOthers;
51 return NULL;
54 std::string HistogramName(const char* prefix, const char* label) {
55 return base::StringPrintf("WebFont.%s_%s", prefix, label);
58 void RecordCacheEvent(WebFontDiskCacheEventType type, const char* label) {
59 CACHE_HISTOGRAM_ENUMERATION(HistogramName("DiskCacheHit", label),
60 type, CACHE_EVENT_MAX);
63 } // namespace
65 namespace disk_cache {
66 namespace web_fonts_histogram {
68 void RecordCacheMiss(const std::string& key) {
69 const char* label = HistogramLabel(key);
70 if (label)
71 RecordCacheEvent(CACHE_EVENT_MISS, label);
74 void RecordEvictedEntry(const std::string& key) {
75 const char* label = HistogramLabel(key);
76 if (label)
77 RecordCacheEvent(CACHE_EVENT_EVICTED_ENTRY, label);
80 void RecordCacheHit(EntryImpl* entry) {
81 const char* label = HistogramLabel(entry->GetKey());
82 if (!label)
83 return;
84 EntryStore* info = entry->entry()->Data();
85 CACHE_HISTOGRAM_COUNTS_10000(HistogramName("DiskCache.ReuseCount.Hit", label),
86 info->reuse_count);
87 CACHE_HISTOGRAM_AGE(HistogramName("DiskCache.EntryAge.Hit", label),
88 base::Time::FromInternalValue(info->creation_time));
89 RecordCacheEvent(CACHE_EVENT_HIT, label);
92 void RecordEviction(EntryImpl* entry) {
93 const char* label = HistogramLabel(entry->GetKey());
94 if (!label)
95 return;
96 EntryStore* info = entry->entry()->Data();
97 CACHE_HISTOGRAM_COUNTS_10000(
98 HistogramName("DiskCache.ReuseCount.Evict", label),
99 info->reuse_count);
100 CACHE_HISTOGRAM_AGE(HistogramName("DiskCache.EntryAge.Evict", label),
101 base::Time::FromInternalValue(info->creation_time));
104 } // namespace web_fonts_histogram
105 } // namespace disk_cache