Support progressive loading

This commit is contained in:
kipp 1998-07-13 16:25:15 +00:00
parent 4fe59b0a91
commit 511d35305d
3 changed files with 176 additions and 104 deletions

View File

@ -49,6 +49,7 @@ nsDocLoader::nsDocLoader(nsIViewerContainer* aContainer, nsViewer* aViewer, PRIn
mURLList = new nsVoidArray();
NSRepository::CreateInstance(kCDocumentLoaderCID, nsnull, kIDocumentLoaderIID, (void**)&mDocLoader);
NS_INIT_REFCNT();
}
nsDocLoader::~nsDocLoader()
@ -73,6 +74,10 @@ nsDocLoader::~nsDocLoader()
NS_RELEASE(mDocLoader);
}
static NS_DEFINE_IID(kIStreamObserverIID, NS_ISTREAMOBSERVER_IID);
NS_IMPL_ISUPPORTS(nsDocLoader, kIStreamObserverIID);
// Set the delay (by default, the timer is set to one second)
void nsDocLoader::SetDelay(PRInt32 aSeconds)
@ -129,6 +134,36 @@ void nsDocLoader::StartTimedLoading()
}
}
void nsDocLoader::StartLoading()
{
// Only start once!
if (mStart == PR_FALSE)
{
mStart = PR_TRUE;
if (mURLList)
{
if (mDocNum < mURLList->Count())
{
LoadDoc(mDocNum, PR_TRUE);
}
}
}
}
void
nsDocLoader::LoadDoc(PRInt32 aDocNum, PRBool aObserveIt)
{
nsString* url = (nsString*)mURLList->ElementAt(aDocNum);
if (url) {
mDocLoader->LoadURL(*url, // URL string
nsnull, // Command
mContainer, // Container
nsnull, // Post Data
nsnull, // Extra Info...
aObserveIt ? this : nsnull); // Observer
}
}
@ -139,7 +174,7 @@ void nsDocLoader::StartTimedLoading()
*/
nsDocLoader* gDocLoader = nsnull;
void MyCallback (nsITimer *aTimer, void *aClosure)
static void MyCallback (nsITimer *aTimer, void *aClosure)
{
if (gDocLoader != nsnull)
@ -165,7 +200,7 @@ void nsDocLoader::CreateOneShot(PRUint32 aDelay)
}
void MyRepeatCallback (nsITimer *aTimer, void *aClosure)
static void MyRepeatCallback (nsITimer *aTimer, void *aClosure)
{
if (gDocLoader != nsnull)
{
@ -201,14 +236,10 @@ void nsDocLoader::DoAction(PRInt32 aDocNum)
if (aDocNum >= 0 && aDocNum < mURLList->Count())
{
nsString* url = (nsString*)mURLList->ElementAt(aDocNum);
if (url)
/// mWebWidget->LoadURL(*url, nsnull);
mDocLoader->LoadURL(*url, // URL string
nsnull, // Command
mContainer, // Container
nsnull, // Post Data
nsnull, // Extra Info...
nsnull); // Observer
printf("Now loading ");
fputs(*url, stdout);
printf("\n");
LoadDoc(aDocNum, PR_FALSE);
}
}
@ -267,3 +298,40 @@ void nsDocLoader::CancelAll()
}
}
NS_IMETHODIMP
nsDocLoader::OnStartBinding(const char *aContentType)
{
nsString* url = (nsString*)mURLList->ElementAt(mDocNum);
fputs(*url, stdout);
printf(": start\n");
return NS_OK;
}
NS_IMETHODIMP
nsDocLoader::OnProgress(PRInt32 aProgress, PRInt32 aProgressMax,
const nsString& aMsg)
{
nsString* url = (nsString*)mURLList->ElementAt(mDocNum);
fputs(*url, stdout);
printf(": progress %d", aProgress);
if (0 != aProgressMax) {
printf(" (out of %d)", aProgressMax);
}
fputs("\n", stdout);
return NS_OK;
}
NS_IMETHODIMP
nsDocLoader::OnStopBinding(PRInt32 status, const nsString& aMsg)
{
nsString* url = (nsString*)mURLList->ElementAt(mDocNum);
fputs(*url, stdout);
printf(": stop\n");
++mDocNum;
if (mDocNum < mURLList->Count()) {
LoadDoc(mDocNum, PR_TRUE);
}
return NS_OK;
}

View File

@ -19,7 +19,7 @@
#define nsDocLoader_h___
#include "nscore.h"
#include "nsISupports.h"
#include "nsIStreamListener.h"
class nsIViewerContainer;
@ -35,66 +35,74 @@ class nsViewer;
posts an exit application message
*/
class nsDocLoader
{
public:
nsDocLoader(nsIViewerContainer* aContainer, nsViewer* aViewer, PRInt32 aSeconds=1, PRBool aPostExit=PR_TRUE);
virtual ~nsDocLoader();
class nsDocLoader : public nsIStreamObserver {
public:
nsDocLoader(nsIViewerContainer* aContainer, nsViewer* aViewer,
PRInt32 aSeconds=1, PRBool aPostExit=PR_TRUE);
// nsISupports
NS_DECL_ISUPPORTS;
// nsIStreamObserver
NS_IMETHOD OnStartBinding(const char *aContentType);
NS_IMETHOD OnProgress(PRInt32 aProgress, PRInt32 aProgressMax,
const nsString& aMsg);
NS_IMETHOD OnStopBinding(PRInt32 status, const nsString& aMsg);
// Add a URL to the doc loader
void AddURL(char* aURL);
void AddURL(nsString* aURL);
// Add a URL to the doc loader
void AddURL(char* aURL);
void AddURL(nsString* aURL);
// Get the timer and the url list
// needed to be available for the call back methods
nsVoidArray* GetTimers()
{ return mTimers; }
nsVoidArray* GetURLList()
{ return mURLList; }
// Get the timer and the url list
// needed to be available for the call back methods
nsVoidArray* GetTimers()
{ return mTimers; }
nsVoidArray* GetURLList()
{ return mURLList; }
void CreateOneShot(PRUint32 aDelay);
void CreateRepeat(PRUint32 aDelay);
void CallTest();
void CreateOneShot(PRUint32 aDelay);
void CreateRepeat(PRUint32 aDelay);
void CallTest();
// Set the delay (by default, the timer is set to one second)
void SetDelay(PRInt32 aSeconds);
// Set the delay (by default, the timer is set to one second)
void SetDelay(PRInt32 aSeconds);
// If set to TRUE the loader will post an exit message on
// exit
void SetExitOnDone(PRBool aPostExit);
// If set to TRUE the loader will post an exit message on
// exit
void SetExitOnDone(PRBool aPostExit);
// Start Loading the list of documents and executing the
// Do Action Method on the documents
void StartTimedLoading();
// Start Loading the list of documents and executing the
// Do Action Method on the documents
void StartTimedLoading();
// Start Loading the list of documents and executing the
// Do Action Method on the documents
void StartLoading();
// By default this method loads a document from
// the list of documents added to the loader
// Override in subclasses to get different behavior
virtual void DoAction(PRInt32 aDocNum);
// By default this method loads a document from
// the list of documents added to the loader
// Override in subclasses to get different behavior
virtual void DoAction(PRInt32 aDocNum);
protected:
virtual ~nsDocLoader();
private:
void CancelAll();
void CancelAll();
void LoadDoc(PRInt32 aDocNum, PRBool aObserveIt);
PRInt32 mDocNum;
PRBool mStart;
PRInt32 mDelay;
PRBool mPostExit;
nsIViewerContainer* mContainer;
nsIDocumentLoader* mDocLoader;
nsViewer* mViewer;
PRInt32 mDocNum;
PRBool mStart;
PRInt32 mDelay;
PRBool mPostExit;
nsIViewerContainer* mContainer;
nsIDocumentLoader* mDocLoader;
nsViewer* mViewer;
nsVoidArray* mURLList;
nsVoidArray* mTimers;
nsVoidArray* mURLList;
nsVoidArray* mTimers;
};
#endif

View File

@ -1057,55 +1057,46 @@ void nsViewer::AddTestDocs(nsDocLoader* aDocLoader)
/*
* SelfTest methods
*/
void nsViewer::AddTestDocsFromFile(nsDocLoader* aDocLoader, char *aFileName)
// Load a bunch of test url's from a file
void nsViewer::AddTestDocsFromFile(nsDocLoader* aDocLoader, char* aFileName)
{
/* Steve's table test code.
Assumes you have a file in the current working directory called aFileName
that contains a list of URLs, one per line, of files to load.
*/
#ifdef XP_PC
FILE* fp = fopen(aFileName, "rb");
#else
FILE* fp = fopen(aFileName, "r");
#endif
PRFileDesc* input = PR_Open(aFileName, PR_RDONLY, 0);
if (nsnull==input)
{
printf("FAILED TO OPEN %s!", aFileName);
return;
}
// read one line of input and pass it in as a URL
char *inputString = new char[10000];
if (nsnull==inputString)
{
printf("couldn't allocate buffer, insufficient memory\n");
exit (-1);
}
nsCRT::memset(inputString, 0, 10000);
PR_Read(input, inputString, 10000);
PR_Close(input);
for (;;) {
char linebuf[2000];
char* cp = fgets(linebuf, sizeof(linebuf), fp);
if (nsnull == cp) {
break;
}
if (linebuf[0] == '#') {
continue;
}
char *nextInput = inputString;
while (nsnull!=nextInput && nsnull!=*nextInput)
{
char * endOfLine = PL_strchr(nextInput, '\n');
if (nsnull!=nextInput)
{
if (nsnull!=endOfLine)
{
char save = *endOfLine;
*endOfLine = nsnull;
// strip crlf's from the line
int len = strlen(linebuf);
if (0 != len) {
if (('\n' == linebuf[len-1]) || ('\r' == linebuf[len-1])) {
linebuf[--len] = 0;
}
if ('#' != *nextInput) // use '#' as a comment character
{
aDocLoader->AddURL(nextInput);
}
if (0 != len) {
if (('\n' == linebuf[len-1]) || ('\r' == linebuf[len-1])) {
linebuf[--len] = 0;
}
if (nsnull!=endOfLine)
{
nextInput = endOfLine+1;
}
else
nextInput = nsnull;
}
// Add non-empty lines to the test list
if (0 != len) {
aDocLoader->AddURL(linebuf);
}
}
if (nsnull!=inputString)
delete [] inputString;
fclose(fp);
}
void nsViewer::DestroyAllWindows()
@ -1477,12 +1468,10 @@ nsEventStatus nsViewer::ProcessMenu(PRUint32 aId, WindowData* wd)
return(result);
}
void nsViewer::CleanupViewer(nsDocLoader* aDl)
void nsViewer::CleanupViewer(nsDocLoader* aDocLoader)
{
if (aDl != nsnull)
delete aDl;
NS_IF_RELEASE(aDocLoader);
ReleaseMemory();
NS_ShutdownINetService();
}
@ -1741,6 +1730,7 @@ nsDocLoader* nsViewer::SetupViewer(nsIWidget **aMainWindow, int argc, char **arg
nsDocLoader* dl = nsnull;
if (gDoPurify) {
dl = new nsDocLoader(wd->observer, this, gDelay);
dl->AddRef();
// Add the documents to the loader
for (PRInt32 i=0; i<gRepeatCount; i++)
@ -1751,9 +1741,15 @@ nsDocLoader* nsViewer::SetupViewer(nsIWidget **aMainWindow, int argc, char **arg
}
else if (gLoadTestFromFile) {
dl = new nsDocLoader(wd->observer, this, gDelay);
dl->AddRef();
for (PRInt32 i=0; i<gRepeatCount; i++)
AddTestDocsFromFile(dl, gInputFileName);
dl->StartTimedLoading();
if (0 == gDelay) {
dl->StartLoading();
}
else {
dl->StartTimedLoading();
}
}
else {
// Load the starting url if we have one