Commit cc081ca9 authored by Bill Budge's avatar Bill Budge Committed by Commit Bot

[blink] Add a ScriptCachedMetadataHandler class.

- Moves ScriptResource::SingleCachedMetadataHandlerImpl out of
  ScriptResource and into the blink namespace so it can be used by other
  script-like resources.
- Creates this CacheHandler type for RawResource type kRaw, which will
  be needed for WASM modules.

Bug: chromium:719172
Change-Id: I34a2740310d2d3f4ccb0cea1bb664db0b9c53f5e
Reviewed-on: https://chromium-review.googlesource.com/1174903
Commit-Queue: Bill Budge <bbudge@chromium.org>
Reviewed-by: default avatarYutaka Hirano <yhirano@chromium.org>
Reviewed-by: default avatarHiroshige Hayashizaki <hiroshige@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584159}
parent c485a2ac
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include "third_party/blink/renderer/platform/loader/fetch/integrity_metadata.h" #include "third_party/blink/renderer/platform/loader/fetch/integrity_metadata.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_client_walker.h" #include "third_party/blink/renderer/platform/loader/fetch/resource_client_walker.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_fetcher.h" #include "third_party/blink/renderer/platform/loader/fetch/resource_fetcher.h"
#include "third_party/blink/renderer/platform/loader/fetch/script_cached_metadata_handler.h"
#include "third_party/blink/renderer/platform/loader/fetch/text_resource_decoder_options.h" #include "third_party/blink/renderer/platform/loader/fetch/text_resource_decoder_options.h"
#include "third_party/blink/renderer/platform/loader/subresource_integrity.h" #include "third_party/blink/renderer/platform/loader/subresource_integrity.h"
#include "third_party/blink/renderer/platform/network/mime/mime_type_registry.h" #include "third_party/blink/renderer/platform/network/mime/mime_type_registry.h"
...@@ -67,109 +68,6 @@ bool IsRequestContextSupported(WebURLRequest::RequestContext request_context) { ...@@ -67,109 +68,6 @@ bool IsRequestContextSupported(WebURLRequest::RequestContext request_context) {
} // namespace } // namespace
// SingleCachedMetadataHandlerImpl should be created when a response is
// received, and can be used independently from Resource. - It doesn't have any
// references to Resource. Necessary data are captured
// from Resource when the handler is created.
// - It is not affected by Resource's revalidation on MemoryCache.
// The validity of the handler is solely checked by |response_url_| and
// |response_time_| (not by Resource) by the browser process, and the cached
// metadata written to the handler is rejected if e.g. the disk cache entry
// has been updated and the handler refers to an older response.
class ScriptResource::SingleCachedMetadataHandlerImpl final
: public SingleCachedMetadataHandler {
public:
SingleCachedMetadataHandlerImpl(const WTF::TextEncoding&,
std::unique_ptr<CachedMetadataSender>);
~SingleCachedMetadataHandlerImpl() override = default;
void Trace(blink::Visitor*) override;
void SetCachedMetadata(uint32_t, const char*, size_t, CacheType) override;
void ClearCachedMetadata(CacheType) override;
scoped_refptr<CachedMetadata> GetCachedMetadata(uint32_t) const override;
// This returns the encoding at the time of ResponseReceived().
// Therefore this does NOT reflect encoding detection from body contents,
// but the final encoding after the encoding detection can be determined
// uniquely from Encoding(), provided the body content is the same,
// as we can assume the encoding detection will results in the same final
// encoding.
// TODO(hiroshige): Make this semantics cleaner.
String Encoding() const override { return String(encoding_.GetName()); }
bool IsServedFromCacheStorage() const override {
return sender_->IsServedFromCacheStorage();
}
// Sets the serialized metadata retrieved from the platform's cache.
void SetSerializedCachedMetadata(const char*, size_t);
private:
void SendToPlatform();
scoped_refptr<CachedMetadata> cached_metadata_;
std::unique_ptr<CachedMetadataSender> sender_;
const WTF::TextEncoding encoding_;
};
ScriptResource::SingleCachedMetadataHandlerImpl::
SingleCachedMetadataHandlerImpl(
const WTF::TextEncoding& encoding,
std::unique_ptr<CachedMetadataSender> sender)
: sender_(std::move(sender)), encoding_(encoding) {}
void ScriptResource::SingleCachedMetadataHandlerImpl::Trace(
blink::Visitor* visitor) {
CachedMetadataHandler::Trace(visitor);
}
void ScriptResource::SingleCachedMetadataHandlerImpl::SetCachedMetadata(
uint32_t data_type_id,
const char* data,
size_t size,
CachedMetadataHandler::CacheType cache_type) {
// Currently, only one type of cached metadata per resource is supported. If
// the need arises for multiple types of metadata per resource this could be
// enhanced to store types of metadata in a map.
DCHECK(!cached_metadata_);
cached_metadata_ = CachedMetadata::Create(data_type_id, data, size);
if (cache_type == CachedMetadataHandler::kSendToPlatform)
SendToPlatform();
}
void ScriptResource::SingleCachedMetadataHandlerImpl::ClearCachedMetadata(
CachedMetadataHandler::CacheType cache_type) {
cached_metadata_ = nullptr;
if (cache_type == CachedMetadataHandler::kSendToPlatform)
SendToPlatform();
}
scoped_refptr<CachedMetadata>
ScriptResource::SingleCachedMetadataHandlerImpl::GetCachedMetadata(
uint32_t data_type_id) const {
if (!cached_metadata_ || cached_metadata_->DataTypeID() != data_type_id)
return nullptr;
return cached_metadata_;
}
void ScriptResource::SingleCachedMetadataHandlerImpl::
SetSerializedCachedMetadata(const char* data, size_t size) {
// We only expect to receive cached metadata from the platform once. If this
// triggers, it indicates an efficiency problem which is most likely
// unexpected in code designed to improve performance.
DCHECK(!cached_metadata_);
cached_metadata_ = CachedMetadata::CreateFromSerializedData(data, size);
}
void ScriptResource::SingleCachedMetadataHandlerImpl::SendToPlatform() {
if (cached_metadata_) {
const Vector<char>& serialized_data = cached_metadata_->SerializedData();
sender_->Send(serialized_data.data(), serialized_data.size());
} else {
sender_->Send(nullptr, 0);
}
}
ScriptResource* ScriptResource::Fetch(FetchParameters& params, ScriptResource* ScriptResource::Fetch(FetchParameters& params,
ResourceFetcher* fetcher, ResourceFetcher* fetcher,
ResourceClient* client) { ResourceClient* client) {
...@@ -218,15 +116,14 @@ SingleCachedMetadataHandler* ScriptResource::CacheHandler() { ...@@ -218,15 +116,14 @@ SingleCachedMetadataHandler* ScriptResource::CacheHandler() {
CachedMetadataHandler* ScriptResource::CreateCachedMetadataHandler( CachedMetadataHandler* ScriptResource::CreateCachedMetadataHandler(
std::unique_ptr<CachedMetadataSender> send_callback) { std::unique_ptr<CachedMetadataSender> send_callback) {
return new SingleCachedMetadataHandlerImpl(Encoding(), return new ScriptCachedMetadataHandler(Encoding(), std::move(send_callback));
std::move(send_callback));
} }
void ScriptResource::SetSerializedCachedMetadata(const char* data, void ScriptResource::SetSerializedCachedMetadata(const char* data,
size_t size) { size_t size) {
Resource::SetSerializedCachedMetadata(data, size); Resource::SetSerializedCachedMetadata(data, size);
SingleCachedMetadataHandlerImpl* cache_handler = ScriptCachedMetadataHandler* cache_handler =
static_cast<SingleCachedMetadataHandlerImpl*>(Resource::CacheHandler()); static_cast<ScriptCachedMetadataHandler*>(Resource::CacheHandler());
if (cache_handler) { if (cache_handler) {
cache_handler->SetSerializedCachedMetadata(data, size); cache_handler->SetSerializedCachedMetadata(data, size);
} }
......
...@@ -40,7 +40,6 @@ namespace blink { ...@@ -40,7 +40,6 @@ namespace blink {
class FetchParameters; class FetchParameters;
class KURL; class KURL;
class ResourceFetcher; class ResourceFetcher;
class ScriptResource;
class CORE_EXPORT ScriptResource final : public TextResource { class CORE_EXPORT ScriptResource final : public TextResource {
public: public:
...@@ -80,8 +79,6 @@ class CORE_EXPORT ScriptResource final : public TextResource { ...@@ -80,8 +79,6 @@ class CORE_EXPORT ScriptResource final : public TextResource {
std::unique_ptr<CachedMetadataSender> send_callback) override; std::unique_ptr<CachedMetadataSender> send_callback) override;
private: private:
class SingleCachedMetadataHandlerImpl;
class ScriptResourceFactory : public ResourceFactory { class ScriptResourceFactory : public ResourceFactory {
public: public:
ScriptResourceFactory() ScriptResourceFactory()
......
...@@ -68,6 +68,8 @@ blink_platform_sources("loader") { ...@@ -68,6 +68,8 @@ blink_platform_sources("loader") {
"fetch/resource_status.h", "fetch/resource_status.h",
"fetch/resource_timing_info.cc", "fetch/resource_timing_info.cc",
"fetch/resource_timing_info.h", "fetch/resource_timing_info.h",
"fetch/script_cached_metadata_handler.cc",
"fetch/script_cached_metadata_handler.h",
"fetch/script_fetch_options.cc", "fetch/script_fetch_options.cc",
"fetch/script_fetch_options.h", "fetch/script_fetch_options.h",
"fetch/source_keyed_cached_metadata_handler.cc", "fetch/source_keyed_cached_metadata_handler.cc",
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include "third_party/blink/renderer/platform/loader/fetch/memory_cache.h" #include "third_party/blink/renderer/platform/loader/fetch/memory_cache.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_client_walker.h" #include "third_party/blink/renderer/platform/loader/fetch/resource_client_walker.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_fetcher.h" #include "third_party/blink/renderer/platform/loader/fetch/resource_fetcher.h"
#include "third_party/blink/renderer/platform/loader/fetch/script_cached_metadata_handler.h"
#include "third_party/blink/renderer/platform/loader/fetch/source_keyed_cached_metadata_handler.h" #include "third_party/blink/renderer/platform/loader/fetch/source_keyed_cached_metadata_handler.h"
#include "third_party/blink/renderer/platform/network/http_names.h" #include "third_party/blink/renderer/platform/network/http_names.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread_scheduler.h" #include "third_party/blink/renderer/platform/scheduler/public/thread_scheduler.h"
...@@ -200,6 +201,11 @@ SourceKeyedCachedMetadataHandler* RawResource::InlineScriptCacheHandler() { ...@@ -200,6 +201,11 @@ SourceKeyedCachedMetadataHandler* RawResource::InlineScriptCacheHandler() {
Resource::CacheHandler()); Resource::CacheHandler());
} }
SingleCachedMetadataHandler* RawResource::ScriptCacheHandler() {
DCHECK_EQ(kRaw, GetType());
return static_cast<SingleCachedMetadataHandler*>(Resource::CacheHandler());
}
void RawResource::ResponseReceived( void RawResource::ResponseReceived(
const ResourceResponse& response, const ResourceResponse& response,
std::unique_ptr<WebDataConsumerHandle> handle) { std::unique_ptr<WebDataConsumerHandle> handle) {
...@@ -228,11 +234,16 @@ void RawResource::ResponseReceived( ...@@ -228,11 +234,16 @@ void RawResource::ResponseReceived(
CachedMetadataHandler* RawResource::CreateCachedMetadataHandler( CachedMetadataHandler* RawResource::CreateCachedMetadataHandler(
std::unique_ptr<CachedMetadataSender> send_callback) { std::unique_ptr<CachedMetadataSender> send_callback) {
// If this is the document resource, create a cache handler that can handle
// multiple inline scripts.
if (GetType() == kMainResource) { if (GetType() == kMainResource) {
// This is a document resource; create a cache handler that can handle
// multiple inline scripts.
return new SourceKeyedCachedMetadataHandler(Encoding(), return new SourceKeyedCachedMetadataHandler(Encoding(),
std::move(send_callback)); std::move(send_callback));
} else if (GetType() == kRaw) {
// This is a resource of indeterminate type, e.g. a fetched WebAssembly
// module; create a cache handler that can store a single metadata entry.
return new ScriptCachedMetadataHandler(Encoding(),
std::move(send_callback));
} }
return Resource::CreateCachedMetadataHandler(std::move(send_callback)); return Resource::CreateCachedMetadataHandler(std::move(send_callback));
} }
......
...@@ -94,6 +94,11 @@ class PLATFORM_EXPORT RawResource final : public Resource { ...@@ -94,6 +94,11 @@ class PLATFORM_EXPORT RawResource final : public Resource {
// kMainResource. // kMainResource.
SourceKeyedCachedMetadataHandler* InlineScriptCacheHandler(); SourceKeyedCachedMetadataHandler* InlineScriptCacheHandler();
// Used for code caching of fetched code resources. Returns a cache handler
// which can only store a single cache metadata entry. This is valid only if
// type is kRaw.
SingleCachedMetadataHandler* ScriptCacheHandler();
scoped_refptr<BlobDataHandle> DownloadedBlob() const { scoped_refptr<BlobDataHandle> DownloadedBlob() const {
return downloaded_blob_; return downloaded_blob_;
} }
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/loader/fetch/script_cached_metadata_handler.h"
#include "third_party/blink/renderer/platform/loader/fetch/cached_metadata.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource.h"
namespace blink {
ScriptCachedMetadataHandler::ScriptCachedMetadataHandler(
const WTF::TextEncoding& encoding,
std::unique_ptr<CachedMetadataSender> sender)
: sender_(std::move(sender)), encoding_(encoding) {}
void ScriptCachedMetadataHandler::Trace(blink::Visitor* visitor) {
CachedMetadataHandler::Trace(visitor);
}
void ScriptCachedMetadataHandler::SetCachedMetadata(
uint32_t data_type_id,
const char* data,
size_t size,
CachedMetadataHandler::CacheType cache_type) {
// Currently, only one type of cached metadata per resource is supported. If
// the need arises for multiple types of metadata per resource this could be
// enhanced to store types of metadata in a map.
DCHECK(!cached_metadata_);
cached_metadata_ = CachedMetadata::Create(data_type_id, data, size);
if (cache_type == CachedMetadataHandler::kSendToPlatform)
SendToPlatform();
}
void ScriptCachedMetadataHandler::ClearCachedMetadata(
CachedMetadataHandler::CacheType cache_type) {
cached_metadata_ = nullptr;
if (cache_type == CachedMetadataHandler::kSendToPlatform)
SendToPlatform();
}
scoped_refptr<CachedMetadata> ScriptCachedMetadataHandler::GetCachedMetadata(
uint32_t data_type_id) const {
if (!cached_metadata_ || cached_metadata_->DataTypeID() != data_type_id)
return nullptr;
return cached_metadata_;
}
void ScriptCachedMetadataHandler::SetSerializedCachedMetadata(const char* data,
size_t size) {
// We only expect to receive cached metadata from the platform once. If this
// triggers, it indicates an efficiency problem which is most likely
// unexpected in code designed to improve performance.
DCHECK(!cached_metadata_);
cached_metadata_ = CachedMetadata::CreateFromSerializedData(data, size);
}
String ScriptCachedMetadataHandler::Encoding() const {
return String(encoding_.GetName());
}
bool ScriptCachedMetadataHandler::IsServedFromCacheStorage() const {
return sender_->IsServedFromCacheStorage();
}
void ScriptCachedMetadataHandler::SendToPlatform() {
if (cached_metadata_) {
const Vector<char>& serialized_data = cached_metadata_->SerializedData();
sender_->Send(serialized_data.data(), serialized_data.size());
} else {
sender_->Send(nullptr, 0);
}
}
} // namespace blink
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_SCRIPT_CACHED_METADATA_HANDLER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_SCRIPT_CACHED_METADATA_HANDLER_H_
#include "third_party/blink/renderer/platform/loader/fetch/cached_metadata_handler.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/forward.h"
#include "third_party/blink/renderer/platform/wtf/text/movable_string.h"
#include "third_party/blink/renderer/platform/wtf/text/text_encoding.h"
namespace blink {
class CachedMetadata;
class CachedMetadataSender;
// ScriptCachedMetadataHandler should be created when a response is received,
// and can be used independently from its Resource.
// - It doesn't have any references to the Resource. Necessary data are captured
// from the Resource when the handler is created.
// - It is not affected by Resource's revalidation on MemoryCache. The validity
// of the handler is solely checked by |response_url_| and |response_time_|
// (not by Resource) by the browser process, and the cached metadata written to
// the handler is rejected if e.g. the disk cache entry has been updated and the
// handler refers to an older response.
class PLATFORM_EXPORT ScriptCachedMetadataHandler final
: public SingleCachedMetadataHandler {
public:
ScriptCachedMetadataHandler(const WTF::TextEncoding&,
std::unique_ptr<CachedMetadataSender>);
~ScriptCachedMetadataHandler() override = default;
void Trace(blink::Visitor*) override;
void SetCachedMetadata(uint32_t, const char*, size_t, CacheType) override;
void ClearCachedMetadata(CacheType) override;
scoped_refptr<CachedMetadata> GetCachedMetadata(uint32_t) const override;
// This returns the encoding at the time of ResponseReceived(). Therefore this
// does NOT reflect encoding detection from body contents, but the actual
// encoding after the encoding detection can be determined uniquely from
// Encoding(), provided the body content is the same, as we can assume the
// encoding detection will result in the same final encoding.
// TODO(hiroshige): Make these semantics cleaner.
String Encoding() const override;
bool IsServedFromCacheStorage() const override;
// Sets the serialized metadata retrieved from the platform's cache.
void SetSerializedCachedMetadata(const char*, size_t);
private:
void SendToPlatform();
scoped_refptr<CachedMetadata> cached_metadata_;
std::unique_ptr<CachedMetadataSender> sender_;
const WTF::TextEncoding encoding_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_SCRIPT_CACHED_METADATA_HANDLER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment