Navigator.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
  3. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Heap/Heap.h>
  8. #include <LibJS/Runtime/Realm.h>
  9. #include <LibWeb/Bindings/Intrinsics.h>
  10. #include <LibWeb/Clipboard/Clipboard.h>
  11. #include <LibWeb/HTML/Navigator.h>
  12. #include <LibWeb/HTML/Scripting/Environments.h>
  13. #include <LibWeb/HTML/Window.h>
  14. #include <LibWeb/Page/Page.h>
  15. namespace Web::HTML {
  16. JS_DEFINE_ALLOCATOR(Navigator);
  17. JS::NonnullGCPtr<Navigator> Navigator::create(JS::Realm& realm)
  18. {
  19. return realm.heap().allocate<Navigator>(realm, realm);
  20. }
  21. Navigator::Navigator(JS::Realm& realm)
  22. : PlatformObject(realm)
  23. {
  24. }
  25. Navigator::~Navigator() = default;
  26. void Navigator::initialize(JS::Realm& realm)
  27. {
  28. Base::initialize(realm);
  29. WEB_SET_PROTOTYPE_FOR_INTERFACE(Navigator);
  30. }
  31. // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-pdfviewerenabled
  32. bool Navigator::pdf_viewer_enabled() const
  33. {
  34. // The NavigatorPlugins mixin's pdfViewerEnabled getter steps are to return the user agent's PDF viewer supported.
  35. // NOTE: The NavigatorPlugins mixin should only be exposed on the Window object.
  36. auto const& window = verify_cast<HTML::Window>(HTML::current_global_object());
  37. return window.page().pdf_viewer_supported();
  38. }
  39. // https://w3c.github.io/webdriver/#dfn-webdriver
  40. bool Navigator::webdriver() const
  41. {
  42. // Returns true if webdriver-active flag is set, false otherwise.
  43. // NOTE: The NavigatorAutomationInformation interface should not be exposed on WorkerNavigator.
  44. auto const& window = verify_cast<HTML::Window>(HTML::current_global_object());
  45. return window.page().is_webdriver_active();
  46. }
  47. void Navigator::visit_edges(Cell::Visitor& visitor)
  48. {
  49. Base::visit_edges(visitor);
  50. visitor.visit(m_mime_type_array);
  51. visitor.visit(m_plugin_array);
  52. visitor.visit(m_clipboard);
  53. }
  54. JS::NonnullGCPtr<MimeTypeArray> Navigator::mime_types()
  55. {
  56. if (!m_mime_type_array)
  57. m_mime_type_array = heap().allocate<MimeTypeArray>(realm(), realm());
  58. return *m_mime_type_array;
  59. }
  60. JS::NonnullGCPtr<PluginArray> Navigator::plugins()
  61. {
  62. if (!m_plugin_array)
  63. m_plugin_array = heap().allocate<PluginArray>(realm(), realm());
  64. return *m_plugin_array;
  65. }
  66. JS::NonnullGCPtr<Clipboard::Clipboard> Navigator::clipboard()
  67. {
  68. if (!m_clipboard)
  69. m_clipboard = heap().allocate<Clipboard::Clipboard>(realm(), realm());
  70. return *m_clipboard;
  71. }
  72. }