/src/serenity/Userland/Libraries/LibWeb/HTML/PluginArray.cpp
Line | Count | Source |
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/PluginArrayPrototype.h> |
9 | | #include <LibWeb/HTML/PluginArray.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(PluginArray); |
17 | | |
18 | | PluginArray::PluginArray(JS::Realm& realm) |
19 | 0 | : Bindings::PlatformObject(realm) |
20 | 0 | { |
21 | 0 | m_legacy_platform_object_flags = LegacyPlatformObjectFlags { |
22 | 0 | .supports_indexed_properties = true, |
23 | 0 | .supports_named_properties = true, |
24 | 0 | .has_legacy_unenumerable_named_properties_interface_extended_attribute = true, |
25 | 0 | }; |
26 | 0 | } |
27 | | |
28 | 0 | PluginArray::~PluginArray() = default; |
29 | | |
30 | | void PluginArray::initialize(JS::Realm& realm) |
31 | 0 | { |
32 | 0 | Base::initialize(realm); |
33 | 0 | WEB_SET_PROTOTYPE_FOR_INTERFACE(PluginArray); |
34 | 0 | } |
35 | | |
36 | | // https://html.spec.whatwg.org/multipage/system-state.html#dom-pluginarray-refresh |
37 | | void PluginArray::refresh() const |
38 | 0 | { |
39 | | // The PluginArray interface's refresh() method steps are to do nothing. |
40 | 0 | } |
41 | | |
42 | | // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewing-support:support-named-properties |
43 | | Vector<FlyString> PluginArray::supported_property_names() const |
44 | 0 | { |
45 | | // The PluginArray interface supports named properties. If the user agent's PDF viewer supported is true, then they are the PDF viewer plugin names. Otherwise, they are the empty list. |
46 | 0 | auto const& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this)); |
47 | 0 | if (!window.page().pdf_viewer_supported()) |
48 | 0 | return {}; |
49 | | |
50 | | // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-plugin-names |
51 | 0 | static Vector<FlyString> const plugin_names = { |
52 | 0 | "PDF Viewer"_fly_string, |
53 | 0 | "Chrome PDF Viewer"_fly_string, |
54 | 0 | "Chromium PDF Viewer"_fly_string, |
55 | 0 | "Microsoft Edge PDF Viewer"_fly_string, |
56 | 0 | "WebKit built-in PDF"_fly_string, |
57 | 0 | }; |
58 | |
|
59 | 0 | return plugin_names; |
60 | 0 | } |
61 | | |
62 | | // https://html.spec.whatwg.org/multipage/system-state.html#dom-pluginarray-length |
63 | | size_t PluginArray::length() const |
64 | 0 | { |
65 | | // The PluginArray interface's length getter steps are to return this's relevant global object's PDF viewer plugin objects's size. |
66 | 0 | auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this)); |
67 | 0 | return window.pdf_viewer_plugin_objects().size(); |
68 | 0 | } |
69 | | |
70 | | // https://html.spec.whatwg.org/multipage/system-state.html#dom-pluginarray-item |
71 | | JS::GCPtr<Plugin> PluginArray::item(u32 index) const |
72 | 0 | { |
73 | | // 1. Let plugins be this's relevant global object's PDF viewer plugin objects. |
74 | 0 | auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this)); |
75 | 0 | auto plugins = window.pdf_viewer_plugin_objects(); |
76 | | |
77 | | // 2. If index < plugins's size, then return plugins[index]. |
78 | 0 | if (index < plugins.size()) |
79 | 0 | return plugins[index]; |
80 | | |
81 | | // 3. Return null. |
82 | 0 | return nullptr; |
83 | 0 | } |
84 | | |
85 | | // https://html.spec.whatwg.org/multipage/system-state.html#dom-pluginarray-nameditem |
86 | | JS::GCPtr<Plugin> PluginArray::named_item(FlyString const& name) const |
87 | 0 | { |
88 | | // 1. For each Plugin plugin of this's relevant global object's PDF viewer plugin objects: if plugin's name is name, then return plugin. |
89 | 0 | auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this)); |
90 | 0 | auto plugins = window.pdf_viewer_plugin_objects(); |
91 | |
|
92 | 0 | for (auto& plugin : plugins) { |
93 | 0 | if (plugin->name() == name) |
94 | 0 | return plugin; |
95 | 0 | } |
96 | | |
97 | | // 2. Return null. |
98 | 0 | return nullptr; |
99 | 0 | } |
100 | | |
101 | | Optional<JS::Value> PluginArray::item_value(size_t index) const |
102 | 0 | { |
103 | 0 | auto return_value = item(index); |
104 | 0 | if (!return_value) |
105 | 0 | return {}; |
106 | 0 | return return_value.ptr(); |
107 | 0 | } |
108 | | |
109 | | JS::Value PluginArray::named_item_value(FlyString const& name) const |
110 | 0 | { |
111 | 0 | auto return_value = named_item(name); |
112 | 0 | if (!return_value) |
113 | 0 | return JS::js_null(); |
114 | 0 | return return_value.ptr(); |
115 | 0 | } |
116 | | |
117 | | } |