webfonts_histogram.cc revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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.
4
5#include "net/disk_cache/blockfile/webfonts_histogram.h"
6
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"
11
12namespace {
13
14enum WebFontDiskCacheEventType {
15  CACHE_EVENT_MISS,
16  CACHE_EVENT_HIT,
17  CACHE_EVENT_EVICTED_ENTRY,
18  CACHE_EVENT_MAX
19};
20
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.
23bool 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;
28  }
29  return false;
30}
31
32const char kRoboto[] = "roboto";
33const char kOpenSans[] = "opensans";
34const char kOthers[] = "others";
35
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").
38const 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;
49    }
50  }
51  return NULL;
52}
53
54std::string HistogramName(const char* prefix, const char* label) {
55  return base::StringPrintf("WebFont.%s_%s", prefix, label);
56}
57
58void RecordCacheEvent(WebFontDiskCacheEventType type, const char* label) {
59  CACHE_HISTOGRAM_ENUMERATION(HistogramName("DiskCacheHit", label),
60                              type, CACHE_EVENT_MAX);
61}
62
63}  // namespace
64
65namespace disk_cache {
66namespace web_fonts_histogram {
67
68void RecordCacheMiss(const std::string& key) {
69  const char* label = HistogramLabel(key);
70  if (label)
71    RecordCacheEvent(CACHE_EVENT_MISS, label);
72}
73
74void RecordEvictedEntry(const std::string& key) {
75  const char* label = HistogramLabel(key);
76  if (label)
77    RecordCacheEvent(CACHE_EVENT_EVICTED_ENTRY, label);
78}
79
80void RecordCacheHit(EntryImpl* entry) {
81  const char* label = HistogramLabel(entry->entry()->Data()->key);
82  if (!label)
83    return;
84  CACHE_HISTOGRAM_COUNTS_10000(HistogramName("DiskCache.ReuseCount.Hit", label),
85                               entry->entry()->Data()->reuse_count);
86  RecordCacheEvent(CACHE_EVENT_HIT, label);
87}
88
89void RecordEviction(EntryImpl* entry) {
90  const char* label = HistogramLabel(entry->entry()->Data()->key);
91  if (label)
92    return;
93  CACHE_HISTOGRAM_COUNTS_10000(
94      HistogramName("DiskCache.ReuseCount.Evict", label),
95      entry->entry()->Data()->reuse_count);
96}
97
98}  // namespace web_fonts_histogram
99}  // namespace disk_cache
100