Plugin.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/Plugin.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. Plugin::Plugin(JS::Realm& realm, String name)
  13. : Bindings::LegacyPlatformObject(realm)
  14. , m_name(move(name))
  15. {
  16. }
  17. Plugin::~Plugin() = default;
  18. void Plugin::initialize(JS::Realm& realm)
  19. {
  20. Base::initialize(realm);
  21. set_prototype(&Bindings::ensure_web_prototype<Bindings::PluginPrototype>(realm, "Plugin"));
  22. }
  23. // https://html.spec.whatwg.org/multipage/system-state.html#dom-plugin-name
  24. String const& Plugin::name() const
  25. {
  26. // The Plugin interface's name getter steps are to return this's name.
  27. return m_name;
  28. }
  29. // https://html.spec.whatwg.org/multipage/system-state.html#dom-plugin-description
  30. String Plugin::description() const
  31. {
  32. // The Plugin interface's description getter steps are to return "Portable Document Format".
  33. static String description_string = "Portable Document Format"_string;
  34. return description_string;
  35. }
  36. // https://html.spec.whatwg.org/multipage/system-state.html#dom-plugin-filename
  37. String Plugin::filename() const
  38. {
  39. // The Plugin interface's filename getter steps are to return "internal-pdf-viewer".
  40. static String filename_string = "internal-pdf-viewer"_string;
  41. return filename_string;
  42. }
  43. // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewing-support:support-named-properties-3
  44. Vector<DeprecatedString> Plugin::supported_property_names() const
  45. {
  46. // The Plugin interface supports named properties. If the user agent's PDF viewer supported is true, then they are the PDF viewer mime types. Otherwise, they are the empty list.
  47. auto const& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this));
  48. VERIFY(window.page());
  49. if (!window.page()->pdf_viewer_supported())
  50. return {};
  51. // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-mime-types
  52. static Vector<DeprecatedString> mime_types = {
  53. "application/pdf"sv,
  54. "text/pdf"sv,
  55. };
  56. return mime_types;
  57. }
  58. // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewing-support:supports-indexed-properties-3
  59. bool Plugin::is_supported_property_index(u32 index) const
  60. {
  61. // The Plugin interface supports indexed properties. The supported property indices are the indices of this's relevant global object's PDF viewer mime type objects.
  62. auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this));
  63. return index < window.pdf_viewer_mime_type_objects().size();
  64. }
  65. // https://html.spec.whatwg.org/multipage/system-state.html#dom-plugin-length
  66. size_t Plugin::length() const
  67. {
  68. // The Plugin interface's length getter steps are to return this's relevant global object's PDF viewer mime type objects's size.
  69. auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this));
  70. return window.pdf_viewer_mime_type_objects().size();
  71. }
  72. // https://html.spec.whatwg.org/multipage/system-state.html#dom-plugin-item
  73. JS::GCPtr<MimeType> Plugin::item(u32 index) const
  74. {
  75. // 1. Let mimeTypes be this's relevant global object's PDF viewer mime type objects.
  76. auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this));
  77. auto mime_types = window.pdf_viewer_mime_type_objects();
  78. // 2. If index < mimeType's size, then return mimeTypes[index].
  79. if (index < mime_types.size())
  80. return mime_types[index];
  81. // 3. Return null.
  82. return nullptr;
  83. }
  84. JS::GCPtr<MimeType> Plugin::named_item(String const& name) const
  85. {
  86. // 1. For each MimeType mimeType of this's relevant global object's PDF viewer mime type objects: if mimeType's type is name, then return mimeType.
  87. auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this));
  88. auto mime_types = window.pdf_viewer_mime_type_objects();
  89. for (auto& mime_type : mime_types) {
  90. if (mime_type->type() == name)
  91. return mime_type;
  92. }
  93. // 2. Return null.
  94. return nullptr;
  95. }
  96. WebIDL::ExceptionOr<JS::Value> Plugin::item_value(size_t index) const
  97. {
  98. auto return_value = item(index);
  99. if (!return_value)
  100. return JS::js_null();
  101. return return_value.ptr();
  102. }
  103. WebIDL::ExceptionOr<JS::Value> Plugin::named_item_value(DeprecatedFlyString const& name) const
  104. {
  105. auto converted_name = TRY_OR_THROW_OOM(vm(), String::from_deprecated_string(name));
  106. auto return_value = named_item(converted_name);
  107. if (!return_value)
  108. return JS::js_null();
  109. return return_value.ptr();
  110. }
  111. }