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 1050594 of ChromeWorker

  • Revision slug: Web/API/ChromeWorker
  • Revision title: ChromeWorker
  • Revision id: 1050594
  • Created:
  • Creator: kmaglione
  • Is current revision? Yes
  • Comment Remove no-longer-relevant XPCOM content

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. Examples of ChromeWorker's using js-ctypes are availabe on Github and are linked to from the See Also section below. To use a postMessage with callback version of ChromeWorker that features promises, see PromiseWorker.

Addons must use absolute URLs to load their workers, and 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:

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. Examples of ChromeWorker's using js-ctypes are availabe on Github and are linked to from the <a href="#seealso">See Also section below</a>. To use a postMessage with callback version of ChromeWorker that features promises, see <a href="/en-US/docs/Web/API/PromiseWorker">PromiseWorker</a>.</p>

<p>Addons must use absolute URLs to load their workers, and 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="See_Also">See Also<a id="seealso" name="seealso"></a></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="/en-US/docs/Web/API/PromiseWorker">PromiseWorker</a>
  <ul>
   <li><a href="https://github.com/Noitidart/PromiseWorker">GitHub :: PromiseWorker</a> - Shows how to uses promises as an twist on postMessage feature of ChromeWorker</li>
  </ul>
 </li>
</ul>
Revert to this revision