Plugin.cpp 4.5 KB

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