Navigator.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/HTML/Navigator.h>
  11. #include <LibWeb/HTML/Scripting/Environments.h>
  12. #include <LibWeb/HTML/Window.h>
  13. #include <LibWeb/Page/Page.h>
  14. namespace Web::HTML {
  15. JS::NonnullGCPtr<Navigator> Navigator::create(JS::Realm& realm)
  16. {
  17. return realm.heap().allocate<Navigator>(realm, realm);
  18. }
  19. Navigator::Navigator(JS::Realm& realm)
  20. : PlatformObject(realm)
  21. {
  22. }
  23. Navigator::~Navigator() = default;
  24. void Navigator::initialize(JS::Realm& realm)
  25. {
  26. Base::initialize(realm);
  27. set_prototype(&Bindings::ensure_web_prototype<Bindings::NavigatorPrototype>(realm, "Navigator"));
  28. }
  29. // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-pdfviewerenabled
  30. bool Navigator::pdf_viewer_enabled() const
  31. {
  32. // The NavigatorPlugins mixin's pdfViewerEnabled getter steps are to return the user agent's PDF viewer supported.
  33. // NOTE: The NavigatorPlugins mixin should only be exposed on the Window object.
  34. auto const& window = verify_cast<HTML::Window>(HTML::current_global_object());
  35. return window.page()->pdf_viewer_supported();
  36. }
  37. // https://w3c.github.io/webdriver/#dfn-webdriver
  38. bool Navigator::webdriver() const
  39. {
  40. // Returns true if webdriver-active flag is set, false otherwise.
  41. // NOTE: The NavigatorAutomationInformation interface should not be exposed on WorkerNavigator.
  42. auto const& window = verify_cast<HTML::Window>(HTML::current_global_object());
  43. return window.page()->is_webdriver_active();
  44. }
  45. void Navigator::visit_edges(Cell::Visitor& visitor)
  46. {
  47. Base::visit_edges(visitor);
  48. visitor.visit(m_mime_type_array);
  49. visitor.visit(m_plugin_array);
  50. }
  51. JS::NonnullGCPtr<MimeTypeArray> Navigator::mime_types()
  52. {
  53. if (!m_mime_type_array)
  54. m_mime_type_array = heap().allocate<MimeTypeArray>(realm(), realm());
  55. return *m_mime_type_array;
  56. }
  57. JS::NonnullGCPtr<PluginArray> Navigator::plugins()
  58. {
  59. if (!m_plugin_array)
  60. m_plugin_array = heap().allocate<PluginArray>(realm(), realm());
  61. return *m_plugin_array;
  62. }
  63. }