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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2000 Simon Hausmann (hausmann@kde.org)
* (C) 2001 Dirk Mueller (mueller@kde.org)
* Copyright (C) 2004-2023 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "HTMLFrameElementBase.h"
#include "Document.h"
#include "ElementInlines.h"
#include "EventLoop.h"
#include "FocusController.h"
#include "FrameLoader.h"
#include "HTMLNames.h"
#include "JSDOMBindingSecurity.h"
#include "LocalFrame.h"
#include "LocalFrameView.h"
#include "Page.h"
#include "RenderWidget.h"
#include "ScriptController.h"
#include "Settings.h"
#include "SubframeLoader.h"
#include <wtf/IsoMallocInlines.h>
#include <wtf/URL.h>
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLFrameElementBase);
using namespace HTMLNames;
HTMLFrameElementBase::HTMLFrameElementBase(const QualifiedName& tagName, Document& document)
: HTMLFrameOwnerElement(tagName, document, CreateHTMLFrameElementBase)
{
}
bool HTMLFrameElementBase::canLoadScriptURL(const URL& scriptURL) const
{
return canLoadURL(scriptURL);
}
bool HTMLFrameElementBase::canLoad() const
{
// FIXME: Why is it valuable to return true when m_frameURL is empty?
// FIXME: After openURL replaces an empty URL with the blank URL, this may no longer necessarily return true.
return m_frameURL.isEmpty() || canLoadURL(m_frameURL);
}
bool HTMLFrameElementBase::canLoadURL(const String& relativeURL) const
{
return canLoadURL(document().completeURL(relativeURL));
}
// Note that unlike HTMLPlugInImageElement::canLoadURL this uses ScriptController::canAccessFromCurrentOrigin.
bool HTMLFrameElementBase::canLoadURL(const URL& completeURL) const
{
if (completeURL.protocolIsJavaScript()) {
RefPtr<Document> contentDocument = this->contentDocument();
if (contentDocument && !ScriptController::canAccessFromCurrentOrigin(contentDocument->frame(), document()))
return false;
}
return !isProhibitedSelfReference(completeURL);
}
void HTMLFrameElementBase::openURL(LockHistory lockHistory, LockBackForwardList lockBackForwardList)
{
if (!canLoad())
return;
if (m_frameURL.isEmpty())
m_frameURL = AtomString { aboutBlankURL().string() };
RefPtr parentFrame { document().frame() };
if (!parentFrame)
return;
auto frameName = getNameAttribute();
if (frameName.isNull() && UNLIKELY(document().settings().needsFrameNameFallbackToIdQuirk()))
frameName = getIdAttribute();
if (shouldLoadFrameLazily()) {
parentFrame->loader().subframeLoader().createFrameIfNecessary(*this, frameName);
return;
}
document().willLoadFrameElement(document().completeURL(m_frameURL));
parentFrame->loader().subframeLoader().requestFrame(*this, m_frameURL, frameName, lockHistory, lockBackForwardList);
}
void HTMLFrameElementBase::attributeChanged(const QualifiedName& name, const AtomString& oldValue, const AtomString& newValue, AttributeModificationReason attributeModificationReason)
{
// FIXME: trimming whitespace is probably redundant with the URL parser
if (name == srcdocAttr) {
if (newValue.isNull())
setLocation(attributeWithoutSynchronization(srcAttr).string().trim(isASCIIWhitespace));
else
setLocation("about:srcdoc"_s);
} else if (name == srcAttr && !hasAttributeWithoutSynchronization(srcdocAttr))
setLocation(newValue.string().trim(isASCIIWhitespace));
else
HTMLFrameOwnerElement::attributeChanged(name, oldValue, newValue, attributeModificationReason);
}
Node::InsertedIntoAncestorResult HTMLFrameElementBase::insertedIntoAncestor(InsertionType insertionType, ContainerNode& parentOfInsertedTree)
{
HTMLFrameOwnerElement::insertedIntoAncestor(insertionType, parentOfInsertedTree);
if (insertionType.connectedToDocument)
return InsertedIntoAncestorResult::NeedsPostInsertionCallback;
return InsertedIntoAncestorResult::Done;
}
void HTMLFrameElementBase::didFinishInsertingNode()
{
if (!isConnected())
return;
// DocumentFragments don't kick off any loads.
if (!document().frame())
return;
if (!SubframeLoadingDisabler::canLoadFrame(*this))
return;
if (!renderer())
invalidateStyleAndRenderersForSubtree();
auto work = [this, weakThis = WeakPtr { *this }] {
if (!weakThis)
return;
Ref<HTMLFrameElementBase> protectedThis { *this };
m_openingURLAfterInserting = true;
if (isConnected())
openURL();
m_openingURLAfterInserting = false;
};
if (!m_openingURLAfterInserting)
work();
else
document().eventLoop().queueTask(TaskSource::DOMManipulation, WTFMove(work));
}
void HTMLFrameElementBase::didAttachRenderers()
{
if (RenderWidget* part = renderWidget()) {
if (RefPtr frame = dynamicDowncast<LocalFrame>(contentFrame()))
part->setWidget(frame->view());
}
}
void HTMLFrameElementBase::setLocation(const String& str)
{
if (document().settings().needsAcrobatFrameReloadingQuirk() && m_frameURL == str)
return;
m_frameURL = AtomString(str);
if (isConnected())
openURL(LockHistory::No, LockBackForwardList::No);
}
void HTMLFrameElementBase::setLocation(JSC::JSGlobalObject& state, const String& newLocation)
{
if (WTF::protocolIsJavaScript(newLocation)) {
if (!BindingSecurity::shouldAllowAccessToNode(state, contentDocument()))
return;
}
setLocation(newLocation);
}
bool HTMLFrameElementBase::supportsFocus() const
{
return true;
}
void HTMLFrameElementBase::setFocus(bool received, FocusVisibility visibility)
{
HTMLFrameOwnerElement::setFocus(received, visibility);
if (Page* page = document().page()) {
CheckedRef focusController { page->focusController() };
if (received)
focusController->setFocusedFrame(dynamicDowncast<LocalFrame>(contentFrame()));
else if (focusController->focusedFrame() == contentFrame()) // Focus may have already been given to another frame, don't take it away.
focusController->setFocusedFrame(nullptr);
}
}
bool HTMLFrameElementBase::isURLAttribute(const Attribute& attribute) const
{
return attribute.name() == srcAttr || attribute.name() == longdescAttr || HTMLFrameOwnerElement::isURLAttribute(attribute);
}
bool HTMLFrameElementBase::isHTMLContentAttribute(const Attribute& attribute) const
{
return attribute.name() == srcdocAttr || HTMLFrameOwnerElement::isHTMLContentAttribute(attribute);
}
ScrollbarMode HTMLFrameElementBase::scrollingMode() const
{
auto scrollingAttribute = attributeWithoutSynchronization(scrollingAttr);
return equalLettersIgnoringASCIICase(scrollingAttribute, "no"_s)
|| equalLettersIgnoringASCIICase(scrollingAttribute, "noscroll"_s)
|| equalLettersIgnoringASCIICase(scrollingAttribute, "off"_s)
? ScrollbarMode::AlwaysOff : ScrollbarMode::Auto;
}
} // namespace WebCore
|