Plugin.cpp 4.5 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. 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"_fly_string));
  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<FlyString> 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. 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<FlyString> const mime_types = {
  53. "application/pdf"_fly_string,
  54. "text/pdf"_fly_string,
  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(FlyString 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(FlyString const& name) const
  104. {
  105. auto return_value = named_item(name);
  106. if (!return_value)
  107. return JS::js_null();
  108. return return_value.ptr();
  109. }
  110. }