Commit 7c629b1b authored by Jason Chase's avatar Jason Chase Committed by Commit Bot

Extract common method to get current script url

As a follow up to cl/21773473 [1], there are now 3 code locations that
get the current script url from the V8 stack. Each location had their
own helper function with very similar logic.

This CL extracts the equivalent logic into a reusable function, and
uses the new function in all 3 locations.

[1] https://chromium-review.googlesource.com/c/chromium/src/+/2173473/13/third_party/blink/renderer/core/loader/http_equiv.cc#53

Bug: 1073920
Change-Id: I237144bc1cfa75f5beecc9977f522549d8c1fbaf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2273599Reviewed-by: default avatarAndrey Kosyakov <caseq@chromium.org>
Reviewed-by: default avatarDavid Bokan <bokan@chromium.org>
Commit-Queue: Jason Chase <chasej@chromium.org>
Cr-Commit-Position: refs/heads/master@{#786308}
parent 5ba2a3a7
......@@ -14,6 +14,7 @@
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/probe/core_probes.h"
#include "third_party/blink/renderer/platform/bindings/v8_binding.h"
#include "third_party/blink/renderer/platform/loader/fetch/fetch_initiator_type_names.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_request.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
......@@ -90,23 +91,7 @@ void AdTracker::Shutdown() {
}
String AdTracker::ScriptAtTopOfStack() {
// CurrentStackTrace is 10x faster than CaptureStackTrace if all that you need
// is the url of the script at the top of the stack. See crbug.com/1057211 for
// more detail.
v8::Isolate* isolate = v8::Isolate::GetCurrent();
DCHECK(isolate);
v8::Local<v8::StackTrace> stack_trace =
v8::StackTrace::CurrentStackTrace(isolate, /*frame_limit=*/1);
if (stack_trace.IsEmpty() || stack_trace->GetFrameCount() < 1)
return String();
v8::Local<v8::StackFrame> frame = stack_trace->GetFrame(isolate, 0);
v8::Local<v8::String> script_name = frame->GetScriptNameOrSourceURL();
if (script_name.IsEmpty() || !script_name->Length())
return String();
return ToCoreString(script_name);
return GetCurrentScriptUrl(/*max_stack_depth=*/1);
}
ExecutionContext* AdTracker::GetCurrentExecutionContext() {
......
......@@ -41,6 +41,7 @@
#include "third_party/blink/renderer/core/paint/paint_layer.h"
#include "third_party/blink/renderer/core/paint/paint_layer_paint_order_iterator.h"
#include "third_party/blink/renderer/core/paint/paint_layer_scrollable_area.h"
#include "third_party/blink/renderer/platform/bindings/v8_binding.h"
#include "v8/include/v8-inspector.h"
namespace blink {
......@@ -94,29 +95,6 @@ std::unique_ptr<protocol::DOMSnapshot::RareBooleanData> BooleanData() {
.build();
}
String GetOriginUrlFast(int max_stack_depth) {
static const v8::StackTrace::StackTraceOptions stackTraceOptions =
static_cast<v8::StackTrace::StackTraceOptions>(v8::StackTrace::kDetailed);
v8::Isolate* isolate = v8::Isolate::GetCurrent();
DCHECK(isolate);
v8::Local<v8::StackTrace> v8StackTrace = v8::StackTrace::CurrentStackTrace(
isolate, max_stack_depth, stackTraceOptions);
if (v8StackTrace.IsEmpty())
return String();
for (int i = 0, frame_count = v8StackTrace->GetFrameCount(); i < frame_count;
++i) {
v8::Local<v8::StackFrame> frame = v8StackTrace->GetFrame(isolate, i);
if (frame.IsEmpty())
continue;
v8::Local<v8::String> script_name = frame->GetScriptNameOrSourceURL();
if (script_name.IsEmpty() || !script_name->Length())
continue;
return ToCoreString(script_name);
}
return String();
}
String GetOriginUrl(const Node* node) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
ThreadDebugger* debugger = ThreadDebugger::From(isolate);
......@@ -124,10 +102,10 @@ String GetOriginUrl(const Node* node) {
return String();
v8::HandleScope handleScope(isolate);
// Try not getting the entire stack first.
String url = GetOriginUrlFast(/* maxStackSize=*/5);
String url = GetCurrentScriptUrl(/* maxStackSize=*/5);
if (!url.IsEmpty())
return url;
url = GetOriginUrlFast(/* maxStackSize=*/200);
url = GetCurrentScriptUrl(/* maxStackSize=*/200);
if (!url.IsEmpty())
return url;
// If we did not get anything from the sync stack, let's try the slow
......
......@@ -26,7 +26,6 @@
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/weborigin/reporting_disposition.h"
#include "v8/include/v8.h"
namespace blink {
......@@ -48,35 +47,6 @@ bool AllowScriptFromSourceWithoutNotifying(
return allow_script;
}
// Gets the url of the currently executing script. Returns empty url, if no
// script is executing (e.g. during parsing of a meta tag in markup), or the
// script context is otherwise unavailable.
// TODO(crbug.com/1073920): Extract this function into a reusable location. This
// function was cloned from:
// https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/renderer/core/frame/ad_tracker.cc;l=92;drc=51291f88a8f94602d26403716e2dfa781f8846ee?originalUrl=https:%2F%2Fcs.chromium.org%2F
// There's also a similar implementation here:
// https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/renderer/core/inspector/inspector_dom_snapshot_agent.cc;l=97;drc=f002f3b90c510002b98aa08c2fe7f3d93372007e?originalUrl=https:%2F%2Fcs.chromium.org%2F
KURL GetCurrentScriptUrl() {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
if (!isolate || !isolate->InContext())
return NullURL();
// CurrentStackTrace is 10x faster than CaptureStackTrace if all that you need
// is the url of the script at the top of the stack. See crbug.com/1057211 for
// more detail.
v8::Local<v8::StackTrace> stack_trace =
v8::StackTrace::CurrentStackTrace(isolate, /*frame_limit=*/1);
if (stack_trace.IsEmpty() || stack_trace->GetFrameCount() < 1)
return NullURL();
v8::Local<v8::StackFrame> frame = stack_trace->GetFrame(isolate, 0);
v8::Local<v8::String> script_name = frame->GetScriptNameOrSourceURL();
if (script_name.IsEmpty() || !script_name->Length())
return NullURL();
return KURL(ToCoreString(script_name));
}
} // namespace
void HttpEquiv::Process(Document& document,
......@@ -181,7 +151,7 @@ void HttpEquiv::ProcessHttpEquivOriginTrial(LocalDOMWindow* window,
// the comment thread in the design doc for details:
// https://docs.google.com/document/d/1xALH9W7rWmX0FpjudhDeS2TNTEOXuPn4Tlc9VmuPdHA/edit?disco=AAAAJyG8StI
if (RuntimeEnabledFeatures::ThirdPartyOriginTrialsEnabled()) {
KURL external_script_url = GetCurrentScriptUrl();
KURL external_script_url(GetCurrentScriptUrl(/*max_stack_depth=*/1));
if (external_script_url.IsValid()) {
scoped_refptr<SecurityOrigin> external_origin =
......
......@@ -50,4 +50,30 @@ v8::Local<v8::Value> FreezeV8Object(v8::Local<v8::Value> value,
return value;
}
String GetCurrentScriptUrl(int max_stack_depth) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
DCHECK(isolate);
if (!isolate->InContext())
return String();
// CurrentStackTrace is 10x faster than CaptureStackTrace if all that you need
// is the url of the script at the top of the stack. See crbug.com/1057211 for
// more detail.
v8::Local<v8::StackTrace> stack_trace =
v8::StackTrace::CurrentStackTrace(isolate, max_stack_depth);
if (stack_trace.IsEmpty())
return String();
for (int i = 0, frame_count = stack_trace->GetFrameCount(); i < frame_count;
++i) {
v8::Local<v8::StackFrame> frame = stack_trace->GetFrame(isolate, i);
if (frame.IsEmpty())
continue;
v8::Local<v8::String> script_name = frame->GetScriptNameOrSourceURL();
if (script_name.IsEmpty() || !script_name->Length())
continue;
return ToCoreString(script_name);
}
return String();
}
} // namespace blink
......@@ -400,6 +400,11 @@ enum class NamedPropertyDeleterResult {
kDidNotDelete, // Intercepted but failed to delete.
};
// Gets the url of the currently executing script. Returns empty string, if no
// script is executing (e.g. during parsing of a meta tag in markup), or the
// script context is otherwise unavailable.
PLATFORM_EXPORT String GetCurrentScriptUrl(int max_stack_depth);
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_V8_BINDING_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