NavigatorObject.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Array.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibWeb/Bindings/NavigatorObject.h>
  9. #include <LibWeb/Bindings/NavigatorPrototype.h>
  10. #include <LibWeb/Loader/ResourceLoader.h>
  11. namespace Web {
  12. namespace Bindings {
  13. NavigatorObject::NavigatorObject(JS::GlobalObject& global_object)
  14. : Object(static_cast<WindowObject&>(global_object).ensure_web_prototype<NavigatorPrototype>("Navigator"))
  15. {
  16. }
  17. void NavigatorObject::initialize(JS::GlobalObject& global_object)
  18. {
  19. auto& heap = this->heap();
  20. auto* languages = MUST(JS::Array::create(global_object, 0));
  21. languages->indexed_properties().append(js_string(heap, "en-US"));
  22. // FIXME: All of these should be in Navigator's prototype and be native accessors
  23. u8 attr = JS::Attribute::Configurable | JS::Attribute::Writable | JS::Attribute::Enumerable;
  24. define_direct_property("appCodeName", js_string(heap, "Mozilla"), attr);
  25. define_direct_property("appName", js_string(heap, "Netscape"), attr);
  26. define_direct_property("appVersion", js_string(heap, "4.0"), attr);
  27. define_direct_property("language", languages->get_without_side_effects(0), attr);
  28. define_direct_property("languages", languages, attr);
  29. define_direct_property("platform", js_string(heap, "SerenityOS"), attr);
  30. define_direct_property("product", js_string(heap, "Gecko"), attr);
  31. define_native_accessor("userAgent", user_agent_getter, {}, JS::Attribute::Configurable | JS::Attribute::Enumerable);
  32. define_native_accessor("cookieEnabled", cookie_enabled_getter, {}, JS::Attribute::Configurable | JS::Attribute::Enumerable);
  33. define_native_function("javaEnabled", java_enabled, 0, JS::Attribute::Configurable | JS::Attribute::Enumerable);
  34. // FIXME: Reflect actual connectivity status.
  35. define_direct_property("onLine", JS::Value(true), attr);
  36. }
  37. JS_DEFINE_NATIVE_FUNCTION(NavigatorObject::user_agent_getter)
  38. {
  39. return JS::js_string(vm, ResourceLoader::the().user_agent());
  40. }
  41. JS_DEFINE_NATIVE_FUNCTION(NavigatorObject::cookie_enabled_getter)
  42. {
  43. // No way of disabling cookies right now :^)
  44. return JS::Value(true);
  45. }
  46. // https://html.spec.whatwg.org/multipage/system-state.html#dom-navigator-javaenabled
  47. JS_DEFINE_NATIVE_FUNCTION(NavigatorObject::java_enabled)
  48. {
  49. // The NavigatorPlugins mixin's javaEnabled() method steps are to return false.
  50. return JS::Value(false);
  51. }
  52. }
  53. }