PluginArray.cpp 4.3 KB

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