PluginArray.cpp 4.1 KB

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