WorkerGlobalScope.cpp 4.0 KB

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