~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/toolkit/content/XPCNativeWrapper.js

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ***** BEGIN LICENSE BLOCK *****
 
2
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
3
 *
 
4
 * The contents of this file are subject to the Mozilla Public License Version
 
5
 * 1.1 (the "License"); you may not use this file except in compliance with
 
6
 * the License. You may obtain a copy of the License at
 
7
 * http://www.mozilla.org/MPL/
 
8
 *
 
9
 * Software distributed under the License is distributed on an "AS IS" basis,
 
10
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
11
 * for the specific language governing rights and limitations under the
 
12
 * License.
 
13
 *
 
14
 * The Original Code is XPConnect Native Wrapper.
 
15
 *
 
16
 * The Initial Developer of the Original Code is
 
17
 * Christopher A. Aillon <christopher@aillon.com>.
 
18
 * Portions created by the Initial Developer are Copyright (C) 2002
 
19
 * the Initial Developer. All Rights Reserved.
 
20
 *
 
21
 * Contributor(s):
 
22
 *   Christopher A. Aillon <christopher@aillon.com>
 
23
 *
 
24
 * Alternatively, the contents of this file may be used under the terms of
 
25
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
26
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
27
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
28
 * of those above. If you wish to allow use of your version of this file only
 
29
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
30
 * use your version of this file under the terms of the MPL, indicate your
 
31
 * decision by deleting the provisions above and replace them with the notice
 
32
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
33
 * the provisions above, a recipient may use your version of this file under
 
34
 * the terms of any one of the MPL, the GPL or the LGPL.
 
35
 *
 
36
 * ***** END LICENSE BLOCK ***** */
 
37
 
 
38
/**
 
39
 * Get a wrapper to certain XPConnect wrapped native properties of an
 
40
 * untrusted object.
 
41
 *
 
42
 * Usage:
 
43
 *   var wrapper = new XPCNativeWrapper(untrusted, 'title', 'getElementById()');
 
44
 *   var title = wrapper.title;
 
45
 *   var elem  = wrapper.getElementById('fooId');
 
46
 *
 
47
 * @param aUntrustedObject the untrusted object we want native wrappers for
 
48
 * @param arg1...argN strings of the form 'property' or 'method()' which
 
49
 *                    denote the property or method names we wish to wrap
 
50
 * @throws NS_ERROR_XPC_BAD_CONVERT_JS if a property or method requested
 
51
 *                                     does not have a native wrapper on
 
52
 *                                     the untrusted object.
 
53
 * @see nsXPCComponents::LookupMethod()
 
54
 */
 
55
function XPCNativeWrapper(aUntrustedObject)
 
56
{
 
57
  this.mUntrustedObject = aUntrustedObject;
 
58
 
 
59
  for (var i = arguments.length - 1; i > 0; --i)
 
60
    this.importXPCNative(arguments[i]);
 
61
}
 
62
 
 
63
XPCNativeWrapper.prototype =
 
64
{
 
65
  importXPCNative:
 
66
    function(aName)
 
67
    {
 
68
      // If we are passed a string like "foo()", ary[2] will be of type string,
 
69
      // and contain "()".  If passed "foo", then it will be of type undefined.
 
70
      if (aName.slice(-2) == "()")
 
71
        this._doImportMethod(aName.slice(0, -2));
 
72
      else
 
73
        this._doImportProperty(aName);
 
74
    },
 
75
 
 
76
  _doImportMethod:
 
77
    function(aMethodName)
 
78
    {
 
79
      var nativeMethod = Components.lookupMethod(this.mUntrustedObject,
 
80
                                                 aMethodName);
 
81
      this[aMethodName] = function()
 
82
      {
 
83
        return nativeMethod.apply(this.mUntrustedObject, arguments);
 
84
      };
 
85
    },
 
86
 
 
87
  _doImportProperty:
 
88
    function(aPropName)
 
89
    {
 
90
      var nativeMethod = Components.lookupMethod(this.mUntrustedObject,
 
91
                                                 aPropName);
 
92
      var theGetter = function()
 
93
      {
 
94
        return nativeMethod.call(this.mUntrustedObject);
 
95
      };
 
96
      var theSetter = function(val)
 
97
      {
 
98
        return nativeMethod.call(this.mUntrustedObject, val);
 
99
      };
 
100
 
 
101
      this.__defineGetter__(aPropName, theGetter);
 
102
      this.__defineSetter__(aPropName, theSetter);
 
103
    }
 
104
};