Bug 1325878: Create less hardcoded nsMediaList instances. r=xidorn

MozReview-Commit-ID: K6T3MM1ZrFb
Signed-off-by: Emilio Cobos Álvarez <emilio@crisal.io>
This commit is contained in:
Emilio Cobos Álvarez 2017-04-10 10:25:33 +08:00
parent 3cb9f7d4fb
commit 16cd642c17
13 changed files with 44 additions and 26 deletions

View File

@ -9,11 +9,11 @@
#include "mozilla/dom/HTMLImageElement.h"
#include "mozilla/dom/ResponsiveImageSelector.h"
#include "mozilla/dom/MediaList.h"
#include "mozilla/dom/MediaSource.h"
#include "nsGkAtoms.h"
#include "nsMediaList.h"
#include "nsCSSParser.h"
#include "nsHostObjectProtocolHandler.h"
@ -57,7 +57,7 @@ HTMLSourceElement::MatchesCurrentMedia()
if (mMediaList) {
nsIPresShell* presShell = OwnerDoc()->GetShell();
nsPresContext* pctx = presShell ? presShell->GetPresContext() : nullptr;
return pctx && mMediaList->Matches(pctx, nullptr);
return pctx && mMediaList->Matches(*pctx);
}
// No media specified
@ -75,11 +75,8 @@ HTMLSourceElement::WouldMatchMediaForDocument(const nsAString& aMedia,
nsIPresShell* presShell = aDocument->GetShell();
nsPresContext* pctx = presShell ? presShell->GetPresContext() : nullptr;
nsCSSParser cssParser;
RefPtr<nsMediaList> mediaList = new nsMediaList();
cssParser.ParseMediaList(aMedia, nullptr, 0, mediaList);
return pctx && mediaList->Matches(pctx, nullptr);
RefPtr<MediaList> mediaList = MediaList::Create(*aDocument, aMedia);
return pctx && mediaList->Matches(*pctx, nullptr);
}
void
@ -92,8 +89,7 @@ HTMLSourceElement::UpdateMediaList(const nsAttrValue* aValue)
}
nsCSSParser cssParser;
mMediaList = new nsMediaList();
cssParser.ParseMediaList(mediaStr, nullptr, 0, mMediaList);
mMediaList = MediaList::Create(*OwnerDoc(), mediaStr);
}
nsresult

View File

@ -12,12 +12,13 @@
#include "nsGenericHTMLElement.h"
#include "mozilla/dom/HTMLMediaElement.h"
class nsMediaList;
class nsAttrValue;
namespace mozilla {
namespace dom {
class MediaList;
class HTMLSourceElement final : public nsGenericHTMLElement,
public nsIDOMHTMLSourceElement
{
@ -113,10 +114,10 @@ protected:
bool aNotify) override;
private:
RefPtr<nsMediaList> mMediaList;
RefPtr<MediaList> mMediaList;
RefPtr<MediaSource> mSrcMediaSource;
// Generates a new nsMediaList using the given input
// Generates a new MediaList using the given input
void UpdateMediaList(const nsAttrValue* aValue);
};

View File

@ -513,8 +513,9 @@ CSSStyleSheet::UseForPresentation(nsPresContext* aPresContext,
nsMediaQueryResultCacheKey& aKey) const
{
if (mMedia) {
MOZ_ASSERT(aPresContext);
auto media = static_cast<nsMediaList*>(mMedia.get());
return media->Matches(aPresContext, &aKey);
return media->Matches(*aPresContext, &aKey);
}
return true;
}

View File

@ -72,10 +72,10 @@ MediaList::DoMediaChange(Func aCallback)
}
/* static */ already_AddRefed<MediaList>
MediaList::Create(nsIDocument* aDocument, const nsAString& aMedia)
MediaList::Create(const nsIDocument& aDocument,
const nsAString& aMedia)
{
MOZ_ASSERT(aDocument);
if (aDocument->IsStyledByServo()) {
if (aDocument.IsStyledByServo()) {
RefPtr<ServoMediaList> mediaList = new ServoMediaList(aMedia);
return mediaList.forget();
}

View File

@ -16,6 +16,8 @@
#include "nsWrapperCache.h"
class nsIDocument;
class nsPresContext;
class nsMediaQueryResultCacheKey;
namespace mozilla {
class StyleSheet;
@ -40,7 +42,7 @@ public:
/**
* Creates a MediaList backed by the StyleBackendType of the document.
*/
static already_AddRefed<MediaList> Create(nsIDocument* aDocument,
static already_AddRefed<MediaList> Create(const nsIDocument& aDocument,
const nsAString& aMedia);
virtual already_AddRefed<MediaList> Clone() = 0;
@ -50,6 +52,8 @@ public:
virtual void GetText(nsAString& aMediaText) = 0;
virtual void SetText(const nsAString& aMediaText) = 0;
virtual bool Matches(nsPresContext& aPresContext,
nsMediaQueryResultCacheKey* = nullptr) const = 0;
void SetStyleSheet(StyleSheet* aSheet);

View File

@ -234,7 +234,7 @@ MediaQueryList::RecomputeMatches()
return;
}
mMatches = mMediaList->Matches(presContext, nullptr);
mMatches = mMediaList->Matches(*presContext, nullptr);
mMatchesValid = true;
}

View File

@ -285,6 +285,9 @@ SERVO_BINDING_FUNC(Servo_DeclarationBlock_SetBackgroundImage, void,
// MediaList
SERVO_BINDING_FUNC(Servo_MediaList_Create, RawServoMediaListStrong)
SERVO_BINDING_FUNC(Servo_MediaList_Matches, bool,
RawServoMediaListBorrowed list,
RawServoStyleSetBorrowed set)
SERVO_BINDING_FUNC(Servo_MediaList_GetText, void,
RawServoMediaListBorrowed list, nsAString* result)
SERVO_BINDING_FUNC(Servo_MediaList_SetText, void,

View File

@ -9,6 +9,7 @@
#include "mozilla/ServoMediaList.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/ServoStyleSet.h"
namespace mozilla {
@ -79,4 +80,12 @@ ServoMediaList::Delete(const nsAString& aOldMedium)
return NS_ERROR_DOM_NOT_FOUND_ERR;
}
bool
ServoMediaList::Matches(nsPresContext& aPresContext,
nsMediaQueryResultCacheKey*) const
{
const RawServoStyleSet& rawSet = aPresContext.StyleSet()->AsServo()->RawSet();
return Servo_MediaList_Matches(mRawList, &rawSet);
}
} // namespace mozilla

View File

@ -31,6 +31,8 @@ public:
void IndexedGetter(uint32_t aIndex, bool& aFound,
nsAString& aReturn) final;
bool Matches(nsPresContext&, nsMediaQueryResultCacheKey*) const final;
protected:
nsresult Delete(const nsAString& aOldMedium) final;
nsresult Append(const nsAString& aNewMedium) final;

View File

@ -93,6 +93,7 @@ public:
void Shutdown();
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
const RawServoStyleSet& RawSet() const { return *mRawSet; }
bool GetAuthorStyleDisabled() const;
nsresult SetAuthorStyleDisabled(bool aStyleDisabled);

View File

@ -494,10 +494,11 @@ MediaRule::SetConditionText(const nsAString& aConditionText)
// GroupRule interface
/* virtual */ bool
MediaRule::UseForPresentation(nsPresContext* aPresContext,
nsMediaQueryResultCacheKey& aKey)
nsMediaQueryResultCacheKey& aKey)
{
if (mMedia) {
return mMedia->Matches(aPresContext, &aKey);
MOZ_ASSERT(aPresContext);
return mMedia->Matches(*aPresContext, &aKey);
}
return true;
}

View File

@ -525,11 +525,11 @@ nsMediaList::SetText(const nsAString& aMediaText)
}
bool
nsMediaList::Matches(nsPresContext* aPresContext,
nsMediaQueryResultCacheKey* aKey)
nsMediaList::Matches(nsPresContext& aPresContext,
nsMediaQueryResultCacheKey* aKey) const
{
for (int32_t i = 0, i_end = mArray.Length(); i < i_end; ++i) {
if (mArray[i]->Matches(aPresContext, aKey)) {
if (mArray[i]->Matches(&aPresContext, aKey)) {
return true;
}
}

View File

@ -236,7 +236,7 @@ public:
// Does this query apply to the presentation?
// If |aKey| is non-null, add cache information to it.
bool Matches(nsPresContext* aPresContext,
nsMediaQueryResultCacheKey* aKey) const;
nsMediaQueryResultCacheKey* aKey) const;
private:
bool mNegated;
@ -257,8 +257,8 @@ public:
// Does this query apply to the presentation?
// If |aKey| is non-null, add cache information to it.
bool Matches(nsPresContext* aPresContext,
nsMediaQueryResultCacheKey* aKey);
bool Matches(nsPresContext& aPresContext,
nsMediaQueryResultCacheKey* aKey) const final;
void AppendQuery(nsAutoPtr<nsMediaQuery>& aQuery) {
// Takes ownership of aQuery