StyleRuleImport.cpp [plain text]
#include "config.h"
#include "StyleRuleImport.h"
#include "CSSStyleSheet.h"
#include "CachedCSSStyleSheet.h"
#include "CachedResourceLoader.h"
#include "CachedResourceRequest.h"
#include "CachedResourceRequestInitiators.h"
#include "Document.h"
#include "MediaList.h"
#include "SecurityOrigin.h"
#include "StyleSheetContents.h"
#include <wtf/StdLibExtras.h>
namespace WebCore {
Ref<StyleRuleImport> StyleRuleImport::create(const String& href, Ref<MediaQuerySet>&& media)
{
return adoptRef(*new StyleRuleImport(href, WTFMove(media)));
}
StyleRuleImport::StyleRuleImport(const String& href, Ref<MediaQuerySet>&& media)
: StyleRuleBase(Import, 0)
, m_parentStyleSheet(0)
, m_styleSheetClient(this)
, m_strHref(href)
, m_mediaQueries(WTFMove(media))
, m_cachedSheet(0)
, m_loading(false)
{
if (!m_mediaQueries)
m_mediaQueries = MediaQuerySet::create(String());
}
StyleRuleImport::~StyleRuleImport()
{
if (m_styleSheet)
m_styleSheet->clearOwnerRule();
if (m_cachedSheet)
m_cachedSheet->removeClient(&m_styleSheetClient);
}
void StyleRuleImport::setCSSStyleSheet(const String& href, const URL& baseURL, const String& charset, const CachedCSSStyleSheet* cachedStyleSheet)
{
if (m_styleSheet)
m_styleSheet->clearOwnerRule();
CSSParserContext context = m_parentStyleSheet ? m_parentStyleSheet->parserContext() : CSSStrictMode;
context.charset = charset;
if (!baseURL.isNull())
context.baseURL = baseURL;
Document* document = m_parentStyleSheet ? m_parentStyleSheet->singleOwnerDocument() : nullptr;
m_styleSheet = StyleSheetContents::create(this, href, context);
m_styleSheet->parseAuthorStyleSheet(cachedStyleSheet, document ? document->securityOrigin() : nullptr);
m_loading = false;
if (m_parentStyleSheet) {
m_parentStyleSheet->notifyLoadedSheet(cachedStyleSheet);
m_parentStyleSheet->checkLoaded();
}
}
bool StyleRuleImport::isLoading() const
{
return m_loading || (m_styleSheet && m_styleSheet->isLoading());
}
void StyleRuleImport::requestStyleSheet()
{
if (!m_parentStyleSheet)
return;
Document* document = m_parentStyleSheet->singleOwnerDocument();
if (!document)
return;
URL absURL;
if (!m_parentStyleSheet->baseURL().isNull())
absURL = URL(m_parentStyleSheet->baseURL(), m_strHref);
else
absURL = document->completeURL(m_strHref);
StyleSheetContents* rootSheet = m_parentStyleSheet;
for (StyleSheetContents* sheet = m_parentStyleSheet; sheet; sheet = sheet->parentStyleSheet()) {
if (equalIgnoringFragmentIdentifier(absURL, sheet->baseURL())
|| equalIgnoringFragmentIdentifier(absURL, document->completeURL(sheet->originalURL())))
return;
rootSheet = sheet;
}
CachedResourceRequest request(ResourceRequest(absURL), m_parentStyleSheet->charset());
request.setInitiator(cachedResourceRequestInitiators().css);
if (m_cachedSheet)
m_cachedSheet->removeClient(&m_styleSheetClient);
if (m_parentStyleSheet->isUserStyleSheet())
m_cachedSheet = document->cachedResourceLoader().requestUserCSSStyleSheet(request);
else
m_cachedSheet = document->cachedResourceLoader().requestCSSStyleSheet(request);
if (m_cachedSheet) {
if (m_parentStyleSheet && m_parentStyleSheet->loadCompleted() && rootSheet == m_parentStyleSheet)
m_parentStyleSheet->startLoadingDynamicSheet();
m_loading = true;
m_cachedSheet->addClient(&m_styleSheetClient);
}
}
}