PluginArray.cpp 4.2 KB

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