Home | History | Annotate | Download | only in blockfile
      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 
     12 namespace {
     13 
     14 enum 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.
     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;
     28   }
     29   return false;
     30 }
     31 
     32 const char kRoboto[] = "roboto";
     33 const char kOpenSans[] = "opensans";
     34 const 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").
     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;
     49     }
     50   }
     51   return NULL;
     52 }
     53 
     54 std::string HistogramName(const char* prefix, const char* label) {
     55   return base::StringPrintf("WebFont.%s_%s", prefix, label);
     56 }
     57 
     58 void RecordCacheEvent(WebFontDiskCacheEventType type, const char* label) {
     59   CACHE_HISTOGRAM_ENUMERATION(HistogramName("DiskCacheHit", label),
     60                               type, CACHE_EVENT_MAX);
     61 }
     62 
     63 }  // namespace
     64 
     65 namespace disk_cache {
     66 namespace web_fonts_histogram {
     67 
     68 void RecordCacheMiss(const std::string& key) {
     69   const char* label = HistogramLabel(key);
     70   if (label)
     71     RecordCacheEvent(CACHE_EVENT_MISS, label);
     72 }
     73 
     74 void RecordEvictedEntry(const std::string& key) {
     75   const char* label = HistogramLabel(key);
     76   if (label)
     77     RecordCacheEvent(CACHE_EVENT_EVICTED_ENTRY, label);
     78 }
     79 
     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);
     90 }
     91 
     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));
    102 }
    103 
    104 }  // namespace web_fonts_histogram
    105 }  // namespace disk_cache
    106