Bug Summary

File:Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/Source/WebCore/loader/archive/cf/LegacyWebArchive.cpp
Warning:line 776, column 31
Local variable 'cachedResource' is uncounted and unsafe

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple arm64-apple-ios26.2.0-simulator -O3 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name UnifiedSource287.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=osx -analyzer-checker=security.insecureAPI.decodeValueOfObjCType -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=non-leaf -ffp-contract=on -fno-rounding-math -target-sdk-version=26.2 -fdepfile-entry=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/SDKSettings.json -fcompatibility-qualified-id-block-type-checking -fvisibility-inlines-hidden-static-local-var -fno-modulemap-allow-subdirectory-search -fdefine-target-os-macros -enable-tlsdesc -target-cpu apple-m1 -target-feature +v8.4a -target-feature +aes -target-feature +altnzcv -target-feature +ccdp -target-feature +ccpp -target-feature +complxnum -target-feature +crc -target-feature +dotprod -target-feature +flagm -target-feature +fp-armv8 -target-feature +fp16fml -target-feature +fptoint -target-feature +fullfp16 -target-feature +jsconv -target-feature +lse -target-feature +neon -target-feature +pauth -target-feature +perfmon -target-feature +predres -target-feature +ras -target-feature +rcpc -target-feature +rdm -target-feature +sb -target-feature +sha2 -target-feature +sha3 -target-feature +specrestrict -target-feature +ssbs -target-abi darwinpcs -dwarf-ext-refs -fmodule-format=obj -debug-info-kind=standalone -dwarf-version=5 -debugger-tuning=lldb -fdebug-compilation-dir=/Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/Source/WebCore -target-linker-version 1230.1 -fcoverage-compilation-dir=/Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/Source/WebCore -resource-dir /Users/buildbot/Library/Developer/Toolchains/swift-webkit.xctoolchain/usr/lib/clang/21 -dependency-file /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/scan-build-output/StaticAnalyzer/WebCore/WebCore/normal/arm64/UnifiedSource287.d -skip-unused-modulemap-deps -MT dependencies -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk -iquote /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/WebCore.build/Release-iphonesimulator/WebCore.build/WebCore-generated-files.hmap -iquote /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/WebCore.build/Release-iphonesimulator/WebCore.build/WebCore-project-headers.hmap -isystem /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/Source/WebCore/PAL/ThirdParty/dav1d/include -isystem /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator/SDKAdditions/usr/include -iframework /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator/SDKAdditions/System/Library/Frameworks -iframework /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitLibraries/SDKs/iphonesimulator26.0-additions.sdk/System/Library/PrivateFrameworks -iframework /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/System/Library/Frameworks -include-pch /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/PrecompiledHeaders/SharedPrecompiledHeaders/11721353671419897710/WebCorePrefix.h.gch -D _LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE -D NDEBUG -D BUILDING_WEBKIT -D GL_SILENCE_DEPRECATION=1 -D GLES_SILENCE_DEPRECATION=1 -D __clang_analyzer__ -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/WebCore.build/Release-iphonesimulator/WebCore.build/WebCore-own-target-headers.hmap -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/WebCore.build/Release-iphonesimulator/WebCore.build/WebCore-all-target-headers.hmap -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator/include -I PAL -I ForwardingHeaders -I /usr/include/libxslt -I /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/usr/include/libxml2 -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator/DerivedSources/WebCore -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator/usr/local/include -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator/usr/local/include/WebKitAdditions -I /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/usr/local/include/WebKitAdditions -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator/usr/local/include -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitLibraries/SDKs/iphonesimulator26.0-additions.sdk/usr/local/include -I /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/usr/local/include -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator/usr/local/include/webrtc -I /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/usr/local/include/webrtc -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator/usr/local/include/webm -I /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/usr/local/include/webm -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/PAL.build/PAL.build/DerivedSources -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/PAL.build/Release-iphonesimulator/PAL.build/DerivedSources -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/Source/WebCore -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/WebCore.build/Release-iphonesimulator/WebCore.build/DerivedSources-normal/arm64 -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/WebCore.build/Release-iphonesimulator/WebCore.build/DerivedSources/arm64 -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/WebCore.build/Release-iphonesimulator/WebCore.build/DerivedSources -F/Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator -F/Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator -I/usr/local/include -stdlib=libc++ -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/usr/include/c++/v1 -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/usr/local/include -internal-isystem /Users/buildbot/Library/Developer/Toolchains/swift-webkit.xctoolchain/usr/lib/clang/21/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/usr/include -internal-iframework /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/System/Library/Frameworks -internal-iframework /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/System/Library/SubFrameworks -internal-iframework /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/Library/Frameworks -Wno-trigraphs -Werror -Wno-missing-field-initializers -Wmissing-prototypes -Wunreachable-code -Wnon-virtual-dtor -Wno-overloaded-virtual -Wno-exit-time-destructors -Wno-missing-braces -Wparentheses -Wswitch -Wunused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wempty-body -Wuninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wconstant-conversion -Wint-conversion -Wbool-conversion -Wenum-conversion -Wno-float-conversion -Wnon-literal-null-conversion -Wobjc-literal-conversion -Wsign-compare -Wno-shorten-64-to-32 -Wnewline-eof -Wno-c++11-extensions -Wno-implicit-fallthrough -Wdeprecated-declarations -Winvalid-offsetof -Wno-sign-conversion -Winfinite-recursion -Wmove -Wcomma -Wblock-capture-autoreleasing -Wstrict-prototypes -Wrange-loop-analysis -Wno-semicolon-before-method-body -Wall -Wc99-designator -Wconditional-uninitialized -Wextra -Wdeprecated-enum-enum-conversion -Wdeprecated-enum-float-conversion -Wenum-float-conversion -Wfinal-dtor-non-final-class -Wformat=2 -Wmisleading-indentation -Wpointer-to-int-cast -Wreorder-init-list -Wundef -Wunused-but-set-variable -Wvla -Werror=unguarded-availability -Wno-elaborated-enum-base -Wcast-qual -Wchar-subscripts -Wextra-tokens -Winit-self -Wmissing-noreturn -Wpacked -Wpointer-arith -Wredundant-decls -Wwrite-strings -Wexit-time-destructors -Wglobal-constructors -Wtautological-compare -Wimplicit-fallthrough -Wvla -Wno-unknown-warning-option -Wliteral-conversion -Wthread-safety -Wno-profile-instr-out-of-date -Wno-profile-instr-unprofiled -Wno-backend-plugin -Wunsafe-buffer-usage -std=c++2b -fdeprecated-macro -ferror-limit 19 -fmacro-backtrace-limit=0 -fvisibility=hidden -fvisibility-inlines-hidden -stack-protector 1 -fblocks -fencode-extended-block-signature -fno-rtti -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fno-cxx-modules -fno-implicit-modules -fskip-odr-check-in-gmf -fpascal-strings -fmax-type-align=16 -fdiagnostics-show-note-include-stack -vectorize-loops -vectorize-slp -fsafe-buffer-usage-suggestions -analyzer-output=html -analyzer-config report-in-main-source-file=true -analyzer-config nullability:NoDiagnoseCallsToSystemHeaders=true -analyzer-checker optin.osx.cocoa.localizability.NonLocalizedStringChecker -analyzer-checker security.insecureAPI.UncheckedReturn -analyzer-checker security.insecureAPI.getpw -analyzer-checker security.insecureAPI.gets -analyzer-checker security.insecureAPI.mkstemp -analyzer-checker security.insecureAPI.mktemp -analyzer-disable-checker security.insecureAPI.rand -analyzer-disable-checker security.insecureAPI.strcpy -analyzer-checker security.insecureAPI.vfork -analyzer-disable-checker alpha,apiModeling,core,cplusplus,deadcode,debug,fuchsia,nullability,optin,osx,security,unix,webkit -analyzer-checker alpha.webkit.ForwardDeclChecker,alpha.webkit.MemoryUnsafeCastChecker,alpha.webkit.NoDeleteChecker,alpha.webkit.NoUncheckedPtrMemberChecker,alpha.webkit.NoUnretainedMemberChecker,alpha.webkit.RetainPtrCtorAdoptChecker,alpha.webkit.UncheckedCallArgsChecker,alpha.webkit.UncheckedLocalVarsChecker,alpha.webkit.UncountedCallArgsChecker,alpha.webkit.UncountedLocalVarsChecker,alpha.webkit.UnretainedCallArgsChecker,alpha.webkit.UnretainedLambdaCapturesChecker,alpha.webkit.UnretainedLocalVarsChecker,webkit.NoUncountedMemberChecker,webkit.RefCntblBaseVirtualDtor,webkit.UncountedLambdaCapturesChecker -analyzer-config max-nodes=10000000 -analyzer-config verbose-report-filename=true -fdwarf2-cfi-asm -o /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/scan-build-output/StaticAnalyzer/WebCore/WebCore/normal/arm64/UnifiedSource287.plist -x c++ /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator/DerivedSources/WebCore/unified-sources/UnifiedSource287.cpp
1/*
2 * Copyright (C) 2008-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 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30#include "LegacyWebArchive.h"
31
32#include "BoundaryPointInlines.h"
33#include "CSSImportRule.h"
34#include "CSSSerializationContext.h"
35#include "CachedResource.h"
36#include "DeprecatedGlobalSettings.h"
37#include "DocumentLoader.h"
38#include "DocumentPage.h"
39#include "Editor.h"
40#include "EditorClient.h"
41#include "FrameDestructionObserverInlines.h"
42#include "FrameLoader.h"
43#include "FrameSelection.h"
44#include "FrameTree.h"
45#include "HTMLAttachmentElement.h"
46#include "HTMLFrameElement.h"
47#include "HTMLFrameOwnerElement.h"
48#include "HTMLIFrameElement.h"
49#include "HTMLNames.h"
50#include "HTMLObjectElement.h"
51#include "Image.h"
52#include "LocalFrame.h"
53#include "LocalFrameInlines.h"
54#include "Logging.h"
55#include "MIMETypeRegistry.h"
56#include "MemoryCache.h"
57#include "SerializedAttachmentData.h"
58#include "Settings.h"
59#include "SharedBuffer.h"
60#include "StyleSheet.h"
61#include "StyleSheetList.h"
62#include "markup.h"
63#include <algorithm>
64#include <wtf/ListHashSet.h>
65#include <wtf/RetainPtr.h>
66#include <wtf/URLHash.h>
67#include <wtf/URLParser.h>
68#include <wtf/text/CString.h>
69#include <wtf/text/MakeString.h>
70#include <wtf/text/StringBuilder.h>
71
72namespace WebCore {
73
74static constexpr unsigned maxFileNameSizeInBytes = 255;
75static constexpr char defaultFileName[] = "file";
76static const CFStringRef LegacyWebArchiveMainResourceKey = CFSTR("WebMainResource")((CFStringRef) __builtin___CFStringMakeConstantString ("" "WebMainResource"
""))
;
77static const CFStringRef LegacyWebArchiveSubresourcesKey = CFSTR("WebSubresources")((CFStringRef) __builtin___CFStringMakeConstantString ("" "WebSubresources"
""))
;
78static const CFStringRef LegacyWebArchiveSubframeArchivesKey = CFSTR("WebSubframeArchives")((CFStringRef) __builtin___CFStringMakeConstantString ("" "WebSubframeArchives"
""))
;
79static const CFStringRef LegacyWebArchiveResourceDataKey = CFSTR("WebResourceData")((CFStringRef) __builtin___CFStringMakeConstantString ("" "WebResourceData"
""))
;
80static const CFStringRef LegacyWebArchiveResourceFrameNameKey = CFSTR("WebResourceFrameName")((CFStringRef) __builtin___CFStringMakeConstantString ("" "WebResourceFrameName"
""))
;
81static const CFStringRef LegacyWebArchiveResourceMIMETypeKey = CFSTR("WebResourceMIMEType")((CFStringRef) __builtin___CFStringMakeConstantString ("" "WebResourceMIMEType"
""))
;
82static const CFStringRef LegacyWebArchiveResourceURLKey = CFSTR("WebResourceURL")((CFStringRef) __builtin___CFStringMakeConstantString ("" "WebResourceURL"
""))
;
83static const CFStringRef LegacyWebArchiveResourceFilePathKey = CFSTR("WebResourceFilePath")((CFStringRef) __builtin___CFStringMakeConstantString ("" "WebResourceFilePath"
""))
;
84static const CFStringRef LegacyWebArchiveResourceTextEncodingNameKey = CFSTR("WebResourceTextEncodingName")((CFStringRef) __builtin___CFStringMakeConstantString ("" "WebResourceTextEncodingName"
""))
;
85static const CFStringRef LegacyWebArchiveResourceResponseKey = CFSTR("WebResourceResponse")((CFStringRef) __builtin___CFStringMakeConstantString ("" "WebResourceResponse"
""))
;
86static const CFStringRef LegacyWebArchiveResourceResponseVersionKey = CFSTR("WebResourceResponseVersion")((CFStringRef) __builtin___CFStringMakeConstantString ("" "WebResourceResponseVersion"
""))
;
87
88static bool NODELETE[[clang::annotate_type("webkit.nodelete")]] isUnreservedURICharacter(char16_t character)
89{
90 return isASCIIAlphanumeric(character) || character == '-' || character == '.' || character == '_' || character == '~';
91}
92
93static String getFileNameFromURIComponent(StringView input)
94{
95 auto decodedInput = WTF::URLParser::formURLDecode(input);
96 if (!decodedInput)
97 return { };
98
99 unsigned length = decodedInput->length();
100 if (!length)
101 return { };
102
103 StringBuilder result;
104 result.reserveCapacity(length);
105 for (unsigned index = 0; index < length; ++index) {
106 char16_t character = decodedInput->characterAt(index);
107 if (isUnreservedURICharacter(character)) {
108 result.append(character);
109 continue;
110 }
111 result.append('-');
112 }
113
114 return result.toString();
115}
116
117static String generateValidFileName(const URL& url, const HashSet<String>& existingFileNames, const String& extension = { })
118{
119 String suffix = extension.isEmpty() ? emptyString() : makeString('.', extension);
120 auto extractedFileName = getFileNameFromURIComponent(url.lastPathComponent());
121 if (extractedFileName.endsWith(suffix))
122 extractedFileName = extractedFileName.left(extractedFileName.length() - suffix.length());
123 auto fileName = extractedFileName.isEmpty() ? String::fromLatin1(defaultFileName) : extractedFileName;
124
125 RELEASE_ASSERT(suffix.length() < maxFileNameSizeInBytes)do { if (__builtin_expect(!!(!(suffix.length() < maxFileNameSizeInBytes
)), 0)) do { WTF::isIntegralOrPointerType(); compilerFenceForCrash
(); WTFCrashWithInfo(125, "/Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/Source/WebCore/loader/archive/cf/LegacyWebArchive.cpp"
, __PRETTY_FUNCTION__ ); } while (false); } while (0)
;
126 unsigned maxUniqueFileNameLength = maxFileNameSizeInBytes - suffix.length();
127 String uniqueFileName;
128
129 unsigned count = 0;
130 do {
131 uniqueFileName = fileName;
132 if (count)
133 uniqueFileName = makeString(fileName, '-', count);
134 if (uniqueFileName.sizeInBytes() > maxUniqueFileNameLength)
135 uniqueFileName = uniqueFileName.right(maxUniqueFileNameLength);
136 uniqueFileName = makeString(uniqueFileName, suffix);
137 ++count;
138 } while (existingFileNames.contains(uniqueFileName));
139
140 return uniqueFileName;
141}
142
143RetainPtr<CFDictionaryRef> LegacyWebArchive::createPropertyListRepresentation(ArchiveResource* resource, MainResourceStatus isMainResource)
144{
145 if (!resource) {
146 // The property list representation of a null/empty WebResource has the following 3 objects stored as nil.
147 // FIXME: 0 is not serializable. Presumably we need to use kCFNull here instead for compatibility.
148 // FIXME: But why do we need to support a resource of 0? Who relies on that?
149 RetainPtr<CFMutableDictionaryRef> propertyList = adoptCF(CFDictionaryCreateMutable(0, 3, 0, 0));
150 CFDictionarySetValue(propertyList.get(), LegacyWebArchiveResourceDataKey, 0);
151 CFDictionarySetValue(propertyList.get(), LegacyWebArchiveResourceURLKey, 0);
152 CFDictionarySetValue(propertyList.get(), LegacyWebArchiveResourceMIMETypeKey, 0);
153 return propertyList;
154 }
155
156 auto propertyList = adoptCF(CFDictionaryCreateMutable(0, 6, 0, &kCFTypeDictionaryValueCallBacks));
157
158 // Resource data can be empty, but must be represented by an empty CFDataRef
159 Ref data = resource->data();
160
161 CFDictionarySetValue(propertyList.get(), LegacyWebArchiveResourceDataKey, data->makeContiguous()->createCFData().get());
162
163 // Resource URL cannot be null
164 if (auto cfURL = resource->url().string().createCFString())
165 CFDictionarySetValue(propertyList.get(), LegacyWebArchiveResourceURLKey, cfURL.get());
166 else {
167 LOG(Archives, "LegacyWebArchive - NULL resource URL is invalid - returning null property list")((void)0);
168 return nullptr;
169 }
170
171 auto& filePath = resource->relativeFilePath();
172 if (!filePath.isEmpty())
173 CFDictionarySetValue(propertyList.get(), LegacyWebArchiveResourceFilePathKey, filePath.createCFString().get());
174
175 // FrameName should be left out if empty for subresources, but always included for main resources
176 auto& frameName = resource->frameName();
177 if (!frameName.isEmpty() || isMainResource)
178 CFDictionarySetValue(propertyList.get(), LegacyWebArchiveResourceFrameNameKey, frameName.createCFString().get());
179
180 // Set MIMEType, TextEncodingName, and ResourceResponse only if they actually exist
181 auto& mimeType = resource->mimeType();
182 if (!mimeType.isEmpty())
183 CFDictionarySetValue(propertyList.get(), LegacyWebArchiveResourceMIMETypeKey, mimeType.createCFString().get());
184
185 auto& textEncoding = resource->textEncoding();
186 if (!textEncoding.isEmpty())
187 CFDictionarySetValue(propertyList.get(), LegacyWebArchiveResourceTextEncodingNameKey, textEncoding.createCFString().get());
188
189 // Don't include the resource response for the main resource
190 if (!isMainResource) {
191 if (auto resourceResponseData = createPropertyListRepresentation(resource->response()))
192 CFDictionarySetValue(propertyList.get(), LegacyWebArchiveResourceResponseKey, resourceResponseData.get());
193 }
194
195 return propertyList;
196}
197
198RetainPtr<CFDictionaryRef> LegacyWebArchive::createPropertyListRepresentation(Archive& archive)
199{
200 auto propertyList = adoptCF(CFDictionaryCreateMutable(0, 3, 0, &kCFTypeDictionaryValueCallBacks));
201
202 auto mainResourceDict = createPropertyListRepresentation(archive.mainResource(), MainResource);
203 ASSERT(mainResourceDict)((void)0);
204 if (!mainResourceDict)
205 return nullptr;
206 CFDictionarySetValue(propertyList.get(), LegacyWebArchiveMainResourceKey, mainResourceDict.get());
207
208 auto subresourcesArray = adoptCF(CFArrayCreateMutable(0, archive.subresources().size(), &kCFTypeArrayCallBacks));
209 for (auto& resource : archive.subresources()) {
210 if (auto subresource = createPropertyListRepresentation(resource.ptr(), Subresource))
211 CFArrayAppendValue(subresourcesArray.get(), subresource.get());
212 else
213 LOG(Archives, "LegacyWebArchive - Failed to create property list for subresource")((void)0);
214 }
215 if (CFArrayGetCount(subresourcesArray.get()))
216 CFDictionarySetValue(propertyList.get(), LegacyWebArchiveSubresourcesKey, subresourcesArray.get());
217
218 auto subframesArray = adoptCF(CFArrayCreateMutable(0, archive.subframeArchives().size(), &kCFTypeArrayCallBacks));
219 for (auto& subframe : archive.subframeArchives()) {
220 if (auto subframeArchive = createPropertyListRepresentation(subframe.get()))
221 CFArrayAppendValue(subframesArray.get(), subframeArchive.get());
222 else
223 LOG(Archives, "LegacyWebArchive - Failed to create property list for subframe archive")((void)0);
224 }
225 if (CFArrayGetCount(subframesArray.get()))
226 CFDictionarySetValue(propertyList.get(), LegacyWebArchiveSubframeArchivesKey, subframesArray.get());
227
228 return propertyList;
229}
230
231ResourceResponse LegacyWebArchive::createResourceResponseFromPropertyListData(CFDataRef data, CFStringRef responseDataType)
232{
233 ASSERT(data)((void)0);
234 if (!data)
235 return ResourceResponse();
236
237 // If the ResourceResponseVersion (passed in as responseDataType) exists at all, this is a "new" web archive that we
238 // can parse well in a cross platform manner If it doesn't exist, we will assume this is an "old" web archive with,
239 // NSURLResponse objects in it and parse the ResourceResponse as such.
240 if (!responseDataType)
241 return createResourceResponseFromMacArchivedData(data);
242
243 // FIXME: Parse the "new" format that the above comment references here. This format doesn't exist yet.
244 return ResourceResponse();
245}
246
247RefPtr<ArchiveResource> LegacyWebArchive::createResource(CFDictionaryRef dictionary)
248{
249 ASSERT(dictionary)((void)0);
250 if (!dictionary)
251 return nullptr;
252
253 RetainPtr resourceData = static_cast<CFDataRef>(CFDictionaryGetValue(dictionary, LegacyWebArchiveResourceDataKey));
254 if (resourceData && CFGetTypeID(resourceData.get()) != CFDataGetTypeID()) {
255 LOG(Archives, "LegacyWebArchive - Resource data is not of type CFData, cannot create invalid resource")((void)0);
256 return nullptr;
257 }
258
259 RetainPtr frameName = static_cast<CFStringRef>(CFDictionaryGetValue(dictionary, LegacyWebArchiveResourceFrameNameKey));
260 if (frameName && CFGetTypeID(frameName.get()) != CFStringGetTypeID()) {
261 LOG(Archives, "LegacyWebArchive - Frame name is not of type CFString, cannot create invalid resource")((void)0);
262 return nullptr;
263 }
264
265 RetainPtr mimeType = static_cast<CFStringRef>(CFDictionaryGetValue(dictionary, LegacyWebArchiveResourceMIMETypeKey));
266 if (mimeType && CFGetTypeID(mimeType.get()) != CFStringGetTypeID()) {
267 LOG(Archives, "LegacyWebArchive - MIME type is not of type CFString, cannot create invalid resource")((void)0);
268 return nullptr;
269 }
270
271 RetainPtr url = static_cast<CFStringRef>(CFDictionaryGetValue(dictionary, LegacyWebArchiveResourceURLKey));
272 if (url && CFGetTypeID(url.get()) != CFStringGetTypeID()) {
273 LOG(Archives, "LegacyWebArchive - URL is not of type CFString, cannot create invalid resource")((void)0);
274 return nullptr;
275 }
276
277 RetainPtr textEncoding = static_cast<CFStringRef>(CFDictionaryGetValue(dictionary, LegacyWebArchiveResourceTextEncodingNameKey));
278 if (textEncoding && CFGetTypeID(textEncoding.get()) != CFStringGetTypeID()) {
279 LOG(Archives, "LegacyWebArchive - Text encoding is not of type CFString, cannot create invalid resource")((void)0);
280 return nullptr;
281 }
282
283 ResourceResponse response;
284
285 if (RetainPtr resourceResponseData = static_cast<CFDataRef>(CFDictionaryGetValue(dictionary, LegacyWebArchiveResourceResponseKey))) {
286 if (CFGetTypeID(resourceResponseData.get()) != CFDataGetTypeID()) {
287 LOG(Archives, "LegacyWebArchive - Resource response data is not of type CFData, cannot create invalid resource")((void)0);
288 return nullptr;
289 }
290
291 RetainPtr resourceResponseVersion = static_cast<CFStringRef>(CFDictionaryGetValue(dictionary, LegacyWebArchiveResourceResponseVersionKey));
292 if (resourceResponseVersion && CFGetTypeID(resourceResponseVersion.get()) != CFStringGetTypeID()) {
293 LOG(Archives, "LegacyWebArchive - Resource response version is not of type CFString, cannot create invalid resource")((void)0);
294 return nullptr;
295 }
296
297 response = createResourceResponseFromPropertyListData(resourceResponseData.get(), resourceResponseVersion.get());
298 }
299
300 auto filePathValue = CFDictionaryGetValue(dictionary, LegacyWebArchiveResourceFilePathKey);
301 RetainPtr filePath = dynamic_cf_cast<CFStringRef>(filePathValue);
302 if (filePathValue && !filePath) {
303 LOG(Archives, "LegacyWebArchive - File path is not of type CFString, cannot create invalid resource")((void)0);
304 return nullptr;
305 }
306
307 return ArchiveResource::create(SharedBuffer::create(resourceData.get()), URL { url.get() }, mimeType.get(), textEncoding.get(), frameName.get(), response, filePath.get());
308}
309
310LegacyWebArchive::LegacyWebArchive(std::optional<FrameIdentifier> frameIdentifier, Vector<FrameIdentifier>&& subframeIdentifiers)
311 : m_frameIdentifier(frameIdentifier)
312 , m_subframeIdentifiers(WTF::move(subframeIdentifiers))
313{
314}
315
316Ref<LegacyWebArchive> LegacyWebArchive::create(Ref<ArchiveResource>&& mainResource, Vector<Ref<ArchiveResource>>&& subresources, Vector<FrameIdentifier>&& subframeIdentifiers, std::optional<FrameIdentifier> mainFrameIdentifier)
317{
318 auto archive = adoptRef(*new LegacyWebArchive(mainFrameIdentifier, WTF::move(subframeIdentifiers)));
319 archive->setMainResource(WTF::move(mainResource));
320
321 for (auto& subresource : subresources)
322 archive->addSubresource(WTF::move(subresource));
323
324 return archive;
325}
326
327Ref<LegacyWebArchive> LegacyWebArchive::create(Ref<ArchiveResource>&& mainResource, Vector<Ref<ArchiveResource>>&& subresources, Vector<Ref<LegacyWebArchive>>&& subframeArchives, std::optional<FrameIdentifier> mainFrameIdentifier)
328{
329 auto archive = adoptRef(*new LegacyWebArchive(mainFrameIdentifier, { }));
330 archive->setMainResource(WTF::move(mainResource));
331
332 for (auto& subresource : subresources)
333 archive->addSubresource(WTF::move(subresource));
334
335 for (auto& subframeArchive : subframeArchives)
336 archive->addSubframeArchive(WTF::move(subframeArchive));
337
338 return archive;
339}
340
341RefPtr<LegacyWebArchive> LegacyWebArchive::create(FragmentedSharedBuffer& data)
342{
343 return create(URL(), data);
344}
345
346RefPtr<LegacyWebArchive> LegacyWebArchive::create(const URL&, FragmentedSharedBuffer& data)
347{
348 LOG(Archives, "LegacyWebArchive - Creating from raw data")((void)0);
349
350 RetainPtr<CFDataRef> cfData = data.makeContiguous()->createCFData();
351 if (!cfData)
352 return nullptr;
353
354 CFErrorRef error = nullptr;
355
356 RetainPtr<CFDictionaryRef> plist = adoptCF(static_cast<CFDictionaryRef>(CFPropertyListCreateWithData(0, cfData.get(), kCFPropertyListImmutable, 0, &error)));
357 if (!plist) {
358#if !LOG_DISABLED!0
359 RetainPtr<CFStringRef> errorString = error ? adoptCF(CFErrorCopyDescription(error)) : 0;
360 const char* cError = errorString ? CFStringGetCStringPtr(errorString.get(), kCFStringEncodingUTF8) : "unknown error";
361 LOG(Archives, "LegacyWebArchive - Error parsing PropertyList from archive data - %s", cError)((void)0);
362#endif
363 if (error)
364 CFRelease(error);
365 return nullptr;
366 }
367
368 if (CFGetTypeID(plist.get()) != CFDictionaryGetTypeID()) {
369 LOG(Archives, "LegacyWebArchive - Archive property list is not the expected CFDictionary, aborting invalid WebArchive")((void)0);
370 return nullptr;
371 }
372
373 return create(plist.get());
374}
375
376RefPtr<LegacyWebArchive> LegacyWebArchive::create(CFDictionaryRef dictionary)
377{
378 ASSERT(dictionary)((void)0);
379 if (!dictionary) {
380 LOG(Archives, "LegacyWebArchive - Null root CFDictionary, aborting invalid WebArchive")((void)0);
381 return nullptr;
382 }
383
384 RetainPtr mainResourceDict = static_cast<CFDictionaryRef>(CFDictionaryGetValue(dictionary, LegacyWebArchiveMainResourceKey));
385 if (!mainResourceDict) {
386 LOG(Archives, "LegacyWebArchive - No main resource in archive, aborting invalid WebArchive")((void)0);
387 return nullptr;
388 }
389 if (CFGetTypeID(mainResourceDict.get()) != CFDictionaryGetTypeID()) {
390 LOG(Archives, "LegacyWebArchive - Main resource is not the expected CFDictionary, aborting invalid WebArchive")((void)0);
391 return nullptr;
392 }
393
394 RefPtr mainResource = createResource(mainResourceDict.get());
395 if (!mainResource) {
396 LOG(Archives, "LegacyWebArchive - Failed to parse main resource from CFDictionary or main resource does not exist, aborting invalid WebArchive")((void)0);
397 return nullptr;
398 }
399
400 if (mainResource->mimeType().isNull()) {
401 LOG(Archives, "LegacyWebArchive - Main resource MIME type is required, but was null.")((void)0);
402 return nullptr;
403 }
404
405 auto subresourceArray = static_cast<CFArrayRef>(CFDictionaryGetValue(dictionary, LegacyWebArchiveSubresourcesKey));
406 if (subresourceArray && CFGetTypeID(subresourceArray) != CFArrayGetTypeID()) {
407 LOG(Archives, "LegacyWebArchive - Subresources is not the expected Array, aborting invalid WebArchive")((void)0);
408 return nullptr;
409 }
410
411 Vector<Ref<ArchiveResource>> subresources;
412 if (subresourceArray) {
413 auto count = CFArrayGetCount(subresourceArray);
414 for (CFIndex i = 0; i < count; ++i) {
415 auto subresourceDict = static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(subresourceArray, i));
416 if (CFGetTypeID(subresourceDict) != CFDictionaryGetTypeID()) {
417 LOG(Archives, "LegacyWebArchive - Subresource is not expected CFDictionary, aborting invalid WebArchive")((void)0);
418 return nullptr;
419 }
420
421 if (auto subresource = createResource(subresourceDict))
422 subresources.append(subresource.releaseNonNull());
423 }
424 }
425
426 auto subframeArray = static_cast<CFArrayRef>(CFDictionaryGetValue(dictionary, LegacyWebArchiveSubframeArchivesKey));
427 if (subframeArray && CFGetTypeID(subframeArray) != CFArrayGetTypeID()) {
428 LOG(Archives, "LegacyWebArchive - Subframe archives is not the expected Array, aborting invalid WebArchive")((void)0);
429 return nullptr;
430 }
431
432 Vector<Ref<LegacyWebArchive>> subframeArchives;
433 if (subframeArray) {
434 auto count = CFArrayGetCount(subframeArray);
435 for (CFIndex i = 0; i < count; ++i) {
436 auto subframeDict = static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(subframeArray, i));
437 if (CFGetTypeID(subframeDict) != CFDictionaryGetTypeID()) {
438 LOG(Archives, "LegacyWebArchive - Subframe array is not expected CFDictionary, aborting invalid WebArchive")((void)0);
439 return nullptr;
440 }
441
442 if (RefPtr subFrameArchive = create(subframeDict))
443 subframeArchives.append(subFrameArchive.releaseNonNull());
444 }
445 }
446
447 return create(mainResource.releaseNonNull(), WTF::move(subresources), WTF::move(subframeArchives), std::nullopt);
448}
449
450RetainPtr<CFDataRef> LegacyWebArchive::rawDataRepresentation()
451{
452 auto propertyList = createPropertyListRepresentation(*this);
453 ASSERT(propertyList)((void)0);
454 if (!propertyList) {
455 LOG(Archives, "LegacyWebArchive - Failed to create property list for archive, returning no data")((void)0);
456 return nullptr;
457 }
458
459 auto stream = adoptCF(CFWriteStreamCreateWithAllocatedBuffers(0, 0));
460
461 CFWriteStreamOpen(stream.get());
462 CFPropertyListWrite(propertyList.get(), stream.get(), kCFPropertyListBinaryFormat_v1_0, 0, 0);
463
464 auto plistData = adoptCF(static_cast<CFDataRef>(CFWriteStreamCopyProperty(stream.get(), kCFStreamPropertyDataWritten)));
465 ASSERT(plistData)((void)0);
466
467 CFWriteStreamClose(stream.get());
468
469 if (!plistData) {
470 LOG(Archives, "LegacyWebArchive - Failed to convert property list into raw data, returning no data")((void)0);
471 return nullptr;
472 }
473
474 return plistData;
475}
476
477#if !PLATFORM(COCOA)(defined 1 && 1)
478
479ResourceResponse LegacyWebArchive::createResourceResponseFromMacArchivedData(CFDataRef responseData)
480{
481 // FIXME: If is is possible to parse in a serialized NSURLResponse manually, without using
482 // NSKeyedUnarchiver, manipulating plists directly, then we want to do that here.
483 // Until then, this can be done on Mac only.
484 return ResourceResponse();
485}
486
487RetainPtr<CFDataRef> LegacyWebArchive::createPropertyListRepresentation(const ResourceResponse& response)
488{
489 // FIXME: Write out the "new" format described in createResourceResponseFromPropertyListData once we invent it.
490 return nullptr;
491}
492
493#endif
494
495RefPtr<LegacyWebArchive> LegacyWebArchive::create(Node& node)
496{
497 return create(node, { });
498}
499
500RefPtr<LegacyWebArchive> LegacyWebArchive::create(Node& node, ArchiveOptions&& options, NOESCAPE[[clang::noescape]] const Function<bool(LocalFrame&)>& frameFilter)
501{
502 RefPtr frame = node.document().frame();
503 if (!frame)
504 return nullptr;
505
506 auto currentOptions = WTF::move(options);
507 // If the page was loaded with JavaScript enabled, we don't want to archive <noscript> tags
508 // In practice we don't actually know whether scripting was enabled when the page was originally loaded
509 // but we can approximate that by checking if scripting is enabled right now.
510 if (frame->page() && frame->page()->settings().isScriptEnabled())
511 currentOptions.markupExclusionRules.append(MarkupExclusionRule { AtomString { "noscript"_s }, { } });
512
513 // This archive is created for saving, and all subresources URLs will be rewritten to relative file paths
514 // based on the main resource file.
515 if (!currentOptions.mainResourceFileName.isEmpty())
516 currentOptions.markupExclusionRules.append(MarkupExclusionRule { AtomString { "base"_s }, { } });
517
518 return createInternal(node, currentOptions, frameFilter);
519}
520
521RefPtr<LegacyWebArchive> LegacyWebArchive::create(LocalFrame& frame)
522{
523 return create(frame, { });
524}
525
526RefPtr<LegacyWebArchive> LegacyWebArchive::create(LocalFrame& frame, ArchiveOptions&& options)
527{
528 RefPtr documentLoader = frame.loader().documentLoader();
529 if (!documentLoader)
530 return nullptr;
531
532 auto mainResource = documentLoader->mainResource();
533 if (!mainResource)
534 return nullptr;
535
536 Vector<Ref<LegacyWebArchive>> subframeArchives;
537 Vector<FrameIdentifier> subframeIdentifiers;
538 for (RefPtr child = frame.tree().firstChild(); child; child = child->tree().nextSibling()) {
539 if (options.shouldArchiveSubframes == ShouldArchiveSubframes::No) {
540 subframeIdentifiers.append(child->frameID());
541 continue;
542 }
543
544 if (auto localChild = dynamicDowncast<LocalFrame>(child.get())) {
545 if (auto childFrameArchive = create(*localChild, { }))
546 subframeArchives.append(childFrameArchive.releaseNonNull());
547 }
548 }
549
550 if (!subframeIdentifiers.isEmpty()) {
551 ASSERT(subframeArchives.isEmpty())((void)0);
552 return create(mainResource.releaseNonNull(), documentLoader->subresources(), WTF::move(subframeIdentifiers), frame.frameID());
553 }
554
555 return create(mainResource.releaseNonNull(), documentLoader->subresources(), WTF::move(subframeArchives), frame.frameID());
556}
557
558RefPtr<LegacyWebArchive> LegacyWebArchive::create(const SimpleRange& range)
559{
560 return LegacyWebArchive::create(range, { });
561}
562
563RefPtr<LegacyWebArchive> LegacyWebArchive::create(const SimpleRange& range, ArchiveOptions&& options)
564{
565 Ref document = range.start.document();
566 RefPtr frame = document->frame();
567 if (!frame)
568 return nullptr;
569
570 // FIXME: This is always "for interchange". Is that right?
571 Vector<Ref<Node>> nodeList;
572 auto markupString = makeString(documentTypeString(document), serializePreservingVisualAppearance(range, &nodeList, AnnotateForInterchange::Yes));
573 return createInternal(markupString, WTF::move(options), *frame, WTF::move(nodeList), nullptr);
574}
575
576#if ENABLE(ATTACHMENT_ELEMENT)(defined 1 && 1)
577
578static void addSubresourcesForAttachmentElementsIfNecessary(LocalFrame& frame, const Vector<Ref<Node>>& nodes, Vector<Ref<ArchiveResource>>& subresources)
579{
580 if (!DeprecatedGlobalSettings::attachmentElementEnabled())
581 return;
582
583 Vector<String> identifiers;
584 for (Ref node : nodes) {
585 RefPtr attachment = dynamicDowncast<HTMLAttachmentElement>(node);
586 if (!attachment)
587 continue;
588
589 auto uniqueIdentifier = attachment->uniqueIdentifier();
590 if (uniqueIdentifier.isEmpty())
591 continue;
592
593 identifiers.append(WTF::move(uniqueIdentifier));
594 }
595
596 if (identifiers.isEmpty())
597 return;
598
599 CheckedPtr editorClient = frame.editor().client();
600 if (!editorClient)
601 return;
602
603 auto frameName = frame.tree().uniqueName();
604 for (auto& data : editorClient->serializedAttachmentDataForIdentifiers(WTF::move(identifiers))) {
605 auto resourceURL = HTMLAttachmentElement::archiveResourceURL(data.identifier);
606 if (auto resource = ArchiveResource::create(data.data.ptr(), WTF::move(resourceURL), data.mimeType, { }, frameName))
607 subresources.append(resource.releaseNonNull());
608 }
609}
610
611#endif
612
613static HashMap<Ref<CSSStyleSheet>, String> addSubresourcesForCSSStyleSheetsIfNecessary(LocalFrame& frame, const String& subresourcesDirectoryName, HashSet<String>& uniqueFileNames, HashMap<String, String>& uniqueSubresources, Vector<Ref<ArchiveResource>>& subresources)
614{
615 if (subresourcesDirectoryName.isEmpty())
616 return { };
617
618 RefPtr document = frame.document();
619 if (!document)
620 return { };
621
622 CSS::SerializationContext serializationContext;
623
624 HashMap<Ref<CSSStyleSheet>, String> uniqueCSSStyleSheets;
625 Ref documentStyleSheets = document->styleSheets();
626 for (unsigned index = 0; index < documentStyleSheets->length(); ++index) {
627 RefPtr cssStyleSheet = dynamicDowncast<CSSStyleSheet>(documentStyleSheets->item(index));
628 if (!cssStyleSheet)
629 continue;
630
631 if (uniqueCSSStyleSheets.contains(*cssStyleSheet))
632 continue;
633
634 HashSet<Ref<CSSStyleSheet>> cssStyleSheets;
635 cssStyleSheets.add(*cssStyleSheet);
636 cssStyleSheet->getChildStyleSheets(cssStyleSheets);
637 for (auto& currentCSSStyleSheet : cssStyleSheets) {
638 bool isExternalStyleSheet = !currentCSSStyleSheet->href().isEmpty() || currentCSSStyleSheet->ownerRule();
639 if (!isExternalStyleSheet)
640 continue;
641
642 auto url = currentCSSStyleSheet->baseURL();
643 if (url.isNull() || url.isEmpty())
644 continue;
645
646 auto addResult = uniqueCSSStyleSheets.add(currentCSSStyleSheet.copyRef(), emptyString());
647 if (!addResult.isNewEntry)
648 continue;
649
650 // Delete cached resource for this style sheet.
651 auto index = subresources.findIf([&](auto& subresource) {
652 return subresource->url() == url;
653 });
654 if (index != notFound) {
655 auto fileName = FileSystem::lastComponentOfPathIgnoringTrailingSlash(subresources[index]->relativeFilePath());
656 uniqueFileNames.remove(fileName);
657 uniqueSubresources.remove(url.string());
658 subresources.removeAt(index);
659 }
660
661 auto extension = MIMETypeRegistry::preferredExtensionForMIMEType(cssContentTypeAtom());
662 String subresourceFileName = generateValidFileName(url, uniqueFileNames, extension);
663 uniqueFileNames.add(subresourceFileName);
664 addResult.iterator->value = FileSystem::pathByAppendingComponent(subresourcesDirectoryName, subresourceFileName);
665 serializationContext.replacementURLStringsForCSSStyleSheet.add(currentCSSStyleSheet.copyRef(), subresourceFileName);
666 }
667 }
668
669 auto frameName = frame.tree().uniqueName();
670 for (auto& [urlString, path] : uniqueSubresources) {
671 // The style sheet files are stored in the same directory as other subresources.
672 serializationContext.replacementURLStrings.add(urlString, FileSystem::lastComponentOfPathIgnoringTrailingSlash(path));
673 }
674
675 for (auto& [cssStyleSheet, path] : uniqueCSSStyleSheets) {
676 auto contentString = cssStyleSheet->cssText(serializationContext);
677 if (auto newResource = ArchiveResource::create(utf8Buffer(contentString), URL { cssStyleSheet->href() }, "text/css"_s, "UTF-8"_s, frameName, ResourceResponse(), path))
678 subresources.append(newResource.releaseNonNull());
679 }
680
681 return frame.isMainFrame() ? uniqueCSSStyleSheets : serializationContext.replacementURLStringsForCSSStyleSheet;
682}
683
684RefPtr<LegacyWebArchive> LegacyWebArchive::createInternal(Node& node, const ArchiveOptions& options, NOESCAPE[[clang::noescape]] const Function<bool(LocalFrame&)>& frameFilter)
685{
686 RefPtr frame = node.document().frame();
687 if (!frame)
688 return nullptr;
689
690 Vector<Ref<Node>> nodeList;
691 String markupString = serializeFragment(node, SerializedNodes::SubtreeIncludingNode, &nodeList, ResolveURLs::No, std::nullopt, SerializeShadowRoots::AllForInterchange, { }, options.markupExclusionRules);
692 auto nodeType = node.nodeType();
693 if (nodeType != NodeType::Document && nodeType != NodeType::DocumentType)
694 markupString = makeString(documentTypeString(node.document()), markupString);
695
696 return createInternal(markupString, options, *frame, WTF::move(nodeList), frameFilter);
697}
698
699RefPtr<LegacyWebArchive> LegacyWebArchive::createInternal(const String& markupString, const ArchiveOptions& options, LocalFrame& frame, Vector<Ref<Node>>&& nodes, NOESCAPE[[clang::noescape]] const Function<bool(LocalFrame&)>& frameFilter)
700{
701 auto& response = frame.loader().documentLoader()->response();
702 URL responseURL = response.url();
703
704 // it's possible to have a response without a URL here
705 // <rdar://problem/5454935>
706 if (responseURL.isNull())
707 responseURL = URL({ }, emptyString());
708
709 auto mainResource = ArchiveResource::create(utf8Buffer(markupString), responseURL, response.mimeType(), "UTF-8"_s, frame.tree().uniqueName());
710 if (!mainResource)
711 return nullptr;
712
713 Vector<Ref<LegacyWebArchive>> subframeArchives;
714 Vector<FrameIdentifier> subframeIdentifiers;
715 Vector<Ref<ArchiveResource>> subresources;
716 HashMap<String, String> uniqueSubresources;
717 HashSet<String> uniqueFileNames;
718 String subresourcesDirectoryName = options.mainResourceFileName.isNull() ? String { } : makeString(options.mainResourceFileName, "_files"_s);
719
720 for (auto& node : nodes) {
721 RefPtr frameOwnerElement = dynamicDowncast<HTMLFrameOwnerElement>(node);
722 RefPtr childFrame = frameOwnerElement ? frameOwnerElement->contentFrame() : nullptr;
723 RefPtr localChildFrame = dynamicDowncast<LocalFrame>(childFrame.get());
724 if (childFrame) {
725 if (frameFilter && localChildFrame && !frameFilter(*localChildFrame))
726 continue;
727
728 if (options.shouldArchiveSubframes == ShouldArchiveSubframes::No) {
729 subframeIdentifiers.append(childFrame->frameID());
730 continue;
731 }
732
733 if (!localChildFrame)
734 continue;
735
736 if (auto subframeArchive = createInternal(*localChildFrame->document(), options, frameFilter)) {
737 RefPtr subframeMainResource = subframeArchive->mainResource();
738 auto subframeMainResourceURL = subframeMainResource ? subframeMainResource->url() : URL { };
739 if (!subframeMainResourceURL.isEmpty()) {
740 auto subframeMainResourceRelativePath = frame.isMainFrame() ? subframeMainResource->relativeFilePath() : FileSystem::lastComponentOfPathIgnoringTrailingSlash(subframeMainResource->relativeFilePath());
741 uniqueSubresources.add(makeString(childFrame->frameID().toUInt64()), subframeMainResourceRelativePath);
742 }
743 subframeArchives.append(subframeArchive.releaseNonNull());
744 } else
745 LOG_ERROR("Unabled to archive subframe %s", childFrame->tree().uniqueName().string().utf8().data())((void)0);
746
747 } else {
748 ListHashSet<URL> subresourceURLs;
749 node->getSubresourceURLs(subresourceURLs);
750 node->getCandidateSubresourceURLs(subresourceURLs);
751
752 if (options.shouldSaveScriptsFromMemoryCache == ShouldSaveScriptsFromMemoryCache::Yes && responseURL.protocolIsInHTTPFamily()) {
753 RegistrableDomain domain { responseURL };
754 MemoryCache::singleton().forEachSessionResource(frame.page()->sessionID(), [&](auto& resource) {
755 if (domain.matches(resource.url()) && resource.hasClients() && (resource.type() == CachedResource::Type::Script || resource.type() == CachedResource::Type::JSON))
756 subresourceURLs.add(resource.url());
757 });
758 }
759
760 ASSERT(frame.loader().documentLoader())((void)0);
761 Ref documentLoader = *frame.loader().documentLoader();
762
763 for (auto& subresourceURL : subresourceURLs) {
764 if (uniqueSubresources.contains(subresourceURL.string()))
765 continue;
766
767 // WebArchive is created for saving, and we don't need to store resources for data URLs.
768 if (!subresourcesDirectoryName.isNull() && subresourceURL.protocolIsData())
769 continue;
770
771 auto addResult = uniqueSubresources.add(subresourceURL.string(), emptyString());
772 auto resource = documentLoader->subresource(subresourceURL);
773 if (!resource) {
774 ResourceRequest request(URL { subresourceURL });
775 request.setDomainForCachePartition(frame.document()->domainForCachePartition());
776 if (auto* cachedResource = MemoryCache::singleton().resourceForRequest(request, frame.page()->sessionID()))
Local variable 'cachedResource' is uncounted and unsafe
777 resource = ArchiveResource::create(cachedResource->resourceBuffer(), subresourceURL, cachedResource->response());
778 }
779
780 if (!resource) {
781 // FIXME: should do something better than spew to console here
782 LOG_ERROR("Failed to archive subresource for %s", subresourceURL.string().utf8().data())((void)0);
783 continue;
784 }
785
786 if (!subresourcesDirectoryName.isNull()) {
787 String subresourceFileName = generateValidFileName(subresourceURL, uniqueFileNames);
788 uniqueFileNames.add(subresourceFileName);
789 String subresourceFilePath = FileSystem::pathByAppendingComponent(subresourcesDirectoryName, subresourceFileName);
790 resource->setRelativeFilePath(subresourceFilePath);
791 addResult.iterator->value = frame.isMainFrame() ? subresourceFilePath : subresourceFileName;
792 }
793
794 subresources.append(resource.releaseNonNull());
795 }
796 }
797 }
798
799 auto uniqueCSSStyleSheets = addSubresourcesForCSSStyleSheetsIfNecessary(frame, subresourcesDirectoryName, uniqueFileNames, uniqueSubresources, subresources);
800
801#if ENABLE(ATTACHMENT_ELEMENT)(defined 1 && 1)
802 addSubresourcesForAttachmentElementsIfNecessary(frame, nodes, subresources);
803#endif
804
805 // If we are archiving the entire page, add any link icons that we have data for.
806 if (!nodes.isEmpty() && nodes[0]->isDocumentNode()) {
807 RefPtr documentLoader = frame.loader().documentLoader();
808 ASSERT(documentLoader)((void)0);
809 for (auto& icon : documentLoader->linkIcons()) {
810 if (auto resource = documentLoader->subresource(icon.url))
811 subresources.append(resource.releaseNonNull());
812 }
813 }
814
815 if (!options.mainResourceFileName.isNull()) {
816 RefPtr document = frame.document();
817 if (!document)
818 return nullptr;
819
820 if (responseURL.isEmpty())
821 return nullptr;
822
823 auto extension = MIMETypeRegistry::preferredExtensionForMIMEType(textHTMLContentTypeAtom());
824 if (!extension.isEmpty())
825 extension = makeString('.', extension);
826 auto mainFrameFileNameWithExtension = options.mainResourceFileName.endsWith(extension) ? options.mainResourceFileName : makeString(options.mainResourceFileName, extension);
827 auto fileNameWithExtension = frame.isMainFrame() ? mainFrameFileNameWithExtension : makeString(subresourcesDirectoryName, "/frame_"_s, frame.frameID().toUInt64(), extension);
828
829 ResolveURLs resolveURLs = ResolveURLs::No;
830 // Base element is excluded, so all URLs should be replaced with absolute URL.
831 bool baseElementExcluded = std::ranges::any_of(options.markupExclusionRules, [&](auto& rule) {
832 return rule.elementLocalName == "base"_s;
833 });
834 if (!document->baseElementURL().isEmpty() && baseElementExcluded)
835 resolveURLs = ResolveURLs::Yes;
836
837 String updatedMarkupString = serializeFragmentWithURLReplacement(*document, SerializedNodes::SubtreeIncludingNode, nullptr, resolveURLs, std::nullopt, WTF::move(uniqueSubresources), WTF::move(uniqueCSSStyleSheets), SerializeShadowRoots::AllForInterchange, { }, options.markupExclusionRules);
838 mainResource = ArchiveResource::create(utf8Buffer(updatedMarkupString), responseURL, response.mimeType(), "UTF-8"_s, frame.tree().uniqueName(), ResourceResponse(), fileNameWithExtension);
839 }
840
841 if (!subframeIdentifiers.isEmpty()) {
842 ASSERT(subframeArchives.isEmpty())((void)0);
843 return create(mainResource.releaseNonNull(), WTF::move(subresources), WTF::move(subframeIdentifiers), frame.frameID());
844 }
845
846 return create(mainResource.releaseNonNull(), WTF::move(subresources), WTF::move(subframeArchives), frame.frameID());
847}
848
849RefPtr<LegacyWebArchive> LegacyWebArchive::createFromSelection(LocalFrame* frame)
850{
851 return createFromSelection(frame, { });
852}
853
854RefPtr<LegacyWebArchive> LegacyWebArchive::createFromSelection(LocalFrame* frame, ArchiveOptions&& options)
855{
856 if (!frame)
857 return nullptr;
858
859 RefPtr document = frame->document();
860 if (!document)
861 return nullptr;
862
863 StringBuilder builder;
864 builder.append(documentTypeString(*document));
865
866 Vector<Ref<Node>> nodeList;
867 builder.append(serializePreservingVisualAppearance(frame->selection().selection(), ResolveURLs::No, SerializeComposedTree::Yes, IgnoreUserSelectNone::Yes, PreserveBaseElement::Yes, PreserveDirectionForInlineText::Yes, &nodeList));
868
869 RefPtr archive = createInternal(builder.toString(), WTF::move(options), *frame, WTF::move(nodeList), { });
870 if (!archive)
871 return nullptr;
872
873 if (!document->isFrameSet())
874 return archive;
875
876 // Wrap the frameset document in an iframe so it can be pasted into
877 // another document (which will have a body or frameset of its own).
878 auto iframeMarkup = makeString("<iframe frameborder=\"no\" marginwidth=\"0\" marginheight=\"0\" width=\"98%%\" height=\"98%%\" src=\""_s, frame->loader().documentLoader()->response().url().string(), "\"></iframe>"_s);
879 auto iframeResource = ArchiveResource::create(utf8Buffer(iframeMarkup), aboutBlankURL(), textHTMLContentTypeAtom(), "UTF-8"_s, String());
880
881 return create(iframeResource.releaseNonNull(), { }, { archive.releaseNonNull() }, frame->frameID());
882}
883
884}