Serenity Operating System
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/HTML/Plugin.h>
9#include <LibWeb/HTML/Scripting/Environments.h>
10#include <LibWeb/HTML/Window.h>
11#include <LibWeb/Page/Page.h>
12
13namespace Web::HTML {
14
15Plugin::Plugin(JS::Realm& realm, String name)
16 : Bindings::LegacyPlatformObject(realm)
17 , m_name(move(name))
18{
19}
20
21Plugin::~Plugin() = default;
22
23JS::ThrowCompletionOr<void> Plugin::initialize(JS::Realm& realm)
24{
25 MUST_OR_THROW_OOM(Base::initialize(realm));
26 set_prototype(&Bindings::ensure_web_prototype<Bindings::PluginPrototype>(realm, "Plugin"));
27
28 return {};
29}
30
31// https://html.spec.whatwg.org/multipage/system-state.html#dom-plugin-name
32String const& Plugin::name() const
33{
34 // The Plugin interface's name getter steps are to return this's name.
35 return m_name;
36}
37
38// https://html.spec.whatwg.org/multipage/system-state.html#dom-plugin-description
39JS::ThrowCompletionOr<String> Plugin::description() const
40{
41 // The Plugin interface's description getter steps are to return "Portable Document Format".
42 static String description_string = TRY_OR_THROW_OOM(vm(), "Portable Document Format"_string);
43 return description_string;
44}
45
46// https://html.spec.whatwg.org/multipage/system-state.html#dom-plugin-filename
47JS::ThrowCompletionOr<String> Plugin::filename() const
48{
49 // The Plugin interface's filename getter steps are to return "internal-pdf-viewer".
50 static String filename_string = TRY_OR_THROW_OOM(vm(), "internal-pdf-viewer"_string);
51 return filename_string;
52}
53
54// https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewing-support:support-named-properties-3
55Vector<DeprecatedString> Plugin::supported_property_names() const
56{
57 // 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.
58 auto const& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this));
59 VERIFY(window.page());
60 if (!window.page()->pdf_viewer_supported())
61 return {};
62
63 // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-mime-types
64 static Vector<DeprecatedString> mime_types = {
65 "application/pdf"sv,
66 "text/pdf"sv,
67 };
68
69 return mime_types;
70}
71
72// https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewing-support:supports-indexed-properties-3
73bool Plugin::is_supported_property_index(u32 index) const
74{
75 // The Plugin interface supports indexed properties. The supported property indices are the indices of this's relevant global object's PDF viewer mime type objects.
76 auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this));
77 return index < window.pdf_viewer_mime_type_objects().size();
78}
79
80// https://html.spec.whatwg.org/multipage/system-state.html#dom-plugin-length
81size_t Plugin::length() const
82{
83 // The Plugin interface's length getter steps are to return this's relevant global object's PDF viewer mime type objects's size.
84 auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this));
85 return window.pdf_viewer_mime_type_objects().size();
86}
87
88// https://html.spec.whatwg.org/multipage/system-state.html#dom-plugin-item
89JS::GCPtr<MimeType> Plugin::item(u32 index) const
90{
91 // 1. Let mimeTypes be this's relevant global object's PDF viewer mime type objects.
92 auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this));
93 auto mime_types = window.pdf_viewer_mime_type_objects();
94
95 // 2. If index < mimeType's size, then return mimeTypes[index].
96 if (index < mime_types.size())
97 return mime_types[index];
98
99 // 3. Return null.
100 return nullptr;
101}
102
103JS::GCPtr<MimeType> Plugin::named_item(String const& name) const
104{
105 // 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.
106 auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this));
107 auto mime_types = window.pdf_viewer_mime_type_objects();
108
109 for (auto& mime_type : mime_types) {
110 if (mime_type->type() == name)
111 return mime_type;
112 }
113
114 // 2. Return null.
115 return nullptr;
116}
117
118WebIDL::ExceptionOr<JS::Value> Plugin::item_value(size_t index) const
119{
120 auto return_value = item(index);
121 if (!return_value)
122 return JS::js_null();
123 return return_value.ptr();
124}
125
126WebIDL::ExceptionOr<JS::Value> Plugin::named_item_value(DeprecatedFlyString const& name) const
127{
128 auto converted_name = TRY_OR_THROW_OOM(vm(), String::from_deprecated_string(name));
129 auto return_value = named_item(converted_name);
130 if (!return_value)
131 return JS::js_null();
132 return return_value.ptr();
133}
134
135}