2016-02-26 04:51:01 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2016-03-02 01:38:13 +03:00
|
|
|
#include "mozilla/StyleSheet.h"
|
|
|
|
|
2016-10-14 14:25:38 +03:00
|
|
|
#include "mozilla/dom/CSSRuleList.h"
|
2016-03-02 02:10:45 +03:00
|
|
|
#include "mozilla/dom/ShadowRoot.h"
|
2016-03-02 01:38:13 +03:00
|
|
|
#include "mozilla/ServoStyleSheet.h"
|
|
|
|
#include "mozilla/StyleSheetInlines.h"
|
|
|
|
#include "mozilla/CSSStyleSheet.h"
|
2016-02-26 04:51:01 +03:00
|
|
|
|
2016-11-23 02:26:20 +03:00
|
|
|
#include "mozAutoDocUpdate.h"
|
2016-12-16 06:50:36 +03:00
|
|
|
#include "nsMediaList.h"
|
2016-09-29 09:19:06 +03:00
|
|
|
#include "nsNullPrincipal.h"
|
|
|
|
|
2016-02-26 04:51:01 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
2016-08-02 23:12:27 +03:00
|
|
|
StyleSheet::StyleSheet(StyleBackendType aType, css::SheetParsingMode aParsingMode)
|
2016-03-01 23:39:29 +03:00
|
|
|
: mDocument(nullptr)
|
|
|
|
, mOwningNode(nullptr)
|
2016-08-02 23:12:27 +03:00
|
|
|
, mParsingMode(aParsingMode)
|
2016-03-02 01:38:13 +03:00
|
|
|
, mType(aType)
|
2016-02-26 04:51:01 +03:00
|
|
|
, mDisabled(false)
|
2017-01-20 07:49:44 +03:00
|
|
|
, mDocumentAssociationMode(NotOwnedByDocument)
|
2016-02-26 04:51:01 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
StyleSheet::StyleSheet(const StyleSheet& aCopy,
|
2016-03-01 23:39:29 +03:00
|
|
|
nsIDocument* aDocumentToUse,
|
2016-02-26 04:51:01 +03:00
|
|
|
nsINode* aOwningNodeToUse)
|
2016-10-14 14:25:38 +03:00
|
|
|
: mTitle(aCopy.mTitle)
|
|
|
|
, mDocument(aDocumentToUse)
|
2016-03-01 23:39:29 +03:00
|
|
|
, mOwningNode(aOwningNodeToUse)
|
2016-02-26 04:51:01 +03:00
|
|
|
, mParsingMode(aCopy.mParsingMode)
|
2016-03-02 01:38:13 +03:00
|
|
|
, mType(aCopy.mType)
|
2016-02-26 04:51:01 +03:00
|
|
|
, mDisabled(aCopy.mDisabled)
|
2017-01-20 07:49:44 +03:00
|
|
|
// We only use this constructor during cloning. It's the cloner's
|
|
|
|
// responsibility to notify us if we end up being owned by a document.
|
|
|
|
, mDocumentAssociationMode(NotOwnedByDocument)
|
2016-02-26 04:51:01 +03:00
|
|
|
{
|
2017-01-06 10:05:24 +03:00
|
|
|
if (aCopy.mMedia) {
|
|
|
|
// XXX This is wrong; we should be keeping @import rules and
|
|
|
|
// sheets in sync!
|
|
|
|
mMedia = aCopy.mMedia->Clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StyleSheet::~StyleSheet()
|
|
|
|
{
|
|
|
|
DropMedia();
|
2016-02-26 04:51:01 +03:00
|
|
|
}
|
|
|
|
|
2016-10-14 14:25:38 +03:00
|
|
|
// QueryInterface implementation for StyleSheet
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(StyleSheet)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMStyleSheet)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMCSSStyleSheet)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(StyleSheet)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(StyleSheet)
|
|
|
|
|
2017-01-06 10:05:24 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(StyleSheet)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(StyleSheet)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMedia)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(StyleSheet)
|
|
|
|
tmp->DropMedia();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(StyleSheet)
|
2016-10-14 14:25:38 +03:00
|
|
|
|
2016-08-02 23:17:06 +03:00
|
|
|
mozilla::dom::CSSStyleSheetParsingMode
|
|
|
|
StyleSheet::ParsingModeDOM()
|
|
|
|
{
|
|
|
|
#define CHECK(X, Y) \
|
|
|
|
static_assert(static_cast<int>(X) == static_cast<int>(Y), \
|
|
|
|
"mozilla::dom::CSSStyleSheetParsingMode and mozilla::css::SheetParsingMode should have identical values");
|
|
|
|
|
|
|
|
CHECK(mozilla::dom::CSSStyleSheetParsingMode::Agent, css::eAgentSheetFeatures);
|
|
|
|
CHECK(mozilla::dom::CSSStyleSheetParsingMode::User, css::eUserSheetFeatures);
|
|
|
|
CHECK(mozilla::dom::CSSStyleSheetParsingMode::Author, css::eAuthorSheetFeatures);
|
|
|
|
|
|
|
|
#undef CHECK
|
|
|
|
|
|
|
|
return static_cast<mozilla::dom::CSSStyleSheetParsingMode>(mParsingMode);
|
|
|
|
}
|
|
|
|
|
2016-03-02 02:10:45 +03:00
|
|
|
bool
|
|
|
|
StyleSheet::IsComplete() const
|
|
|
|
{
|
|
|
|
return SheetInfo().mComplete;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StyleSheet::SetComplete()
|
|
|
|
{
|
2016-09-26 15:03:25 +03:00
|
|
|
NS_ASSERTION(!IsGecko() || !AsGecko()->mDirty,
|
|
|
|
"Can't set a dirty sheet complete!");
|
2016-03-02 02:10:45 +03:00
|
|
|
SheetInfo().mComplete = true;
|
|
|
|
if (mDocument && !mDisabled) {
|
|
|
|
// Let the document know
|
|
|
|
mDocument->BeginUpdate(UPDATE_STYLE);
|
2016-09-26 15:03:25 +03:00
|
|
|
mDocument->SetStyleSheetApplicableState(this, true);
|
2016-03-02 02:10:45 +03:00
|
|
|
mDocument->EndUpdate(UPDATE_STYLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOwningNode && !mDisabled &&
|
|
|
|
mOwningNode->HasFlag(NODE_IS_IN_SHADOW_TREE) &&
|
|
|
|
mOwningNode->IsContent()) {
|
2016-07-08 00:05:28 +03:00
|
|
|
dom::ShadowRoot* shadowRoot = mOwningNode->AsContent()->GetContainingShadow();
|
2016-03-02 02:10:45 +03:00
|
|
|
shadowRoot->StyleSheetChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-06 10:05:24 +03:00
|
|
|
void
|
|
|
|
StyleSheet::SetEnabled(bool aEnabled)
|
|
|
|
{
|
|
|
|
// Internal method, so callers must handle BeginUpdate/EndUpdate
|
|
|
|
bool oldDisabled = mDisabled;
|
|
|
|
mDisabled = !aEnabled;
|
|
|
|
|
|
|
|
if (IsComplete() && oldDisabled != mDisabled) {
|
|
|
|
EnabledStateChanged();
|
|
|
|
|
|
|
|
if (mDocument) {
|
|
|
|
mDocument->SetStyleSheetApplicableState(this, !mDisabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 09:19:06 +03:00
|
|
|
StyleSheetInfo::StyleSheetInfo(CORSMode aCORSMode,
|
|
|
|
ReferrerPolicy aReferrerPolicy,
|
|
|
|
const dom::SRIMetadata& aIntegrity)
|
|
|
|
: mPrincipal(nsNullPrincipal::Create())
|
|
|
|
, mCORSMode(aCORSMode)
|
|
|
|
, mReferrerPolicy(aReferrerPolicy)
|
|
|
|
, mIntegrity(aIntegrity)
|
|
|
|
, mComplete(false)
|
|
|
|
#ifdef DEBUG
|
|
|
|
, mPrincipalSet(false)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (!mPrincipal) {
|
|
|
|
NS_RUNTIMEABORT("nsNullPrincipal::Init failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 14:25:38 +03:00
|
|
|
// nsIDOMStyleSheet interface
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StyleSheet::GetType(nsAString& aType)
|
|
|
|
{
|
|
|
|
aType.AssignLiteral("text/css");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StyleSheet::GetDisabled(bool* aDisabled)
|
|
|
|
{
|
|
|
|
*aDisabled = Disabled();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StyleSheet::SetDisabled(bool aDisabled)
|
|
|
|
{
|
|
|
|
// DOM method, so handle BeginUpdate/EndUpdate
|
|
|
|
MOZ_AUTO_DOC_UPDATE(mDocument, UPDATE_STYLE, true);
|
2017-01-09 12:44:30 +03:00
|
|
|
SetEnabled(!aDisabled);
|
2016-10-14 14:25:38 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StyleSheet::GetOwnerNode(nsIDOMNode** aOwnerNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> ownerNode = do_QueryInterface(GetOwnerNode());
|
|
|
|
ownerNode.forget(aOwnerNode);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StyleSheet::GetHref(nsAString& aHref)
|
|
|
|
{
|
|
|
|
if (nsIURI* sheetURI = SheetInfo().mOriginalSheetURI) {
|
|
|
|
nsAutoCString str;
|
|
|
|
nsresult rv = sheetURI->GetSpec(str);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
CopyUTF8toUTF16(str, aHref);
|
|
|
|
} else {
|
|
|
|
SetDOMStringToNull(aHref);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StyleSheet::GetTitle(nsAString& aTitle)
|
|
|
|
{
|
|
|
|
aTitle.Assign(mTitle);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-10-14 14:25:38 +03:00
|
|
|
// nsIDOMStyleSheet interface
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StyleSheet::GetParentStyleSheet(nsIDOMStyleSheet** aParentStyleSheet)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aParentStyleSheet);
|
|
|
|
NS_IF_ADDREF(*aParentStyleSheet = GetParentStyleSheet());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StyleSheet::GetMedia(nsIDOMMediaList** aMedia)
|
|
|
|
{
|
|
|
|
NS_ADDREF(*aMedia = Media());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StyleSheet::GetOwnerRule(nsIDOMCSSRule** aOwnerRule)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aOwnerRule = GetDOMOwnerRule());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StyleSheet::GetCssRules(nsIDOMCSSRuleList** aCssRules)
|
|
|
|
{
|
|
|
|
ErrorResult rv;
|
|
|
|
nsCOMPtr<nsIDOMCSSRuleList> rules =
|
|
|
|
GetCssRules(*nsContentUtils::SubjectPrincipal(), rv);
|
|
|
|
rules.forget(aCssRules);
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StyleSheet::InsertRule(const nsAString& aRule, uint32_t aIndex,
|
|
|
|
uint32_t* aReturn)
|
|
|
|
{
|
|
|
|
ErrorResult rv;
|
|
|
|
*aReturn =
|
|
|
|
InsertRule(aRule, aIndex, *nsContentUtils::SubjectPrincipal(), rv);
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StyleSheet::DeleteRule(uint32_t aIndex)
|
|
|
|
{
|
|
|
|
ErrorResult rv;
|
|
|
|
DeleteRule(aIndex, *nsContentUtils::SubjectPrincipal(), rv);
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
|
|
|
|
2016-10-14 14:25:38 +03:00
|
|
|
// WebIDL CSSStyleSheet API
|
|
|
|
|
|
|
|
#define FORWARD_INTERNAL(method_, args_) \
|
|
|
|
if (IsServo()) { \
|
|
|
|
return AsServo()->method_ args_; \
|
|
|
|
} \
|
|
|
|
return AsGecko()->method_ args_;
|
|
|
|
|
|
|
|
dom::CSSRuleList*
|
|
|
|
StyleSheet::GetCssRules(nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (!AreRulesAvailable(aSubjectPrincipal, aRv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
FORWARD_INTERNAL(GetCssRulesInternal, (aRv))
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
StyleSheet::InsertRule(const nsAString& aRule, uint32_t aIndex,
|
|
|
|
nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (!AreRulesAvailable(aSubjectPrincipal, aRv)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
FORWARD_INTERNAL(InsertRuleInternal, (aRule, aIndex, aRv))
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StyleSheet::DeleteRule(uint32_t aIndex,
|
|
|
|
nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (!AreRulesAvailable(aSubjectPrincipal, aRv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FORWARD_INTERNAL(DeleteRuleInternal, (aIndex, aRv))
|
|
|
|
}
|
|
|
|
|
2017-01-06 10:05:24 +03:00
|
|
|
void
|
|
|
|
StyleSheet::EnabledStateChanged()
|
|
|
|
{
|
|
|
|
FORWARD_INTERNAL(EnabledStateChangedInternal, ())
|
|
|
|
}
|
|
|
|
|
2016-10-14 14:25:38 +03:00
|
|
|
#undef FORWARD_INTERNAL
|
|
|
|
|
2016-10-14 14:25:38 +03:00
|
|
|
void
|
|
|
|
StyleSheet::SubjectSubsumesInnerPrincipal(nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
StyleSheetInfo& info = SheetInfo();
|
|
|
|
|
|
|
|
if (aSubjectPrincipal.Subsumes(info.mPrincipal)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow access only if CORS mode is not NONE
|
|
|
|
if (GetCORSMode() == CORS_NONE) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now make sure we set the principal of our inner to the subjectPrincipal.
|
|
|
|
// We do this because we're in a situation where the caller would not normally
|
|
|
|
// be able to access the sheet, but the sheet has opted in to being read.
|
|
|
|
// Unfortunately, that means it's also opted in to being _edited_, and if the
|
|
|
|
// caller now makes edits to the sheet we want the resulting resource loads,
|
|
|
|
// if any, to look as if they are coming from the caller's principal, not the
|
|
|
|
// original sheet principal.
|
|
|
|
//
|
|
|
|
// That means we need a unique inner, of course. But we don't want to do that
|
|
|
|
// if we're not complete yet. Luckily, all the callers of this method throw
|
|
|
|
// anyway if not complete, so we can just do that here too.
|
|
|
|
if (!info.mComplete) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WillDirty();
|
|
|
|
|
|
|
|
info.mPrincipal = &aSubjectPrincipal;
|
|
|
|
|
|
|
|
DidDirty();
|
|
|
|
}
|
|
|
|
|
2016-10-14 14:25:38 +03:00
|
|
|
bool
|
2016-10-14 14:25:38 +03:00
|
|
|
StyleSheet::AreRulesAvailable(nsIPrincipal& aSubjectPrincipal,
|
2016-10-14 14:25:38 +03:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
// Rules are not available on incomplete sheets.
|
|
|
|
if (!SheetInfo().mComplete) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//-- Security check: Only scripts whose principal subsumes that of the
|
|
|
|
// style sheet can access rule collections.
|
|
|
|
SubjectSubsumesInnerPrincipal(aSubjectPrincipal, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-06 10:05:24 +03:00
|
|
|
void
|
|
|
|
StyleSheet::SetMedia(nsMediaList* aMedia)
|
|
|
|
{
|
|
|
|
mMedia = aMedia;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StyleSheet::DropMedia()
|
|
|
|
{
|
|
|
|
if (mMedia) {
|
|
|
|
mMedia->SetStyleSheet(nullptr);
|
|
|
|
mMedia = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsMediaList*
|
|
|
|
StyleSheet::Media()
|
|
|
|
{
|
|
|
|
if (!mMedia) {
|
|
|
|
mMedia = new nsMediaList();
|
|
|
|
mMedia->SetStyleSheet(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mMedia;
|
|
|
|
}
|
|
|
|
|
2016-10-14 14:25:38 +03:00
|
|
|
// nsWrapperCache
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
StyleSheet::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
|
|
{
|
2016-11-23 02:26:20 +03:00
|
|
|
return dom::CSSStyleSheetBinding::Wrap(aCx, this, aGivenProto);
|
2016-10-14 14:25:38 +03:00
|
|
|
}
|
|
|
|
|
2016-02-26 04:51:01 +03:00
|
|
|
} // namespace mozilla
|