WorkerGlobalScope.h 5.4 KB

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