| File: | Source/WebKit/Shared/WebHitTestResultData.cpp |
| Warning: | line 128, column 23 Local variable 'cachedImage' is uncounted and unsafe |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* |
| 2 | * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies) |
| 3 | * Copyright (C) 2013-2025 Apple Inc. All rights reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Library General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Library General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Library General Public License |
| 16 | * along with this library; see the file COPYING.LIB. If not, write to |
| 17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 18 | * Boston, MA 02110-1301, USA. |
| 19 | */ |
| 20 | |
| 21 | #include "config.h" |
| 22 | #include "WebHitTestResultData.h" |
| 23 | |
| 24 | #include "ShareableBitmapUtilities.h" |
| 25 | #include "WebFrame.h" |
| 26 | #include <WebCore/DocumentView.h> |
| 27 | #include <WebCore/ElementInlines.h> |
| 28 | #include <WebCore/EventHandler.h> |
| 29 | #include <WebCore/FrameDestructionObserverInlines.h> |
| 30 | #include <WebCore/HitTestResult.h> |
| 31 | #include <WebCore/LocalFrame.h> |
| 32 | #include <WebCore/LocalFrameView.h> |
| 33 | #include <WebCore/NavigationAction.h> |
| 34 | #include <WebCore/Node.h> |
| 35 | #include <WebCore/RenderImage.h> |
| 36 | #include <WebCore/SharedBuffer.h> |
| 37 | #include <WebCore/Text.h> |
| 38 | #include <wtf/URL.h> |
| 39 | #include <wtf/text/WTFString.h> |
| 40 | |
| 41 | namespace WebKit { |
| 42 | using namespace WebCore; |
| 43 | |
| 44 | static WebHitTestResultData::ElementType elementTypeFromHitTestResult(const HitTestResult& hitTestResult) |
| 45 | { |
| 46 | if (!hitTestResult.hasMediaElement()) |
| 47 | return WebHitTestResultData::ElementType::None; |
| 48 | |
| 49 | return hitTestResult.mediaIsVideo() ? WebHitTestResultData::ElementType::Video : WebHitTestResultData::ElementType::Audio; |
| 50 | } |
| 51 | |
| 52 | static RefPtr<WebFrame> webFrameFromHitTestResult(const HitTestResult& hitTestResult) |
| 53 | { |
| 54 | RefPtr coreFrame = hitTestResult.frame(); |
| 55 | if (!coreFrame) |
| 56 | return nullptr; |
| 57 | |
| 58 | return WebFrame::fromCoreFrame(*coreFrame); |
| 59 | } |
| 60 | |
| 61 | static std::optional<ResourceResponse> linkLocalResourceFromHitTestResult(const HitTestResult& hitTestResult) |
| 62 | { |
| 63 | if (!hitTestResult.hasLocalDataForLinkURL()) |
| 64 | return std::nullopt; |
| 65 | |
| 66 | RefPtr webFrame = webFrameFromHitTestResult(hitTestResult); |
| 67 | if (!webFrame) |
| 68 | return std::nullopt; |
| 69 | |
| 70 | return webFrame->resourceResponseForURL(hitTestResult.absoluteLinkURL()); |
| 71 | } |
| 72 | |
| 73 | static String imageSuggestedFilenameFromHitTestResult(const HitTestResult& hitTestResult) |
| 74 | { |
| 75 | if (!hitTestResult.hasEntireImage()) |
| 76 | return nullString(); |
| 77 | |
| 78 | RefPtr webFrame = webFrameFromHitTestResult(hitTestResult); |
| 79 | if (!webFrame) |
| 80 | return nullString(); |
| 81 | |
| 82 | return webFrame->suggestedFilenameForResourceWithURL(hitTestResult.absoluteImageURL()); |
| 83 | } |
| 84 | |
| 85 | WebHitTestResultData::WebHitTestResultData() = default; |
| 86 | |
| 87 | WebHitTestResultData::WebHitTestResultData(const HitTestResult& hitTestResult, const String& tooltipText, bool includeImage) |
| 88 | : absoluteImageURL(hitTestResult.absoluteImageURL().string()) |
| 89 | , absolutePDFURL(hitTestResult.absolutePDFURL().string()) |
| 90 | , absoluteLinkURL(hitTestResult.absoluteLinkURL().string()) |
| 91 | , absoluteMediaURL(hitTestResult.absoluteMediaURL().string()) |
| 92 | , linkLabel(hitTestResult.textContent()) |
| 93 | , linkTitle(hitTestResult.titleDisplayString()) |
| 94 | , linkSuggestedFilename(hitTestResult.linkSuggestedFilename()) |
| 95 | , imageSuggestedFilename(imageSuggestedFilenameFromHitTestResult(hitTestResult)) |
| 96 | , isContentEditable(hitTestResult.isContentEditable()) |
| 97 | , elementBoundingBox(elementBoundingBoxInWindowCoordinates(hitTestResult)) |
| 98 | , isScrollbar(IsScrollbar::No) |
| 99 | , isSelected(hitTestResult.isSelected()) |
| 100 | , isTextNode(is<Text>(hitTestResult.innerNode())) |
| 101 | , isOverTextInsideFormControlElement(hitTestResult.isOverTextInsideFormControlElement()) |
| 102 | , isDownloadableMedia(hitTestResult.isDownloadableMedia()) |
| 103 | , mediaIsInFullscreen(hitTestResult.mediaIsInFullscreen()) |
| 104 | , isActivePDFAnnotation(false) |
| 105 | , elementType(elementTypeFromHitTestResult(hitTestResult)) |
| 106 | , frameInfo(frameInfoDataFromHitTestResult(hitTestResult)) |
| 107 | , targetFrame(hitTestResult.targetFrame() ? std::optional(hitTestResult.targetFrame()->frameID()) : std::nullopt) |
| 108 | , tooltipText(tooltipText) |
| 109 | , hasEntireImage(hitTestResult.hasEntireImage()) |
| 110 | , allowsFollowingLink(hitTestResult.allowsFollowingLink()) |
| 111 | , allowsFollowingImageURL(hitTestResult.allowsFollowingImageURL()) |
| 112 | , linkLocalResourceResponse(linkLocalResourceFromHitTestResult(hitTestResult)) |
| 113 | { |
| 114 | if (auto* scrollbar = hitTestResult.scrollbar()) |
| 115 | isScrollbar = scrollbar->orientation() == ScrollbarOrientation::Horizontal ? IsScrollbar::Horizontal : IsScrollbar::Vertical; |
| 116 | |
| 117 | if (!includeImage) |
| 118 | return; |
| 119 | |
| 120 | if (RefPtr image = hitTestResult.image()) { |
| 121 | if (RefPtr buffer = image->data()) |
| 122 | imageSharedMemory = WebCore::SharedMemory::copyBuffer(*buffer); |
| 123 | } |
| 124 | |
| 125 | if (RefPtr target = hitTestResult.innerNonSharedNode()) { |
| 126 | if (CheckedPtr renderer = dynamicDowncast<RenderImage>(target->renderer())) { |
| 127 | imageBitmap = createShareableBitmap(*renderer); |
| 128 | if (auto* cachedImage = renderer->cachedImage()) { |
Local variable 'cachedImage' is uncounted and unsafe | |
| 129 | if (RefPtr image = cachedImage->image()) |
| 130 | sourceImageMIMEType = image->mimeType(); |
| 131 | } |
| 132 | |
| 133 | imageText = [&]() -> String { |
| 134 | if (RefPtr element = dynamicDowncast<Element>(target.get())) { |
| 135 | auto& title = element->attributeWithoutSynchronization(HTMLNames::titleAttr); |
| 136 | if (!title.isEmpty()) |
| 137 | return title; |
| 138 | } |
| 139 | |
| 140 | return renderer->altText(); |
| 141 | }(); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | WebHitTestResultData::WebHitTestResultData(const HitTestResult& hitTestResult, const String& tooltipText) |
| 147 | : WebHitTestResultData(hitTestResult, tooltipText, false) { } |
| 148 | |
| 149 | WebHitTestResultData::WebHitTestResultData(const HitTestResult& hitTestResult, bool includeImage) |
| 150 | : WebHitTestResultData(hitTestResult, String(), includeImage) { } |
| 151 | |
| 152 | WebHitTestResultData::WebHitTestResultData(const String& absoluteImageURL, const String& absolutePDFURL, const String& absoluteLinkURL, const String& absoluteMediaURL, const String& linkLabel, const String& linkTitle, const String& linkSuggestedFilename, const String& imageSuggestedFilename, bool isContentEditable, const WebCore::IntRect& elementBoundingBox, const WebKit::WebHitTestResultData::IsScrollbar& isScrollbar, bool isSelected, bool isTextNode, bool isOverTextInsideFormControlElement, bool isDownloadableMedia, bool mediaIsInFullscreen, bool isActivePDFAnnotation, const WebHitTestResultData::ElementType& elementType, std::optional<FrameInfoData>&& frameInfo, std::optional<WebCore::FrameIdentifier> targetFrame, std::optional<WebCore::RemoteUserInputEventData> remoteUserInputEventData, const String& lookupText, const String& tooltipText, const String& imageText, std::optional<WebCore::SharedMemory::Handle>&& imageHandle, const RefPtr<WebCore::ShareableBitmap>& imageBitmap, const String& sourceImageMIMEType, bool hasEntireImage, bool allowsFollowingLink, bool allowsFollowingImageURL, std::optional<WebCore::ResourceResponse>&& linkLocalResourceResponse, |
| 153 | #if PLATFORM(MAC)(defined 1 && 1) |
| 154 | const WebHitTestResultPlatformData& platformData, |
| 155 | #endif |
| 156 | const WebCore::DictionaryPopupInfo& dictionaryPopupInfo, const RefPtr<WebCore::TextIndicator>& linkTextIndicator) |
| 157 | : absoluteImageURL(absoluteImageURL) |
| 158 | , absolutePDFURL(absolutePDFURL) |
| 159 | , absoluteLinkURL(absoluteLinkURL) |
| 160 | , absoluteMediaURL(absoluteMediaURL) |
| 161 | , linkLabel(linkLabel) |
| 162 | , linkTitle(linkTitle) |
| 163 | , linkSuggestedFilename(linkSuggestedFilename) |
| 164 | , imageSuggestedFilename(imageSuggestedFilename) |
| 165 | , isContentEditable(isContentEditable) |
| 166 | , elementBoundingBox(elementBoundingBox) |
| 167 | , isScrollbar(isScrollbar) |
| 168 | , isSelected(isSelected) |
| 169 | , isTextNode(isTextNode) |
| 170 | , isOverTextInsideFormControlElement(isOverTextInsideFormControlElement) |
| 171 | , isDownloadableMedia(isDownloadableMedia) |
| 172 | , mediaIsInFullscreen(mediaIsInFullscreen) |
| 173 | , isActivePDFAnnotation(isActivePDFAnnotation) |
| 174 | , elementType(elementType) |
| 175 | , frameInfo(WTF::move(frameInfo)) |
| 176 | , targetFrame(targetFrame) |
| 177 | , remoteUserInputEventData(remoteUserInputEventData) |
| 178 | , lookupText(lookupText) |
| 179 | , tooltipText(tooltipText) |
| 180 | , imageText(imageText) |
| 181 | , imageBitmap(imageBitmap) |
| 182 | , sourceImageMIMEType(sourceImageMIMEType) |
| 183 | , hasEntireImage(hasEntireImage) |
| 184 | , allowsFollowingLink(allowsFollowingLink) |
| 185 | , allowsFollowingImageURL(allowsFollowingImageURL) |
| 186 | , linkLocalResourceResponse(linkLocalResourceResponse) |
| 187 | #if PLATFORM(MAC)(defined 1 && 1) |
| 188 | , platformData(platformData) |
| 189 | #endif |
| 190 | , dictionaryPopupInfo(dictionaryPopupInfo) |
| 191 | , linkTextIndicator(linkTextIndicator) |
| 192 | { |
| 193 | if (imageHandle) |
| 194 | imageSharedMemory = WebCore::SharedMemory::map(WTF::move(*imageHandle), WebCore::SharedMemory::Protection::ReadOnly); |
| 195 | } |
| 196 | |
| 197 | WebHitTestResultData::~WebHitTestResultData() |
| 198 | { |
| 199 | } |
| 200 | |
| 201 | IntRect WebHitTestResultData::elementBoundingBoxInWindowCoordinates(const WebCore::HitTestResult& hitTestResult) |
| 202 | { |
| 203 | RefPtr node = hitTestResult.innerNonSharedNode(); |
| 204 | if (!node) |
| 205 | return IntRect(); |
| 206 | |
| 207 | RefPtr frame = node->document().frame(); |
| 208 | if (!frame) |
| 209 | return IntRect(); |
| 210 | |
| 211 | RefPtr view = frame->view(); |
| 212 | if (!view) |
| 213 | return IntRect(); |
| 214 | |
| 215 | CheckedPtr renderer = node->renderer(); |
| 216 | if (!renderer) |
| 217 | return IntRect(); |
| 218 | |
| 219 | return view->contentsToWindow(renderer->absoluteBoundingBoxRect()); |
| 220 | } |
| 221 | |
| 222 | std::optional<WebCore::SharedMemory::Handle> WebHitTestResultData::getImageSharedMemoryHandle() const |
| 223 | { |
| 224 | std::optional<WebCore::SharedMemory::Handle> imageHandle = std::nullopt; |
| 225 | if (RefPtr memory = imageSharedMemory; memory && !memory->span().empty()) { |
| 226 | if (auto handle = memory->createHandle(WebCore::SharedMemory::Protection::ReadOnly)) |
| 227 | imageHandle = WTF::move(*handle); |
| 228 | } |
| 229 | return imageHandle; |
| 230 | } |
| 231 | |
| 232 | std::optional<FrameInfoData> WebHitTestResultData::frameInfoDataFromHitTestResult(const WebCore::HitTestResult& hitTestResult) |
| 233 | { |
| 234 | RefPtr webFrame = webFrameFromHitTestResult(hitTestResult); |
| 235 | if (!webFrame) |
| 236 | return std::nullopt; |
| 237 | |
| 238 | return webFrame->info(); |
| 239 | } |
| 240 | |
| 241 | } // WebKit |