WorkerGlobalScope.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Vector.h>
  7. #include <LibWeb/Bindings/DedicatedWorkerExposedInterfaces.h>
  8. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  9. #include <LibWeb/Bindings/Intrinsics.h>
  10. #include <LibWeb/Bindings/WorkerGlobalScopePrototype.h>
  11. #include <LibWeb/Forward.h>
  12. #include <LibWeb/HTML/EventHandler.h>
  13. #include <LibWeb/HTML/EventNames.h>
  14. #include <LibWeb/HTML/WorkerGlobalScope.h>
  15. #include <LibWeb/HTML/WorkerLocation.h>
  16. #include <LibWeb/HTML/WorkerNavigator.h>
  17. namespace Web::HTML {
  18. JS_DEFINE_ALLOCATOR(WorkerGlobalScope);
  19. WorkerGlobalScope::WorkerGlobalScope(JS::Realm& realm, Web::Page& page)
  20. : DOM::EventTarget(realm)
  21. , m_page(page)
  22. {
  23. }
  24. WorkerGlobalScope::~WorkerGlobalScope() = default;
  25. void WorkerGlobalScope::initialize_web_interfaces(Badge<WorkerEnvironmentSettingsObject>)
  26. {
  27. auto& realm = this->realm();
  28. Base::initialize(realm);
  29. // FIXME: Handle shared worker
  30. add_dedicated_worker_exposed_interfaces(*this);
  31. Object::set_prototype(&Bindings::ensure_web_prototype<Bindings::WorkerGlobalScopePrototype>(realm, "WorkerGlobalScope"));
  32. WindowOrWorkerGlobalScopeMixin::initialize(realm);
  33. m_navigator = WorkerNavigator::create(*this);
  34. }
  35. void WorkerGlobalScope::visit_edges(Cell::Visitor& visitor)
  36. {
  37. Base::visit_edges(visitor);
  38. WindowOrWorkerGlobalScopeMixin::visit_edges(visitor);
  39. visitor.visit(m_location);
  40. visitor.visit(m_navigator);
  41. }
  42. // https://html.spec.whatwg.org/multipage/workers.html#importing-scripts-and-libraries
  43. WebIDL::ExceptionOr<void> WorkerGlobalScope::import_scripts(Vector<String> urls)
  44. {
  45. // The algorithm may optionally be customized by supplying custom perform the fetch hooks,
  46. // which if provided will be used when invoking fetch a classic worker-imported script.
  47. // NOTE: Service Workers is an example of a specification that runs this algorithm with its own options for the perform the fetch hook.
  48. // FIXME: 1. If worker global scope's type is "module", throw a TypeError exception.
  49. // FIXME: 2. Let settings object be the current settings object.
  50. // 3. If urls is empty, return.
  51. if (urls.is_empty())
  52. return {};
  53. // FIXME: 4. Parse each value in urls relative to settings object. If any fail, throw a "SyntaxError" DOMException.
  54. // FIXME: 5. For each url in the resulting URL records, run these substeps:
  55. // 1. Fetch a classic worker-imported script given url and settings object, passing along any custom perform the fetch steps provided.
  56. // If this succeeds, let script be the result. Otherwise, rethrow the exception.
  57. // 2. Run the classic script script, with the rethrow errors argument set to true.
  58. // NOTE: script will run until it either returns, fails to parse, fails to catch an exception,
  59. // or gets prematurely aborted by the terminate a worker algorithm defined above.
  60. // If an exception was thrown or if the script was prematurely aborted, then abort all these steps,
  61. // letting the exception or aborting continue to be processed by the calling script.
  62. return {};
  63. }
  64. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerglobalscope-location
  65. JS::NonnullGCPtr<WorkerLocation> WorkerGlobalScope::location() const
  66. {
  67. // The location attribute must return the WorkerLocation object whose associated WorkerGlobalScope object is the WorkerGlobalScope object.
  68. return *m_location;
  69. }
  70. // https://html.spec.whatwg.org/multipage/workers.html#dom-worker-navigator
  71. JS::NonnullGCPtr<WorkerNavigator> WorkerGlobalScope::navigator() const
  72. {
  73. // The navigator attribute of the WorkerGlobalScope interface must return an instance of the WorkerNavigator interface,
  74. // which represents the identity and state of the user agent (the client).
  75. return *m_navigator;
  76. }
  77. #undef __ENUMERATE
  78. #define __ENUMERATE(attribute_name, event_name) \
  79. void WorkerGlobalScope::set_##attribute_name(WebIDL::CallbackType* value) \
  80. { \
  81. set_event_handler_attribute(event_name, move(value)); \
  82. } \
  83. WebIDL::CallbackType* WorkerGlobalScope::attribute_name() \
  84. { \
  85. return event_handler_attribute(event_name); \
  86. }
  87. ENUMERATE_WORKER_GLOBAL_SCOPE_EVENT_HANDLERS(__ENUMERATE)
  88. #undef __ENUMERATE
  89. }