{{ gecko_minversion_header("2") }}
{{ non-standard_header() }}
{{ APIRef() }}
Summary
If you're developing privileged code, and would like to create a worker that can use js-ctypes to perform calls to native code, you can do so by using ChromeWorker
instead of the standard {{ domxref("Worker") }} object. It works exactly like a standard {{ domxref("Worker") }}, except that it has access to js-ctypes via a global ctypes
object available in the global scope of the worker.
In addition, you can use ChromeWorker
from JavaScript code modules. See Using workers in JavaScript code modules for details.
Finally, you can use ChromeWorker modules in ChromeWorkers.
See Using web workers for examples and details.
Using XPCOM from chrome workers
{{ obsolete_header("8.0") }}
{{ gecko_callout_heading("8.0") }}
Support for using XPCOM and XPConnect from ChromeWorker
s was removed in Gecko 8.0 when workers were updated to run in their own operating system level threads, with one thread per worker. See {{ bug(649537) }} for details.
Prior to Gecko 8.0 {{ geckoRelease("8.0") }}, you could use the {{ domxref("Worker") }} postMessage()
method to send XPCWrappedNative
objects to chrome workers, as long as the underlying object is marked with the nsIClassInfo.THREADSAFE
flag, which indicates that the object is safe to access from multiple threads at once.
In addition, ChromeWorker
objects have access to a new global XPCOM object -- which is only available to chrome workers -- which has two methods:
-
createInstance()
- Creates an instance of an XPCOM interface. Accepts a single parameter: the contract ID of the interface to create an instance of.
-
getService()
- Returns the shared global service object for the specified XPCOM interface. Accepts a single parameter: the contract ID of the interface to which you wish to obtain a reference.
Using the XPCOM object
Before using the XPCOM object to create instances or access services, it's important to note that this only works with components that are thread safe; if you try to do so with a component that isn't thread safe, an exception will be thrown.
Creating instances of components
If your worker wants to create a thread pool in which it can create additional threads, you can do this:
let myThreadPool = XPCOM.createInstance("@mozilla.org/thread-pool;1"); myThreadPool.threadLimit = 6; /* send some events with the pool */ myThreadPool.shutdown();
Passing objects to chrome workers
Thread safe objects can be passed to chrome workers using the postMessage()
method. Attempting to pass a non-thread safe object will throw an exception.
/* create a new thread, then pass it to the worker */ var thread = Components.classes["@mozilla.org/thread-manager;1"].getService().newThread(0); worker.postMessage(thread);
See also
- Using web workers
- Using workers in JavaScript code modules
- {{ domxref("Worker") }}
SharedWorker
- Web Workers specification
- {{ domxref("WorkerGlobalScope") }}