| File: | Volumes/Data/worker/macOS-Safer-CPP-Checks-EWS/build/Source/WebCore/page/Frame.cpp |
| Warning: | line 219, column 18 Local variable 'child' is uncounted and unsafe |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* |
| 2 | * Copyright (C) 2018-2025 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "Frame.h" |
| 28 | |
| 29 | #include "ContainerNodeInlines.h" |
| 30 | #include "FrameLoader.h" |
| 31 | #include "FrameLoaderClient.h" |
| 32 | #include "HTMLFrameOwnerElement.h" |
| 33 | #include "HTMLIFrameElement.h" |
| 34 | #include "LocalDOMWindow.h" |
| 35 | #include "NavigationScheduler.h" |
| 36 | #include "NodeDocument.h" |
| 37 | #include "OwnerPermissionsPolicyData.h" |
| 38 | #include "Page.h" |
| 39 | #include "RemoteFrame.h" |
| 40 | #include "RenderElement.h" |
| 41 | #include "RenderWidget.h" |
| 42 | #include "ScrollingCoordinator.h" |
| 43 | #include "Settings.h" |
| 44 | #include "WindowProxy.h" |
| 45 | #include <wtf/Assertions.h> |
| 46 | #include <wtf/NeverDestroyed.h> |
| 47 | |
| 48 | namespace WebCore { |
| 49 | |
| 50 | #if ASSERT_ENABLED0 |
| 51 | class FrameLifetimeVerifier { |
| 52 | public: |
| 53 | static FrameLifetimeVerifier& singleton() |
| 54 | { |
| 55 | static NeverDestroyed<FrameLifetimeVerifier> instance; |
| 56 | return instance.get(); |
| 57 | } |
| 58 | |
| 59 | void frameCreated(Frame& frame) |
| 60 | { |
| 61 | auto& pair = m_map.ensure(frame.frameID(), [] { |
| 62 | return std::pair<WeakPtr<LocalFrame>, WeakPtr<RemoteFrame>> { }; |
| 63 | }).iterator->value; |
| 64 | |
| 65 | switch (frame.frameType()) { |
| 66 | case Frame::FrameType::Local: |
| 67 | ASSERT_WITH_MESSAGE(!pair.first, "There should never be two LocalFrames with the same ID in the same process")((void)0); |
| 68 | pair.first = downcast<LocalFrame>(frame); |
| 69 | break; |
| 70 | case Frame::FrameType::Remote: |
| 71 | ASSERT_WITH_MESSAGE(!pair.second, "There should never be two RemoteFrames with the same ID in the same process")((void)0); |
| 72 | pair.second = downcast<RemoteFrame>(frame); |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void frameDestroyed(Frame& frame) |
| 78 | { |
| 79 | auto it = m_map.find(frame.frameID()); |
| 80 | ASSERT(it != m_map.end())((void)0); |
| 81 | auto& pair = it->value; |
| 82 | switch (frame.frameType()) { |
| 83 | case Frame::FrameType::Local: |
| 84 | ASSERT(pair.first == &frame)((void)0); |
| 85 | if (pair.second) |
| 86 | pair.first = nullptr; |
| 87 | else |
| 88 | m_map.remove(it); |
| 89 | break; |
| 90 | case Frame::FrameType::Remote: |
| 91 | ASSERT(pair.second == &frame)((void)0); |
| 92 | if (pair.first) |
| 93 | pair.second = nullptr; |
| 94 | else |
| 95 | m_map.remove(it); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | bool isRootFrameIdentifier(FrameIdentifier identifier) |
| 100 | { |
| 101 | auto it = m_map.find(identifier); |
| 102 | if (it == m_map.end()) |
| 103 | return false; |
| 104 | return it->value.first && it->value.first->isRootFrame(); |
| 105 | } |
| 106 | private: |
| 107 | HashMap<FrameIdentifier, std::pair<WeakPtr<LocalFrame>, WeakPtr<RemoteFrame>>> m_map; |
| 108 | }; |
| 109 | #endif |
| 110 | |
| 111 | Frame::Frame(Page& page, FrameIdentifier frameID, FrameType frameType, HTMLFrameOwnerElement* ownerElement, Frame* parent, Frame* opener, Ref<FrameTreeSyncData>&& frameTreeSyncData, AddToFrameTree addToFrameTree) |
| 112 | : m_page(page) |
| 113 | , m_frameID(frameID) |
| 114 | , m_treeNode(*this, addToFrameTree == AddToFrameTree::Yes ? parent : nullptr) |
| 115 | , m_windowProxy(WindowProxy::create(*this)) |
| 116 | , m_ownerElement(ownerElement) |
| 117 | , m_mainFrame(parent ? page.mainFrame() : *this) |
| 118 | , m_settings(page.settings()) |
| 119 | , m_frameType(frameType) |
| 120 | , m_navigationScheduler(makeUniqueRefWithoutRefCountedCheck<NavigationScheduler>(*this)) |
| 121 | , m_opener(opener) |
| 122 | , m_frameTreeSyncData(WTF::move(frameTreeSyncData)) |
| 123 | { |
| 124 | relaxAdoptionRequirement(); |
| 125 | if (parent && addToFrameTree == AddToFrameTree::Yes) |
| 126 | parent->tree().appendChild(*this); |
| 127 | |
| 128 | if (ownerElement) |
| 129 | ownerElement->setContentFrame(*this); |
| 130 | |
| 131 | if (opener) |
| 132 | opener->m_openedFrames.add(*this); |
| 133 | |
| 134 | #if ASSERT_ENABLED0 |
| 135 | FrameLifetimeVerifier::singleton().frameCreated(*this); |
| 136 | #endif |
| 137 | } |
| 138 | |
| 139 | Frame::~Frame() |
| 140 | { |
| 141 | protectedWindowProxy()->detachFromFrame(); |
| 142 | protectedNavigationScheduler()->cancel(); |
| 143 | |
| 144 | #if ASSERT_ENABLED0 |
| 145 | FrameLifetimeVerifier::singleton().frameDestroyed(*this); |
| 146 | #endif |
| 147 | } |
| 148 | |
| 149 | void Frame::resetWindowProxy() |
| 150 | { |
| 151 | m_windowProxy = WindowProxy::create(*this); |
| 152 | } |
| 153 | |
| 154 | void Frame::detachFromPage() |
| 155 | { |
| 156 | if (isRootFrame()) { |
| 157 | if (RefPtr page = m_page.get()) { |
| 158 | page->removeRootFrame(downcast<LocalFrame>(*this)); |
| 159 | if (RefPtr scrollingCoordinator = page->scrollingCoordinator()) |
| 160 | scrollingCoordinator->rootFrameWasRemoved(frameID()); |
| 161 | } |
| 162 | } |
| 163 | m_page = nullptr; |
| 164 | } |
| 165 | |
| 166 | void Frame::disconnectOwnerElement() |
| 167 | { |
| 168 | if (RefPtr ownerElement = m_ownerElement.get()) { |
| 169 | ownerElement->clearContentFrame(); |
| 170 | m_ownerElement = nullptr; |
| 171 | } |
| 172 | |
| 173 | frameWasDisconnectedFromOwner(); |
| 174 | } |
| 175 | |
| 176 | void Frame::takeWindowProxyAndOpenerFrom(Frame& frame) |
| 177 | { |
| 178 | ASSERT(is<LocalDOMWindow>(window()) != is<LocalDOMWindow>(frame.window()) || page() != frame.page())((void)0); |
| 179 | ASSERT(m_windowProxy->frame() == this)((void)0); |
| 180 | protectedWindowProxy()->detachFromFrame(); |
| 181 | m_windowProxy = frame.windowProxy(); |
| 182 | frame.resetWindowProxy(); |
| 183 | protectedWindowProxy()->replaceFrame(*this); |
| 184 | |
| 185 | ASSERT(!m_opener)((void)0); |
| 186 | m_opener = frame.m_opener; |
| 187 | if (m_opener) |
| 188 | m_opener->m_openedFrames.add(*this); |
| 189 | |
| 190 | for (Ref opened : frame.m_openedFrames) { |
| 191 | ASSERT(opened->m_opener.get() == &frame)((void)0); |
| 192 | opened->m_opener = *this; |
| 193 | m_openedFrames.add(opened); |
| 194 | } |
| 195 | frame.m_openedFrames.clear(); |
| 196 | } |
| 197 | |
| 198 | Ref<WindowProxy> Frame::protectedWindowProxy() const |
| 199 | { |
| 200 | return m_windowProxy; |
| 201 | } |
| 202 | |
| 203 | RefPtr<DOMWindow> Frame::protectedWindow() const |
| 204 | { |
| 205 | return window(); |
| 206 | } |
| 207 | |
| 208 | Ref<NavigationScheduler> Frame::protectedNavigationScheduler() const |
| 209 | { |
| 210 | return m_navigationScheduler.get(); |
| 211 | } |
| 212 | |
| 213 | std::optional<uint64_t> Frame::indexInFrameTreeSiblings() const |
| 214 | { |
| 215 | if (!tree().parent()) |
| 216 | return std::nullopt; |
| 217 | |
| 218 | for (uint64_t i = 0; i < tree().parent()->tree().childCount(); i++) { |
| 219 | if (auto child = tree().parent()->tree().child(i); child->frameID() == this->frameID()) |
Local variable 'child' is uncounted and unsafe | |
| 220 | return i; |
| 221 | } |
| 222 | |
| 223 | ASSERT_NOT_REACHED("This frame should be in its own tree")((void)0); |
| 224 | return std::nullopt; |
| 225 | } |
| 226 | |
| 227 | Vector<uint64_t> Frame::pathToFrame() const |
| 228 | { |
| 229 | Vector<uint64_t> path; |
| 230 | RefPtr current = this; |
| 231 | |
| 232 | while (current) { |
| 233 | if (auto index = current->indexInFrameTreeSiblings()) |
| 234 | path.append(*index); |
| 235 | current = current->tree().parent(); |
| 236 | } |
| 237 | |
| 238 | path.reverse(); |
| 239 | return path; |
| 240 | } |
| 241 | |
| 242 | RenderWidget* Frame::ownerRenderer() const |
| 243 | { |
| 244 | RefPtr ownerElement = this->ownerElement(); |
| 245 | if (!ownerElement) |
| 246 | return nullptr; |
| 247 | // FIXME: If <object> is ever fixed to disassociate itself from frames |
| 248 | // that it has started but canceled, then this can turn into an ASSERT |
| 249 | // since ownerElement would be nullptr when the load is canceled. |
| 250 | // https://bugs.webkit.org/show_bug.cgi?id=18585 |
| 251 | return dynamicDowncast<RenderWidget>(ownerElement->renderer()); |
| 252 | } |
| 253 | |
| 254 | RefPtr<FrameView> Frame::protectedVirtualView() const |
| 255 | { |
| 256 | return virtualView(); |
| 257 | } |
| 258 | |
| 259 | #if ASSERT_ENABLED0 |
| 260 | bool Frame::isRootFrameIdentifier(FrameIdentifier identifier) |
| 261 | { |
| 262 | return FrameLifetimeVerifier::singleton().isRootFrameIdentifier(identifier); |
| 263 | } |
| 264 | #endif |
| 265 | |
| 266 | void Frame::updateOpener(Frame& newOpener, NotifyUIProcess notifyUIProcess) |
| 267 | { |
| 268 | if (notifyUIProcess == NotifyUIProcess::Yes) |
| 269 | loaderClient().updateOpener(newOpener.frameID()); |
| 270 | if (m_opener) |
| 271 | m_opener->m_openedFrames.remove(*this); |
| 272 | newOpener.m_openedFrames.add(*this); |
| 273 | if (RefPtr page = this->page()) |
| 274 | page->setOpenedByDOMWithOpener(true); |
| 275 | m_opener = newOpener; |
| 276 | |
| 277 | reinitializeDocumentSecurityContext(); |
| 278 | } |
| 279 | |
| 280 | void Frame::disownOpener(NotifyUIProcess notifyUIProcess) |
| 281 | { |
| 282 | if (m_opener) { |
| 283 | if (notifyUIProcess == NotifyUIProcess::Yes) |
| 284 | loaderClient().updateOpener(std::nullopt); |
| 285 | m_opener->m_openedFrames.remove(*this); |
| 286 | } |
| 287 | |
| 288 | m_opener = nullptr; |
| 289 | |
| 290 | reinitializeDocumentSecurityContext(); |
| 291 | } |
| 292 | |
| 293 | void Frame::setOpenerForWebKitLegacy(Frame* frame) |
| 294 | { |
| 295 | ASSERT(!m_opener)((void)0); |
| 296 | ASSERT(frame)((void)0); |
| 297 | m_opener = frame; |
| 298 | m_page->setOpenedByDOMWithOpener(true); |
| 299 | reinitializeDocumentSecurityContext(); |
| 300 | } |
| 301 | |
| 302 | void Frame::detachFromAllOpenedFrames() |
| 303 | { |
| 304 | for (Ref frame : std::exchange(m_openedFrames, { })) |
| 305 | frame->m_opener = nullptr; |
| 306 | } |
| 307 | |
| 308 | bool Frame::hasOpenedFrames() const |
| 309 | { |
| 310 | return !m_openedFrames.isEmptyIgnoringNullReferences(); |
| 311 | } |
| 312 | |
| 313 | void Frame::setOwnerElement(HTMLFrameOwnerElement* element) |
| 314 | { |
| 315 | m_ownerElement = element; |
| 316 | if (element) { |
| 317 | element->clearContentFrame(); |
| 318 | element->setContentFrame(*this); |
| 319 | } |
| 320 | updateScrollingMode(); |
| 321 | } |
| 322 | |
| 323 | void Frame::setOwnerPermissionsPolicy(OwnerPermissionsPolicyData&& ownerPermissionsPolicy) |
| 324 | { |
| 325 | m_ownerPermisssionsPolicyOverride = makeUnique<OwnerPermissionsPolicyData>(WTF::move(ownerPermissionsPolicy)); |
| 326 | } |
| 327 | |
| 328 | std::optional<OwnerPermissionsPolicyData> Frame::ownerPermissionsPolicy() const |
| 329 | { |
| 330 | if (m_ownerPermisssionsPolicyOverride) |
| 331 | return *m_ownerPermisssionsPolicyOverride; |
| 332 | |
| 333 | RefPtr owner = ownerElement(); |
| 334 | if (!owner) |
| 335 | return std::nullopt; |
| 336 | |
| 337 | auto documentOrigin = owner->protectedDocument()->securityOrigin().data(); |
| 338 | auto documentPolicy = owner->protectedDocument()->permissionsPolicy(); |
| 339 | |
| 340 | RefPtr iframe = dynamicDowncast<HTMLIFrameElement>(owner); |
| 341 | auto containerPolicy = iframe ? PermissionsPolicy::processPermissionsPolicyAttribute(*iframe) : PermissionsPolicy::PolicyDirective { }; |
| 342 | return OwnerPermissionsPolicyData { WTF::move(documentOrigin), WTF::move(documentPolicy), WTF::move(containerPolicy) }; |
| 343 | } |
| 344 | |
| 345 | void Frame::updateSandboxFlags(SandboxFlags flags, NotifyUIProcess notifyUIProcess) |
| 346 | { |
| 347 | if (notifyUIProcess == NotifyUIProcess::Yes) |
| 348 | loaderClient().updateSandboxFlags(flags); |
| 349 | } |
| 350 | |
| 351 | void Frame::stopForBackForwardCache() |
| 352 | { |
| 353 | if (RefPtr localFrame = dynamicDowncast<LocalFrame>(*this)) |
| 354 | localFrame->loader().stopForBackForwardCache(); |
| 355 | else { |
| 356 | for (RefPtr child = tree().firstChild(); child; child = child->tree().nextSibling()) |
| 357 | child->stopForBackForwardCache(); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | void Frame::updateFrameTreeSyncData(Ref<FrameTreeSyncData>&& data) |
| 362 | { |
| 363 | m_frameTreeSyncData = WTF::move(data); |
| 364 | } |
| 365 | |
| 366 | void Frame::updateFrameTreeSyncData(const FrameTreeSyncSerializationData& data) |
| 367 | { |
| 368 | protectedFrameTreeSyncData()->update(data); |
| 369 | } |
| 370 | |
| 371 | bool Frame::frameCanCreatePaymentSession() const |
| 372 | { |
| 373 | // Prefer the LocalFrame code path when site isolation is disabled. |
| 374 | ASSERT(m_settings->siteIsolationEnabled())((void)0); |
| 375 | return m_frameTreeSyncData->frameCanCreatePaymentSession; |
| 376 | } |
| 377 | |
| 378 | bool Frame::isPrinting() const |
| 379 | { |
| 380 | return m_isPrinting; |
| 381 | } |
| 382 | |
| 383 | void Frame::setPrinting(bool printing, FloatSize pageSize, FloatSize originalPageSize, float maximumShrinkRatio, AdjustViewSize shouldAdjustViewSize, NotifyUIProcess notifyUIProcess) |
| 384 | { |
| 385 | m_isPrinting = printing; |
| 386 | if (notifyUIProcess == NotifyUIProcess::Yes && m_settings->siteIsolationEnabled()) |
| 387 | loaderClient().setPrinting(printing, pageSize, originalPageSize, maximumShrinkRatio, shouldAdjustViewSize); |
| 388 | } |
| 389 | |
| 390 | } // namespace WebCore |