MimeType.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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/MimeTypePrototype.h>
  8. #include <LibWeb/HTML/MimeType.h>
  9. #include <LibWeb/HTML/Scripting/Environments.h>
  10. #include <LibWeb/HTML/Window.h>
  11. namespace Web::HTML {
  12. GC_DEFINE_ALLOCATOR(MimeType);
  13. MimeType::MimeType(JS::Realm& realm, String type)
  14. : Bindings::PlatformObject(realm)
  15. , m_type(move(type))
  16. {
  17. }
  18. MimeType::~MimeType() = default;
  19. void MimeType::initialize(JS::Realm& realm)
  20. {
  21. Base::initialize(realm);
  22. WEB_SET_PROTOTYPE_FOR_INTERFACE(MimeType);
  23. }
  24. // https://html.spec.whatwg.org/multipage/system-state.html#concept-mimetype-type
  25. String const& MimeType::type() const
  26. {
  27. // The MimeType interface's type getter steps are to return this's type.
  28. return m_type;
  29. }
  30. // https://html.spec.whatwg.org/multipage/system-state.html#dom-mimetype-description
  31. String MimeType::description() const
  32. {
  33. // The MimeType 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-mimetype-suffixes
  38. String const& MimeType::suffixes() const
  39. {
  40. // The MimeType interface's suffixes getter steps are to return "pdf".
  41. static String suffixes_string = "pdf"_string;
  42. return suffixes_string;
  43. }
  44. // https://html.spec.whatwg.org/multipage/system-state.html#dom-mimetype-enabledplugin
  45. GC::Ref<Plugin> MimeType::enabled_plugin() const
  46. {
  47. // The MimeType interface's enabledPlugin getter steps are to return this's relevant global object's PDF viewer plugin objects[0] (i.e., the generic "PDF Viewer" one).
  48. auto& window = verify_cast<HTML::Window>(HTML::relevant_global_object(*this));
  49. auto plugin_objects = window.pdf_viewer_plugin_objects();
  50. // NOTE: If a MimeType object was created, that means PDF viewer support is enabled, meaning there will be Plugin objects.
  51. VERIFY(!plugin_objects.is_empty());
  52. return plugin_objects.first();
  53. }
  54. }