WorkerGlobalScope.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Optional.h>
  8. #include <AK/RefCounted.h>
  9. #include <AK/URL.h>
  10. #include <LibWeb/DOM/EventTarget.h>
  11. #include <LibWeb/Forward.h>
  12. #include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
  13. #include <LibWeb/HTML/WorkerLocation.h>
  14. #include <LibWeb/HTML/WorkerNavigator.h>
  15. #include <LibWeb/WebIDL/ExceptionOr.h>
  16. #define ENUMERATE_WORKER_GLOBAL_SCOPE_EVENT_HANDLERS(E) \
  17. E(onerror, HTML::EventNames::error) \
  18. E(onlanguagechange, HTML::EventNames::languagechange) \
  19. E(ononline, HTML::EventNames::online) \
  20. E(onoffline, HTML::EventNames::offline) \
  21. E(onrejectionhandled, HTML::EventNames::rejectionhandled) \
  22. E(onunhandledrejection, HTML::EventNames::unhandledrejection)
  23. namespace Web::HTML {
  24. // https://html.spec.whatwg.org/multipage/workers.html#the-workerglobalscope-common-interface
  25. // WorkerGlobalScope is the base class of each real WorkerGlobalScope that will be created when the
  26. // user agent runs the run a worker algorithm.
  27. class WorkerGlobalScope
  28. : public DOM::EventTarget
  29. , public WindowOrWorkerGlobalScopeMixin {
  30. WEB_PLATFORM_OBJECT(WorkerGlobalScope, DOM::EventTarget);
  31. public:
  32. virtual ~WorkerGlobalScope() override;
  33. // ^WindowOrWorkerGlobalScopeMixin
  34. virtual Bindings::PlatformObject& this_impl() override { return *this; }
  35. virtual Bindings::PlatformObject const& this_impl() const override { return *this; }
  36. using WindowOrWorkerGlobalScopeMixin::atob;
  37. using WindowOrWorkerGlobalScopeMixin::btoa;
  38. using WindowOrWorkerGlobalScopeMixin::clear_interval;
  39. using WindowOrWorkerGlobalScopeMixin::clear_timeout;
  40. using WindowOrWorkerGlobalScopeMixin::fetch;
  41. using WindowOrWorkerGlobalScopeMixin::queue_microtask;
  42. using WindowOrWorkerGlobalScopeMixin::set_interval;
  43. using WindowOrWorkerGlobalScopeMixin::set_timeout;
  44. using WindowOrWorkerGlobalScopeMixin::structured_clone;
  45. // Following methods are from the WorkerGlobalScope IDL definition
  46. // https://html.spec.whatwg.org/multipage/workers.html#the-workerglobalscope-common-interface
  47. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerglobalscope-self
  48. JS::NonnullGCPtr<WorkerGlobalScope const> self() const { return *this; }
  49. JS::NonnullGCPtr<WorkerLocation> location() const;
  50. JS::NonnullGCPtr<WorkerNavigator> navigator() const;
  51. WebIDL::ExceptionOr<void> import_scripts(Vector<String> urls);
  52. #undef __ENUMERATE
  53. #define __ENUMERATE(attribute_name, event_name) \
  54. void set_##attribute_name(WebIDL::CallbackType*); \
  55. WebIDL::CallbackType* attribute_name();
  56. ENUMERATE_WORKER_GLOBAL_SCOPE_EVENT_HANDLERS(__ENUMERATE)
  57. #undef __ENUMERATE
  58. // Non-IDL public methods
  59. AK::URL const& url() const { return m_url.value(); }
  60. void set_url(AK::URL const& url) { m_url = url; }
  61. // Spec note: While the WorkerLocation object is created after the WorkerGlobalScope object,
  62. // this is not problematic as it cannot be observed from script.
  63. void set_location(JS::NonnullGCPtr<WorkerLocation> loc) { m_location = move(loc); }
  64. void initialize_web_interfaces(Badge<WorkerEnvironmentSettingsObject>);
  65. Web::Page* page() { return &m_page; }
  66. protected:
  67. explicit WorkerGlobalScope(JS::Realm&, Web::Page&);
  68. private:
  69. virtual void visit_edges(Cell::Visitor&) override;
  70. JS::GCPtr<WorkerLocation> m_location;
  71. JS::GCPtr<WorkerNavigator> m_navigator;
  72. // FIXME: Add all these internal slots
  73. // https://html.spec.whatwg.org/multipage/workers.html#concept-WorkerGlobalScope-owner-set
  74. // A WorkerGlobalScope object has an associated owner set (a set of Document and WorkerGlobalScope objects). It is initially empty and populated when the worker is created or obtained.
  75. // Note: It is a set, instead of a single owner, to accommodate SharedWorkerGlobalScope objects.
  76. // https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-type
  77. // A WorkerGlobalScope object has an associated type ("classic" or "module"). It is set during creation.
  78. // https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-url
  79. // A WorkerGlobalScope object has an associated url (null or a URL). It is initially null.
  80. Optional<AK::URL> m_url;
  81. // https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-name
  82. // A WorkerGlobalScope object has an associated name (a string). It is set during creation.
  83. // Note: The name can have different semantics for each subclass of WorkerGlobalScope.
  84. // For DedicatedWorkerGlobalScope instances, it is simply a developer-supplied name, useful mostly for debugging purposes.
  85. // For SharedWorkerGlobalScope instances, it allows obtaining a reference to a common shared worker via the SharedWorker() constructor.
  86. // For ServiceWorkerGlobalScope objects, it doesn't make sense (and as such isn't exposed through the JavaScript API at all).
  87. // https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-policy-container
  88. // A WorkerGlobalScope object has an associated policy container (a policy container). It is initially a new policy container.
  89. // https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-embedder-policy
  90. // A WorkerGlobalScope object has an associated embedder policy (an embedder policy).
  91. // https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-module-map
  92. // A WorkerGlobalScope object has an associated module map. It is a module map, initially empty.
  93. // https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-cross-origin-isolated-capability
  94. bool m_cross_origin_isolated_capability { false };
  95. Web::Page& m_page;
  96. };
  97. }