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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
// Copyright 2016 The Chromium Authors
// 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/core/html/link_style.h"
#include "base/metrics/histogram_functions.h"
#include "services/network/public/mojom/referrer_policy.mojom-blink.h"
#include "third_party/blink/renderer/core/css/style_sheet_contents.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/frame/csp/content_security_policy.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/local_frame_client.h"
#include "third_party/blink/renderer/core/html/cross_origin_attribute.h"
#include "third_party/blink/renderer/core/html/html_link_element.h"
#include "third_party/blink/renderer/core/html_names.h"
#include "third_party/blink/renderer/core/inspector/inspector_audits_issue.h"
#include "third_party/blink/renderer/core/loader/fetch_priority_attribute.h"
#include "third_party/blink/renderer/core/loader/link_load_parameters.h"
#include "third_party/blink/renderer/core/loader/resource/css_style_sheet_resource.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/loader/fetch/fetch_parameters.h"
#include "third_party/blink/renderer/platform/loader/subresource_integrity.h"
#include "third_party/blink/renderer/platform/network/mime/content_type.h"
#include "third_party/blink/renderer/platform/network/mime/mime_type_registry.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/weborigin/security_policy.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
namespace blink {
static bool StyleSheetTypeIsSupported(const String& type) {
String trimmed_type = ContentType(type).GetType();
return trimmed_type.empty() ||
MIMETypeRegistry::IsSupportedStyleSheetMIMEType(trimmed_type);
}
LinkStyle::LinkStyle(HTMLLinkElement* owner)
: LinkResource(owner),
disabled_state_(kUnset),
pending_sheet_type_(PendingSheetType::kNone),
render_blocking_behavior_(RenderBlockingBehavior::kUnset),
loading_(false),
fired_load_(false),
loaded_sheet_(false) {}
LinkStyle::~LinkStyle() = default;
void LinkStyle::NotifyFinished(Resource* resource) {
if (!owner_->isConnected()) {
// While the stylesheet is asynchronously loading, the owner can be
// disconnected from a document.
// In that case, cancel any processing on the loaded content.
loading_ = false;
RemovePendingSheet();
if (sheet_)
ClearSheet();
return;
}
if (resource->LoadFailedOrCanceled()) {
AuditsIssue::ReportStylesheetLoadingRequestFailedIssue(
&GetDocument(), resource->Url(),
resource->LastResourceRequest().GetDevToolsId(), GetDocument().Url(),
resource->Options().initiator_info.position.line_,
resource->Options().initiator_info.position.column_,
resource->GetResourceError().LocalizedDescription());
}
auto* cached_style_sheet = To<CSSStyleSheetResource>(resource);
if ((!cached_style_sheet->ErrorOccurred() &&
!owner_->FastGetAttribute(html_names::kIntegrityAttr).empty() &&
!cached_style_sheet->IntegrityMetadata().empty()) ||
resource->ForceIntegrityChecks()) {
cached_style_sheet->IntegrityReport().SendReports(GetExecutionContext());
if (!cached_style_sheet->PassedIntegrityChecks()) {
loading_ = false;
RemovePendingSheet();
NotifyLoadedSheetAndAllCriticalSubresources(
Node::kErrorOccurredLoadingSubresource);
return;
}
}
auto* parser_context = MakeGarbageCollected<CSSParserContext>(
GetDocument(), cached_style_sheet->GetResponse().ResponseUrl(),
cached_style_sheet->GetResponse().IsCorsSameOrigin(),
Referrer(cached_style_sheet->GetResponse().ResponseUrl(),
cached_style_sheet->GetReferrerPolicy()),
cached_style_sheet->Encoding());
if (cached_style_sheet->GetResourceRequest().IsAdResource()) {
parser_context->SetIsAdRelated();
}
if (StyleSheetContents* parsed_sheet =
cached_style_sheet->CreateParsedStyleSheetFromCache(parser_context)) {
if (sheet_)
ClearSheet();
sheet_ = MakeGarbageCollected<CSSStyleSheet>(parsed_sheet, *owner_);
sheet_->SetMediaQueries(
MediaQuerySet::Create(owner_->Media(), GetExecutionContext()));
if (owner_->IsInDocumentTree())
SetSheetTitle(owner_->title());
loading_ = false;
parsed_sheet->CheckLoaded();
parsed_sheet->SetRenderBlocking(render_blocking_behavior_);
return;
}
auto parser_start_time = base::TimeTicks::Now();
auto* style_sheet = MakeGarbageCollected<StyleSheetContents>(
parser_context, cached_style_sheet->Url());
if (sheet_)
ClearSheet();
sheet_ = MakeGarbageCollected<CSSStyleSheet>(style_sheet, *owner_);
sheet_->SetMediaQueries(
MediaQuerySet::Create(owner_->Media(), GetExecutionContext()));
if (owner_->IsInDocumentTree())
SetSheetTitle(owner_->title());
style_sheet->SetRenderBlocking(render_blocking_behavior_);
style_sheet->ParseAuthorStyleSheet(cached_style_sheet);
loading_ = false;
style_sheet->NotifyLoadedSheet(cached_style_sheet);
style_sheet->CheckLoaded();
if (style_sheet->IsCacheableForResource()) {
const_cast<CSSStyleSheetResource*>(cached_style_sheet)
->SaveParsedStyleSheet(style_sheet);
}
base::UmaHistogramMicrosecondsTimes(
"Blink.CSSStyleSheetResource.ParseTime",
base::TimeTicks::Now() - parser_start_time);
ClearResource();
}
bool LinkStyle::SheetLoaded() {
if (!StyleSheetIsLoading()) {
RemovePendingSheet();
return true;
}
return false;
}
void LinkStyle::NotifyLoadedSheetAndAllCriticalSubresources(
Node::LoadedSheetErrorStatus error_status) {
if (fired_load_)
return;
loaded_sheet_ = (error_status == Node::kNoErrorLoadingSubresource);
if (owner_)
owner_->ScheduleEvent();
fired_load_ = true;
}
void LinkStyle::SetToPendingState() {
DCHECK_LT(pending_sheet_type_, PendingSheetType::kBlocking);
AddPendingSheet(PendingSheetType::kBlocking);
}
void LinkStyle::ClearSheet() {
DCHECK(sheet_);
DCHECK_EQ(sheet_->ownerNode(), owner_);
sheet_.Release()->ClearOwnerNode();
}
bool LinkStyle::StyleSheetIsLoading() const {
if (loading_)
return true;
if (!sheet_)
return false;
return sheet_->Contents()->IsLoading();
}
void LinkStyle::AddPendingSheet(PendingSheetType type) {
if (type <= pending_sheet_type_)
return;
pending_sheet_type_ = type;
if (pending_sheet_type_ == PendingSheetType::kNonBlocking)
return;
GetDocument().GetStyleEngine().AddPendingBlockingSheet(*owner_,
pending_sheet_type_);
}
void LinkStyle::RemovePendingSheet() {
DCHECK(owner_);
PendingSheetType type = pending_sheet_type_;
pending_sheet_type_ = PendingSheetType::kNone;
if (type == PendingSheetType::kNone)
return;
if (type == PendingSheetType::kNonBlocking) {
// Tell StyleEngine to re-compute styleSheets of this owner_'s treescope.
GetDocument().GetStyleEngine().ModifiedStyleSheetCandidateNode(*owner_);
return;
}
GetDocument().GetStyleEngine().RemovePendingBlockingSheet(*owner_, type);
}
void LinkStyle::SetDisabledState(bool disabled) {
LinkStyle::DisabledState old_disabled_state = disabled_state_;
disabled_state_ = disabled ? kDisabled : kEnabledViaScript;
// Whenever the disabled attribute is removed, set the link element's
// explicitly enabled attribute to true.
if (!disabled)
explicitly_enabled_ = true;
if (old_disabled_state == disabled_state_)
return;
// If we change the disabled state while the sheet is still loading, then we
// have to perform three checks:
if (StyleSheetIsLoading()) {
// Check #1: The sheet becomes disabled while loading.
if (disabled_state_ == kDisabled)
RemovePendingSheet();
// Check #2: An alternate sheet becomes enabled while it is still loading.
if (owner_->RelAttribute().IsAlternate() &&
disabled_state_ == kEnabledViaScript)
AddPendingSheet(PendingSheetType::kBlocking);
// Check #3: A main sheet becomes enabled while it was still loading and
// after it was disabled via script. It takes really terrible code to make
// this happen (a double toggle for no reason essentially). This happens
// on virtualplastic.net, which manages to do about 12 enable/disables on
// only 3 sheets. :)
if (!owner_->RelAttribute().IsAlternate() &&
disabled_state_ == kEnabledViaScript && old_disabled_state == kDisabled)
AddPendingSheet(PendingSheetType::kBlocking);
// If the sheet is already loading just bail.
return;
}
if (sheet_) {
DCHECK(disabled) << "If link is being enabled, sheet_ shouldn't exist yet";
ClearSheet();
GetDocument().GetStyleEngine().SetNeedsActiveStyleUpdate(
owner_->GetTreeScope());
return;
}
if (disabled_state_ == kEnabledViaScript && owner_->ShouldProcessStyle())
Process(LinkLoadParameters::Reason::kDefault);
}
LinkStyle::LoadReturnValue LinkStyle::LoadStylesheetIfNeeded(
const LinkLoadParameters& params,
const WTF::TextEncoding& charset) {
if (disabled_state_ == kDisabled || !owner_->RelAttribute().IsStyleSheet() ||
!StyleSheetTypeIsSupported(params.type) || !ShouldLoadResource() ||
!params.href.IsValid())
return kNotNeeded;
if (GetResource() && !GetDocument().StatePreservingAtomicMoveInProgress()) {
RemovePendingSheet();
ClearResource();
}
if (!owner_->ShouldLoadLink())
return kBail;
loading_ = true;
String title = owner_->title();
if (!title.empty() && !owner_->IsAlternate() &&
disabled_state_ != kEnabledViaScript && owner_->IsInDocumentTree()) {
GetDocument().GetStyleEngine().SetPreferredStylesheetSetNameIfNotSet(title);
}
bool media_query_matches = true;
LocalFrame* frame = LoadingFrame();
if (!owner_->Media().empty() && frame) {
MediaQuerySet* media =
MediaQuerySet::Create(owner_->Media(), GetExecutionContext());
MediaQueryEvaluator* evaluator =
MakeGarbageCollected<MediaQueryEvaluator>(frame);
media_query_matches = evaluator->Eval(*media);
}
// Don't hold up layout tree construction and script execution on
// stylesheets that are not needed for the layout at the moment.
bool critical_style = media_query_matches && !owner_->IsAlternate();
auto type_and_behavior = ComputePendingSheetTypeAndRenderBlockingBehavior(
*owner_, critical_style, owner_->IsCreatedByParser());
PendingSheetType type = type_and_behavior.first;
if (!GetDocument().StatePreservingAtomicMoveInProgress()) {
AddPendingSheet(type);
}
// Load stylesheets that are not needed for the layout immediately with low
// priority. When the link element is created by scripts, load the
// stylesheets asynchronously but in high priority.
FetchParameters::DeferOption defer_option =
!critical_style ? FetchParameters::kLazyLoad : FetchParameters::kNoDefer;
render_blocking_behavior_ = type_and_behavior.second;
owner_->LoadStylesheet(params, charset, defer_option, this,
render_blocking_behavior_);
if (loading_ && !GetResource()) {
// Fetch() synchronous failure case.
// The request may have been denied if (for example) the stylesheet is
// local and the document is remote, or if there was a Content Security
// Policy Failure.
loading_ = false;
RemovePendingSheet();
NotifyLoadedSheetAndAllCriticalSubresources(
Node::kErrorOccurredLoadingSubresource);
}
return kLoaded;
}
void LinkStyle::Process(LinkLoadParameters::Reason reason) {
DCHECK(owner_->ShouldProcessStyle());
const LinkLoadParameters params(
owner_->RelAttribute(),
GetCrossOriginAttributeValue(
owner_->FastGetAttribute(html_names::kCrossoriginAttr)),
owner_->TypeValue().DeprecatedLower(),
owner_->AsValue().DeprecatedLower(), owner_->Media().DeprecatedLower(),
owner_->nonce(), owner_->IntegrityValue(),
owner_->FetchPriorityHintValue().LowerASCII(),
owner_->GetReferrerPolicy(),
owner_->GetNonEmptyURLAttribute(html_names::kHrefAttr),
owner_->FastGetAttribute(html_names::kImagesrcsetAttr),
owner_->FastGetAttribute(html_names::kImagesizesAttr),
owner_->FastGetAttribute(html_names::kBlockingAttr), reason);
WTF::TextEncoding charset = GetCharset();
if (owner_->RelAttribute().GetIconType() !=
mojom::blink::FaviconIconType::kInvalid &&
params.href.IsValid() && !params.href.IsEmpty()) {
if (!owner_->ShouldLoadLink())
return;
if (!GetExecutionContext())
return;
if (!GetExecutionContext()->GetSecurityOrigin()->CanDisplay(params.href))
return;
if (!GetExecutionContext()
->GetContentSecurityPolicy()
->AllowImageFromSource(params.href, params.href,
RedirectStatus::kNoRedirect)) {
return;
}
if (GetDocument().GetFrame())
GetDocument().GetFrame()->UpdateFaviconURL();
}
if (!sheet_ && !owner_->LoadLink(params))
return;
if (LoadStylesheetIfNeeded(params, charset) == kNotNeeded && sheet_) {
// we no longer contain a stylesheet, e.g. perhaps rel or type was changed
ClearSheet();
GetDocument().GetStyleEngine().SetNeedsActiveStyleUpdate(
owner_->GetTreeScope());
}
}
void LinkStyle::SetSheetTitle(const String& title) {
if (!owner_->IsInDocumentTree() || !owner_->RelAttribute().IsStyleSheet())
return;
if (sheet_)
sheet_->SetTitle(title);
if (title.empty() || !IsUnset() || owner_->IsAlternate())
return;
const KURL& href = owner_->GetNonEmptyURLAttribute(html_names::kHrefAttr);
if (href.IsValid() && !href.IsEmpty())
GetDocument().GetStyleEngine().SetPreferredStylesheetSetNameIfNotSet(title);
}
void LinkStyle::OwnerRemoved() {
if (StyleSheetIsLoading() &&
!GetDocument().StatePreservingAtomicMoveInProgress()) {
RemovePendingSheet();
}
if (sheet_)
ClearSheet();
}
void LinkStyle::UnblockRenderingForPendingSheet() {
DCHECK(StyleSheetIsLoading());
if (pending_sheet_type_ == PendingSheetType::kDynamicRenderBlocking) {
GetDocument().GetStyleEngine().RemovePendingBlockingSheet(
*owner_, pending_sheet_type_);
pending_sheet_type_ = PendingSheetType::kNonBlocking;
}
}
void LinkStyle::Trace(Visitor* visitor) const {
visitor->Trace(sheet_);
LinkResource::Trace(visitor);
ResourceClient::Trace(visitor);
}
} // namespace blink
|