1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_dom_HTMLIFrameElement_h
8 #define mozilla_dom_HTMLIFrameElement_h
9 
10 #include "mozilla/Attributes.h"
11 #include "nsGenericHTMLElement.h"
12 #include "nsGenericHTMLFrameElement.h"
13 #include "nsDOMTokenList.h"
14 
15 namespace mozilla {
16 namespace dom {
17 
18 class FeaturePolicy;
19 
20 class HTMLIFrameElement final : public nsGenericHTMLFrameElement {
21  public:
22   explicit HTMLIFrameElement(
23       already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
24       FromParser aFromParser = NOT_FROM_PARSER);
25 
NS_IMPL_FROMNODE_HTML_WITH_TAG(HTMLIFrameElement,iframe)26   NS_IMPL_FROMNODE_HTML_WITH_TAG(HTMLIFrameElement, iframe)
27 
28   // nsISupports
29   NS_DECL_ISUPPORTS_INHERITED
30   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLIFrameElement,
31                                            nsGenericHTMLFrameElement)
32 
33   // Element
34   virtual bool IsInteractiveHTMLContent() const override { return true; }
35 
36   // nsIContent
37   virtual bool ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
38                               const nsAString& aValue,
39                               nsIPrincipal* aMaybeScriptedPrincipal,
40                               nsAttrValue& aResult) override;
41   NS_IMETHOD_(bool) IsAttributeMapped(const nsAtom* aAttribute) const override;
42   virtual nsMapRuleToAttributesFunc GetAttributeMappingFunction()
43       const override;
44 
45   virtual nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override;
46 
47   void BindToBrowsingContext(BrowsingContext* aBrowsingContext);
48 
49   uint32_t GetSandboxFlags() const;
50 
51   // Web IDL binding methods
GetSrc(nsString & aSrc)52   void GetSrc(nsString& aSrc) const {
53     GetURIAttr(nsGkAtoms::src, nullptr, aSrc);
54   }
SetSrc(const nsAString & aSrc,nsIPrincipal * aTriggeringPrincipal,ErrorResult & aError)55   void SetSrc(const nsAString& aSrc, nsIPrincipal* aTriggeringPrincipal,
56               ErrorResult& aError) {
57     SetHTMLAttr(nsGkAtoms::src, aSrc, aTriggeringPrincipal, aError);
58   }
GetSrcdoc(DOMString & aSrcdoc)59   void GetSrcdoc(DOMString& aSrcdoc) {
60     GetHTMLAttr(nsGkAtoms::srcdoc, aSrcdoc);
61   }
SetSrcdoc(const nsAString & aSrcdoc,ErrorResult & aError)62   void SetSrcdoc(const nsAString& aSrcdoc, ErrorResult& aError) {
63     SetHTMLAttr(nsGkAtoms::srcdoc, aSrcdoc, aError);
64   }
GetName(DOMString & aName)65   void GetName(DOMString& aName) { GetHTMLAttr(nsGkAtoms::name, aName); }
SetName(const nsAString & aName,ErrorResult & aError)66   void SetName(const nsAString& aName, ErrorResult& aError) {
67     SetHTMLAttr(nsGkAtoms::name, aName, aError);
68   }
Sandbox()69   nsDOMTokenList* Sandbox() {
70     if (!mSandbox) {
71       mSandbox =
72           new nsDOMTokenList(this, nsGkAtoms::sandbox, sSupportedSandboxTokens);
73     }
74     return mSandbox;
75   }
76 
AllowFullscreen()77   bool AllowFullscreen() const {
78     return GetBoolAttr(nsGkAtoms::allowfullscreen);
79   }
80 
SetAllowFullscreen(bool aAllow,ErrorResult & aError)81   void SetAllowFullscreen(bool aAllow, ErrorResult& aError) {
82     SetHTMLBoolAttr(nsGkAtoms::allowfullscreen, aAllow, aError);
83   }
84 
GetWidth(DOMString & aWidth)85   void GetWidth(DOMString& aWidth) { GetHTMLAttr(nsGkAtoms::width, aWidth); }
SetWidth(const nsAString & aWidth,ErrorResult & aError)86   void SetWidth(const nsAString& aWidth, ErrorResult& aError) {
87     SetHTMLAttr(nsGkAtoms::width, aWidth, aError);
88   }
GetHeight(DOMString & aHeight)89   void GetHeight(DOMString& aHeight) {
90     GetHTMLAttr(nsGkAtoms::height, aHeight);
91   }
SetHeight(const nsAString & aHeight,ErrorResult & aError)92   void SetHeight(const nsAString& aHeight, ErrorResult& aError) {
93     SetHTMLAttr(nsGkAtoms::height, aHeight, aError);
94   }
95   using nsGenericHTMLFrameElement::GetContentDocument;
96   using nsGenericHTMLFrameElement::GetContentWindow;
GetAlign(DOMString & aAlign)97   void GetAlign(DOMString& aAlign) { GetHTMLAttr(nsGkAtoms::align, aAlign); }
SetAlign(const nsAString & aAlign,ErrorResult & aError)98   void SetAlign(const nsAString& aAlign, ErrorResult& aError) {
99     SetHTMLAttr(nsGkAtoms::align, aAlign, aError);
100   }
GetAllow(DOMString & aAllow)101   void GetAllow(DOMString& aAllow) { GetHTMLAttr(nsGkAtoms::allow, aAllow); }
SetAllow(const nsAString & aAllow,ErrorResult & aError)102   void SetAllow(const nsAString& aAllow, ErrorResult& aError) {
103     SetHTMLAttr(nsGkAtoms::allow, aAllow, aError);
104   }
GetScrolling(DOMString & aScrolling)105   void GetScrolling(DOMString& aScrolling) {
106     GetHTMLAttr(nsGkAtoms::scrolling, aScrolling);
107   }
SetScrolling(const nsAString & aScrolling,ErrorResult & aError)108   void SetScrolling(const nsAString& aScrolling, ErrorResult& aError) {
109     SetHTMLAttr(nsGkAtoms::scrolling, aScrolling, aError);
110   }
GetFrameBorder(DOMString & aFrameBorder)111   void GetFrameBorder(DOMString& aFrameBorder) {
112     GetHTMLAttr(nsGkAtoms::frameborder, aFrameBorder);
113   }
SetFrameBorder(const nsAString & aFrameBorder,ErrorResult & aError)114   void SetFrameBorder(const nsAString& aFrameBorder, ErrorResult& aError) {
115     SetHTMLAttr(nsGkAtoms::frameborder, aFrameBorder, aError);
116   }
GetLongDesc(nsAString & aLongDesc)117   void GetLongDesc(nsAString& aLongDesc) const {
118     GetURIAttr(nsGkAtoms::longdesc, nullptr, aLongDesc);
119   }
SetLongDesc(const nsAString & aLongDesc,ErrorResult & aError)120   void SetLongDesc(const nsAString& aLongDesc, ErrorResult& aError) {
121     SetHTMLAttr(nsGkAtoms::longdesc, aLongDesc, aError);
122   }
GetMarginWidth(DOMString & aMarginWidth)123   void GetMarginWidth(DOMString& aMarginWidth) {
124     GetHTMLAttr(nsGkAtoms::marginwidth, aMarginWidth);
125   }
SetMarginWidth(const nsAString & aMarginWidth,ErrorResult & aError)126   void SetMarginWidth(const nsAString& aMarginWidth, ErrorResult& aError) {
127     SetHTMLAttr(nsGkAtoms::marginwidth, aMarginWidth, aError);
128   }
GetMarginHeight(DOMString & aMarginHeight)129   void GetMarginHeight(DOMString& aMarginHeight) {
130     GetHTMLAttr(nsGkAtoms::marginheight, aMarginHeight);
131   }
SetMarginHeight(const nsAString & aMarginHeight,ErrorResult & aError)132   void SetMarginHeight(const nsAString& aMarginHeight, ErrorResult& aError) {
133     SetHTMLAttr(nsGkAtoms::marginheight, aMarginHeight, aError);
134   }
SetReferrerPolicy(const nsAString & aReferrer,ErrorResult & aError)135   void SetReferrerPolicy(const nsAString& aReferrer, ErrorResult& aError) {
136     SetHTMLAttr(nsGkAtoms::referrerpolicy, aReferrer, aError);
137   }
GetReferrerPolicy(nsAString & aReferrer)138   void GetReferrerPolicy(nsAString& aReferrer) {
139     GetEnumAttr(nsGkAtoms::referrerpolicy, "", aReferrer);
140   }
GetSVGDocument(nsIPrincipal & aSubjectPrincipal)141   Document* GetSVGDocument(nsIPrincipal& aSubjectPrincipal) {
142     return GetContentDocument(aSubjectPrincipal);
143   }
Mozbrowser()144   bool Mozbrowser() const { return GetBoolAttr(nsGkAtoms::mozbrowser); }
SetMozbrowser(bool aAllow,ErrorResult & aError)145   void SetMozbrowser(bool aAllow, ErrorResult& aError) {
146     SetHTMLBoolAttr(nsGkAtoms::mozbrowser, aAllow, aError);
147   }
148   using nsGenericHTMLFrameElement::SetMozbrowser;
149   // nsGenericHTMLFrameElement::GetFrameLoader is fine
150   // nsGenericHTMLFrameElement::GetAppManifestURL is fine
151 
152   // The fullscreen flag is set to true only when requestFullscreen is
153   // explicitly called on this <iframe> element. In case this flag is
154   // set, the fullscreen state of this element will not be reverted
155   // automatically when its subdocument exits fullscreen.
FullscreenFlag()156   bool FullscreenFlag() const { return mFullscreenFlag; }
SetFullscreenFlag(bool aValue)157   void SetFullscreenFlag(bool aValue) { mFullscreenFlag = aValue; }
158 
159   mozilla::dom::FeaturePolicy* FeaturePolicy() const;
160 
161  protected:
162   virtual ~HTMLIFrameElement();
163 
164   virtual JSObject* WrapNode(JSContext* aCx,
165                              JS::Handle<JSObject*> aGivenProto) override;
166 
167   virtual nsresult AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
168                                 const nsAttrValue* aValue,
169                                 const nsAttrValue* aOldValue,
170                                 nsIPrincipal* aMaybeScriptedPrincipal,
171                                 bool aNotify) override;
172   virtual nsresult OnAttrSetButNotChanged(int32_t aNamespaceID, nsAtom* aName,
173                                           const nsAttrValueOrString& aValue,
174                                           bool aNotify) override;
175 
176  private:
177   static void MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
178                                     MappedDeclarations&);
179 
180   static const DOMTokenListSupportedToken sSupportedSandboxTokens[];
181 
182   void RefreshFeaturePolicy(bool aParseAllowAttribute);
183 
184   // If this iframe has a 'srcdoc' attribute, the document's origin will be
185   // returned. Otherwise, if this iframe has a 'src' attribute, the origin will
186   // be the parsing of its value as URL. If the URL is invalid, or 'src'
187   // attribute doesn't exist, the origin will be the document's origin.
188   already_AddRefed<nsIPrincipal> GetFeaturePolicyDefaultOrigin() const;
189 
190   /**
191    * This function is called by AfterSetAttr and OnAttrSetButNotChanged.
192    * This function will be called by AfterSetAttr whether the attribute is being
193    * set or unset.
194    *
195    * @param aNamespaceID the namespace of the attr being set
196    * @param aName the localname of the attribute being set
197    * @param aNotify Whether we plan to notify document observers.
198    */
199   void AfterMaybeChangeAttr(int32_t aNamespaceID, nsAtom* aName, bool aNotify);
200 
201   /**
202    * Feature policy inheritance is broken in cross process model, so we may
203    * have to store feature policy in browsingContext when neccesary.
204    */
205   void MaybeStoreCrossOriginFeaturePolicy();
206 
207   RefPtr<dom::FeaturePolicy> mFeaturePolicy;
208   RefPtr<nsDOMTokenList> mSandbox;
209 };
210 
211 }  // namespace dom
212 }  // namespace mozilla
213 
214 #endif
215