Commit 2cc82d4b authored by Leszek Swirski's avatar Leszek Swirski Committed by Commit Bot

[blink] Harden inspector ScriptResource content access

Check for an edge case where a ScriptResource may have a null resource
buffer without having been loaded. As part of this, extract the
inspector's logic for accessing a ScriptResource's text into the
ScriptResource, as it relies on ScriptResource internal state.

Bug: 865098
Bug: 899217
Change-Id: I5ceb6f105e21e4eeb7ce5f9bc82c7d7c7f7f6aad
Reviewed-on: https://chromium-review.googlesource.com/c/1352183
Commit-Queue: Leszek Swirski <leszeks@chromium.org>
Reviewed-by: default avatarHiroshige Hayashizaki <hiroshige@chromium.org>
Cr-Commit-Position: refs/heads/master@{#611255}
parent 86487ee6
...@@ -348,9 +348,7 @@ bool InspectorPageAgent::CachedResourceContent(Resource* cached_resource, ...@@ -348,9 +348,7 @@ bool InspectorPageAgent::CachedResourceContent(Resource* cached_resource,
return true; return true;
case blink::ResourceType::kScript: case blink::ResourceType::kScript:
MaybeEncodeTextContent( MaybeEncodeTextContent(
cached_resource->ResourceBuffer() ToScriptResource(cached_resource)->TextForInspector(),
? ToScriptResource(cached_resource)->DecodedText()
: ToScriptResource(cached_resource)->SourceText().ToString(),
cached_resource->ResourceBuffer(), result, base64_encoded); cached_resource->ResourceBuffer(), result, base64_encoded);
return true; return true;
default: default:
......
...@@ -145,6 +145,35 @@ const ParkableString& ScriptResource::SourceText() { ...@@ -145,6 +145,35 @@ const ParkableString& ScriptResource::SourceText() {
return source_text_; return source_text_;
} }
String ScriptResource::TextForInspector() const {
// If the resource buffer exists, we can safely return the decoded text.
if (ResourceBuffer())
return DecodedText();
// If there is no resource buffer, then we have three cases.
// TODO(crbug.com/865098): Simplify the below code and remove the CHECKs once
// the assumptions are confirmed.
if (IsLoaded()) {
CHECK(IsFinishedInternal());
if (!source_text_.IsNull()) {
// 1. We have finished loading, and have already decoded the buffer into
// the source text and cleared the resource buffer to save space.
return source_text_.ToString();
}
// 2. We have finished loading with no data received, so no streaming ever
// happened or streaming was suppressed.
CHECK(!streamer_ || streamer_->StreamingSuppressedReason() ==
ScriptStreamer::kScriptTooSmall);
return "";
}
// 3. We haven't started loading, and actually haven't received any data yet
// at all to initialise the resource buffer, so the resource is empty.
return "";
}
SingleCachedMetadataHandler* ScriptResource::CacheHandler() { SingleCachedMetadataHandler* ScriptResource::CacheHandler() {
return static_cast<SingleCachedMetadataHandler*>(Resource::CacheHandler()); return static_cast<SingleCachedMetadataHandler*>(Resource::CacheHandler());
} }
......
...@@ -132,6 +132,10 @@ class CORE_EXPORT ScriptResource final : public TextResource { ...@@ -132,6 +132,10 @@ class CORE_EXPORT ScriptResource final : public TextResource {
const ParkableString& SourceText(); const ParkableString& SourceText();
// Get the resource's current text. This can return partial data, so should
// not be used outside of the inspector.
String TextForInspector() const;
SingleCachedMetadataHandler* CacheHandler(); SingleCachedMetadataHandler* CacheHandler();
// Gets the script streamer from the ScriptResource, clearing the resource's // Gets the script streamer from the ScriptResource, clearing the resource's
......
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