Bug 1899734 - Part 5: Make SharedSubResourceCache capable of handling CacheablePerformanceTimingData. r=emilio

Differential Revision: https://phabricator.services.mozilla.com/D223351
This commit is contained in:
Tooru Fujisawa 2024-10-01 12:17:22 +00:00
parent a54fd92819
commit a8b0cd5ccc
6 changed files with 90 additions and 8 deletions

View File

@ -7,14 +7,14 @@
#ifndef mozilla_dom_SharedScriptCache_h
#define mozilla_dom_SharedScriptCache_h
#include "PLDHashTable.h" // PLDHashEntryHdr
#include "js/loader/LoadedScript.h" // JS::loader::LoadedScript
#include "js/loader/ScriptKind.h" // JS::loader::ScriptKind
#include "js/loader/ScriptLoadRequest.h" // JS::loader::ScriptLoadRequest
#include "mozilla/WeakPtr.h" // SupportsWeakPtr
#include "mozilla/CORSMode.h" // mozilla::CORSMode
#include "mozilla/MemoryReporting.h" // MallocSizeOf
#include "mozilla/SharedSubResourceCache.h" // SharedSubResourceCache, SharedSubResourceCacheLoadingValueBase
#include "PLDHashTable.h" // PLDHashEntryHdr
#include "js/loader/LoadedScript.h" // JS::loader::LoadedScript
#include "js/loader/ScriptKind.h" // JS::loader::ScriptKind
#include "js/loader/ScriptLoadRequest.h" // JS::loader::ScriptLoadRequest
#include "mozilla/WeakPtr.h" // SupportsWeakPtr
#include "mozilla/CORSMode.h" // mozilla::CORSMode
#include "mozilla/MemoryReporting.h" // MallocSizeOf
#include "mozilla/SharedSubResourceCache.h" // SharedSubResourceCache, SharedSubResourceCacheLoadingValueBase, SubResourceNetworkMetadataHolder
#include "mozilla/dom/CacheExpirationTime.h" // CacheExpirationTime
#include "nsIMemoryReporter.h" // nsIMemoryReporter, NS_DECL_NSIMEMORYREPORTER
#include "nsIObserver.h" // nsIObserver, NS_DECL_NSIOBSERVER
@ -134,6 +134,11 @@ class ScriptLoadData final
bool IsCancelled() const override { return false; }
bool IsSyncLoad() const override { return true; }
SubResourceNetworkMetadataHolder* GetNetworkMetadata() const override {
// TODO: Bug 1916635.
return nullptr;
}
void StartLoading() override {}
void SetLoadCompleted() override {}
void OnCoalescedTo(const ScriptLoadData& aExistingLoad) override {}

View File

@ -0,0 +1,26 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "SharedSubResourceCache.h"
#include "mozilla/dom/CacheablePerformanceTimingData.h"
#include "nsCOMPtr.h"
#include "nsIHttpChannel.h"
#include "nsIRequest.h"
#include "nsITimedChannel.h"
namespace mozilla {
SubResourceNetworkMetadataHolder::SubResourceNetworkMetadataHolder(
nsIRequest* aRequest) {
nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(aRequest);
if (timedChannel) {
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aRequest);
mPerfData.emplace(timedChannel, httpChannel);
}
}
} // namespace mozilla

View File

@ -27,6 +27,7 @@
// SheetLoadData.
#include "mozilla/PrincipalHashKey.h"
#include "mozilla/RefPtr.h"
#include "mozilla/WeakPtr.h"
#include "nsTHashMap.h"
#include "nsIMemoryReporter.h"
@ -36,10 +37,42 @@
#include "mozilla/dom/CacheExpirationTime.h"
#include "mozilla/dom/Document.h"
#include "nsContentUtils.h"
#include "nsISupportsImpl.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/dom/CacheablePerformanceTimingData.h"
namespace mozilla {
// A struct to hold the network-related metadata associated with the cache.
//
// When inserting a cache, the consumer should create this from the request and
// make it available via
// SharedSubResourceCacheLoadingValueBase::GetNetworkMetadata.
//
// When using a cache, the consumer can retrieve this from
// SharedSubResourceCache::Result::mNetworkMetadata and use it for notifying
// the observers once the necessary data becomes ready.
// This struct is ref-counted in order to allow this usage.
class SubResourceNetworkMetadataHolder {
public:
SubResourceNetworkMetadataHolder() = delete;
explicit SubResourceNetworkMetadataHolder(nsIRequest* aRequest);
const dom::CacheablePerformanceTimingData* GetPerfData() const {
return mPerfData.ptrOr(nullptr);
}
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SubResourceNetworkMetadataHolder)
private:
~SubResourceNetworkMetadataHolder() = default;
mozilla::Maybe<dom::CacheablePerformanceTimingData> mPerfData;
// TODO: Add HTTP headers for DevTools/WebDriver notifications (bug 1915626).
};
enum class CachedSubResourceState {
Miss,
Loading,
@ -56,6 +89,8 @@ struct SharedSubResourceCacheLoadingValueBase {
virtual bool IsCancelled() const = 0;
virtual bool IsSyncLoad() const = 0;
virtual SubResourceNetworkMetadataHolder* GetNetworkMetadata() const = 0;
virtual void StartLoading() = 0;
virtual void SetLoadCompleted() = 0;
virtual void OnCoalescedTo(const Derived& aExistingLoad) = 0;
@ -113,11 +148,13 @@ class SharedSubResourceCache {
protected:
struct CompleteSubResource {
RefPtr<Value> mResource;
RefPtr<SubResourceNetworkMetadataHolder> mNetworkMetadata;
CacheExpirationTime mExpirationTime = CacheExpirationTime::Never();
bool mWasSyncLoad = false;
explicit CompleteSubResource(LoadingValue& aValue)
: mResource(aValue.ValueForCache()),
mNetworkMetadata(aValue.GetNetworkMetadata()),
mExpirationTime(aValue.ExpirationTime()),
mWasSyncLoad(aValue.IsSyncLoad()) {}
@ -127,6 +164,8 @@ class SharedSubResourceCache {
public:
struct Result {
Value* mCompleteValue = nullptr;
RefPtr<SubResourceNetworkMetadataHolder> mNetworkMetadata;
LoadingValue* mLoadingOrPendingValue = nullptr;
CachedSubResourceState mState = CachedSubResourceState::Miss;
@ -134,6 +173,7 @@ class SharedSubResourceCache {
explicit constexpr Result(const CompleteSubResource& aCompleteSubResource)
: mCompleteValue(aCompleteSubResource.mResource.get()),
mNetworkMetadata(aCompleteSubResource.mNetworkMetadata),
mLoadingOrPendingValue(nullptr),
mState(CachedSubResourceState::Complete) {}

View File

@ -7,6 +7,7 @@
#ifndef mozilla_css_SheetLoadData_h
#define mozilla_css_SheetLoadData_h
#include "mozilla/RefPtr.h"
#include "mozilla/css/Loader.h"
#include "mozilla/css/SheetParsingMode.h"
#include "mozilla/Encoding.h"
@ -257,6 +258,8 @@ class SheetLoadData final
const bool mRecordErrors;
RefPtr<SubResourceNetworkMetadataHolder> mNetworkMetadata;
bool ShouldDefer() const { return mWasAlternate || !mMediaMatched; }
RefPtr<StyleSheet> ValueForCache() const;
@ -287,6 +290,10 @@ class SheetLoadData final
bool IsLoading() const override { return mIsLoading; }
bool IsCancelled() const override { return mIsCancelled; }
SubResourceNetworkMetadataHolder* GetNetworkMetadata() const override {
return mNetworkMetadata.get();
}
void StartLoading() override;
void SetLoadCompleted() override;
void OnCoalescedTo(const SheetLoadData& aExistingLoad) override {

View File

@ -113,6 +113,9 @@ StreamLoader::OnStopRequest(nsIRequest* aRequest, nsresult aStatus) {
// Resolution of parse promise fires onLoadEvent and this should not happen
// before main thread OnStopRequest is dispatched.
if (NS_IsMainThread()) {
mSheetLoadData->mNetworkMetadata =
new SubResourceNetworkMetadataHolder(aRequest);
if (mOnDataFinishedTime) {
// collect telemetry for the delta between OnDataFinished and
// OnStopRequest

View File

@ -260,6 +260,7 @@ UNIFIED_SOURCES += [
"ServoStyleSet.cpp",
"ShadowParts.cpp",
"SharedStyleSheetCache.cpp",
"SharedSubResourceCache.cpp",
"StreamLoader.cpp",
"StyleAnimationValue.cpp",
"StyleColor.cpp",