Personal tools

Electrolysis/CPOW

From MozillaWiki

Jump to: navigation, search

The Cross-Process Object Wrapper (CPOW) is a proxy object which makes a JS object in one process appear to be just like a JS object in another process.

Progress on this wrapper is tracked in bug 516522.

This wrapper's implementation landed in changeset e483a5868df5 on March 23, 2010. Its test suite landed separately, in changeset e75320dab85b.

CPOWs were once called JPWs, for Javascript inter-Process Wrapper.

Basic Usage

The CPOW is a one-way wrapper so that extensions and other chrome script can make synchronous calls to get information from content script. The content process cannot hold or call any object in the chrome process.

The initial wrapped object will always be the window object in the content process. Currently the method for obtaining this object is more convoluted than we might like:

   var browser = document.getElementsByTagName("browser").item(0),
       Ci = Components.interfaces,
       frameLoader = browser.QueryInterface(Ci.nsIFrameLoaderOwner),
       cpow = frameLoader.crossProcessObjectWrapper;

In the future (TODO) the CPOW should be transparently available as element.contentWindow for DOM elements with the attribute remote="true" (in particular, <browser remote="true">).

Property accesses and function calls on the CPOW may return plain data (string/number/boolean/undefined) or a reference to another content object.

Function parameters passed to the CPOW must be string/number/boolean/undefined or a reference to a content object in the child process.

If an object of an unknown type is passed to a CPOW function, CPOW will throw an exception; it will not attempt to automatically convert the object to a known type.

If the parent process needs to create a new content object, it may do so by obtaining a CPOW wrapping the child's Object constructor:

   var ChildObject = cpow.Object,
       newObject = new ChildObject;
   newObject.foo = 42;
   newObject.self = newObject;
   newObject.win = cpow;
   // and so on...

Reverse thinking

For some extensions, the dominant processing will be between the extension JS and content. Running that over CPOW will be slow. An alternative is to run the content-processing JS code in the content process and us something like CPOW in reverse for the operations the extension needs apply to the main process. -jjb

Note: This is a complicated problem where we have to pick acceptable limitations carefully. Further discussion needs to be example-driven.

How far can we get by simply exposing permanent chrome methods to content? These methods could take any kind of parameters, including object references. What use cases are impossible under this model?