Navigator.cpp 3.8 KB

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