Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 742259 of ChromeWorker

  • Revision slug: Web/API/ChromeWorker
  • Revision title: ChromeWorker
  • Revision id: 742259
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment Mountain View APIRef Project

Revision Content

{{ gecko_minversion_header("2") }}

{{ non-standard_header() }}

{{ APIRef("Web Workers") }}

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.

Addons must use absolute URLs to load their workers, and that those URLs have to be using a chrome:// or resource:// protocol (file:// is not accepted.) Addons that wish to use file:// URLs must first register a resource replacement path, using code like this:

var fileuri = Services.io.newFileURI(file);
Services.io.getProtocolHandler("resource").
              QueryInterface(Ci.nsIResProtocolHandler).
              setSubstitution("my-cool-addon", fileuri);
  var worker = new Worker("resource://my-cool-addon/worker.js");

More references:

Using XPCOM from chrome workers

{{ obsolete_header("8.0") }}

Note: Support for using XPCOM and XPConnect from ChromeWorkers 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

Revision Source

<p>{{ gecko_minversion_header("2") }}</p>

<p>{{ non-standard_header() }}</p>

<p>{{ APIRef("Web Workers") }}</p>

<h2 id="Summary">Summary</h2>

<p>If you're developing privileged code, and would like to create a worker that can use <a href="/en/js-ctypes" title="en/js-ctypes">js-ctypes</a> to perform calls to native code, you can do so by using <code>ChromeWorker</code> instead of the standard {{ domxref("Worker") }} object. It works exactly like a standard {{ domxref("Worker") }}, except that it has access to <a href="/en/js-ctypes" title="en/js-ctypes">js-ctypes</a> via a global <code>ctypes</code> object available in the global scope of the worker.</p>

<p>Addons must use absolute URLs to load their workers, and that those URLs have to be using a <code>chrome://</code> or <code>resource://</code> protocol (<code>file://</code> is not accepted.) Addons that wish to use <code>file://</code> URLs must first register a resource replacement path, using code like this:</p>

<pre class="brush: js">
var fileuri = Services.io.newFileURI(file);
Services.io.getProtocolHandler("resource").
              QueryInterface(Ci.nsIResProtocolHandler).
              setSubstitution("my-cool-addon", fileuri);
  var worker = new Worker("resource://my-cool-addon/worker.js");</pre>

<p>More references:</p>

<ul>
 <li>You can use <code>ChromeWorker</code> from <a href="/en/JavaScript_code_modules" title="en/JavaScript code modules">JavaScript code modules</a>. See <a href="/en/JavaScript_code_modules/Using_workers_in_JavaScript_code_modules" title="en/JavaScript code modules/Using workers in JavaScript code modules">Using workers in JavaScript code modules</a> for details.</li>
 <li>You can use <a href="https://developer.mozilla.org/en-US/docs/Mozilla/ChromeWorkers/Chrome_Worker_Modules">ChromeWorker modules</a> in ChromeWorkers.</li>
 <li>See <a class="internal" href="/En/Using_web_workers" title="en/Using DOM workers">Using web workers</a> for examples and details.</li>
</ul>

<h2 id="Using_XPCOM_from_chrome_workers">Using XPCOM from chrome workers</h2>

<p>{{ obsolete_header("8.0") }}</p>

<div class="note">
<p><strong>Note:</strong> Support for using XPCOM and XPConnect from <code>ChromeWorker</code>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.</p>
</div>

<p>Prior to Gecko 8.0 {{ geckoRelease("8.0") }}, you could use the {{ domxref("Worker") }} <code>postMessage()</code> method to send <a href="/en/XPConnect_wrappers#XPCWrappedNative" title="en/XPConnect wrappers#XPCWrappedNative"><code>XPCWrappedNative</code></a> objects to chrome workers, as long as the underlying object is marked with the <code>nsIClassInfo.THREADSAFE</code> flag, which indicates that the object is safe to access from multiple threads at once.</p>

<p>In addition, <code>ChromeWorker</code> objects have access to a new global XPCOM&nbsp;object -- which is only available to chrome workers -- which has two methods:</p>

<dl>
 <dt><code>createInstance()</code></dt>
 <dd>Creates an instance of an XPCOM&nbsp;interface. Accepts a single parameter: the contract ID of the interface to create an instance of.</dd>
 <dt><code>getService()</code></dt>
 <dd>Returns the shared global service object for the specified XPCOM&nbsp;interface. Accepts a single parameter: the contract ID of the interface to which you wish to obtain a reference.</dd>
</dl>

<h3 id="Using_the_XPCOM_object">Using the XPCOM&nbsp;object</h3>

<p>Before using the XPCOM&nbsp;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.</p>

<h4 id="Creating_instances_of_components">Creating instances of components</h4>

<p>If your worker wants to create a thread pool in which it can create additional threads, you can do this:</p>

<pre>
let myThreadPool = XPCOM.createInstance("@mozilla.org/thread-pool;1");
myThreadPool.threadLimit = 6;
/* send some events with the pool */
myThreadPool.shutdown();</pre>

<h4 id="Passing_objects_to_chrome_workers">Passing objects to chrome workers</h4>

<p>Thread safe objects can be passed to chrome workers using the <code>postMessage()</code>&nbsp;method. Attempting to pass a non-thread safe object will throw an exception.</p>

<pre>
/* 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); </pre>

<h2 id="See_also">See also</h2>

<ul>
 <li><a class="internal" href="/En/Using_web_workers" title="en/Using DOM workers">Using web workers</a></li>
 <li><a href="/en/JavaScript_code_modules/Using_workers_in_JavaScript_code_modules" title="en/JavaScript code modules/Using workers in JavaScript code modules">Using workers in JavaScript code modules</a></li>
 <li>{{ domxref("Worker") }}</li>
 <li><code><a class="internal" href="/En/DOM/SharedWorker" title="en/DOM/SharedWorker">SharedWorker</a></code></li>
 <li><a class="external" href="https://www.whatwg.org/specs/web-workers/current-work/" title="https://www.whatwg.org/specs/web-workers/current-work/">Web Workers specification</a></li>
 <li>{{ domxref("WorkerGlobalScope") }}</li>
 <li><a href="https://github.com/Noitidart/ChromeWorker">GitHub :: ChromeWorker</a> - A fully working demo addon using js-ctypes from a chrome worker. Uses WinAPI example.</li>
 <li><a href="https://github.com/Noitidart/PromiseWorker">GitHub :: PromiseWorker</a> - Shows how to ues promises as an twist on postMessage feature of ChromeWorker</li>
</ul>
Revert to this revision