Plugin.cpp 4.7 KB

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