Navigator.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/Bindings/NavigatorPrototype.h>
  11. #include <LibWeb/Clipboard/Clipboard.h>
  12. #include <LibWeb/HTML/Navigator.h>
  13. #include <LibWeb/HTML/Scripting/Environments.h>
  14. #include <LibWeb/HTML/Window.h>
  15. #include <LibWeb/Page/Page.h>
  16. namespace Web::HTML {
  17. JS_DEFINE_ALLOCATOR(Navigator);
  18. JS::NonnullGCPtr<Navigator> Navigator::create(JS::Realm& realm)
  19. {
  20. return realm.heap().allocate<Navigator>(realm, realm);
  21. }
  22. Navigator::Navigator(JS::Realm& realm)
  23. : PlatformObject(realm)
  24. {
  25. }
  26. Navigator::~Navigator() = default;
  27. void Navigator::initialize(JS::Realm& realm)
  28. {
  29. Base::initialize(realm);
  30. WEB_SET_PROTOTYPE_FOR_INTERFACE(Navigator);
  31. }
  32. // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-pdfviewerenabled
  33. bool Navigator::pdf_viewer_enabled() const
  34. {
  35. // The NavigatorPlugins mixin's pdfViewerEnabled getter steps are to return the user agent's PDF viewer supported.
  36. // NOTE: The NavigatorPlugins mixin should only be exposed on the Window object.
  37. auto const& window = verify_cast<HTML::Window>(HTML::current_global_object());
  38. return window.page().pdf_viewer_supported();
  39. }
  40. // https://w3c.github.io/webdriver/#dfn-webdriver
  41. bool Navigator::webdriver() const
  42. {
  43. // Returns true if webdriver-active flag is set, false otherwise.
  44. // NOTE: The NavigatorAutomationInformation interface should not be exposed on WorkerNavigator.
  45. auto const& window = verify_cast<HTML::Window>(HTML::current_global_object());
  46. return window.page().is_webdriver_active();
  47. }
  48. void Navigator::visit_edges(Cell::Visitor& visitor)
  49. {
  50. Base::visit_edges(visitor);
  51. visitor.visit(m_mime_type_array);
  52. visitor.visit(m_plugin_array);
  53. visitor.visit(m_clipboard);
  54. visitor.visit(m_user_activation);
  55. }
  56. JS::NonnullGCPtr<MimeTypeArray> Navigator::mime_types()
  57. {
  58. if (!m_mime_type_array)
  59. m_mime_type_array = heap().allocate<MimeTypeArray>(realm(), realm());
  60. return *m_mime_type_array;
  61. }
  62. JS::NonnullGCPtr<PluginArray> Navigator::plugins()
  63. {
  64. if (!m_plugin_array)
  65. m_plugin_array = heap().allocate<PluginArray>(realm(), realm());
  66. return *m_plugin_array;
  67. }
  68. JS::NonnullGCPtr<Clipboard::Clipboard> Navigator::clipboard()
  69. {
  70. if (!m_clipboard)
  71. m_clipboard = heap().allocate<Clipboard::Clipboard>(realm(), realm());
  72. return *m_clipboard;
  73. }
  74. JS::NonnullGCPtr<UserActivation> Navigator::user_activation()
  75. {
  76. if (!m_user_activation)
  77. m_user_activation = heap().allocate<UserActivation>(realm(), realm());
  78. return *m_user_activation;
  79. }
  80. // https://w3c.github.io/pointerevents/#dom-navigator-maxtouchpoints
  81. WebIDL::Long Navigator::max_touch_points()
  82. {
  83. dbgln("FIXME: Unimplemented Navigator.maxTouchPoints");
  84. return 0;
  85. }
  86. }