WorkerGlobalScope.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <LibCore/Socket.h>
  10. #include <LibURL/URL.h>
  11. #include <LibWeb/DOM/EventTarget.h>
  12. #include <LibWeb/Forward.h>
  13. #include <LibWeb/HTML/Scripting/Fetching.h>
  14. #include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
  15. #include <LibWeb/HTML/WorkerLocation.h>
  16. #include <LibWeb/HTML/WorkerNavigator.h>
  17. #include <LibWeb/WebIDL/ExceptionOr.h>
  18. // FIXME: message/messageerror belong on subclasses only
  19. #define ENUMERATE_WORKER_GLOBAL_SCOPE_EVENT_HANDLERS(E) \
  20. E(onerror, HTML::EventNames::error) \
  21. E(onlanguagechange, HTML::EventNames::languagechange) \
  22. E(ononline, HTML::EventNames::online) \
  23. E(onoffline, HTML::EventNames::offline) \
  24. E(onrejectionhandled, HTML::EventNames::rejectionhandled) \
  25. E(onunhandledrejection, HTML::EventNames::unhandledrejection) \
  26. E(onmessage, HTML::EventNames::message) \
  27. E(onmessageerror, HTML::EventNames::messageerror)
  28. namespace Web::HTML {
  29. // https://html.spec.whatwg.org/multipage/workers.html#the-workerglobalscope-common-interface
  30. // WorkerGlobalScope is the base class of each real WorkerGlobalScope that will be created when the
  31. // user agent runs the run a worker algorithm.
  32. class WorkerGlobalScope
  33. : public DOM::EventTarget
  34. , public WindowOrWorkerGlobalScopeMixin {
  35. WEB_PLATFORM_OBJECT(WorkerGlobalScope, DOM::EventTarget);
  36. JS_DECLARE_ALLOCATOR(WorkerGlobalScope);
  37. public:
  38. virtual ~WorkerGlobalScope() override;
  39. // ^WindowOrWorkerGlobalScopeMixin
  40. virtual Bindings::PlatformObject& this_impl() override { return *this; }
  41. virtual Bindings::PlatformObject const& this_impl() const override { return *this; }
  42. using WindowOrWorkerGlobalScopeMixin::atob;
  43. using WindowOrWorkerGlobalScopeMixin::btoa;
  44. using WindowOrWorkerGlobalScopeMixin::clear_interval;
  45. using WindowOrWorkerGlobalScopeMixin::clear_timeout;
  46. using WindowOrWorkerGlobalScopeMixin::create_image_bitmap;
  47. using WindowOrWorkerGlobalScopeMixin::fetch;
  48. using WindowOrWorkerGlobalScopeMixin::performance;
  49. using WindowOrWorkerGlobalScopeMixin::queue_microtask;
  50. using WindowOrWorkerGlobalScopeMixin::set_interval;
  51. using WindowOrWorkerGlobalScopeMixin::set_timeout;
  52. using WindowOrWorkerGlobalScopeMixin::structured_clone;
  53. // Following methods are from the WorkerGlobalScope IDL definition
  54. // https://html.spec.whatwg.org/multipage/workers.html#the-workerglobalscope-common-interface
  55. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerglobalscope-self
  56. JS::NonnullGCPtr<WorkerGlobalScope const> self() const { return *this; }
  57. JS::NonnullGCPtr<WorkerLocation> location() const;
  58. JS::NonnullGCPtr<WorkerNavigator> navigator() const;
  59. WebIDL::ExceptionOr<void> import_scripts(Vector<String> const& urls, PerformTheFetchHook = nullptr);
  60. #undef __ENUMERATE
  61. #define __ENUMERATE(attribute_name, event_name) \
  62. void set_##attribute_name(WebIDL::CallbackType*); \
  63. WebIDL::CallbackType* attribute_name();
  64. ENUMERATE_WORKER_GLOBAL_SCOPE_EVENT_HANDLERS(__ENUMERATE)
  65. #undef __ENUMERATE
  66. WebIDL::ExceptionOr<void> post_message(JS::Value message, StructuredSerializeOptions const&);
  67. JS::NonnullGCPtr<CSS::FontFaceSet> fonts();
  68. // Non-IDL public methods
  69. URL::URL const& url() const { return m_url.value(); }
  70. void set_url(URL::URL const& url) { m_url = url; }
  71. // Spec note: While the WorkerLocation object is created after the WorkerGlobalScope object,
  72. // this is not problematic as it cannot be observed from script.
  73. void set_location(JS::NonnullGCPtr<WorkerLocation> loc) { m_location = move(loc); }
  74. void set_internal_port(JS::NonnullGCPtr<MessagePort> port);
  75. void initialize_web_interfaces(Badge<WorkerEnvironmentSettingsObject>);
  76. Web::Page* page() { return m_page.ptr(); }
  77. PolicyContainer policy_container() const { return m_policy_container; }
  78. bool is_closing() const { return m_closing; }
  79. void close();
  80. protected:
  81. explicit WorkerGlobalScope(JS::Realm&, JS::NonnullGCPtr<Web::Page>);
  82. private:
  83. virtual void visit_edges(Cell::Visitor&) override;
  84. virtual void finalize() override;
  85. JS::GCPtr<WorkerLocation> m_location;
  86. JS::GCPtr<WorkerNavigator> m_navigator;
  87. JS::NonnullGCPtr<Web::Page> m_page;
  88. JS::GCPtr<MessagePort> m_internal_port;
  89. // FIXME: Add all these internal slots
  90. // https://html.spec.whatwg.org/multipage/workers.html#concept-WorkerGlobalScope-owner-set
  91. // 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.
  92. // Note: It is a set, instead of a single owner, to accommodate SharedWorkerGlobalScope objects.
  93. // https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-type
  94. // A WorkerGlobalScope object has an associated type ("classic" or "module"). It is set during creation.
  95. // https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-url
  96. // A WorkerGlobalScope object has an associated url (null or a URL). It is initially null.
  97. Optional<URL::URL> m_url;
  98. // https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-name
  99. // A WorkerGlobalScope object has an associated name (a string). It is set during creation.
  100. // Note: The name can have different semantics for each subclass of WorkerGlobalScope.
  101. // For DedicatedWorkerGlobalScope instances, it is simply a developer-supplied name, useful mostly for debugging purposes.
  102. // For SharedWorkerGlobalScope instances, it allows obtaining a reference to a common shared worker via the SharedWorker() constructor.
  103. // For ServiceWorkerGlobalScope objects, it doesn't make sense (and as such isn't exposed through the JavaScript API at all).
  104. // https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-policy-container
  105. // A WorkerGlobalScope object has an associated policy container (a policy container). It is initially a new policy container.
  106. PolicyContainer m_policy_container;
  107. // https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-embedder-policy
  108. // A WorkerGlobalScope object has an associated embedder policy (an embedder policy).
  109. EmbedderPolicy m_embedder_policy;
  110. // https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-module-map
  111. // A WorkerGlobalScope object has an associated module map. It is a module map, initially empty.
  112. // https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-cross-origin-isolated-capability
  113. bool m_cross_origin_isolated_capability { false };
  114. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerglobalscope-closing
  115. bool m_closing { false };
  116. // https://drafts.csswg.org/css-font-loading/#font-source
  117. JS::GCPtr<CSS::FontFaceSet> m_fonts;
  118. };
  119. }