Skip to content

Commit

Permalink
Add a histogram to roughly categorize MIME types for link rel=prefetch.
Browse files Browse the repository at this point in the history
It isn't obvious what sort of resource this is typically used to
prefetch, and getting a rough estimate of this data from the field would
be helpful in understanding how this feature is used and how it can be
specified and evolved.

Some uncertainty arose in
w3c/resource-hints#86

Change-Id: I62d7e37c60aef7b5072d440a697642bfbe3816fd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3546102
Reviewed-by: Ian Clelland <iclelland@chromium.org>
Commit-Queue: Jeremy Roman <jbroman@chromium.org>
Cr-Commit-Position: refs/heads/main@{#984502}
NOKEYCHECK=True
GitOrigin-RevId: 5d98fbfd97bb9d589364a22ce29b41f714bbc492
  • Loading branch information
jeremyroman authored and copybara-github committed Mar 23, 2022
1 parent 76628d9 commit c90e836
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions blink/renderer/core/loader/resource_load_observer_for_frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "third_party/blink/renderer/core/loader/resource_load_observer_for_frame.h"

#include "base/metrics/histogram_macros.h"
#include "base/stl_util.h"
#include "components/power_scheduler/power_mode_arbiter.h"
#include "services/network/public/cpp/cors/cors_error_status.h"
Expand All @@ -12,6 +13,7 @@
#include "third_party/blink/public/common/security/address_space_feature.h"
#include "third_party/blink/public/mojom/frame/frame.mojom-blink.h"
#include "third_party/blink/renderer/core/core_probes_inl.h"
#include "third_party/blink/renderer/core/dom/events/event_target.h"
#include "third_party/blink/renderer/core/frame/attribution_src_loader.h"
#include "third_party/blink/renderer/core/frame/deprecation/deprecation.h"
#include "third_party/blink/renderer/core/frame/frame_console.h"
Expand Down Expand Up @@ -173,6 +175,50 @@ void ResourceLoadObserverForFrame::DidChangePriority(
identifier, priority);
}

namespace {

// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
//
// Must remain in sync with LinkPrefetchMimeType in
// tools/metrics/histograms/enums.xml.
enum class LinkPrefetchMimeType {
kUnknown = 0,
kHtml = 1,
kScript = 2,
kStyle = 3,
kFont = 4,
kImage = 5,
kMedia = 6,
kMaxValue = kMedia,
};

void LogLinkPrefetchMimeTypeHistogram(const AtomicString& mime) {
// Loosely based on https://mimesniff.spec.whatwg.org/#mime-type-groups.
// This could be done properly if needed, but this is just to gather
// approximate data.
LinkPrefetchMimeType type = LinkPrefetchMimeType::kUnknown;
if (mime == "text/html" || mime == "application/xhtml+xml") {
type = LinkPrefetchMimeType::kHtml;
} else if (mime == "application/javascript" || mime == "text/javascript") {
type = LinkPrefetchMimeType::kScript;
} else if (mime == "text/css") {
type = LinkPrefetchMimeType::kStyle;
} else if (mime.StartsWith("font/") || mime.StartsWith("application/font-") ||
mime == "application/vnd.ms-fontobject" ||
mime == "application/vnd.ms-opentype") {
type = LinkPrefetchMimeType::kFont;
} else if (mime.StartsWith("image/")) {
type = LinkPrefetchMimeType::kImage;
} else if (mime.StartsWith("audio/") || mime.StartsWith("video/") ||
mime == "application/ogg") {
type = LinkPrefetchMimeType::kMedia;
}
UMA_HISTOGRAM_ENUMERATION("Blink.Prefetch.LinkPrefetchMimeType", type);
}

} // namespace

void ResourceLoadObserverForFrame::DidReceiveResponse(
uint64_t identifier,
const ResourceRequest& request,
Expand Down Expand Up @@ -230,6 +276,9 @@ void ResourceLoadObserverForFrame::DidReceiveResponse(
}
}

if (resource->GetType() == ResourceType::kLinkPrefetch)
LogLinkPrefetchMimeTypeHistogram(response.MimeType());

PreloadHelper::CanLoadResources resource_loading_policy =
response_source == ResponseSource::kFromMemoryCache
? PreloadHelper::kDoNotLoadResources
Expand Down

0 comments on commit c90e836

Please sign in to comment.