PluginArray.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/Bindings/PluginArrayPrototype.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. namespace Web::HTML {
  13. GC_DEFINE_ALLOCATOR(PluginArray);
  14. PluginArray::PluginArray(JS::Realm& realm)
  15. : Bindings::PlatformObject(realm)
  16. {
  17. m_legacy_platform_object_flags = LegacyPlatformObjectFlags {
  18. .supports_indexed_properties = true,
  19. .supports_named_properties = true,
  20. .has_legacy_unenumerable_named_properties_interface_extended_attribute = true,
  21. };
  22. }
  23. PluginArray::~PluginArray() = default;
  24. void PluginArray::initialize(JS::Realm& realm)
  25. {
  26. Base::initialize(realm);
  27. WEB_SET_PROTOTYPE_FOR_INTERFACE(PluginArray);
  28. }
  29. // https://html.spec.whatwg.org/multipage/system-state.html#dom-pluginarray-refresh
  30. void PluginArray::refresh() const
  31. {
  32. // The PluginArray interface's refresh() method steps are to do nothing.
  33. }
  34. // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewing-support:support-named-properties
  35. Vector<FlyString> PluginArray::supported_property_names() const
  36. {
  37. // 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.
  38. auto const& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this));
  39. if (!window.page().pdf_viewer_supported())
  40. return {};
  41. // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-plugin-names
  42. static Vector<FlyString> const plugin_names = {
  43. "PDF Viewer"_fly_string,
  44. "Chrome PDF Viewer"_fly_string,
  45. "Chromium PDF Viewer"_fly_string,
  46. "Microsoft Edge PDF Viewer"_fly_string,
  47. "WebKit built-in PDF"_fly_string,
  48. };
  49. return plugin_names;
  50. }
  51. // https://html.spec.whatwg.org/multipage/system-state.html#dom-pluginarray-length
  52. size_t PluginArray::length() const
  53. {
  54. // The PluginArray interface's length getter steps are to return this's relevant global object's PDF viewer plugin objects's size.
  55. auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this));
  56. return window.pdf_viewer_plugin_objects().size();
  57. }
  58. // https://html.spec.whatwg.org/multipage/system-state.html#dom-pluginarray-item
  59. GC::Ptr<Plugin> PluginArray::item(u32 index) const
  60. {
  61. // 1. Let plugins be this's relevant global object's PDF viewer plugin objects.
  62. auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this));
  63. auto plugins = window.pdf_viewer_plugin_objects();
  64. // 2. If index < plugins's size, then return plugins[index].
  65. if (index < plugins.size())
  66. return plugins[index];
  67. // 3. Return null.
  68. return nullptr;
  69. }
  70. // https://html.spec.whatwg.org/multipage/system-state.html#dom-pluginarray-nameditem
  71. GC::Ptr<Plugin> PluginArray::named_item(FlyString const& name) const
  72. {
  73. // 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.
  74. auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this));
  75. auto plugins = window.pdf_viewer_plugin_objects();
  76. for (auto& plugin : plugins) {
  77. if (plugin->name() == name)
  78. return plugin;
  79. }
  80. // 2. Return null.
  81. return nullptr;
  82. }
  83. Optional<JS::Value> PluginArray::item_value(size_t index) const
  84. {
  85. auto return_value = item(index);
  86. if (!return_value)
  87. return {};
  88. return return_value.ptr();
  89. }
  90. JS::Value PluginArray::named_item_value(FlyString const& name) const
  91. {
  92. auto return_value = named_item(name);
  93. if (!return_value)
  94. return JS::js_null();
  95. return return_value.ptr();
  96. }
  97. }