WorkerGlobalScope.h 6.4 KB

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