XPConnect Chrome Object Wrappers: Difference between revisions

added Cu.exposeToContent() call on functions that need wrapping
(added more)
(added Cu.exposeToContent() call on functions that need wrapping)
Line 10: Line 10:


<pre class="brush:js;">
<pre class="brush:js;">
var sandbox = Components.utils.Sandbox("http://www.mozilla.org");
const Cu = Components.utils;
sandbox.foo = function foo(x) { /* ... */ };
 
var result = Components.utils.evalInSandbox("foo({bar: 5});");
var sandbox = Cu.Sandbox("http://www.mozilla.org");
sandbox.foo = Cu.exposeToContent(function foo(x) { /* ... */ });
var result = Cu.evalInSandbox("foo({bar: 5});");
</pre>
</pre>


In the above example, <tt>foo()</tt> is wrapped by a COW when accessed by sandboxed code executed via <tt>Components.utils.evalInSandbox()</tt>.  The object <tt>{bar: 5}</tt> is wrapped in an <tt>XPCSafeJSObjectWrapper</tt> before being passed into <tt>foo()</tt>, and whatever is returned from <tt>foo()</tt> and placed in <tt>result</tt> is usually wrapped in a COW as well.
In the above example, <tt>foo()</tt> is wrapped by a COW when accessed by sandboxed code executed via <tt>Components.utils.evalInSandbox()</tt>.  The object <tt>{bar: 5}</tt> is wrapped in an <tt>XPCSafeJSObjectWrapper</tt> before being passed into <tt>foo()</tt>, and whatever is returned from <tt>foo()</tt> and placed in <tt>result</tt> is usually wrapped in a COW as well.
874

edits