diff --git a/silentdl/SilentDownload.idl b/silentdl/SilentDownload.idl new file mode 100644 index 00000000000..4276d4f66a8 --- /dev/null +++ b/silentdl/SilentDownload.idl @@ -0,0 +1,16 @@ +interface SilentDownload +{ +/* IID: { 0x8648c1e0, 0x938b, 0x11d2, \ + { 0xb0, 0xdd, 0x0, 0x80, 0x5f, 0x8a, 0x88, 0x51 }} */ + + attribute long ByteRange; + attribute long Interval; + + void Startup(); + void Shutdown(); + + void Add(in SilentDownloadTask task); + void Remove(in SilentDownloadTask task); + + SilentDownloadTask Find(in wstring id); +}; diff --git a/silentdl/SilentDownloadTask.idl b/silentdl/SilentDownloadTask.idl new file mode 100644 index 00000000000..64aee400f4d --- /dev/null +++ b/silentdl/SilentDownloadTask.idl @@ -0,0 +1,38 @@ +interface SilentDownloadTask +{ +/* IID: { 0x8648c1e0, 0x938b, 0x11d2, \ +{ 0xb0, 0xdd, 0x0, 0x80, 0x5f, 0x8a, 0x88, 0x50 }} */ + + const int SDL_NOT_INITED = -2; + const int SDL_NOT_ADDED = -1; + const int SDL_STARTED = 0; + const int SDL_SUSPENDED = 1; + const int SDL_COMPLETED = 2; + const int SDL_DOWNLOADING_NOW = 3; + const int SDL_ERROR = 4; + + + readonly attribute wstring id; + readonly attribute wstring url; + readonly attribute wstring script; + + attribute long state; + attribute wstring errorMsg; + attribute long nextByte; + + readonly attribute wstring outFile; + + void SilentDownloadTask(); + void Init(in wstring id, in wstring url, in wstring script); + + void Remove(); + void Suspend(); + void Resume(); + void DownloadNow(); + + + void DownloadSelf(in long range); + +}; + + diff --git a/silentdl/makefile.win b/silentdl/makefile.win new file mode 100644 index 00000000000..31ab64062e6 --- /dev/null +++ b/silentdl/makefile.win @@ -0,0 +1,79 @@ +#!gmake +# +# The contents of this file are subject to the Netscape Public License +# Version 1.0 (the "NPL"); you may not use this file except in +# compliance with the NPL. You may obtain a copy of the NPL at +# http://www.mozilla.org/NPL/ +# +# Software distributed under the NPL is distributed on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL +# for the specific language governing rights and limitations under the +# NPL. +# +# The Initial Developer of this code under the NPL is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All Rights +# Reserved. + + +DEPTH=.. +IGNORE_MANIFEST=1 + +MAKE_OBJ_TYPE = DLL +MODULE=silentdl + +DLL=.\$(OBJDIR)\$(MODULE).dll + +DEFINES=-D_IMPL_NS_DOM -DWIN32_LEAN_AND_MEAN + +LCFLAGS = \ + $(LCFLAGS) \ + $(DEFINES) \ + $(NULL) + +LINCS= \ + -I$(XPDIST)\public\js \ + -I$(XPDIST)\public\netlib \ + -I$(XPDIST)\public\network \ + -I$(XPDIST)\public\dom \ + -I$(XPDIST)\public\xpcom \ + -I$(XPDIST)\public\util \ + -I$(XPDIST)\public\nspr2 \ + -I$(XPDIST)\public\pref \ + -I$(XPDIST)\public\raptor \ + $(NULL) + +LLIBS = \ + $(DIST)\lib\netlib.lib \ + $(DIST)\lib\xpcom32.lib \ + $(DIST)\lib\xppref32.lib \ + $(DIST)\lib\xplib.lib \ + $(DIST)\lib\js3250.lib \ + $(DIST)\lib\jsdombase_s.lib \ + $(DIST)\lib\raptorbase.lib \ + $(DIST)\lib\raptorgfxwin.lib \ + $(LIBNSPR) \ + $(DIST)\lib\libplc21.lib \ + $(NULL) + +OBJS = \ + .\$(OBJDIR)\nsJSSilentDownload.obj \ + .\$(OBJDIR)\nsJSSilentDownloadTask.obj \ + .\$(OBJDIR)\nsSilentDownload.obj \ + $(NULL) + + +EXPORTS= \ + nsIDOMSilentDownload.h \ + nsIDOMSilentDownloadTask.h \ + nsSilentDownload.h + +include <$(DEPTH)\config\rules.mak> + +install:: $(DLL) + $(MAKE_INSTALL) .\$(OBJDIR)\$(MODULE).dll $(DIST)\bin + $(MAKE_INSTALL) .\$(OBJDIR)\$(MODULE).lib $(DIST)\lib + +clobber:: + rm -f $(DIST)\lib\$(MODULE).lib + rm -f $(DIST)\bin\$(MODULE).dll diff --git a/silentdl/nsIDOMSilentDownload.h b/silentdl/nsIDOMSilentDownload.h new file mode 100644 index 00000000000..e12a955c7a2 --- /dev/null +++ b/silentdl/nsIDOMSilentDownload.h @@ -0,0 +1,84 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ +/* AUTO-GENERATED. DO NOT EDIT!!! */ + +#ifndef nsIDOMSilentDownload_h__ +#define nsIDOMSilentDownload_h__ + +#include "nsISupports.h" +#include "nsString.h" +#include "nsIScriptContext.h" + +class nsIDOMSilentDownloadTask; + +#define NS_IDOMSILENTDOWNLOAD_IID \ + { 0x8648c1e0, 0x938b, 0x11d2, \ + { 0xb0, 0xdd, 0x0, 0x80, 0x5f, 0x8a, 0x88, 0x51 }} + +class nsIDOMSilentDownload : public nsISupports { +public: + static const nsIID& IID() { static nsIID iid = NS_IDOMSILENTDOWNLOAD_IID; return iid; } + + NS_IMETHOD GetByteRange(PRInt32* aByteRange)=0; + NS_IMETHOD SetByteRange(PRInt32 aByteRange)=0; + + NS_IMETHOD GetInterval(PRInt32* aInterval)=0; + NS_IMETHOD SetInterval(PRInt32 aInterval)=0; + + NS_IMETHOD Startup()=0; + + NS_IMETHOD Shutdown()=0; + + NS_IMETHOD Add(nsIDOMSilentDownloadTask* aTask)=0; + + NS_IMETHOD Remove(nsIDOMSilentDownloadTask* aTask)=0; + + NS_IMETHOD Find(const nsString& aId, nsIDOMSilentDownloadTask** aReturn)=0; +}; + + +#define NS_DECL_IDOMSILENTDOWNLOAD \ + NS_IMETHOD GetByteRange(PRInt32* aByteRange); \ + NS_IMETHOD SetByteRange(PRInt32 aByteRange); \ + NS_IMETHOD GetInterval(PRInt32* aInterval); \ + NS_IMETHOD SetInterval(PRInt32 aInterval); \ + NS_IMETHOD Startup(); \ + NS_IMETHOD Shutdown(); \ + NS_IMETHOD Add(nsIDOMSilentDownloadTask* aTask); \ + NS_IMETHOD Remove(nsIDOMSilentDownloadTask* aTask); \ + NS_IMETHOD Find(const nsString& aId, nsIDOMSilentDownloadTask** aReturn); \ + + + +#define NS_FORWARD_IDOMSILENTDOWNLOAD(_to) \ + NS_IMETHOD GetByteRange(PRInt32* aByteRange) { return _to##GetByteRange(aByteRange); } \ + NS_IMETHOD SetByteRange(PRInt32 aByteRange) { return _to##SetByteRange(aByteRange); } \ + NS_IMETHOD GetInterval(PRInt32* aInterval) { return _to##GetInterval(aInterval); } \ + NS_IMETHOD SetInterval(PRInt32 aInterval) { return _to##SetInterval(aInterval); } \ + NS_IMETHOD Startup() { return _to##Startup(); } \ + NS_IMETHOD Shutdown() { return _to##Shutdown(); } \ + NS_IMETHOD Add(nsIDOMSilentDownloadTask* aTask) { return _to##Add(aTask); } \ + NS_IMETHOD Remove(nsIDOMSilentDownloadTask* aTask) { return _to##Remove(aTask); } \ + NS_IMETHOD Find(const nsString& aId, nsIDOMSilentDownloadTask** aReturn) { return _to##Find(aId, aReturn); } \ + + +extern nsresult NS_InitSilentDownloadClass(nsIScriptContext *aContext, void **aPrototype); + +extern "C" NS_DOM nsresult NS_NewScriptSilentDownload(nsIScriptContext *aContext, nsISupports *aSupports, nsISupports *aParent, void **aReturn); + +#endif // nsIDOMSilentDownload_h__ diff --git a/silentdl/nsIDOMSilentDownloadTask.h b/silentdl/nsIDOMSilentDownloadTask.h new file mode 100644 index 00000000000..541c28aa8f9 --- /dev/null +++ b/silentdl/nsIDOMSilentDownloadTask.h @@ -0,0 +1,119 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ +/* AUTO-GENERATED. DO NOT EDIT!!! */ + +#ifndef nsIDOMSilentDownloadTask_h__ +#define nsIDOMSilentDownloadTask_h__ + +#include "nsISupports.h" +#include "nsString.h" +#include "nsIScriptContext.h" + + +#define NS_IDOMSILENTDOWNLOADTASK_IID \ + { 0x8648c1e0, 0x938b, 0x11d2, \ +{ 0xb0, 0xdd, 0x0, 0x80, 0x5f, 0x8a, 0x88, 0x50 }} + +class nsIDOMSilentDownloadTask : public nsISupports { +public: + static const nsIID& IID() { static nsIID iid = NS_IDOMSILENTDOWNLOADTASK_IID; return iid; } + enum { + SDL_NOT_INITED = -2, + SDL_NOT_ADDED = -1, + SDL_STARTED = 0, + SDL_SUSPENDED = 1, + SDL_COMPLETED = 2, + SDL_DOWNLOADING_NOW = 3, + SDL_ERROR = 4 + }; + + NS_IMETHOD GetId(nsString& aId)=0; + + NS_IMETHOD GetUrl(nsString& aUrl)=0; + + NS_IMETHOD GetScript(nsString& aScript)=0; + + NS_IMETHOD GetState(PRInt32* aState)=0; + NS_IMETHOD SetState(PRInt32 aState)=0; + + NS_IMETHOD GetErrorMsg(nsString& aErrorMsg)=0; + NS_IMETHOD SetErrorMsg(const nsString& aErrorMsg)=0; + + NS_IMETHOD GetNextByte(PRInt32* aNextByte)=0; + NS_IMETHOD SetNextByte(PRInt32 aNextByte)=0; + + NS_IMETHOD GetOutFile(nsString& aOutFile)=0; + + NS_IMETHOD Init(const nsString& aId, const nsString& aUrl, const nsString& aScript)=0; + + NS_IMETHOD Remove()=0; + + NS_IMETHOD Suspend()=0; + + NS_IMETHOD Resume()=0; + + NS_IMETHOD DownloadNow()=0; + + NS_IMETHOD DownloadSelf(PRInt32 aRange)=0; +}; + + +#define NS_DECL_IDOMSILENTDOWNLOADTASK \ + NS_IMETHOD GetId(nsString& aId); \ + NS_IMETHOD GetUrl(nsString& aUrl); \ + NS_IMETHOD GetScript(nsString& aScript); \ + NS_IMETHOD GetState(PRInt32* aState); \ + NS_IMETHOD SetState(PRInt32 aState); \ + NS_IMETHOD GetErrorMsg(nsString& aErrorMsg); \ + NS_IMETHOD SetErrorMsg(const nsString& aErrorMsg); \ + NS_IMETHOD GetNextByte(PRInt32* aNextByte); \ + NS_IMETHOD SetNextByte(PRInt32 aNextByte); \ + NS_IMETHOD GetOutFile(nsString& aOutFile); \ + NS_IMETHOD Init(const nsString& aId, const nsString& aUrl, const nsString& aScript); \ + NS_IMETHOD Remove(); \ + NS_IMETHOD Suspend(); \ + NS_IMETHOD Resume(); \ + NS_IMETHOD DownloadNow(); \ + NS_IMETHOD DownloadSelf(PRInt32 aRange); \ + + + +#define NS_FORWARD_IDOMSILENTDOWNLOADTASK(_to) \ + NS_IMETHOD GetId(nsString& aId) { return _to##GetId(aId); } \ + NS_IMETHOD GetUrl(nsString& aUrl) { return _to##GetUrl(aUrl); } \ + NS_IMETHOD GetScript(nsString& aScript) { return _to##GetScript(aScript); } \ + NS_IMETHOD GetState(PRInt32* aState) { return _to##GetState(aState); } \ + NS_IMETHOD SetState(PRInt32 aState) { return _to##SetState(aState); } \ + NS_IMETHOD GetErrorMsg(nsString& aErrorMsg) { return _to##GetErrorMsg(aErrorMsg); } \ + NS_IMETHOD SetErrorMsg(const nsString& aErrorMsg) { return _to##SetErrorMsg(aErrorMsg); } \ + NS_IMETHOD GetNextByte(PRInt32* aNextByte) { return _to##GetNextByte(aNextByte); } \ + NS_IMETHOD SetNextByte(PRInt32 aNextByte) { return _to##SetNextByte(aNextByte); } \ + NS_IMETHOD GetOutFile(nsString& aOutFile) { return _to##GetOutFile(aOutFile); } \ + NS_IMETHOD Init(const nsString& aId, const nsString& aUrl, const nsString& aScript) { return _to##Init(aId, aUrl, aScript); } \ + NS_IMETHOD Remove() { return _to##Remove(); } \ + NS_IMETHOD Suspend() { return _to##Suspend(); } \ + NS_IMETHOD Resume() { return _to##Resume(); } \ + NS_IMETHOD DownloadNow() { return _to##DownloadNow(); } \ + NS_IMETHOD DownloadSelf(PRInt32 aRange) { return _to##DownloadSelf(aRange); } \ + + +extern nsresult NS_InitSilentDownloadTaskClass(nsIScriptContext *aContext, void **aPrototype); + +extern "C" NS_DOM nsresult NS_NewScriptSilentDownloadTask(nsIScriptContext *aContext, nsISupports *aSupports, nsISupports *aParent, void **aReturn); + +#endif // nsIDOMSilentDownloadTask_h__ diff --git a/silentdl/nsJSSilentDownload.cpp b/silentdl/nsJSSilentDownload.cpp new file mode 100644 index 00000000000..8f004745653 --- /dev/null +++ b/silentdl/nsJSSilentDownload.cpp @@ -0,0 +1,524 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ +/* AUTO-GENERATED. DO NOT EDIT!!! */ + +#include "jsapi.h" +#include "nsJSUtils.h" +#include "nscore.h" +#include "nsIScriptContext.h" +#include "nsIJSScriptObject.h" +#include "nsIScriptObjectOwner.h" +#include "nsIScriptGlobalObject.h" +#include "nsIPtr.h" +#include "nsString.h" +#include "nsIDOMSilentDownload.h" +#include "nsIDOMSilentDownloadTask.h" + + +static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID); +static NS_DEFINE_IID(kIJSScriptObjectIID, NS_IJSSCRIPTOBJECT_IID); +static NS_DEFINE_IID(kIScriptGlobalObjectIID, NS_ISCRIPTGLOBALOBJECT_IID); +static NS_DEFINE_IID(kISilentDownloadIID, NS_IDOMSILENTDOWNLOAD_IID); +static NS_DEFINE_IID(kISilentDownloadTaskIID, NS_IDOMSILENTDOWNLOADTASK_IID); + +NS_DEF_PTR(nsIDOMSilentDownload); +NS_DEF_PTR(nsIDOMSilentDownloadTask); + +// +// SilentDownload property ids +// +enum SilentDownload_slots { + SILENTDOWNLOAD_BYTERANGE = -1, + SILENTDOWNLOAD_INTERVAL = -2 +}; + +/***********************************************************************/ +// +// SilentDownload Properties Getter +// +PR_STATIC_CALLBACK(JSBool) +GetSilentDownloadProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) +{ + nsIDOMSilentDownload *a = (nsIDOMSilentDownload*)JS_GetPrivate(cx, obj); + + // If there's no private data, this must be the prototype, so ignore + if (nsnull == a) { + return JS_TRUE; + } + + if (JSVAL_IS_INT(id)) { + switch(JSVAL_TO_INT(id)) { + case SILENTDOWNLOAD_BYTERANGE: + { + PRInt32 prop; + if (NS_OK == a->GetByteRange(&prop)) { + *vp = INT_TO_JSVAL(prop); + } + else { + return JS_FALSE; + } + break; + } + case SILENTDOWNLOAD_INTERVAL: + { + PRInt32 prop; + if (NS_OK == a->GetInterval(&prop)) { + *vp = INT_TO_JSVAL(prop); + } + else { + return JS_FALSE; + } + break; + } + default: + return nsJSUtils::nsCallJSScriptObjectGetProperty(a, cx, id, vp); + } + } + else { + return nsJSUtils::nsCallJSScriptObjectGetProperty(a, cx, id, vp); + } + + return PR_TRUE; +} + +/***********************************************************************/ +// +// SilentDownload Properties Setter +// +PR_STATIC_CALLBACK(JSBool) +SetSilentDownloadProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) +{ + nsIDOMSilentDownload *a = (nsIDOMSilentDownload*)JS_GetPrivate(cx, obj); + + // If there's no private data, this must be the prototype, so ignore + if (nsnull == a) { + return JS_TRUE; + } + + if (JSVAL_IS_INT(id)) { + switch(JSVAL_TO_INT(id)) { + case SILENTDOWNLOAD_BYTERANGE: + { + PRInt32 prop; + int32 temp; + if (JSVAL_IS_NUMBER(*vp) && JS_ValueToInt32(cx, *vp, &temp)) { + prop = (PRInt32)temp; + } + else { + JS_ReportError(cx, "Parameter must be a number"); + return JS_FALSE; + } + + a->SetByteRange(prop); + + break; + } + case SILENTDOWNLOAD_INTERVAL: + { + PRInt32 prop; + int32 temp; + if (JSVAL_IS_NUMBER(*vp) && JS_ValueToInt32(cx, *vp, &temp)) { + prop = (PRInt32)temp; + } + else { + JS_ReportError(cx, "Parameter must be a number"); + return JS_FALSE; + } + + a->SetInterval(prop); + + break; + } + default: + return nsJSUtils::nsCallJSScriptObjectSetProperty(a, cx, id, vp); + } + } + else { + return nsJSUtils::nsCallJSScriptObjectSetProperty(a, cx, id, vp); + } + + return PR_TRUE; +} + + +// +// SilentDownload finalizer +// +PR_STATIC_CALLBACK(void) +FinalizeSilentDownload(JSContext *cx, JSObject *obj) +{ + nsJSUtils::nsGenericFinalize(cx, obj); +} + + +// +// SilentDownload enumerate +// +PR_STATIC_CALLBACK(JSBool) +EnumerateSilentDownload(JSContext *cx, JSObject *obj) +{ + return nsJSUtils::nsGenericEnumerate(cx, obj); +} + + +// +// SilentDownload resolve +// +PR_STATIC_CALLBACK(JSBool) +ResolveSilentDownload(JSContext *cx, JSObject *obj, jsval id) +{ + return nsJSUtils::nsGenericResolve(cx, obj, id); +} + + +// +// Native method Startup +// +PR_STATIC_CALLBACK(JSBool) +SilentDownloadStartup(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + nsIDOMSilentDownload *nativeThis = (nsIDOMSilentDownload*)JS_GetPrivate(cx, obj); + JSBool rBool = JS_FALSE; + + *rval = JSVAL_NULL; + + // If there's no private data, this must be the prototype, so ignore + if (nsnull == nativeThis) { + return JS_TRUE; + } + + if (argc >= 0) { + + if (NS_OK != nativeThis->Startup()) { + return JS_FALSE; + } + + *rval = JSVAL_VOID; + } + else { + JS_ReportError(cx, "Function Startup requires 0 parameters"); + return JS_FALSE; + } + + return JS_TRUE; +} + + +// +// Native method Shutdown +// +PR_STATIC_CALLBACK(JSBool) +SilentDownloadShutdown(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + nsIDOMSilentDownload *nativeThis = (nsIDOMSilentDownload*)JS_GetPrivate(cx, obj); + JSBool rBool = JS_FALSE; + + *rval = JSVAL_NULL; + + // If there's no private data, this must be the prototype, so ignore + if (nsnull == nativeThis) { + return JS_TRUE; + } + + if (argc >= 0) { + + if (NS_OK != nativeThis->Shutdown()) { + return JS_FALSE; + } + + *rval = JSVAL_VOID; + } + else { + JS_ReportError(cx, "Function Shutdown requires 0 parameters"); + return JS_FALSE; + } + + return JS_TRUE; +} + + +// +// Native method Add +// +PR_STATIC_CALLBACK(JSBool) +SilentDownloadAdd(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + nsIDOMSilentDownload *nativeThis = (nsIDOMSilentDownload*)JS_GetPrivate(cx, obj); + JSBool rBool = JS_FALSE; + nsIDOMSilentDownloadTaskPtr b0; + + *rval = JSVAL_NULL; + + // If there's no private data, this must be the prototype, so ignore + if (nsnull == nativeThis) { + return JS_TRUE; + } + + if (argc >= 1) { + + if (JS_FALSE == nsJSUtils::nsConvertJSValToObject((nsISupports **)&b0, + kISilentDownloadTaskIID, + "SilentDownloadTask", + cx, + argv[0])) { + return JS_FALSE; + } + + if (NS_OK != nativeThis->Add(b0)) { + return JS_FALSE; + } + + *rval = JSVAL_VOID; + } + else { + JS_ReportError(cx, "Function Add requires 1 parameters"); + return JS_FALSE; + } + + return JS_TRUE; +} + + +// +// Native method Remove +// +PR_STATIC_CALLBACK(JSBool) +SilentDownloadRemove(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + nsIDOMSilentDownload *nativeThis = (nsIDOMSilentDownload*)JS_GetPrivate(cx, obj); + JSBool rBool = JS_FALSE; + nsIDOMSilentDownloadTaskPtr b0; + + *rval = JSVAL_NULL; + + // If there's no private data, this must be the prototype, so ignore + if (nsnull == nativeThis) { + return JS_TRUE; + } + + if (argc >= 1) { + + if (JS_FALSE == nsJSUtils::nsConvertJSValToObject((nsISupports **)&b0, + kISilentDownloadTaskIID, + "SilentDownloadTask", + cx, + argv[0])) { + return JS_FALSE; + } + + if (NS_OK != nativeThis->Remove(b0)) { + return JS_FALSE; + } + + *rval = JSVAL_VOID; + } + else { + JS_ReportError(cx, "Function Remove requires 1 parameters"); + return JS_FALSE; + } + + return JS_TRUE; +} + + +// +// Native method Find +// +PR_STATIC_CALLBACK(JSBool) +SilentDownloadFind(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + nsIDOMSilentDownload *nativeThis = (nsIDOMSilentDownload*)JS_GetPrivate(cx, obj); + JSBool rBool = JS_FALSE; + nsIDOMSilentDownloadTask* nativeRet; + nsAutoString b0; + + *rval = JSVAL_NULL; + + // If there's no private data, this must be the prototype, so ignore + if (nsnull == nativeThis) { + return JS_TRUE; + } + + if (argc >= 1) { + + nsJSUtils::nsConvertJSValToString(b0, cx, argv[0]); + + if (NS_OK != nativeThis->Find(b0, &nativeRet)) { + return JS_FALSE; + } + + nsJSUtils::nsConvertObjectToJSVal(nativeRet, cx, rval); + } + else { + JS_ReportError(cx, "Function Find requires 1 parameters"); + return JS_FALSE; + } + + return JS_TRUE; +} + + +/***********************************************************************/ +// +// class for SilentDownload +// +JSClass SilentDownloadClass = { + "SilentDownload", + JSCLASS_HAS_PRIVATE, + JS_PropertyStub, + JS_PropertyStub, + GetSilentDownloadProperty, + SetSilentDownloadProperty, + EnumerateSilentDownload, + ResolveSilentDownload, + JS_ConvertStub, + FinalizeSilentDownload +}; + + +// +// SilentDownload class properties +// +static JSPropertySpec SilentDownloadProperties[] = +{ + {"ByteRange", SILENTDOWNLOAD_BYTERANGE, JSPROP_ENUMERATE}, + {"Interval", SILENTDOWNLOAD_INTERVAL, JSPROP_ENUMERATE}, + {0} +}; + + +// +// SilentDownload class methods +// +static JSFunctionSpec SilentDownloadMethods[] = +{ + {"Startup", SilentDownloadStartup, 0}, + {"Shutdown", SilentDownloadShutdown, 0}, + {"Add", SilentDownloadAdd, 1}, + {"Remove", SilentDownloadRemove, 1}, + {"Find", SilentDownloadFind, 1}, + {0} +}; + + +// +// SilentDownload constructor +// +PR_STATIC_CALLBACK(JSBool) +SilentDownload(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + return JS_FALSE; +} + + +// +// SilentDownload class initialization +// +nsresult NS_InitSilentDownloadClass(nsIScriptContext *aContext, void **aPrototype) +{ + JSContext *jscontext = (JSContext *)aContext->GetNativeContext(); + JSObject *proto = nsnull; + JSObject *constructor = nsnull; + JSObject *parent_proto = nsnull; + JSObject *global = JS_GetGlobalObject(jscontext); + jsval vp; + + if ((PR_TRUE != JS_LookupProperty(jscontext, global, "SilentDownload", &vp)) || + !JSVAL_IS_OBJECT(vp) || + ((constructor = JSVAL_TO_OBJECT(vp)) == nsnull) || + (PR_TRUE != JS_LookupProperty(jscontext, JSVAL_TO_OBJECT(vp), "prototype", &vp)) || + !JSVAL_IS_OBJECT(vp)) { + + proto = JS_InitClass(jscontext, // context + global, // global object + parent_proto, // parent proto + &SilentDownloadClass, // JSClass + SilentDownload, // JSNative ctor + 0, // ctor args + SilentDownloadProperties, // proto props + SilentDownloadMethods, // proto funcs + nsnull, // ctor props (static) + nsnull); // ctor funcs (static) + if (nsnull == proto) { + return NS_ERROR_FAILURE; + } + + } + else if ((nsnull != constructor) && JSVAL_IS_OBJECT(vp)) { + proto = JSVAL_TO_OBJECT(vp); + } + else { + return NS_ERROR_FAILURE; + } + + if (aPrototype) { + *aPrototype = proto; + } + return NS_OK; +} + + +// +// Method for creating a new SilentDownload JavaScript object +// +extern "C" NS_DOM nsresult NS_NewScriptSilentDownload(nsIScriptContext *aContext, nsISupports *aSupports, nsISupports *aParent, void **aReturn) +{ + NS_PRECONDITION(nsnull != aContext && nsnull != aSupports && nsnull != aReturn, "null argument to NS_NewScriptSilentDownload"); + JSObject *proto; + JSObject *parent; + nsIScriptObjectOwner *owner; + JSContext *jscontext = (JSContext *)aContext->GetNativeContext(); + nsresult result = NS_OK; + nsIDOMSilentDownload *aSilentDownload; + + if (nsnull == aParent) { + parent = nsnull; + } + else if (NS_OK == aParent->QueryInterface(kIScriptObjectOwnerIID, (void**)&owner)) { + if (NS_OK != owner->GetScriptObject(aContext, (void **)&parent)) { + NS_RELEASE(owner); + return NS_ERROR_FAILURE; + } + NS_RELEASE(owner); + } + else { + return NS_ERROR_FAILURE; + } + + if (NS_OK != NS_InitSilentDownloadClass(aContext, (void **)&proto)) { + return NS_ERROR_FAILURE; + } + + result = aSupports->QueryInterface(kISilentDownloadIID, (void **)&aSilentDownload); + if (NS_OK != result) { + return result; + } + + // create a js object for this class + *aReturn = JS_NewObject(jscontext, &SilentDownloadClass, proto, parent); + if (nsnull != *aReturn) { + // connect the native object to the js object + JS_SetPrivate(jscontext, (JSObject *)*aReturn, aSilentDownload); + } + else { + NS_RELEASE(aSilentDownload); + return NS_ERROR_FAILURE; + } + + return NS_OK; +} diff --git a/silentdl/nsJSSilentDownloadTask.cpp b/silentdl/nsJSSilentDownloadTask.cpp new file mode 100644 index 00000000000..9aed4b83e25 --- /dev/null +++ b/silentdl/nsJSSilentDownloadTask.cpp @@ -0,0 +1,690 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ +/* AUTO-GENERATED. DO NOT EDIT!!! */ + +#include "jsapi.h" +#include "nsJSUtils.h" +#include "nscore.h" +#include "nsIScriptContext.h" +#include "nsIJSScriptObject.h" +#include "nsIScriptObjectOwner.h" +#include "nsIScriptGlobalObject.h" +#include "nsIPtr.h" +#include "nsString.h" +#include "nsIDOMSilentDownloadTask.h" +#include "nsIScriptNameSpaceManager.h" +#include "nsRepository.h" +#include "nsDOMCID.h" + + +static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID); +static NS_DEFINE_IID(kIJSScriptObjectIID, NS_IJSSCRIPTOBJECT_IID); +static NS_DEFINE_IID(kIScriptGlobalObjectIID, NS_ISCRIPTGLOBALOBJECT_IID); +static NS_DEFINE_IID(kISilentDownloadTaskIID, NS_IDOMSILENTDOWNLOADTASK_IID); + +NS_DEF_PTR(nsIDOMSilentDownloadTask); + +// +// SilentDownloadTask property ids +// +enum SilentDownloadTask_slots { + SILENTDOWNLOADTASK_ID = -1, + SILENTDOWNLOADTASK_URL = -2, + SILENTDOWNLOADTASK_SCRIPT = -3, + SILENTDOWNLOADTASK_STATE = -4, + SILENTDOWNLOADTASK_ERRORMSG = -5, + SILENTDOWNLOADTASK_NEXTBYTE = -6, + SILENTDOWNLOADTASK_OUTFILE = -7 +}; + +/***********************************************************************/ +// +// SilentDownloadTask Properties Getter +// +PR_STATIC_CALLBACK(JSBool) +GetSilentDownloadTaskProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) +{ + nsIDOMSilentDownloadTask *a = (nsIDOMSilentDownloadTask*)JS_GetPrivate(cx, obj); + + // If there's no private data, this must be the prototype, so ignore + if (nsnull == a) { + return JS_TRUE; + } + + if (JSVAL_IS_INT(id)) { + switch(JSVAL_TO_INT(id)) { + case SILENTDOWNLOADTASK_ID: + { + nsAutoString prop; + if (NS_OK == a->GetId(prop)) { + nsJSUtils::nsConvertStringToJSVal(prop, cx, vp); + } + else { + return JS_FALSE; + } + break; + } + case SILENTDOWNLOADTASK_URL: + { + nsAutoString prop; + if (NS_OK == a->GetUrl(prop)) { + nsJSUtils::nsConvertStringToJSVal(prop, cx, vp); + } + else { + return JS_FALSE; + } + break; + } + case SILENTDOWNLOADTASK_SCRIPT: + { + nsAutoString prop; + if (NS_OK == a->GetScript(prop)) { + nsJSUtils::nsConvertStringToJSVal(prop, cx, vp); + } + else { + return JS_FALSE; + } + break; + } + case SILENTDOWNLOADTASK_STATE: + { + PRInt32 prop; + if (NS_OK == a->GetState(&prop)) { + *vp = INT_TO_JSVAL(prop); + } + else { + return JS_FALSE; + } + break; + } + case SILENTDOWNLOADTASK_ERRORMSG: + { + nsAutoString prop; + if (NS_OK == a->GetErrorMsg(prop)) { + nsJSUtils::nsConvertStringToJSVal(prop, cx, vp); + } + else { + return JS_FALSE; + } + break; + } + case SILENTDOWNLOADTASK_NEXTBYTE: + { + PRInt32 prop; + if (NS_OK == a->GetNextByte(&prop)) { + *vp = INT_TO_JSVAL(prop); + } + else { + return JS_FALSE; + } + break; + } + case SILENTDOWNLOADTASK_OUTFILE: + { + nsAutoString prop; + if (NS_OK == a->GetOutFile(prop)) { + nsJSUtils::nsConvertStringToJSVal(prop, cx, vp); + } + else { + return JS_FALSE; + } + break; + } + default: + return nsJSUtils::nsCallJSScriptObjectGetProperty(a, cx, id, vp); + } + } + else { + return nsJSUtils::nsCallJSScriptObjectGetProperty(a, cx, id, vp); + } + + return PR_TRUE; +} + +/***********************************************************************/ +// +// SilentDownloadTask Properties Setter +// +PR_STATIC_CALLBACK(JSBool) +SetSilentDownloadTaskProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) +{ + nsIDOMSilentDownloadTask *a = (nsIDOMSilentDownloadTask*)JS_GetPrivate(cx, obj); + + // If there's no private data, this must be the prototype, so ignore + if (nsnull == a) { + return JS_TRUE; + } + + if (JSVAL_IS_INT(id)) { + switch(JSVAL_TO_INT(id)) { + case SILENTDOWNLOADTASK_STATE: + { + PRInt32 prop; + int32 temp; + if (JSVAL_IS_NUMBER(*vp) && JS_ValueToInt32(cx, *vp, &temp)) { + prop = (PRInt32)temp; + } + else { + JS_ReportError(cx, "Parameter must be a number"); + return JS_FALSE; + } + + a->SetState(prop); + + break; + } + case SILENTDOWNLOADTASK_ERRORMSG: + { + nsAutoString prop; + nsJSUtils::nsConvertJSValToString(prop, cx, *vp); + + a->SetErrorMsg(prop); + + break; + } + case SILENTDOWNLOADTASK_NEXTBYTE: + { + PRInt32 prop; + int32 temp; + if (JSVAL_IS_NUMBER(*vp) && JS_ValueToInt32(cx, *vp, &temp)) { + prop = (PRInt32)temp; + } + else { + JS_ReportError(cx, "Parameter must be a number"); + return JS_FALSE; + } + + a->SetNextByte(prop); + + break; + } + default: + return nsJSUtils::nsCallJSScriptObjectSetProperty(a, cx, id, vp); + } + } + else { + return nsJSUtils::nsCallJSScriptObjectSetProperty(a, cx, id, vp); + } + + return PR_TRUE; +} + + +// +// SilentDownloadTask finalizer +// +PR_STATIC_CALLBACK(void) +FinalizeSilentDownloadTask(JSContext *cx, JSObject *obj) +{ + nsJSUtils::nsGenericFinalize(cx, obj); +} + + +// +// SilentDownloadTask enumerate +// +PR_STATIC_CALLBACK(JSBool) +EnumerateSilentDownloadTask(JSContext *cx, JSObject *obj) +{ + return nsJSUtils::nsGenericEnumerate(cx, obj); +} + + +// +// SilentDownloadTask resolve +// +PR_STATIC_CALLBACK(JSBool) +ResolveSilentDownloadTask(JSContext *cx, JSObject *obj, jsval id) +{ + return nsJSUtils::nsGenericResolve(cx, obj, id); +} + + +// +// Native method Init +// +PR_STATIC_CALLBACK(JSBool) +SilentDownloadTaskInit(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + nsIDOMSilentDownloadTask *nativeThis = (nsIDOMSilentDownloadTask*)JS_GetPrivate(cx, obj); + JSBool rBool = JS_FALSE; + nsAutoString b0; + nsAutoString b1; + nsAutoString b2; + + *rval = JSVAL_NULL; + + // If there's no private data, this must be the prototype, so ignore + if (nsnull == nativeThis) { + return JS_TRUE; + } + + if (argc >= 3) { + + nsJSUtils::nsConvertJSValToString(b0, cx, argv[0]); + + nsJSUtils::nsConvertJSValToString(b1, cx, argv[1]); + + nsJSUtils::nsConvertJSValToString(b2, cx, argv[2]); + + if (NS_OK != nativeThis->Init(b0, b1, b2)) { + return JS_FALSE; + } + + *rval = JSVAL_VOID; + } + else { + JS_ReportError(cx, "Function Init requires 3 parameters"); + return JS_FALSE; + } + + return JS_TRUE; +} + + +// +// Native method Remove +// +PR_STATIC_CALLBACK(JSBool) +SilentDownloadTaskRemove(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + nsIDOMSilentDownloadTask *nativeThis = (nsIDOMSilentDownloadTask*)JS_GetPrivate(cx, obj); + JSBool rBool = JS_FALSE; + + *rval = JSVAL_NULL; + + // If there's no private data, this must be the prototype, so ignore + if (nsnull == nativeThis) { + return JS_TRUE; + } + + if (argc >= 0) { + + if (NS_OK != nativeThis->Remove()) { + return JS_FALSE; + } + + *rval = JSVAL_VOID; + } + else { + JS_ReportError(cx, "Function Remove requires 0 parameters"); + return JS_FALSE; + } + + return JS_TRUE; +} + + +// +// Native method Suspend +// +PR_STATIC_CALLBACK(JSBool) +SilentDownloadTaskSuspend(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + nsIDOMSilentDownloadTask *nativeThis = (nsIDOMSilentDownloadTask*)JS_GetPrivate(cx, obj); + JSBool rBool = JS_FALSE; + + *rval = JSVAL_NULL; + + // If there's no private data, this must be the prototype, so ignore + if (nsnull == nativeThis) { + return JS_TRUE; + } + + if (argc >= 0) { + + if (NS_OK != nativeThis->Suspend()) { + return JS_FALSE; + } + + *rval = JSVAL_VOID; + } + else { + JS_ReportError(cx, "Function Suspend requires 0 parameters"); + return JS_FALSE; + } + + return JS_TRUE; +} + + +// +// Native method Resume +// +PR_STATIC_CALLBACK(JSBool) +SilentDownloadTaskResume(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + nsIDOMSilentDownloadTask *nativeThis = (nsIDOMSilentDownloadTask*)JS_GetPrivate(cx, obj); + JSBool rBool = JS_FALSE; + + *rval = JSVAL_NULL; + + // If there's no private data, this must be the prototype, so ignore + if (nsnull == nativeThis) { + return JS_TRUE; + } + + if (argc >= 0) { + + if (NS_OK != nativeThis->Resume()) { + return JS_FALSE; + } + + *rval = JSVAL_VOID; + } + else { + JS_ReportError(cx, "Function Resume requires 0 parameters"); + return JS_FALSE; + } + + return JS_TRUE; +} + + +// +// Native method DownloadNow +// +PR_STATIC_CALLBACK(JSBool) +SilentDownloadTaskDownloadNow(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + nsIDOMSilentDownloadTask *nativeThis = (nsIDOMSilentDownloadTask*)JS_GetPrivate(cx, obj); + JSBool rBool = JS_FALSE; + + *rval = JSVAL_NULL; + + // If there's no private data, this must be the prototype, so ignore + if (nsnull == nativeThis) { + return JS_TRUE; + } + + if (argc >= 0) { + + if (NS_OK != nativeThis->DownloadNow()) { + return JS_FALSE; + } + + *rval = JSVAL_VOID; + } + else { + JS_ReportError(cx, "Function DownloadNow requires 0 parameters"); + return JS_FALSE; + } + + return JS_TRUE; +} + + +// +// Native method DownloadSelf +// +PR_STATIC_CALLBACK(JSBool) +SilentDownloadTaskDownloadSelf(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + nsIDOMSilentDownloadTask *nativeThis = (nsIDOMSilentDownloadTask*)JS_GetPrivate(cx, obj); + JSBool rBool = JS_FALSE; + PRInt32 b0; + + *rval = JSVAL_NULL; + + // If there's no private data, this must be the prototype, so ignore + if (nsnull == nativeThis) { + return JS_TRUE; + } + + if (argc >= 1) { + + if (!JS_ValueToInt32(cx, argv[0], (int32 *)&b0)) { + JS_ReportError(cx, "Parameter must be a number"); + return JS_FALSE; + } + + if (NS_OK != nativeThis->DownloadSelf(b0)) { + return JS_FALSE; + } + + *rval = JSVAL_VOID; + } + else { + JS_ReportError(cx, "Function DownloadSelf requires 1 parameters"); + return JS_FALSE; + } + + return JS_TRUE; +} + + +/***********************************************************************/ +// +// class for SilentDownloadTask +// +JSClass SilentDownloadTaskClass = { + "SilentDownloadTask", + JSCLASS_HAS_PRIVATE, + JS_PropertyStub, + JS_PropertyStub, + GetSilentDownloadTaskProperty, + SetSilentDownloadTaskProperty, + EnumerateSilentDownloadTask, + ResolveSilentDownloadTask, + JS_ConvertStub, + FinalizeSilentDownloadTask +}; + + +// +// SilentDownloadTask class properties +// +static JSPropertySpec SilentDownloadTaskProperties[] = +{ + {"id", SILENTDOWNLOADTASK_ID, JSPROP_ENUMERATE | JSPROP_READONLY}, + {"url", SILENTDOWNLOADTASK_URL, JSPROP_ENUMERATE | JSPROP_READONLY}, + {"script", SILENTDOWNLOADTASK_SCRIPT, JSPROP_ENUMERATE | JSPROP_READONLY}, + {"state", SILENTDOWNLOADTASK_STATE, JSPROP_ENUMERATE}, + {"errorMsg", SILENTDOWNLOADTASK_ERRORMSG, JSPROP_ENUMERATE}, + {"nextByte", SILENTDOWNLOADTASK_NEXTBYTE, JSPROP_ENUMERATE}, + {"outFile", SILENTDOWNLOADTASK_OUTFILE, JSPROP_ENUMERATE | JSPROP_READONLY}, + {0} +}; + + +// +// SilentDownloadTask class methods +// +static JSFunctionSpec SilentDownloadTaskMethods[] = +{ + {"Init", SilentDownloadTaskInit, 3}, + {"Remove", SilentDownloadTaskRemove, 0}, + {"Suspend", SilentDownloadTaskSuspend, 0}, + {"Resume", SilentDownloadTaskResume, 0}, + {"DownloadNow", SilentDownloadTaskDownloadNow, 0}, + {"DownloadSelf", SilentDownloadTaskDownloadSelf, 1}, + {0} +}; + + +// +// SilentDownloadTask constructor +// +PR_STATIC_CALLBACK(JSBool) +SilentDownloadTask(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + nsresult result; + nsIID classID; + nsIScriptContext* context = (nsIScriptContext*)JS_GetContextPrivate(cx); + nsIScriptNameSpaceManager* manager; + nsIDOMSilentDownloadTask *nativeThis; + nsIScriptObjectOwner *owner = nsnull; + + static NS_DEFINE_IID(kIDOMSilentDownloadTaskIID, NS_IDOMSILENTDOWNLOADTASK_IID); + + result = context->GetNameSpaceManager(&manager); + if (NS_OK != result) { + return JS_FALSE; + } + + result = manager->LookupName("SilentDownloadTask", PR_TRUE, classID); + NS_RELEASE(manager); + if (NS_OK != result) { + return JS_FALSE; + } + + result = nsRepository::CreateInstance(classID, + nsnull, + kIDOMSilentDownloadTaskIID, + (void **)&nativeThis); + if (NS_OK != result) { + return JS_FALSE; + } + + // XXX We should be calling Init() on the instance + + result = nativeThis->QueryInterface(kIScriptObjectOwnerIID, (void **)&owner); + if (NS_OK != result) { + NS_RELEASE(nativeThis); + return JS_FALSE; + } + + owner->SetScriptObject((void *)obj); + JS_SetPrivate(cx, obj, nativeThis); + + NS_RELEASE(owner); + return JS_TRUE; +} + +// +// SilentDownloadTask class initialization +// +nsresult NS_InitSilentDownloadTaskClass(nsIScriptContext *aContext, void **aPrototype) +{ + JSContext *jscontext = (JSContext *)aContext->GetNativeContext(); + JSObject *proto = nsnull; + JSObject *constructor = nsnull; + JSObject *parent_proto = nsnull; + JSObject *global = JS_GetGlobalObject(jscontext); + jsval vp; + + if ((PR_TRUE != JS_LookupProperty(jscontext, global, "SilentDownloadTask", &vp)) || + !JSVAL_IS_OBJECT(vp) || + ((constructor = JSVAL_TO_OBJECT(vp)) == nsnull) || + (PR_TRUE != JS_LookupProperty(jscontext, JSVAL_TO_OBJECT(vp), "prototype", &vp)) || + !JSVAL_IS_OBJECT(vp)) { + + proto = JS_InitClass(jscontext, // context + global, // global object + parent_proto, // parent proto + &SilentDownloadTaskClass, // JSClass + SilentDownloadTask, // JSNative ctor + 0, // ctor args + SilentDownloadTaskProperties, // proto props + SilentDownloadTaskMethods, // proto funcs + nsnull, // ctor props (static) + nsnull); // ctor funcs (static) + if (nsnull == proto) { + return NS_ERROR_FAILURE; + } + + if ((PR_TRUE == JS_LookupProperty(jscontext, global, "SilentDownloadTask", &vp)) && + JSVAL_IS_OBJECT(vp) && + ((constructor = JSVAL_TO_OBJECT(vp)) != nsnull)) { + vp = INT_TO_JSVAL(nsIDOMSilentDownloadTask::SDL_NOT_INITED); + JS_SetProperty(jscontext, constructor, "SDL_NOT_INITED", &vp); + + vp = INT_TO_JSVAL(nsIDOMSilentDownloadTask::SDL_NOT_ADDED); + JS_SetProperty(jscontext, constructor, "SDL_NOT_ADDED", &vp); + + vp = INT_TO_JSVAL(nsIDOMSilentDownloadTask::SDL_STARTED); + JS_SetProperty(jscontext, constructor, "SDL_STARTED", &vp); + + vp = INT_TO_JSVAL(nsIDOMSilentDownloadTask::SDL_SUSPENDED); + JS_SetProperty(jscontext, constructor, "SDL_SUSPENDED", &vp); + + vp = INT_TO_JSVAL(nsIDOMSilentDownloadTask::SDL_COMPLETED); + JS_SetProperty(jscontext, constructor, "SDL_COMPLETED", &vp); + + vp = INT_TO_JSVAL(nsIDOMSilentDownloadTask::SDL_DOWNLOADING_NOW); + JS_SetProperty(jscontext, constructor, "SDL_DOWNLOADING_NOW", &vp); + + vp = INT_TO_JSVAL(nsIDOMSilentDownloadTask::SDL_ERROR); + JS_SetProperty(jscontext, constructor, "SDL_ERROR", &vp); + + } + + } + else if ((nsnull != constructor) && JSVAL_IS_OBJECT(vp)) { + proto = JSVAL_TO_OBJECT(vp); + } + else { + return NS_ERROR_FAILURE; + } + + if (aPrototype) { + *aPrototype = proto; + } + return NS_OK; +} + + +// +// Method for creating a new SilentDownloadTask JavaScript object +// +extern "C" NS_DOM nsresult NS_NewScriptSilentDownloadTask(nsIScriptContext *aContext, nsISupports *aSupports, nsISupports *aParent, void **aReturn) +{ + NS_PRECONDITION(nsnull != aContext && nsnull != aSupports && nsnull != aReturn, "null argument to NS_NewScriptSilentDownloadTask"); + JSObject *proto; + JSObject *parent; + nsIScriptObjectOwner *owner; + JSContext *jscontext = (JSContext *)aContext->GetNativeContext(); + nsresult result = NS_OK; + nsIDOMSilentDownloadTask *aSilentDownloadTask; + + if (nsnull == aParent) { + parent = nsnull; + } + else if (NS_OK == aParent->QueryInterface(kIScriptObjectOwnerIID, (void**)&owner)) { + if (NS_OK != owner->GetScriptObject(aContext, (void **)&parent)) { + NS_RELEASE(owner); + return NS_ERROR_FAILURE; + } + NS_RELEASE(owner); + } + else { + return NS_ERROR_FAILURE; + } + + if (NS_OK != NS_InitSilentDownloadTaskClass(aContext, (void **)&proto)) { + return NS_ERROR_FAILURE; + } + + result = aSupports->QueryInterface(kISilentDownloadTaskIID, (void **)&aSilentDownloadTask); + if (NS_OK != result) { + return result; + } + + // create a js object for this class + *aReturn = JS_NewObject(jscontext, &SilentDownloadTaskClass, proto, parent); + if (nsnull != *aReturn) { + // connect the native object to the js object + JS_SetPrivate(jscontext, (JSObject *)*aReturn, aSilentDownloadTask); + } + else { + NS_RELEASE(aSilentDownloadTask); + return NS_ERROR_FAILURE; + } + + return NS_OK; +} diff --git a/silentdl/nsSilentDownload.cpp b/silentdl/nsSilentDownload.cpp new file mode 100644 index 00000000000..46bb198e63c --- /dev/null +++ b/silentdl/nsSilentDownload.cpp @@ -0,0 +1,1499 @@ + +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + * the License for the specific language governing rights and limitations + * under the License. + * + * The Original Code is Mozilla Communicator client code. + * + * The Initial Developer of the Original Code is Netscape Communications + * Corporation. Portions created by Netscape are Copyright (C) 1998 + * Netscape Communications Corporation. All Rights Reserved. + */ + + +#include "nsSilentDownload.h" +#include "nsSilentDownloadPrivate.h" + +#include "nscore.h" +#include "nsIFactory.h" +#include "nsISupports.h" + +#include "nsRepository.h" + +#include "nsIScriptObjectOwner.h" +#include "nsIScriptGlobalObject.h" + +#include "pratom.h" +#include "prmem.h" +#include "prio.h" +#include "mkutils.h" +#include "prefapi.h" + +#include "nsIURL.h" +#include "nsINetlibURL.h" +#include "nsINetService.h" +#include "nsIInputStream.h" +#include "nsIStreamListener.h" + +#include "nsIDOMSilentDownload.h" +#include "nsIDOMSilentDownloadTask.h" + +/* For Javascript Namespace Access */ +#include "nsDOMCID.h" +#include "nsIServiceManager.h" +#include "nsINameSpaceManager.h" +#include "nsIScriptNameSetRegistry.h" +#include "nsIScriptNameSpaceManager.h" +#include "nsIScriptExternalNameSet.h" + +#include "nsITimer.h" +#include "nsITimerCallback.h" + +#include "nsIBrowserWindow.h" +#include "nsIWebShell.h" + +// Globals + +static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); +static NS_DEFINE_IID(kIFactoryIID, NS_IFACTORY_IID); +static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID); + +static NS_DEFINE_IID(kIScriptNameSetRegistryIID, NS_ISCRIPTNAMESETREGISTRY_IID); +static NS_DEFINE_IID(kCScriptNameSetRegistryCID, NS_SCRIPT_NAMESET_REGISTRY_CID); +static NS_DEFINE_IID(kIScriptExternalNameSetIID, NS_ISCRIPTEXTERNALNAMESET_IID); + +static NS_DEFINE_IID(kInetServiceIID, NS_INETSERVICE_IID); +static NS_DEFINE_IID(kInetServiceCID, NS_NETSERVICE_CID); +static NS_DEFINE_IID(kInetLibURLIID, NS_INETLIBURL_IID); +static NS_DEFINE_IID(kIStreamListenerIID, NS_ISTREAMLISTENER_IID); + +static NS_DEFINE_IID(kBrowserWindowCID, NS_BROWSER_WINDOW_CID); +static NS_DEFINE_IID(kIBrowserWindowIID, NS_IBROWSER_WINDOW_IID); + +static NS_DEFINE_IID(kISilentDownloadTaskIID, NS_IDOMSILENTDOWNLOADTASK_IID); +static NS_DEFINE_IID(kISilentDownloadIID, NS_IDOMSILENTDOWNLOAD_IID); +static NS_DEFINE_IID(kSilentDownloadCID, NS_SilentDownload_CID); +static NS_DEFINE_IID(kSilentDownloadTaskCID, NS_SilentDownloadTask_CID); +static NS_DEFINE_IID(kSilentDownloadFactoryCID, NS_SilentDownloadFactory_CID); +static NS_DEFINE_IID(kSilentDownloadTaskFactoryCID, NS_SilentDownloadTaskFactory_CID); + + + + +static PRInt32 gLockCnt = 0; +static PRInt32 gInstanceCnt = 0; + +static nsITimer *gTaskTimer = nsnull; +static nsINetService *gInetService = nsnull; +static SDL_TaskList *gTasks = nsnull; +static SDL_TaskList *gNextReadyTask = nsnull; + +static PRInt32 gByteRange = 0; +static PRInt32 gInterval = 0xFFFF; + +///////////////////////////////////////////////////////////////////////// +// static helper meathods +///////////////////////////////////////////////////////////////////////// + + +static int32 +GetCurrentSize(char *file) +{ + struct stat in_stat; + int stat_result = -1; + stat_result = stat(file, &in_stat); + if (stat_result == 0) { + return in_stat.st_size; + } + return 0; +} + +static char* +AssureDir(char* path) +{ + char *autoupdt_dir = PR_smprintf("%sSilentDL", path); + if (PR_SUCCESS != PR_Access(autoupdt_dir, PR_ACCESS_WRITE_OK)) { + if ((PR_MkDir(autoupdt_dir, 0777)) < 0) { + /* Creation of directory failed. Don't do SilentDownload. */ + return NULL; + } + } + return autoupdt_dir; +} + +static void +GetSilentDownloadDirectory(char* directory) +{ + if (PREF_OK != PREF_CopyCharPref("SilentDownload.directory", (char**)directory)) + { + directory = NULL; + } + else + { + if ((directory) && (XP_STRCMP(directory, "") == 0)) + { + directory = NULL; + } + } +} + +static void +GetSilentDownloadDefaults(XP_Bool* enabled, PRInt32 *bytes_range, PRInt32 *interval) +{ + PREF_GetBoolPref( "SilentDownload.enabled", enabled); + if (!enabled) + return; + + + if (PREF_OK != PREF_GetIntPref("SilentDownload.range", (long*)*bytes_range)) + { + *bytes_range = 3000; + } + + if (PREF_OK != PREF_GetIntPref("SilentDownload.interval", (long*)*interval)) + { + *interval = 10000; + } +} + + + +static char* +CreateOutFileLocation(char* url, char* directory) +{ + /* FIX: The outfile that this functions creates must be compatible with + PR_Open! + */ + char* outfile = NULL; + + char* autoupdt_dir; + char* slash; + char* filename; + char Path[1024 + 1]; + + slash = PL_strrchr(url, '/'); + if (slash != NULL) + { + filename = ++slash; + } + else + { + filename = "prg"; + } + + +#ifdef XP_UNIX + + if (directory) + { + /* what if the directory already has a / appended to it? */ + PR_snprintf( Path, MAXPATHLEN, "%s/", directory); + PR_FREEIF(directory); + } + else + { + directory = getenv("MOZILLA_HOME"); + if (directory) + { + PR_snprintf( Path, MAXPATHLEN, "%s/", directory); + } + else + { + extern void fe_GetProgramDirectory( char *, int ); + //fe_GetProgramDirectory( Path, MAXPATHLEN-1 ); + } + } + + autoupdt_dir = AssureDir(Path); + + if (autoupdt_dir != NULL) + { + outfile = PR_smprintf("%s/%s", autoupdt_dir, filename); + PR_FREEIF(autoupdt_dir); + } + +#elif defined(WIN32) + + if (directory) + { + PR_snprintf( Path, MAXPATHLEN, "%s\\", directory); + PR_FREEIF(directory); + } + else + { + PR_snprintf( Path, MAXPATHLEN, "C:\\Temp\\"); + extern char * FE_GetProgramDirectory(char *buffer, int length); + //FE_GetProgramDirectory( Path, _MAX_PATH ); + } + + autoupdt_dir = AssureDir(Path); + if (autoupdt_dir != NULL) + { + outfile = PR_smprintf("%s\\%s", autoupdt_dir, filename); + PR_FREEIF(autoupdt_dir); + } + +#elif defined(MAC) + + /* XXX: Fix it for Mac with the correct folder */ + + if (directory) + { + PR_snprintf( Path, MAXPATHLEN, "%s:", directory); + PR_FREEIF(directory); + } + else + { + directory = XP_TempDirName(); + PR_snprintf( Path, MAXPATHLEN, "%s:", directory); + } + + outfile = PR_smprintf("%s:%s", Path, filename); + +#else + outfile = NULL; +#endif + + return outfile; +} + + + + +///////////////////////////////////////////////////////////////////////// +// nsSilentDownloadManager +///////////////////////////////////////////////////////////////////////// + +nsSilentDownloadManager::nsSilentDownloadManager() +{ + mScriptObject = nsnull; + + PR_AtomicIncrement(&gInstanceCnt); + NS_INIT_REFCNT(); +} + + +nsSilentDownloadManager::~nsSilentDownloadManager() +{ + PR_AtomicDecrement(&gInstanceCnt); +} + +nsresult +nsSilentDownloadManager::LoadAllTasks(void) +{ + gNextReadyTask = gTasks; + return NS_OK; +} + +nsresult +nsSilentDownloadManager::StoreAllTasks(void) +{ + return NS_OK; +} + + +NS_IMPL_ADDREF(nsSilentDownloadManager) +NS_IMPL_RELEASE(nsSilentDownloadManager) + + + +NS_IMETHODIMP +nsSilentDownloadManager::QueryInterface(REFNSIID aIID,void** aInstancePtr) +{ + if (aInstancePtr == NULL) + { + return NS_ERROR_NULL_POINTER; + } + + // Always NULL result, in case of failure + *aInstancePtr = NULL; + + + if ( aIID.Equals(kISilentDownloadIID) ) + { + nsIDOMSilentDownload* tmp = this; + *aInstancePtr = (void*)tmp; + AddRef(); + return NS_OK; + } + else if ( aIID.Equals(kIScriptObjectOwnerIID)) + { + nsIScriptObjectOwner* tmp = this; + *aInstancePtr = (void*)tmp; + AddRef(); + return NS_OK; + } + else if ( aIID.Equals(kISupportsIID) ) + { + + nsIDOMSilentDownload* tmp1 = this; + nsISupports* tmp2 = tmp1; + + *aInstancePtr = (void*)tmp2; + AddRef(); + return NS_OK; + } + + return NS_NOINTERFACE; +} + + +NS_IMETHODIMP +nsSilentDownloadManager::GetScriptObject(nsIScriptContext *aContext, void** aScriptObject) +{ + nsresult res = NS_OK; + + if (nsnull == mScriptObject) + { + nsIScriptGlobalObject *global = aContext->GetGlobalObject(); + + res = NS_NewScriptSilentDownload(aContext, (nsISupports *)(nsIDOMSilentDownload*)this, global, (void**)&mScriptObject); + + NS_IF_RELEASE(global); + } + + + *aScriptObject = mScriptObject; + return res; +} + +NS_IMETHODIMP +nsSilentDownloadManager::SetScriptObject(void *aScriptObject) +{ + mScriptObject = aScriptObject; + return NS_OK; +} + +void nsSilentDownloadManager::Notify(nsITimer * aTimer) +{ + /* Fix: maybe some additional check on gNextReadyTask */ + + if (gTasks != NULL && gNextReadyTask != NULL) + { + if (! gInetService->AreThereActiveConnections() ) + { + gNextReadyTask->task->DownloadSelf(gByteRange); + + if (gNextReadyTask->next == NULL) + { + gNextReadyTask = gTasks; + } + else + { + gNextReadyTask = gNextReadyTask->next; + } + } + } + + NS_RELEASE(gTaskTimer); + + if (NS_OK == NS_NewTimer(&gTaskTimer)) + gTaskTimer->Init(this, gInterval); +} + +NS_IMETHODIMP +nsSilentDownloadManager::Startup() +{ + PRBool enabled; + + GetSilentDownloadDefaults(&enabled, &gByteRange, &gInterval); + + if (enabled == PR_FALSE) + return -1; + + + /***************************************/ + /* Add us to the Javascript Name Space */ + /***************************************/ + + nsIScriptNameSetRegistry *registry; + nsresult result = nsServiceManager::GetService(kCScriptNameSetRegistryCID, + kIScriptNameSetRegistryIID, + (nsISupports **)®istry); + if (NS_OK == result) + { + nsSilentDownloadNameSet* nameSet = new nsSilentDownloadNameSet(); + registry->AddExternalNameSet(nameSet); + /* FIX - do we need to release this service? When we do, it get deleted,and our name is lost. */ + } + + /************************************************************************/ + /* Create an instance of INet so that we can see if the network is busy */ + /************************************************************************/ + + result = nsRepository::CreateInstance(kInetServiceCID, + nsnull, + kInetServiceIID, + (void**)&gInetService); + + if (result != NS_OK) + return result; + + /************************/ + /* Load all saved tasks */ + /************************/ + result = LoadAllTasks(); + + if (result != NS_OK) + return result; + + /************************/ + /* Start Timer */ + /************************/ + + if (NS_OK == NS_NewTimer(&gTaskTimer)) + gTaskTimer->Init(this, gInterval); + + return result; +} + +NS_IMETHODIMP +nsSilentDownloadManager::Shutdown() +{ + StoreAllTasks(); + return NS_OK; +} + +NS_IMETHODIMP +nsSilentDownloadManager::Add(nsIDOMSilentDownloadTask* aTask) +{ + + if (aTask == NULL) + return NS_ERROR_FAILURE; + + /* Check to see if we already have this task in our list */ + SDL_TaskList *node = gTasks; + nsString nodeIDString; + nsString addIDString; + + aTask->GetId(addIDString); + + while (node != NULL) + { + node->task->GetId(nodeIDString); + + if (nodeIDString == addIDString) + { + /*we already have this ID in our list, ignore */ + return NS_OK; + } + + node = node->next; + } + + /* add the task to our list */ + SDL_TaskList* taskNode = (SDL_TaskList*)PR_MALLOC(sizeof(SDL_TaskList)); + + aTask->AddRef(); + + taskNode->next = gTasks; + taskNode->task = aTask; + gTasks = taskNode; + + /* Lets set the next task to run to this one */ + gNextReadyTask = taskNode; + + aTask->SetState(nsIDOMSilentDownloadTask::SDL_STARTED); + + return NS_OK; +} +NS_IMETHODIMP +nsSilentDownloadManager::Remove(nsIDOMSilentDownloadTask* aTask) +{ + if (aTask == NULL) + return NS_ERROR_FAILURE; + + /* Remove from our list */ + + SDL_TaskList *node = gTasks; + SDL_TaskList *lastnode = gTasks; + nsString nodeIDString; + nsString doomedIDString; + + aTask->GetId(doomedIDString); + + while (node != NULL) + { + node->task->GetId(nodeIDString); + + if (nodeIDString == doomedIDString) + { + /* we want to delete this node */ + + if (node == gTasks) + { + gTasks = node->next; + } + else + { + lastnode->next = node->next; + } + + node->task->Release(); + PR_DELETE(node); + break; + } + + lastnode = node; + node = node->next; + } + return NS_OK; +} + + + +NS_IMETHODIMP +nsSilentDownloadManager::Find(const nsString& aId, nsIDOMSilentDownloadTask** aReturn) +{ + *aReturn=nsnull; + + SDL_TaskList *node = gTasks; + nsString nodeIDString; + + while (node != NULL) + { + node->task->GetId(nodeIDString); + + if (nodeIDString == aId) + { + *aReturn = node->task; + node->task->AddRef(); + break; + } + node = node->next; + } + + return NS_OK; +} + + +NS_IMETHODIMP +nsSilentDownloadManager::GetByteRange(PRInt32* aByterange) +{ + *aByterange = gByteRange; + return NS_OK; +} + +NS_IMETHODIMP +nsSilentDownloadManager::SetByteRange(PRInt32 aByterange) +{ + gByteRange = aByterange; + return NS_OK; +} + +NS_IMETHODIMP +nsSilentDownloadManager::GetInterval(PRInt32* aInterval) +{ + *aInterval = gInterval; + return NS_OK; +} + +NS_IMETHODIMP +nsSilentDownloadManager::SetInterval(PRInt32 aInterval) +{ + gInterval = aInterval; + return NS_OK; +} + + + +///////////////////////////////////////////////////////////////////////// +// nsSilentDownloadTask +///////////////////////////////////////////////////////////////////////// + +nsSilentDownloadTask::nsSilentDownloadTask() +{ + mScriptObject = nsnull; + mListener = nsnull; + mWindow = nsnull; + mWebShell = nsnull; + mState = nsIDOMSilentDownloadTask::SDL_NOT_INITED; + mNextByte = 0; + + PR_AtomicIncrement(&gInstanceCnt); + NS_INIT_REFCNT(); +} + +nsSilentDownloadTask::~nsSilentDownloadTask() +{ + mListener->Release(); + PR_AtomicDecrement(&gInstanceCnt); +} + + +void +nsSilentDownloadTask::LoadScript(void) +{ + if (mState == nsIDOMSilentDownloadTask::SDL_NOT_ADDED || + mState == nsIDOMSilentDownloadTask::SDL_NOT_INITED ) + return; + + if (mWindow == nsnull) + { + nsresult rv = nsRepository::CreateInstance(kBrowserWindowCID, nsnull, + kIBrowserWindowIID, + (void**) &mWindow); + if (rv == NS_OK) + { + nsRect rect(0, 0, 275, 300); + + rv = mWindow->Init(nsnull, nsnull, rect, PRUint32(0), PR_FALSE); + + if (rv == NS_OK) + { + rv = mWindow->GetWebShell(mWebShell); + mWebShell->LoadURL(mScript); + } + else + { + mWindow->Release(); + mWindow=nsnull; + + mState = nsIDOMSilentDownloadTask::SDL_ERROR; + SetErrorMsg("Couldn't Open Window"); + } + } + } + else + { + if (mWebShell) + { + mWebShell->LoadURL(mScript); + } + else + { + mState = nsIDOMSilentDownloadTask::SDL_ERROR; + SetErrorMsg("Couldn't Open Window"); + } + } +} + + +NS_IMPL_ADDREF(nsSilentDownloadTask) +NS_IMPL_RELEASE(nsSilentDownloadTask) + + +NS_IMETHODIMP +nsSilentDownloadTask::QueryInterface(REFNSIID aIID,void** aInstancePtr) +{ + if (aInstancePtr == NULL) + { + return NS_ERROR_NULL_POINTER; + } + + // Always NULL result, in case of failure + *aInstancePtr = NULL; + + if ( aIID.Equals(kIScriptObjectOwnerIID)) + { + *aInstancePtr = (void*) ((nsIScriptObjectOwner*)this); + AddRef(); + return NS_OK; + } + else if ( aIID.Equals(kISilentDownloadTaskIID) ) + { + *aInstancePtr = (void*) ((nsIDOMSilentDownloadTask*)this); + AddRef(); + return NS_OK; + } + else if ( aIID.Equals(kISupportsIID) ) + { + *aInstancePtr = (void*)(nsISupports*)(nsIScriptObjectOwner*)this; + AddRef(); + return NS_OK; + } + + return NS_NOINTERFACE; +} + + +NS_IMETHODIMP +nsSilentDownloadTask::GetScriptObject(nsIScriptContext *aContext, void** aScriptObject) +{ + NS_PRECONDITION(nsnull != aScriptObject, "null arg"); + nsresult res = NS_OK; + if (nsnull == mScriptObject) + { + res = NS_NewScriptSilentDownloadTask( aContext, + (nsISupports *)(nsIDOMSilentDownloadTask*)this, + nsnull, + &mScriptObject); + } + + *aScriptObject = mScriptObject; + return res; +} + +NS_IMETHODIMP +nsSilentDownloadTask::SetScriptObject(void *aScriptObject) +{ + mScriptObject = aScriptObject; + return NS_OK; +} + + +NS_IMETHODIMP +nsSilentDownloadTask::Init(const nsString& aId, const nsString& aUrl, const nsString& aScript) +{ + char *defaultDirectoryCString = NULL; + char *urlCString = NULL; + char *outFileCString = NULL; + + GetSilentDownloadDirectory(defaultDirectoryCString); + + mId = aId; + mUrl = aUrl; + mScript = aScript; + + urlCString = aUrl.ToNewCString(); + outFileCString = CreateOutFileLocation(urlCString, defaultDirectoryCString); + delete[] urlCString; + + if (outFileCString == NULL) + { + SetState(nsIDOMSilentDownloadTask::SDL_ERROR); + SetErrorMsg("Couldn't access destination directory to save file"); + return -1; + } + else + { + mOutFile = outFileCString; + } + + + mNextByte = GetCurrentSize(outFileCString); + /* FIX delete outFileCString and defaultDirectoryCString */ + + + if (mNextByte < 0) + { + SetState(nsIDOMSilentDownloadTask::SDL_ERROR); + SetErrorMsg("Negative Byte!"); + return NS_OK; + + } + + /* Everything looks good. Mark as SDL_NOT_ADDED. */ + mState = nsIDOMSilentDownloadTask::SDL_NOT_ADDED; + + mListener = new nsSilentDownloadListener(); + mListener->AddRef(); + mListener->SetSilentDownloadInfo(this); + + nsSilentDownloadManager* sdm = new nsSilentDownloadManager(); + sdm->Add(this); + delete sdm; + + return NS_OK; +} + + +NS_IMETHODIMP +nsSilentDownloadTask::GetId(nsString& aId) +{ + aId = mId; + return NS_OK; +} + + +NS_IMETHODIMP +nsSilentDownloadTask::GetUrl(nsString& aUrl) +{ + aUrl = mUrl; + return NS_OK; +} + +NS_IMETHODIMP +nsSilentDownloadTask::GetScript(nsString& aScript) +{ + aScript = mScript; + return NS_OK; +} + + + +NS_IMETHODIMP +nsSilentDownloadTask::GetState(PRInt32* aState) +{ + *aState = mState; + return NS_OK; +} + + +NS_IMETHODIMP +nsSilentDownloadTask::SetState(PRInt32 aState) +{ +/* This function is so that our listener can change the state. +I would rather not expose this to Javascript if possible*/ + + mState = aState; + LoadScript(); + + return NS_OK; +} + +NS_IMETHODIMP +nsSilentDownloadTask::GetErrorMsg(nsString& aErrorMsg) +{ +/* This function is so that our listener can change the state. +I would rather not expose this to Javascript if possible*/ + aErrorMsg = mErrorMsg; + return NS_OK; +} + +NS_IMETHODIMP +nsSilentDownloadTask::SetErrorMsg(const nsString& aErrorMsg) +{ + mErrorMsg = aErrorMsg; + return NS_OK; +} + + +NS_IMETHODIMP +nsSilentDownloadTask::GetNextByte(PRInt32* aNext_byte) +{ + *aNext_byte = mNextByte; + return NS_OK; +} + +NS_IMETHODIMP +nsSilentDownloadTask::SetNextByte(PRInt32 aNextByte) +{ + mNextByte = aNextByte; + return NS_OK; +} + +NS_IMETHODIMP +nsSilentDownloadTask::GetOutFile(nsString& aOutFile) +{ + aOutFile = mOutFile; + return NS_OK; +} + +NS_IMETHODIMP +nsSilentDownloadTask::Remove() +{ + mState = nsIDOMSilentDownloadTask::SDL_NOT_ADDED; + + mWebShell->Release(); + + mWindow->Close(); + mWindow->Release(); + mWindow = nsnull; + + nsSilentDownloadManager* sdm = new nsSilentDownloadManager(); + sdm->Remove(this); + delete sdm; + + return NS_OK; +} + + +NS_IMETHODIMP +nsSilentDownloadTask::Suspend() +{ + SetState(nsIDOMSilentDownloadTask::SDL_SUSPENDED); + return NS_OK; +} + +NS_IMETHODIMP +nsSilentDownloadTask::Resume() +{ + SetState(nsIDOMSilentDownloadTask::SDL_STARTED); + return NS_OK; +} + + +NS_IMETHODIMP +nsSilentDownloadTask::DownloadNow() +{ + SetState(nsIDOMSilentDownloadTask::SDL_DOWNLOADING_NOW); + DownloadSelf(0); + return NS_OK; +} + + +NS_IMETHODIMP +nsSilentDownloadTask::DownloadSelf(PRInt32 range) +{ + long result=0; + char* byteRangeString=NULL; + nsIURL *pURL; + nsILoadAttribs *loadAttr; + + if (mState != nsIDOMSilentDownloadTask::SDL_STARTED && + mState != nsIDOMSilentDownloadTask::SDL_DOWNLOADING_NOW) + { + /* We do not have to do a download here. */ + return 0; + } + + + // Create the URL object... + pURL = NULL; + + result = NS_NewURL(&pURL, mUrl); + + if (result != NS_OK) + { + SetState(nsIDOMSilentDownloadTask::SDL_ERROR); + SetErrorMsg("Couldn't set up download. Out of memory"); + return -1; + } + + pURL->GetLoadAttribs(&loadAttr); + loadAttr->SetLoadType(nsURLLoadBackground); + + + byteRangeString = PR_sprintf_append( byteRangeString, + "bytes=%ld-%ld", + mNextByte, + mNextByte+range); + + loadAttr->SetByteRangeHeader(byteRangeString); + + PR_FREEIF(byteRangeString); + + if (mState != nsIDOMSilentDownloadTask::SDL_DOWNLOADING_NOW) + { + /* Do Byte Range Stuff */ + } + + result = NS_OpenURL(pURL, mListener); + + /* If the open failed... */ + if (NS_OK != result) + { + } + + return result; +} + +///////////////////////////////////////////////////////////////////////// +// nsSilentDownloadImplFactory +///////////////////////////////////////////////////////////////////////// + +nsSilentDownloadManagerFactory::nsSilentDownloadManagerFactory(void) +{ + mRefCnt=0; + PR_AtomicIncrement(&gInstanceCnt); +} + +nsSilentDownloadManagerFactory::~nsSilentDownloadManagerFactory(void) +{ + PR_AtomicDecrement(&gInstanceCnt); +} + + + +NS_IMETHODIMP +nsSilentDownloadManagerFactory::QueryInterface(REFNSIID aIID,void** aInstancePtr) +{ + if (aInstancePtr == NULL) + { + return NS_ERROR_NULL_POINTER; + } + + // Always NULL result, in case of failure + *aInstancePtr = NULL; + + if ( aIID.Equals(kISupportsIID) ) + { + *aInstancePtr = (void*) this; + } + else if ( aIID.Equals(kIFactoryIID) ) + { + *aInstancePtr = (void*) this; + } + + if (aInstancePtr == NULL) + { + return NS_ERROR_NO_INTERFACE; + } + + AddRef(); + return NS_OK; +} + + + +NS_IMETHODIMP +nsSilentDownloadManagerFactory::AddRef(void) +{ + return ++mRefCnt; +} + + +NS_IMETHODIMP +nsSilentDownloadManagerFactory::Release(void) +{ + if (--mRefCnt ==0) + { + delete this; + return 0; // Don't access mRefCnt after deleting! + } + + return mRefCnt; +} + +NS_IMETHODIMP +nsSilentDownloadManagerFactory::CreateInstance(nsISupports *aOuter, REFNSIID aIID, void **aResult) +{ + if (aResult == NULL) + { + return NS_ERROR_NULL_POINTER; + } + + *aResult = NULL; + + /* do I have to use iSupports? */ + nsSilentDownloadManager *inst = new nsSilentDownloadManager(); + + if (inst == NULL) + return NS_ERROR_OUT_OF_MEMORY; + + nsresult result = inst->QueryInterface(aIID, aResult); + + if (result != NS_OK) + delete inst; + + return result; + +} + +NS_IMETHODIMP +nsSilentDownloadManagerFactory::LockFactory(PRBool aLock) +{ + if (aLock) + PR_AtomicIncrement(&gLockCnt); + else + PR_AtomicDecrement(&gLockCnt); + + return NS_OK; +} +///////////////////////////////////////////////////////////////////////// +// nsSilentDownloadTaskFactory +///////////////////////////////////////////////////////////////////////// + +nsSilentDownloadTaskFactory::nsSilentDownloadTaskFactory(void) +{ + mRefCnt=0; + PR_AtomicIncrement(&gInstanceCnt); +} + +nsSilentDownloadTaskFactory::~nsSilentDownloadTaskFactory(void) +{ + PR_AtomicDecrement(&gInstanceCnt); +} + + + +NS_IMETHODIMP +nsSilentDownloadTaskFactory::QueryInterface(REFNSIID aIID,void** aInstancePtr) +{ + if (aInstancePtr == NULL) + { + return NS_ERROR_NULL_POINTER; + } + + // Always NULL result, in case of failure + *aInstancePtr = NULL; + + if ( aIID.Equals(kISupportsIID) ) + { + *aInstancePtr = (void*) this; + } + else if ( aIID.Equals(kIFactoryIID) ) + { + *aInstancePtr = (void*) this; + } + + if (aInstancePtr == NULL) + { + return NS_ERROR_NO_INTERFACE; + } + + AddRef(); + return NS_OK; +} + + + +NS_IMETHODIMP +nsSilentDownloadTaskFactory::AddRef(void) +{ + return ++mRefCnt; +} + + +NS_IMETHODIMP +nsSilentDownloadTaskFactory::Release(void) +{ + if (--mRefCnt ==0) + { + delete this; + return 0; // Don't access mRefCnt after deleting! + } + + return mRefCnt; +} + +NS_IMETHODIMP +nsSilentDownloadTaskFactory::CreateInstance(nsISupports *aOuter, REFNSIID aIID, void **aResult) +{ + if (aResult == NULL) + { + return NS_ERROR_NULL_POINTER; + } + + *aResult = NULL; + + /* do I have to use iSupports? */ + nsSilentDownloadTask *inst = new nsSilentDownloadTask(); + + if (inst == NULL) + return NS_ERROR_OUT_OF_MEMORY; + + nsresult result = inst->QueryInterface(aIID, aResult); + + if (result != NS_OK) + delete inst; + + return result; + +} + +NS_IMETHODIMP +nsSilentDownloadTaskFactory::LockFactory(PRBool aLock) +{ + if (aLock) + PR_AtomicIncrement(&gLockCnt); + else + PR_AtomicDecrement(&gLockCnt); + + return NS_OK; +} + +//////////////////////////////////////////////////////////////////////////////// +// nsSilentDownloadNameSet +//////////////////////////////////////////////////////////////////////////////// + +nsSilentDownloadNameSet::nsSilentDownloadNameSet() +{ + NS_INIT_REFCNT(); +} + +nsSilentDownloadNameSet::~nsSilentDownloadNameSet() +{ +} + +NS_IMPL_ISUPPORTS(nsSilentDownloadNameSet, kIScriptExternalNameSetIID); + + + + +NS_IMETHODIMP +nsSilentDownloadNameSet::InitializeClasses(nsIScriptContext* aScriptContext) +{ + nsresult result = NS_OK; + + result = NS_InitSilentDownloadClass(aScriptContext, nsnull); + if (NS_OK != result) return result; + + result = NS_InitSilentDownloadTaskClass(aScriptContext, nsnull); + + return result; +} + + + + +NS_IMETHODIMP +nsSilentDownloadNameSet::AddNameSet(nsIScriptContext* aScriptContext) +{ + nsresult result = NS_OK; + nsIScriptNameSpaceManager* manager; + + result = aScriptContext->GetNameSpaceManager(&manager); + if (NS_OK == result) + { + result = manager->RegisterGlobalName("SilentDownloadTask", + kSilentDownloadTaskCID, + PR_TRUE); + + if (NS_OK != result) return result; + + result = manager->RegisterGlobalName("SilentDownloadManager", + kSilentDownloadCID, + PR_FALSE); + + + NS_RELEASE(manager); + } + return result; +} + + + +//////////////////////////////////////////////////////////////////////////////// +// nsSilentDownloadListener +//////////////////////////////////////////////////////////////////////////////// +nsSilentDownloadListener::nsSilentDownloadListener() +{ + NS_INIT_REFCNT(); +} + +nsSilentDownloadListener::~nsSilentDownloadListener() +{ + mSilentDownloadTask->Release(); + + if (mOutFileDesc != NULL) + PR_Close(mOutFileDesc); +} + + +NS_IMPL_ISUPPORTS( nsSilentDownloadListener, kIStreamListenerIID ) + +NS_IMETHODIMP +nsSilentDownloadListener::GetBindInfo(nsIURL* aURL, nsStreamBindingInfo* info) +{ + return NS_OK; +} + +NS_IMETHODIMP +nsSilentDownloadListener::OnProgress( nsIURL* aURL, + PRUint32 Progress, + PRUint32 ProgressMax) +{ + return NS_OK; +} + +NS_IMETHODIMP +nsSilentDownloadListener::OnStatus(nsIURL* aURL, + const PRUnichar* aMsg) +{ + return NS_OK; +} + +NS_IMETHODIMP +nsSilentDownloadListener::OnStartBinding(nsIURL* aURL, + const char *aContentType) +{ + return NS_OK; +} + +NS_IMETHODIMP +nsSilentDownloadListener::OnStopBinding(nsIURL* aURL, + nsresult status, + const PRUnichar* aMsg) +{ + nsresult result = NS_OK; + PRInt32 content_length = 0; + PRInt32 server_status = 0; + PRInt32 nextByte; + + switch( status ) + { + + case NS_BINDING_SUCCEEDED: + /* + What we are going to is check to see if our nextByte that the task will + get is passed the content_length of the URL. + + What happens if there is no content_length? + */ + + aURL->GetServerStatus(&server_status); + + if (server_status == 404) + { + mSilentDownloadTask->SetState(nsIDOMSilentDownloadTask::SDL_ERROR); + mSilentDownloadTask->SetErrorMsg("Server Error 400."); + return NS_OK; + } + else if (server_status == 500) + { + mSilentDownloadTask->SetState(nsIDOMSilentDownloadTask::SDL_ERROR); + mSilentDownloadTask->SetErrorMsg("Server Error 500."); + return NS_OK; + } + + aURL->GetContentLength(&content_length); + + mSilentDownloadTask->GetNextByte(&nextByte); + + if (content_length <= nextByte || content_length == 0) + { + mSilentDownloadTask->SetState(nsIDOMSilentDownloadTask::SDL_COMPLETED); + PR_Close(mOutFileDesc); + mOutFileDesc=NULL; + } + break; + + case NS_BINDING_FAILED: + case NS_BINDING_ABORTED: + mSilentDownloadTask->SetState(nsIDOMSilentDownloadTask::SDL_ERROR); + mSilentDownloadTask->SetErrorMsg("Could Not Download."); + break; + + default: + result = NS_ERROR_ILLEGAL_VALUE; + } + + return result; +} + + +NS_IMETHODIMP +nsSilentDownloadListener::OnDataAvailable(nsIURL* aURL, nsIInputStream *pIStream, PRUint32 length) +{ + PRUint32 len; + PRInt32 nextByte; + nsresult err; + char buffer[80]; + + do + { + err = pIStream->Read(buffer, 0, 80, &len); + if (err == NS_OK) + { + mSilentDownloadTask->GetNextByte(&nextByte); + if ( PR_Seek(mOutFileDesc, nextByte, PR_SEEK_SET) == -1 ) + { + /* Error */ + mSilentDownloadTask->SetState(nsIDOMSilentDownloadTask::SDL_ERROR); + mSilentDownloadTask->SetErrorMsg("File Seek Error."); + return -1; + } + + if( PR_Write(mOutFileDesc, buffer, len) == -1 ) + { + /* Error */ + mSilentDownloadTask->SetState(nsIDOMSilentDownloadTask::SDL_ERROR); + mSilentDownloadTask->SetErrorMsg("File Write Error."); + return -1; + } + + mSilentDownloadTask->SetNextByte(nextByte+len); + } + } while (len > 0); + + return 0; +} + +NS_IMETHODIMP +nsSilentDownloadListener::SetSilentDownloadInfo(nsIDOMSilentDownloadTask* con) +{ + nsString aString; + char *fileName; + + con->AddRef(); + + mSilentDownloadTask = con; + mSilentDownloadTask->GetOutFile(aString); + fileName = aString.ToNewCString(); + + mOutFileDesc = PR_Open(fileName, PR_CREATE_FILE | PR_RDWR, 0644); + + delete[] fileName; + + if(mOutFileDesc == NULL) + { + mSilentDownloadTask->SetState(nsIDOMSilentDownloadTask::SDL_ERROR); + mSilentDownloadTask->SetErrorMsg("Could not create OUT file."); + return NS_OK; + }; + + + return NS_OK; +} + + + + + +//////////////////////////////////////////////////////////////////////////////// +// DLL Entry Points: +//////////////////////////////////////////////////////////////////////////////// + +extern "C" NS_EXPORT PRBool +NSCanUnload(void) +{ + return PRBool (gInstanceCnt == 0 && gLockCnt == 0); +} + +extern "C" NS_EXPORT nsresult +NSRegisterSelf(const char *path) +{ + printf("*** SilentDownload is being registered\n"); + nsRepository::RegisterFactory(kSilentDownloadCID, path, PR_TRUE, PR_TRUE); + nsRepository::RegisterFactory(kSilentDownloadTaskCID, path, PR_TRUE, PR_TRUE); + return NS_OK; +} + +extern "C" NS_EXPORT nsresult +NSUnregisterSelf(const char *path) +{ + printf("*** SilentDownload is being unregistered\n"); + + nsRepository::UnregisterFactory(kSilentDownloadCID, path); + nsRepository::UnregisterFactory(kSilentDownloadTaskCID, path); + + return NS_OK; +} + + + +extern "C" NS_EXPORT nsresult +NSGetFactory(const nsCID &aClass, nsIFactory **aFactory) +{ + + if (aFactory == NULL) + { + return NS_ERROR_NULL_POINTER; + } + + *aFactory = NULL; + nsISupports *inst; + + + if ( aClass.Equals(kSilentDownloadCID) ) + { + inst = new nsSilentDownloadManagerFactory(); + } + else if ( aClass.Equals(kSilentDownloadTaskCID) ) + { + inst = new nsSilentDownloadTaskFactory(); + } + else + { + return NS_ERROR_ILLEGAL_VALUE; + } + + + if (inst == NULL) + { + return NS_ERROR_OUT_OF_MEMORY; + } + + + nsresult res = inst->QueryInterface(kIFactoryIID, (void**) aFactory); + + if (res != NS_OK) + { + delete inst; + } + + return res; + +} + + diff --git a/silentdl/nsSilentDownload.h b/silentdl/nsSilentDownload.h new file mode 100644 index 00000000000..e2551372412 --- /dev/null +++ b/silentdl/nsSilentDownload.h @@ -0,0 +1,38 @@ +#ifndef nsSilentDownload_h___ +#define nsSilentDownload_h___ + +/* need to generate real CIDs! */ + +#define NS_SilentDownload_CID \ +{ /* 5196e6a0-938b-11d2-b0dd-00805f8a8851 */ \ + 0x5196e6a0, \ + 0x938b, \ + 0x11d2, \ + {0xb0, 0xdd, 0x00, 0x80, 0x5f, 0x8a, 0x88, 0x51} \ +}; + +#define NS_SilentDownloadTask_CID \ +{ /* 5196e6a0-938b-11d2-b0dd-00805f8a8851 */ \ + 0x5196e6a0, \ + 0x938b, \ + 0x11d2, \ + {0xb0, 0xdd, 0x00, 0x80, 0x5f, 0x8a, 0x88, 0x52} \ +}; + +#define NS_SilentDownloadFactory_CID \ +{ /* 5196e6a0-938b-11d2-b0dd-00805f8a8851 */ \ + 0x5196e6a0, \ + 0x938b, \ + 0x11d2, \ + {0xb0, 0xdd, 0x00, 0x80, 0x5f, 0x8a, 0x88, 0x53} \ +}; + +#define NS_SilentDownloadTaskFactory_CID \ +{ /* 5196e6a0-938b-11d2-b0dd-00805f8a8851 */ \ + 0x5196e6a0, \ + 0x938b, \ + 0x11d2, \ + {0xb0, 0xdd, 0x00, 0x80, 0x5f, 0x8a, 0x88, 0x54} \ +}; + +#endif /* nsSilentDownload_h___ */ \ No newline at end of file diff --git a/silentdl/nsSilentDownloadPrivate.h b/silentdl/nsSilentDownloadPrivate.h new file mode 100644 index 00000000000..616276a5a01 --- /dev/null +++ b/silentdl/nsSilentDownloadPrivate.h @@ -0,0 +1,254 @@ +#ifndef nsSilentDownloadPrivate_h___ +#define nsSilentDownloadPrivate_h___ + +#include "nsSilentDownload.h" + +#include "nscore.h" +#include "nsString.h" +#include "nsIFactory.h" +#include "nsISupports.h" +#include "nsIScriptObjectOwner.h" + +#include "nsINetService.h" +#include "nsIScriptExternalNameSet.h" +#include "nsIStreamListener.h" + +#include "nsIBrowserWindow.h" +#include "nsIWebShell.h" + +#include "nsIDOMSilentDownload.h" +#include "nsIDOMSilentDownloadTask.h" + +#include "nsITimer.h" +#include "nsITimerCallback.h" + +#include "prio.h" + +//////////////////////////////////////////////////////////////////////////////// +// DLL Entry Points: +//////////////////////////////////////////////////////////////////////////////// +extern "C" NS_EXPORT nsresult +NSGetFactory(const nsCID &aClass, nsIFactory **aFactory); + +extern "C" NS_EXPORT PRBool +NSCanUnload(void); + +extern "C" NS_EXPORT nsresult +NSRegisterSelf(const char *path); + +extern "C" NS_EXPORT nsresult +NSUnregisterSelf(const char *path); + +//////////////////////////////////////////////////////////////////////////////// +// nsSilentDownloadListener: +//////////////////////////////////////////////////////////////////////////////// + +class nsSilentDownloadListener : public nsIStreamListener +{ + + public: + NS_DECL_ISUPPORTS + + nsSilentDownloadListener(); + + NS_IMETHOD GetBindInfo(nsIURL* aURL, nsStreamBindingInfo* info); + NS_IMETHOD OnProgress(nsIURL* aURL, PRUint32 Progress, PRUint32 ProgressMax); + NS_IMETHOD OnStatus(nsIURL* aURL, const PRUnichar* aMsg); + NS_IMETHOD OnStartBinding(nsIURL* aURL, const char *aContentType); + NS_IMETHOD OnDataAvailable(nsIURL* aURL, nsIInputStream *pIStream, PRUint32 length); + NS_IMETHOD OnStopBinding(nsIURL* aURL, nsresult status, const PRUnichar* aMsg); + + NS_METHOD SetSilentDownloadInfo(nsIDOMSilentDownloadTask* con); + + protected: + ~nsSilentDownloadListener(); + + private: + // + nsIDOMSilentDownloadTask* mSilentDownloadTask; + PRFileDesc *mOutFileDesc; +}; + + + +//////////////////////////////////////////////////////////////////////////////// +// nsSilentDownloadTask: +//////////////////////////////////////////////////////////////////////////////// + +class nsSilentDownloadTask : public nsIScriptObjectOwner, public nsIDOMSilentDownloadTask +{ + public: + + nsSilentDownloadTask(); + ~nsSilentDownloadTask(); + + + NS_DECL_ISUPPORTS + NS_IMETHOD GetScriptObject(nsIScriptContext *aContext, void** aScriptObject); + NS_IMETHOD SetScriptObject(void* aScriptObject); + + NS_IMETHOD Init(const nsString& aId, const nsString& aUrl, const nsString& aScript); + + + NS_IMETHOD GetId(nsString& aId); + NS_IMETHOD GetUrl(nsString& aUrl); + NS_IMETHOD GetScript(nsString& aScript); + NS_IMETHOD GetOutFile(nsString& aOutFile); + + NS_IMETHOD GetState(PRInt32* aState); + NS_IMETHOD SetState(PRInt32 aState); + + NS_IMETHOD GetErrorMsg(nsString& aErrorMsg); + NS_IMETHOD SetErrorMsg(const nsString& aErrorMsg); + + NS_IMETHOD GetNextByte(PRInt32* aStart_byte); + NS_IMETHOD SetNextByte(PRInt32 aStart_byte); + + NS_IMETHOD Remove(); + NS_IMETHOD Suspend(); + NS_IMETHOD Resume(); + NS_IMETHOD DownloadNow(); + + NS_IMETHOD DownloadSelf(PRInt32 aRange); + + private: + + void LoadScript(void); + + nsString mId; /* User ID */ + nsString mUrl; /* What to Download */ + nsString mScript; /* What controls the download */ + + PRInt32 mFile_size; /* How big is the download */ + + nsString mErrorMsg; /* Human readable error message */ + nsString mOutFile; /* Where on the users system file is stored */ + + PRInt32 mState; /* State of task */ + + PRInt32 mNextByte; /* The start byte in the file where will will start downloading */ + + + nsSilentDownloadListener *mListener; + nsIBrowserWindow *mWindow; + nsIWebShell *mWebShell; + void *mScriptObject; +}; + + +//////////////////////////////////////////////////////////////////////////////// +// nsSilentDownloadManager: +//////////////////////////////////////////////////////////////////////////////// +typedef struct _SDL_TaskList SDL_TaskList; + +typedef struct _SDL_TaskList +{ + nsIDOMSilentDownloadTask *task; + SDL_TaskList *next; + +} SDL_TaskList; + +class nsSilentDownloadManager : public nsIScriptObjectOwner, public nsIDOMSilentDownload, public nsITimerCallback +{ + public: + + nsSilentDownloadManager(); + ~nsSilentDownloadManager(); + + NS_DECL_ISUPPORTS + + NS_IMETHOD GetScriptObject(nsIScriptContext *aContext, void** aScriptObject); + NS_IMETHOD SetScriptObject(void* aScriptObject); + + NS_IMETHOD GetByteRange(PRInt32* aByterange); + NS_IMETHOD SetByteRange(PRInt32 aByterange); + NS_IMETHOD GetInterval(PRInt32* aInterval); + NS_IMETHOD SetInterval(PRInt32 aInterval); + + NS_IMETHOD Remove(nsIDOMSilentDownloadTask* aTask); + NS_IMETHOD Add(nsIDOMSilentDownloadTask* aTask); + NS_IMETHOD Find(const nsString& aId, nsIDOMSilentDownloadTask** aReturn); + + NS_IMETHOD Startup(); + NS_IMETHOD Shutdown(); + + protected: + // nsITimerCallback Interface + virtual void Notify(nsITimer *timer); + + private: + + + nsresult StoreAllTasks(void); + nsresult LoadAllTasks(void); + + void *mScriptObject; + +}; + + + + +//////////////////////////////////////////////////////////////////////////////// +// nsSilentDownloadManagerFactory: +//////////////////////////////////////////////////////////////////////////////// + +class nsSilentDownloadManagerFactory : public nsIFactory +{ + public: + + nsSilentDownloadManagerFactory(); + ~nsSilentDownloadManagerFactory(); + + PRBool CanUnload(void); + + NS_DECL_ISUPPORTS + + NS_IMETHOD CreateInstance(nsISupports *aOuter, + REFNSIID aIID, + void **aResult); + + NS_IMETHOD LockFactory(PRBool aLock); + +}; + + +//////////////////////////////////////////////////////////////////////////////// +// nsSilentDownloadTaskFactory: +//////////////////////////////////////////////////////////////////////////////// + +class nsSilentDownloadTaskFactory : public nsIFactory +{ + public: + + nsSilentDownloadTaskFactory(); + ~nsSilentDownloadTaskFactory(); + + PRBool CanUnload(void); + + NS_DECL_ISUPPORTS + + NS_IMETHOD CreateInstance(nsISupports *aOuter, + REFNSIID aIID, + void **aResult); + + NS_IMETHOD LockFactory(PRBool aLock); + +}; + +//////////////////////////////////////////////////////////////////////////////// +// nsSilentDownloadNameSet: +//////////////////////////////////////////////////////////////////////////////// + +class nsSilentDownloadNameSet : public nsIScriptExternalNameSet +{ + public: + nsSilentDownloadNameSet(); + ~nsSilentDownloadNameSet(); + + NS_DECL_ISUPPORTS + NS_IMETHOD InitializeClasses(nsIScriptContext* aScriptContext); + NS_IMETHOD AddNameSet(nsIScriptContext* aScriptContext); +}; + +#endif