Backed out changeset 93928936b1ab bug 382690 due to check-in during string freeze.
[mozilla-central.git] / xpinstall / src / nsJSInstallTriggerGlobal.cpp
blob5dc6f1c6f4270ac5c5e94b7ff6d9320cbcf40f1e
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Dave Townsend <dtownsend@oxymoronical.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "jsapi.h"
40 #include "nscore.h"
41 #include "nsAutoPtr.h"
42 #include "nsIScriptContext.h"
43 #include "nsIScriptObjectOwner.h"
44 #include "nsIScriptGlobalObject.h"
45 #include "nsCRT.h"
46 #include "nsString.h"
47 #include "nsIDOMInstallTriggerGlobal.h"
48 #include "nsPIDOMWindow.h"
49 #include "nsIDOMDocument.h"
50 #include "nsIDocument.h"
51 #include "nsIDocShell.h"
52 #include "nsIObserverService.h"
53 #include "nsInstallTrigger.h"
54 #include "nsXPITriggerInfo.h"
55 #include "nsDOMJSUtils.h"
56 #include "nsXPIInstallInfo.h"
58 #include "nsIComponentManager.h"
59 #include "nsNetUtil.h"
60 #include "nsIScriptSecurityManager.h"
62 #include "nsSoftwareUpdateIIDs.h"
64 void ConvertJSValToStr(nsString& aString,
65 JSContext* aContext,
66 jsval aValue)
68 JSString *jsstring;
70 if ( !JSVAL_IS_NULL(aValue) &&
71 (jsstring = JS_ValueToString(aContext, aValue)) != nsnull)
73 aString.Assign(reinterpret_cast<const PRUnichar*>(JS_GetStringChars(jsstring)));
75 else
77 aString.Truncate();
81 PR_STATIC_CALLBACK(void)
82 FinalizeInstallTriggerGlobal(JSContext *cx, JSObject *obj);
84 /***********************************************************************/
86 // class for InstallTriggerGlobal
88 JSClass InstallTriggerGlobalClass = {
89 "InstallTrigger",
90 JSCLASS_HAS_PRIVATE,
91 JS_PropertyStub,
92 JS_PropertyStub,
93 JS_PropertyStub,
94 JS_PropertyStub,
95 JS_EnumerateStub,
96 JS_ResolveStub,
97 JS_ConvertStub,
98 FinalizeInstallTriggerGlobal
103 // InstallTriggerGlobal finalizer
105 static void
106 FinalizeInstallTriggerGlobal(JSContext *cx, JSObject *obj)
108 nsISupports *nativeThis = (nsISupports*)JS_GetPrivate(cx, obj);
110 if (nsnull != nativeThis) {
111 // get the js object
112 nsIScriptObjectOwner *owner = nsnull;
113 if (NS_OK == nativeThis->QueryInterface(NS_GET_IID(nsIScriptObjectOwner),
114 (void**)&owner)) {
115 owner->SetScriptObject(nsnull);
116 NS_RELEASE(owner);
119 // The addref was part of JSObject construction
120 NS_RELEASE(nativeThis);
124 static JSBool CreateNativeObject(JSContext *cx, JSObject *obj, nsIDOMInstallTriggerGlobal **aResult)
126 nsresult result;
127 nsIScriptObjectOwner *owner = nsnull;
128 nsIDOMInstallTriggerGlobal *nativeThis;
130 static NS_DEFINE_CID(kInstallTrigger_CID,
131 NS_SoftwareUpdateInstallTrigger_CID);
133 result = CallCreateInstance(kInstallTrigger_CID, &nativeThis);
134 if (NS_FAILED(result)) return JS_FALSE;
136 result = nativeThis->QueryInterface(NS_GET_IID(nsIScriptObjectOwner),
137 (void **)&owner);
139 if (NS_OK != result)
141 NS_RELEASE(nativeThis);
142 return JS_FALSE;
145 owner->SetScriptObject((void *)obj);
146 JS_SetPrivate(cx, obj, nativeThis);
148 *aResult = nativeThis;
150 NS_RELEASE(nativeThis); // we only want one refcnt. JSUtils cleans us up.
151 return JS_TRUE;
155 // Helper function for URI verification
157 static nsresult
158 InstallTriggerCheckLoadURIFromScript(JSContext *cx, const nsAString& uriStr)
160 nsresult rv;
161 nsCOMPtr<nsIScriptSecurityManager> secman(
162 do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID,&rv));
163 NS_ENSURE_SUCCESS(rv, rv);
165 // get the script principal
166 nsCOMPtr<nsIPrincipal> principal;
167 rv = secman->GetSubjectPrincipal(getter_AddRefs(principal));
168 NS_ENSURE_SUCCESS(rv, rv);
169 if (!principal)
170 return NS_ERROR_FAILURE;
172 // convert the requested URL string to a URI
173 // Note that we use a null base URI here, since that's what we use when we
174 // actually convert the string into a URI to load.
175 nsCOMPtr<nsIURI> uri;
176 rv = NS_NewURI(getter_AddRefs(uri), uriStr);
177 NS_ENSURE_SUCCESS(rv, rv);
179 // are we allowed to load this one?
180 rv = secman->CheckLoadURIWithPrincipal(principal, uri,
181 nsIScriptSecurityManager::DISALLOW_INHERIT_PRINCIPAL);
182 return rv;
186 // Helper function to get native object
188 // This is our own version of JS_GetInstancePrivate() that in addition
189 // performs the delayed creation of the native InstallTrigger if necessary
191 static nsIDOMInstallTriggerGlobal* getTriggerNative(JSContext *cx, JSObject *obj)
193 if (!JS_InstanceOf(cx, obj, &InstallTriggerGlobalClass, nsnull))
194 return nsnull;
196 nsIDOMInstallTriggerGlobal *native = (nsIDOMInstallTriggerGlobal*)JS_GetPrivate(cx, obj);
197 if (!native) {
198 // xpinstall script contexts delay creation of the native.
199 CreateNativeObject(cx, obj, &native);
201 return native;
205 // Native method UpdateEnabled
207 static JSBool
208 InstallTriggerGlobalUpdateEnabled(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
210 nsIDOMInstallTriggerGlobal *nativeThis = getTriggerNative(cx, obj);
211 if (!nativeThis)
212 return JS_FALSE;
214 *rval = JSVAL_FALSE;
216 nsIScriptGlobalObject *globalObject = nsnull;
217 nsIScriptContext *scriptContext = GetScriptContextFromJSContext(cx);
218 if (scriptContext)
219 globalObject = scriptContext->GetGlobalObject();
221 PRBool nativeRet = PR_FALSE;
222 if (globalObject)
223 nativeThis->UpdateEnabled(globalObject, XPI_GLOBAL, &nativeRet);
225 *rval = BOOLEAN_TO_JSVAL(nativeRet);
226 return JS_TRUE;
231 // Native method Install
233 static JSBool
234 InstallTriggerGlobalInstall(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
236 nsIDOMInstallTriggerGlobal *nativeThis = getTriggerNative(cx, obj);
237 if (!nativeThis)
238 return JS_FALSE;
240 *rval = JSVAL_FALSE;
242 // make sure XPInstall is enabled, return false if not
243 nsIScriptGlobalObject *globalObject = nsnull;
244 nsIScriptContext *scriptContext = GetScriptContextFromJSContext(cx);
245 if (scriptContext)
246 globalObject = scriptContext->GetGlobalObject();
248 if (!globalObject)
249 return JS_TRUE;
251 nsCOMPtr<nsIScriptSecurityManager> secman(do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID));
252 if (!secman)
254 JS_ReportError(cx, "Could not the script security manager service.");
255 return JS_FALSE;
257 // get the principal. if it doesn't exist, die.
258 nsCOMPtr<nsIPrincipal> principal;
259 secman->GetSubjectPrincipal(getter_AddRefs(principal));
260 if (!principal)
262 JS_ReportError(cx, "Could not get the Subject Principal during InstallTrigger.Install()");
263 return JS_FALSE;
266 // get window.location to construct relative URLs
267 nsCOMPtr<nsIURI> baseURL;
268 JSObject* global = JS_GetGlobalObject(cx);
269 if (global)
271 jsval v;
272 if (JS_GetProperty(cx,global,"location",&v))
274 nsAutoString location;
275 ConvertJSValToStr( location, cx, v );
276 NS_NewURI(getter_AddRefs(baseURL), location);
280 PRBool abortLoad = PR_FALSE;
282 // parse associative array of installs
283 if ( argc >= 1 && JSVAL_IS_OBJECT(argv[0]) && JSVAL_TO_OBJECT(argv[0]) )
285 nsXPITriggerInfo *trigger = new nsXPITriggerInfo();
286 if (!trigger)
287 return JS_FALSE;
289 trigger->SetPrincipal(principal);
291 JSIdArray *ida = JS_Enumerate( cx, JSVAL_TO_OBJECT(argv[0]) );
292 if ( ida )
294 jsval v;
295 const PRUnichar *name, *URL;
296 const PRUnichar *iconURL = nsnull;
297 const char *hash;
299 for (int i = 0; i < ida->length && !abortLoad; i++ )
301 JS_IdToValue( cx, ida->vector[i], &v );
302 JSString * str = JS_ValueToString( cx, v );
303 if (!str)
305 abortLoad = PR_TRUE;
306 break;
309 name = reinterpret_cast<const PRUnichar*>(JS_GetStringChars( str ));
311 URL = iconURL = nsnull;
312 hash = nsnull;
313 JS_GetUCProperty( cx, JSVAL_TO_OBJECT(argv[0]), reinterpret_cast<const jschar*>(name), nsCRT::strlen(name), &v );
314 if ( JSVAL_IS_OBJECT(v) && JSVAL_TO_OBJECT(v) )
316 jsval v2;
317 if (JS_GetProperty( cx, JSVAL_TO_OBJECT(v), "URL", &v2 ) && !JSVAL_IS_VOID(v2))
318 URL = reinterpret_cast<const PRUnichar*>(JS_GetStringChars( JS_ValueToString( cx, v2 ) ));
320 if (JS_GetProperty( cx, JSVAL_TO_OBJECT(v), "IconURL", &v2 ) && !JSVAL_IS_VOID(v2))
321 iconURL = reinterpret_cast<const PRUnichar*>(JS_GetStringChars( JS_ValueToString( cx, v2 ) ));
323 if (JS_GetProperty( cx, JSVAL_TO_OBJECT(v), "Hash", &v2) && !JSVAL_IS_VOID(v2))
324 hash = reinterpret_cast<const char*>(JS_GetStringBytes( JS_ValueToString( cx, v2 ) ));
326 else
328 URL = reinterpret_cast<const PRUnichar*>(JS_GetStringChars( JS_ValueToString( cx, v ) ));
331 if ( URL )
333 // Get relative URL to load
334 nsAutoString xpiURL(URL);
335 if (baseURL)
337 nsCAutoString resolvedURL;
338 baseURL->Resolve(NS_ConvertUTF16toUTF8(xpiURL), resolvedURL);
339 xpiURL = NS_ConvertUTF8toUTF16(resolvedURL);
342 nsAutoString icon(iconURL);
343 if (iconURL && baseURL)
345 nsCAutoString resolvedIcon;
346 baseURL->Resolve(NS_ConvertUTF16toUTF8(icon), resolvedIcon);
347 icon = NS_ConvertUTF8toUTF16(resolvedIcon);
350 // Make sure we're allowed to load this URL and the icon URL
351 nsresult rv = InstallTriggerCheckLoadURIFromScript(cx, xpiURL);
352 if (NS_FAILED(rv))
353 abortLoad = PR_TRUE;
355 if (!abortLoad && iconURL)
357 rv = InstallTriggerCheckLoadURIFromScript(cx, icon);
358 if (NS_FAILED(rv))
359 abortLoad = PR_TRUE;
362 if (!abortLoad)
364 // Add the install item to the trigger collection
365 nsXPITriggerItem *item =
366 new nsXPITriggerItem( name, xpiURL.get(), icon.get(), hash );
367 if ( item )
369 trigger->Add( item );
371 else
372 abortLoad = PR_TRUE;
375 else
376 abortLoad = PR_TRUE;
378 JS_DestroyIdArray( cx, ida );
382 // pass on only if good stuff found
383 if (!abortLoad && trigger->Size() > 0)
385 nsCOMPtr<nsIURI> checkuri;
386 nsresult rv = nativeThis->GetOriginatingURI(globalObject,
387 getter_AddRefs(checkuri));
388 if (NS_SUCCEEDED(rv))
390 nsCOMPtr<nsIDOMWindowInternal> win(do_QueryInterface(globalObject));
391 nsCOMPtr<nsIXPIInstallInfo> installInfo =
392 new nsXPIInstallInfo(win, checkuri, trigger, 0);
393 if (installInfo)
395 // installInfo now owns triggers
396 PRBool enabled = PR_FALSE;
397 nativeThis->UpdateEnabled(checkuri, XPI_WHITELIST, &enabled);
398 if (!enabled)
400 nsCOMPtr<nsIObserverService> os(do_GetService("@mozilla.org/observer-service;1"));
401 if (os)
402 os->NotifyObservers(installInfo,
403 "xpinstall-install-blocked",
404 nsnull);
406 else
408 // save callback function if any (ignore bad args for now)
409 if ( argc >= 2 && JS_TypeOfValue(cx,argv[1]) == JSTYPE_FUNCTION )
411 trigger->SaveCallback( cx, argv[1] );
414 PRBool result;
415 nativeThis->StartInstall(installInfo, &result);
416 *rval = BOOLEAN_TO_JSVAL(result);
418 return JS_TRUE;
422 // didn't pass it on so we must delete trigger
423 delete trigger;
426 JS_ReportError(cx, "Incorrect arguments to InstallTrigger.Install()");
427 return JS_FALSE;
432 // Native method InstallChrome
434 static JSBool
435 InstallTriggerGlobalInstallChrome(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
437 nsIDOMInstallTriggerGlobal *nativeThis = getTriggerNative(cx, obj);
438 if (!nativeThis)
439 return JS_FALSE;
441 uint32 chromeType = NOT_CHROME;
442 nsAutoString sourceURL;
443 nsAutoString name;
445 *rval = JSVAL_FALSE;
447 // get chromeType first, the update enabled check for skins skips whitelisting
448 if (argc >=1)
449 JS_ValueToECMAUint32(cx, argv[0], &chromeType);
451 // make sure XPInstall is enabled, return if not
452 nsIScriptGlobalObject *globalObject = nsnull;
453 nsIScriptContext *scriptContext = GetScriptContextFromJSContext(cx);
454 if (scriptContext)
455 globalObject = scriptContext->GetGlobalObject();
457 if (!globalObject)
458 return JS_TRUE;
460 // get window.location to construct relative URLs
461 nsCOMPtr<nsIURI> baseURL;
462 JSObject* global = JS_GetGlobalObject(cx);
463 if (global)
465 jsval v;
466 if (JS_GetProperty(cx,global,"location",&v))
468 nsAutoString location;
469 ConvertJSValToStr( location, cx, v );
470 NS_NewURI(getter_AddRefs(baseURL), location);
475 if ( argc >= 3 )
477 ConvertJSValToStr(sourceURL, cx, argv[1]);
478 ConvertJSValToStr(name, cx, argv[2]);
480 if (baseURL)
482 nsCAutoString resolvedURL;
483 baseURL->Resolve(NS_ConvertUTF16toUTF8(sourceURL), resolvedURL);
484 sourceURL = NS_ConvertUTF8toUTF16(resolvedURL);
487 // Make sure caller is allowed to load this url.
488 nsresult rv = InstallTriggerCheckLoadURIFromScript(cx, sourceURL);
489 if (NS_FAILED(rv))
490 return JS_FALSE;
492 if ( chromeType & CHROME_ALL )
494 // there's at least one known chrome type
495 nsCOMPtr<nsIURI> checkuri;
496 nsresult rv = nativeThis->GetOriginatingURI(globalObject,
497 getter_AddRefs(checkuri));
498 if (NS_SUCCEEDED(rv))
500 nsAutoPtr<nsXPITriggerInfo> trigger(new nsXPITriggerInfo());
501 nsAutoPtr<nsXPITriggerItem> item(new nsXPITriggerItem(name.get(),
502 sourceURL.get(),
503 nsnull));
504 if (trigger && item)
506 // trigger will free item when complete
507 trigger->Add(item.forget());
508 nsCOMPtr<nsIDOMWindowInternal> win(do_QueryInterface(globalObject));
509 nsCOMPtr<nsIXPIInstallInfo> installInfo =
510 new nsXPIInstallInfo(win, checkuri, trigger, chromeType);
511 if (installInfo)
513 // installInfo owns trigger now
514 trigger.forget();
515 PRBool enabled = PR_FALSE;
516 nativeThis->UpdateEnabled(checkuri, XPI_WHITELIST,
517 &enabled);
518 if (!enabled)
520 nsCOMPtr<nsIObserverService> os(do_GetService("@mozilla.org/observer-service;1"));
521 if (os)
522 os->NotifyObservers(installInfo,
523 "xpinstall-install-blocked",
524 nsnull);
526 else
528 PRBool nativeRet = PR_FALSE;
529 nativeThis->StartInstall(installInfo, &nativeRet);
530 *rval = BOOLEAN_TO_JSVAL(nativeRet);
537 return JS_TRUE;
542 // Native method StartSoftwareUpdate
544 static JSBool
545 InstallTriggerGlobalStartSoftwareUpdate(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
547 nsIDOMInstallTriggerGlobal *nativeThis = getTriggerNative(cx, obj);
548 if (!nativeThis)
549 return JS_FALSE;
551 PRBool nativeRet;
552 PRInt32 flags = 0;
554 *rval = JSVAL_FALSE;
556 nsIScriptGlobalObject *globalObject = nsnull;
557 nsIScriptContext *scriptContext = GetScriptContextFromJSContext(cx);
558 if (scriptContext)
559 globalObject = scriptContext->GetGlobalObject();
561 if (!globalObject)
562 return JS_TRUE;
564 // get window.location to construct relative URLs
565 nsCOMPtr<nsIURI> baseURL;
566 JSObject* global = JS_GetGlobalObject(cx);
567 if (global)
569 jsval v;
570 if (JS_GetProperty(cx,global,"location",&v))
572 nsAutoString location;
573 ConvertJSValToStr( location, cx, v );
574 NS_NewURI(getter_AddRefs(baseURL), location);
579 if ( argc >= 1 )
581 nsAutoString xpiURL;
582 ConvertJSValToStr(xpiURL, cx, argv[0]);
583 if (baseURL)
585 nsCAutoString resolvedURL;
586 baseURL->Resolve(NS_ConvertUTF16toUTF8(xpiURL), resolvedURL);
587 xpiURL = NS_ConvertUTF8toUTF16(resolvedURL);
590 // Make sure caller is allowed to load this url.
591 nsresult rv = InstallTriggerCheckLoadURIFromScript(cx, xpiURL);
592 if (NS_FAILED(rv))
593 return JS_FALSE;
595 if (argc >= 2 && !JS_ValueToInt32(cx, argv[1], (int32 *)&flags))
597 JS_ReportError(cx, "StartSoftwareUpdate() 2nd parameter must be a number");
598 return JS_FALSE;
601 nsCOMPtr<nsIURI> checkuri;
602 rv = nativeThis->GetOriginatingURI(globalObject, getter_AddRefs(checkuri));
603 if (NS_SUCCEEDED(rv))
605 nsAutoPtr<nsXPITriggerInfo> trigger(new nsXPITriggerInfo());
606 nsAutoPtr<nsXPITriggerItem> item(new nsXPITriggerItem(0,
607 xpiURL.get(),
608 nsnull));
609 if (trigger && item)
611 // trigger will free item when complete
612 trigger->Add(item.forget());
613 nsCOMPtr<nsIDOMWindowInternal> win(do_QueryInterface(globalObject));
614 nsCOMPtr<nsIXPIInstallInfo> installInfo =
615 new nsXPIInstallInfo(win, checkuri, trigger, 0);
616 if (installInfo)
618 // From here trigger is owned by installInfo until passed on to nsXPInstallManager
619 trigger.forget();
620 PRBool enabled = PR_FALSE;
621 nativeThis->UpdateEnabled(checkuri, XPI_WHITELIST, &enabled);
622 if (!enabled)
624 nsCOMPtr<nsIObserverService> os(do_GetService("@mozilla.org/observer-service;1"));
625 if (os)
626 os->NotifyObservers(installInfo,
627 "xpinstall-install-blocked",
628 nsnull);
630 else
632 nativeThis->StartInstall(installInfo, &nativeRet);
633 *rval = BOOLEAN_TO_JSVAL(nativeRet);
639 else
641 JS_ReportError(cx, "Function StartSoftwareUpdate requires 1 parameters");
642 return JS_FALSE;
645 return JS_TRUE;
650 // InstallTriggerGlobal class methods
652 static JSFunctionSpec InstallTriggerGlobalMethods[] =
654 {"UpdateEnabled", InstallTriggerGlobalUpdateEnabled, 0,0,0},
655 {"StartSoftwareUpdate", InstallTriggerGlobalStartSoftwareUpdate, 2,0,0},
656 {"updateEnabled", InstallTriggerGlobalUpdateEnabled, 0,0,0},
657 {"enabled", InstallTriggerGlobalUpdateEnabled, 0,0,0},
658 {"install", InstallTriggerGlobalInstall, 2,0,0},
659 {"installChrome", InstallTriggerGlobalInstallChrome, 2,0,0},
660 {"startSoftwareUpdate", InstallTriggerGlobalStartSoftwareUpdate, 2,0,0},
661 {nsnull,nsnull,0,0,0}
665 static JSConstDoubleSpec diff_constants[] =
667 { nsIDOMInstallTriggerGlobal::MAJOR_DIFF, "MAJOR_DIFF" },
668 { nsIDOMInstallTriggerGlobal::MINOR_DIFF, "MINOR_DIFF" },
669 { nsIDOMInstallTriggerGlobal::REL_DIFF, "REL_DIFF" },
670 { nsIDOMInstallTriggerGlobal::BLD_DIFF, "BLD_DIFF" },
671 { nsIDOMInstallTriggerGlobal::EQUAL, "EQUAL" },
672 { nsIDOMInstallTriggerGlobal::NOT_FOUND, "NOT_FOUND" },
673 { CHROME_SKIN, "SKIN" },
674 { CHROME_LOCALE, "LOCALE" },
675 { CHROME_CONTENT, "CONTENT" },
676 { CHROME_ALL, "PACKAGE" },
677 {0,nsnull}
682 nsresult InitInstallTriggerGlobalClass(JSContext *jscontext, JSObject *global, void** prototype)
684 JSObject *proto = nsnull;
686 if (prototype != nsnull)
687 *prototype = nsnull;
689 proto = JS_InitClass(jscontext, // context
690 global, // global object
691 nsnull, // parent proto
692 &InstallTriggerGlobalClass, // JSClass
693 nsnull, // JSNative ctor
694 nsnull, // ctor args
695 nsnull, // proto props
696 nsnull, // proto funcs
697 nsnull, // ctor props (static)
698 InstallTriggerGlobalMethods); // ctor funcs (static)
701 if (nsnull == proto) return NS_ERROR_FAILURE;
703 if ( PR_FALSE == JS_DefineConstDoubles(jscontext, proto, diff_constants) )
704 return NS_ERROR_FAILURE;
706 if (prototype != nsnull)
707 *prototype = proto;
709 return NS_OK;
715 // InstallTriggerGlobal class initialization
717 nsresult NS_InitInstallTriggerGlobalClass(nsIScriptContext *aContext, void **aPrototype)
719 JSContext *jscontext = (JSContext *)aContext->GetNativeContext();
720 JSObject *proto = nsnull;
721 JSObject *constructor = nsnull;
722 JSObject *global = JS_GetGlobalObject(jscontext);
723 jsval vp;
725 if ((PR_TRUE != JS_LookupProperty(jscontext, global, "InstallTriggerGlobal", &vp)) ||
726 !JSVAL_IS_OBJECT(vp) ||
727 ((constructor = JSVAL_TO_OBJECT(vp)) == nsnull) ||
728 (PR_TRUE != JS_LookupProperty(jscontext, JSVAL_TO_OBJECT(vp), "prototype", &vp)) ||
729 !JSVAL_IS_OBJECT(vp))
731 nsresult rv = InitInstallTriggerGlobalClass(jscontext, global, (void**)&proto);
732 if (NS_FAILED(rv)) return rv;
734 else if ((nsnull != constructor) && JSVAL_IS_OBJECT(vp))
736 proto = JSVAL_TO_OBJECT(vp);
738 else
740 return NS_ERROR_FAILURE;
743 if (aPrototype)
744 *aPrototype = proto;
746 return NS_OK;
751 // Method for creating a new InstallTriggerGlobal JavaScript object
753 nsresult
754 NS_NewScriptInstallTriggerGlobal(nsIScriptContext *aContext,
755 nsISupports *aSupports, nsISupports *aParent,
756 void **aReturn)
758 NS_PRECONDITION(nsnull != aContext && nsnull != aSupports &&
759 nsnull != aReturn,
760 "null argument to NS_NewScriptInstallTriggerGlobal");
762 JSObject *proto;
763 JSObject *parent = nsnull;
764 JSContext *jscontext = (JSContext *)aContext->GetNativeContext();
765 nsresult result = NS_OK;
766 nsIDOMInstallTriggerGlobal *installTriggerGlobal;
768 nsCOMPtr<nsIScriptObjectOwner> owner(do_QueryInterface(aParent));
770 if (owner) {
771 if (NS_OK != owner->GetScriptObject(aContext, (void **)&parent)) {
772 return NS_ERROR_FAILURE;
774 } else {
775 nsCOMPtr<nsIScriptGlobalObject> sgo(do_QueryInterface(aParent));
777 if (sgo) {
778 parent = sgo->GetGlobalJSObject();
779 } else {
780 return NS_ERROR_FAILURE;
784 if (NS_OK != NS_InitInstallTriggerGlobalClass(aContext, (void **)&proto)) {
785 return NS_ERROR_FAILURE;
788 result = CallQueryInterface(aSupports, &installTriggerGlobal);
789 if (NS_OK != result) {
790 return result;
793 // create a js object for this class
794 *aReturn = JS_NewObject(jscontext, &InstallTriggerGlobalClass, proto, parent);
795 if (nsnull != *aReturn) {
796 // connect the native object to the js object
797 JS_SetPrivate(jscontext, (JSObject *)*aReturn, installTriggerGlobal);
799 else {
800 NS_RELEASE(installTriggerGlobal);
801 return NS_ERROR_FAILURE;
804 return NS_OK;