Navigator.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/Loader/ResourceLoader.h>
  16. #include <LibWeb/Page/Page.h>
  17. namespace Web::HTML {
  18. JS_DEFINE_ALLOCATOR(Navigator);
  19. JS::NonnullGCPtr<Navigator> Navigator::create(JS::Realm& realm)
  20. {
  21. return realm.heap().allocate<Navigator>(realm, realm);
  22. }
  23. Navigator::Navigator(JS::Realm& realm)
  24. : PlatformObject(realm)
  25. {
  26. }
  27. Navigator::~Navigator() = default;
  28. void Navigator::initialize(JS::Realm& realm)
  29. {
  30. Base::initialize(realm);
  31. WEB_SET_PROTOTYPE_FOR_INTERFACE(Navigator);
  32. }
  33. // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-pdfviewerenabled
  34. bool Navigator::pdf_viewer_enabled() const
  35. {
  36. // The NavigatorPlugins mixin's pdfViewerEnabled getter steps are to return the user agent's PDF viewer supported.
  37. // NOTE: The NavigatorPlugins mixin should only be exposed on the Window object.
  38. auto const& window = verify_cast<HTML::Window>(HTML::current_global_object());
  39. return window.page().pdf_viewer_supported();
  40. }
  41. // https://w3c.github.io/webdriver/#dfn-webdriver
  42. bool Navigator::webdriver() const
  43. {
  44. // Returns true if webdriver-active flag is set, false otherwise.
  45. // NOTE: The NavigatorAutomationInformation interface should not be exposed on WorkerNavigator.
  46. auto const& window = verify_cast<HTML::Window>(HTML::current_global_object());
  47. return window.page().is_webdriver_active();
  48. }
  49. void Navigator::visit_edges(Cell::Visitor& visitor)
  50. {
  51. Base::visit_edges(visitor);
  52. visitor.visit(m_mime_type_array);
  53. visitor.visit(m_plugin_array);
  54. visitor.visit(m_clipboard);
  55. visitor.visit(m_user_activation);
  56. }
  57. JS::NonnullGCPtr<MimeTypeArray> Navigator::mime_types()
  58. {
  59. if (!m_mime_type_array)
  60. m_mime_type_array = heap().allocate<MimeTypeArray>(realm(), realm());
  61. return *m_mime_type_array;
  62. }
  63. JS::NonnullGCPtr<PluginArray> Navigator::plugins()
  64. {
  65. if (!m_plugin_array)
  66. m_plugin_array = heap().allocate<PluginArray>(realm(), realm());
  67. return *m_plugin_array;
  68. }
  69. JS::NonnullGCPtr<Clipboard::Clipboard> Navigator::clipboard()
  70. {
  71. if (!m_clipboard)
  72. m_clipboard = heap().allocate<Clipboard::Clipboard>(realm(), realm());
  73. return *m_clipboard;
  74. }
  75. JS::NonnullGCPtr<UserActivation> Navigator::user_activation()
  76. {
  77. if (!m_user_activation)
  78. m_user_activation = heap().allocate<UserActivation>(realm(), realm());
  79. return *m_user_activation;
  80. }
  81. // https://w3c.github.io/pointerevents/#dom-navigator-maxtouchpoints
  82. WebIDL::Long Navigator::max_touch_points()
  83. {
  84. dbgln("FIXME: Unimplemented Navigator.maxTouchPoints");
  85. return 0;
  86. }
  87. // https://www.w3.org/TR/tracking-dnt/#dom-navigator-donottrack
  88. Optional<FlyString> Navigator::do_not_track() const
  89. {
  90. // The value is null if no DNT header field would be sent (e.g., because a tracking preference is not
  91. // enabled and no user-granted exception is applicable); otherwise, the value is a string beginning with
  92. // "0" or "1", possibly followed by DNT-extension characters.
  93. if (ResourceLoader::the().enable_do_not_track())
  94. return "1"_fly_string;
  95. return {};
  96. }
  97. }