webfonts_histogram.cc 2.2 KB

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