Coverage Report

Created: 2025-03-04 07:22

/src/serenity/Userland/Libraries/LibWeb/HTML/Plugin.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#include <LibWeb/Bindings/Intrinsics.h>
8
#include <LibWeb/Bindings/PluginPrototype.h>
9
#include <LibWeb/HTML/Plugin.h>
10
#include <LibWeb/HTML/Scripting/Environments.h>
11
#include <LibWeb/HTML/Window.h>
12
#include <LibWeb/Page/Page.h>
13
14
namespace Web::HTML {
15
16
JS_DEFINE_ALLOCATOR(Plugin);
17
18
Plugin::Plugin(JS::Realm& realm, String name)
19
0
    : Bindings::PlatformObject(realm)
20
0
    , m_name(move(name))
21
0
{
22
0
    m_legacy_platform_object_flags = LegacyPlatformObjectFlags {
23
0
        .supports_indexed_properties = true,
24
0
        .supports_named_properties = true,
25
0
        .has_legacy_unenumerable_named_properties_interface_extended_attribute = true,
26
0
    };
27
0
}
28
29
0
Plugin::~Plugin() = default;
30
31
void Plugin::initialize(JS::Realm& realm)
32
0
{
33
0
    Base::initialize(realm);
34
0
    WEB_SET_PROTOTYPE_FOR_INTERFACE(Plugin);
35
0
}
36
37
// https://html.spec.whatwg.org/multipage/system-state.html#dom-plugin-name
38
String const& Plugin::name() const
39
0
{
40
    // The Plugin interface's name getter steps are to return this's name.
41
0
    return m_name;
42
0
}
43
44
// https://html.spec.whatwg.org/multipage/system-state.html#dom-plugin-description
45
String Plugin::description() const
46
0
{
47
    // The Plugin interface's description getter steps are to return "Portable Document Format".
48
0
    static String description_string = "Portable Document Format"_string;
49
0
    return description_string;
50
0
}
51
52
// https://html.spec.whatwg.org/multipage/system-state.html#dom-plugin-filename
53
String Plugin::filename() const
54
0
{
55
    // The Plugin interface's filename getter steps are to return "internal-pdf-viewer".
56
0
    static String filename_string = "internal-pdf-viewer"_string;
57
0
    return filename_string;
58
0
}
59
60
// https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewing-support:support-named-properties-3
61
Vector<FlyString> Plugin::supported_property_names() const
62
0
{
63
    // The Plugin interface supports named properties. If the user agent's PDF viewer supported is true, then they are the PDF viewer mime types. Otherwise, they are the empty list.
64
0
    auto const& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this));
65
0
    if (!window.page().pdf_viewer_supported())
66
0
        return {};
67
68
    // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-mime-types
69
0
    static Vector<FlyString> const mime_types = {
70
0
        "application/pdf"_fly_string,
71
0
        "text/pdf"_fly_string,
72
0
    };
73
74
0
    return mime_types;
75
0
}
76
77
// https://html.spec.whatwg.org/multipage/system-state.html#dom-plugin-length
78
size_t Plugin::length() const
79
0
{
80
    // The Plugin interface's length getter steps are to return this's relevant global object's PDF viewer mime type objects's size.
81
0
    auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this));
82
0
    return window.pdf_viewer_mime_type_objects().size();
83
0
}
84
85
// https://html.spec.whatwg.org/multipage/system-state.html#dom-plugin-item
86
JS::GCPtr<MimeType> Plugin::item(u32 index) const
87
0
{
88
    // 1. Let mimeTypes be this's relevant global object's PDF viewer mime type objects.
89
0
    auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this));
90
0
    auto mime_types = window.pdf_viewer_mime_type_objects();
91
92
    // 2. If index < mimeType's size, then return mimeTypes[index].
93
0
    if (index < mime_types.size())
94
0
        return mime_types[index];
95
96
    // 3. Return null.
97
0
    return nullptr;
98
0
}
99
100
JS::GCPtr<MimeType> Plugin::named_item(FlyString const& name) const
101
0
{
102
    // 1. For each MimeType mimeType of this's relevant global object's PDF viewer mime type objects: if mimeType's type is name, then return mimeType.
103
0
    auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this));
104
0
    auto mime_types = window.pdf_viewer_mime_type_objects();
105
106
0
    for (auto& mime_type : mime_types) {
107
0
        if (mime_type->type() == name)
108
0
            return mime_type;
109
0
    }
110
111
    // 2. Return null.
112
0
    return nullptr;
113
0
}
114
115
Optional<JS::Value> Plugin::item_value(size_t index) const
116
0
{
117
0
    auto return_value = item(index);
118
0
    if (!return_value)
119
0
        return {};
120
0
    return return_value.ptr();
121
0
}
122
123
JS::Value Plugin::named_item_value(FlyString const& name) const
124
0
{
125
0
    auto return_value = named_item(name);
126
0
    if (!return_value)
127
0
        return JS::js_null();
128
0
    return return_value.ptr();
129
0
}
130
131
}