bz: Note that this is a work in progress. Some rewriting may need to happen pending the outcome of . Please let me know if you plan to make any major changes to this document before doing so. --Bzbarsky 15:19, 31 May 2005 (PDT)
XPCNativeWrapper is a way to wrap up an object so that it's safe to access from privileged code. It can be used in all Firefox versions, though the
behavior changed somewhat starting with Firefox 1.1.
XPCNativeWrapper in Firefox versions prior to 1.1
In Firefox versions prior to 1.1, use of
XPCNativeWrapper requires manually constructing an
XPCNativeWrapper and passing it the object to be wrapped and the names of the methods/properties to be exposed as arguments. The resulting object exposes ONLY the methods/properties whose methods were passed as arguments. This is described in more detail in the the entry for
XPCNativeWrapper at the MozillaZine KnowledgeBase.
XPCNativeWrapper in Firefox versions starting with 1.1
bz: Three types, or two, or four? Still sorting this out. See https://bugzilla.mozilla.org/show_bug.cgi?id=295782
There are three slightly different types of
XPCNativeWrapper in Firefox 1.1. All three types wrap a possibly-unsafe object and provide safe access to all of its properties and methods (unlike
XPCNativeWrapper in versions before 1.1, which only provided safe access to the properties and methods listed in its constructor). If unsafe access to a property is required for some reason, this can be accomplished via the
wrappedJSObject property of the wrapper. For example, if
docWrapper is a wrapper for
is the same as
XPCNativeWrapper with string arguments
var contentWinWrapper = new XPCNativeWrapper(content, "document");
This syntax has been kept for compatibility with versions prior to Firefox 1.1. While all properties of the
contentWinWrapper object can now be safely accessed, the return values of these properties are NOT safe to access (just like in versions prior to Firefox 1.1). So to compare the content document title to the current content selection, one must do:
var winWrapper = new XPCNativeWrapper(content, "document", "getSelection()"); var docWrapper = new XPCNativeWrapper(winWrapper.document, "title"); return docWrapper.title == winWrapper.getSelection();
Note that the
"getSelection()" argument is not strictly needed here; if the code is not intended for use with Firefox versions before 1.1 it can be removed. A single string argument after the object being wrapped is all that is required for Firefox 1.1.
XPCNativeWrapper with no string arguments
var contentWinWrapper = new XPCNativeWrapper(content);
The resulting object behaves just like an implicit wrapper (most importantly, it will be deep), except that properties set on it will not be visible on either other explicit wrappers or implicit wrappers. Note that this means that for code not using
xpcnativewrappers=yes this type of
XPCNativeWrapper is useless at the moment.
bz: Again, see https://bugzilla.mozilla.org/show_bug.cgi?id=295782 -- that might introduce more differences between this type of wrapper and implicit wrappers.
This type of wrapper can be created in one of two ways: either by accessing a content object from code that has
xpcnativewrappers=yes set or by accessing a property of a deep wrapper.
bz: The latter may not always be true pending outcome of https://bugzilla.mozilla.org/show_bug.cgi?id=295782
The wrapper created in this way is always a deep wrapper. Furthermore, a wrapper created in this way will stick around as long as the object being wrapped does.
bz: except for https://bugzilla.mozilla.org/show_bug.cgi?id=295937 -- sort that out before trying to explain the exact behavior here.
If a wrapper is deep, that means that the return value of any property access or method call on the wrapper will be implicitly wrapped.
If a deep wrapper is accessed from code that doesn't use
xpcnativewrappers=yes, it will NOT provide safe access to properties. Instead, it will automatically forward all accesses to the underlying wrappedJSObject.
bz: This may not be the case for all deep wrappers pending https://bugzilla.mozilla.org/show_bug.cgi?id=295782
Properties that cannot be accessed with
There are some commonly used properties that cannot be used with
- Assigning to the
locationproperty of a wrapped document or window will not work. Code that wishes to set the location should assign to the
hrefproperty of the location object (after wrapping the location object itself, as needed).
document.open()(with no arguments) will not work. Code that wishes to do this should call