summaryrefslogtreecommitdiffstats
path: root/image/SVGDocumentWrapper.h
blob: 75924a4ea58166a2714b6a56cf4386aa3a72b605 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* This class wraps an SVG document, for use by VectorImage objects. */

#ifndef mozilla_image_SVGDocumentWrapper_h
#define mozilla_image_SVGDocumentWrapper_h

#include "mozilla/Attributes.h"

#include "nsCOMPtr.h"
#include "nsIStreamListener.h"
#include "nsIObserver.h"
#include "nsIContentViewer.h"
#include "nsWeakReference.h"
#include "nsSize.h"

class nsIRequest;
class nsILoadGroup;
class nsIFrame;

#define OBSERVER_SVC_CID "@mozilla.org/observer-service;1"

namespace mozilla {
class PresShell;
namespace dom {
class SVGSVGElement;
class SVGDocument;
}  // namespace dom

namespace image {
class AutoRestoreSVGState;

class SVGDocumentWrapper final : public nsIStreamListener,
                                 public nsIObserver,
                                 public nsSupportsWeakReference {
 public:
  SVGDocumentWrapper();

  NS_DECL_ISUPPORTS
  NS_DECL_NSISTREAMLISTENER
  NS_DECL_NSIREQUESTOBSERVER
  NS_DECL_NSIOBSERVER

  enum Dimension { eWidth, eHeight };

  /**
   * Returns the wrapped document, or nullptr on failure. (No AddRef.)
   */
  mozilla::dom::SVGDocument* GetDocument();

  /**
   * Returns the root <svg> element for the wrapped document, or nullptr on
   * failure.
   */
  mozilla::dom::SVGSVGElement* GetRootSVGElem();

  /**
   * Returns the root nsIFrame* for the wrapped document, or nullptr on failure.
   *
   * @return the root nsIFrame* for the wrapped document, or nullptr on failure.
   */
  nsIFrame* GetRootLayoutFrame();

  /**
   * Returns the mozilla::PresShell for the wrapped document.
   */
  inline mozilla::PresShell* GetPresShell() { return mViewer->GetPresShell(); }

  /**
   * Modifier to update the viewport dimensions of the wrapped document. This
   * method performs a synchronous "FlushType::Layout" on the wrapped document,
   * since a viewport-change affects layout.
   *
   * @param aViewportSize The new viewport dimensions.
   */
  void UpdateViewportBounds(const nsIntSize& aViewportSize);

  /**
   * If an SVG image's helper document has a pending notification for an
   * override on the root node's "preserveAspectRatio" attribute, then this
   * method will flush that notification so that the image can paint correctly.
   * (First, though, it sets the mIgnoreInvalidation flag so that we won't
   * notify the image's observers and trigger unwanted repaint-requests.)
   */
  void FlushImageTransformInvalidation();

  /**
   * Returns a bool indicating whether the document has any SMIL animations.
   *
   * @return true if the document has any SMIL animations. Else, false.
   */
  bool IsAnimated();

  /**
   * Indicates whether we should currently ignore rendering invalidations sent
   * from the wrapped SVG doc.
   *
   * @return true if we should ignore invalidations sent from this SVG doc.
   */
  bool ShouldIgnoreInvalidation() { return mIgnoreInvalidation; }

  /**
   * Returns a bool indicating whether the document is currently drawing.
   *
   * @return true if the document is drawing. Else, false.
   */
  bool IsDrawing() const { return mIsDrawing; }

  /**
   * Methods to control animation.
   */
  void StartAnimation();
  void StopAnimation();
  void ResetAnimation();
  float GetCurrentTimeAsFloat();
  void SetCurrentTime(float aTime);
  void TickRefreshDriver();

  /**
   * Force a layout flush of the underlying SVG document.
   */
  void FlushLayout();

 private:
  friend class AutoRestoreSVGState;

  ~SVGDocumentWrapper();

  nsresult SetupViewer(nsIRequest* aRequest, nsIContentViewer** aViewer,
                       nsILoadGroup** aLoadGroup);
  void DestroyViewer();
  void RegisterForXPCOMShutdown();
  void UnregisterForXPCOMShutdown();

  nsCOMPtr<nsIContentViewer> mViewer;
  nsCOMPtr<nsILoadGroup> mLoadGroup;
  nsCOMPtr<nsIStreamListener> mListener;
  bool mIgnoreInvalidation;
  bool mRegisteredForXPCOMShutdown;
  bool mIsDrawing;
};

}  // namespace image
}  // namespace mozilla

/**
 * Casting SVGDocumentWrapper to nsISupports is ambiguous. This method handles
 * that.
 */
inline nsISupports* ToSupports(mozilla::image::SVGDocumentWrapper* p) {
  return NS_ISUPPORTS_CAST(nsSupportsWeakReference*, p);
}

#endif  // mozilla_image_SVGDocumentWrapper_h