Back to index

lightning-sunbird  0.9+nobinonly
nsJSFile.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "jsapi.h"
00039 #include "nscore.h"
00040 #include "nsIScriptContext.h"
00041 
00042 #include "nsString.h"
00043 #include "nsInstall.h"
00044 
00045 #include "nsIDOMInstallVersion.h"
00046 
00047 #include <stdio.h>
00048 
00049 #ifdef _WINDOWS
00050 #include "nsJSWinReg.h"
00051 #include "nsJSWinProfile.h"
00052 
00053 extern JSClass WinRegClass;
00054 extern JSClass WinProfileClass;
00055 #endif
00056 
00057 #include "nsJSFileSpecObj.h"
00058 extern JSClass FileSpecObjectClass;
00059 
00060 
00061 /* externs from nsJSInstall.cpp */
00062 extern void ConvertJSValToStr(nsString&  aString,
00063                              JSContext* aContext,
00064                              jsval      aValue);
00065 
00066 extern void ConvertStrToJSVal(const nsString& aProp,
00067                              JSContext* aContext,
00068                              jsval* aReturn);
00069 
00070 extern PRBool ConvertJSValToBool(PRBool* aProp,
00071                                 JSContext* aContext,
00072                                 jsval aValue);
00073 
00074 extern JSObject *gFileSpecProto;
00075 extern JSClass InstallClass;
00076 
00077 /***********************************************************************/
00078 //
00079 // class for FileOp operations
00080 //
00081 JSClass FileOpClass = {
00082   "File", 
00083   JSCLASS_HAS_PRIVATE,
00084   JS_PropertyStub,
00085   JS_PropertyStub,
00086   JS_PropertyStub,
00087   JS_PropertyStub,
00088   JS_EnumerateStub,
00089   JS_ResolveStub,
00090   JS_ConvertStub,
00091   JS_FinalizeStub
00092 };
00093 
00094 static nsInstall *
00095 GetNativeThis(JSContext *cx, JSObject *obj, jsval *argv)
00096 {
00097   // Pass null for argv in the first test to avoid spurious error
00098   // reports when called against the File object.  The Install-global
00099   // methods are considered deprecated in favour of the File-scoped ones,
00100   // so if we get neither here we'll mention File in the report to point
00101   // authors at the new way.
00102   if (JS_InstanceOf(cx, obj, &InstallClass, nsnull) ||
00103       JS_InstanceOf(cx, obj, &FileOpClass, argv)) {
00104     return (nsInstall *)JS_GetPrivate(cx, obj);
00105   }
00106   return nsnull;
00107 }
00108 
00109 //
00110 // Native method DirCreate
00111 //
00112 JSBool PR_CALLBACK
00113 InstallFileOpDirCreate(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00114 {
00115   nsInstall *nativeThis =
00116     GetNativeThis(cx, obj, argv);
00117   if (!nativeThis)
00118     return JS_FALSE;
00119 
00120   PRInt32 nativeRet;
00121   JSObject *jsObj;
00122   nsInstallFolder *folder;
00123 
00124   *rval = INT_TO_JSVAL(nsInstall::UNEXPECTED_ERROR);
00125 
00126   //  public int DirCreate (nsInstallFolder aNativeFolderPath);
00127 
00128   if ( argc == 0 || argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
00129   {
00130     *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00131     return JS_TRUE;
00132   }
00133 
00134   jsObj = JSVAL_TO_OBJECT(argv[0]);
00135 
00136   if (!JS_InstanceOf(cx, jsObj, &FileSpecObjectClass, nsnull))
00137   {
00138     *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00139     return JS_TRUE;
00140   }
00141 
00142   folder = (nsInstallFolder*)JS_GetPrivate(cx, jsObj);
00143   
00144   if(!folder || NS_OK != nativeThis->FileOpDirCreate(*folder, &nativeRet))
00145   {
00146     return JS_TRUE;
00147   }
00148 
00149   *rval = INT_TO_JSVAL(nativeRet);
00150   return JS_TRUE;
00151 }
00152 
00153 
00154 //
00155 // Native method DirGetParent
00156 //
00157 JSBool PR_CALLBACK
00158 InstallFileOpDirGetParent(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00159 {
00160   nsInstall *nativeThis =
00161     GetNativeThis(cx, obj, argv);
00162   if (!nativeThis)
00163     return JS_FALSE;
00164 
00165   JSObject *jsObj;
00166   nsInstallFolder *parentFolder, *folder;
00167 
00168   *rval = JSVAL_NULL;
00169 
00170   //  public int DirGetParent (nsInstallFolder NativeFolderPath);
00171 
00172   if ( argc == 0 || argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
00173   {
00174     // error, return NULL
00175     return JS_TRUE;
00176   }
00177 
00178   jsObj = JSVAL_TO_OBJECT(argv[0]);
00179 
00180   if (!JS_InstanceOf(cx, jsObj, &FileSpecObjectClass, nsnull))
00181   {
00182     // error, return NULL
00183     return JS_TRUE;
00184   }
00185 
00186   folder = (nsInstallFolder*)JS_GetPrivate(cx, jsObj);
00187 
00188   if(!folder || NS_OK != nativeThis->FileOpDirGetParent(*folder, &parentFolder))
00189   {
00190     return JS_TRUE;
00191   }
00192 
00193   if ( parentFolder )
00194   {
00195     /* Now create the new JSObject */
00196     JSObject *fileSpecObject;
00197 
00198     fileSpecObject = JS_NewObject(cx, &FileSpecObjectClass, gFileSpecProto, NULL);
00199 
00200     if (fileSpecObject)
00201     {
00202       JS_SetPrivate(cx, fileSpecObject, parentFolder);
00203 
00204       if (fileSpecObject)
00205       {
00206         *rval = OBJECT_TO_JSVAL(fileSpecObject);
00207       }
00208     }
00209   }
00210 
00211   return JS_TRUE;
00212 }
00213 
00214 //
00215 // Native method DirRemove
00216 //
00217 JSBool PR_CALLBACK
00218 InstallFileOpDirRemove(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00219 {
00220   nsInstall *nativeThis =
00221     GetNativeThis(cx, obj, argv);
00222   if (!nativeThis)
00223     return JS_FALSE;
00224 
00225   PRInt32 nativeRet;
00226   PRBool  bRecursive = PR_FALSE;
00227   JSObject *jsObj;
00228   nsInstallFolder *folder;
00229 
00230   *rval = INT_TO_JSVAL(nsInstall::UNEXPECTED_ERROR);
00231 
00232   //  public int DirRemove (nsInstallFolder aNativeFolderPath,
00233   //                        Bool   aRecursive);
00234 
00235   if ( argc == 0 || argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
00236   {
00237     *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00238     return JS_TRUE;
00239   }
00240 
00241   jsObj = JSVAL_TO_OBJECT(argv[0]);
00242 
00243   if (!JS_InstanceOf(cx, jsObj, &FileSpecObjectClass, nsnull))
00244   {
00245     *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00246     return JS_TRUE;
00247   }
00248 
00249   folder = (nsInstallFolder*)JS_GetPrivate(cx, jsObj);
00250 
00251   if( argc > 1 && !ConvertJSValToBool(&bRecursive, cx, argv[1]))
00252   {
00253     JS_ReportError(cx, "2nd parameter needs to be a Boolean value");
00254     return JS_TRUE;
00255   }
00256 
00257   if(!folder || NS_OK != nativeThis->FileOpDirRemove(*folder, bRecursive, &nativeRet))
00258   {
00259     return JS_TRUE;
00260   }
00261 
00262   *rval = INT_TO_JSVAL(nativeRet);
00263 
00264   return JS_TRUE;
00265 }
00266 
00267 //
00268 // Native method DirRename
00269 //
00270 JSBool PR_CALLBACK
00271 InstallFileOpDirRename(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00272 {
00273   nsInstall *nativeThis =
00274     GetNativeThis(cx, obj, argv);
00275   if (!nativeThis)
00276     return JS_FALSE;
00277 
00278   PRInt32 nativeRet;
00279   nsAutoString b1;
00280   JSObject *jsObj;
00281   nsInstallFolder *folder;
00282 
00283   *rval = INT_TO_JSVAL(nsInstall::UNEXPECTED_ERROR);
00284 
00285   if(argc >= 2)
00286   {
00287     //  public int DirRename (String aSourceFolder,
00288     //                        String aTargetFolder);
00289 
00290     ConvertJSValToStr(b1, cx, argv[1]);
00291 
00292 // fix: nsFileSpec::Rename() does not accept new name as a
00293 //      nsFileSpec type.  It only accepts a char* type for the new name
00294 //      This is a bug with nsFileSpec.  A char* will be used until
00295 //      nsFileSpec if fixed.
00296 //    nsFileSpec fsB1(b1);
00297     
00298     if (argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
00299     {
00300       *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00301       return JS_TRUE;
00302     }
00303 
00304     jsObj = JSVAL_TO_OBJECT(argv[0]);
00305 
00306     if (!JS_InstanceOf(cx, jsObj, &FileSpecObjectClass, nsnull))
00307     {
00308       *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00309       return JS_TRUE;
00310     }
00311 
00312     folder = (nsInstallFolder*)JS_GetPrivate(cx, jsObj);
00313 
00314     if(!folder || NS_OK != nativeThis->FileOpDirRename(*folder, b1, &nativeRet))
00315     {
00316       return JS_TRUE;
00317     }
00318 
00319     *rval = INT_TO_JSVAL(nativeRet);
00320   }
00321   else
00322   {
00323     JS_ReportError(cx, "Function DirRename requires 2 parameters");
00324     return JS_TRUE;
00325   }
00326 
00327   return JS_TRUE;
00328 }
00329 
00330 //
00331 // Native method FileCopy
00332 //
00333 JSBool PR_CALLBACK
00334 InstallFileOpFileCopy(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00335 {
00336   nsInstall *nativeThis =
00337     GetNativeThis(cx, obj, argv);
00338   if (!nativeThis)
00339     return JS_FALSE;
00340 
00341   PRInt32 nativeRet;
00342   JSObject *jsSrcObj, *jsTargetObj;
00343   nsInstallFolder *srcFolder, *targetFolder;
00344 
00345   *rval = INT_TO_JSVAL(nsInstall::UNEXPECTED_ERROR);
00346 
00347   if(argc >= 2)
00348   {
00349     //  public int FileCopy (nsInstallFolder aSourceFolder,
00350     //                       nsInstallFolder aTargetFolder);
00351 
00352     if (argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
00353     {
00354       *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00355       return JS_TRUE;
00356     }
00357 
00358     if (argv[1] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[1])) //argv[1] MUST be a jsval
00359     {
00360       *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00361       return JS_TRUE;
00362     }
00363 
00364     jsSrcObj = JSVAL_TO_OBJECT(argv[0]);
00365     jsTargetObj = JSVAL_TO_OBJECT(argv[1]);
00366 
00367     if (!JS_InstanceOf(cx, jsSrcObj, &FileSpecObjectClass, nsnull))
00368     {
00369       *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00370       return JS_TRUE;
00371     }
00372     if (!JS_InstanceOf(cx, jsTargetObj, &FileSpecObjectClass, nsnull))
00373     {
00374       *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00375       return JS_TRUE;
00376     }
00377 
00378     srcFolder = (nsInstallFolder*)JS_GetPrivate(cx, jsSrcObj);
00379     targetFolder = (nsInstallFolder*)JS_GetPrivate(cx, jsTargetObj);
00380     PRInt32 tempRet;
00381     tempRet = nativeThis->FileOpFileCopy(*srcFolder, *targetFolder, &nativeRet);
00382 
00383     if(!srcFolder || !targetFolder || NS_OK != tempRet)
00384     {
00385       return JS_TRUE;
00386     }
00387 
00388     *rval = INT_TO_JSVAL(nativeRet);
00389   }
00390   else
00391   {
00392     JS_ReportError(cx, "Function FileCopy requires 2 parameters");
00393     return JS_TRUE;
00394   }
00395 
00396   return JS_TRUE;
00397 }
00398 
00399 //
00400 // Native method FileDelete
00401 //
00402 JSBool PR_CALLBACK
00403 InstallFileOpFileRemove(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00404 {
00405   nsInstall *nativeThis =
00406     GetNativeThis(cx, obj, argv);
00407   if (!nativeThis)
00408     return JS_FALSE;
00409 
00410   PRInt32 nativeRet;
00411   JSObject *jsObj;
00412   nsInstallFolder *folder;
00413 
00414   *rval = INT_TO_JSVAL(nsInstall::UNEXPECTED_ERROR);
00415 
00416   //  public int FileDelete (nsInstallFolder aSourceFolder);
00417 
00418   if (argc == 0 || argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
00419   {
00420     *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00421     return JS_TRUE;
00422   }
00423 
00424   jsObj = JSVAL_TO_OBJECT(argv[0]);
00425 
00426   if (!JS_InstanceOf(cx, jsObj, &FileSpecObjectClass, nsnull))
00427   {
00428     *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00429     return JS_TRUE;
00430   }
00431 
00432   folder = (nsInstallFolder*)JS_GetPrivate(cx, jsObj);
00433 
00434   if(!folder || NS_OK != nativeThis->FileOpFileDelete(*folder, PR_FALSE, &nativeRet))
00435   {
00436     return JS_TRUE;
00437   }
00438 
00439   *rval = INT_TO_JSVAL(nativeRet);
00440 
00441   return JS_TRUE;
00442 }
00443 
00444 //
00445 // Native method FileExists
00446 //
00447 JSBool PR_CALLBACK
00448 InstallFileOpFileExists(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00449 {
00450   nsInstall *nativeThis =
00451     GetNativeThis(cx, obj, argv);
00452   if (!nativeThis)
00453     return JS_FALSE;
00454 
00455   PRInt32 nativeRet;
00456   JSObject *jsObj;
00457   nsInstallFolder *folder;
00458 
00459   *rval = INT_TO_JSVAL(nsInstall::UNEXPECTED_ERROR);
00460 
00461   //  public int FileExists (nsInstallFolder NativeFolderPath);
00462 
00463   if (argc == 0 || argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
00464   {
00465     *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00466     return JS_TRUE;
00467   }
00468 
00469   jsObj = JSVAL_TO_OBJECT(argv[0]);
00470 
00471   if (!JS_InstanceOf(cx, jsObj, &FileSpecObjectClass, nsnull))
00472   {
00473     *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00474     return JS_TRUE;
00475   }
00476 
00477   folder = (nsInstallFolder*)JS_GetPrivate(cx, jsObj);
00478 
00479   if(!folder || NS_OK != nativeThis->FileOpFileExists(*folder, &nativeRet))
00480   {
00481     return JS_TRUE;
00482   }
00483 
00484   *rval = INT_TO_JSVAL(nativeRet);
00485 
00486   return JS_TRUE;
00487 }
00488 
00489 //
00490 // Native method FileExecute
00491 //
00492 JSBool PR_CALLBACK
00493 InstallFileOpFileExecute(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00494 {
00495   nsInstall *nativeThis =
00496     GetNativeThis(cx, obj, argv);
00497   if (!nativeThis)
00498     return JS_FALSE;
00499 
00500   PRInt32 nativeRet;
00501   nsAutoString b1;
00502   PRBool blocking = PR_FALSE;
00503   JSObject *jsObj;
00504   nsInstallFolder *folder;
00505 
00506   *rval = INT_TO_JSVAL(nsInstall::UNEXPECTED_ERROR);
00507 
00508   if(argc >= 3)
00509   {
00510     //  public int FileExecute (nsInstallFolder aSourceFolder,
00511     //                          String aParameters
00512     //                          PRBool aBlocking);
00513 
00514     ConvertJSValToStr(b1, cx, argv[1]);
00515     ConvertJSValToBool(&blocking, cx, argv[2]);
00516   }
00517   else if(argc >= 2)
00518   {
00519     if(JSVAL_IS_BOOLEAN(argv[1]))
00520     {
00521       //  public int FileExecute (nsInstallFolder aSourceFolder,
00522       //                          PRBool aBlocking);
00523       ConvertJSValToBool(&blocking, cx, argv[1]);
00524       b1.SetLength(0);
00525     }
00526     else
00527     {
00528       //  public int FileExecute (nsInstallFolder aSourceFolder,
00529       //                          String aParameters);
00530       ConvertJSValToStr(b1, cx, argv[1]);
00531     }
00532   }
00533   else
00534     b1.SetLength(0);
00535 
00536   if (argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
00537   {
00538       *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00539       return JS_TRUE;
00540   }
00541 
00542   jsObj = JSVAL_TO_OBJECT(argv[0]);
00543 
00544   if (!JS_InstanceOf(cx, jsObj, &FileSpecObjectClass, nsnull))
00545   {
00546       *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00547       return JS_TRUE;
00548   }
00549 
00550   folder = (nsInstallFolder*)JS_GetPrivate(cx, jsObj);
00551 
00552   jsrefcount saveDepth;
00553   saveDepth = JS_SuspendRequest(cx);//Need to suspend use of thread or deadlock occurs
00554   nsresult rv = nativeThis->FileOpFileExecute(*folder, b1, blocking, &nativeRet);
00555   JS_ResumeRequest(cx, saveDepth);
00556   if(NS_FAILED(rv)) 
00557      return JS_TRUE;
00558 
00559   *rval = INT_TO_JSVAL(nativeRet);
00560 
00561   return JS_TRUE;
00562 }
00563 
00564 //
00565 // Native method FileGetNativeVersion
00566 //
00567 JSBool PR_CALLBACK
00568 InstallFileOpFileGetNativeVersion(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00569 {
00570   nsInstall *nativeThis =
00571     GetNativeThis(cx, obj, argv);
00572   if (!nativeThis)
00573     return JS_FALSE;
00574 
00575   nsAutoString nativeRet;
00576   JSObject *jsObj;
00577   nsInstallFolder *folder;
00578 
00579   *rval = JSVAL_NULL;
00580 
00581   //  public int FileGetNativeVersion (nsInstallFolder NativeFolderPath);
00582 
00583   if (argc == 0 || argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
00584   {
00585     *rval = JSVAL_NULL;
00586     return JS_TRUE;
00587   }
00588 
00589   jsObj = JSVAL_TO_OBJECT(argv[0]);
00590 
00591   if (!JS_InstanceOf(cx, jsObj, &FileSpecObjectClass, nsnull))
00592   {
00593     *rval = JSVAL_NULL;
00594     return JS_TRUE;
00595   }
00596 
00597   folder = (nsInstallFolder*)JS_GetPrivate(cx, jsObj);
00598 
00599   if(!folder || NS_OK != nativeThis->FileOpFileGetNativeVersion(*folder, &nativeRet))
00600   {
00601     return JS_TRUE;
00602   }
00603 
00604   *rval = STRING_TO_JSVAL(JS_NewUCStringCopyN(cx, NS_REINTERPRET_CAST(const jschar*, nativeRet.get()), nativeRet.Length()));
00605 
00606   return JS_TRUE;
00607 }
00608 
00609 //
00610 // Native method FileGetDiskSpaceAvailable
00611 //
00612 JSBool PR_CALLBACK
00613 InstallFileOpFileGetDiskSpaceAvailable(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00614 {
00615 
00616   nsInstall *nativeThis =
00617     GetNativeThis(cx, obj, argv);
00618   if (!nativeThis)
00619     return JS_FALSE;
00620 
00621   PRInt64     nativeRet;
00622   JSObject *jsObj;
00623   nsInstallFolder *folder;
00624 
00625   *rval = INT_TO_JSVAL(nsInstall::UNEXPECTED_ERROR);
00626 
00627   //  public int FileGetDiskSpaceAvailable (String NativeFolderPath);
00628 
00629   if (argc == 0 || argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
00630   {
00631     *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00632     return JS_TRUE;
00633   }
00634 
00635   jsObj = JSVAL_TO_OBJECT(argv[0]);
00636 
00637   if (!JS_InstanceOf(cx, jsObj, &FileSpecObjectClass, nsnull))
00638   {
00639     *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00640     return JS_TRUE;
00641   }
00642 
00643   folder = (nsInstallFolder*)JS_GetPrivate(cx, jsObj);
00644 
00645   if(!folder || NS_OK != nativeThis->FileOpFileGetDiskSpaceAvailable(*folder, &nativeRet))
00646   {
00647     return JS_TRUE;
00648   }
00649   
00650   double d;
00651   LL_L2D(d, nativeRet);
00652   JS_NewDoubleValue( cx, d, rval );
00653 
00654   return JS_TRUE;
00655 }
00656 
00657 //
00658 // Native method FileGetModDate
00659 //
00660 JSBool PR_CALLBACK
00661 InstallFileOpFileGetModDate(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00662 {
00663   nsInstall *nativeThis =
00664     GetNativeThis(cx, obj, argv);
00665   if (!nativeThis)
00666     return JS_FALSE;
00667 
00668   double     nativeRet;
00669   JSObject *jsObj;
00670   nsInstallFolder *folder;
00671 
00672   *rval = INT_TO_JSVAL(nsInstall::UNEXPECTED_ERROR);;
00673 
00674   //  public int FileGetModDate (nsInstallFolder NativeFolderPath);
00675 
00676   if (argc == 0 || argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
00677   {
00678     *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00679     return JS_TRUE;
00680   }
00681 
00682   jsObj = JSVAL_TO_OBJECT(argv[0]);
00683 
00684   if (!JS_InstanceOf(cx, jsObj, &FileSpecObjectClass, nsnull))
00685   {
00686     *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00687     return JS_TRUE;
00688   }
00689 
00690   folder = (nsInstallFolder*)JS_GetPrivate(cx, jsObj);
00691 
00692   if(!folder || NS_OK != nativeThis->FileOpFileGetModDate(*folder, &nativeRet))
00693   {
00694     return JS_TRUE;
00695   }
00696 
00697   JS_NewDoubleValue( cx, nativeRet, rval );
00698 
00699   return JS_TRUE;
00700 }
00701 
00702 //
00703 // Native method FileGetSize
00704 //
00705 JSBool PR_CALLBACK
00706 InstallFileOpFileGetSize(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00707 {
00708   nsInstall *nativeThis =
00709     GetNativeThis(cx, obj, argv);
00710   if (!nativeThis)
00711     return JS_FALSE;
00712 
00713   PRInt64     nativeRet;
00714   JSObject *jsObj;
00715   nsInstallFolder *folder;
00716 
00717   *rval = INT_TO_JSVAL(nsInstall::UNEXPECTED_ERROR);
00718 
00719   //  public int FileGetSize (String NativeFolderPath);
00720 
00721   if (argc == 0 || argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
00722   {
00723     *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00724     return JS_TRUE;
00725   }
00726 
00727   jsObj = JSVAL_TO_OBJECT(argv[0]);
00728 
00729   if (!JS_InstanceOf(cx, jsObj, &FileSpecObjectClass, nsnull))
00730   {
00731     *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00732     return JS_TRUE;
00733   }
00734 
00735   folder = (nsInstallFolder*)JS_GetPrivate(cx, jsObj);
00736 
00737   if(!folder || NS_OK != nativeThis->FileOpFileGetSize(*folder, &nativeRet))
00738   {
00739     return JS_TRUE;
00740   }
00741    
00742   PRFloat64 f; /* jsdouble's *are* PRFloat64's */
00743   
00744   LL_L2F( f, nativeRet ); /* make float which is same type for js and nspr (native double) */
00745   JS_NewDoubleValue( cx, f, rval );
00746   
00747   return JS_TRUE;
00748 }
00749 
00750 //
00751 // Native method FileIsDirectory
00752 //
00753 JSBool PR_CALLBACK
00754 InstallFileOpFileIsDirectory(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00755 {
00756   nsInstall *nativeThis =
00757     GetNativeThis(cx, obj, argv);
00758   if (!nativeThis)
00759     return JS_FALSE;
00760 
00761   PRInt32 nativeRet;
00762   JSObject *jsObj;
00763   nsInstallFolder *folder;
00764 
00765   *rval = BOOLEAN_TO_JSVAL(PR_FALSE);
00766 
00767   //  public int FileIsDirectory (String NativeFolderPath);
00768 
00769   if (argc == 0 || argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
00770   {
00771     *rval = BOOLEAN_TO_JSVAL(PR_FALSE);
00772     return JS_TRUE;
00773   }
00774 
00775   jsObj = JSVAL_TO_OBJECT(argv[0]);
00776 
00777   if (!JS_InstanceOf(cx, jsObj, &FileSpecObjectClass, nsnull))
00778   {
00779     *rval = BOOLEAN_TO_JSVAL(PR_FALSE);
00780     return JS_TRUE;
00781   }
00782 
00783   folder = (nsInstallFolder*)JS_GetPrivate(cx, jsObj);
00784 
00785   if(!folder || NS_OK != nativeThis->FileOpFileIsDirectory(*folder, &nativeRet))
00786   {
00787     return JS_TRUE;
00788   }
00789 
00790   *rval = BOOLEAN_TO_JSVAL(nativeRet);
00791 
00792   return JS_TRUE;
00793 }
00794 
00795 //
00796 // Native method FileIsWritable
00797 //
00798 JSBool PR_CALLBACK
00799 InstallFileOpFileIsWritable(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00800 {
00801   nsInstall *nativeThis =
00802     GetNativeThis(cx, obj, argv);
00803   if (!nativeThis)
00804     return JS_FALSE;
00805 
00806   PRInt32 nativeRet;
00807   JSObject *jsObj;
00808   nsInstallFolder *folder;
00809 
00810   *rval = BOOLEAN_TO_JSVAL(PR_FALSE);
00811 
00812   if (argc == 0 || argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
00813   {
00814     *rval = BOOLEAN_TO_JSVAL(PR_FALSE);
00815     return JS_TRUE;
00816   }
00817 
00818   jsObj = JSVAL_TO_OBJECT(argv[0]);
00819 
00820   if (!JS_InstanceOf(cx, jsObj, &FileSpecObjectClass, nsnull))
00821   {
00822     *rval = BOOLEAN_TO_JSVAL(PR_FALSE);
00823     return JS_TRUE;
00824   }
00825 
00826   folder = (nsInstallFolder*)JS_GetPrivate(cx, jsObj);
00827 
00828   if(!folder || NS_OK != nativeThis->FileOpFileIsWritable(*folder, &nativeRet))
00829   {
00830     return JS_TRUE;
00831   }
00832 
00833   *rval = BOOLEAN_TO_JSVAL(nativeRet);
00834 
00835   return JS_TRUE;
00836 }
00837 
00838 
00839 //
00840 // Native method FileIsFile
00841 //
00842 JSBool PR_CALLBACK
00843 InstallFileOpFileIsFile(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00844 {
00845   nsInstall *nativeThis =
00846     GetNativeThis(cx, obj, argv);
00847   if (!nativeThis)
00848     return JS_FALSE;
00849 
00850   PRInt32 nativeRet;
00851   JSObject *jsObj;
00852   nsInstallFolder *folder;
00853 
00854  *rval = BOOLEAN_TO_JSVAL(PR_FALSE);
00855 
00856   //  public int FileIsFile (nsInstallFolder NativeFolderPath);
00857 
00858   if (argc == 0 || argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
00859   {
00860     *rval = BOOLEAN_TO_JSVAL(PR_FALSE);
00861     return JS_TRUE;
00862   }
00863 
00864   jsObj = JSVAL_TO_OBJECT(argv[0]);
00865 
00866   if (!JS_InstanceOf(cx, jsObj, &FileSpecObjectClass, nsnull))
00867   {
00868     *rval = BOOLEAN_TO_JSVAL(PR_FALSE);
00869     return JS_TRUE;
00870   }
00871 
00872   folder = (nsInstallFolder*)JS_GetPrivate(cx, jsObj);
00873 
00874   if(!folder || NS_OK != nativeThis->FileOpFileIsFile(*folder, &nativeRet))
00875   {
00876     return JS_TRUE;
00877   }
00878 
00879   *rval = BOOLEAN_TO_JSVAL(nativeRet);
00880 
00881   return JS_TRUE;
00882 }
00883 
00884 //
00885 // Native method FileModDateChanged
00886 //
00887 JSBool PR_CALLBACK
00888 InstallFileOpFileModDateChanged(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00889 {
00890   nsInstall *nativeThis =
00891     GetNativeThis(cx, obj, argv);
00892   if (!nativeThis)
00893     return JS_FALSE;
00894 
00895   PRInt32      nativeRet;
00896   JSObject *jsObj;
00897   nsInstallFolder *folder;
00898 
00899   *rval = BOOLEAN_TO_JSVAL(PR_FALSE);
00900 
00901   if(argc >= 2)
00902   {
00903     //  public int FileModDateChanged (nsInstallFolder aSourceFolder,
00904     //                                 Number aOldDate);
00905 
00906     jsdouble dval = *(JSVAL_TO_DOUBLE(argv[1]));
00907     if (argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
00908     {
00909       *rval =     *rval = BOOLEAN_TO_JSVAL(PR_FALSE);
00910       return JS_TRUE;
00911     }
00912 
00913     jsObj = JSVAL_TO_OBJECT(argv[0]);
00914 
00915     if (!JS_InstanceOf(cx, jsObj, &FileSpecObjectClass, nsnull))
00916     {
00917       *rval = BOOLEAN_TO_JSVAL(PR_FALSE);
00918       return JS_TRUE;
00919     }
00920 
00921     folder = (nsInstallFolder*)JS_GetPrivate(cx, jsObj);
00922 
00923     if(!folder || NS_OK != nativeThis->FileOpFileModDateChanged(*folder, dval, &nativeRet))
00924     {
00925       return JS_TRUE;
00926     }
00927 
00928     *rval = BOOLEAN_TO_JSVAL(nativeRet);
00929   }
00930   else
00931   {
00932     JS_ReportError(cx, "Function FileModDateChanged requires 2 parameters");
00933     return JS_TRUE;
00934   }
00935 
00936   return JS_TRUE;
00937 }
00938 
00939 //
00940 // Native method FileMove
00941 //
00942 JSBool PR_CALLBACK
00943 InstallFileOpFileMove(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00944 {
00945   nsInstall *nativeThis =
00946     GetNativeThis(cx, obj, argv);
00947   if (!nativeThis)
00948     return JS_FALSE;
00949 
00950   PRInt32 nativeRet;
00951   JSObject *jsSrcObj, *jsTargetObj;
00952   nsInstallFolder *srcFolder, *targetFolder;
00953 
00954   *rval = INT_TO_JSVAL(nsInstall::UNEXPECTED_ERROR);
00955 
00956   if(argc >= 2)
00957   {
00958     //  public int FileMove (nsInstallFolder aSourceFolder,
00959     //                       nsInstallFolder aTargetFolder);
00960 
00961     if (argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
00962     {
00963       *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00964       return JS_TRUE;
00965     }
00966     if (argv[1] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[1])) //argv[0] MUST be a jsval
00967     {
00968       *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00969       return JS_TRUE;
00970     }
00971 
00972     jsSrcObj = JSVAL_TO_OBJECT(argv[0]);
00973     jsTargetObj = JSVAL_TO_OBJECT(argv[1]);
00974 
00975     if (!JS_InstanceOf(cx, jsSrcObj, &FileSpecObjectClass, nsnull))
00976     {
00977       *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00978       return JS_TRUE;
00979     }
00980     if (!JS_InstanceOf(cx, jsTargetObj, &FileSpecObjectClass, nsnull))
00981     {
00982       *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
00983       return JS_TRUE;
00984     }
00985 
00986     srcFolder = (nsInstallFolder*)JS_GetPrivate(cx, jsSrcObj);
00987     targetFolder = (nsInstallFolder*)JS_GetPrivate(cx, jsTargetObj);
00988 
00989     if(!srcFolder || !targetFolder || NS_OK != nativeThis->FileOpFileMove(*srcFolder, *targetFolder, &nativeRet))
00990     {
00991       return JS_TRUE;
00992     }
00993 
00994     *rval = INT_TO_JSVAL(nativeRet);
00995   }
00996   else
00997   {
00998     JS_ReportError(cx, "Function FileMove requires 2 parameters");
00999     return JS_TRUE;
01000   }
01001 
01002   return JS_TRUE;
01003 }
01004 
01005 //
01006 // Native method FileRename
01007 //
01008 JSBool PR_CALLBACK
01009 InstallFileOpFileRename(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
01010 {
01011   nsInstall *nativeThis =
01012     GetNativeThis(cx, obj, argv);
01013   if (!nativeThis)
01014     return JS_FALSE;
01015 
01016   PRInt32 nativeRet;
01017   nsAutoString b1;
01018   JSObject *jsObj;
01019   nsInstallFolder *folder;
01020 
01021   *rval = INT_TO_JSVAL(nsInstall::UNEXPECTED_ERROR);
01022 
01023   if(argc >= 2)
01024   {
01025     //  public int FileRename (nsInstallFolder aSourceFolder,
01026     //                         String aTargetFolder);
01027 
01028     ConvertJSValToStr(b1, cx, argv[1]);
01029 
01030 // fix: nsFileSpec::Rename() does not accept new name as a
01031 //      nsFileSpec type.  It only accepts a char* type for the new name
01032 //      This is a bug with nsFileSpec.  A char* will be used until
01033 //      nsFileSpec if fixed.
01034 //    nsFileSpec fsB1(b1);
01035 
01036     if (argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
01037     {
01038       *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
01039       return JS_TRUE;
01040     }
01041 
01042     jsObj = JSVAL_TO_OBJECT(argv[0]);
01043 
01044     if (!JS_InstanceOf(cx, jsObj, &FileSpecObjectClass, nsnull))
01045     {
01046       *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
01047       return JS_TRUE;
01048     }
01049 
01050     folder = (nsInstallFolder*)JS_GetPrivate(cx, jsObj);
01051 
01052     if(!folder || NS_OK != nativeThis->FileOpFileRename(*folder, b1, &nativeRet))
01053     {
01054       return JS_TRUE;
01055     }
01056 
01057     *rval = INT_TO_JSVAL(nativeRet);
01058   }
01059   else
01060   {
01061     JS_ReportError(cx, "Function FileRename requires 2 parameters");
01062     return JS_TRUE;
01063   }
01064 
01065   return JS_TRUE;
01066 }
01067 
01068 //
01069 // Native method FileWindowsGetShortName
01070 //
01071 JSBool PR_CALLBACK
01072 InstallFileOpFileWindowsGetShortName(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
01073 {
01074   nsAutoString shortPathName;
01075   nsInstall *nativeThis =
01076     GetNativeThis(cx, obj, argv);
01077   if (!nativeThis)
01078     return JS_FALSE;
01079 
01080   JSObject *jsObj;
01081   nsInstallFolder *longPathName;
01082 
01083   *rval = JSVAL_NULL;
01084 
01085   //  public String windowsGetShortName (File NativeFolderPath);
01086 
01087   if ( argc == 0 || argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
01088   {
01089     // error, return NULL
01090     return JS_TRUE;
01091   }
01092 
01093   jsObj = JSVAL_TO_OBJECT(argv[0]);
01094 
01095   if (!JS_InstanceOf(cx, jsObj, &FileSpecObjectClass, nsnull))
01096   {
01097     // error, return NULL
01098     return JS_TRUE;
01099   }
01100 
01101   longPathName = (nsInstallFolder*)JS_GetPrivate(cx, jsObj);
01102 
01103   if(!longPathName || NS_OK != nativeThis->FileOpFileWindowsGetShortName(*longPathName, shortPathName))
01104   {
01105     return JS_TRUE;
01106   }
01107 
01108   if(!shortPathName.IsEmpty())
01109     *rval = STRING_TO_JSVAL(JS_NewUCStringCopyN(cx, NS_REINTERPRET_CAST(const jschar*, shortPathName.get()), shortPathName.Length()));
01110 
01111   return JS_TRUE;
01112 }
01113 
01114 //
01115 // Native method FileWindowsShortcut
01116 //
01117 JSBool PR_CALLBACK
01118 InstallFileOpFileWindowsShortcut(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
01119 {
01120   nsInstall *nativeThis =
01121     GetNativeThis(cx, obj, argv);
01122   if (!nativeThis)
01123     return JS_FALSE;
01124 
01125   PRInt32 nativeRet;
01126   nsAutoString b0;
01127   nsAutoString b1;
01128   nsAutoString b2;
01129   nsAutoString b3;
01130   nsAutoString b4;
01131   nsAutoString b5;
01132   nsCOMPtr<nsILocalFile> nsfsB0;
01133   nsCOMPtr<nsILocalFile> nsfsB1;
01134   nsCOMPtr<nsILocalFile> nsfsB3;
01135   nsCOMPtr<nsILocalFile> nsfsB5;
01136   PRInt32      b6;
01137 
01138   //JSObject *jsObj;
01139   //nsInstallFolder *folder;
01140 
01141   *rval = JSVAL_NULL;
01142 
01143   if(argc >= 7)
01144   {
01145     //  public int FileWindowsShortcut(String aTarget,
01146     //                                 String aShortcutPath,
01147     //                                 String aDescription,
01148     //                                 String aWorkingPath,
01149     //                                 String aParams,
01150     //                                 String aIcon,
01151     //                                 Number aIconId);
01152 
01153 
01154     ConvertJSValToStr(b0, cx, argv[0]);
01155     NS_NewLocalFile(b0, PR_TRUE, getter_AddRefs(nsfsB0));
01156     ConvertJSValToStr(b1, cx, argv[1]);
01157     NS_NewLocalFile(b1, PR_TRUE, getter_AddRefs(nsfsB1));
01158     ConvertJSValToStr(b2, cx, argv[2]);
01159     ConvertJSValToStr(b3, cx, argv[3]);
01160     NS_NewLocalFile(b3, PR_TRUE, getter_AddRefs(nsfsB3));
01161     ConvertJSValToStr(b4, cx, argv[4]);
01162     ConvertJSValToStr(b5, cx, argv[5]);
01163     NS_NewLocalFile(b5, PR_TRUE, getter_AddRefs(nsfsB5));
01164 
01165     if(JSVAL_IS_NULL(argv[6]))
01166     {
01167       b6 = 0;
01168     }
01169     else
01170     {
01171       b6 = JSVAL_TO_INT(argv[6]);
01172     }
01173 
01174     if(NS_OK != nativeThis->FileOpFileWindowsShortcut(nsfsB0, nsfsB1, b2, nsfsB3, b4, nsfsB5, b6, &nativeRet))
01175     {
01176       return JS_TRUE;
01177     }
01178 
01179     *rval = INT_TO_JSVAL(nativeRet);
01180   }
01181   else
01182   {
01183     JS_ReportError(cx, "Function FileWindowsShortcut requires 7 parameters");
01184     return JS_TRUE;
01185   }
01186 
01187   return JS_TRUE;
01188 }
01189 
01190 //
01191 // Native method FileMacAlias
01192 //
01193 JSBool PR_CALLBACK
01194 InstallFileOpFileMacAlias(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
01195 {
01196   nsInstall *nativeThis =
01197     GetNativeThis(cx, obj, argv);
01198   if (!nativeThis)
01199     return JS_FALSE;
01200 
01201   PRInt32          nativeRet;
01202   nsAutoString     sourceLeaf, aliasLeaf;
01203   JSObject         *jsoSourceFolder, *jsoAliasFolder;
01204   nsInstallFolder  *nsifSourceFolder, *nsifAliasFolder;
01205   nsresult         rv1 = NS_OK, rv2 = NS_OK;
01206   
01207   *rval = JSVAL_NULL;
01208 
01209   if(argc >= 3)
01210   {
01211     // public int FileMacAlias( InstallFolder aSourceFolder,
01212     //                          String        aSourceName,
01213     //                          InstallFolder aAliasFolder );
01214     // where
01215     //      aSourceFolder -- the folder of the installed file from Get*Folder() APIs
01216     //      aSourceName   -- the installed file's name
01217     //      aAliasFolder  -- the folder of the new alias from Get*Folder() APIs
01218     //
01219     // NOTE: 
01220     //      The destination alias name is aSourceName + " alias" (Mac-like behavior). 
01221     //      
01222     //      returns SUCCESS for successful scheduling of operation
01223     //              UNEXPECTED_ERROR for failure
01224     
01225     if (argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0]) ||  // InstallFolder aSourceFolder
01226         argv[2] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[2]))    // InstallFolder aAliasFolder
01227     {
01228         *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
01229         return JS_TRUE;
01230     }
01231 
01232     jsoSourceFolder = JSVAL_TO_OBJECT(argv[0]);
01233     jsoAliasFolder  = JSVAL_TO_OBJECT(argv[2]);
01234     if ((!JS_InstanceOf(cx, jsoSourceFolder, &FileSpecObjectClass, nsnull)) ||
01235         (!JS_InstanceOf(cx, jsoAliasFolder,  &FileSpecObjectClass, nsnull)))
01236     {
01237         *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
01238         return JS_TRUE;
01239     }
01240 
01241     nsifSourceFolder = (nsInstallFolder *) JS_GetPrivate(cx, jsoSourceFolder);
01242     nsifAliasFolder  = (nsInstallFolder *) JS_GetPrivate(cx, jsoAliasFolder);
01243     if (!nsifSourceFolder || !nsifAliasFolder)
01244     {
01245         *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
01246         return JS_TRUE;
01247     }
01248 
01249     // make copies of the InstallFolders leaving originals uncontaminated
01250     nsCOMPtr<nsIFile> iFileSourceOrig = nsifSourceFolder->GetFileSpec(); 
01251     nsCOMPtr<nsIFile> iFileAliasOrig  = nsifAliasFolder->GetFileSpec();
01252     nsCOMPtr<nsIFile> iFileSource;
01253     nsCOMPtr<nsIFile> iFileAlias;
01254     rv1 = iFileSourceOrig->Clone(getter_AddRefs(iFileSource));
01255     rv2 = iFileAliasOrig->Clone(getter_AddRefs(iFileAlias));
01256     if (NS_FAILED(rv1) || NS_FAILED(rv2))
01257     {
01258         *rval = INT_TO_JSVAL(nsInstall::UNEXPECTED_ERROR);
01259         return JS_TRUE;
01260     }
01261     
01262     ConvertJSValToStr(sourceLeaf, cx, argv[1]);
01263     rv1 = iFileSource->Append(sourceLeaf);
01264         
01265     // public int FileMacAlias( InstallFolder aSourceFolder,
01266     //                          String        aSourceFileName,
01267     //                          InstallFolder aAliasFolder,
01268     //                          String        aAliasName );
01269     // where
01270     //      aSourceFolder -- the folder of the installed file from Get*Folder() APIs
01271     //      aSourceName   -- the installed file's name
01272     //      aAliasFolder  -- the folder of the new alias from Get*Folder() APIs
01273     //      aAliasName    -- the actual name of the new alias
01274     //      
01275     //      returns SUCCESS for successful scheduling of operation
01276     //              UNEXPECTED_ERROR for failure
01277     
01278     if (argc >= 4) 
01279     {
01280         ConvertJSValToStr(aliasLeaf, cx, argv[3]);
01281     }
01282     else
01283     {
01284        aliasLeaf = sourceLeaf;
01285         aliasLeaf.AppendLiteral(" alias");   // XXX use GetResourcedString(id)
01286     }
01287     
01288     rv2 = iFileAlias->Append(aliasLeaf);
01289        if (NS_FAILED(rv1) || NS_FAILED(rv2))
01290        {
01291               *rval = INT_TO_JSVAL(nsInstall::UNEXPECTED_ERROR);
01292               return JS_TRUE;
01293        }
01294 
01295     if(NS_OK != nativeThis->FileOpFileMacAlias(iFileSource, iFileAlias, &nativeRet))
01296     {
01297         *rval = INT_TO_JSVAL(nsInstall::UNEXPECTED_ERROR);
01298         return JS_TRUE;
01299     }
01300 
01301     *rval = INT_TO_JSVAL(nativeRet);
01302   }
01303   else
01304   {
01305     JS_ReportError(cx, "Function FileMacAlias requires 3 or 4 parameters");
01306     return JS_TRUE;
01307   }
01308 
01309   return JS_TRUE;
01310 }
01311 
01312 //
01313 // Native method FileUnixLinkCreate
01314 //
01315 JSBool PR_CALLBACK
01316 InstallFileOpFileUnixLink(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
01317 {
01318   nsInstall *nativeThis =
01319     GetNativeThis(cx, obj, argv);
01320   if (!nativeThis)
01321     return JS_FALSE;
01322 
01323   PRInt32 nativeRet;
01324   //nsAutoString b0;
01325   PRInt32      b1;
01326   JSObject *jsObj;
01327   nsInstallFolder *folder;
01328 
01329   *rval = JSVAL_NULL;
01330 
01331   if(argc >= 2)
01332   {
01333     //  public int FileUnixLinkCreate (String aSourceFolder,
01334     //                                 Number aFlags);
01335 
01336     b1 = JSVAL_TO_INT(argv[1]);
01337     if (argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
01338     {
01339       *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
01340       return JS_TRUE;
01341     }
01342 
01343     jsObj = JSVAL_TO_OBJECT(argv[0]);
01344 
01345     if (!JS_InstanceOf(cx, jsObj, &FileSpecObjectClass, nsnull))
01346     {
01347       *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
01348       return JS_TRUE;
01349     }
01350 
01351     folder = (nsInstallFolder*)JS_GetPrivate(cx, jsObj);
01352 
01353     if(NS_OK != nativeThis->FileOpFileUnixLink(*folder, b1, &nativeRet))
01354     {
01355       return JS_TRUE;
01356     }
01357 
01358     *rval = INT_TO_JSVAL(nativeRet);
01359   }
01360   else
01361   {
01362     JS_ReportError(cx, "Function FileUnixLink requires 2 parameters");
01363     return JS_TRUE;
01364   }
01365 
01366   return JS_TRUE;
01367 }
01368 
01369 //
01370 // Native method WindowsRegisterServer
01371 //
01372 JSBool PR_CALLBACK
01373 InstallFileOpWinRegisterServer(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
01374 {
01375   nsInstall *nativeThis =
01376     GetNativeThis(cx, obj, argv);
01377   if (!nativeThis)
01378     return JS_FALSE;
01379 
01380   PRInt32 nativeRet;
01381   JSObject *jsObj;
01382   nsInstallFolder *folder;
01383 
01384   *rval = INT_TO_JSVAL(nsInstall::UNEXPECTED_ERROR);
01385 
01386   //  public int WinRegisterServer (nsInstallFolder aNativeFolderPath);
01387 
01388   if ( argc == 0 || argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval
01389   {
01390     *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
01391     return JS_TRUE;
01392   }
01393 
01394   jsObj = JSVAL_TO_OBJECT(argv[0]);
01395 
01396   if (!JS_InstanceOf(cx, jsObj, &FileSpecObjectClass, nsnull))
01397   {
01398     *rval = INT_TO_JSVAL(nsInstall::INVALID_ARGUMENTS);
01399     return JS_TRUE;
01400   }
01401 
01402   folder = (nsInstallFolder*)JS_GetPrivate(cx, jsObj);
01403   
01404   if(!folder || NS_OK != nativeThis->FileOpWinRegisterServer(*folder, &nativeRet))
01405   {
01406     return JS_TRUE;
01407   }
01408 
01409   *rval = INT_TO_JSVAL(nativeRet);
01410   return JS_TRUE;
01411 }
01412 
01413 
01414 //
01415 // File class methods
01416 //
01417 static JSFunctionSpec FileOpMethods[] = 
01418 {
01419   {"dirCreate",                 InstallFileOpDirCreate,                1},
01420   {"dirGetParent",              InstallFileOpDirGetParent,             1},
01421   {"dirRemove",                 InstallFileOpDirRemove,                2},
01422   {"dirRename",                 InstallFileOpDirRename,                2},
01423   {"copy",                      InstallFileOpFileCopy,                 2},
01424   {"remove",                    InstallFileOpFileRemove,               1},
01425   {"exists",                    InstallFileOpFileExists,               1},
01426   {"execute",                   InstallFileOpFileExecute,              2},
01427   {"nativeVersion",             InstallFileOpFileGetNativeVersion,     1},
01428   {"windowsVersion",            InstallFileOpFileGetNativeVersion,     1},
01429   {"diskSpaceAvailable",        InstallFileOpFileGetDiskSpaceAvailable,1},
01430   {"modDate",                   InstallFileOpFileGetModDate,           1},
01431   {"size",                      InstallFileOpFileGetSize,              1},
01432   {"isDirectory",               InstallFileOpFileIsDirectory,          1},
01433   {"isWritable",                InstallFileOpFileIsWritable,           1},
01434   {"isFile",                    InstallFileOpFileIsFile,               1},
01435   {"modDateChanged",            InstallFileOpFileModDateChanged,       2},
01436   {"move",                      InstallFileOpFileMove,                 2},
01437   {"rename",                    InstallFileOpFileRename,               2},
01438   {"windowsGetShortName",       InstallFileOpFileWindowsGetShortName,  1},
01439   {"windowsShortcut",           InstallFileOpFileWindowsShortcut,      7},
01440   {"macAlias",                  InstallFileOpFileMacAlias,             2},
01441   {"unixLink",                  InstallFileOpFileUnixLink,             2},
01442   {"windowsRegisterServer",     InstallFileOpWinRegisterServer,        1},
01443   {0}
01444 };
01445 
01446 
01447 
01448 PRInt32 InitXPFileOpObjectPrototype(JSContext *jscontext, 
01449                                     JSObject *global,
01450                                     JSObject **fileOpObjectPrototype)
01451 
01452 {
01453     if (global == nsnull)
01454     {
01455         return NS_ERROR_FAILURE;
01456     }
01457 
01458     *fileOpObjectPrototype  =  JS_InitClass( jscontext,  // context
01459                                  global,            // global object
01460                                  nsnull,            // parent proto 
01461                                  &FileOpClass,      // JSClass
01462                                  nsnull,            // JSNative ctor
01463                                  0,                 // ctor args
01464                                  nsnull,            // proto props
01465                                  nsnull,            // proto funcs
01466                                  nsnull,            // ctor props (static)
01467                                  FileOpMethods);    // ctor funcs (static)
01468 
01469   if (nsnull == fileOpObjectPrototype) 
01470   {
01471       return NS_ERROR_FAILURE;
01472   }
01473 
01474   return NS_OK;
01475 }
01476