Worker.h (4173B)
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/. */ 6 7 #ifndef mozilla_dom_Worker_h 8 #define mozilla_dom_Worker_h 9 10 #include "mozilla/Attributes.h" 11 #include "mozilla/DOMEventTargetHelper.h" 12 #include "mozilla/RefPtr.h" 13 #include "mozilla/WeakPtr.h" 14 #include "mozilla/dom/DebuggerNotificationBinding.h" 15 16 #ifdef XP_WIN 17 # undef PostMessage 18 #endif 19 20 namespace mozilla::dom { 21 22 class EventWithOptionsRunnable; 23 struct StructuredSerializeOptions; 24 struct WorkerOptions; 25 class WorkerPrivate; 26 27 class TrustedScriptURLOrUSVString; 28 29 class Worker : public DOMEventTargetHelper, public SupportsWeakPtr { 30 public: 31 NS_DECL_ISUPPORTS_INHERITED 32 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(Worker, 33 DOMEventTargetHelper) 34 // TODO(bug 1749042): Mark as MOZ_CAN_RUN_SCRIPT when IDL constructors can be. 35 MOZ_CAN_RUN_SCRIPT_BOUNDARY static already_AddRefed<Worker> Constructor( 36 const GlobalObject& aGlobal, 37 const TrustedScriptURLOrUSVString& aScriptURL, 38 const WorkerOptions& aOptions, ErrorResult& aRv); 39 40 JSObject* WrapObject(JSContext* aCx, 41 JS::Handle<JSObject*> aGivenProto) override; 42 43 Maybe<EventCallbackDebuggerNotificationType> GetDebuggerNotificationType() 44 const override { 45 return Some(EventCallbackDebuggerNotificationType::Worker); 46 } 47 48 // True if the worker is not yet closing from the perspective of this, the 49 // owning thread, and therefore it's okay to post a message to the worker. 50 // This is not a guarantee that the worker will process the message. 51 // 52 // This method will return false if `globalThis.close()` is invoked on the 53 // worker before that method returns control to the caller and without waiting 54 // for any task to be queued on this thread and run; this biases us to avoid 55 // doing wasteful work but does mean if you are exposing something to content 56 // that is specified to only transition as the result of a task, then you 57 // should not use this method. 58 // 59 // The method name comes from 60 // https://html.spec.whatwg.org/multipage/web-messaging.html#eligible-for-messaging 61 // and is intended to convey whether it's okay to begin to take the steps to 62 // create an `EventWithOptionsRunnable` to pass to `PostEventWithOptions`. 63 // Note that early returning based on calling this method without performing 64 // the structured serialization steps that would otherwise run is potentially 65 // observable to content if content is in control of any of the payload in 66 // such a way that an object with getters or a proxy could be provided. 67 // 68 // There is an identically named method on nsIGlobalObject and the semantics 69 // are intentionally similar but please make sure you document your 70 // assumptions when calling either method. 71 bool IsEligibleForMessaging(); 72 73 void PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage, 74 const Sequence<JSObject*>& aTransferable, ErrorResult& aRv); 75 76 void PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage, 77 const StructuredSerializeOptions& aOptions, 78 ErrorResult& aRv); 79 80 // Callers must call `IsEligibleForMessaging` before constructing an 81 // `EventWithOptionsRunnable` subclass. 82 void PostEventWithOptions(JSContext* aCx, JS::Handle<JS::Value> aOptions, 83 const Sequence<JSObject*>& aTransferable, 84 EventWithOptionsRunnable* aRunnable, 85 ErrorResult& aRv); 86 87 void Terminate(); 88 89 IMPL_EVENT_HANDLER(error) 90 IMPL_EVENT_HANDLER(message) 91 IMPL_EVENT_HANDLER(messageerror) 92 93 protected: 94 Worker(nsIGlobalObject* aGlobalObject, 95 already_AddRefed<WorkerPrivate> aWorkerPrivate); 96 ~Worker(); 97 98 friend class EventWithOptionsRunnable; 99 RefPtr<WorkerPrivate> mWorkerPrivate; 100 }; 101 102 } // namespace mozilla::dom 103 104 #endif /* mozilla_dom_Worker_h */