#include "third_party/blink/renderer/core/html/parser/html_document_parser.h"
#include <memory>
#include <utility>
#include "base/feature_list.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/strcat.h"
#include "base/synchronization/lock.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/types/optional_util.h"
#include "components/miracle_parameter/common/public/miracle_parameter.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/loader/loading_behavior_flag.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/renderer/core/css/media_values_cached.h"
#include "third_party/blink/renderer/core/css/style_engine.h"
#include "third_party/blink/renderer/core/dom/document_fragment.h"
#include "third_party/blink/renderer/core/dom/element.h"
#include "third_party/blink/renderer/core/execution_context/agent.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/settings.h"
#include "third_party/blink/renderer/core/html/html_document.h"
#include "third_party/blink/renderer/core/html/html_meta_element.h"
#include "third_party/blink/renderer/core/html/html_template_element.h"
#include "third_party/blink/renderer/core/html/nesting_level_incrementer.h"
#include "third_party/blink/renderer/core/html/parser/atomic_html_token.h"
#include "third_party/blink/renderer/core/html/parser/background_html_scanner.h"
#include "third_party/blink/renderer/core/html/parser/html_element_stack.h"
#include "third_party/blink/renderer/core/html/parser/html_parser_metrics.h"
#include "third_party/blink/renderer/core/html/parser/html_preload_scanner.h"
#include "third_party/blink/renderer/core/html/parser/html_resource_preloader.h"
#include "third_party/blink/renderer/core/html/parser/html_tree_builder.h"
#include "third_party/blink/renderer/core/html_names.h"
#include "third_party/blink/renderer/core/inspector/inspector_trace_events.h"
#include "third_party/blink/renderer/core/loader/document_loader.h"
#include "third_party/blink/renderer/core/loader/prefetched_signed_exchange_manager.h"
#include "third_party/blink/renderer/core/loader/preload_helper.h"
#include "third_party/blink/renderer/core/probe/core_probes.h"
#include "third_party/blink/renderer/core/script/html_parser_script_runner.h"
#include "third_party/blink/renderer/platform/bindings/runtime_call_stats.h"
#include "third_party/blink/renderer/platform/heap/cross_thread_handle.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/heap/persistent.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_fetcher.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/non_main_thread.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread_scheduler.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_base.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"
#include "third_party/blink/renderer/platform/wtf/shared_buffer.h"
#include "third_party/blink/renderer/platform/wtf/thread_safe_ref_counted.h"
#include "third_party/blink/renderer/platform/wtf/wtf.h"
namespace blink {
constexpr int kDefaultMaxTokenizationBudget = …;
constexpr int kInfiniteTokenizationBudget = …;
constexpr int kNumYieldsWithDefaultBudget = …;
class EndIfDelayedForbiddenScope;
class ShouldCompleteScope;
class AttemptToEndForbiddenScope;
enum class FeatureResetMode { … };
const char kHistogramScanAndPreloadTime[] = …;
bool ThreadedPreloadScannerEnabled(
FeatureResetMode reset_mode = FeatureResetMode::kUseCached) { … }
bool TimedParserBudgetEnabled() { … }
bool CheckParserBudgetLessOften() { … }
bool PrecompileInlineScriptsEnabled(
FeatureResetMode reset_mode = FeatureResetMode::kUseCached) { … }
NonMainThread* GetPreloadScannerThread() { … }
PreloadProcessingMode GetPreloadProcessingMode() { … }
bool BackgroundScanMainFrameOnly() { … }
bool IsPreloadScanningEnabled(Document* document) { … }
MIRACLE_PARAMETER_FOR_TIME_DELTA(GetDefaultParserBudget,
features::kTimedHTMLParserBudget,
"default-parser-budget",
base::Milliseconds(10))
constexpr int kNumYieldsWithDefaultBudgetDefaultValue = …2
#else
6
#endif
;
MIRACLE_PARAMETER_FOR_INT(GetNumYieldsWithDefaultBudget,
features::kTimedHTMLParserBudget,
"num-yields-with-default-budget",
kNumYieldsWithDefaultBudgetDefaultValue)
constexpr base::TimeDelta kLongParserBudgetDefaultValue = …base::Milliseconds(50)
#else
base::Milliseconds(500)
#endif
;
MIRACLE_PARAMETER_FOR_TIME_DELTA(GetLongParserBudget,
features::kTimedHTMLParserBudget,
"long-parser-budget",
kLongParserBudgetDefaultValue)
base::TimeDelta GetDefaultTimedBudget() { … }
base::TimeDelta GetTimedBudget(int times_yielded) { … }
class EndIfDelayedForbiddenScope { … };
class AttemptToEndForbiddenScope { … };
class ShouldCompleteScope { … };
static HTMLTokenizer::State TokenizerStateForContextElement(
Element* context_element,
bool report_errors,
const HTMLParserOptions& options) { … }
HTMLDocumentParserState::HTMLDocumentParserState(
ParserSynchronizationPolicy mode,
int budget)
: … { … }
class HTMLDocumentParser::PendingPreloads
: public ThreadSafeRefCounted<PendingPreloads> { … };
HTMLDocumentParser::HTMLDocumentParser(HTMLDocument& document,
ParserSynchronizationPolicy sync_policy,
ParserPrefetchPolicy prefetch_policy)
: … { … }
HTMLDocumentParser::HTMLDocumentParser(
DocumentFragment* fragment,
Element* context_element,
ParserContentPolicy parser_content_policy,
ParserPrefetchPolicy parser_prefetch_policy)
: … { … }
HTMLDocumentParser::HTMLDocumentParser(Document& document,
ParserContentPolicy content_policy,
ParserSynchronizationPolicy sync_policy,
ParserPrefetchPolicy prefetch_policy)
: … { … }
HTMLDocumentParser::~HTMLDocumentParser() { … }
void HTMLDocumentParser::Trace(Visitor* visitor) const { … }
bool HTMLDocumentParser::HasPendingWorkScheduledForTesting() const { … }
unsigned HTMLDocumentParser::GetChunkCountForTesting() const { … }
void HTMLDocumentParser::Detach() { … }
void HTMLDocumentParser::StopParsing() { … }
void HTMLDocumentParser::PrepareToStopParsing() { … }
bool HTMLDocumentParser::IsParsingFragment() const { … }
void HTMLDocumentParser::DeferredPumpTokenizerIfPossible(
bool from_finish_append,
base::TimeTicks schedule_time) { … }
void HTMLDocumentParser::PumpTokenizerIfPossible() { … }
void HTMLDocumentParser::RunScriptsForPausedTreeBuilder() { … }
void HTMLDocumentParser::ForcePlaintextForTextDocument() { … }
bool HTMLDocumentParser::PumpTokenizer() { … }
void HTMLDocumentParser::SchedulePumpTokenizer(bool from_finish_append) { … }
void HTMLDocumentParser::ScheduleEndIfDelayed() { … }
void HTMLDocumentParser::ConstructTreeFromToken(AtomicHTMLToken& atomic_token) { … }
bool HTMLDocumentParser::HasInsertionPoint() { … }
void HTMLDocumentParser::insert(const String& source) { … }
void HTMLDocumentParser::Append(const String& input_source) { … }
void HTMLDocumentParser::FinishAppend() { … }
void HTMLDocumentParser::CommitPreloadedData() { … }
void HTMLDocumentParser::end() { … }
void HTMLDocumentParser::AttemptToRunDeferredScriptsAndEnd() { … }
bool HTMLDocumentParser::ShouldDelayEnd() const { … }
void HTMLDocumentParser::AttemptToEnd() { … }
void HTMLDocumentParser::EndIfDelayed() { … }
void HTMLDocumentParser::Finish() { … }
bool HTMLDocumentParser::IsExecutingScript() const { … }
OrdinalNumber HTMLDocumentParser::LineNumber() const { … }
TextPosition HTMLDocumentParser::GetTextPosition() const { … }
bool HTMLDocumentParser::IsWaitingForScripts() const { … }
void HTMLDocumentParser::ResumeParsingAfterPause() { … }
void HTMLDocumentParser::AppendCurrentInputStreamToPreloadScannerAndScan() { … }
void HTMLDocumentParser::NotifyScriptLoaded() { … }
void HTMLDocumentParser::ResetCachedFeaturesForTesting() { … }
void HTMLDocumentParser::FlushPreloadScannerThreadForTesting() { … }
void HTMLDocumentParser::ExecuteScriptsWaitingForResources() { … }
void HTMLDocumentParser::DidAddPendingParserBlockingStylesheet() { … }
void HTMLDocumentParser::DidLoadAllPendingParserBlockingStylesheets() { … }
void HTMLDocumentParser::CheckIfBlockingStylesheetAdded() { … }
void HTMLDocumentParser::ParseDocumentFragment(
const String& source,
DocumentFragment* fragment,
Element* context_element,
ParserContentPolicy parser_content_policy) { … }
void HTMLDocumentParser::AppendBytes(base::span<const uint8_t> data) { … }
void HTMLDocumentParser::Flush() { … }
void HTMLDocumentParser::SetDecoder(
std::unique_ptr<TextResourceDecoder> decoder) { … }
void HTMLDocumentParser::DocumentElementAvailable() { … }
std::unique_ptr<HTMLPreloadScanner> HTMLDocumentParser::CreatePreloadScanner(
TokenPreloadScanner::ScannerType scanner_type) { … }
void HTMLDocumentParser::ScanAndPreload(HTMLPreloadScanner* scanner) { … }
void HTMLDocumentParser::ProcessPreloadData(
std::unique_ptr<PendingPreloadData> preload_data) { … }
void HTMLDocumentParser::FetchQueuedPreloads() { … }
std::string HTMLDocumentParser::GetPreloadHistogramSuffix() { … }
DocumentParser::BackgroundScanCallback
HTMLDocumentParser::TakeBackgroundScanCallback() { … }
void HTMLDocumentParser::ScanInBackground(const String& source) { … }
void HTMLDocumentParser::AddPreloadDataOnBackgroundThread(
CrossThreadWeakHandle<HTMLDocumentParser> parser_handle,
scoped_refptr<PendingPreloads> pending_preloads,
scoped_refptr<base::SequencedTaskRunner> task_runner,
std::unique_ptr<PendingPreloadData> preload_data) { … }
bool HTMLDocumentParser::HasPendingPreloads() { … }
void HTMLDocumentParser::FlushPendingPreloads() { … }
bool HTMLDocumentParser::ShouldPumpTokenizerNowForFinishAppend() const { … }
ALWAYS_INLINE bool HTMLDocumentParser::ShouldCheckTimeBudget(
NextTokenStatus next_token_status,
html_names::HTMLTag tag,
int newly_consumed_characters,
int tokens_parsed) const { … }
}