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"
14 enum WebFontDiskCacheEventType
{
17 CACHE_EVENT_EVICTED_ENTRY
,
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();
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
))
46 if (Consume(str
, kOpenSans
, &pos
))
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
);
65 namespace disk_cache
{
66 namespace web_fonts_histogram
{
68 void RecordCacheMiss(const std::string
& key
) {
69 const char* label
= HistogramLabel(key
);
71 RecordCacheEvent(CACHE_EVENT_MISS
, label
);
74 void RecordEvictedEntry(const std::string
& key
) {
75 const char* label
= HistogramLabel(key
);
77 RecordCacheEvent(CACHE_EVENT_EVICTED_ENTRY
, label
);
80 void RecordCacheHit(EntryImpl
* entry
) {
81 const char* label
= HistogramLabel(entry
->GetKey());
84 EntryStore
* info
= entry
->entry()->Data();
85 CACHE_HISTOGRAM_COUNTS_10000(HistogramName("DiskCache.ReuseCount.Hit", label
),
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());
96 EntryStore
* info
= entry
->entry()->Data();
97 CACHE_HISTOGRAM_COUNTS_10000(
98 HistogramName("DiskCache.ReuseCount.Evict", label
),
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