NavigatorObject.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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/Loader/ResourceLoader.h>
  10. namespace Web {
  11. namespace Bindings {
  12. NavigatorObject::NavigatorObject(JS::GlobalObject& global_object)
  13. : Object(*global_object.object_prototype())
  14. {
  15. }
  16. void NavigatorObject::initialize(JS::GlobalObject& global_object)
  17. {
  18. auto& heap = this->heap();
  19. auto* languages = JS::Array::create(global_object, 0);
  20. languages->indexed_properties().append(js_string(heap, "en-US"));
  21. // FIXME: All of these should be in Navigator's prototype and be native accessors
  22. u8 attr = JS::Attribute::Configurable | JS::Attribute::Writable | JS::Attribute::Enumerable;
  23. define_direct_property("appCodeName", js_string(heap, "Mozilla"), attr);
  24. define_direct_property("appName", js_string(heap, "Netscape"), attr);
  25. define_direct_property("appVersion", js_string(heap, "4.0"), attr);
  26. define_direct_property("language", languages->get_without_side_effects(0), attr);
  27. define_direct_property("languages", languages, attr);
  28. define_direct_property("platform", js_string(heap, "SerenityOS"), attr);
  29. define_direct_property("product", js_string(heap, "Gecko"), attr);
  30. define_native_accessor("userAgent", user_agent_getter, {}, JS::Attribute::Configurable | JS::Attribute::Enumerable);
  31. define_native_accessor("cookieEnabled", cookie_enabled_getter, {}, JS::Attribute::Configurable | JS::Attribute::Enumerable);
  32. // FIXME: Reflect actual connectivity status.
  33. define_direct_property("onLine", JS::Value(true), attr);
  34. }
  35. NavigatorObject::~NavigatorObject()
  36. {
  37. }
  38. JS_DEFINE_NATIVE_FUNCTION(NavigatorObject::user_agent_getter)
  39. {
  40. return JS::js_string(vm, ResourceLoader::the().user_agent());
  41. }
  42. JS_DEFINE_NATIVE_FUNCTION(NavigatorObject::cookie_enabled_getter)
  43. {
  44. // No way of disabling cookies right now :^)
  45. return JS::Value(true);
  46. }
  47. }
  48. }