Bug 1570721 - Simplify emulated medium setup in nsPresContext. r=heycam

Differential Revision: https://phabricator.services.mozilla.com/D40246
This commit is contained in:
Emilio Cobos Álvarez 2019-08-01 18:15:04 +02:00
Родитель ef68a372a3
Коммит e288006e4e
4 изменённых файлов: 36 добавлений и 73 удалений

Просмотреть файл

@ -318,6 +318,8 @@ class nsDocumentViewer final : public nsIContentViewer,
// nsIDocumentViewerPrint Printing Methods
NS_DECL_NSIDOCUMENTVIEWERPRINT
void EmulateMediumInternal(nsAtom*);
protected:
virtual ~nsDocumentViewer();
@ -2957,61 +2959,42 @@ nsDocumentViewer::GetAuthorStyleDisabled(bool* aStyleDisabled) {
}
static bool ExtResourceEmulateMedium(Document* aDocument, void* aClosure) {
nsPresContext* ctxt = aDocument->GetPresContext();
if (ctxt) {
const nsAString* mediaType = static_cast<nsAString*>(aClosure);
ctxt->EmulateMedium(*mediaType);
if (nsPresContext* pc = aDocument->GetPresContext()) {
pc->EmulateMedium(static_cast<nsAtom*>(aClosure));
}
return true;
}
static void ChildEmulateMedium(nsDocumentViewer* aChild, void* aClosure) {
const nsAString* mediaType = static_cast<nsAString*>(aClosure);
aChild->EmulateMedium(*mediaType);
aChild->EmulateMediumInternal(static_cast<nsAtom*>(aClosure));
}
void nsDocumentViewer::EmulateMediumInternal(nsAtom* aMedia) {
if (mPresContext) {
mPresContext->EmulateMedium(aMedia);
}
CallChildren(ChildEmulateMedium, aMedia);
if (mDocument) {
mDocument->EnumerateExternalResources(ExtResourceEmulateMedium, aMedia);
}
}
NS_IMETHODIMP
nsDocumentViewer::EmulateMedium(const nsAString& aMediaType) {
if (mPresContext) {
mPresContext->EmulateMedium(aMediaType);
}
CallChildren(ChildEmulateMedium, const_cast<nsAString*>(&aMediaType));
if (mDocument) {
mDocument->EnumerateExternalResources(ExtResourceEmulateMedium,
const_cast<nsAString*>(&aMediaType));
}
nsAutoString mediaType;
nsContentUtils::ASCIIToLower(aMediaType, mediaType);
RefPtr<nsAtom> media = NS_Atomize(mediaType);
EmulateMediumInternal(media);
return NS_OK;
}
static bool ExtResourceStopEmulatingMedium(Document* aDocument,
void* aClosure) {
nsPresContext* ctxt = aDocument->GetPresContext();
if (ctxt) {
ctxt->StopEmulatingMedium();
}
return true;
}
static void ChildStopEmulatingMedium(nsIContentViewer* aChild, void* aClosure) {
aChild->StopEmulatingMedium();
}
NS_IMETHODIMP
nsDocumentViewer::StopEmulatingMedium() {
if (mPresContext) {
mPresContext->StopEmulatingMedium();
}
CallChildren(ChildStopEmulatingMedium, nullptr);
if (mDocument) {
mDocument->EnumerateExternalResources(ExtResourceStopEmulatingMedium,
nullptr);
}
EmulateMediumInternal(nullptr);
return NS_OK;
}

Просмотреть файл

@ -144,7 +144,6 @@ nsPresContext::nsPresContext(dom::Document* aDocument, nsPresContextType aType)
mPresShell(nullptr),
mDocument(aDocument),
mMedium(aType == eContext_Galley ? nsGkAtoms::screen : nsGkAtoms::print),
mMediaEmulated(mMedium),
mInflationDisabledForShrinkWrap(false),
mSystemFontScale(1.0),
mTextZoom(1.0),
@ -186,7 +185,6 @@ nsPresContext::nsPresContext(dom::Document* aDocument, nsPresContextType aType)
mPendingUIResolutionChanged(false),
mPrefChangePendingNeedsReflow(false),
mPostedPrefChangedRunnable(false),
mIsEmulatingMedia(false),
mIsGlyph(false),
mUsesRootEMUnits(false),
mUsesExChUnits(false),
@ -1423,27 +1421,15 @@ void nsPresContext::UIResolutionChangedInternalScale(double aScale) {
&aScale);
}
void nsPresContext::EmulateMedium(const nsAString& aMediaType) {
nsAtom* previousMedium = Medium();
mIsEmulatingMedia = true;
nsAutoString mediaType;
nsContentUtils::ASCIIToLower(aMediaType, mediaType);
mMediaEmulated = NS_Atomize(mediaType);
void nsPresContext::EmulateMedium(nsAtom* aMediaType) {
MOZ_ASSERT(!aMediaType || aMediaType->IsAsciiLowercase());
RefPtr<const nsAtom> previousMedium = Medium();
mMediaEmulated = aMediaType;
if (mMediaEmulated != previousMedium && mPresShell) {
MediaFeatureValuesChanged({MediaFeatureChangeReason::MediumChange});
}
}
void nsPresContext::StopEmulatingMedium() {
nsAtom* previousMedium = Medium();
mIsEmulatingMedia = false;
if (Medium() != previousMedium) {
MediaFeatureValuesChanged({MediaFeatureChangeReason::MediumChange});
}
}
void nsPresContext::ContentLanguageChanged() {
PostRebuildAllStyleDataEvent(nsChangeHint(0),
RestyleHint::RecascadeSubtree());

Просмотреть файл

@ -303,21 +303,18 @@ class nsPresContext : public nsISupports,
/**
* Get medium of presentation
*/
nsAtom* Medium() {
if (!mIsEmulatingMedia) return mMedium;
return mMediaEmulated;
const nsAtom* Medium() {
MOZ_ASSERT(mMedium);
return mMediaEmulated ? mMediaEmulated.get() : mMedium;
}
/*
* Render the document as if being viewed on a device with the specified
* media type.
*
* If passed null, it stops emulating.
*/
void EmulateMedium(const nsAString& aMediaType);
/*
* Restore the viewer's natural medium
*/
void StopEmulatingMedium();
void EmulateMedium(nsAtom* aMediaType);
/** Get a cached integer pref, by its type */
// * - initially created for bugs 30910, 61883, 74186, 84398
@ -1119,8 +1116,7 @@ class nsPresContext : public nsISupports,
mozilla::UniquePtr<nsAnimationManager> mAnimationManager;
mozilla::UniquePtr<mozilla::RestyleManager> mRestyleManager;
RefPtr<mozilla::CounterStyleManager> mCounterStyleManager;
nsAtom* MOZ_UNSAFE_REF(
"always a static atom") mMedium; // initialized by subclass ctors
const nsStaticAtom* mMedium;
RefPtr<nsAtom> mMediaEmulated;
RefPtr<gfxFontFeatureValueSet> mFontFeatureValuesLookup;
@ -1221,7 +1217,6 @@ class nsPresContext : public nsISupports,
unsigned mPendingUIResolutionChanged : 1;
unsigned mPrefChangePendingNeedsReflow : 1;
unsigned mPostedPrefChangedRunnable : 1;
unsigned mIsEmulatingMedia : 1;
// Are we currently drawing an SVG glyph?
unsigned mIsGlyph : 1;

Просмотреть файл

@ -213,12 +213,11 @@ impl Device {
None => return MediaType::screen(),
};
// Gecko allows emulating random media with mIsEmulatingMedia and
// mMediaEmulated.
let medium_to_use = if pc.mIsEmulatingMedia() != 0 {
// Gecko allows emulating random media with mMediaEmulated.
let medium_to_use = if !pc.mMediaEmulated.mRawPtr.is_null() {
pc.mMediaEmulated.mRawPtr
} else {
pc.mMedium
pc.mMedium as *const bindings::nsAtom as *mut _
};
MediaType(CustomIdent(unsafe { Atom::from_raw(medium_to_use) }))