LCOV - code coverage report
Current view: top level - layout/base - nsPresContext.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 801 1540 52.0 %
Date: 2017-07-14 16:53:18 Functions: 104 171 60.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2             : /* This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
       5             : 
       6             : /* a presentation of a document, part 1 */
       7             : 
       8             : #include "mozilla/ArrayUtils.h"
       9             : #include "mozilla/DebugOnly.h"
      10             : #include "mozilla/Encoding.h"
      11             : #include "mozilla/EventDispatcher.h"
      12             : #include "mozilla/EventStateManager.h"
      13             : 
      14             : #include "base/basictypes.h"
      15             : 
      16             : #include "nsCOMPtr.h"
      17             : #include "nsPresContext.h"
      18             : #include "nsIPresShell.h"
      19             : #include "nsIPresShellInlines.h"
      20             : #include "nsDocShell.h"
      21             : #include "nsIContentViewer.h"
      22             : #include "nsPIDOMWindow.h"
      23             : #include "mozilla/StyleSetHandle.h"
      24             : #include "mozilla/StyleSetHandleInlines.h"
      25             : #include "nsIContent.h"
      26             : #include "nsIFrame.h"
      27             : #include "nsIDocument.h"
      28             : #include "nsIPrintSettings.h"
      29             : #include "nsLanguageAtomService.h"
      30             : #include "mozilla/LookAndFeel.h"
      31             : #include "nsIInterfaceRequestorUtils.h"
      32             : #include "nsIDOMHTMLDocument.h"
      33             : #include "nsIDOMHTMLElement.h"
      34             : #include "nsIWeakReferenceUtils.h"
      35             : #include "nsThreadUtils.h"
      36             : #include "nsFrameManager.h"
      37             : #include "nsLayoutUtils.h"
      38             : #include "nsViewManager.h"
      39             : #include "mozilla/GeckoRestyleManager.h"
      40             : #include "mozilla/RestyleManager.h"
      41             : #include "mozilla/RestyleManagerInlines.h"
      42             : #include "SurfaceCacheUtils.h"
      43             : #include "nsCSSRuleProcessor.h"
      44             : #include "nsRuleNode.h"
      45             : #include "gfxPlatform.h"
      46             : #include "nsCSSRules.h"
      47             : #include "nsFontFaceLoader.h"
      48             : #include "mozilla/EffectCompositor.h"
      49             : #include "mozilla/EventListenerManager.h"
      50             : #include "prenv.h"
      51             : #include "nsPluginFrame.h"
      52             : #include "nsTransitionManager.h"
      53             : #include "nsAnimationManager.h"
      54             : #include "CounterStyleManager.h"
      55             : #include "mozilla/MemoryReporting.h"
      56             : #include "mozilla/dom/Element.h"
      57             : #include "nsIMessageManager.h"
      58             : #include "mozilla/dom/MediaQueryList.h"
      59             : #include "nsSMILAnimationController.h"
      60             : #include "mozilla/css/ImageLoader.h"
      61             : #include "mozilla/dom/PBrowserParent.h"
      62             : #include "mozilla/dom/TabChild.h"
      63             : #include "mozilla/dom/TabParent.h"
      64             : #include "nsRefreshDriver.h"
      65             : #include "Layers.h"
      66             : #include "LayerUserData.h"
      67             : #include "ClientLayerManager.h"
      68             : #include "mozilla/dom/NotifyPaintEvent.h"
      69             : #include "gfxPrefs.h"
      70             : #include "nsIDOMChromeWindow.h"
      71             : #include "nsFrameLoader.h"
      72             : #include "mozilla/dom/FontFaceSet.h"
      73             : #include "nsContentUtils.h"
      74             : #include "nsPIWindowRoot.h"
      75             : #include "mozilla/Preferences.h"
      76             : #include "gfxTextRun.h"
      77             : #include "nsFontFaceUtils.h"
      78             : #include "nsLayoutStylesheetCache.h"
      79             : #include "mozilla/ServoBindings.h"
      80             : #include "mozilla/StyleSheet.h"
      81             : #include "mozilla/StyleSheetInlines.h"
      82             : #include "mozilla/Telemetry.h"
      83             : #include "mozilla/dom/Performance.h"
      84             : #include "mozilla/dom/PerformanceTiming.h"
      85             : #include "mozilla/layers/APZThreadUtils.h"
      86             : 
      87             : // Needed for Start/Stop of Image Animation
      88             : #include "imgIContainer.h"
      89             : #include "nsIImageLoadingContent.h"
      90             : 
      91             : #include "nsCSSParser.h"
      92             : #include "nsBidiUtils.h"
      93             : #include "nsServiceManagerUtils.h"
      94             : #include "nsBidi.h"
      95             : 
      96             : #include "mozilla/dom/URL.h"
      97             : 
      98             : using namespace mozilla;
      99             : using namespace mozilla::dom;
     100             : using namespace mozilla::layers;
     101             : 
     102             : uint8_t gNotifySubDocInvalidationData;
     103             : 
     104             : // This preference was first introduced in Bug 232227, in order to prevent
     105             : // system colors from being exposed to CSS or canvas.
     106             : constexpr char kUseStandinsForNativeColors[] = "ui.use_standins_for_native_colors";
     107             : 
     108             : /**
     109             :  * Layer UserData for ContainerLayers that want to be notified
     110             :  * of local invalidations of them and their descendant layers.
     111             :  * Pass a callback to ComputeDifferences to have these called.
     112             :  */
     113           0 : class ContainerLayerPresContext : public LayerUserData {
     114             : public:
     115             :   nsPresContext* mPresContext;
     116             : };
     117             : 
     118             : namespace {
     119             : 
     120           6 : class CharSetChangingRunnable : public Runnable
     121             : {
     122             : public:
     123           2 :   CharSetChangingRunnable(nsPresContext* aPresContext,
     124             :                           NotNull<const Encoding*> aCharSet)
     125           2 :     : Runnable("CharSetChangingRunnable"),
     126             :       mPresContext(aPresContext),
     127           2 :       mCharSet(aCharSet)
     128             :   {
     129           2 :   }
     130             : 
     131           2 :   NS_IMETHOD Run() override
     132             :   {
     133           2 :     mPresContext->DoChangeCharSet(mCharSet);
     134           2 :     return NS_OK;
     135             :   }
     136             : 
     137             : private:
     138             :   RefPtr<nsPresContext> mPresContext;
     139             :   NotNull<const Encoding*> mCharSet;
     140             : };
     141             : 
     142             : } // namespace
     143             : 
     144             : nscolor
     145         152 : nsPresContext::MakeColorPref(const nsString& aColor)
     146             : {
     147         304 :   nsCSSParser parser;
     148         304 :   nsCSSValue value;
     149         152 :   if (!parser.ParseColorString(aColor, nullptr, 0, value)) {
     150             :     // Any better choices?
     151           0 :     return NS_RGB(0, 0, 0);
     152             :   }
     153             : 
     154             :   nscolor color;
     155         152 :   return nsRuleNode::ComputeColor(value, this, nullptr, color)
     156         152 :     ? color
     157         152 :     : NS_RGB(0, 0, 0);
     158             : }
     159             : 
     160             : bool
     161           0 : nsPresContext::IsDOMPaintEventPending()
     162             : {
     163           0 :   if (mFireAfterPaintEvents) {
     164           0 :     return true;
     165             :   }
     166           0 :   nsRootPresContext* drpc = GetRootPresContext();
     167           0 :   if (drpc && drpc->mRefreshDriver->ViewManagerFlushIsPending()) {
     168             :     // Since we're promising that there will be a MozAfterPaint event
     169             :     // fired, we record an empty invalidation in case display list
     170             :     // invalidation doesn't invalidate anything further.
     171           0 :     NotifyInvalidation(drpc->mRefreshDriver->LastTransactionId() + 1, nsRect(0, 0, 0, 0));
     172           0 :     NS_ASSERTION(mFireAfterPaintEvents, "Why aren't we planning to fire the event?");
     173           0 :     return true;
     174             :   }
     175           0 :   return false;
     176             : }
     177             : 
     178             : void
     179           0 : nsPresContext::PrefChangedCallback(const char* aPrefName, void* instance_data)
     180             : {
     181             :   RefPtr<nsPresContext>  presContext =
     182           0 :     static_cast<nsPresContext*>(instance_data);
     183             : 
     184           0 :   NS_ASSERTION(nullptr != presContext, "bad instance data");
     185           0 :   if (nullptr != presContext) {
     186           0 :     presContext->PreferenceChanged(aPrefName);
     187             :   }
     188           0 : }
     189             : 
     190             : void
     191           0 : nsPresContext::PrefChangedUpdateTimerCallback(nsITimer *aTimer, void *aClosure)
     192             : {
     193           0 :   nsPresContext*  presContext = (nsPresContext*)aClosure;
     194           0 :   NS_ASSERTION(presContext != nullptr, "bad instance data");
     195           0 :   if (presContext)
     196           0 :     presContext->UpdateAfterPreferencesChanged();
     197           0 : }
     198             : 
     199             : static bool
     200          30 : IsVisualCharset(NotNull<const Encoding*> aCharset)
     201             : {
     202          30 :   return aCharset == ISO_8859_8_ENCODING;
     203             : }
     204             : 
     205          28 : nsPresContext::nsPresContext(nsIDocument* aDocument, nsPresContextType aType)
     206             :   : mType(aType),
     207             :     mShell(nullptr),
     208             :     mDocument(aDocument),
     209          28 :     mMedium(aType == eContext_Galley ? nsGkAtoms::screen : nsGkAtoms::print),
     210             :     mMediaEmulated(mMedium),
     211             :     mLinkHandler(nullptr),
     212             :     mInflationDisabledForShrinkWrap(false),
     213             :     mBaseMinFontSize(0),
     214             :     mSystemFontScale(1.0),
     215             :     mTextZoom(1.0),
     216             :     mEffectiveTextZoom(1.0),
     217             :     mFullZoom(1.0),
     218             :     mOverrideDPPX(0.0),
     219             :     mLastFontInflationScreenSize(gfxSize(-1.0, -1.0)),
     220             :     mCurAppUnitsPerDevPixel(0),
     221             :     mAutoQualityMinFontSizePixelsPref(0),
     222             :     mPageSize(-1, -1),
     223             :     mPageScale(0.0),
     224             :     mPPScale(1.0f),
     225             :     mDefaultColor(NS_RGBA(0,0,0,0)),
     226             :     mBackgroundColor(NS_RGB(0xFF, 0xFF, 0xFF)),
     227             :     mLinkColor(NS_RGB(0x00, 0x00, 0xEE)),
     228             :     mActiveLinkColor(NS_RGB(0xEE, 0x00, 0x00)),
     229             :     mVisitedLinkColor(NS_RGB(0x55, 0x1A, 0x8B)),
     230          28 :     mFocusBackgroundColor(mBackgroundColor),
     231          28 :     mFocusTextColor(mDefaultColor),
     232          28 :     mBodyTextColor(mDefaultColor),
     233             :     mViewportScrollbarOverrideNode(nullptr),
     234             :     mViewportStyleScrollbar(NS_STYLE_OVERFLOW_AUTO, NS_STYLE_OVERFLOW_AUTO),
     235             :     mFocusRingWidth(1),
     236             :     mExistThrottledUpdates(false),
     237             :     // mImageAnimationMode is initialised below, in constructor body
     238             :     mImageAnimationModePref(imgIContainer::kNormalAnimMode),
     239             :     mFontGroupCacheDirty(true),
     240             :     mInterruptChecksToSkip(0),
     241             :     mElementsRestyled(0),
     242             :     mFramesConstructed(0),
     243             :     mFramesReflowed(0),
     244             :     mInteractionTimeEnabled(true),
     245             :     mTelemetryScrollLastY(0),
     246             :     mTelemetryScrollMaxY(0),
     247             :     mTelemetryScrollTotalY(0),
     248             :     mHasPendingInterrupt(false),
     249             :     mPendingInterruptFromTest(false),
     250             :     mInterruptsEnabled(false),
     251             :     mUseDocumentFonts(true),
     252             :     mUseDocumentColors(true),
     253             :     mUnderlineLinks(true),
     254             :     mSendAfterPaintToContent(false),
     255             :     mUseFocusColors(false),
     256             :     mFocusRingOnAnything(false),
     257             :     mFocusRingStyle(false),
     258             :     mDrawImageBackground(true), // always draw the background
     259             :     mDrawColorBackground(true),
     260             :     // mNeverAnimate is initialised below, in constructor body
     261             :     mIsRenderingOnlySelection(false),
     262          28 :     mPaginated(aType != eContext_Galley),
     263             :     mCanPaginatedScroll(false),
     264             :     mDoScaledTwips(true),
     265             :     mIsRootPaginatedDocument(false),
     266             :     mPrefBidiDirection(false),
     267             :     mPrefScrollbarSide(0),
     268             :     mPendingSysColorChanged(false),
     269             :     mPendingThemeChanged(false),
     270             :     mPendingUIResolutionChanged(false),
     271             :     mPendingMediaFeatureValuesChanged(false),
     272             :     mPrefChangePendingNeedsReflow(false),
     273             :     mIsEmulatingMedia(false),
     274             :     mIsGlyph(false),
     275             :     mUsesRootEMUnits(false),
     276             :     mUsesExChUnits(false),
     277             :     mUsesViewportUnits(false),
     278             :     mPendingViewportChange(false),
     279             :     mCounterStylesDirty(true),
     280             :     mPostedFlushCounterStyles(false),
     281             :     mSuppressResizeReflow(false),
     282             :     mIsVisual(false),
     283             :     mFireAfterPaintEvents(false),
     284             :     mIsChrome(false),
     285             :     mIsChromeOriginImage(false),
     286             :     mPaintFlashing(false),
     287             :     mPaintFlashingInitialized(false),
     288             :     mHasWarnedAboutPositionedTableParts(false),
     289             :     mHasWarnedAboutTooLargeDashedOrDottedRadius(false),
     290             :     mQuirkSheetAdded(false),
     291             :     mNeedsPrefUpdate(false),
     292             :     mHadNonBlankPaint(false)
     293             : #ifdef RESTYLE_LOGGING
     294             :     , mRestyleLoggingEnabled(false)
     295             : #endif
     296             : #ifdef DEBUG
     297         168 :     , mInitialized(false)
     298             : #endif
     299             : {
     300          28 :   PodZero(&mBorderWidthTable);
     301             : #ifdef DEBUG
     302          28 :   PodZero(&mLayoutPhaseCount);
     303             : #endif
     304             : 
     305          28 :   if (!IsDynamic()) {
     306           0 :     mImageAnimationMode = imgIContainer::kDontAnimMode;
     307           0 :     mNeverAnimate = true;
     308             :   } else {
     309          28 :     mImageAnimationMode = imgIContainer::kNormalAnimMode;
     310          28 :     mNeverAnimate = false;
     311             :   }
     312          28 :   NS_ASSERTION(mDocument, "Null document");
     313             : 
     314             :   // if text perf logging enabled, init stats struct
     315          28 :   if (MOZ_LOG_TEST(gfxPlatform::GetLog(eGfxLog_textperf), LogLevel::Warning)) {
     316           0 :     mTextPerf = new gfxTextPerfMetrics();
     317             :   }
     318             : 
     319          28 :   if (Preferences::GetBool(GFX_MISSING_FONTS_NOTIFY_PREF)) {
     320           0 :     mMissingFonts = new gfxMissingFontRecorder();
     321             :   }
     322          28 : }
     323             : 
     324             : void
     325           0 : nsPresContext::Destroy()
     326             : {
     327           0 :   if (mEventManager) {
     328             :     // unclear if these are needed, but can't hurt
     329           0 :     mEventManager->NotifyDestroyPresContext(this);
     330           0 :     mEventManager->SetPresContext(nullptr);
     331           0 :     mEventManager = nullptr;
     332             :   }
     333             : 
     334           0 :   if (mPrefChangedTimer)
     335             :   {
     336           0 :     mPrefChangedTimer->Cancel();
     337           0 :     mPrefChangedTimer = nullptr;
     338             :   }
     339             : 
     340             :   // Unregister preference callbacks
     341             :   Preferences::UnregisterPrefixCallback(nsPresContext::PrefChangedCallback,
     342             :                                         "font.",
     343           0 :                                         this);
     344             :   Preferences::UnregisterPrefixCallback(nsPresContext::PrefChangedCallback,
     345             :                                         "browser.display.",
     346           0 :                                         this);
     347             :   Preferences::UnregisterCallback(nsPresContext::PrefChangedCallback,
     348             :                                   "browser.underline_anchors",
     349           0 :                                   this);
     350             :   Preferences::UnregisterCallback(nsPresContext::PrefChangedCallback,
     351             :                                   "browser.anchor_color",
     352           0 :                                   this);
     353             :   Preferences::UnregisterCallback(nsPresContext::PrefChangedCallback,
     354             :                                   "browser.active_color",
     355           0 :                                   this);
     356             :   Preferences::UnregisterCallback(nsPresContext::PrefChangedCallback,
     357             :                                   "browser.visited_color",
     358           0 :                                   this);
     359             :   Preferences::UnregisterCallback(nsPresContext::PrefChangedCallback,
     360             :                                   "image.animation_mode",
     361           0 :                                   this);
     362             :   Preferences::UnregisterPrefixCallback(nsPresContext::PrefChangedCallback,
     363             :                                         "bidi.",
     364           0 :                                         this);
     365             :   Preferences::UnregisterCallback(nsPresContext::PrefChangedCallback,
     366             :                                   "dom.send_after_paint_to_content",
     367           0 :                                   this);
     368             :   Preferences::UnregisterPrefixCallback(nsPresContext::PrefChangedCallback,
     369             :                                         "gfx.font_rendering.",
     370           0 :                                         this);
     371             :   Preferences::UnregisterCallback(nsPresContext::PrefChangedCallback,
     372             :                                   "layout.css.dpi",
     373           0 :                                   this);
     374             :   Preferences::UnregisterCallback(nsPresContext::PrefChangedCallback,
     375             :                                   "layout.css.devPixelsPerPx",
     376           0 :                                   this);
     377             :   Preferences::UnregisterCallback(nsPresContext::PrefChangedCallback,
     378             :                                   "nglayout.debug.paint_flashing",
     379           0 :                                   this);
     380             :   Preferences::UnregisterCallback(nsPresContext::PrefChangedCallback,
     381             :                                   "nglayout.debug.paint_flashing_chrome",
     382           0 :                                   this);
     383             :   Preferences::UnregisterCallback(nsPresContext::PrefChangedCallback,
     384             :                                   kUseStandinsForNativeColors,
     385           0 :                                   this);
     386             : 
     387           0 :   mRefreshDriver = nullptr;
     388           0 : }
     389             : 
     390           0 : nsPresContext::~nsPresContext()
     391             : {
     392           0 :   NS_PRECONDITION(!mShell, "Presshell forgot to clear our mShell pointer");
     393           0 :   DetachShell();
     394             : 
     395           0 :   Destroy();
     396           0 : }
     397             : 
     398          28 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsPresContext)
     399           0 :    NS_INTERFACE_MAP_ENTRY(nsISupports)
     400           0 :    NS_INTERFACE_MAP_ENTRY(nsIObserver)
     401           0 : NS_INTERFACE_MAP_END
     402             : 
     403        4606 : NS_IMPL_CYCLE_COLLECTING_ADDREF(nsPresContext)
     404        4509 : NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_LAST_RELEASE(nsPresContext, LastRelease())
     405             : 
     406             : void
     407           3 : nsPresContext::LastRelease()
     408             : {
     409           3 :   if (IsRoot()) {
     410           2 :     static_cast<nsRootPresContext*>(this)->CancelAllDidPaintTimers();
     411             :   }
     412           3 :   if (mMissingFonts) {
     413           0 :     mMissingFonts->Clear();
     414             :   }
     415           3 : }
     416             : 
     417             : NS_IMPL_CYCLE_COLLECTION_CLASS(nsPresContext)
     418             : 
     419           0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsPresContext)
     420           0 :   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAnimationManager);
     421           0 :   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocument);
     422             :   // NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(mDeviceContext); // not xpcom
     423           0 :   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEffectCompositor);
     424           0 :   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEventManager);
     425             :   // NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(mLanguage); // an atom
     426             : 
     427             :   // NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTheme); // a service
     428             :   // NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mLangService); // a service
     429           0 :   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPrintSettings);
     430           0 :   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPrefChangedTimer);
     431           0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
     432             : 
     433           0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsPresContext)
     434           0 :   NS_IMPL_CYCLE_COLLECTION_UNLINK(mAnimationManager);
     435           0 :   NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocument);
     436           0 :   NS_IMPL_CYCLE_COLLECTION_UNLINK(mDeviceContext); // worth bothering?
     437           0 :   NS_IMPL_CYCLE_COLLECTION_UNLINK(mEffectCompositor);
     438             :   // NS_RELEASE(tmp->mLanguage); // an atom
     439             :   // NS_IMPL_CYCLE_COLLECTION_UNLINK(mTheme); // a service
     440             :   // NS_IMPL_CYCLE_COLLECTION_UNLINK(mLangService); // a service
     441           0 :   NS_IMPL_CYCLE_COLLECTION_UNLINK(mPrintSettings);
     442             : 
     443           0 :   tmp->Destroy();
     444           0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
     445             : 
     446             : // whether no native theme service exists;
     447             : // if this gets set to true, we'll stop asking for it.
     448             : static bool sNoTheme = false;
     449             : 
     450             : // Set to true when LookAndFeelChanged needs to be called.  This is used
     451             : // because the look and feel is a service, so there's no need to notify it from
     452             : // more than one prescontext.
     453             : static bool sLookAndFeelChanged;
     454             : 
     455             : // Set to true when ThemeChanged needs to be called on mTheme.  This is used
     456             : // because mTheme is a service, so there's no need to notify it from more than
     457             : // one prescontext.
     458             : static bool sThemeChanged;
     459             : 
     460             : void
     461          35 : nsPresContext::GetDocumentColorPreferences()
     462             : {
     463             :   // Make sure the preferences are initialized.  In the normal run,
     464             :   // they would already be, because gfxPlatform would have been created,
     465             :   // but in some reference tests, that is not the case.
     466          35 :   gfxPrefs::GetSingleton();
     467             : 
     468          35 :   int32_t useAccessibilityTheme = 0;
     469          35 :   bool usePrefColors = true;
     470          35 :   bool isChromeDocShell = false;
     471             :   static int32_t sDocumentColorsSetting;
     472             :   static bool sDocumentColorsSettingPrefCached = false;
     473             :   static bool sUseStandinsForNativeColors = false;
     474          35 :   if (!sDocumentColorsSettingPrefCached) {
     475           2 :     sDocumentColorsSettingPrefCached = true;
     476             :     Preferences::AddIntVarCache(&sDocumentColorsSetting,
     477             :                                 "browser.display.document_color_use",
     478           2 :                                 0);
     479             : 
     480             :     // The preference "ui.use_standins_for_native_colors" also affects
     481             :     // default foreground and background colors.
     482             :     Preferences::AddBoolVarCache(&sUseStandinsForNativeColors,
     483           2 :                                  kUseStandinsForNativeColors);
     484             :   }
     485             : 
     486          35 :   nsIDocument* doc = mDocument->GetDisplayDocument();
     487          35 :   if (doc && doc->GetDocShell()) {
     488           0 :     isChromeDocShell = nsIDocShellTreeItem::typeChrome ==
     489           0 :                        doc->GetDocShell()->ItemType();
     490             :   } else {
     491          70 :     nsCOMPtr<nsIDocShellTreeItem> docShell(mContainer);
     492          35 :     if (docShell) {
     493          14 :       isChromeDocShell = nsIDocShellTreeItem::typeChrome == docShell->ItemType();
     494             :     }
     495             :   }
     496             : 
     497          56 :   mIsChromeOriginImage = mDocument->IsBeingUsedAsImage() &&
     498          21 :                          IsChromeURI(mDocument->GetDocumentURI());
     499             : 
     500          35 :   if (isChromeDocShell || mIsChromeOriginImage) {
     501          29 :     usePrefColors = false;
     502             :   } else {
     503             :     useAccessibilityTheme =
     504           6 :       LookAndFeel::GetInt(LookAndFeel::eIntID_UseAccessibilityTheme, 0);
     505           6 :     usePrefColors = !useAccessibilityTheme;
     506             :   }
     507          35 :   if (usePrefColors) {
     508           6 :     usePrefColors =
     509           6 :       !Preferences::GetBool("browser.display.use_system_colors", false);
     510             :   }
     511             : 
     512          35 :   if (sUseStandinsForNativeColors) {
     513             :     // Once the preference "ui.use_standins_for_native_colors" is enabled,
     514             :     // use fixed color values instead of prefered colors and system colors.
     515           0 :     mDefaultColor = LookAndFeel::GetColorUsingStandins(
     516             :         LookAndFeel::eColorID_windowtext, NS_RGB(0x00, 0x00, 0x00));
     517           0 :     mBackgroundColor = LookAndFeel::GetColorUsingStandins(
     518             :         LookAndFeel::eColorID_window, NS_RGB(0xff, 0xff, 0xff));
     519          35 :   } else if (usePrefColors) {
     520             :     nsAdoptingString colorStr =
     521          12 :       Preferences::GetString("browser.display.foreground_color");
     522             : 
     523           6 :     if (!colorStr.IsEmpty()) {
     524           6 :       mDefaultColor = MakeColorPref(colorStr);
     525             :     }
     526             : 
     527           6 :     colorStr = Preferences::GetString("browser.display.background_color");
     528             : 
     529           6 :     if (!colorStr.IsEmpty()) {
     530           6 :       mBackgroundColor = MakeColorPref(colorStr);
     531             :     }
     532             :   }
     533             :   else {
     534          29 :     mDefaultColor =
     535          29 :       LookAndFeel::GetColor(LookAndFeel::eColorID_WindowForeground,
     536             :                             NS_RGB(0x00, 0x00, 0x00));
     537          29 :     mBackgroundColor =
     538          29 :       LookAndFeel::GetColor(LookAndFeel::eColorID_WindowBackground,
     539             :                             NS_RGB(0xFF, 0xFF, 0xFF));
     540             :   }
     541             : 
     542             :   // Wherever we got the default background color from, ensure it is
     543             :   // opaque.
     544          35 :   mBackgroundColor = NS_ComposeColors(NS_RGB(0xFF, 0xFF, 0xFF),
     545             :                                       mBackgroundColor);
     546             : 
     547             : 
     548             :   // Now deal with the pref:
     549             :   // 0 = default: always, except in high contrast mode
     550             :   // 1 = always
     551             :   // 2 = never
     552          35 :   if (sDocumentColorsSetting == 1 || mDocument->IsBeingUsedAsImage()) {
     553          21 :     mUseDocumentColors = true;
     554          14 :   } else if (sDocumentColorsSetting == 2) {
     555           0 :     mUseDocumentColors = isChromeDocShell || mIsChromeOriginImage;
     556             :   } else {
     557          14 :     MOZ_ASSERT(!useAccessibilityTheme ||
     558             :                !(isChromeDocShell || mIsChromeOriginImage),
     559             :                "The accessibility theme should only be on for non-chrome");
     560          14 :     mUseDocumentColors = !useAccessibilityTheme;
     561             :   }
     562          35 : }
     563             : 
     564             : void
     565          28 : nsPresContext::GetUserPreferences()
     566             : {
     567          28 :   if (!GetPresShell()) {
     568             :     // No presshell means nothing to do here.  We'll do this when we
     569             :     // get a presshell.
     570           0 :     return;
     571             :   }
     572             : 
     573          28 :   mAutoQualityMinFontSizePixelsPref =
     574          28 :     Preferences::GetInt("browser.display.auto_quality_min_font_size");
     575             : 
     576             :   // * document colors
     577          28 :   GetDocumentColorPreferences();
     578             : 
     579          28 :   mSendAfterPaintToContent =
     580          28 :     Preferences::GetBool("dom.send_after_paint_to_content",
     581          28 :                          mSendAfterPaintToContent);
     582             : 
     583             :   // * link colors
     584          28 :   mUnderlineLinks =
     585          28 :     Preferences::GetBool("browser.underline_anchors", mUnderlineLinks);
     586             : 
     587          56 :   nsAdoptingString colorStr = Preferences::GetString("browser.anchor_color");
     588             : 
     589          28 :   if (!colorStr.IsEmpty()) {
     590          28 :     mLinkColor = MakeColorPref(colorStr);
     591             :   }
     592             : 
     593          28 :   colorStr = Preferences::GetString("browser.active_color");
     594             : 
     595          28 :   if (!colorStr.IsEmpty()) {
     596          28 :     mActiveLinkColor = MakeColorPref(colorStr);
     597             :   }
     598             : 
     599          28 :   colorStr = Preferences::GetString("browser.visited_color");
     600             : 
     601          28 :   if (!colorStr.IsEmpty()) {
     602          28 :     mVisitedLinkColor = MakeColorPref(colorStr);
     603             :   }
     604             : 
     605          28 :   mUseFocusColors =
     606          28 :     Preferences::GetBool("browser.display.use_focus_colors", mUseFocusColors);
     607             : 
     608          28 :   mFocusTextColor = mDefaultColor;
     609          28 :   mFocusBackgroundColor = mBackgroundColor;
     610             : 
     611          28 :   colorStr = Preferences::GetString("browser.display.focus_text_color");
     612             : 
     613          28 :   if (!colorStr.IsEmpty()) {
     614          28 :     mFocusTextColor = MakeColorPref(colorStr);
     615             :   }
     616             : 
     617          28 :   colorStr = Preferences::GetString("browser.display.focus_background_color");
     618             : 
     619          28 :   if (!colorStr.IsEmpty()) {
     620          28 :     mFocusBackgroundColor = MakeColorPref(colorStr);
     621             :   }
     622             : 
     623          28 :   mFocusRingWidth =
     624          28 :     Preferences::GetInt("browser.display.focus_ring_width", mFocusRingWidth);
     625             : 
     626          28 :   mFocusRingOnAnything =
     627          28 :     Preferences::GetBool("browser.display.focus_ring_on_anything",
     628          28 :                          mFocusRingOnAnything);
     629             : 
     630          28 :   mFocusRingStyle =
     631          28 :     Preferences::GetInt("browser.display.focus_ring_style", mFocusRingStyle);
     632             : 
     633          28 :   mBodyTextColor = mDefaultColor;
     634             : 
     635             :   // * use fonts?
     636          28 :   mUseDocumentFonts =
     637          28 :     Preferences::GetInt("browser.display.use_document_fonts") != 0;
     638             : 
     639          28 :   mPrefScrollbarSide = Preferences::GetInt("layout.scrollbar.side");
     640             : 
     641          28 :   mLangGroupFontPrefs.Reset();
     642          28 :   mFontGroupCacheDirty = true;
     643          28 :   StaticPresData::Get()->ResetCachedFontPrefs();
     644             : 
     645             :   // * image animation
     646             :   const nsAdoptingCString& animatePref =
     647          56 :     Preferences::GetCString("image.animation_mode");
     648          28 :   if (animatePref.EqualsLiteral("normal"))
     649          28 :     mImageAnimationModePref = imgIContainer::kNormalAnimMode;
     650           0 :   else if (animatePref.EqualsLiteral("none"))
     651           0 :     mImageAnimationModePref = imgIContainer::kDontAnimMode;
     652           0 :   else if (animatePref.EqualsLiteral("once"))
     653           0 :     mImageAnimationModePref = imgIContainer::kLoopOnceAnimMode;
     654             :   else // dynamic change to invalid value should act like it does initially
     655           0 :     mImageAnimationModePref = imgIContainer::kNormalAnimMode;
     656             : 
     657          28 :   uint32_t bidiOptions = GetBidi();
     658             : 
     659             :   int32_t prefInt =
     660          28 :     Preferences::GetInt(IBMBIDI_TEXTDIRECTION_STR,
     661          56 :                         GET_BIDI_OPTION_DIRECTION(bidiOptions));
     662          28 :   SET_BIDI_OPTION_DIRECTION(bidiOptions, prefInt);
     663          28 :   mPrefBidiDirection = prefInt;
     664             : 
     665             :   prefInt =
     666          28 :     Preferences::GetInt(IBMBIDI_TEXTTYPE_STR,
     667          56 :                         GET_BIDI_OPTION_TEXTTYPE(bidiOptions));
     668          28 :   SET_BIDI_OPTION_TEXTTYPE(bidiOptions, prefInt);
     669             : 
     670             :   prefInt =
     671          28 :     Preferences::GetInt(IBMBIDI_NUMERAL_STR,
     672          56 :                         GET_BIDI_OPTION_NUMERAL(bidiOptions));
     673          28 :   SET_BIDI_OPTION_NUMERAL(bidiOptions, prefInt);
     674             : 
     675             :   // We don't need to force reflow: either we are initializing a new
     676             :   // prescontext or we are being called from UpdateAfterPreferencesChanged()
     677             :   // which triggers a reflow anyway.
     678          28 :   SetBidi(bidiOptions, false);
     679             : }
     680             : 
     681             : void
     682           1 : nsPresContext::InvalidatePaintedLayers()
     683             : {
     684           1 :   if (!mShell)
     685           0 :     return;
     686           1 :   nsIFrame* rootFrame = mShell->FrameManager()->GetRootFrame();
     687           1 :   if (rootFrame) {
     688             :     // FrameLayerBuilder caches invalidation-related values that depend on the
     689             :     // appunits-per-dev-pixel ratio, so ensure that all PaintedLayer drawing
     690             :     // is completely flushed.
     691           0 :     rootFrame->InvalidateFrameSubtree();
     692             :   }
     693             : }
     694             : 
     695             : void
     696           1 : nsPresContext::AppUnitsPerDevPixelChanged()
     697             : {
     698           1 :   InvalidatePaintedLayers();
     699             : 
     700           1 :   if (mDeviceContext) {
     701           1 :     mDeviceContext->FlushFontCache();
     702             :   }
     703             : 
     704           1 :   if (HasCachedStyleData()) {
     705             :     // All cached style data must be recomputed.
     706           0 :     MediaFeatureValuesChanged(eRestyle_ForceDescendants, NS_STYLE_HINT_REFLOW);
     707             :   }
     708             : 
     709           1 :   mCurAppUnitsPerDevPixel = AppUnitsPerDevPixel();
     710           1 : }
     711             : 
     712             : void
     713           0 : nsPresContext::PreferenceChanged(const char* aPrefName)
     714             : {
     715           0 :   nsDependentCString prefName(aPrefName);
     716           0 :   if (prefName.EqualsLiteral("layout.css.dpi") ||
     717           0 :       prefName.EqualsLiteral("layout.css.devPixelsPerPx")) {
     718             : 
     719           0 :     int32_t oldAppUnitsPerDevPixel = AppUnitsPerDevPixel();
     720           0 :     if (mDeviceContext->CheckDPIChange() && mShell) {
     721           0 :       nsCOMPtr<nsIPresShell> shell = mShell;
     722             :       // Re-fetch the view manager's window dimensions in case there's a deferred
     723             :       // resize which hasn't affected our mVisibleArea yet
     724             :       nscoord oldWidthAppUnits, oldHeightAppUnits;
     725           0 :       RefPtr<nsViewManager> vm = shell->GetViewManager();
     726           0 :       if (!vm) {
     727           0 :         return;
     728             :       }
     729           0 :       vm->GetWindowDimensions(&oldWidthAppUnits, &oldHeightAppUnits);
     730           0 :       float oldWidthDevPixels = oldWidthAppUnits/oldAppUnitsPerDevPixel;
     731           0 :       float oldHeightDevPixels = oldHeightAppUnits/oldAppUnitsPerDevPixel;
     732             : 
     733           0 :       nscoord width = NSToCoordRound(oldWidthDevPixels*AppUnitsPerDevPixel());
     734           0 :       nscoord height = NSToCoordRound(oldHeightDevPixels*AppUnitsPerDevPixel());
     735           0 :       vm->SetWindowDimensions(width, height);
     736             : 
     737           0 :       AppUnitsPerDevPixelChanged();
     738             :     }
     739           0 :     return;
     740             :   }
     741           0 :   if (prefName.EqualsLiteral(GFX_MISSING_FONTS_NOTIFY_PREF)) {
     742           0 :     if (Preferences::GetBool(GFX_MISSING_FONTS_NOTIFY_PREF)) {
     743           0 :       if (!mMissingFonts) {
     744           0 :         mMissingFonts = new gfxMissingFontRecorder();
     745             :         // trigger reflow to detect missing fonts on the current page
     746           0 :         mPrefChangePendingNeedsReflow = true;
     747             :       }
     748             :     } else {
     749           0 :       if (mMissingFonts) {
     750           0 :         mMissingFonts->Clear();
     751             :       }
     752           0 :       mMissingFonts = nullptr;
     753             :     }
     754             :   }
     755           0 :   if (StringBeginsWith(prefName, NS_LITERAL_CSTRING("font."))) {
     756             :     // Changes to font family preferences don't change anything in the
     757             :     // computed style data, so the style system won't generate a reflow
     758             :     // hint for us.  We need to do that manually.
     759             : 
     760             :     // FIXME We could probably also handle changes to
     761             :     // browser.display.auto_quality_min_font_size here, but that
     762             :     // probably also requires clearing the text run cache, so don't
     763             :     // bother (yet, anyway).
     764           0 :     mPrefChangePendingNeedsReflow = true;
     765             :   }
     766           0 :   if (StringBeginsWith(prefName, NS_LITERAL_CSTRING("bidi."))) {
     767             :     // Changes to bidi prefs need to trigger a reflow (see bug 443629)
     768           0 :     mPrefChangePendingNeedsReflow = true;
     769             : 
     770             :     // Changes to bidi.numeral also needs to empty the text run cache.
     771             :     // This is handled in gfxTextRunWordCache.cpp.
     772             :   }
     773           0 :   if (StringBeginsWith(prefName, NS_LITERAL_CSTRING("gfx.font_rendering."))) {
     774             :     // Changes to font_rendering prefs need to trigger a reflow
     775           0 :     mPrefChangePendingNeedsReflow = true;
     776             :   }
     777             :   // we use a zero-delay timer to coalesce multiple pref updates
     778           0 :   if (!mPrefChangedTimer)
     779             :   {
     780             :     // We will end up calling InvalidatePreferenceSheets one from each pres
     781             :     // context, but all it's doing is clearing its cached sheet pointers,
     782             :     // so it won't be wastefully recreating the sheet multiple times.
     783             :     // The first pres context that has its mPrefChangedTimer called will
     784             :     // be the one to cause the reconstruction of the pref style sheet.
     785           0 :     nsLayoutStylesheetCache::InvalidatePreferenceSheets();
     786           0 :     mPrefChangedTimer = CreateTimer(PrefChangedUpdateTimerCallback,
     787           0 :                                     "PrefChangedUpdateTimerCallback", 0);
     788           0 :     if (!mPrefChangedTimer) {
     789           0 :       return;
     790             :     }
     791             :   }
     792           0 :   if (prefName.EqualsLiteral("nglayout.debug.paint_flashing") ||
     793           0 :       prefName.EqualsLiteral("nglayout.debug.paint_flashing_chrome")) {
     794           0 :     mPaintFlashingInitialized = false;
     795           0 :     return;
     796             :   }
     797             : }
     798             : 
     799             : void
     800           0 : nsPresContext::UpdateAfterPreferencesChanged()
     801             : {
     802           0 :   mPrefChangedTimer = nullptr;
     803             : 
     804           0 :   if (!mContainer) {
     805             :     // Delay updating until there is a container
     806           0 :     mNeedsPrefUpdate = true;
     807           0 :     return;
     808             :   }
     809             : 
     810           0 :   nsCOMPtr<nsIDocShellTreeItem> docShell(mContainer);
     811           0 :   if (docShell && nsIDocShellTreeItem::typeChrome == docShell->ItemType()) {
     812           0 :     return;
     813             :   }
     814             : 
     815             :   // Initialize our state from the user preferences
     816           0 :   GetUserPreferences();
     817             : 
     818             :   // update the presShell: tell it to set the preference style rules up
     819           0 :   if (mShell) {
     820           0 :     mShell->UpdatePreferenceStyles();
     821             :   }
     822             : 
     823           0 :   InvalidatePaintedLayers();
     824           0 :   mDeviceContext->FlushFontCache();
     825             : 
     826           0 :   nsChangeHint hint = nsChangeHint(0);
     827             : 
     828           0 :   if (mPrefChangePendingNeedsReflow) {
     829           0 :     hint |= NS_STYLE_HINT_REFLOW;
     830             :   }
     831             : 
     832             :   // Preferences require rerunning selector matching because we rebuild
     833             :   // the pref style sheet for some preference changes.
     834           0 :   RebuildAllStyleData(hint, eRestyle_Subtree);
     835             : }
     836             : 
     837             : nsresult
     838          28 : nsPresContext::Init(nsDeviceContext* aDeviceContext)
     839             : {
     840          28 :   NS_ASSERTION(!mInitialized, "attempt to reinit pres context");
     841          28 :   NS_ENSURE_ARG(aDeviceContext);
     842             : 
     843          28 :   mDeviceContext = aDeviceContext;
     844             : 
     845             :   // In certain rare cases (such as changing page mode), we tear down layout
     846             :   // state and re-initialize a new prescontext for a document. Given that we
     847             :   // hang style state off the DOM, we detect that re-initialization case and
     848             :   // lazily drop the servo data. We don't do this eagerly during layout teardown
     849             :   // because that would incur an extra whole-tree traversal that's unnecessary
     850             :   // most of the time.
     851          28 :   if (mDocument->IsStyledByServo()) {
     852           0 :     Element* root = mDocument->GetRootElement();
     853           0 :     if (root && root->HasServoData()) {
     854           0 :       ServoRestyleManager::ClearServoDataFromSubtree(root);
     855             :     }
     856             :   }
     857             : 
     858          28 :   if (mDeviceContext->SetFullZoom(mFullZoom))
     859           0 :     mDeviceContext->FlushFontCache();
     860          28 :   mCurAppUnitsPerDevPixel = AppUnitsPerDevPixel();
     861             : 
     862          28 :   mEventManager = new mozilla::EventStateManager();
     863             : 
     864          28 :   mEffectCompositor = new mozilla::EffectCompositor(this);
     865          28 :   mTransitionManager = new nsTransitionManager(this);
     866          28 :   mAnimationManager = new nsAnimationManager(this);
     867             : 
     868          28 :   if (mDocument->GetDisplayDocument()) {
     869           0 :     NS_ASSERTION(mDocument->GetDisplayDocument()->GetShell() &&
     870             :                  mDocument->GetDisplayDocument()->GetShell()->GetPresContext(),
     871             :                  "Why are we being initialized?");
     872             :     mRefreshDriver = mDocument->GetDisplayDocument()->GetShell()->
     873           0 :       GetPresContext()->RefreshDriver();
     874             :   } else {
     875          28 :     nsIDocument* parent = mDocument->GetParentDocument();
     876             :     // Unfortunately, sometimes |parent| here has no presshell because
     877             :     // printing screws up things.  Assert that in other cases it does,
     878             :     // but whenever the shell is null just fall back on using our own
     879             :     // refresh driver.
     880          28 :     NS_ASSERTION(!parent || mDocument->IsStaticDocument() || parent->GetShell(),
     881             :                  "How did we end up with a presshell if our parent doesn't "
     882             :                  "have one?");
     883          28 :     if (parent && parent->GetShell()) {
     884           1 :       NS_ASSERTION(parent->GetShell()->GetPresContext(),
     885             :                    "How did we get a presshell?");
     886             : 
     887             :       // We don't have our container set yet at this point
     888           2 :       nsCOMPtr<nsIDocShellTreeItem> ourItem = mDocument->GetDocShell();
     889           1 :       if (ourItem) {
     890           2 :         nsCOMPtr<nsIDocShellTreeItem> parentItem;
     891           1 :         ourItem->GetSameTypeParent(getter_AddRefs(parentItem));
     892           1 :         if (parentItem) {
     893             :           Element* containingElement =
     894           0 :             parent->FindContentForSubDocument(mDocument);
     895           0 :           if (!containingElement->IsXULElement() ||
     896           0 :               !containingElement->
     897           0 :                 HasAttr(kNameSpaceID_None,
     898             :                         nsGkAtoms::forceOwnRefreshDriver)) {
     899           0 :             mRefreshDriver = parent->GetShell()->GetPresContext()->RefreshDriver();
     900             :           }
     901             :         }
     902             :       }
     903             :     }
     904             : 
     905          28 :     if (!mRefreshDriver) {
     906          28 :       mRefreshDriver = new nsRefreshDriver(this);
     907             :     }
     908             :   }
     909             : 
     910          28 :   mLangService = nsLanguageAtomService::GetService();
     911             : 
     912             :   // Register callbacks so we're notified when the preferences change
     913             :   Preferences::RegisterPrefixCallback(nsPresContext::PrefChangedCallback,
     914             :                                       "font.",
     915          28 :                                       this);
     916             :   Preferences::RegisterPrefixCallback(nsPresContext::PrefChangedCallback,
     917             :                                       "browser.display.",
     918          28 :                                       this);
     919             :   Preferences::RegisterCallback(nsPresContext::PrefChangedCallback,
     920             :                                 "browser.underline_anchors",
     921          28 :                                 this);
     922             :   Preferences::RegisterCallback(nsPresContext::PrefChangedCallback,
     923             :                                 "browser.anchor_color",
     924          28 :                                 this);
     925             :   Preferences::RegisterCallback(nsPresContext::PrefChangedCallback,
     926             :                                 "browser.active_color",
     927          28 :                                 this);
     928             :   Preferences::RegisterCallback(nsPresContext::PrefChangedCallback,
     929             :                                 "browser.visited_color",
     930          28 :                                 this);
     931             :   Preferences::RegisterCallback(nsPresContext::PrefChangedCallback,
     932             :                                 "image.animation_mode",
     933          28 :                                 this);
     934             :   Preferences::RegisterPrefixCallback(nsPresContext::PrefChangedCallback,
     935             :                                       "bidi.",
     936          28 :                                       this);
     937             :   Preferences::RegisterCallback(nsPresContext::PrefChangedCallback,
     938             :                                 "dom.send_after_paint_to_content",
     939          28 :                                 this);
     940             :   Preferences::RegisterPrefixCallback(nsPresContext::PrefChangedCallback,
     941             :                                       "gfx.font_rendering.",
     942          28 :                                       this);
     943             :   Preferences::RegisterCallback(nsPresContext::PrefChangedCallback,
     944             :                                 "layout.css.dpi",
     945          28 :                                 this);
     946             :   Preferences::RegisterCallback(nsPresContext::PrefChangedCallback,
     947             :                                 "layout.css.devPixelsPerPx",
     948          28 :                                 this);
     949             :   Preferences::RegisterCallback(nsPresContext::PrefChangedCallback,
     950             :                                 "nglayout.debug.paint_flashing",
     951          28 :                                 this);
     952             :   Preferences::RegisterCallback(nsPresContext::PrefChangedCallback,
     953             :                                 "nglayout.debug.paint_flashing_chrome",
     954          28 :                                 this);
     955             :   Preferences::RegisterCallback(nsPresContext::PrefChangedCallback,
     956             :                                 kUseStandinsForNativeColors,
     957          28 :                                 this);
     958             : 
     959          28 :   nsresult rv = mEventManager->Init();
     960          28 :   NS_ENSURE_SUCCESS(rv, rv);
     961             : 
     962          28 :   mEventManager->SetPresContext(this);
     963             : 
     964             : #ifdef RESTYLE_LOGGING
     965          28 :   mRestyleLoggingEnabled = GeckoRestyleManager::RestyleLoggingInitiallyEnabled();
     966             : #endif
     967             : 
     968             : #ifdef DEBUG
     969          28 :   mInitialized = true;
     970             : #endif
     971             : 
     972          28 :   return NS_OK;
     973             : }
     974             : 
     975             : // Note: We don't hold a reference on the shell; it has a reference to
     976             : // us
     977             : void
     978          28 : nsPresContext::AttachShell(nsIPresShell* aShell, StyleBackendType aBackendType)
     979             : {
     980          28 :   MOZ_ASSERT(!mShell);
     981          28 :   mShell = aShell;
     982             : 
     983          28 :   if (aBackendType == StyleBackendType::Servo) {
     984           0 :     mRestyleManager = new ServoRestyleManager(this);
     985             :   } else {
     986          28 :     mRestyleManager = new GeckoRestyleManager(this);
     987             :   }
     988             : 
     989             :   // Since CounterStyleManager is also the name of a method of
     990             :   // nsPresContext, it is necessary to prefix the class with the mozilla
     991             :   // namespace here.
     992          28 :   mCounterStyleManager = new mozilla::CounterStyleManager(this);
     993             : 
     994          28 :   nsIDocument *doc = mShell->GetDocument();
     995          28 :   NS_ASSERTION(doc, "expect document here");
     996          28 :   if (doc) {
     997             :     // Have to update PresContext's mDocument before calling any other methods.
     998          28 :     mDocument = doc;
     999             :   }
    1000             :   // Initialize our state from the user preferences, now that we
    1001             :   // have a presshell, and hence a document.
    1002          28 :   GetUserPreferences();
    1003             : 
    1004          28 :   if (doc) {
    1005          28 :     nsIURI *docURI = doc->GetDocumentURI();
    1006             : 
    1007          28 :     if (IsDynamic() && docURI) {
    1008          28 :       bool isChrome = false;
    1009          28 :       bool isRes = false;
    1010          28 :       docURI->SchemeIs("chrome", &isChrome);
    1011          28 :       docURI->SchemeIs("resource", &isRes);
    1012             : 
    1013          28 :       if (!isChrome && !isRes)
    1014           5 :         mImageAnimationMode = mImageAnimationModePref;
    1015             :       else
    1016          23 :         mImageAnimationMode = imgIContainer::kNormalAnimMode;
    1017             :     }
    1018             : 
    1019          28 :     doc->AddCharSetObserver(this);
    1020          28 :     UpdateCharSet(doc->GetDocumentCharacterSet());
    1021             :   }
    1022          28 : }
    1023             : 
    1024             : void
    1025           4 : nsPresContext::DetachShell()
    1026             : {
    1027             :   // Remove ourselves as the charset observer from the shell's doc, because
    1028             :   // this shell may be going away for good.
    1029           4 :   nsIDocument *doc = mShell ? mShell->GetDocument() : nullptr;
    1030           4 :   if (doc) {
    1031           4 :     doc->RemoveCharSetObserver(this);
    1032             :   }
    1033             : 
    1034             :   // The counter style manager's destructor needs to deallocate with the
    1035             :   // presshell arena. Disconnect it before nulling out the shell.
    1036             :   //
    1037             :   // XXXbholley: Given recent refactorings, it probably makes more sense to
    1038             :   // just null our mShell at the bottom of this function. I'm leaving it
    1039             :   // this way to preserve the old ordering, but I doubt anything would break.
    1040           4 :   if (mCounterStyleManager) {
    1041           4 :     mCounterStyleManager->Disconnect();
    1042           4 :     mCounterStyleManager = nullptr;
    1043             :   }
    1044             : 
    1045           4 :   mShell = nullptr;
    1046             : 
    1047           4 :   if (mEffectCompositor) {
    1048           4 :     mEffectCompositor->Disconnect();
    1049           4 :     mEffectCompositor = nullptr;
    1050             :   }
    1051           4 :   if (mTransitionManager) {
    1052           4 :     mTransitionManager->Disconnect();
    1053           4 :     mTransitionManager = nullptr;
    1054             :   }
    1055           4 :   if (mAnimationManager) {
    1056           4 :     mAnimationManager->Disconnect();
    1057           4 :     mAnimationManager = nullptr;
    1058             :   }
    1059           4 :   if (mRestyleManager) {
    1060           4 :     mRestyleManager->Disconnect();
    1061           4 :     mRestyleManager = nullptr;
    1062             :   }
    1063           4 :   if (mRefreshDriver && mRefreshDriver->GetPresContext() == this) {
    1064           4 :     mRefreshDriver->Disconnect();
    1065             :     // Can't null out the refresh driver here.
    1066             :   }
    1067             : 
    1068           4 :   if (IsRoot()) {
    1069           3 :     nsRootPresContext* thisRoot = static_cast<nsRootPresContext*>(this);
    1070             : 
    1071             :     // Have to cancel our plugin geometry timer, because the
    1072             :     // callback for that depends on a non-null presshell.
    1073           3 :     thisRoot->CancelApplyPluginGeometryTimer();
    1074             : 
    1075             :     // The did-paint timer also depends on a non-null pres shell.
    1076           3 :     thisRoot->CancelAllDidPaintTimers();
    1077             :   }
    1078           4 : }
    1079             : 
    1080             : void
    1081           2 : nsPresContext::DoChangeCharSet(NotNull<const Encoding*> aCharSet)
    1082             : {
    1083           2 :   UpdateCharSet(aCharSet);
    1084           2 :   mDeviceContext->FlushFontCache();
    1085           2 :   RebuildAllStyleData(NS_STYLE_HINT_REFLOW, nsRestyleHint(0));
    1086           2 : }
    1087             : 
    1088             : void
    1089          30 : nsPresContext::UpdateCharSet(NotNull<const Encoding*> aCharSet)
    1090             : {
    1091          30 :   mLanguage = mLangService->LookupCharSet(aCharSet);
    1092             :   // this will be a language group (or script) code rather than a true language code
    1093             : 
    1094             :   // bug 39570: moved from nsLanguageAtomService::LookupCharSet()
    1095          30 :   if (mLanguage == nsGkAtoms::Unicode) {
    1096          28 :     mLanguage = mLangService->GetLocaleLanguage();
    1097             :   }
    1098          30 :   mLangGroupFontPrefs.Reset();
    1099          30 :   mFontGroupCacheDirty = true;
    1100             : 
    1101          30 :   switch (GET_BIDI_OPTION_TEXTTYPE(GetBidi())) {
    1102             : 
    1103             :     case IBMBIDI_TEXTTYPE_LOGICAL:
    1104           0 :       SetVisualMode(false);
    1105           0 :       break;
    1106             : 
    1107             :     case IBMBIDI_TEXTTYPE_VISUAL:
    1108           0 :       SetVisualMode(true);
    1109           0 :       break;
    1110             : 
    1111             :     case IBMBIDI_TEXTTYPE_CHARSET:
    1112             :     default:
    1113          30 :       SetVisualMode(IsVisualCharset(aCharSet));
    1114             :   }
    1115          30 : }
    1116             : 
    1117             : NS_IMETHODIMP
    1118           2 : nsPresContext::Observe(nsISupports* aSubject,
    1119             :                         const char* aTopic,
    1120             :                         const char16_t* aData)
    1121             : {
    1122           2 :   if (!nsCRT::strcmp(aTopic, "charset")) {
    1123           2 :     auto encoding = Encoding::ForName(NS_LossyConvertUTF16toASCII(aData));
    1124             :     RefPtr<CharSetChangingRunnable> runnable =
    1125           6 :       new CharSetChangingRunnable(this, encoding);
    1126           6 :     return Document()->Dispatch("CharSetChangingRunnable",
    1127             :                                 TaskCategory::Other,
    1128           6 :                                 runnable.forget());
    1129             :   }
    1130             : 
    1131           0 :   NS_WARNING("unrecognized topic in nsPresContext::Observe");
    1132           0 :   return NS_ERROR_FAILURE;
    1133             : }
    1134             : 
    1135             : nsPresContext*
    1136       16446 : nsPresContext::GetParentPresContext()
    1137             : {
    1138       16446 :   nsIPresShell* shell = GetPresShell();
    1139       16446 :   if (shell) {
    1140       16446 :     nsViewManager* viewManager = shell->GetViewManager();
    1141       16446 :     if (viewManager) {
    1142       16446 :       nsView* view = viewManager->GetRootView();
    1143       16446 :       if (view) {
    1144       16446 :         view = view->GetParent(); // anonymous inner view
    1145       16446 :         if (view) {
    1146           0 :           view = view->GetParent(); // subdocumentframe's view
    1147           0 :           if (view) {
    1148           0 :             nsIFrame* f = view->GetFrame();
    1149           0 :             if (f) {
    1150           0 :               return f->PresContext();
    1151             :             }
    1152             :           }
    1153             :         }
    1154             :       }
    1155             :     }
    1156             :   }
    1157       16446 :   return nullptr;
    1158             : }
    1159             : 
    1160             : nsPresContext*
    1161          96 : nsPresContext::GetToplevelContentDocumentPresContext()
    1162             : {
    1163          96 :   if (IsChrome())
    1164          83 :     return nullptr;
    1165          13 :   nsPresContext* pc = this;
    1166             :   for (;;) {
    1167          13 :     nsPresContext* parent = pc->GetParentPresContext();
    1168          13 :     if (!parent || parent->IsChrome())
    1169          13 :       return pc;
    1170           0 :     pc = parent;
    1171           0 :   }
    1172             : }
    1173             : 
    1174             : nsIWidget*
    1175           0 : nsPresContext::GetNearestWidget(nsPoint* aOffset)
    1176             : {
    1177           0 :   NS_ENSURE_TRUE(mShell, nullptr);
    1178           0 :   nsIFrame* frame = mShell->GetRootFrame();
    1179           0 :   NS_ENSURE_TRUE(frame, nullptr);
    1180           0 :   return frame->GetView()->GetNearestWidget(aOffset);
    1181             : }
    1182             : 
    1183             : nsIWidget*
    1184         268 : nsPresContext::GetRootWidget()
    1185             : {
    1186         268 :   NS_ENSURE_TRUE(mShell, nullptr);
    1187         268 :   nsViewManager* vm = mShell->GetViewManager();
    1188         268 :   if (!vm) {
    1189           0 :     return nullptr;
    1190             :   }
    1191         536 :   nsCOMPtr<nsIWidget> widget;
    1192         268 :   vm->GetRootWidget(getter_AddRefs(widget));
    1193         268 :   return widget.get();
    1194             : }
    1195             : 
    1196             : // We may want to replace this with something faster, maybe caching the root prescontext
    1197             : nsRootPresContext*
    1198       16204 : nsPresContext::GetRootPresContext()
    1199             : {
    1200       16204 :   nsPresContext* pc = this;
    1201             :   for (;;) {
    1202       16204 :     nsPresContext* parent = pc->GetParentPresContext();
    1203       16204 :     if (!parent)
    1204       16204 :       break;
    1205           0 :     pc = parent;
    1206           0 :   }
    1207       16204 :   return pc->IsRoot() ? static_cast<nsRootPresContext*>(pc) : nullptr;
    1208             : }
    1209             : 
    1210             : void
    1211          30 : nsPresContext::CompatibilityModeChanged()
    1212             : {
    1213          30 :   if (!mShell) {
    1214          24 :     return;
    1215             :   }
    1216             : 
    1217          30 :   nsIDocument* doc = mShell->GetDocument();
    1218          30 :   if (!doc) {
    1219           0 :     return;
    1220             :   }
    1221             : 
    1222          30 :   StyleSetHandle styleSet = mShell->StyleSet();
    1223          30 :   if (styleSet->IsServo()) {
    1224           0 :     styleSet->AsServo()->CompatibilityModeChanged();
    1225             :   }
    1226             : 
    1227          30 :   if (doc->IsSVGDocument()) {
    1228             :     // SVG documents never load quirk.css.
    1229          21 :     return;
    1230             :   }
    1231             : 
    1232           9 :   bool needsQuirkSheet = CompatibilityMode() == eCompatibility_NavQuirks;
    1233           9 :   if (mQuirkSheetAdded == needsQuirkSheet) {
    1234           3 :     return;
    1235             :   }
    1236             : 
    1237           6 :   auto cache = nsLayoutStylesheetCache::For(styleSet->BackendType());
    1238           6 :   StyleSheet* sheet = cache->QuirkSheet();
    1239             : 
    1240           6 :   if (needsQuirkSheet) {
    1241             :     // quirk.css needs to come after html.css; we just keep it at the end.
    1242             :     DebugOnly<nsresult> rv =
    1243          12 :       styleSet->AppendStyleSheet(SheetType::Agent, sheet);
    1244           6 :     NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "failed to insert quirk.css");
    1245             :   } else {
    1246             :     DebugOnly<nsresult> rv =
    1247           0 :       styleSet->RemoveStyleSheet(SheetType::Agent, sheet);
    1248           0 :     NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "failed to remove quirk.css");
    1249             :   }
    1250             : 
    1251           6 :   mQuirkSheetAdded = needsQuirkSheet;
    1252             : }
    1253             : 
    1254             : // Helper function for setting Anim Mode on image
    1255           0 : static void SetImgAnimModeOnImgReq(imgIRequest* aImgReq, uint16_t aMode)
    1256             : {
    1257           0 :   if (aImgReq) {
    1258           0 :     nsCOMPtr<imgIContainer> imgCon;
    1259           0 :     aImgReq->GetImage(getter_AddRefs(imgCon));
    1260           0 :     if (imgCon) {
    1261           0 :       imgCon->SetAnimationMode(aMode);
    1262             :     }
    1263             :   }
    1264           0 : }
    1265             : 
    1266             : // IMPORTANT: Assumption is that all images for a Presentation
    1267             : // have the same Animation Mode (pavlov said this was OK)
    1268             : //
    1269             : // Walks content and set the animation mode
    1270             : // this is a way to turn on/off image animations
    1271           0 : void nsPresContext::SetImgAnimations(nsIContent *aParent, uint16_t aMode)
    1272             : {
    1273           0 :   nsCOMPtr<nsIImageLoadingContent> imgContent(do_QueryInterface(aParent));
    1274           0 :   if (imgContent) {
    1275           0 :     nsCOMPtr<imgIRequest> imgReq;
    1276           0 :     imgContent->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
    1277           0 :                            getter_AddRefs(imgReq));
    1278           0 :     SetImgAnimModeOnImgReq(imgReq, aMode);
    1279             :   }
    1280             : 
    1281           0 :   uint32_t count = aParent->GetChildCount();
    1282           0 :   for (uint32_t i = 0; i < count; ++i) {
    1283           0 :     SetImgAnimations(aParent->GetChildAt(i), aMode);
    1284             :   }
    1285           0 : }
    1286             : 
    1287             : void
    1288           0 : nsPresContext::SetSMILAnimations(nsIDocument *aDoc, uint16_t aNewMode,
    1289             :                                  uint16_t aOldMode)
    1290             : {
    1291           0 :   if (aDoc->HasAnimationController()) {
    1292           0 :     nsSMILAnimationController* controller = aDoc->GetAnimationController();
    1293           0 :     switch (aNewMode)
    1294             :     {
    1295             :       case imgIContainer::kNormalAnimMode:
    1296             :       case imgIContainer::kLoopOnceAnimMode:
    1297           0 :         if (aOldMode == imgIContainer::kDontAnimMode)
    1298           0 :           controller->Resume(nsSMILTimeContainer::PAUSE_USERPREF);
    1299           0 :         break;
    1300             : 
    1301             :       case imgIContainer::kDontAnimMode:
    1302           0 :         if (aOldMode != imgIContainer::kDontAnimMode)
    1303           0 :           controller->Pause(nsSMILTimeContainer::PAUSE_USERPREF);
    1304           0 :         break;
    1305             :     }
    1306             :   }
    1307           0 : }
    1308             : 
    1309             : void
    1310           0 : nsPresContext::SetImageAnimationModeInternal(uint16_t aMode)
    1311             : {
    1312           0 :   NS_ASSERTION(aMode == imgIContainer::kNormalAnimMode ||
    1313             :                aMode == imgIContainer::kDontAnimMode ||
    1314             :                aMode == imgIContainer::kLoopOnceAnimMode, "Wrong Animation Mode is being set!");
    1315             : 
    1316             :   // Image animation mode cannot be changed when rendering to a printer.
    1317           0 :   if (!IsDynamic())
    1318           0 :     return;
    1319             : 
    1320             :   // Now walk the content tree and set the animation mode
    1321             :   // on all the images.
    1322           0 :   if (mShell != nullptr) {
    1323           0 :     nsIDocument *doc = mShell->GetDocument();
    1324           0 :     if (doc) {
    1325           0 :       doc->StyleImageLoader()->SetAnimationMode(aMode);
    1326             : 
    1327           0 :       Element *rootElement = doc->GetRootElement();
    1328           0 :       if (rootElement) {
    1329           0 :         SetImgAnimations(rootElement, aMode);
    1330             :       }
    1331           0 :       SetSMILAnimations(doc, aMode, mImageAnimationMode);
    1332             :     }
    1333             :   }
    1334             : 
    1335           0 :   mImageAnimationMode = aMode;
    1336             : }
    1337             : 
    1338             : void
    1339           0 : nsPresContext::SetImageAnimationModeExternal(uint16_t aMode)
    1340             : {
    1341           0 :   SetImageAnimationModeInternal(aMode);
    1342           0 : }
    1343             : 
    1344             : already_AddRefed<nsIAtom>
    1345         128 : nsPresContext::GetContentLanguage() const
    1346             : {
    1347         256 :   nsAutoString language;
    1348         128 :   Document()->GetContentLanguage(language);
    1349         128 :   language.StripWhitespace();
    1350             : 
    1351             :   // Content-Language may be a comma-separated list of language codes,
    1352             :   // in which case the HTML5 spec says to treat it as unknown
    1353         128 :   if (!language.IsEmpty() &&
    1354           0 :       !language.Contains(char16_t(','))) {
    1355           0 :     return NS_Atomize(language);
    1356             :     // NOTE:  This does *not* count as an explicit language; in other
    1357             :     // words, it doesn't trigger language-specific hyphenation.
    1358             :   }
    1359         128 :   return nullptr;
    1360             : }
    1361             : 
    1362             : void
    1363           0 : nsPresContext::UpdateEffectiveTextZoom()
    1364             : {
    1365           0 :   float newZoom = mSystemFontScale * mTextZoom;
    1366           0 :   float minZoom = nsLayoutUtils::MinZoom();
    1367           0 :   float maxZoom = nsLayoutUtils::MaxZoom();
    1368             : 
    1369           0 :   if (newZoom < minZoom) {
    1370           0 :     newZoom = minZoom;
    1371           0 :   } else if (newZoom > maxZoom) {
    1372           0 :     newZoom = maxZoom;
    1373             :   }
    1374             : 
    1375           0 :   mEffectiveTextZoom = newZoom;
    1376             : 
    1377           0 :   if (HasCachedStyleData()) {
    1378             :     // Media queries could have changed, since we changed the meaning
    1379             :     // of 'em' units in them.
    1380           0 :     MediaFeatureValuesChanged(eRestyle_ForceDescendants,
    1381           0 :                               NS_STYLE_HINT_REFLOW);
    1382             :   }
    1383           0 : }
    1384             : 
    1385             : void
    1386          33 : nsPresContext::SetFullZoom(float aZoom)
    1387             : {
    1388          33 :   if (!mShell || mFullZoom == aZoom) {
    1389          33 :     return;
    1390             :   }
    1391             : 
    1392             :   // Re-fetch the view manager's window dimensions in case there's a deferred
    1393             :   // resize which hasn't affected our mVisibleArea yet
    1394             :   nscoord oldWidthAppUnits, oldHeightAppUnits;
    1395           0 :   mShell->GetViewManager()->GetWindowDimensions(&oldWidthAppUnits, &oldHeightAppUnits);
    1396           0 :   float oldWidthDevPixels = oldWidthAppUnits / float(mCurAppUnitsPerDevPixel);
    1397           0 :   float oldHeightDevPixels = oldHeightAppUnits / float(mCurAppUnitsPerDevPixel);
    1398           0 :   mDeviceContext->SetFullZoom(aZoom);
    1399             : 
    1400           0 :   NS_ASSERTION(!mSuppressResizeReflow, "two zooms happening at the same time? impossible!");
    1401           0 :   mSuppressResizeReflow = true;
    1402             : 
    1403           0 :   mFullZoom = aZoom;
    1404           0 :   mShell->GetViewManager()->
    1405           0 :     SetWindowDimensions(NSToCoordRound(oldWidthDevPixels * AppUnitsPerDevPixel()),
    1406           0 :                         NSToCoordRound(oldHeightDevPixels * AppUnitsPerDevPixel()));
    1407             : 
    1408           0 :   AppUnitsPerDevPixelChanged();
    1409             : 
    1410           0 :   mSuppressResizeReflow = false;
    1411             : }
    1412             : 
    1413             : void
    1414          31 : nsPresContext::SetOverrideDPPX(float aDPPX)
    1415             : {
    1416          31 :   mOverrideDPPX = aDPPX;
    1417             : 
    1418          31 :   if (HasCachedStyleData()) {
    1419           0 :     MediaFeatureValuesChanged(nsRestyleHint(0), nsChangeHint(0));
    1420             :   }
    1421          31 : }
    1422             : 
    1423             : gfxSize
    1424           0 : nsPresContext::ScreenSizeInchesForFontInflation(bool* aChanged)
    1425             : {
    1426           0 :   if (aChanged) {
    1427           0 :     *aChanged = false;
    1428             :   }
    1429             : 
    1430           0 :   nsDeviceContext *dx = DeviceContext();
    1431           0 :   nsRect clientRect;
    1432           0 :   dx->GetClientRect(clientRect); // FIXME: GetClientRect looks expensive
    1433           0 :   float unitsPerInch = dx->AppUnitsPerPhysicalInch();
    1434           0 :   gfxSize deviceSizeInches(float(clientRect.width) / unitsPerInch,
    1435           0 :                            float(clientRect.height) / unitsPerInch);
    1436             : 
    1437           0 :   if (mLastFontInflationScreenSize == gfxSize(-1.0, -1.0)) {
    1438           0 :     mLastFontInflationScreenSize = deviceSizeInches;
    1439             :   }
    1440             : 
    1441           0 :   if (deviceSizeInches != mLastFontInflationScreenSize && aChanged) {
    1442           0 :     *aChanged = true;
    1443           0 :     mLastFontInflationScreenSize = deviceSizeInches;
    1444             :   }
    1445             : 
    1446           0 :   return deviceSizeInches;
    1447             : }
    1448             : 
    1449             : static bool
    1450          30 : CheckOverflow(const nsStyleDisplay* aDisplay, ScrollbarStyles* aStyles)
    1451             : {
    1452         120 :   if (aDisplay->mOverflowX == NS_STYLE_OVERFLOW_VISIBLE &&
    1453          58 :       aDisplay->mScrollBehavior == NS_STYLE_SCROLL_BEHAVIOR_AUTO &&
    1454          58 :       aDisplay->mScrollSnapTypeX == NS_STYLE_SCROLL_SNAP_TYPE_NONE &&
    1455          87 :       aDisplay->mScrollSnapTypeY == NS_STYLE_SCROLL_SNAP_TYPE_NONE &&
    1456         175 :       aDisplay->mScrollSnapPointsX == nsStyleCoord(eStyleUnit_None) &&
    1457         117 :       aDisplay->mScrollSnapPointsY == nsStyleCoord(eStyleUnit_None) &&
    1458          58 :       !aDisplay->mScrollSnapDestination.mXPosition.mHasPercent &&
    1459          58 :       !aDisplay->mScrollSnapDestination.mYPosition.mHasPercent &&
    1460         118 :       aDisplay->mScrollSnapDestination.mXPosition.mLength == 0 &&
    1461          29 :       aDisplay->mScrollSnapDestination.mYPosition.mLength == 0) {
    1462          29 :     return false;
    1463             :   }
    1464             : 
    1465           1 :   if (aDisplay->mOverflowX == NS_STYLE_OVERFLOW_CLIP) {
    1466           2 :     *aStyles = ScrollbarStyles(NS_STYLE_OVERFLOW_HIDDEN,
    1467           1 :                                NS_STYLE_OVERFLOW_HIDDEN, aDisplay);
    1468             :   } else {
    1469           0 :     *aStyles = ScrollbarStyles(aDisplay);
    1470             :   }
    1471           1 :   return true;
    1472             : }
    1473             : 
    1474             : static nsIContent*
    1475          26 : GetPropagatedScrollbarStylesForViewport(nsPresContext* aPresContext,
    1476             :                                         ScrollbarStyles *aStyles)
    1477             : {
    1478          26 :   nsIDocument* document = aPresContext->Document();
    1479          26 :   Element* docElement = document->GetRootElement();
    1480             : 
    1481             :   // docElement might be null if we're doing this after removing it.
    1482          26 :   if (!docElement) {
    1483           0 :     return nullptr;
    1484             :   }
    1485             : 
    1486             :   // Check the style on the document root element
    1487          26 :   StyleSetHandle styleSet = aPresContext->StyleSet();
    1488             :   RefPtr<nsStyleContext> rootStyle =
    1489          52 :     styleSet->ResolveStyleFor(docElement, nullptr, LazyComputeBehavior::Allow);
    1490          26 :   if (CheckOverflow(rootStyle->StyleDisplay(), aStyles)) {
    1491             :     // tell caller we stole the overflow style from the root element
    1492           1 :     return docElement;
    1493             :   }
    1494             : 
    1495             :   // Don't look in the BODY for non-HTML documents or HTML documents
    1496             :   // with non-HTML roots
    1497             :   // XXX this should be earlier; we shouldn't even look at the document root
    1498             :   // for non-HTML documents. Fix this once we support explicit CSS styling
    1499             :   // of the viewport
    1500             :   // XXX what about XHTML?
    1501          50 :   nsCOMPtr<nsIDOMHTMLDocument> htmlDoc(do_QueryInterface(document));
    1502          25 :   if (!htmlDoc || !docElement->IsHTMLElement()) {
    1503          21 :     return nullptr;
    1504             :   }
    1505             : 
    1506           8 :   nsCOMPtr<nsIDOMHTMLElement> body;
    1507           4 :   htmlDoc->GetBody(getter_AddRefs(body));
    1508           8 :   nsCOMPtr<nsIContent> bodyElement = do_QueryInterface(body);
    1509             : 
    1510           8 :   if (!bodyElement ||
    1511           4 :       !bodyElement->NodeInfo()->Equals(nsGkAtoms::body)) {
    1512             :     // The body is not a <body> tag, it's a <frameset>.
    1513           0 :     return nullptr;
    1514             :   }
    1515             : 
    1516             :   RefPtr<nsStyleContext> bodyStyle =
    1517           8 :     styleSet->ResolveStyleFor(bodyElement->AsElement(), rootStyle,
    1518           8 :                               LazyComputeBehavior::Allow);
    1519             : 
    1520           4 :   if (CheckOverflow(bodyStyle->StyleDisplay(), aStyles)) {
    1521             :     // tell caller we stole the overflow style from the body element
    1522           0 :     return bodyElement;
    1523             :   }
    1524             : 
    1525           4 :   return nullptr;
    1526             : }
    1527             : 
    1528             : nsIContent*
    1529          26 : nsPresContext::UpdateViewportScrollbarStylesOverride()
    1530             : {
    1531             :   // Start off with our default styles, and then update them as needed.
    1532          52 :   mViewportStyleScrollbar = ScrollbarStyles(NS_STYLE_OVERFLOW_AUTO,
    1533          26 :                                             NS_STYLE_OVERFLOW_AUTO);
    1534          26 :   mViewportScrollbarOverrideNode = nullptr;
    1535             :   // Don't propagate the scrollbar state in printing or print preview.
    1536          26 :   if (!IsPaginated()) {
    1537          26 :     mViewportScrollbarOverrideNode =
    1538          26 :       GetPropagatedScrollbarStylesForViewport(this, &mViewportStyleScrollbar);
    1539             :   }
    1540             : 
    1541          26 :   nsIDocument* document = Document();
    1542          26 :   if (Element* fullscreenElement = document->GetFullscreenElement()) {
    1543             :     // If the document is in fullscreen, but the fullscreen element is
    1544             :     // not the root element, we should explicitly suppress the scrollbar
    1545             :     // here. Note that, we still need to return the original element
    1546             :     // the styles are from, so that the state of those elements is not
    1547             :     // affected across fullscreen change.
    1548           0 :     if (fullscreenElement != document->GetRootElement() &&
    1549           0 :         fullscreenElement != mViewportScrollbarOverrideNode) {
    1550           0 :       mViewportStyleScrollbar = ScrollbarStyles(NS_STYLE_OVERFLOW_HIDDEN,
    1551           0 :                                                 NS_STYLE_OVERFLOW_HIDDEN);
    1552             :     }
    1553             :   }
    1554          26 :   return mViewportScrollbarOverrideNode;
    1555             : }
    1556             : 
    1557             : bool
    1558           0 : nsPresContext::ElementWouldPropagateScrollbarStyles(Element* aElement)
    1559             : {
    1560           0 :   MOZ_ASSERT(IsPaginated(), "Should only be called on paginated contexts");
    1561           0 :   if (aElement->GetParent() && !aElement->IsHTMLElement(nsGkAtoms::body)) {
    1562             :     // We certainly won't be propagating from this element.
    1563           0 :     return false;
    1564             :   }
    1565             : 
    1566             :   // Go ahead and just call GetPropagatedScrollbarStylesForViewport, but update
    1567             :   // a dummy ScrollbarStyles we don't care about.  It'll do a bit of extra work,
    1568             :   // but saves us having to have more complicated code or more code duplication;
    1569             :   // in practice we will make this call quite rarely, because we checked for all
    1570             :   // the common cases above.
    1571           0 :   ScrollbarStyles dummy(NS_STYLE_OVERFLOW_AUTO, NS_STYLE_OVERFLOW_AUTO);
    1572           0 :   return GetPropagatedScrollbarStylesForViewport(this, &dummy) == aElement;
    1573             : }
    1574             : 
    1575             : void
    1576          11 : nsPresContext::SetContainer(nsIDocShell* aDocShell)
    1577             : {
    1578          11 :   if (aDocShell) {
    1579           7 :     NS_ASSERTION(!(mContainer && mNeedsPrefUpdate),
    1580             :                  "Should only need pref update if mContainer is null.");
    1581           7 :     mContainer = static_cast<nsDocShell*>(aDocShell);
    1582           7 :     if (mNeedsPrefUpdate) {
    1583           0 :       if (!mPrefChangedTimer) {
    1584           0 :         mPrefChangedTimer = CreateTimer(PrefChangedUpdateTimerCallback,
    1585           0 :                                         "PrefChangedUpdateTimerCallback", 0);
    1586             :       }
    1587           0 :       mNeedsPrefUpdate = false;
    1588             :     }
    1589             :   } else {
    1590           4 :     mContainer = WeakPtr<nsDocShell>();
    1591             :   }
    1592          11 :   UpdateIsChrome();
    1593          11 :   if (mContainer) {
    1594           7 :     GetDocumentColorPreferences();
    1595             :   }
    1596          11 : }
    1597             : 
    1598             : nsISupports*
    1599         443 : nsPresContext::GetContainerWeakInternal() const
    1600             : {
    1601         443 :   return static_cast<nsIDocShell*>(mContainer);
    1602             : }
    1603             : 
    1604             : nsISupports*
    1605           0 : nsPresContext::GetContainerWeakExternal() const
    1606             : {
    1607           0 :   return GetContainerWeakInternal();
    1608             : }
    1609             : 
    1610             : nsIDocShell*
    1611         746 : nsPresContext::GetDocShell() const
    1612             : {
    1613         746 :   return mContainer;
    1614             : }
    1615             : 
    1616             : /* virtual */ void
    1617           4 : nsPresContext::Detach()
    1618             : {
    1619           4 :   SetContainer(nullptr);
    1620           4 :   SetLinkHandler(nullptr);
    1621           4 : }
    1622             : 
    1623             : bool
    1624           0 : nsPresContext::BidiEnabledExternal() const
    1625             : {
    1626           0 :   return BidiEnabledInternal();
    1627             : }
    1628             : 
    1629             : bool
    1630         386 : nsPresContext::BidiEnabledInternal() const
    1631             : {
    1632         386 :   return Document()->GetBidiEnabled();
    1633             : }
    1634             : 
    1635             : void
    1636           0 : nsPresContext::SetBidiEnabled() const
    1637             : {
    1638           0 :   if (mShell) {
    1639           0 :     nsIDocument *doc = mShell->GetDocument();
    1640           0 :     if (doc) {
    1641           0 :       doc->SetBidiEnabled();
    1642             :     }
    1643             :   }
    1644           0 : }
    1645             : 
    1646             : void
    1647          28 : nsPresContext::SetBidi(uint32_t aSource, bool aForceRestyle)
    1648             : {
    1649             :   // Don't do all this stuff unless the options have changed.
    1650          28 :   if (aSource == GetBidi()) {
    1651          28 :     return;
    1652             :   }
    1653             : 
    1654           0 :   NS_ASSERTION(!(aForceRestyle && (GetBidi() == 0)),
    1655             :                "ForceReflow on new prescontext");
    1656             : 
    1657           0 :   Document()->SetBidiOptions(aSource);
    1658           0 :   if (IBMBIDI_TEXTDIRECTION_RTL == GET_BIDI_OPTION_DIRECTION(aSource)
    1659           0 :       || IBMBIDI_NUMERAL_HINDI == GET_BIDI_OPTION_NUMERAL(aSource)) {
    1660           0 :     SetBidiEnabled();
    1661             :   }
    1662           0 :   if (IBMBIDI_TEXTTYPE_VISUAL == GET_BIDI_OPTION_TEXTTYPE(aSource)) {
    1663           0 :     SetVisualMode(true);
    1664             :   }
    1665           0 :   else if (IBMBIDI_TEXTTYPE_LOGICAL == GET_BIDI_OPTION_TEXTTYPE(aSource)) {
    1666           0 :     SetVisualMode(false);
    1667             :   }
    1668             :   else {
    1669           0 :     nsIDocument* doc = mShell->GetDocument();
    1670           0 :     if (doc) {
    1671           0 :       SetVisualMode(IsVisualCharset(doc->GetDocumentCharacterSet()));
    1672             :     }
    1673             :   }
    1674           0 :   if (aForceRestyle && mShell) {
    1675             :     // Reconstruct the root document element's frame and its children,
    1676             :     // because we need to trigger frame reconstruction for direction change.
    1677           0 :     mDocument->RebuildUserFontSet();
    1678           0 :     mShell->ReconstructFrames();
    1679             :   }
    1680             : }
    1681             : 
    1682             : uint32_t
    1683         357 : nsPresContext::GetBidi() const
    1684             : {
    1685         357 :   return Document()->GetBidiOptions();
    1686             : }
    1687             : 
    1688             : bool
    1689           2 : nsPresContext::IsTopLevelWindowInactive()
    1690             : {
    1691           4 :   nsCOMPtr<nsIDocShellTreeItem> treeItem(mContainer);
    1692           2 :   if (!treeItem)
    1693           0 :     return false;
    1694             : 
    1695           4 :   nsCOMPtr<nsIDocShellTreeItem> rootItem;
    1696           2 :   treeItem->GetRootTreeItem(getter_AddRefs(rootItem));
    1697           2 :   if (!rootItem) {
    1698           0 :     return false;
    1699             :   }
    1700             : 
    1701           4 :   nsCOMPtr<nsPIDOMWindowOuter> domWindow = rootItem->GetWindow();
    1702             : 
    1703           2 :   return domWindow && !domWindow->IsActive();
    1704             : }
    1705             : 
    1706             : void
    1707           4 : nsPresContext::RecordInteractionTime(InteractionType aType,
    1708             :                                      const TimeStamp& aTimeStamp)
    1709             : {
    1710           4 :   if (!mInteractionTimeEnabled || aTimeStamp.IsNull()) {
    1711           4 :     return;
    1712             :   }
    1713             : 
    1714             :   // Array of references to the member variable of each time stamp
    1715             :   // for the different interaction types, keyed by InteractionType.
    1716             :   TimeStamp nsPresContext::*interactionTimes[] = {
    1717             :     &nsPresContext::mFirstClickTime,
    1718             :     &nsPresContext::mFirstKeyTime,
    1719             :     &nsPresContext::mFirstMouseMoveTime,
    1720             :     &nsPresContext::mFirstScrollTime
    1721           4 :   };
    1722             : 
    1723             :   // Array of histogram IDs for the different interaction types,
    1724             :   // keyed by InteractionType.
    1725             :   Telemetry::HistogramID histogramIds[] = {
    1726             :     Telemetry::TIME_TO_FIRST_CLICK_MS,
    1727             :     Telemetry::TIME_TO_FIRST_KEY_INPUT_MS,
    1728             :     Telemetry::TIME_TO_FIRST_MOUSE_MOVE_MS,
    1729             :     Telemetry::TIME_TO_FIRST_SCROLL_MS
    1730           4 :   };
    1731             : 
    1732             :   TimeStamp& interactionTime = this->*(
    1733           4 :     interactionTimes[static_cast<uint32_t>(aType)]);
    1734           4 :   if (!interactionTime.IsNull()) {
    1735             :     // We have already recorded an interaction time.
    1736           3 :     return;
    1737             :   }
    1738             : 
    1739             :   // Record the interaction time if it occurs after the first paint
    1740             :   // of the top level content document.
    1741             :   nsPresContext* topContentPresContext =
    1742           1 :     GetToplevelContentDocumentPresContext();
    1743             : 
    1744           1 :   if (!topContentPresContext) {
    1745             :     // There is no top content pres context so we don't care
    1746             :     // about the interaction time. Record a value anyways to avoid
    1747             :     // trying to find the top content pres context in future interactions.
    1748           1 :     interactionTime = TimeStamp::Now();
    1749           1 :     return;
    1750             :   }
    1751             : 
    1752           0 :   if (topContentPresContext->mFirstNonBlankPaintTime.IsNull() ||
    1753           0 :       topContentPresContext->mFirstNonBlankPaintTime > aTimeStamp) {
    1754             :     // Top content pres context has not had a non-blank paint yet
    1755             :     // or the event timestamp is before the first non-blank paint,
    1756             :     // so don't record interaction time.
    1757           0 :     return;
    1758             :   }
    1759             : 
    1760             :   // Check if we are recording the first of any of the interaction types.
    1761           0 :   bool isFirstInteraction = true;
    1762           0 :   for (TimeStamp nsPresContext::* memberPtr : interactionTimes) {
    1763           0 :     TimeStamp& timeStamp = this->*(memberPtr);
    1764           0 :     if (!timeStamp.IsNull()) {
    1765           0 :       isFirstInteraction = false;
    1766           0 :       break;
    1767             :     }
    1768             :   }
    1769             : 
    1770           0 :   interactionTime = TimeStamp::Now();
    1771             :   // Only the top level content pres context reports first interaction
    1772             :   // time to telemetry (if it hasn't already done so).
    1773           0 :   if (this == topContentPresContext) {
    1774           0 :     if (Telemetry::CanRecordExtended()) {
    1775             :        double millis =
    1776           0 :          (interactionTime - mFirstNonBlankPaintTime).ToMilliseconds();
    1777           0 :        Telemetry::Accumulate(histogramIds[static_cast<uint32_t>(aType)],
    1778           0 :                              millis);
    1779             : 
    1780           0 :        if (isFirstInteraction) {
    1781           0 :          Telemetry::Accumulate(Telemetry::TIME_TO_FIRST_INTERACTION_MS, millis);
    1782             :        }
    1783             :     }
    1784             :   } else {
    1785           0 :     topContentPresContext->RecordInteractionTime(aType, aTimeStamp);
    1786             :   }
    1787             : }
    1788             : 
    1789             : nsITheme*
    1790        1658 : nsPresContext::GetTheme()
    1791             : {
    1792        1658 :   if (!sNoTheme && !mTheme) {
    1793           3 :     mTheme = do_GetService("@mozilla.org/chrome/chrome-native-theme;1");
    1794           3 :     if (!mTheme)
    1795           0 :       sNoTheme = true;
    1796             :   }
    1797             : 
    1798        1658 :   return mTheme;
    1799             : }
    1800             : 
    1801             : void
    1802           0 : nsPresContext::ThemeChanged()
    1803             : {
    1804           0 :   if (!mPendingThemeChanged) {
    1805           0 :     sLookAndFeelChanged = true;
    1806           0 :     sThemeChanged = true;
    1807             : 
    1808             :     nsCOMPtr<nsIRunnable> ev =
    1809           0 :       NewRunnableMethod("nsPresContext::ThemeChangedInternal",
    1810             :                         this,
    1811           0 :                         &nsPresContext::ThemeChangedInternal);
    1812           0 :     nsresult rv = Document()->Dispatch("nsPresContext::ThemeChangedInternal",
    1813             :                                        TaskCategory::Other,
    1814           0 :                                        ev.forget());
    1815           0 :     if (NS_SUCCEEDED(rv)) {
    1816           0 :       mPendingThemeChanged = true;
    1817             :     }
    1818             :   }
    1819           0 : }
    1820             : 
    1821             : static bool
    1822           0 : NotifyThemeChanged(TabParent* aTabParent, void* aArg)
    1823             : {
    1824           0 :   aTabParent->ThemeChanged();
    1825           0 :   return false;
    1826             : }
    1827             : 
    1828             : void
    1829           0 : nsPresContext::ThemeChangedInternal()
    1830             : {
    1831           0 :   mPendingThemeChanged = false;
    1832             : 
    1833             :   // Tell the theme that it changed, so it can flush any handles to stale theme
    1834             :   // data.
    1835           0 :   if (mTheme && sThemeChanged) {
    1836           0 :     mTheme->ThemeChanged();
    1837           0 :     sThemeChanged = false;
    1838             :   }
    1839             : 
    1840           0 :   if (sLookAndFeelChanged) {
    1841             :     // Clear all cached LookAndFeel colors.
    1842           0 :     LookAndFeel::Refresh();
    1843           0 :     sLookAndFeelChanged = false;
    1844             : 
    1845             :     // Vector images (SVG) may be using theme colors so we discard all cached
    1846             :     // surfaces. (We could add a vector image only version of DiscardAll, but
    1847             :     // in bug 940625 we decided theme changes are rare enough not to bother.)
    1848           0 :     image::SurfaceCacheUtils::DiscardAll();
    1849             :   }
    1850             : 
    1851             :   // This will force the system metrics to be generated the next time they're used
    1852           0 :   nsCSSRuleProcessor::FreeSystemMetrics();
    1853             : 
    1854             :   // Changes to system metrics can change media queries on them, or
    1855             :   // :-moz-system-metric selectors (which requires eRestyle_Subtree).
    1856             :   // Changes in theme can change system colors (whose changes are
    1857             :   // properly reflected in computed style data), system fonts (whose
    1858             :   // changes are not), and -moz-appearance (whose changes likewise are
    1859             :   // not), so we need to reflow.
    1860           0 :   MediaFeatureValuesChanged(eRestyle_Subtree, NS_STYLE_HINT_REFLOW);
    1861             : 
    1862             :   // Recursively notify all remote leaf descendants that the
    1863             :   // system theme has changed.
    1864           0 :   nsContentUtils::CallOnAllRemoteChildren(mDocument->GetWindow(),
    1865           0 :                                           NotifyThemeChanged, nullptr);
    1866           0 : }
    1867             : 
    1868             : void
    1869           0 : nsPresContext::SysColorChanged()
    1870             : {
    1871           0 :   if (!mPendingSysColorChanged) {
    1872           0 :     sLookAndFeelChanged = true;
    1873             :     nsCOMPtr<nsIRunnable> ev =
    1874           0 :       NewRunnableMethod("nsPresContext::SysColorChangedInternal",
    1875             :                         this,
    1876           0 :                         &nsPresContext::SysColorChangedInternal);
    1877           0 :     nsresult rv = Document()->Dispatch("nsPresContext::SysColorChangedInternal",
    1878             :                                        TaskCategory::Other,
    1879           0 :                                        ev.forget());
    1880           0 :     if (NS_SUCCEEDED(rv)) {
    1881           0 :       mPendingSysColorChanged = true;
    1882             :     }
    1883             :   }
    1884           0 : }
    1885             : 
    1886             : void
    1887           0 : nsPresContext::SysColorChangedInternal()
    1888             : {
    1889           0 :   mPendingSysColorChanged = false;
    1890             : 
    1891           0 :   if (sLookAndFeelChanged) {
    1892             :      // Don't use the cached values for the system colors
    1893           0 :     LookAndFeel::Refresh();
    1894           0 :     sLookAndFeelChanged = false;
    1895             :   }
    1896             : 
    1897             :   // Invalidate cached '-moz-windows-accent-color-applies' media query:
    1898           0 :   nsCSSRuleProcessor::FreeSystemMetrics();
    1899             : 
    1900             :   // Reset default background and foreground colors for the document since
    1901             :   // they may be using system colors
    1902           0 :   GetDocumentColorPreferences();
    1903             : 
    1904             :   // The system color values are computed to colors in the style data,
    1905             :   // so normal style data comparison is sufficient here.
    1906           0 :   RebuildAllStyleData(nsChangeHint(0), nsRestyleHint(0));
    1907           0 : }
    1908             : 
    1909             : void
    1910           1 : nsPresContext::UIResolutionChanged()
    1911             : {
    1912           1 :   if (!mPendingUIResolutionChanged) {
    1913             :     nsCOMPtr<nsIRunnable> ev =
    1914           2 :       NewRunnableMethod("nsPresContext::UIResolutionChangedInternal",
    1915             :                         this,
    1916           2 :                         &nsPresContext::UIResolutionChangedInternal);
    1917             :     nsresult rv =
    1918           1 :       Document()->Dispatch("nsPresContext::UIResolutionChangedInternal",
    1919             :                            TaskCategory::Other,
    1920           1 :                            ev.forget());
    1921           1 :     if (NS_SUCCEEDED(rv)) {
    1922           1 :       mPendingUIResolutionChanged = true;
    1923             :     }
    1924             :   }
    1925           1 : }
    1926             : 
    1927             : void
    1928           0 : nsPresContext::UIResolutionChangedSync()
    1929             : {
    1930           0 :   if (!mPendingUIResolutionChanged) {
    1931           0 :     mPendingUIResolutionChanged = true;
    1932           0 :     UIResolutionChangedInternalScale(0.0);
    1933             :   }
    1934           0 : }
    1935             : 
    1936             : /*static*/ bool
    1937           0 : nsPresContext::UIResolutionChangedSubdocumentCallback(nsIDocument* aDocument,
    1938             :                                                       void* aData)
    1939             : {
    1940           0 :   nsIPresShell* shell = aDocument->GetShell();
    1941           0 :   if (shell) {
    1942           0 :     nsPresContext* pc = shell->GetPresContext();
    1943           0 :     if (pc) {
    1944             :       // For subdocuments, we want to apply the parent's scale, because there
    1945             :       // are cases where the subdoc's device context is connected to a widget
    1946             :       // that has an out-of-date resolution (it's on a different screen, but
    1947             :       // currently hidden, and will not be updated until shown): bug 1249279.
    1948           0 :       double scale = *static_cast<double*>(aData);
    1949           0 :       pc->UIResolutionChangedInternalScale(scale);
    1950             :     }
    1951             :   }
    1952           0 :   return true;
    1953             : }
    1954             : 
    1955             : static void
    1956           0 : NotifyTabUIResolutionChanged(TabParent* aTab, void *aArg)
    1957             : {
    1958           0 :   aTab->UIResolutionChanged();
    1959           0 : }
    1960             : 
    1961             : static void
    1962           1 : NotifyChildrenUIResolutionChanged(nsPIDOMWindowOuter* aWindow)
    1963             : {
    1964           2 :   nsCOMPtr<nsIDocument> doc = aWindow->GetExtantDoc();
    1965           2 :   RefPtr<nsPIWindowRoot> topLevelWin = nsContentUtils::GetWindowRoot(doc);
    1966           1 :   if (!topLevelWin) {
    1967           0 :     return;
    1968             :   }
    1969           1 :   topLevelWin->EnumerateBrowsers(NotifyTabUIResolutionChanged, nullptr);
    1970             : }
    1971             : 
    1972             : void
    1973           1 : nsPresContext::UIResolutionChangedInternal()
    1974             : {
    1975           1 :   UIResolutionChangedInternalScale(0.0);
    1976           1 : }
    1977             : 
    1978             : void
    1979           1 : nsPresContext::UIResolutionChangedInternalScale(double aScale)
    1980             : {
    1981           1 :   mPendingUIResolutionChanged = false;
    1982             : 
    1983           1 :   mDeviceContext->CheckDPIChange(&aScale);
    1984           1 :   if (mCurAppUnitsPerDevPixel != AppUnitsPerDevPixel()) {
    1985           1 :     AppUnitsPerDevPixelChanged();
    1986             :   }
    1987             : 
    1988             :   // Recursively notify all remote leaf descendants of the change.
    1989           1 :   if (nsPIDOMWindowOuter* window = mDocument->GetWindow()) {
    1990           1 :     NotifyChildrenUIResolutionChanged(window);
    1991             :   }
    1992             : 
    1993           1 :   mDocument->EnumerateSubDocuments(UIResolutionChangedSubdocumentCallback,
    1994           1 :                                    &aScale);
    1995           1 : }
    1996             : 
    1997             : void
    1998           0 : nsPresContext::EmulateMedium(const nsAString& aMediaType)
    1999             : {
    2000           0 :   nsIAtom* previousMedium = Medium();
    2001           0 :   mIsEmulatingMedia = true;
    2002             : 
    2003           0 :   nsAutoString mediaType;
    2004           0 :   nsContentUtils::ASCIIToLower(aMediaType, mediaType);
    2005             : 
    2006           0 :   mMediaEmulated = NS_Atomize(mediaType);
    2007           0 :   if (mMediaEmulated != previousMedium && mShell) {
    2008           0 :     MediaFeatureValuesChanged(nsRestyleHint(0), nsChangeHint(0));
    2009             :   }
    2010           0 : }
    2011             : 
    2012           0 : void nsPresContext::StopEmulatingMedium()
    2013             : {
    2014           0 :   nsIAtom* previousMedium = Medium();
    2015           0 :   mIsEmulatingMedia = false;
    2016           0 :   if (Medium() != previousMedium) {
    2017           0 :     MediaFeatureValuesChanged(nsRestyleHint(0), nsChangeHint(0));
    2018             :   }
    2019           0 : }
    2020             : 
    2021             : void
    2022           0 : nsPresContext::ForceCacheLang(nsIAtom *aLanguage)
    2023             : {
    2024             :   // force it to be cached
    2025           0 :   GetDefaultFont(kPresContext_DefaultVariableFont_ID, aLanguage);
    2026           0 :   mLanguagesUsed.PutEntry(aLanguage);
    2027           0 : }
    2028             : 
    2029             : void
    2030           0 : nsPresContext::CacheAllLangs()
    2031             : {
    2032           0 :   if (mFontGroupCacheDirty) {
    2033           0 :     nsCOMPtr<nsIAtom> thisLang = nsStyleFont::GetLanguage(this);
    2034           0 :     GetDefaultFont(kPresContext_DefaultVariableFont_ID, thisLang.get());
    2035           0 :     GetDefaultFont(kPresContext_DefaultVariableFont_ID, nsGkAtoms::x_math);
    2036             :     // https://bugzilla.mozilla.org/show_bug.cgi?id=1362599#c12
    2037           0 :     GetDefaultFont(kPresContext_DefaultVariableFont_ID, nsGkAtoms::Unicode);
    2038           0 :     for (auto iter = mLanguagesUsed.Iter(); !iter.Done(); iter.Next()) {
    2039             : 
    2040           0 :       GetDefaultFont(kPresContext_DefaultVariableFont_ID, iter.Get()->GetKey());
    2041             :     }
    2042             :   }
    2043           0 :   mFontGroupCacheDirty = false;
    2044           0 : }
    2045             : 
    2046             : void
    2047           3 : nsPresContext::RebuildAllStyleData(nsChangeHint aExtraHint,
    2048             :                                    nsRestyleHint aRestyleHint)
    2049             : {
    2050           3 :   if (!mShell) {
    2051             :     // We must have been torn down. Nothing to do here.
    2052           0 :     return;
    2053             :   }
    2054             : 
    2055           3 :   mUsesRootEMUnits = false;
    2056           3 :   mUsesExChUnits = false;
    2057           3 :   mUsesViewportUnits = false;
    2058           3 :   mDocument->RebuildUserFontSet();
    2059           3 :   RebuildCounterStyles();
    2060             : 
    2061           3 :   RestyleManager()->RebuildAllStyleData(aExtraHint, aRestyleHint);
    2062             : }
    2063             : 
    2064             : void
    2065           0 : nsPresContext::PostRebuildAllStyleDataEvent(nsChangeHint aExtraHint,
    2066             :                                             nsRestyleHint aRestyleHint)
    2067             : {
    2068           0 :   if (!mShell) {
    2069             :     // We must have been torn down. Nothing to do here.
    2070           0 :     return;
    2071             :   }
    2072           0 :   RestyleManager()->PostRebuildAllStyleDataEvent(aExtraHint, aRestyleHint);
    2073             : }
    2074             : 
    2075             : struct MediaFeatureHints
    2076             : {
    2077             :   nsRestyleHint restyleHint;
    2078             :   nsChangeHint changeHint;
    2079             : };
    2080             : 
    2081             : static bool
    2082           1 : MediaFeatureValuesChangedAllDocumentsCallback(nsIDocument* aDocument, void* aHints)
    2083             : {
    2084           1 :   MediaFeatureHints* hints = static_cast<MediaFeatureHints*>(aHints);
    2085           1 :   if (nsIPresShell* shell = aDocument->GetShell()) {
    2086           0 :     if (nsPresContext* pc = shell->GetPresContext()) {
    2087           0 :       pc->MediaFeatureValuesChangedAllDocuments(hints->restyleHint,
    2088           0 :                                                 hints->changeHint);
    2089             :     }
    2090             :   }
    2091           1 :   return true;
    2092             : }
    2093             : 
    2094             : void
    2095           1 : nsPresContext::MediaFeatureValuesChangedAllDocuments(nsRestyleHint aRestyleHint,
    2096             :                                                      nsChangeHint aChangeHint)
    2097             : {
    2098           1 :     MediaFeatureValuesChanged(aRestyleHint, aChangeHint);
    2099             :     MediaFeatureHints hints = {
    2100             :       aRestyleHint,
    2101             :       aChangeHint
    2102           1 :     };
    2103             : 
    2104           1 :     mDocument->EnumerateSubDocuments(MediaFeatureValuesChangedAllDocumentsCallback,
    2105           1 :                                      &hints);
    2106           1 : }
    2107             : 
    2108             : void
    2109          21 : nsPresContext::MediaFeatureValuesChanged(nsRestyleHint aRestyleHint,
    2110             :                                          nsChangeHint aChangeHint)
    2111             : {
    2112          21 :   mPendingMediaFeatureValuesChanged = false;
    2113             : 
    2114             :   // MediumFeaturesChanged updates the applied rules, so it always gets called.
    2115          21 :   if (mShell && mShell->StyleSet()->MediumFeaturesChanged()) {
    2116           1 :     aRestyleHint |= eRestyle_Subtree;
    2117             :   }
    2118             : 
    2119          41 :   if (mPendingViewportChange &&
    2120          61 :       (mUsesViewportUnits || mDocument->IsStyledByServo())) {
    2121             :     // Rebuild all style data without rerunning selector matching.
    2122             :     //
    2123             :     // FIXME(emilio, bug 1328652): We don't set mUsesViewportUnits in stylo yet,
    2124             :     // so assume the worst.
    2125             :     //
    2126             :     // Also, in this case we don't need to do a rebuild of the style data, only
    2127             :     // post a restyle.
    2128           0 :     aRestyleHint |= eRestyle_ForceDescendants;
    2129             :   }
    2130             : 
    2131          21 :   if (aRestyleHint || aChangeHint) {
    2132           1 :     RebuildAllStyleData(aChangeHint, aRestyleHint);
    2133             :   }
    2134             : 
    2135          21 :   mPendingViewportChange = false;
    2136             : 
    2137          21 :   if (mDocument->IsBeingUsedAsImage()) {
    2138          18 :     MOZ_ASSERT(mDocument->MediaQueryLists().isEmpty());
    2139          18 :     return;
    2140             :   }
    2141             : 
    2142           3 :   mDocument->NotifyMediaFeatureValuesChanged();
    2143             : 
    2144           3 :   MOZ_ASSERT(nsContentUtils::IsSafeToRunScript());
    2145             : 
    2146             :   // Media query list listeners should be notified from a queued task
    2147             :   // (in HTML5 terms), although we also want to notify them on certain
    2148             :   // flushes.  (We're already running off an event.)
    2149             :   //
    2150             :   // Note that we do this after the new style from media queries in
    2151             :   // style sheets has been computed.
    2152             : 
    2153           3 :   if (!mDocument->MediaQueryLists().isEmpty()) {
    2154             :     // We build a list of all the notifications we're going to send
    2155             :     // before we send any of them.
    2156          10 :     for (auto mql : mDocument->MediaQueryLists()) {
    2157          14 :       nsAutoMicroTask mt;
    2158           7 :       mql->MaybeNotify();
    2159             :     }
    2160             :   }
    2161             : }
    2162             : 
    2163             : void
    2164          20 : nsPresContext::PostMediaFeatureValuesChangedEvent()
    2165             : {
    2166             :   // FIXME: We should probably replace this event with use of
    2167             :   // nsRefreshDriver::AddStyleFlushObserver (except the pres shell would
    2168             :   // need to track whether it's been added).
    2169          20 :   if (!mPendingMediaFeatureValuesChanged && mShell) {
    2170             :     nsCOMPtr<nsIRunnable> ev =
    2171          40 :       NewRunnableMethod("nsPresContext::HandleMediaFeatureValuesChangedEvent",
    2172          40 :                         this, &nsPresContext::HandleMediaFeatureValuesChangedEvent);
    2173             :     nsresult rv =
    2174          20 :       Document()->Dispatch("nsPresContext::HandleMediaFeatureValuesChangedEvent",
    2175             :                            TaskCategory::Other,
    2176          20 :                            ev.forget());
    2177          20 :     if (NS_SUCCEEDED(rv)) {
    2178          20 :       mPendingMediaFeatureValuesChanged = true;
    2179          20 :       mShell->SetNeedStyleFlush();
    2180             :     }
    2181             :   }
    2182          20 : }
    2183             : 
    2184             : void
    2185          20 : nsPresContext::HandleMediaFeatureValuesChangedEvent()
    2186             : {
    2187             :   // Null-check mShell in case the shell has been destroyed (and the
    2188             :   // event is the only thing holding the pres context alive).
    2189          20 :   if (mPendingMediaFeatureValuesChanged && mShell) {
    2190           0 :     MediaFeatureValuesChanged(nsRestyleHint(0));
    2191             :   }
    2192          20 : }
    2193             : 
    2194             : static bool
    2195           1 : NotifyTabSizeModeChanged(TabParent* aTab, void* aArg)
    2196             : {
    2197           1 :   nsSizeMode* sizeMode = static_cast<nsSizeMode*>(aArg);
    2198           1 :   aTab->SizeModeChanged(*sizeMode);
    2199           1 :   return false;
    2200             : }
    2201             : 
    2202             : void
    2203           2 : nsPresContext::SizeModeChanged(nsSizeMode aSizeMode)
    2204             : {
    2205           2 :   if (HasCachedStyleData()) {
    2206           1 :     nsContentUtils::CallOnAllRemoteChildren(mDocument->GetWindow(),
    2207             :                                             NotifyTabSizeModeChanged,
    2208           1 :                                             &aSizeMode);
    2209           1 :     MediaFeatureValuesChangedAllDocuments(nsRestyleHint(0));
    2210             :   }
    2211           2 : }
    2212             : 
    2213             : nsCompatibility
    2214         748 : nsPresContext::CompatibilityMode() const
    2215             : {
    2216         748 :   return Document()->GetCompatibilityMode();
    2217             : }
    2218             : 
    2219             : void
    2220           0 : nsPresContext::SetPaginatedScrolling(bool aPaginated)
    2221             : {
    2222           0 :   if (mType == eContext_PrintPreview || mType == eContext_PageLayout)
    2223           0 :     mCanPaginatedScroll = aPaginated;
    2224           0 : }
    2225             : 
    2226             : void
    2227           0 : nsPresContext::SetPrintSettings(nsIPrintSettings *aPrintSettings)
    2228             : {
    2229           0 :   if (mMedium == nsGkAtoms::print)
    2230           0 :     mPrintSettings = aPrintSettings;
    2231           0 : }
    2232             : 
    2233             : bool
    2234           3 : nsPresContext::EnsureVisible()
    2235             : {
    2236           6 :   nsCOMPtr<nsIDocShell> docShell(mContainer);
    2237           3 :   if (docShell) {
    2238           3 :     nsCOMPtr<nsIContentViewer> cv;
    2239           3 :     docShell->GetContentViewer(getter_AddRefs(cv));
    2240             :     // Make sure this is the content viewer we belong with
    2241           3 :     if (cv) {
    2242           3 :       RefPtr<nsPresContext> currentPresContext;
    2243           3 :       cv->GetPresContext(getter_AddRefs(currentPresContext));
    2244           3 :       if (currentPresContext == this) {
    2245             :         // OK, this is us.  We want to call Show() on the content viewer.
    2246           3 :         nsresult result = cv->Show();
    2247           3 :         if (NS_SUCCEEDED(result)) {
    2248           3 :           return true;
    2249             :         }
    2250             :       }
    2251             :     }
    2252             :   }
    2253           0 :   return false;
    2254             : }
    2255             : 
    2256             : #ifdef MOZ_REFLOW_PERF
    2257             : void
    2258         735 : nsPresContext::CountReflows(const char * aName, nsIFrame * aFrame)
    2259             : {
    2260         735 :   if (mShell) {
    2261         735 :     mShell->CountReflows(aName, aFrame);
    2262             :   }
    2263         735 : }
    2264             : #endif
    2265             : 
    2266             : void
    2267          11 : nsPresContext::UpdateIsChrome()
    2268             : {
    2269          18 :   mIsChrome = mContainer &&
    2270          18 :               nsIDocShellTreeItem::typeChrome == mContainer->ItemType();
    2271          11 : }
    2272             : 
    2273             : bool
    2274           0 : nsPresContext::HasAuthorSpecifiedRules(const nsIFrame* aFrame,
    2275             :                                        uint32_t aRuleTypeMask) const
    2276             : {
    2277           0 :   if (mShell->StyleSet()->IsGecko()) {
    2278             :     return
    2279           0 :       nsRuleNode::HasAuthorSpecifiedRules(aFrame->StyleContext(),
    2280             :                                           aRuleTypeMask,
    2281           0 :                                           UseDocumentColors());
    2282             :   }
    2283           0 :   Element* elem = aFrame->GetContent()->AsElement();
    2284             : 
    2285           0 :   MOZ_ASSERT(elem->GetPseudoElementType() ==
    2286             :              aFrame->StyleContext()->GetPseudoType());
    2287           0 :   MOZ_ASSERT(elem->HasServoData());
    2288           0 :   return Servo_HasAuthorSpecifiedRules(elem,
    2289             :                                        aRuleTypeMask,
    2290           0 :                                        UseDocumentColors());
    2291             : }
    2292             : 
    2293             : gfxUserFontSet*
    2294         423 : nsPresContext::GetUserFontSet(bool aFlushUserFontSet)
    2295             : {
    2296         423 :   return mDocument->GetUserFontSet(aFlushUserFontSet);
    2297             : }
    2298             : 
    2299             : void
    2300           0 : nsPresContext::UserFontSetUpdated(gfxUserFontEntry* aUpdatedFont)
    2301             : {
    2302           0 :   if (!mShell)
    2303           0 :     return;
    2304             : 
    2305             :   // Note: this method is called without a font when rules in the userfont set
    2306             :   // are updated, which may occur during reflow as a result of the lazy
    2307             :   // initialization of the userfont set. It would be better to avoid a full
    2308             :   // restyle but until this method is only called outside of reflow, schedule a
    2309             :   // full restyle in these cases.
    2310           0 :   if (!aUpdatedFont) {
    2311           0 :     PostRebuildAllStyleDataEvent(NS_STYLE_HINT_REFLOW, eRestyle_ForceDescendants);
    2312           0 :     return;
    2313             :   }
    2314             : 
    2315             :   // Special case - if either the 'ex' or 'ch' units are used, these
    2316             :   // depend upon font metrics. Updating this information requires
    2317             :   // rebuilding the rule tree from the top, avoiding the reuse of cached
    2318             :   // data even when no style rules have changed.
    2319           0 :   if (UsesExChUnits()) {
    2320           0 :     PostRebuildAllStyleDataEvent(nsChangeHint(0), eRestyle_ForceDescendants);
    2321             :   }
    2322             : 
    2323             :   // Iterate over the frame tree looking for frames associated with the
    2324             :   // downloadable font family in question. If a frame's nsStyleFont has
    2325             :   // the name, check the font group associated with the metrics to see if
    2326             :   // it contains that specific font (i.e. the one chosen within the family
    2327             :   // given the weight, width, and slant from the nsStyleFont). If it does,
    2328             :   // mark that frame dirty and skip inspecting its descendants.
    2329           0 :   nsIFrame* root = mShell->GetRootFrame();
    2330           0 :   if (root) {
    2331           0 :     nsFontFaceUtils::MarkDirtyForFontChange(root, aUpdatedFont);
    2332             :   }
    2333             : }
    2334             : 
    2335             : class CounterStyleCleaner : public nsAPostRefreshObserver
    2336             : {
    2337             : public:
    2338           0 :   CounterStyleCleaner(nsRefreshDriver* aRefreshDriver,
    2339             :                       CounterStyleManager* aCounterStyleManager)
    2340           0 :     : mRefreshDriver(aRefreshDriver)
    2341           0 :     , mCounterStyleManager(aCounterStyleManager)
    2342             :   {
    2343           0 :   }
    2344           0 :   virtual ~CounterStyleCleaner() {}
    2345             : 
    2346           0 :   void DidRefresh() final
    2347             :   {
    2348           0 :     mRefreshDriver->RemovePostRefreshObserver(this);
    2349           0 :     mCounterStyleManager->CleanRetiredStyles();
    2350           0 :     delete this;
    2351           0 :   }
    2352             : 
    2353             : private:
    2354             :   RefPtr<nsRefreshDriver> mRefreshDriver;
    2355             :   RefPtr<CounterStyleManager> mCounterStyleManager;
    2356             : };
    2357             : 
    2358             : void
    2359         190 : nsPresContext::FlushCounterStyles()
    2360             : {
    2361         190 :   if (!mShell) {
    2362           0 :     return; // we've been torn down
    2363             :   }
    2364         190 :   if (mCounterStyleManager->IsInitial()) {
    2365             :     // Still in its initial state, no need to clean.
    2366         190 :     return;
    2367             :   }
    2368             : 
    2369           0 :   if (mCounterStylesDirty) {
    2370           0 :     bool changed = mCounterStyleManager->NotifyRuleChanged();
    2371           0 :     if (changed) {
    2372           0 :       PresShell()->NotifyCounterStylesAreDirty();
    2373           0 :       PostRebuildAllStyleDataEvent(NS_STYLE_HINT_REFLOW,
    2374           0 :                                    eRestyle_ForceDescendants);
    2375           0 :       RefreshDriver()->AddPostRefreshObserver(
    2376           0 :         new CounterStyleCleaner(RefreshDriver(), mCounterStyleManager));
    2377             :     }
    2378           0 :     mCounterStylesDirty = false;
    2379             :   }
    2380             : }
    2381             : 
    2382             : void
    2383          57 : nsPresContext::RebuildCounterStyles()
    2384             : {
    2385          57 :   if (mCounterStyleManager->IsInitial()) {
    2386             :     // Still in its initial state, no need to reset.
    2387          57 :     return;
    2388             :   }
    2389             : 
    2390           0 :   mCounterStylesDirty = true;
    2391           0 :   if (mShell) {
    2392           0 :     mShell->SetNeedStyleFlush();
    2393             :   }
    2394           0 :   if (!mPostedFlushCounterStyles) {
    2395             :     nsCOMPtr<nsIRunnable> ev =
    2396           0 :       NewRunnableMethod("nsPresContext::HandleRebuildCounterStyles",
    2397           0 :                         this, &nsPresContext::HandleRebuildCounterStyles);
    2398             :     nsresult rv =
    2399           0 :       Document()->Dispatch("nsPresContext::HandleRebuildCounterStyles",
    2400             :                            TaskCategory::Other,
    2401           0 :                            ev.forget());
    2402           0 :     if (NS_SUCCEEDED(rv)) {
    2403           0 :       mPostedFlushCounterStyles = true;
    2404             :     }
    2405             :   }
    2406             : }
    2407             : 
    2408             : void
    2409          68 : nsPresContext::NotifyMissingFonts()
    2410             : {
    2411          68 :   if (mMissingFonts) {
    2412           0 :     mMissingFonts->Flush();
    2413             :   }
    2414          68 : }
    2415             : 
    2416             : void
    2417           0 : nsPresContext::EnsureSafeToHandOutCSSRules()
    2418             : {
    2419           0 :   if (!mShell->StyleSet()->EnsureUniqueInnerOnCSSSheets()) {
    2420             :     // Nothing to do.
    2421           0 :     return;
    2422             :   }
    2423             : 
    2424           0 :   RebuildAllStyleData(nsChangeHint(0), eRestyle_Subtree);
    2425             : }
    2426             : 
    2427             : void
    2428          25 : nsPresContext::FireDOMPaintEvent(nsTArray<nsRect>* aList, uint64_t aTransactionId,
    2429             :                                  mozilla::TimeStamp aTimeStamp /* = mozilla::TimeStamp() */)
    2430             : {
    2431          25 :   nsPIDOMWindowInner* ourWindow = mDocument->GetInnerWindow();
    2432          25 :   if (!ourWindow)
    2433           0 :     return;
    2434             : 
    2435          50 :   nsCOMPtr<EventTarget> dispatchTarget = do_QueryInterface(ourWindow);
    2436          50 :   nsCOMPtr<EventTarget> eventTarget = dispatchTarget;
    2437          25 :   if (!IsChrome() && !mSendAfterPaintToContent) {
    2438             :     // Don't tell the window about this event, it should not know that
    2439             :     // something happened in a subdocument. Tell only the chrome event handler.
    2440             :     // (Events sent to the window get propagated to the chrome event handler
    2441             :     // automatically.)
    2442           0 :     dispatchTarget = do_QueryInterface(ourWindow->GetParentTarget());
    2443           0 :     if (!dispatchTarget) {
    2444           0 :       return;
    2445             :     }
    2446             :   }
    2447             : 
    2448          25 :   if (aTimeStamp.IsNull()) {
    2449           0 :     aTimeStamp = mozilla::TimeStamp::Now();
    2450             :   }
    2451          25 :   DOMHighResTimeStamp timeStamp = 0;
    2452          25 :   if (ourWindow && ourWindow->IsInnerWindow()) {
    2453          25 :     mozilla::dom::Performance* perf = ourWindow->GetPerformance();
    2454          25 :     if (perf) {
    2455          25 :       timeStamp = perf->GetDOMTiming()->TimeStampToDOMHighRes(aTimeStamp);
    2456             :     }
    2457             :   }
    2458             : 
    2459             :   // Events sent to the window get propagated to the chrome event handler
    2460             :   // automatically.
    2461             :   //
    2462             :   // This will empty our list in case dispatching the event causes more damage
    2463             :   // (hopefully it won't, or we're likely to get an infinite loop! At least
    2464             :   // it won't be blocking app execution though).
    2465             :   RefPtr<NotifyPaintEvent> event =
    2466          50 :     NS_NewDOMNotifyPaintEvent(eventTarget, this, nullptr, eAfterPaint, aList,
    2467          50 :                               aTransactionId, timeStamp);
    2468             : 
    2469             :   // Even if we're not telling the window about the event (so eventTarget is
    2470             :   // the chrome event handler, not the window), the window is still
    2471             :   // logically the event target.
    2472          25 :   event->SetTarget(eventTarget);
    2473          25 :   event->SetTrusted(true);
    2474             :   EventDispatcher::DispatchDOMEvent(dispatchTarget, nullptr,
    2475          25 :                                     static_cast<Event*>(event), this, nullptr);
    2476             : }
    2477             : 
    2478             : static bool
    2479           0 : MayHavePaintEventListenerSubdocumentCallback(nsIDocument* aDocument, void* aData)
    2480             : {
    2481           0 :   bool *result = static_cast<bool*>(aData);
    2482           0 :   nsIPresShell* shell = aDocument->GetShell();
    2483           0 :   if (shell) {
    2484           0 :     nsPresContext* pc = shell->GetPresContext();
    2485           0 :     if (pc) {
    2486           0 :       *result = pc->MayHavePaintEventListenerInSubDocument();
    2487             : 
    2488             :       // If we found a paint event listener, then we can stop enumerating
    2489             :       // sub documents.
    2490           0 :       return !*result;
    2491             :     }
    2492             :   }
    2493           0 :   return true;
    2494             : }
    2495             : 
    2496             : static bool
    2497          45 : MayHavePaintEventListener(nsPIDOMWindowInner* aInnerWindow)
    2498             : {
    2499          45 :   if (!aInnerWindow)
    2500          18 :     return false;
    2501          27 :   if (aInnerWindow->HasPaintEventListeners())
    2502          27 :     return true;
    2503             : 
    2504           0 :   EventTarget* parentTarget = aInnerWindow->GetParentTarget();
    2505           0 :   if (!parentTarget)
    2506           0 :     return false;
    2507             : 
    2508           0 :   EventListenerManager* manager = nullptr;
    2509           0 :   if ((manager = parentTarget->GetExistingListenerManager()) &&
    2510           0 :       manager->MayHavePaintEventListener()) {
    2511           0 :     return true;
    2512             :   }
    2513             : 
    2514           0 :   nsCOMPtr<nsINode> node;
    2515           0 :   if (parentTarget != aInnerWindow->GetChromeEventHandler()) {
    2516             :     nsCOMPtr<nsIInProcessContentFrameMessageManager> mm =
    2517           0 :       do_QueryInterface(parentTarget);
    2518           0 :     if (mm) {
    2519           0 :       node = mm->GetOwnerContent();
    2520             :     }
    2521             :   }
    2522             : 
    2523           0 :   if (!node) {
    2524           0 :     node = do_QueryInterface(parentTarget);
    2525             :   }
    2526           0 :   if (node)
    2527           0 :     return MayHavePaintEventListener(node->OwnerDoc()->GetInnerWindow());
    2528             : 
    2529           0 :   nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(parentTarget);
    2530           0 :   if (window)
    2531           0 :     return MayHavePaintEventListener(window);
    2532             : 
    2533           0 :   nsCOMPtr<nsPIWindowRoot> root = do_QueryInterface(parentTarget);
    2534             :   EventTarget* tabChildGlobal;
    2535           0 :   return root &&
    2536           0 :          (tabChildGlobal = root->GetParentTarget()) &&
    2537           0 :          (manager = tabChildGlobal->GetExistingListenerManager()) &&
    2538           0 :          manager->MayHavePaintEventListener();
    2539             : }
    2540             : 
    2541             : bool
    2542          45 : nsPresContext::MayHavePaintEventListener()
    2543             : {
    2544          45 :   return ::MayHavePaintEventListener(mDocument->GetInnerWindow());
    2545             : }
    2546             : 
    2547             : bool
    2548          45 : nsPresContext::MayHavePaintEventListenerInSubDocument()
    2549             : {
    2550          45 :   if (MayHavePaintEventListener()) {
    2551          27 :     return true;
    2552             :   }
    2553             : 
    2554          18 :   bool result = false;
    2555          18 :   mDocument->EnumerateSubDocuments(MayHavePaintEventListenerSubdocumentCallback, &result);
    2556          18 :   return result;
    2557             : }
    2558             : 
    2559             : void
    2560          25 : nsPresContext::NotifyInvalidation(uint64_t aTransactionId, const nsIntRect& aRect)
    2561             : {
    2562             :   // Prevent values from overflow after DevPixelsToAppUnits().
    2563             :   //
    2564             :   // DevPixelsTopAppUnits() will multiple a factor (60) to the value,
    2565             :   // it may make the result value over the edge (overflow) of max or
    2566             :   // min value of int32_t. Compute the max sized dev pixel rect that
    2567             :   // we can support and intersect with it.
    2568          25 :   nsIntRect clampedRect = nsIntRect::MaxIntRect();
    2569          25 :   clampedRect.ScaleInverseRoundIn(AppUnitsPerDevPixel());
    2570             : 
    2571          25 :   clampedRect = clampedRect.Intersect(aRect);
    2572             : 
    2573             :   nsRect rect(DevPixelsToAppUnits(clampedRect.x),
    2574             :               DevPixelsToAppUnits(clampedRect.y),
    2575             :               DevPixelsToAppUnits(clampedRect.width),
    2576          50 :               DevPixelsToAppUnits(clampedRect.height));
    2577          25 :   NotifyInvalidation(aTransactionId, rect);
    2578          25 : }
    2579             : 
    2580             : void
    2581          25 : nsPresContext::NotifyInvalidation(uint64_t aTransactionId, const nsRect& aRect)
    2582             : {
    2583          25 :   MOZ_ASSERT(GetContainerWeak(), "Invalidation in detached pres context");
    2584             : 
    2585             :   // If there is no paint event listener, then we don't need to fire
    2586             :   // the asynchronous event. We don't even need to record invalidation.
    2587             :   // MayHavePaintEventListener is pretty cheap and we could make it
    2588             :   // even cheaper by providing a more efficient
    2589             :   // nsPIDOMWindow::GetListenerManager.
    2590             : 
    2591             :   nsPresContext* pc;
    2592          30 :   for (pc = this; pc; pc = pc->GetParentPresContext()) {
    2593          25 :     if (pc->mFireAfterPaintEvents)
    2594          20 :       break;
    2595           5 :     pc->mFireAfterPaintEvents = true;
    2596             :   }
    2597          25 :   if (!pc) {
    2598           5 :     nsRootPresContext* rpc = GetRootPresContext();
    2599           5 :     if (rpc) {
    2600           5 :       rpc->EnsureEventualDidPaintEvent(aTransactionId);
    2601             :     }
    2602             :   }
    2603             : 
    2604          25 :   TransactionInvalidations* transaction = nullptr;
    2605          49 :   for (TransactionInvalidations& t : mTransactions) {
    2606          24 :     if (t.mTransactionId == aTransactionId) {
    2607           0 :       transaction = &t;
    2608           0 :       break;
    2609             :     }
    2610             :   }
    2611          25 :   if (!transaction) {
    2612          25 :     transaction = mTransactions.AppendElement();
    2613          25 :     transaction->mTransactionId = aTransactionId;
    2614             :   }
    2615             : 
    2616          25 :   transaction->mInvalidations.AppendElement(aRect);
    2617          25 : }
    2618             : 
    2619             : /* static */ void
    2620          82 : nsPresContext::NotifySubDocInvalidation(ContainerLayer* aContainer,
    2621             :                                         const nsIntRegion& aRegion)
    2622             : {
    2623             :   ContainerLayerPresContext *data =
    2624             :     static_cast<ContainerLayerPresContext*>(
    2625          82 :       aContainer->GetUserData(&gNotifySubDocInvalidationData));
    2626          82 :   if (!data) {
    2627          82 :     return;
    2628             :   }
    2629             : 
    2630           0 :   nsIntPoint topLeft = aContainer->GetVisibleRegion().ToUnknownRegion().GetBounds().TopLeft();
    2631             : 
    2632           0 :   for (auto iter = aRegion.RectIter(); !iter.Done(); iter.Next()) {
    2633           0 :     nsIntRect rect(iter.Get());
    2634             :     //PresContext coordinate space is relative to the start of our visible
    2635             :     // region. Is this really true? This feels like the wrong way to get the right
    2636             :     // answer.
    2637           0 :     rect.MoveBy(-topLeft);
    2638           0 :     data->mPresContext->NotifyInvalidation(aContainer->Manager()->GetLastTransactionId(), rect);
    2639             :   }
    2640             : }
    2641             : 
    2642             : void
    2643           0 : nsPresContext::SetNotifySubDocInvalidationData(ContainerLayer* aContainer)
    2644             : {
    2645           0 :   ContainerLayerPresContext* pres = new ContainerLayerPresContext;
    2646           0 :   pres->mPresContext = this;
    2647           0 :   aContainer->SetUserData(&gNotifySubDocInvalidationData, pres);
    2648           0 : }
    2649             : 
    2650             : /* static */ void
    2651         239 : nsPresContext::ClearNotifySubDocInvalidationData(ContainerLayer* aContainer)
    2652             : {
    2653         239 :   aContainer->SetUserData(&gNotifySubDocInvalidationData, nullptr);
    2654         239 : }
    2655             : 
    2656             : struct NotifyDidPaintSubdocumentCallbackClosure {
    2657             :   uint64_t mTransactionId;
    2658             :   const mozilla::TimeStamp& mTimeStamp;
    2659             :   bool mNeedsAnotherDidPaintNotification;
    2660             : };
    2661             : /* static */ bool
    2662          23 : nsPresContext::NotifyDidPaintSubdocumentCallback(nsIDocument* aDocument, void* aData)
    2663             : {
    2664             :   NotifyDidPaintSubdocumentCallbackClosure* closure =
    2665          23 :     static_cast<NotifyDidPaintSubdocumentCallbackClosure*>(aData);
    2666          23 :   nsIPresShell* shell = aDocument->GetShell();
    2667          23 :   if (shell) {
    2668           0 :     nsPresContext* pc = shell->GetPresContext();
    2669           0 :     if (pc) {
    2670           0 :       pc->NotifyDidPaintForSubtree(closure->mTransactionId,
    2671           0 :                                    closure->mTimeStamp);
    2672           0 :       if (pc->mFireAfterPaintEvents) {
    2673           0 :         closure->mNeedsAnotherDidPaintNotification = true;
    2674             :       }
    2675             :     }
    2676             :   }
    2677          23 :   return true;
    2678             : }
    2679             : 
    2680          75 : class DelayedFireDOMPaintEvent : public Runnable {
    2681             : public:
    2682          25 :   DelayedFireDOMPaintEvent(
    2683             :     nsPresContext* aPresContext,
    2684             :     nsTArray<nsRect>* aList,
    2685             :     uint64_t aTransactionId,
    2686             :     const mozilla::TimeStamp& aTimeStamp = mozilla::TimeStamp())
    2687          25 :     : mozilla::Runnable("DelayedFireDOMPaintEvent")
    2688             :     , mPresContext(aPresContext)
    2689             :     , mTransactionId(aTransactionId)
    2690          25 :     , mTimeStamp(aTimeStamp)
    2691             :   {
    2692          25 :     MOZ_ASSERT(mPresContext->GetContainerWeak(),
    2693             :                "DOMPaintEvent requested for a detached pres context");
    2694          25 :     mList.SwapElements(*aList);
    2695          25 :   }
    2696          25 :   NS_IMETHOD Run() override
    2697             :   {
    2698             :     // The pres context might have been detached during the delay -
    2699             :     // that's fine, just don't fire the event.
    2700          25 :     if (mPresContext->GetContainerWeak()) {
    2701          25 :       mPresContext->FireDOMPaintEvent(&mList, mTransactionId, mTimeStamp);
    2702             :     }
    2703          25 :     return NS_OK;
    2704             :   }
    2705             : 
    2706             :   RefPtr<nsPresContext> mPresContext;
    2707             :   uint64_t mTransactionId;
    2708             :   const mozilla::TimeStamp mTimeStamp;
    2709             :   nsTArray<nsRect> mList;
    2710             : };
    2711             : 
    2712             : void
    2713          27 : nsPresContext::NotifyDidPaintForSubtree(uint64_t aTransactionId,
    2714             :                                         const mozilla::TimeStamp& aTimeStamp)
    2715             : {
    2716          27 :   if (IsRoot()) {
    2717          27 :     static_cast<nsRootPresContext*>(this)->CancelDidPaintTimers(aTransactionId);
    2718             : 
    2719          27 :     if (!mFireAfterPaintEvents) {
    2720           4 :       return;
    2721             :     }
    2722             :   }
    2723             : 
    2724          25 :   if (!PresShell()->IsVisible() && !mFireAfterPaintEvents) {
    2725           0 :     return;
    2726             :   }
    2727             : 
    2728             :   // Non-root prescontexts fire MozAfterPaint to all their descendants
    2729             :   // unconditionally, even if no invalidations have been collected. This is
    2730             :   // because we don't want to eat the cost of collecting invalidations for
    2731             :   // every subdocument (which would require putting every subdocument in its
    2732             :   // own layer).
    2733             : 
    2734          25 :   bool sent = false;
    2735          25 :   uint32_t i = 0;
    2736         123 :   while (i < mTransactions.Length()) {
    2737          49 :     if (mTransactions[i].mTransactionId <= aTransactionId) {
    2738             :       nsCOMPtr<nsIRunnable> ev =
    2739          24 :         new DelayedFireDOMPaintEvent(this, &mTransactions[i].mInvalidations,
    2740          72 :                                      mTransactions[i].mTransactionId, aTimeStamp);
    2741          24 :       nsContentUtils::AddScriptRunner(ev);
    2742          24 :       sent = true;
    2743          24 :       mTransactions.RemoveElementAt(i);
    2744             :     } else {
    2745          25 :       i++;
    2746             :     }
    2747             :   }
    2748             : 
    2749          25 :   if (!sent) {
    2750           2 :     nsTArray<nsRect> dummy;
    2751             :     nsCOMPtr<nsIRunnable> ev =
    2752             :       new DelayedFireDOMPaintEvent(this, &dummy,
    2753           2 :                                    aTransactionId, aTimeStamp);
    2754           1 :     nsContentUtils::AddScriptRunner(ev);
    2755             :   }
    2756             : 
    2757          25 :   NotifyDidPaintSubdocumentCallbackClosure closure = { aTransactionId, aTimeStamp, false };
    2758          25 :   mDocument->EnumerateSubDocuments(nsPresContext::NotifyDidPaintSubdocumentCallback, &closure);
    2759             : 
    2760          50 :   if (!closure.mNeedsAnotherDidPaintNotification &&
    2761          25 :       mTransactions.IsEmpty()) {
    2762             :     // Nothing more to do for the moment.
    2763           4 :     mFireAfterPaintEvents = false;
    2764             :   }
    2765             : }
    2766             : 
    2767             : bool
    2768          61 : nsPresContext::HasCachedStyleData()
    2769             : {
    2770          61 :   if (!mShell) {
    2771           0 :     return false;
    2772             :   }
    2773             : 
    2774          61 :   nsStyleSet* styleSet = mShell->StyleSet()->GetAsGecko();
    2775          61 :   if (!styleSet) {
    2776             :     // XXXheycam ServoStyleSets do not use the rule tree, so just assume for now
    2777             :     // that we need to restyle when e.g. dppx changes assuming we're sufficiently
    2778             :     // bootstrapped.
    2779           0 :     return mShell->DidInitialize();
    2780             :   }
    2781             : 
    2782          61 :   return styleSet->HasCachedStyleData();
    2783             : }
    2784             : 
    2785             : already_AddRefed<nsITimer>
    2786           0 : nsPresContext::CreateTimer(nsTimerCallbackFunc aCallback,
    2787             :                            const char* aName,
    2788             :                            uint32_t aDelay)
    2789             : {
    2790           0 :   nsCOMPtr<nsITimer> timer = do_CreateInstance("@mozilla.org/timer;1");
    2791           0 :   timer->SetTarget(Document()->EventTargetFor(TaskCategory::Other));
    2792           0 :   if (timer) {
    2793           0 :     nsresult rv = timer->InitWithNamedFuncCallback(aCallback, this, aDelay,
    2794             :                                                    nsITimer::TYPE_ONE_SHOT,
    2795           0 :                                                    aName);
    2796           0 :     if (NS_SUCCEEDED(rv)) {
    2797           0 :       return timer.forget();
    2798             :     }
    2799             :   }
    2800             : 
    2801           0 :   return nullptr;
    2802             : }
    2803             : 
    2804             : static bool sGotInterruptEnv = false;
    2805             : enum InterruptMode {
    2806             :   ModeRandom,
    2807             :   ModeCounter,
    2808             :   ModeEvent
    2809             : };
    2810             : // Controlled by the GECKO_REFLOW_INTERRUPT_MODE env var; allowed values are
    2811             : // "random" (except on Windows) or "counter".  If neither is used, the mode is
    2812             : // ModeEvent.
    2813             : static InterruptMode sInterruptMode = ModeEvent;
    2814             : #ifndef XP_WIN
    2815             : // Used for the "random" mode.  Controlled by the GECKO_REFLOW_INTERRUPT_SEED
    2816             : // env var.
    2817             : static uint32_t sInterruptSeed = 1;
    2818             : #endif
    2819             : // Used for the "counter" mode.  This is the number of unskipped interrupt
    2820             : // checks that have to happen before we interrupt.  Controlled by the
    2821             : // GECKO_REFLOW_INTERRUPT_FREQUENCY env var.
    2822             : static uint32_t sInterruptMaxCounter = 10;
    2823             : // Used for the "counter" mode.  This counts up to sInterruptMaxCounter and is
    2824             : // then reset to 0.
    2825             : static uint32_t sInterruptCounter;
    2826             : // Number of interrupt checks to skip before really trying to interrupt.
    2827             : // Controlled by the GECKO_REFLOW_INTERRUPT_CHECKS_TO_SKIP env var.
    2828             : static uint32_t sInterruptChecksToSkip = 200;
    2829             : // Number of milliseconds that a reflow should be allowed to run for before we
    2830             : // actually allow interruption.  Controlled by the
    2831             : // GECKO_REFLOW_MIN_NOINTERRUPT_DURATION env var.  Can't be initialized here,
    2832             : // because TimeDuration/TimeStamp is not safe to use in static constructors..
    2833             : static TimeDuration sInterruptTimeout;
    2834             : 
    2835           2 : static void GetInterruptEnv()
    2836             : {
    2837           2 :   char *ev = PR_GetEnv("GECKO_REFLOW_INTERRUPT_MODE");
    2838           2 :   if (ev) {
    2839             : #ifndef XP_WIN
    2840           0 :     if (PL_strcasecmp(ev, "random") == 0) {
    2841           0 :       ev = PR_GetEnv("GECKO_REFLOW_INTERRUPT_SEED");
    2842           0 :       if (ev) {
    2843           0 :         sInterruptSeed = atoi(ev);
    2844             :       }
    2845           0 :       srandom(sInterruptSeed);
    2846           0 :       sInterruptMode = ModeRandom;
    2847             :     } else
    2848             : #endif
    2849           0 :       if (PL_strcasecmp(ev, "counter") == 0) {
    2850           0 :       ev = PR_GetEnv("GECKO_REFLOW_INTERRUPT_FREQUENCY");
    2851           0 :       if (ev) {
    2852           0 :         sInterruptMaxCounter = atoi(ev);
    2853             :       }
    2854           0 :       sInterruptCounter = 0;
    2855           0 :       sInterruptMode = ModeCounter;
    2856             :     }
    2857             :   }
    2858           2 :   ev = PR_GetEnv("GECKO_REFLOW_INTERRUPT_CHECKS_TO_SKIP");
    2859           2 :   if (ev) {
    2860           0 :     sInterruptChecksToSkip = atoi(ev);
    2861             :   }
    2862             : 
    2863           2 :   ev = PR_GetEnv("GECKO_REFLOW_MIN_NOINTERRUPT_DURATION");
    2864           2 :   int duration_ms = ev ? atoi(ev) : 100;
    2865           2 :   sInterruptTimeout = TimeDuration::FromMilliseconds(duration_ms);
    2866           2 : }
    2867             : 
    2868             : bool
    2869           0 : nsPresContext::HavePendingInputEvent()
    2870             : {
    2871           0 :   switch (sInterruptMode) {
    2872             : #ifndef XP_WIN
    2873             :     case ModeRandom:
    2874           0 :       return (random() & 1);
    2875             : #endif
    2876             :     case ModeCounter:
    2877           0 :       if (sInterruptCounter < sInterruptMaxCounter) {
    2878           0 :         ++sInterruptCounter;
    2879           0 :         return false;
    2880             :       }
    2881           0 :       sInterruptCounter = 0;
    2882           0 :       return true;
    2883             :     default:
    2884             :     case ModeEvent: {
    2885           0 :       nsIFrame* f = PresShell()->GetRootFrame();
    2886           0 :       if (f) {
    2887           0 :         nsIWidget* w = f->GetNearestWidget();
    2888           0 :         if (w) {
    2889           0 :           return w->HasPendingInputEvent();
    2890             :         }
    2891             :       }
    2892           0 :       return false;
    2893             :     }
    2894             :   }
    2895             : }
    2896             : 
    2897             : void
    2898          41 : nsPresContext::NotifyFontFaceSetOnRefresh()
    2899             : {
    2900          41 :   FontFaceSet* set = mDocument->GetFonts();
    2901          41 :   if (set) {
    2902           0 :     set->DidRefresh();
    2903             :   }
    2904          41 : }
    2905             : 
    2906             : bool
    2907           0 : nsPresContext::HasPendingRestyleOrReflow()
    2908             : {
    2909           0 :   nsIPresShell* shell = PresShell();
    2910           0 :   return shell->NeedStyleFlush() || shell->HasPendingReflow();
    2911             : }
    2912             : 
    2913             : void
    2914          70 : nsPresContext::ReflowStarted(bool aInterruptible)
    2915             : {
    2916             : #ifdef NOISY_INTERRUPTIBLE_REFLOW
    2917             :   if (!aInterruptible) {
    2918             :     printf("STARTING NONINTERRUPTIBLE REFLOW\n");
    2919             :   }
    2920             : #endif
    2921             :   // We don't support interrupting in paginated contexts, since page
    2922             :   // sequences only handle initial reflow
    2923         108 :   mInterruptsEnabled = aInterruptible && !IsPaginated() &&
    2924          38 :                        nsLayoutUtils::InterruptibleReflowEnabled();
    2925             : 
    2926             :   // Don't set mHasPendingInterrupt based on HavePendingInputEvent() here.  If
    2927             :   // we ever change that, then we need to update the code in
    2928             :   // PresShell::DoReflow to only add the just-reflown root to dirty roots if
    2929             :   // it's actually dirty.  Otherwise we can end up adding a root that has no
    2930             :   // interruptible descendants, just because we detected an interrupt at reflow
    2931             :   // start.
    2932          70 :   mHasPendingInterrupt = false;
    2933             : 
    2934          70 :   mInterruptChecksToSkip = sInterruptChecksToSkip;
    2935             : 
    2936          70 :   if (mInterruptsEnabled) {
    2937          38 :     mReflowStartTime = TimeStamp::Now();
    2938             :   }
    2939          70 : }
    2940             : 
    2941             : bool
    2942         162 : nsPresContext::CheckForInterrupt(nsIFrame* aFrame)
    2943             : {
    2944         162 :   if (mHasPendingInterrupt) {
    2945           0 :     mShell->FrameNeedsToContinueReflow(aFrame);
    2946           0 :     return true;
    2947             :   }
    2948             : 
    2949         162 :   if (!sGotInterruptEnv) {
    2950           2 :     sGotInterruptEnv = true;
    2951           2 :     GetInterruptEnv();
    2952             :   }
    2953             : 
    2954         162 :   if (!mInterruptsEnabled) {
    2955          60 :     return false;
    2956             :   }
    2957             : 
    2958         102 :   if (mInterruptChecksToSkip > 0) {
    2959         102 :     --mInterruptChecksToSkip;
    2960         102 :     return false;
    2961             :   }
    2962           0 :   mInterruptChecksToSkip = sInterruptChecksToSkip;
    2963             : 
    2964             :   // Don't interrupt if it's been less than sInterruptTimeout since we started
    2965             :   // the reflow.
    2966           0 :   mHasPendingInterrupt =
    2967           0 :     TimeStamp::Now() - mReflowStartTime > sInterruptTimeout &&
    2968           0 :     HavePendingInputEvent() &&
    2969           0 :     !IsChrome();
    2970             : 
    2971           0 :   if (mPendingInterruptFromTest) {
    2972           0 :     mPendingInterruptFromTest = false;
    2973           0 :     mHasPendingInterrupt = true;
    2974             :   }
    2975             : 
    2976           0 :   if (mHasPendingInterrupt) {
    2977             : #ifdef NOISY_INTERRUPTIBLE_REFLOW
    2978             :     printf("*** DETECTED pending interrupt (time=%lld)\n", PR_Now());
    2979             : #endif /* NOISY_INTERRUPTIBLE_REFLOW */
    2980           0 :     mShell->FrameNeedsToContinueReflow(aFrame);
    2981             :   }
    2982           0 :   return mHasPendingInterrupt;
    2983             : }
    2984             : 
    2985             : nsIFrame*
    2986           6 : nsPresContext::GetPrimaryFrameFor(nsIContent* aContent)
    2987             : {
    2988           6 :   NS_PRECONDITION(aContent, "Don't do that");
    2989          12 :   if (GetPresShell() &&
    2990           6 :       GetPresShell()->GetDocument() == aContent->GetComposedDoc()) {
    2991           6 :     return aContent->GetPrimaryFrame();
    2992             :   }
    2993           0 :   return nullptr;
    2994             : }
    2995             : 
    2996             : size_t
    2997          21 : nsPresContext::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
    2998             : {
    2999          21 :   return mLangGroupFontPrefs.SizeOfExcludingThis(aMallocSizeOf);
    3000             : 
    3001             :   // Measurement of other members may be added later if DMD finds it is
    3002             :   // worthwhile.
    3003             : }
    3004             : 
    3005             : bool
    3006         305 : nsPresContext::IsRootContentDocument() const
    3007             : {
    3008             :   // We are a root content document if: we are not a resource doc, we are
    3009             :   // not chrome, and we either have no parent or our parent is chrome.
    3010         305 :   if (mDocument->IsResourceDoc()) {
    3011          96 :     return false;
    3012             :   }
    3013         209 :   if (IsChrome()) {
    3014         118 :     return false;
    3015             :   }
    3016             :   // We may not have a root frame, so use views.
    3017          91 :   nsView* view = PresShell()->GetViewManager()->GetRootView();
    3018          91 :   if (!view) {
    3019           0 :     return false;
    3020             :   }
    3021          91 :   view = view->GetParent(); // anonymous inner view
    3022          91 :   if (!view) {
    3023          90 :     return true;
    3024             :   }
    3025           1 :   view = view->GetParent(); // subdocumentframe's view
    3026           1 :   if (!view) {
    3027           0 :     return true;
    3028             :   }
    3029             : 
    3030           1 :   nsIFrame* f = view->GetFrame();
    3031           1 :   return (f && f->PresContext()->IsChrome());
    3032             : }
    3033             : 
    3034             : void
    3035           2 : nsPresContext::NotifyNonBlankPaint()
    3036             : {
    3037           2 :   MOZ_ASSERT(!mHadNonBlankPaint);
    3038           2 :   mHadNonBlankPaint = true;
    3039           2 :   if (IsRootContentDocument()) {
    3040           2 :     RefPtr<nsDOMNavigationTiming> timing = mDocument->GetNavigationTiming();
    3041           1 :     if (timing) {
    3042           1 :       timing->NotifyNonBlankPaintForRootContentDocument();
    3043             :     }
    3044             : 
    3045           1 :     mFirstNonBlankPaintTime = TimeStamp::Now();
    3046             :   }
    3047           2 : }
    3048             : 
    3049          84 : bool nsPresContext::GetPaintFlashing() const
    3050             : {
    3051          84 :   if (!mPaintFlashingInitialized) {
    3052          20 :     bool pref = Preferences::GetBool("nglayout.debug.paint_flashing");
    3053          20 :     if (!pref && IsChrome()) {
    3054           1 :       pref = Preferences::GetBool("nglayout.debug.paint_flashing_chrome");
    3055             :     }
    3056          20 :     mPaintFlashing = pref;
    3057          20 :     mPaintFlashingInitialized = true;
    3058             :   }
    3059          84 :   return mPaintFlashing;
    3060             : }
    3061             : 
    3062             : int32_t
    3063       17632 : nsPresContext::AppUnitsPerDevPixel() const
    3064             : {
    3065       17632 :   return mDeviceContext->AppUnitsPerDevPixel();
    3066             : }
    3067             : 
    3068             : nscoord
    3069           0 : nsPresContext::GfxUnitsToAppUnits(gfxFloat aGfxUnits) const
    3070             : {
    3071           0 :   return mDeviceContext->GfxUnitsToAppUnits(aGfxUnits);
    3072             : }
    3073             : 
    3074             : gfxFloat
    3075         172 : nsPresContext::AppUnitsToGfxUnits(nscoord aAppUnits) const
    3076             : {
    3077         172 :   return mDeviceContext->AppUnitsToGfxUnits(aAppUnits);
    3078             : }
    3079             : 
    3080             : bool
    3081           0 : nsPresContext::IsDeviceSizePageSize()
    3082             : {
    3083           0 :   bool isDeviceSizePageSize = false;
    3084           0 :   nsCOMPtr<nsIDocShell> docShell(mContainer);
    3085           0 :   if (docShell) {
    3086           0 :     isDeviceSizePageSize = docShell->GetDeviceSizeIsPageSize();
    3087             :   }
    3088           0 :   return isDeviceSizePageSize;
    3089             : }
    3090             : 
    3091             : uint64_t
    3092           0 : nsPresContext::GetRestyleGeneration() const
    3093             : {
    3094           0 :   if (!mRestyleManager) {
    3095           0 :     return 0;
    3096             :   }
    3097           0 :   return mRestyleManager->GetRestyleGeneration();
    3098             : }
    3099             : 
    3100             : uint64_t
    3101           4 : nsPresContext::GetUndisplayedRestyleGeneration() const
    3102             : {
    3103           4 :   if (!mRestyleManager) {
    3104           0 :     return 0;
    3105             :   }
    3106           4 :   return mRestyleManager->GetUndisplayedRestyleGeneration();
    3107             : }
    3108             : 
    3109             : nsBidi&
    3110           0 : nsPresContext::GetBidiEngine()
    3111             : {
    3112           0 :   MOZ_ASSERT(NS_IsMainThread());
    3113             : 
    3114           0 :   if (!mBidiEngine) {
    3115           0 :     mBidiEngine.reset(new nsBidi());
    3116             :   }
    3117           0 :   return *mBidiEngine;
    3118             : }
    3119             : 
    3120          27 : nsRootPresContext::nsRootPresContext(nsIDocument* aDocument,
    3121          27 :                                      nsPresContextType aType)
    3122             :   : nsPresContext(aDocument, aType),
    3123          27 :     mDOMGeneration(0)
    3124             : {
    3125          27 : }
    3126             : 
    3127           0 : nsRootPresContext::~nsRootPresContext()
    3128             : {
    3129           0 :   NS_ASSERTION(mRegisteredPlugins.Count() == 0,
    3130             :                "All plugins should have been unregistered");
    3131           0 :   CancelAllDidPaintTimers();
    3132           0 :   CancelApplyPluginGeometryTimer();
    3133           0 : }
    3134             : 
    3135             : /* virtual */ void
    3136           3 : nsRootPresContext::Detach()
    3137             : {
    3138           3 :   CancelAllDidPaintTimers();
    3139             :   // XXXmats maybe also CancelApplyPluginGeometryTimer(); ?
    3140           3 :   nsPresContext::Detach();
    3141           3 : }
    3142             : 
    3143             : void
    3144           0 : nsRootPresContext::RegisterPluginForGeometryUpdates(nsIContent* aPlugin)
    3145             : {
    3146           0 :   mRegisteredPlugins.PutEntry(aPlugin);
    3147           0 : }
    3148             : 
    3149             : void
    3150           0 : nsRootPresContext::UnregisterPluginForGeometryUpdates(nsIContent* aPlugin)
    3151             : {
    3152           0 :   mRegisteredPlugins.RemoveEntry(aPlugin);
    3153           0 : }
    3154             : 
    3155             : void
    3156           0 : nsRootPresContext::ComputePluginGeometryUpdates(nsIFrame* aFrame,
    3157             :                                                 nsDisplayListBuilder* aBuilder,
    3158             :                                                 nsDisplayList* aList)
    3159             : {
    3160           0 :   if (mRegisteredPlugins.Count() == 0) {
    3161           0 :     return;
    3162             :   }
    3163             : 
    3164             :   // Initially make the next state for each plugin descendant of aFrame be
    3165             :   // "hidden". Plugins that are visible will have their next state set to
    3166             :   // unhidden by nsDisplayPlugin::ComputeVisibility.
    3167           0 :   for (auto iter = mRegisteredPlugins.Iter(); !iter.Done(); iter.Next()) {
    3168           0 :     auto f = static_cast<nsPluginFrame*>(iter.Get()->GetKey()->GetPrimaryFrame());
    3169           0 :     if (!f) {
    3170           0 :       NS_WARNING("Null frame in ComputePluginGeometryUpdates");
    3171           0 :       continue;
    3172             :     }
    3173           0 :     if (!nsLayoutUtils::IsAncestorFrameCrossDoc(aFrame, f)) {
    3174             :       // f is not managed by this frame so we should ignore it.
    3175           0 :       continue;
    3176             :     }
    3177           0 :     f->SetEmptyWidgetConfiguration();
    3178             :   }
    3179             : 
    3180           0 :   if (aBuilder) {
    3181           0 :     MOZ_ASSERT(aList);
    3182           0 :     nsIFrame* rootFrame = FrameManager()->GetRootFrame();
    3183             : 
    3184           0 :     if (rootFrame && aBuilder->ContainsPluginItem()) {
    3185           0 :       aBuilder->SetForPluginGeometry();
    3186           0 :       aBuilder->SetAccurateVisibleRegions();
    3187             :       // Merging and flattening has already been done and we should not do it
    3188             :       // again. nsDisplayScroll(Info)Layer doesn't support trying to flatten
    3189             :       // again.
    3190           0 :       aBuilder->SetAllowMergingAndFlattening(false);
    3191           0 :       nsRegion region = rootFrame->GetVisualOverflowRectRelativeToSelf();
    3192             :       // nsDisplayPlugin::ComputeVisibility will automatically set a non-hidden
    3193             :       // widget configuration for the plugin, if it's visible.
    3194           0 :       aList->ComputeVisibilityForRoot(aBuilder, &region);
    3195             :     }
    3196             :   }
    3197             : 
    3198             : #ifdef XP_MACOSX
    3199             :   // We control painting of Mac plugins, so just apply geometry updates now.
    3200             :   // This is not happening during a paint event.
    3201             :   ApplyPluginGeometryUpdates();
    3202             : #else
    3203           0 :   if (XRE_IsParentProcess()) {
    3204           0 :     InitApplyPluginGeometryTimer();
    3205             :   }
    3206             : #endif
    3207             : }
    3208             : 
    3209             : static void
    3210           0 : ApplyPluginGeometryUpdatesCallback(nsITimer *aTimer, void *aClosure)
    3211             : {
    3212           0 :   static_cast<nsRootPresContext*>(aClosure)->ApplyPluginGeometryUpdates();
    3213           0 : }
    3214             : 
    3215             : void
    3216           0 : nsRootPresContext::InitApplyPluginGeometryTimer()
    3217             : {
    3218           0 :   if (mApplyPluginGeometryTimer) {
    3219           0 :     return;
    3220             :   }
    3221             : 
    3222             :   // We'll apply the plugin geometry updates during the next compositing paint in this
    3223             :   // presContext (either from nsPresShell::WillPaintWindow or from
    3224             :   // nsPresShell::DidPaintWindow, depending on the platform).  But paints might
    3225             :   // get optimized away if the old plugin geometry covers the invalid region,
    3226             :   // so set a backup timer to do this too.  We want to make sure this
    3227             :   // won't fire before our normal paint notifications, if those would
    3228             :   // update the geometry, so set it for double the refresh driver interval.
    3229           0 :   mApplyPluginGeometryTimer = CreateTimer(ApplyPluginGeometryUpdatesCallback,
    3230             :                                           "ApplyPluginGeometryUpdatesCallback",
    3231           0 :                                           nsRefreshDriver::DefaultInterval() * 2);
    3232             : }
    3233             : 
    3234             : void
    3235           6 : nsRootPresContext::CancelApplyPluginGeometryTimer()
    3236             : {
    3237           6 :   if (mApplyPluginGeometryTimer) {
    3238           0 :     mApplyPluginGeometryTimer->Cancel();
    3239           0 :     mApplyPluginGeometryTimer = nullptr;
    3240             :   }
    3241           6 : }
    3242             : 
    3243             : #ifndef XP_MACOSX
    3244             : 
    3245             : static bool
    3246           0 : HasOverlap(const LayoutDeviceIntPoint& aOffset1,
    3247             :            const nsTArray<LayoutDeviceIntRect>& aClipRects1,
    3248             :            const LayoutDeviceIntPoint& aOffset2,
    3249             :            const nsTArray<LayoutDeviceIntRect>& aClipRects2)
    3250             : {
    3251           0 :   LayoutDeviceIntPoint offsetDelta = aOffset1 - aOffset2;
    3252           0 :   for (uint32_t i = 0; i < aClipRects1.Length(); ++i) {
    3253           0 :     for (uint32_t j = 0; j < aClipRects2.Length(); ++j) {
    3254           0 :       if ((aClipRects1[i] + offsetDelta).Intersects(aClipRects2[j])) {
    3255           0 :         return true;
    3256             :       }
    3257             :     }
    3258             :   }
    3259           0 :   return false;
    3260             : }
    3261             : 
    3262             : /**
    3263             :  * Given a list of plugin windows to move to new locations, sort the list
    3264             :  * so that for each window move, the window moves to a location that
    3265             :  * does not intersect other windows. This minimizes flicker and repainting.
    3266             :  * It's not always possible to do this perfectly, since in general
    3267             :  * we might have cycles. But we do our best.
    3268             :  * We need to take into account that windows are clipped to particular
    3269             :  * regions and the clip regions change as the windows are moved.
    3270             :  */
    3271             : static void
    3272           0 : SortConfigurations(nsTArray<nsIWidget::Configuration>* aConfigurations)
    3273             : {
    3274           0 :   if (aConfigurations->Length() > 10) {
    3275             :     // Give up, we don't want to get bogged down here
    3276           0 :     return;
    3277             :   }
    3278             : 
    3279           0 :   nsTArray<nsIWidget::Configuration> pluginsToMove;
    3280           0 :   pluginsToMove.SwapElements(*aConfigurations);
    3281             : 
    3282             :   // Our algorithm is quite naive. At each step we try to identify
    3283             :   // a window that can be moved to its new location that won't overlap
    3284             :   // any other windows at the new location. If there is no such
    3285             :   // window, we just move the last window in the list anyway.
    3286           0 :   while (!pluginsToMove.IsEmpty()) {
    3287             :     // Find a window whose destination does not overlap any other window
    3288             :     uint32_t i;
    3289           0 :     for (i = 0; i + 1 < pluginsToMove.Length(); ++i) {
    3290           0 :       nsIWidget::Configuration* config = &pluginsToMove[i];
    3291           0 :       bool foundOverlap = false;
    3292           0 :       for (uint32_t j = 0; j < pluginsToMove.Length(); ++j) {
    3293           0 :         if (i == j)
    3294           0 :           continue;
    3295           0 :         LayoutDeviceIntRect bounds = pluginsToMove[j].mChild->GetBounds();
    3296           0 :         AutoTArray<LayoutDeviceIntRect,1> clipRects;
    3297           0 :         pluginsToMove[j].mChild->GetWindowClipRegion(&clipRects);
    3298           0 :         if (HasOverlap(bounds.TopLeft(), clipRects,
    3299           0 :                        config->mBounds.TopLeft(),
    3300             :                        config->mClipRegion)) {
    3301           0 :           foundOverlap = true;
    3302           0 :           break;
    3303             :         }
    3304             :       }
    3305           0 :       if (!foundOverlap)
    3306           0 :         break;
    3307             :     }
    3308             :     // Note that we always move the last plugin in pluginsToMove, if we
    3309             :     // can't find any other plugin to move
    3310           0 :     aConfigurations->AppendElement(pluginsToMove[i]);
    3311           0 :     pluginsToMove.RemoveElementAt(i);
    3312             :   }
    3313             : }
    3314             : 
    3315             : static void
    3316           3 : PluginGetGeometryUpdate(nsTHashtable<nsRefPtrHashKey<nsIContent>>& aPlugins,
    3317             :                         nsTArray<nsIWidget::Configuration>* aConfigurations)
    3318             : {
    3319           3 :   for (auto iter = aPlugins.Iter(); !iter.Done(); iter.Next()) {
    3320           0 :     auto f = static_cast<nsPluginFrame*>(iter.Get()->GetKey()->GetPrimaryFrame());
    3321           0 :     if (!f) {
    3322           0 :       NS_WARNING("Null frame in PluginGeometryUpdate");
    3323           0 :       continue;
    3324             :     }
    3325           0 :     f->GetWidgetConfiguration(aConfigurations);
    3326             :   }
    3327           3 : }
    3328             : 
    3329             : #endif  // #ifndef XP_MACOSX
    3330             : 
    3331             : static void
    3332           3 : PluginDidSetGeometry(nsTHashtable<nsRefPtrHashKey<nsIContent>>& aPlugins)
    3333             : {
    3334           3 :   for (auto iter = aPlugins.Iter(); !iter.Done(); iter.Next()) {
    3335           0 :     auto f = static_cast<nsPluginFrame*>(iter.Get()->GetKey()->GetPrimaryFrame());
    3336           0 :     if (!f) {
    3337           0 :       NS_WARNING("Null frame in PluginDidSetGeometry");
    3338           0 :       continue;
    3339             :     }
    3340           0 :     f->DidSetWidgetGeometry();
    3341             :   }
    3342           3 : }
    3343             : 
    3344             : void
    3345           3 : nsRootPresContext::ApplyPluginGeometryUpdates()
    3346             : {
    3347             : #ifndef XP_MACOSX
    3348           3 :   CancelApplyPluginGeometryTimer();
    3349             : 
    3350           6 :   nsTArray<nsIWidget::Configuration> configurations;
    3351           3 :   PluginGetGeometryUpdate(mRegisteredPlugins, &configurations);
    3352             :   // Walk mRegisteredPlugins and ask each plugin for its configuration
    3353           3 :   if (!configurations.IsEmpty()) {
    3354           0 :     nsIWidget* widget = configurations[0].mChild->GetParent();
    3355           0 :     NS_ASSERTION(widget, "Plugins must have a parent window");
    3356           0 :     SortConfigurations(&configurations);
    3357           0 :     widget->ConfigureChildren(configurations);
    3358             :   }
    3359             : #endif  // #ifndef XP_MACOSX
    3360             : 
    3361           3 :   PluginDidSetGeometry(mRegisteredPlugins);
    3362           3 : }
    3363             : 
    3364             : void
    3365           2 : nsRootPresContext::CollectPluginGeometryUpdates(LayerManager* aLayerManager)
    3366             : {
    3367             : #ifndef XP_MACOSX
    3368             :   // Collect and pass plugin widget configurations down to the compositor
    3369             :   // for transmission to the chrome process.
    3370           2 :   NS_ASSERTION(aLayerManager, "layer manager is invalid!");
    3371           2 :   mozilla::layers::ClientLayerManager* clm = aLayerManager->AsClientLayerManager();
    3372             : 
    3373           2 :   nsTArray<nsIWidget::Configuration> configurations;
    3374             :   // If there aren't any plugins to configure, clear the plugin data cache
    3375             :   // in the layer system.
    3376           2 :   if (!mRegisteredPlugins.Count() && clm) {
    3377           2 :     clm->StorePluginWidgetConfigurations(configurations);
    3378           2 :     return;
    3379             :   }
    3380           0 :   PluginGetGeometryUpdate(mRegisteredPlugins, &configurations);
    3381           0 :   if (configurations.IsEmpty()) {
    3382           0 :     PluginDidSetGeometry(mRegisteredPlugins);
    3383           0 :     return;
    3384             :   }
    3385           0 :   SortConfigurations(&configurations);
    3386           0 :   if (clm) {
    3387           0 :     clm->StorePluginWidgetConfigurations(configurations);
    3388             :   }
    3389           0 :   PluginDidSetGeometry(mRegisteredPlugins);
    3390             : #endif  // #ifndef XP_MACOSX
    3391             : }
    3392             : 
    3393             : void
    3394           5 : nsRootPresContext::EnsureEventualDidPaintEvent(uint64_t aTransactionId)
    3395             : {
    3396           5 :   for (NotifyDidPaintTimer& t : mNotifyDidPaintTimers) {
    3397           0 :     if (t.mTransactionId == aTransactionId) {
    3398           0 :       return;
    3399             :     }
    3400             :   }
    3401             : 
    3402          10 :   nsCOMPtr<nsITimer> timer = do_CreateInstance("@mozilla.org/timer;1");
    3403           5 :   timer->SetTarget(Document()->EventTargetFor(TaskCategory::Other));
    3404           5 :   if (timer) {
    3405          10 :     RefPtr<nsRootPresContext> self = this;
    3406           5 :     nsresult rv = timer->InitWithCallback(
    3407          24 :       NewNamedTimerCallback([self, aTransactionId](){
    3408           0 :         nsAutoScriptBlocker blockScripts;
    3409           0 :         self->NotifyDidPaintForSubtree(aTransactionId);
    3410          10 :     }, "NotifyDidPaintForSubtree"), 100, nsITimer::TYPE_ONE_SHOT);
    3411             : 
    3412           5 :     if (NS_SUCCEEDED(rv)) {
    3413           5 :       NotifyDidPaintTimer* t = mNotifyDidPaintTimers.AppendElement();
    3414           5 :       t->mTransactionId = aTransactionId;
    3415           5 :       t->mTimer = timer;
    3416             :     }
    3417             :   }
    3418             : }
    3419             : 
    3420             : void
    3421          27 : nsRootPresContext::CancelDidPaintTimers(uint64_t aTransactionId)
    3422             : {
    3423          27 :   uint32_t i = 0;
    3424          37 :   while (i < mNotifyDidPaintTimers.Length()) {
    3425           5 :     if (mNotifyDidPaintTimers[i].mTransactionId <= aTransactionId) {
    3426           4 :       mNotifyDidPaintTimers[i].mTimer->Cancel();
    3427           4 :       mNotifyDidPaintTimers.RemoveElementAt(i);
    3428             :     } else {
    3429           1 :       i++;
    3430             :     }
    3431             :   }
    3432          27 : }
    3433             : 
    3434             : void
    3435           8 : nsRootPresContext::CancelAllDidPaintTimers()
    3436             : {
    3437           8 :   for (uint32_t i = 0; i < mNotifyDidPaintTimers.Length(); i++) {
    3438           0 :     mNotifyDidPaintTimers[i].mTimer->Cancel();
    3439             :   }
    3440           8 :   mNotifyDidPaintTimers.Clear();
    3441           8 : }
    3442             : 
    3443             : void
    3444          11 : nsRootPresContext::AddWillPaintObserver(nsIRunnable* aRunnable)
    3445             : {
    3446          11 :   if (!mWillPaintFallbackEvent.IsPending()) {
    3447           3 :     mWillPaintFallbackEvent = new RunWillPaintObservers(this);
    3448           9 :     Document()->Dispatch("RunWillPaintObservers",
    3449             :                          TaskCategory::Other,
    3450           9 :                          do_AddRef(mWillPaintFallbackEvent.get()));
    3451             :   }
    3452          11 :   mWillPaintObservers.AppendElement(aRunnable);
    3453          11 : }
    3454             : 
    3455             : /**
    3456             :  * Run all runnables that need to get called before the next paint.
    3457             :  */
    3458             : void
    3459          52 : nsRootPresContext::FlushWillPaintObservers()
    3460             : {
    3461          52 :   mWillPaintFallbackEvent = nullptr;
    3462         104 :   nsTArray<nsCOMPtr<nsIRunnable> > observers;
    3463          52 :   observers.SwapElements(mWillPaintObservers);
    3464          63 :   for (uint32_t i = 0; i < observers.Length(); ++i) {
    3465          11 :     observers[i]->Run();
    3466             :   }
    3467          52 : }
    3468             : 
    3469             : size_t
    3470          21 : nsRootPresContext::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
    3471             : {
    3472          21 :   return nsPresContext::SizeOfExcludingThis(aMallocSizeOf);
    3473             : 
    3474             :   // Measurement of the following members may be added later if DMD finds it is
    3475             :   // worthwhile:
    3476             :   // - mNotifyDidPaintTimer
    3477             :   // - mRegisteredPlugins
    3478             :   // - mWillPaintObservers
    3479             :   // - mWillPaintFallbackEvent
    3480             : }

Generated by: LCOV version 1.13