View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.html;
16  
17  import java.util.Set;
18  
19  import org.htmlunit.Page;
20  import org.htmlunit.StringWebResponse;
21  import org.htmlunit.WebResponse;
22  import org.htmlunit.WebWindow;
23  import org.htmlunit.WebWindowImpl;
24  
25  /**
26   * The web window for a frame or iframe.
27   *
28   * @author Brad Clarke
29   * @author Ahmed Ashour
30   * @author Ronald Brill
31   */
32  public class FrameWindow extends WebWindowImpl {
33  
34      /** The different deny states. */
35      public enum PageDenied {
36          /** NONE. */
37          NONE,
38  
39          /** BY_X_FRAME_OPTIONS. */
40          BY_X_FRAME_OPTIONS,
41  
42          /** BY_CONTENT_SECURIRY_POLICY. */
43          BY_CONTENT_SECURIRY_POLICY }
44  
45      private final BaseFrameElement frame_;
46      private PageDenied pageDenied_;
47      // the sandbox tokens for the documents loaded into this window; null if not sandboxed
48      private Set<String> sandboxTokens_;
49  
50      /**
51       * Creates an instance for a given frame.
52       */
53      FrameWindow(final BaseFrameElement frame) {
54          super(frame.getPage().getWebClient());
55          frame_ = frame;
56          performRegistration();
57  
58          final WebWindowImpl parent = (WebWindowImpl) frame_.getPage().getEnclosingWindow();
59          parent.addChildWindow(this);
60      }
61  
62      /**
63       * Returns the sandbox tokens (the allowed features, e.g. 'allow-scripts') used for the
64       * documents loaded into this window.
65       *
66       * @return the tokens or {@code null} if the documents are not sandboxed
67       */
68      public Set<String> getSandboxTokens() {
69          return sandboxTokens_;
70      }
71  
72      /**
73       * Sets the sandbox tokens used for the documents loaded into this window; like the browsers,
74       * the tokens are determined when the frame starts loading.
75       *
76       * @param sandboxTokens the tokens or {@code null} if the documents are not sandboxed
77       */
78      void setSandboxTokens(final Set<String> sandboxTokens) {
79          sandboxTokens_ = sandboxTokens;
80      }
81  
82      /**
83       * {@inheritDoc}
84       * A FrameWindow shares it's name with it's containing frame.
85       */
86      @Override
87      public String getName() {
88          return frame_.getNameAttribute();
89      }
90  
91      /**
92       * {@inheritDoc}
93       * A FrameWindow shares it's name with it's containing frame.
94       */
95      @Override
96      public void setName(final String name) {
97          frame_.setNameAttribute(name);
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public WebWindow getParentWindow() {
105         return frame_.getPage().getEnclosingWindow();
106     }
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     public WebWindow getTopWindow() {
113         return getParentWindow().getTopWindow();
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     protected boolean isJavaScriptInitializationNeeded(final Page page) {
121         return getScriptableObject() == null
122             || !(page.getWebResponse() instanceof StringWebResponse
123                     && ((StringWebResponse) page.getWebResponse()).isFromJavascript());
124         // TODO: find a better way to distinguish content written by document.open(),...
125     }
126 
127     /**
128      * Returns the HTML page in which the &lt;frame&gt; or &lt;iframe&gt; tag is contained
129      * for this frame window.
130      * This is a facility method for <code>(HtmlPage) (getParentWindow().getEnclosedPage())</code>.
131      * @return the page in the parent window
132      */
133     public HtmlPage getEnclosingPage() {
134         return (HtmlPage) frame_.getPage();
135     }
136 
137     /**
138      * {@inheritDoc}
139      */
140     @Override
141     public void setEnclosedPage(final Page page) {
142         setPageDenied(PageDenied.NONE);
143         super.setEnclosedPage(page);
144 
145         // we have updated a frame window by javascript write();
146         // so we have to disable future updates during initialization
147         // see org.htmlunit.html.HtmlPage.loadFrames()
148         final WebResponse webResponse = page.getWebResponse();
149         if (webResponse instanceof StringWebResponse response) {
150             if (response.isFromJavascript()) {
151                 final BaseFrameElement frame = getFrameElement();
152                 frame.setContentLoaded();
153             }
154         }
155     }
156 
157     /**
158      * Gets the DOM node of the (i)frame containing this window.
159      * @return the DOM node
160      */
161     public BaseFrameElement getFrameElement() {
162         return frame_;
163     }
164 
165     /**
166      * Gives a basic representation for debugging purposes.
167      * @return a basic representation
168      */
169     @Override
170     public String toString() {
171         return "FrameWindow[name=\"" + getName() + "\"]";
172     }
173 
174     /**
175      * Closes this frame window.
176      */
177     public void close() {
178         final WebWindowImpl parent = (WebWindowImpl) getParentWindow();
179         parent.removeChildWindow(this);
180         getWebClient().deregisterWebWindow(this);
181     }
182 
183     /**
184      * Marks that the page content as denied.
185      * @param pageDenied the new state
186      */
187     public void setPageDenied(final PageDenied pageDenied) {
188         pageDenied_ = pageDenied;
189     }
190 
191     /**
192      * Returns PageDenied if the page was denied.
193      *
194      * @return PageDenied if the page was denied
195      */
196     public PageDenied getPageDenied() {
197         return pageDenied_;
198     }
199 }