• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Chromium Authors
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/http/webfonts_histogram.h"
6 
7 #include "base/strings/string_piece.h"
8 #include "net/disk_cache/blockfile/histogram_macros.h"
9 
10 namespace {
11 
12 // Tests if the substring of str that begins at pos starts with substr. If so,
13 // returns true and advances pos by the length of substr.
Consume(const std::string & str,base::StringPiece substr,std::string::size_type * pos)14 bool Consume(const std::string& str,
15              base::StringPiece substr,
16              std::string::size_type* pos) {
17   if (!str.compare(*pos, substr.length(), substr.data())) {
18     *pos += substr.length();
19     return true;
20   }
21   return false;
22 }
23 
24 const char kRoboto[] = "roboto";
25 const char kOpenSans[] = "opensans";
26 
27 const char kRobotoHistogramName[] = "WebFont.HttpCacheStatus_roboto";
28 const char kOpenSansHistogramName[] = "WebFont.HttpCacheStatus_opensans";
29 const char kOthersHistogramName[] = "WebFont.HttpCacheStatus_others";
30 
RecordCacheEvent(net::HttpResponseInfo::CacheEntryStatus cache_status,const std::string & histogram_name)31 void RecordCacheEvent(net::HttpResponseInfo::CacheEntryStatus cache_status,
32                       const std::string& histogram_name) {
33   CACHE_HISTOGRAM_ENUMERATION(
34       histogram_name, cache_status,
35       net::HttpResponseInfo::CacheEntryStatus::ENTRY_MAX);
36 }
37 
38 }  // namespace
39 
40 namespace net::web_fonts_histogram {
41 
42 // Check if |key| is a URL for a font resource of Google Fonts.
43 // If so, record the WebFont.HttpCacheStatus histogram suffixed by "roboto",
44 // "opensans" or "others".
MaybeRecordCacheStatus(HttpResponseInfo::CacheEntryStatus cache_status,const std::string & key)45 void MaybeRecordCacheStatus(HttpResponseInfo::CacheEntryStatus cache_status,
46                             const std::string& key) {
47   std::string::size_type pos = 0;
48   if (Consume(key, "https://", &pos) || Consume(key, "http://", &pos)) {
49     if (Consume(key, "themes.googleusercontent.com/static/fonts/", &pos) ||
50         Consume(key, "ssl.gstatic.com/fonts/", &pos) ||
51         Consume(key, "fonts.gstatic.com/s/", &pos)) {
52       if (Consume(key, kRoboto, &pos)) {
53         RecordCacheEvent(cache_status, kRobotoHistogramName);
54       } else if (Consume(key, kOpenSans, &pos)) {
55         RecordCacheEvent(cache_status, kOpenSansHistogramName);
56       } else {
57         RecordCacheEvent(cache_status, kOthersHistogramName);
58       }
59     }
60   }
61 }
62 
63 }  // namespace net::web_fonts_histogram
64