tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

SVGDocumentWrapper.h (4417B)


      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 /* This class wraps an SVG document, for use by VectorImage objects. */
      7 
      8 #ifndef mozilla_image_SVGDocumentWrapper_h
      9 #define mozilla_image_SVGDocumentWrapper_h
     10 
     11 #include "nsCOMPtr.h"
     12 #include "nsIStreamListener.h"
     13 #include "nsIObserver.h"
     14 #include "nsIDocumentViewer.h"
     15 #include "nsWeakReference.h"
     16 #include "nsSize.h"
     17 
     18 class nsIRequest;
     19 class nsILoadGroup;
     20 class nsIFrame;
     21 
     22 namespace mozilla {
     23 class PresShell;
     24 namespace dom {
     25 class SVGSVGElement;
     26 class SVGDocument;
     27 }  // namespace dom
     28 
     29 namespace image {
     30 class AutoRestoreSVGState;
     31 
     32 class SVGDocumentWrapper final : public nsIStreamListener,
     33                                 public nsIObserver,
     34                                 public nsSupportsWeakReference {
     35 public:
     36  SVGDocumentWrapper();
     37 
     38  NS_DECL_ISUPPORTS
     39  NS_DECL_NSISTREAMLISTENER
     40  NS_DECL_NSIREQUESTOBSERVER
     41  NS_DECL_NSIOBSERVER
     42 
     43  enum Dimension { eWidth, eHeight };
     44 
     45  /**
     46   * Returns the wrapped document, or nullptr on failure. (No AddRef.)
     47   */
     48  mozilla::dom::SVGDocument* GetDocument() const;
     49 
     50  /**
     51   * Returns the root <svg> element for the wrapped document, or nullptr on
     52   * failure.
     53   */
     54  mozilla::dom::SVGSVGElement* GetSVGRootElement() const;
     55 
     56  /**
     57   * Returns the root nsIFrame* for the wrapped document, or nullptr on failure.
     58   *
     59   * @return the root nsIFrame* for the wrapped document, or nullptr on failure.
     60   */
     61  nsIFrame* GetRootLayoutFrame() const;
     62 
     63  /**
     64   * Returns the mozilla::PresShell for the wrapped document.
     65   */
     66  mozilla::PresShell* GetPresShell() const { return mViewer->GetPresShell(); }
     67 
     68  /**
     69   * Modifier to update the viewport dimensions of the wrapped document. This
     70   * method performs a synchronous "FlushType::Layout" on the wrapped document,
     71   * since a viewport-change affects layout.
     72   *
     73   * @param aViewportSize The new viewport dimensions.
     74   */
     75  void UpdateViewportBounds(const nsIntSize& aViewportSize);
     76 
     77  /**
     78   * If an SVG image's helper document has a pending notification for an
     79   * override on the root node's "preserveAspectRatio" attribute, then this
     80   * method will flush that notification so that the image can paint correctly.
     81   * (First, though, it sets the mIgnoreInvalidation flag so that we won't
     82   * notify the image's observers and trigger unwanted repaint-requests.)
     83   */
     84  void FlushImageTransformInvalidation();
     85 
     86  /**
     87   * Returns a bool indicating whether the document has any SMIL animations.
     88   *
     89   * @return true if the document has any SMIL animations. Else, false.
     90   */
     91  bool IsAnimated() const;
     92 
     93  /**
     94   * Indicates whether we should currently ignore rendering invalidations sent
     95   * from the wrapped SVG doc.
     96   *
     97   * @return true if we should ignore invalidations sent from this SVG doc.
     98   */
     99  bool ShouldIgnoreInvalidation() const { return mIgnoreInvalidation; }
    100 
    101  /**
    102   * Returns a bool indicating whether the document is currently drawing.
    103   *
    104   * @return true if the document is drawing. Else, false.
    105   */
    106  bool IsDrawing() const { return mIsDrawing; }
    107 
    108  /**
    109   * Methods to control animation.
    110   */
    111  void StartAnimation();
    112  void StopAnimation();
    113  void ResetAnimation();
    114  float GetCurrentTimeAsFloat() const;
    115  void SetCurrentTime(float aTime);
    116  void TickRefreshDriver();
    117 
    118  /**
    119   * Force a layout flush of the underlying SVG document.
    120   */
    121  void FlushLayout();
    122 
    123 private:
    124  friend class AutoRestoreSVGState;
    125 
    126  ~SVGDocumentWrapper();
    127 
    128  nsresult SetupViewer(nsIRequest* aRequest, nsIDocumentViewer** aViewer,
    129                       nsILoadGroup** aLoadGroup);
    130  void DestroyViewer();
    131  void RegisterForXPCOMShutdown();
    132  void UnregisterForXPCOMShutdown();
    133 
    134  nsCOMPtr<nsIDocumentViewer> mViewer;
    135  nsCOMPtr<nsILoadGroup> mLoadGroup;
    136  nsCOMPtr<nsIStreamListener> mListener;
    137  bool mIgnoreInvalidation;
    138  bool mRegisteredForXPCOMShutdown;
    139  bool mIsDrawing;
    140 };
    141 
    142 }  // namespace image
    143 }  // namespace mozilla
    144 
    145 /**
    146 * Casting SVGDocumentWrapper to nsISupports is ambiguous. This method handles
    147 * that.
    148 */
    149 inline nsISupports* ToSupports(mozilla::image::SVGDocumentWrapper* p) {
    150  return NS_ISUPPORTS_CAST(nsSupportsWeakReference*, p);
    151 }
    152 
    153 #endif  // mozilla_image_SVGDocumentWrapper_h