EventWithOptionsRunnable.h (2165B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=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 https://mozilla.org/MPL/2.0/. */ 6 7 #ifndef MOZILLA_DOM_WORKERS_EVENTWITHOPTIONSRUNNABLE_H_ 8 #define MOZILLA_DOM_WORKERS_EVENTWITHOPTIONSRUNNABLE_H_ 9 10 #include "WorkerCommon.h" 11 #include "WorkerRunnable.h" 12 #include "mozilla/dom/StructuredCloneHolder.h" 13 14 namespace mozilla { 15 class DOMEventTargetHelper; 16 17 namespace dom { 18 class Event; 19 class EventTarget; 20 class Worker; 21 class WorkerPrivate; 22 23 // Cargo-culted from MesssageEventRunnable. 24 // Intended to be used for the idiom where arbitrary options are transferred to 25 // the worker thread (with optional transfer functions), which are then used to 26 // build an event, which is then fired on the global worker scope. 27 class EventWithOptionsRunnable : public WorkerDebuggeeRunnable, 28 public StructuredCloneHolder { 29 public: 30 explicit EventWithOptionsRunnable( 31 Worker& aWorker, const char* aName = "EventWithOptionsRunnable"); 32 void InitOptions(JSContext* aCx, JS::Handle<JS::Value> aOptions, 33 const Sequence<JSObject*>& aTransferable, ErrorResult& aRv); 34 35 // Called on the worker thread. The event returned will be fired on the 36 // worker's global scope. If a StrongWorkerRef needs to be retained, the 37 // implementation can do so with the WorkerPrivate. 38 virtual already_AddRefed<Event> BuildEvent( 39 JSContext* aCx, nsIGlobalObject* aGlobal, EventTarget* aTarget, 40 JS::Handle<JS::Value> aOptions) = 0; 41 42 // Called on the worker thread 43 virtual void OptionsDeserializeFailed(ErrorResult& aRv) {} 44 45 protected: 46 virtual ~EventWithOptionsRunnable(); 47 48 private: 49 bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override; 50 bool BuildAndFireEvent(JSContext* aCx, WorkerPrivate* aWorkerPrivate, 51 DOMEventTargetHelper* aTarget); 52 }; 53 } // namespace dom 54 } // namespace mozilla 55 56 #endif // MOZILLA_DOM_WORKERS_EVENTWITHOPTIONSRUNNABLE_H_