Serenity Operating System
at master 121 lines 4.3 kB view raw
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/PluginArray.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 15PluginArray::PluginArray(JS::Realm& realm) 16 : Bindings::LegacyPlatformObject(realm) 17{ 18} 19 20PluginArray::~PluginArray() = default; 21 22JS::ThrowCompletionOr<void> PluginArray::initialize(JS::Realm& realm) 23{ 24 MUST_OR_THROW_OOM(Base::initialize(realm)); 25 set_prototype(&Bindings::ensure_web_prototype<Bindings::PluginArrayPrototype>(realm, "PluginArray")); 26 27 return {}; 28} 29 30// https://html.spec.whatwg.org/multipage/system-state.html#dom-pluginarray-refresh 31void PluginArray::refresh() const 32{ 33 // The PluginArray interface's refresh() method steps are to do nothing. 34} 35 36// https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewing-support:support-named-properties 37Vector<DeprecatedString> PluginArray::supported_property_names() const 38{ 39 // 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. 40 auto const& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this)); 41 VERIFY(window.page()); 42 if (!window.page()->pdf_viewer_supported()) 43 return {}; 44 45 // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-plugin-names 46 static Vector<DeprecatedString> plugin_names = { 47 "PDF Viewer"sv, 48 "Chrome PDF Viewer"sv, 49 "Chromium PDF Viewer"sv, 50 "Microsoft Edge PDF Viewer"sv, 51 "WebKit built-in PDF"sv, 52 }; 53 54 return plugin_names; 55} 56 57// https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewing-support:supports-indexed-properties 58bool PluginArray::is_supported_property_index(u32 index) const 59{ 60 // The PluginArray interface supports indexed properties. The supported property indices are the indices of this's relevant global object's PDF viewer plugin objects. 61 auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this)); 62 return index < window.pdf_viewer_plugin_objects().size(); 63} 64 65// https://html.spec.whatwg.org/multipage/system-state.html#dom-pluginarray-length 66size_t PluginArray::length() const 67{ 68 // The PluginArray interface's length getter steps are to return this's relevant global object's PDF viewer plugin objects's size. 69 auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this)); 70 return window.pdf_viewer_plugin_objects().size(); 71} 72 73// https://html.spec.whatwg.org/multipage/system-state.html#dom-pluginarray-item 74JS::GCPtr<Plugin> PluginArray::item(u32 index) const 75{ 76 // 1. Let plugins be this's relevant global object's PDF viewer plugin objects. 77 auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this)); 78 auto plugins = window.pdf_viewer_plugin_objects(); 79 80 // 2. If index < plugins's size, then return plugins[index]. 81 if (index < plugins.size()) 82 return plugins[index]; 83 84 // 3. Return null. 85 return nullptr; 86} 87 88// https://html.spec.whatwg.org/multipage/system-state.html#dom-pluginarray-nameditem 89JS::GCPtr<Plugin> PluginArray::named_item(String const& name) const 90{ 91 // 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. 92 auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this)); 93 auto plugins = window.pdf_viewer_plugin_objects(); 94 95 for (auto& plugin : plugins) { 96 if (plugin->name() == name) 97 return plugin; 98 } 99 100 // 2. Return null. 101 return nullptr; 102} 103 104WebIDL::ExceptionOr<JS::Value> PluginArray::item_value(size_t index) const 105{ 106 auto return_value = item(index); 107 if (!return_value) 108 return JS::js_null(); 109 return return_value.ptr(); 110} 111 112WebIDL::ExceptionOr<JS::Value> PluginArray::named_item_value(DeprecatedFlyString const& name) const 113{ 114 auto converted_name = TRY_OR_THROW_OOM(vm(), String::from_deprecated_string(name)); 115 auto return_value = named_item(converted_name); 116 if (!return_value) 117 return JS::js_null(); 118 return return_value.ptr(); 119} 120 121}