Plugin.cpp 4.2 KB

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