Title: [289031] trunk/Source/WebCore
Revision
289031
Author
[email protected]
Date
2022-02-03 00:15:36 -0800 (Thu, 03 Feb 2022)

Log Message

Create PDFDocument stub
https://bugs.webkit.org/show_bug.cgi?id=235970

Reviewed by Brent Fulgham.

Create an empty stub with an iframe where the viewer will be loaded.

Changing about:blank?file= to https://mozilla.github.io/pdf.js/web/viewer.html?file= is one way
to test the stub.

That stub is guarded behind the PDF.js viewer internal setting which is only enabled in captive portal mode.

* Headers.cmake:
* Sources.txt:
* WebCore.xcodeproj/project.pbxproj:
* dom/DOMImplementation.cpp:
(WebCore::DOMImplementation::createDocument):
* dom/Document.h:
(WebCore::Document::isPDFDocument const):
* html/PDFDocument.cpp: Added.
(WebCore::PDFDocument::PDFDocument):
(WebCore::m_viewerRendered):
(WebCore::PDFDocumentParser::document const):
(WebCore::PDFDocumentParser::appendBytes):
(WebCore::PDFDocumentParser::finish):
(WebCore::PDFDocument::createParser):
(WebCore::PDFDocument::createDocumentStructure):
(WebCore::PDFDocument::updateDuringParsing):
(WebCore::PDFDocument::finishedParsing):
* html/PDFDocument.h: Added.
(isType):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (289030 => 289031)


--- trunk/Source/WebCore/ChangeLog	2022-02-03 07:33:18 UTC (rev 289030)
+++ trunk/Source/WebCore/ChangeLog	2022-02-03 08:15:36 UTC (rev 289031)
@@ -1,3 +1,37 @@
+2022-02-03  Tim Nguyen  <[email protected]>
+
+        Create PDFDocument stub
+        https://bugs.webkit.org/show_bug.cgi?id=235970
+
+        Reviewed by Brent Fulgham.
+
+        Create an empty stub with an iframe where the viewer will be loaded.
+
+        Changing about:blank?file= to https://mozilla.github.io/pdf.js/web/viewer.html?file= is one way
+        to test the stub.
+
+        That stub is guarded behind the PDF.js viewer internal setting which is only enabled in captive portal mode.
+
+        * Headers.cmake:
+        * Sources.txt:
+        * WebCore.xcodeproj/project.pbxproj:
+        * dom/DOMImplementation.cpp:
+        (WebCore::DOMImplementation::createDocument):
+        * dom/Document.h:
+        (WebCore::Document::isPDFDocument const):
+        * html/PDFDocument.cpp: Added.
+        (WebCore::PDFDocument::PDFDocument):
+        (WebCore::m_viewerRendered):
+        (WebCore::PDFDocumentParser::document const):
+        (WebCore::PDFDocumentParser::appendBytes):
+        (WebCore::PDFDocumentParser::finish):
+        (WebCore::PDFDocument::createParser):
+        (WebCore::PDFDocument::createDocumentStructure):
+        (WebCore::PDFDocument::updateDuringParsing):
+        (WebCore::PDFDocument::finishedParsing):
+        * html/PDFDocument.h: Added.
+        (isType):
+
 2022-02-02  Pablo Saavedra  <[email protected]>
 
         [WPE] Fix for non-unified builds with ACCESSIBILITY=OFF

Modified: trunk/Source/WebCore/Headers.cmake (289030 => 289031)


--- trunk/Source/WebCore/Headers.cmake	2022-02-03 07:33:18 UTC (rev 289030)
+++ trunk/Source/WebCore/Headers.cmake	2022-02-03 08:15:36 UTC (rev 289031)
@@ -902,6 +902,7 @@
     html/MediaDocument.h
     html/MediaElementSession.h
     html/MediaError.h
+    html/PDFDocument.h
     html/PluginDocument.h
     html/StepRange.h
     html/TimeRanges.h

Modified: trunk/Source/WebCore/Sources.txt (289030 => 289031)


--- trunk/Source/WebCore/Sources.txt	2022-02-03 07:33:18 UTC (rev 289030)
+++ trunk/Source/WebCore/Sources.txt	2022-02-03 08:15:36 UTC (rev 289031)
@@ -1309,6 +1309,7 @@
 html/NumberInputType.cpp
 html/OffscreenCanvas.cpp
 html/PasswordInputType.cpp
+html/PDFDocument.cpp
 html/PluginDocument.cpp
 html/PublicURLManager.cpp
 html/RadioInputType.cpp

Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (289030 => 289031)


--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2022-02-03 07:33:18 UTC (rev 289030)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2022-02-03 08:15:36 UTC (rev 289031)
@@ -9364,6 +9364,8 @@
 		494BD7940F55C8EE00747828 /* WebKitPoint.idl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = WebKitPoint.idl; sourceTree = "<group>"; };
 		494BD79B0F55C94C00747828 /* JSWebKitPoint.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSWebKitPoint.cpp; sourceTree = "<group>"; };
 		494BD79C0F55C94C00747828 /* JSWebKitPoint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSWebKitPoint.h; sourceTree = "<group>"; };
+		494EBD0627AA9C5300F4844F /* PDFDocument.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = PDFDocument.h; path = html/PDFDocument.h; sourceTree = "<group>"; };
+		494EBD0827AA9C5300F4844F /* PDFDocument.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = PDFDocument.cpp; path = html/PDFDocument.cpp; sourceTree = "<group>"; };
 		4958781F12A57DDF007238AC /* PlatformCAAnimationCocoa.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = PlatformCAAnimationCocoa.mm; sourceTree = "<group>"; };
 		4958782012A57DDF007238AC /* PlatformCALayerCocoa.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = PlatformCALayerCocoa.mm; sourceTree = "<group>"; };
 		4969B0F013D0B33F00DF3521 /* HitTestingTransformState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HitTestingTransformState.cpp; sourceTree = "<group>"; };
@@ -18896,6 +18898,8 @@
 		0867D691FE84028FC02AAC07 /* WebCore */ = {
 			isa = PBXGroup;
 			children = (
+				494EBD0827AA9C5300F4844F /* PDFDocument.cpp */,
+				494EBD0627AA9C5300F4844F /* PDFDocument.h */,
 				DDB04F37278E5527008D3678 /* libWTF.a */,
 				1C09D04B1E31C32800725F18 /* PAL.xcodeproj */,
 				65C97AF208EA908800ACD273 /* config.h */,

Modified: trunk/Source/WebCore/dom/DOMImplementation.cpp (289030 => 289031)


--- trunk/Source/WebCore/dom/DOMImplementation.cpp	2022-02-03 07:33:18 UTC (rev 289030)
+++ trunk/Source/WebCore/dom/DOMImplementation.cpp	2022-02-03 08:15:36 UTC (rev 289031)
@@ -42,6 +42,7 @@
 #include "MediaDocument.h"
 #include "MediaList.h"
 #include "MediaPlayer.h"
+#include "PDFDocument.h"
 #include "Page.h"
 #include "PluginData.h"
 #include "PluginDocument.h"
@@ -151,6 +152,10 @@
         return XMLDocument::createXHTML(frame, settings, url);
     if (equalLettersIgnoringASCIICase(contentType, "text/plain"))
         return TextDocument::create(frame, settings, url, documentIdentifier);
+
+    if (frame && settings.pdfJSViewerEnabled() && MIMETypeRegistry::isPDFMIMEType(contentType))
+        return PDFDocument::create(*frame, url);
+
     bool isImage = MIMETypeRegistry::isSupportedImageMIMEType(contentType);
     if (frame && isImage && !MIMETypeRegistry::isPDFOrPostScriptMIMEType(contentType))
         return ImageDocument::create(*frame, url);

Modified: trunk/Source/WebCore/dom/Document.h (289030 => 289031)


--- trunk/Source/WebCore/dom/Document.h	2022-02-03 07:33:18 UTC (rev 289030)
+++ trunk/Source/WebCore/dom/Document.h	2022-02-03 08:15:36 UTC (rev 289031)
@@ -534,6 +534,7 @@
 #if ENABLE(MODEL_ELEMENT)
         Model = 1 << 8,
 #endif
+        PDF = 1 << 9,
     };
 
     using DocumentClasses = OptionSet<DocumentClass>;
@@ -549,6 +550,7 @@
 #if ENABLE(MODEL_ELEMENT)
     bool isModelDocument() const { return m_documentClasses.contains(DocumentClass::Model); }
 #endif
+    bool isPDFDocument() const { return m_documentClasses.contains(DocumentClass::PDF); }
     bool hasSVGRootNode() const;
     virtual bool isFrameSet() const { return false; }
 

Added: trunk/Source/WebCore/html/PDFDocument.cpp (0 => 289031)


--- trunk/Source/WebCore/html/PDFDocument.cpp	                        (rev 0)
+++ trunk/Source/WebCore/html/PDFDocument.cpp	2022-02-03 08:15:36 UTC (rev 289031)
@@ -0,0 +1,128 @@
+/*
+ * Copyright (C) 2022 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ */
+
+#include "config.h"
+#include "PDFDocument.h"
+
+#include "DOMWindow.h"
+#include "DocumentLoader.h"
+#include "Frame.h"
+#include "HTMLAnchorElement.h"
+#include "HTMLBodyElement.h"
+#include "HTMLHtmlElement.h"
+#include "HTMLIFrameElement.h"
+#include "HTMLNames.h"
+#include "RawDataDocumentParser.h"
+#include "Settings.h"
+#include <wtf/IsoMallocInlines.h>
+#include <wtf/text/StringConcatenateNumbers.h>
+
+namespace WebCore {
+
+WTF_MAKE_ISO_ALLOCATED_IMPL(PDFDocument);
+
+using namespace HTMLNames;
+
+PDFDocument::PDFDocument(Frame& frame, const URL& url)
+    : HTMLDocument(&frame, frame.settings(), url, { }, { DocumentClass::PDF })
+    , m_viewerRendered(false)
+{
+
+}
+
+class PDFDocumentParser final : public RawDataDocumentParser {
+public:
+    static Ref<PDFDocumentParser> create(PDFDocument& document)
+    {
+        return adoptRef(*new PDFDocumentParser(document));
+    }
+
+private:
+    PDFDocumentParser(PDFDocument& document)
+        : RawDataDocumentParser(document)
+    {
+    }
+
+    PDFDocument& document() const;
+
+    void appendBytes(DocumentWriter&, const uint8_t*, size_t) override;
+    void finish() override;
+};
+
+inline PDFDocument& PDFDocumentParser::document() const
+{
+    // Only used during parsing, so document is guaranteed to be non-null.
+    ASSERT(RawDataDocumentParser::document());
+    return downcast<PDFDocument>(*RawDataDocumentParser::document());
+}
+
+void PDFDocumentParser::appendBytes(DocumentWriter&, const uint8_t*, size_t)
+{
+    document().updateDuringParsing();
+}
+
+void PDFDocumentParser::finish()
+{
+    document().finishedParsing();
+}
+
+Ref<DocumentParser> PDFDocument::createParser()
+{
+    return PDFDocumentParser::create(*this);
+}
+
+void PDFDocument::createDocumentStructure()
+{
+    // FIXME: Replace about:blank with actual viewer URL
+    auto viewerURL = "about:blank?file=";
+    auto rootElement = HTMLHtmlElement::create(*this);
+    appendChild(rootElement);
+    rootElement->insertedByParser();
+
+    frame()->injectUserScripts(UserScriptInjectionTime::DocumentStart);
+
+    auto body = HTMLBodyElement::create(*this);
+    body->setAttribute(styleAttr, AtomString("margin: 0px;height: 100vh;", AtomString::ConstructFromLiteral));
+    rootElement->appendChild(body);
+
+    auto iframe = HTMLIFrameElement::create(HTMLNames::iframeTag, *this);
+    iframe->setAttribute(srcAttr, makeString(viewerURL, encodeWithURLEscapeSequences(url().string())));
+    iframe->setAttribute(styleAttr, AtomString("width: 100%; height: 100%; border: 0; display: block;", AtomString::ConstructFromLiteral));
+    body->appendChild(iframe);
+
+    m_viewerRendered = true;
+}
+
+void PDFDocument::updateDuringParsing()
+{
+    if (!m_viewerRendered)
+        createDocumentStructure();
+}
+
+void PDFDocument::finishedParsing()
+{
+    ASSERT(m_viewerRendered);
+}
+
+}

Added: trunk/Source/WebCore/html/PDFDocument.h (0 => 289031)


--- trunk/Source/WebCore/html/PDFDocument.h	                        (rev 0)
+++ trunk/Source/WebCore/html/PDFDocument.h	2022-02-03 08:15:36 UTC (rev 289031)
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2022 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ */
+
+#pragma once
+
+#include "HTMLDocument.h"
+
+namespace WebCore {
+
+class PDFDocument final : public HTMLDocument {
+    WTF_MAKE_ISO_ALLOCATED(PDFDocument);
+public:
+    static Ref<PDFDocument> create(Frame& frame, const URL& url)
+    {
+        return adoptRef(*new PDFDocument(frame, url));
+    }
+
+    void updateDuringParsing();
+    void finishedParsing();
+
+private:
+    PDFDocument(Frame&, const URL&);
+
+    Ref<DocumentParser> createParser() override;
+
+    void createDocumentStructure();
+    bool m_viewerRendered = false;
+};
+
+} // namespace WebCore
+
+SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::PDFDocument)
+    static bool isType(const WebCore::Document& document) { return document.isPDFDocument(); }
+    static bool isType(const WebCore::Node& node) { return is<WebCore::Document>(node) && isType(downcast<WebCore::Document>(node)); }
+SPECIALIZE_TYPE_TRAITS_END()
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to