Bug 1874684 - Part 38: Enable now passing tests. r=allstarschh
[gecko.git] / dom / workers / Worker.h
blob6a0e295fc264590e9eee044c89488123cf99094d
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_Worker_h
8 #define mozilla_dom_Worker_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/dom/DebuggerNotificationBinding.h"
12 #include "mozilla/DOMEventTargetHelper.h"
13 #include "mozilla/RefPtr.h"
14 #include "mozilla/WeakPtr.h"
16 #ifdef XP_WIN
17 # undef PostMessage
18 #endif
20 namespace mozilla::dom {
22 class EventWithOptionsRunnable;
23 struct StructuredSerializeOptions;
24 struct WorkerOptions;
25 class WorkerPrivate;
27 class Worker : public DOMEventTargetHelper, public SupportsWeakPtr {
28 public:
29 NS_DECL_ISUPPORTS_INHERITED
30 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(Worker,
31 DOMEventTargetHelper)
32 static already_AddRefed<Worker> Constructor(const GlobalObject& aGlobal,
33 const nsAString& aScriptURL,
34 const WorkerOptions& aOptions,
35 ErrorResult& aRv);
37 JSObject* WrapObject(JSContext* aCx,
38 JS::Handle<JSObject*> aGivenProto) override;
40 Maybe<EventCallbackDebuggerNotificationType> GetDebuggerNotificationType()
41 const override {
42 return Some(EventCallbackDebuggerNotificationType::Worker);
45 // True if the worker is not yet closing from the perspective of this, the
46 // owning thread, and therefore it's okay to post a message to the worker.
47 // This is not a guarantee that the worker will process the message.
49 // This method will return false if `globalThis.close()` is invoked on the
50 // worker before that method returns control to the caller and without waiting
51 // for any task to be queued on this thread and run; this biases us to avoid
52 // doing wasteful work but does mean if you are exposing something to content
53 // that is specified to only transition as the result of a task, then you
54 // should not use this method.
56 // The method name comes from
57 // https://html.spec.whatwg.org/multipage/web-messaging.html#eligible-for-messaging
58 // and is intended to convey whether it's okay to begin to take the steps to
59 // create an `EventWithOptionsRunnable` to pass to `PostEventWithOptions`.
60 // Note that early returning based on calling this method without performing
61 // the structured serialization steps that would otherwise run is potentially
62 // observable to content if content is in control of any of the payload in
63 // such a way that an object with getters or a proxy could be provided.
65 // There is an identically named method on nsIGlobalObject and the semantics
66 // are intentionally similar but please make sure you document your
67 // assumptions when calling either method.
68 bool IsEligibleForMessaging();
70 void PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
71 const Sequence<JSObject*>& aTransferable, ErrorResult& aRv);
73 void PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
74 const StructuredSerializeOptions& aOptions,
75 ErrorResult& aRv);
77 // Callers must call `IsEligibleForMessaging` before constructing an
78 // `EventWithOptionsRunnable` subclass.
79 void PostEventWithOptions(JSContext* aCx, JS::Handle<JS::Value> aOptions,
80 const Sequence<JSObject*>& aTransferable,
81 EventWithOptionsRunnable* aRunnable,
82 ErrorResult& aRv);
84 void Terminate();
86 IMPL_EVENT_HANDLER(error)
87 IMPL_EVENT_HANDLER(message)
88 IMPL_EVENT_HANDLER(messageerror)
90 protected:
91 Worker(nsIGlobalObject* aGlobalObject,
92 already_AddRefed<WorkerPrivate> aWorkerPrivate);
93 ~Worker();
95 friend class EventWithOptionsRunnable;
96 RefPtr<WorkerPrivate> mWorkerPrivate;
99 } // namespace mozilla::dom
101 #endif /* mozilla_dom_Worker_h */