1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
/*
* Copyright (C) 2010 Google, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "third_party/blink/renderer/core/script/script_runner.h"
#include <algorithm>
#include "base/feature_list.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/loader/document_loader.h"
#include "third_party/blink/renderer/core/script/script_loader.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/scheduler/public/cooperative_scheduling_manager.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread_scheduler.h"
#include "third_party/blink/renderer/platform/wtf/casting.h"
namespace blink {
ScriptRunner::ScriptRunner(Document* document)
: ExecutionContextLifecycleStateObserver(document->GetExecutionContext()),
document_(document),
task_runner_(document->GetTaskRunner(TaskType::kNetworking)) {
DCHECK(document);
UpdateStateIfNeeded();
}
void ScriptRunner::QueueScriptForExecution(PendingScript* pending_script) {
DCHECK(pending_script);
document_->IncrementLoadEventDelayCount();
switch (pending_script->GetSchedulingType()) {
case ScriptSchedulingType::kAsync:
pending_async_scripts_.insert(pending_script);
break;
case ScriptSchedulingType::kInOrder:
pending_in_order_scripts_.push_back(pending_script);
number_of_in_order_scripts_with_pending_notification_++;
break;
default:
NOTREACHED();
break;
}
}
void ScriptRunner::PostTask(const base::Location& web_trace_location) {
task_runner_->PostTask(
web_trace_location,
WTF::Bind(&ScriptRunner::ExecuteTask, WrapWeakPersistent(this)));
}
void ScriptRunner::ContextLifecycleStateChanged(
mojom::FrameLifecycleState state) {
if (!IsExecutionSuspended())
PostTasksForReadyScripts(FROM_HERE);
}
bool ScriptRunner::IsExecutionSuspended() {
return !GetExecutionContext() || GetExecutionContext()->IsContextPaused() ||
is_force_deferred_;
}
void ScriptRunner::SetForceDeferredExecution(bool force_deferred) {
DCHECK(force_deferred != is_force_deferred_);
is_force_deferred_ = force_deferred;
if (!IsExecutionSuspended())
PostTasksForReadyScripts(FROM_HERE);
}
void ScriptRunner::PostTasksForReadyScripts(
const base::Location& web_trace_location) {
DCHECK(!IsExecutionSuspended());
for (size_t i = 0; i < async_scripts_to_execute_soon_.size(); ++i) {
PostTask(web_trace_location);
}
for (size_t i = 0; i < in_order_scripts_to_execute_soon_.size(); ++i) {
PostTask(web_trace_location);
}
}
void ScriptRunner::ScheduleReadyInOrderScripts() {
while (!pending_in_order_scripts_.IsEmpty() &&
pending_in_order_scripts_.front()
->IsReady()) {
in_order_scripts_to_execute_soon_.push_back(
pending_in_order_scripts_.TakeFirst());
PostTask(FROM_HERE);
}
}
void ScriptRunner::DelayAsyncScriptUntilMilestoneReached(
PendingScript* pending_script) {
DCHECK(!delay_async_script_milestone_reached_);
SECURITY_CHECK(pending_async_scripts_.Contains(pending_script));
pending_async_scripts_.erase(pending_script);
// When the ScriptRunner is notified via
// |NotifyDelayedAsyncScriptsMilestoneReached()|, the scripts in
// |pending_delayed_async_scripts_| will be scheduled for execution.
pending_delayed_async_scripts_.push_back(pending_script);
}
void ScriptRunner::NotifyDelayedAsyncScriptsMilestoneReached() {
delay_async_script_milestone_reached_ = true;
while (!pending_delayed_async_scripts_.IsEmpty()) {
PendingScript* pending_script = pending_delayed_async_scripts_.TakeFirst();
DCHECK_EQ(pending_script->GetSchedulingType(),
ScriptSchedulingType::kAsync);
async_scripts_to_execute_soon_.push_back(pending_script);
PostTask(FROM_HERE);
}
}
bool ScriptRunner::CanDelayAsyncScripts() {
if (delay_async_script_milestone_reached_)
return false;
// We first check to see if the base::Feature is enabled, before the
// RuntimeEnabledFeatures. This is because the RuntimeEnabledFeatures simply
// exist for testing, so if they are enabled *and* the base::Feature is
// enabled, we should log UKM via DocumentLoader::DidObserveLoadingBehavior,
// which is associated with the experiment running the base::Feature flag.
static bool feature_enabled =
base::FeatureList::IsEnabled(features::kDelayAsyncScriptExecution);
bool optimization_guide_hints_unknown =
!document_->GetFrame() ||
!document_->GetFrame()->GetOptimizationGuideHints() ||
!document_->GetFrame()
->GetOptimizationGuideHints()
->delay_async_script_execution_hints ||
document_->GetFrame()
->GetOptimizationGuideHints()
->delay_async_script_execution_hints->delay_type ==
mojom::blink::DelayAsyncScriptExecutionDelayType::kUnknown;
if (feature_enabled) {
if (document_->Parsing() && document_->Loader()) {
document_->Loader()->DidObserveLoadingBehavior(
kLoadingBehaviorAsyncScriptReadyBeforeDocumentFinishedParsing);
}
// If the base::Feature is enabled, we always want to delay async scripts,
// unless we delegate to the OptimizationGuide, but the hints aren't
// available.
if (features::kDelayAsyncScriptExecutionDelayParam.Get() !=
features::DelayAsyncScriptDelayType::kUseOptimizationGuide ||
!optimization_guide_hints_unknown) {
return true;
}
}
// Delay milestone has not been reached yet. We have to check the feature flag
// configuration to see if we are able to delay async scripts or not:
if (RuntimeEnabledFeatures::
DelayAsyncScriptExecutionUntilFinishedParsingEnabled() ||
RuntimeEnabledFeatures::
DelayAsyncScriptExecutionUntilFirstPaintOrFinishedParsingEnabled()) {
return true;
}
return false;
}
void ScriptRunner::NotifyScriptReady(PendingScript* pending_script) {
SECURITY_CHECK(pending_script);
switch (pending_script->GetSchedulingType()) {
case ScriptSchedulingType::kAsync:
// SECURITY_CHECK() makes us crash in a controlled way in error cases
// where the PendingScript is associated with the wrong ScriptRunner
// (otherwise we'd cause a use-after-free in ~ScriptRunner when it tries
// to detach).
SECURITY_CHECK(pending_async_scripts_.Contains(pending_script));
if (pending_script->IsEligibleForDelay() && CanDelayAsyncScripts()) {
DelayAsyncScriptUntilMilestoneReached(pending_script);
return;
}
pending_async_scripts_.erase(pending_script);
async_scripts_to_execute_soon_.push_back(pending_script);
PostTask(FROM_HERE);
break;
case ScriptSchedulingType::kInOrder:
SECURITY_CHECK(number_of_in_order_scripts_with_pending_notification_ > 0);
number_of_in_order_scripts_with_pending_notification_--;
ScheduleReadyInOrderScripts();
break;
default:
NOTREACHED();
break;
}
}
bool ScriptRunner::RemovePendingInOrderScript(PendingScript* pending_script) {
auto it = std::find(pending_in_order_scripts_.begin(),
pending_in_order_scripts_.end(), pending_script);
if (it == pending_in_order_scripts_.end())
return false;
pending_in_order_scripts_.erase(it);
SECURITY_CHECK(number_of_in_order_scripts_with_pending_notification_ > 0);
number_of_in_order_scripts_with_pending_notification_--;
return true;
}
void ScriptRunner::MovePendingScript(Document& old_document,
Document& new_document,
ScriptLoader* script_loader) {
Document* new_context_document =
new_document.GetExecutionContext()
? To<LocalDOMWindow>(new_document.GetExecutionContext())->document()
: &new_document;
Document* old_context_document =
old_document.GetExecutionContext()
? To<LocalDOMWindow>(old_document.GetExecutionContext())->document()
: &old_document;
if (old_context_document == new_context_document)
return;
PendingScript* pending_script =
script_loader
->GetPendingScriptIfControlledByScriptRunnerForCrossDocMove();
if (!pending_script) {
// The ScriptLoader is not controlled by ScriptRunner. This can happen
// because MovePendingScript() is called for all <script> elements
// moved between Documents, not only for those controlled by ScriptRunner.
return;
}
old_context_document->GetScriptRunner()->MovePendingScript(
new_context_document->GetScriptRunner(), pending_script);
}
void ScriptRunner::MovePendingScript(ScriptRunner* new_runner,
PendingScript* pending_script) {
auto it = pending_async_scripts_.find(pending_script);
if (it != pending_async_scripts_.end()) {
new_runner->QueueScriptForExecution(pending_script);
pending_async_scripts_.erase(it);
document_->DecrementLoadEventDelayCount();
return;
}
if (RemovePendingInOrderScript(pending_script)) {
new_runner->QueueScriptForExecution(pending_script);
document_->DecrementLoadEventDelayCount();
}
}
bool ScriptRunner::ExecuteInOrderTask() {
TRACE_EVENT0("blink", "ScriptRunner::ExecuteInOrderTask");
if (in_order_scripts_to_execute_soon_.IsEmpty())
return false;
PendingScript* pending_script = in_order_scripts_to_execute_soon_.TakeFirst();
DCHECK(pending_script);
DCHECK_EQ(pending_script->GetSchedulingType(), ScriptSchedulingType::kInOrder)
<< "In-order scripts queue should not contain any async script.";
pending_script->ExecuteScriptBlock(NullURL());
document_->DecrementLoadEventDelayCount();
return true;
}
bool ScriptRunner::ExecuteAsyncTask() {
TRACE_EVENT0("blink", "ScriptRunner::ExecuteAsyncTask");
if (async_scripts_to_execute_soon_.IsEmpty())
return false;
// Remove the async script loader from the ready-to-exec set and execute.
PendingScript* pending_script = async_scripts_to_execute_soon_.TakeFirst();
DCHECK_EQ(pending_script->GetSchedulingType(), ScriptSchedulingType::kAsync)
<< "Async scripts queue should not contain any in-order script.";
pending_script->ExecuteScriptBlock(NullURL());
document_->DecrementLoadEventDelayCount();
return true;
}
void ScriptRunner::ExecuteTask() {
// This method is triggered by ScriptRunner::PostTask, and runs directly from
// the scheduler. So, the call stack is safe to reenter.
scheduler::CooperativeSchedulingManager::AllowedStackScope
allowed_stack_scope(scheduler::CooperativeSchedulingManager::Instance());
if (IsExecutionSuspended())
return;
if (ExecuteAsyncTask())
return;
if (ExecuteInOrderTask())
return;
}
void ScriptRunner::Trace(Visitor* visitor) const {
ExecutionContextLifecycleStateObserver::Trace(visitor);
visitor->Trace(document_);
visitor->Trace(pending_in_order_scripts_);
visitor->Trace(pending_async_scripts_);
visitor->Trace(pending_delayed_async_scripts_);
visitor->Trace(async_scripts_to_execute_soon_);
visitor->Trace(in_order_scripts_to_execute_soon_);
}
} // namespace blink
|