1998-09-02 23:08:00 +04:00
|
|
|
/* -*- Mode: C; tab-width: 4; 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mkutils.h"
|
|
|
|
#include "autoupdt.h"
|
|
|
|
#include "client.h"
|
1998-09-12 06:37:24 +04:00
|
|
|
#include "prefapi.h"
|
1998-09-22 08:15:00 +04:00
|
|
|
#include "prio.h"
|
1998-09-02 23:08:00 +04:00
|
|
|
|
|
|
|
#ifndef MAXPATHLEN
|
|
|
|
#define MAXPATHLEN 1024
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined (WIN32)
|
|
|
|
extern char * FE_GetProgramDirectory(char *buffer, int length);
|
|
|
|
#elif defined (XP_UNIX)
|
|
|
|
extern void fe_GetProgramDirectory( char *, int );
|
|
|
|
#endif
|
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
PR_STATIC_CALLBACK(unsigned int)
|
|
|
|
autoupdate_WriteReadyNetStream(NET_StreamClass *stream);
|
|
|
|
PR_STATIC_CALLBACK(int)
|
|
|
|
autoupdate_WriteNetStream(NET_StreamClass *stream, const char *str, int32 len);
|
|
|
|
PR_STATIC_CALLBACK(void)
|
|
|
|
autoupdate_AbortNetStream(NET_StreamClass *stream, int status);
|
|
|
|
PR_STATIC_CALLBACK(void)
|
|
|
|
autoupdate_CompleteNetStream(NET_StreamClass *stream);
|
|
|
|
PR_STATIC_CALLBACK(void)
|
|
|
|
autoupdate_GetUrlExitFunc(URL_Struct *urls, int status, MWContext *cx);
|
|
|
|
|
|
|
|
static int32 autoupdate_GetCurrentSize(char *file);
|
|
|
|
static void autoupdate_Free(AutoUpdateConnnection autoupdt);
|
|
|
|
static void autoupdate_Flush(AutoUpdateConnnection autoupdt);
|
|
|
|
static void autoupdate_LoadURL(AutoUpdateConnnection autoupdt);
|
|
|
|
static PRBool autoupdate_IsInQueue(AutoUpdateConnnection autoupdt);
|
1998-09-02 23:08:00 +04:00
|
|
|
static void autoupdate_TimerCallback(void* data);
|
1998-09-22 08:15:00 +04:00
|
|
|
static char* autoupdate_AssureDir(char* path);
|
1998-10-28 23:16:20 +03:00
|
|
|
static void autoupdate_LoadScript(MWContext* context, char* script);
|
|
|
|
static void autoupdate_InvokeJSTimerCallback(void* data);
|
1998-09-02 23:08:00 +04:00
|
|
|
|
|
|
|
AutoUpdateConnnection gAutoUpdateConnnection;
|
1998-10-28 23:16:20 +03:00
|
|
|
char* gMainScript = NULL;
|
1998-09-02 23:08:00 +04:00
|
|
|
|
|
|
|
static int32
|
1998-10-28 23:16:20 +03:00
|
|
|
autoupdate_GetCurrentSize(char *file)
|
1998-09-02 23:08:00 +04:00
|
|
|
{
|
|
|
|
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 void
|
1998-10-28 23:16:20 +03:00
|
|
|
autoupdate_Free(AutoUpdateConnnection autoupdt)
|
1998-09-02 23:08:00 +04:00
|
|
|
{
|
|
|
|
if (autoupdt == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
1998-10-28 23:16:20 +03:00
|
|
|
|
|
|
|
/* XXX: We should remove the downloaded file.
|
|
|
|
* We may have to introduce locks while modifying
|
|
|
|
* this data strcuture, because we may be doing
|
|
|
|
* a background transfer. Then we can free all
|
|
|
|
* the memory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
PR_FREEIF(autoupdt->id);
|
1998-09-02 23:08:00 +04:00
|
|
|
PR_FREEIF(autoupdt->url);
|
|
|
|
PR_FREEIF(autoupdt->outFile);
|
|
|
|
/* Don't free the autoupdt memory because Timer may still have
|
|
|
|
* a pointer to it.
|
|
|
|
*/
|
|
|
|
memset(autoupdt, '\0', sizeof(AutoUpdateConnnectionStruct));
|
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
/* XXX: Remove the object from the global list */
|
|
|
|
gAutoUpdateConnnection = NULL;
|
1998-09-02 23:08:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
|
1998-09-02 23:08:00 +04:00
|
|
|
static void
|
1998-10-28 23:16:20 +03:00
|
|
|
autoupdate_Flush(AutoUpdateConnnection autoupdt)
|
1998-09-02 23:08:00 +04:00
|
|
|
{
|
|
|
|
if (autoupdt == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((autoupdt->cur_len > 0) && (autoupdt->outputBuffer != NULL) &&
|
|
|
|
(autoupdt->outFile != NULL)) {
|
|
|
|
FILE* fp = fopen(autoupdt->outFile, "ab+");
|
|
|
|
if (fp != NULL) {
|
|
|
|
fwrite(autoupdt->outputBuffer, 1, autoupdt->cur_len, fp);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
/* Setup start and end bytes for the next round of download */
|
1998-09-02 23:08:00 +04:00
|
|
|
autoupdt->start_byte = autoupdt->start_byte + autoupdt->cur_len;
|
|
|
|
autoupdt->end_byte = autoupdt->start_byte + autoupdt->range;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (autoupdt->outputBuffer != NULL) {
|
|
|
|
PR_FREEIF(autoupdt->outputBuffer);
|
|
|
|
}
|
|
|
|
autoupdt->outputBuffer = NULL;
|
|
|
|
autoupdt->cur_len = 0;
|
|
|
|
autoupdt->urls = NULL;
|
1998-10-28 23:16:20 +03:00
|
|
|
autoupdt->status = 0;
|
1998-09-02 23:08:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
PR_STATIC_CALLBACK(unsigned int)
|
|
|
|
autoupdate_WriteReadyNetStream(NET_StreamClass *stream)
|
1998-09-02 23:08:00 +04:00
|
|
|
{
|
|
|
|
/* tell it how many bytes you are willing to take */
|
|
|
|
AutoUpdateConnnection autoupdt = (AutoUpdateConnnection)stream->data_object;
|
|
|
|
return autoupdt->range;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
PR_STATIC_CALLBACK(int)
|
|
|
|
autoupdate_WriteNetStream(NET_StreamClass *stream, const char* blob, int32 size)
|
1998-09-02 23:08:00 +04:00
|
|
|
{
|
|
|
|
/* stream->data_object holds your connection data.
|
|
|
|
* return 0 or greater to get more bytes
|
|
|
|
*/
|
|
|
|
AutoUpdateConnnection autoupdt = (AutoUpdateConnnection)stream->data_object;
|
|
|
|
|
|
|
|
if ((autoupdt == NULL) || (autoupdt->outFile == NULL) ||
|
|
|
|
(autoupdt->range == 0) || (autoupdt->outputBuffer == NULL)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (autoupdt->cur_len+size > autoupdt->range) {
|
|
|
|
autoupdt->outputBuffer = PR_REALLOC(autoupdt->outputBuffer,
|
|
|
|
autoupdt->cur_len+size);
|
|
|
|
if (!autoupdt->outputBuffer) {
|
|
|
|
autoupdt->cur_len = 0;
|
1998-10-28 23:16:20 +03:00
|
|
|
TRACEMSG(("Out of memory in autoupdate_WriteNetStream\n"));
|
1998-09-02 23:08:00 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memcpy(&(autoupdt->outputBuffer[autoupdt->cur_len]), blob, size);
|
|
|
|
autoupdt->cur_len += size;
|
|
|
|
if (autoupdt->cur_len >= autoupdt->range) {
|
|
|
|
/* We got the range of bytes we wanted to get */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return autoupdt->range;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
PR_STATIC_CALLBACK(void)
|
|
|
|
autoupdate_AbortNetStream(NET_StreamClass *stream, int status)
|
1998-09-02 23:08:00 +04:00
|
|
|
{
|
|
|
|
/* something bad happened */
|
|
|
|
AutoUpdateConnnection autoupdt = (AutoUpdateConnnection)stream->data_object;
|
1998-10-28 23:16:20 +03:00
|
|
|
autoupdate_Flush(autoupdt);
|
1998-09-02 23:08:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
PR_STATIC_CALLBACK(void)
|
|
|
|
autoupdate_CompleteNetStream(NET_StreamClass *stream)
|
1998-09-02 23:08:00 +04:00
|
|
|
{
|
|
|
|
/* the complete function which gets called when the last packet comes in */
|
|
|
|
AutoUpdateConnnection autoupdt = (AutoUpdateConnnection)stream->data_object;
|
1998-10-28 23:16:20 +03:00
|
|
|
autoupdate_Flush(autoupdt);
|
1998-09-02 23:08:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
PR_IMPLEMENT(NET_StreamClass *)
|
|
|
|
Autoupdate_Converter(FO_Present_Types format_out, void *data_object,
|
1998-09-02 23:08:00 +04:00
|
|
|
URL_Struct *URL_s, MWContext *cx)
|
|
|
|
{
|
1998-10-28 23:16:20 +03:00
|
|
|
AutoUpdateConnnection autoupdt = (AutoUpdateConnnection) URL_s->fe_data;
|
|
|
|
|
|
|
|
if ((autoupdt == NULL) || (autoupdt->url == NULL)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!URL_s->server_can_do_byteranges) {
|
|
|
|
autoupdt->state = BACKGROUND_ERROR;
|
|
|
|
autoupdt->errorMsg = "Server doesn't support byte ranges.";
|
|
|
|
autoupdate_LoadScript(autoupdt->mwcontext, autoupdt->script);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACEMSG(("Setting up Autoupdate stream. Have URL: %s %s\n",
|
|
|
|
autoupdt->url, URL_s->address));
|
1998-09-02 23:08:00 +04:00
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
autoupdt->status = 1;
|
|
|
|
return NET_NewStream("AUTOUPDATE", (MKStreamWriteFunc)autoupdate_WriteNetStream,
|
|
|
|
(MKStreamCompleteFunc)autoupdate_CompleteNetStream,
|
|
|
|
(MKStreamAbortFunc)autoupdate_AbortNetStream,
|
|
|
|
(MKStreamWriteReadyFunc)autoupdate_WriteReadyNetStream,
|
|
|
|
URL_s->fe_data, cx);
|
1998-09-02 23:08:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
PR_STATIC_CALLBACK(void)
|
1998-09-02 23:08:00 +04:00
|
|
|
autoupdate_GetUrlExitFunc (URL_Struct *urls, int status, MWContext *cx)
|
|
|
|
{
|
|
|
|
NET_FreeURLStruct (urls);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
1998-10-28 23:16:20 +03:00
|
|
|
autoupdate_LoadURL(AutoUpdateConnnection autoupdt)
|
1998-09-02 23:08:00 +04:00
|
|
|
{
|
|
|
|
URL_Struct *urls;
|
|
|
|
char *range=NULL;
|
1998-10-28 23:16:20 +03:00
|
|
|
|
|
|
|
if (NET_AreThereActiveConnections()) {
|
|
|
|
/* libnet is busy. Try later */
|
1998-09-02 23:08:00 +04:00
|
|
|
return;
|
|
|
|
}
|
1998-10-28 23:16:20 +03:00
|
|
|
|
|
|
|
/* If we haven't got all the bytes from the previous request
|
|
|
|
* don't start a new one again, until we get all the bytes.
|
|
|
|
*
|
|
|
|
* This check prevents if NET_AreThereActiveConnections
|
|
|
|
* return FALSE, but we haven't cleared autoupdt's buffers yet.
|
|
|
|
*/
|
|
|
|
if (autoupdt->status == 1) {
|
1998-09-02 23:08:00 +04:00
|
|
|
return;
|
|
|
|
}
|
1998-10-28 23:16:20 +03:00
|
|
|
|
1998-09-02 23:08:00 +04:00
|
|
|
urls = NET_CreateURLStruct(autoupdt->url, NET_DONT_RELOAD);
|
|
|
|
if (urls == NULL) {
|
1998-10-28 23:16:20 +03:00
|
|
|
autoupdt->state = BACKGROUND_ERROR;
|
|
|
|
autoupdt->errorMsg = "Couldn't set up download. Out of memory";
|
|
|
|
autoupdate_LoadScript(autoupdt->mwcontext, autoupdt->script);
|
1998-09-02 23:08:00 +04:00
|
|
|
return;
|
|
|
|
}
|
1998-10-28 23:16:20 +03:00
|
|
|
|
1998-09-02 23:08:00 +04:00
|
|
|
urls->fe_data = autoupdt;
|
|
|
|
urls->range_header = PR_sprintf_append(range, "bytes=%ld-%ld",
|
|
|
|
autoupdt->start_byte,
|
|
|
|
autoupdt->end_byte);
|
|
|
|
|
|
|
|
autoupdt->outputBuffer = PR_Malloc(autoupdt->range);
|
|
|
|
memset(autoupdt->outputBuffer, '\0', autoupdt->range);
|
|
|
|
autoupdt->cur_len = 0;
|
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
autoupdt->status = 0;
|
1998-09-02 23:08:00 +04:00
|
|
|
NET_GetURL(urls, FO_AUTOUPDATE, autoupdt->mwcontext, autoupdate_GetUrlExitFunc);
|
1998-10-28 23:16:20 +03:00
|
|
|
autoupdt->urls = urls;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PRBool
|
|
|
|
autoupdate_IsInQueue(AutoUpdateConnnection autoupdt)
|
|
|
|
{
|
|
|
|
if (gAutoUpdateConnnection == autoupdt)
|
|
|
|
return PR_TRUE;
|
|
|
|
return PR_FALSE;
|
1998-09-02 23:08:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
autoupdate_TimerCallback(void* data)
|
|
|
|
{
|
|
|
|
AutoUpdateConnnection autoupdt = (AutoUpdateConnnection)data;
|
1998-10-28 23:16:20 +03:00
|
|
|
|
|
|
|
/* if there is no autoupdate, or the given object is not in our
|
|
|
|
* list of things to do, then stop the timer callback.
|
|
|
|
*/
|
|
|
|
if ((autoupdt == NULL) || (!autoupdate_IsInQueue(autoupdt))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (autoupdt->url == NULL) {
|
|
|
|
autoupdt->state = BACKGROUND_ERROR;
|
|
|
|
autoupdt->errorMsg = "No URL to download";
|
|
|
|
autoupdate_LoadScript(autoupdt->mwcontext, autoupdt->script);
|
1998-09-02 23:08:00 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
if ((autoupdt->start_byte == 0) ||
|
|
|
|
(autoupdt->file_size > autoupdt->start_byte)) {
|
|
|
|
|
|
|
|
/* We will download bytes if the state is in DOWN_LOADING state.
|
|
|
|
* The Script needs to call resume to set the state into DOWN_LOADING
|
|
|
|
* and to clear the ERROR or move it from SUSPEND/NEW states.
|
|
|
|
* If the state is either COMPLETE or DONE, we don't
|
|
|
|
* need to do anything.
|
|
|
|
*
|
|
|
|
* We won't enable the timer callbacks, until the script does the
|
|
|
|
* resume
|
|
|
|
*/
|
|
|
|
if (autoupdt->state != BACKGROUND_DOWN_LOADING) {
|
|
|
|
return;
|
|
|
|
}
|
1998-09-02 23:08:00 +04:00
|
|
|
autoupdt->timeout = FE_SetTimeout(autoupdate_TimerCallback,
|
|
|
|
(void*)autoupdt, autoupdt->interval);
|
1998-10-28 23:16:20 +03:00
|
|
|
autoupdate_LoadURL(autoupdt);
|
1998-09-02 23:08:00 +04:00
|
|
|
} else {
|
1998-10-28 23:16:20 +03:00
|
|
|
autoupdt->state = BACKGROUND_COMPLETE;
|
|
|
|
autoupdate_LoadScript(autoupdt->mwcontext, autoupdt->script);
|
1998-09-02 23:08:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
PR_IMPLEMENT(void)
|
|
|
|
Autoupdate_Suspend(AutoUpdateConnnection autoupdt)
|
1998-09-02 23:08:00 +04:00
|
|
|
{
|
1998-10-28 23:16:20 +03:00
|
|
|
if (autoupdt == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
autoupdt->state = BACKGROUND_SUSPEND;
|
|
|
|
/* We need to stop everything right away. They way it is now,
|
|
|
|
* it will stop next time around.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_IMPLEMENT(void)
|
|
|
|
Autoupdate_Resume(AutoUpdateConnnection autoupdt)
|
|
|
|
{
|
|
|
|
if (autoupdt == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
autoupdt->state = BACKGROUND_DOWN_LOADING;
|
|
|
|
autoupdate_TimerCallback((void *)autoupdt);
|
1998-09-02 23:08:00 +04:00
|
|
|
}
|
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
|
|
|
|
PR_IMPLEMENT(void)
|
|
|
|
Autoupdate_Done(AutoUpdateConnnection autoupdt)
|
|
|
|
{
|
|
|
|
if (autoupdt == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
autoupdate_Free(autoupdt);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PR_IMPLEMENT(void)
|
|
|
|
Autoupdate_DownloadNow(AutoUpdateConnnection autoupdt)
|
|
|
|
{
|
|
|
|
if (autoupdt == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
autoupdt->state = BACKGROUND_DOWN_LOADING_NOW;
|
|
|
|
/* We should initiate a new command to donwload all bytes.
|
|
|
|
* May be we should directly all bytes into the file, instead of
|
|
|
|
* trying to store data into a buffer.
|
|
|
|
*
|
|
|
|
* Changing the state to BACKGROUND_DOWN_LOADING_NOW, will stop
|
|
|
|
* the background download.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PR_IMPLEMENT(void)
|
|
|
|
Autoupdate_Abort(AutoUpdateConnnection autoupdt)
|
1998-09-02 23:08:00 +04:00
|
|
|
{
|
1998-10-28 23:16:20 +03:00
|
|
|
if (autoupdt == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: We should remove the downloaded file.
|
|
|
|
* We should free up the memory.
|
|
|
|
* We should disable TimerCallbacks for this object.
|
|
|
|
*/
|
|
|
|
autoupdate_Free(autoupdt);
|
1998-09-02 23:08:00 +04:00
|
|
|
}
|
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
|
1998-09-22 08:15:00 +04:00
|
|
|
static char*
|
|
|
|
autoupdate_AssureDir(char* path)
|
|
|
|
{
|
|
|
|
char *autoupdt_dir = PR_smprintf("%sautoupdt", 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 autoupdate. */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return autoupdt_dir;
|
|
|
|
}
|
1998-09-12 06:37:24 +04:00
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
|
|
|
|
PR_IMPLEMENT(AutoUpdateConnnection)
|
|
|
|
AutoUpdate_Setup(MWContext* cx, char* id, char* url, int32 file_size,
|
|
|
|
char* script)
|
1998-09-02 23:08:00 +04:00
|
|
|
{
|
1998-09-13 06:02:51 +04:00
|
|
|
int32 bytes_range;
|
|
|
|
int32 interval;
|
1998-09-02 23:08:00 +04:00
|
|
|
char *directory = NULL;
|
|
|
|
#ifdef WIN32
|
|
|
|
char Path[_MAX_PATH+1];
|
|
|
|
#else
|
|
|
|
char Path[MAXPATHLEN+1];
|
|
|
|
#endif
|
|
|
|
char *filename;
|
|
|
|
char *slash;
|
|
|
|
PRInt32 cur_size;
|
1998-09-12 06:37:24 +04:00
|
|
|
AutoUpdateConnnection autoupdt;
|
|
|
|
XP_Bool enabled;
|
1998-09-22 08:15:00 +04:00
|
|
|
char *autoupdt_dir;
|
1998-09-12 06:37:24 +04:00
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
|
1998-09-12 06:37:24 +04:00
|
|
|
PREF_GetBoolPref( "autoupdate.background_download_enabled", &enabled);
|
|
|
|
if (!enabled)
|
1998-10-28 23:16:20 +03:00
|
|
|
return NULL;
|
1998-09-12 06:37:24 +04:00
|
|
|
|
|
|
|
if (PREF_OK != PREF_CopyCharPref("autoupdate.background_download_directory",
|
|
|
|
&directory)) {
|
|
|
|
directory = NULL;
|
|
|
|
} else {
|
|
|
|
if ((directory) && (XP_STRCMP(directory, "") == 0)) {
|
|
|
|
directory = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-09-13 06:02:51 +04:00
|
|
|
if (PREF_OK != PREF_GetIntPref("autoupdate.background_download_byte_range",
|
|
|
|
&bytes_range)) {
|
|
|
|
bytes_range = 3000;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PREF_OK != PREF_GetIntPref("autoupdate.background_download_interval",
|
|
|
|
&interval)) {
|
|
|
|
interval = 10000;
|
|
|
|
}
|
1998-09-02 23:08:00 +04:00
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
/* TODO: XXX Support downloading of multiple downloads */
|
|
|
|
if (gAutoUpdateConnnection)
|
|
|
|
return gAutoUpdateConnnection;
|
|
|
|
|
1998-09-12 06:37:24 +04:00
|
|
|
autoupdt = PR_Malloc(sizeof(AutoUpdateConnnectionStruct));
|
1998-09-02 23:08:00 +04:00
|
|
|
memset(autoupdt, '\0', sizeof(AutoUpdateConnnectionStruct));
|
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
autoupdt->id = PL_strdup(id);
|
|
|
|
autoupdt->url = PL_strdup(url);
|
1998-09-02 23:08:00 +04:00
|
|
|
autoupdt->file_size = file_size;
|
|
|
|
autoupdt->interval = interval;
|
1998-10-28 23:16:20 +03:00
|
|
|
autoupdt->range = bytes_range;
|
|
|
|
autoupdt->script = PL_strdup(script);
|
1998-09-02 23:08:00 +04:00
|
|
|
|
|
|
|
slash = PL_strrchr(url, '/');
|
|
|
|
if (slash != NULL) {
|
|
|
|
filename = ++slash;
|
|
|
|
} else {
|
|
|
|
filename = "prg";
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef XP_UNIX
|
|
|
|
|
|
|
|
if (directory) {
|
|
|
|
PR_snprintf( Path, MAXPATHLEN, "%s/", directory);
|
1998-09-12 06:37:24 +04:00
|
|
|
PR_FREEIF(directory);
|
1998-09-02 23:08:00 +04:00
|
|
|
} else {
|
1998-09-12 06:37:24 +04:00
|
|
|
directory = getenv("MOZILLA_HOME");
|
|
|
|
if (directory) {
|
|
|
|
PR_snprintf( Path, MAXPATHLEN, "%s/", directory);
|
|
|
|
} else {
|
|
|
|
fe_GetProgramDirectory( Path, MAXPATHLEN-1 );
|
|
|
|
}
|
1998-09-02 23:08:00 +04:00
|
|
|
}
|
1998-09-22 08:15:00 +04:00
|
|
|
autoupdt_dir = autoupdate_AssureDir(Path);
|
|
|
|
if (autoupdt_dir != NULL) {
|
|
|
|
autoupdt->outFile = PR_smprintf("%s/%s", autoupdt_dir, filename);
|
|
|
|
PR_FREEIF(autoupdt_dir);
|
|
|
|
}
|
1998-09-02 23:08:00 +04:00
|
|
|
#elif defined(WIN32)
|
|
|
|
|
1998-09-12 06:37:24 +04:00
|
|
|
if (directory) {
|
|
|
|
PR_snprintf( Path, MAXPATHLEN, "%s\\", directory);
|
|
|
|
PR_FREEIF(directory);
|
|
|
|
} else {
|
|
|
|
FE_GetProgramDirectory( Path, _MAX_PATH );
|
|
|
|
}
|
1998-09-22 08:15:00 +04:00
|
|
|
autoupdt_dir = autoupdate_AssureDir(Path);
|
|
|
|
if (autoupdt_dir != NULL) {
|
|
|
|
autoupdt->outFile = PR_smprintf("%s\\%s", autoupdt_dir, filename);
|
|
|
|
PR_FREEIF(autoupdt_dir);
|
|
|
|
}
|
1998-09-02 23:08:00 +04:00
|
|
|
|
|
|
|
#elif defined(MAC)
|
|
|
|
/* XXX: Fix it for Mac with the correct folder */
|
1998-09-12 06:37:24 +04:00
|
|
|
if (directory) {
|
|
|
|
PR_snprintf( Path, MAXPATHLEN, "%s:", directory);
|
|
|
|
PR_FREEIF(directory);
|
|
|
|
} else {
|
|
|
|
directory = XP_TempDirName();
|
|
|
|
PR_snprintf( Path, MAXPATHLEN, "%s:", directory);
|
|
|
|
}
|
|
|
|
autoupdt->outFile = PR_smprintf("%s:%s", Path, filename);
|
1998-09-02 23:08:00 +04:00
|
|
|
#else
|
|
|
|
autoupdt->outFile = NULL;
|
|
|
|
#endif
|
1998-09-12 06:37:24 +04:00
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
autoupdt->outputBuffer = NULL;
|
|
|
|
autoupdt->cur_len = 0;
|
|
|
|
autoupdt->mwcontext = cx;
|
|
|
|
autoupdt->start_byte = 0;
|
|
|
|
autoupdt->end_byte = 0;
|
|
|
|
|
1998-09-02 23:08:00 +04:00
|
|
|
if (autoupdt->outFile == NULL) {
|
1998-10-28 23:16:20 +03:00
|
|
|
autoupdt->state = BACKGROUND_ERROR;
|
|
|
|
autoupdt->errorMsg = "Couldn't access destination directory to save file";
|
|
|
|
return autoupdt;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur_size = autoupdate_GetCurrentSize(autoupdt->outFile);
|
|
|
|
autoupdt->start_byte = cur_size;
|
|
|
|
if (autoupdt->start_byte == 0) {
|
|
|
|
autoupdt->state = BACKGROUND_NEW;
|
|
|
|
autoupdt->end_byte = cur_size + bytes_range;
|
|
|
|
} else if (cur_size >= autoupdt->file_size) {
|
|
|
|
autoupdt->end_byte = cur_size;
|
|
|
|
autoupdt->state = BACKGROUND_COMPLETE;
|
|
|
|
} else {
|
|
|
|
autoupdt->end_byte = cur_size + bytes_range;
|
|
|
|
autoupdt->state = BACKGROUND_SUSPEND;
|
1998-09-02 23:08:00 +04:00
|
|
|
}
|
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
/* TODO: XXX Push the object into the link list */
|
|
|
|
gAutoUpdateConnnection = autoupdt;
|
|
|
|
return autoupdt;
|
|
|
|
}
|
1998-09-02 23:08:00 +04:00
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
static void
|
|
|
|
autoupdate_LoadScript(MWContext* context, char* script)
|
|
|
|
{
|
|
|
|
MWContext* new_context;
|
|
|
|
Chrome chrome;
|
|
|
|
URL_Struct* urls;
|
|
|
|
|
|
|
|
/* if there is no JS to run, then exit.
|
|
|
|
*/
|
|
|
|
if ((script == NULL) || (context == NULL)) {
|
1998-09-02 23:08:00 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
XP_BZERO(&chrome, sizeof(Chrome));
|
|
|
|
chrome.location_is_chrome = TRUE;
|
|
|
|
chrome.type = MWContextDialog;
|
|
|
|
chrome.l_hint = -3000;
|
|
|
|
chrome.t_hint = -3000;
|
|
|
|
urls = NET_CreateURLStruct(script, NET_DONT_RELOAD);
|
|
|
|
if (urls == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
new_context = FE_MakeNewWindow(context, NULL, NULL, &chrome);
|
|
|
|
if (new_context == NULL)
|
|
|
|
return;
|
|
|
|
(void)FE_GetURL(new_context, urls);
|
|
|
|
}
|
|
|
|
|
1998-09-02 23:08:00 +04:00
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
static void
|
|
|
|
autoupdate_InvokeJSTimerCallback(void* data)
|
|
|
|
{
|
|
|
|
autoupdate_LoadScript((MWContext*)data, gMainScript);
|
1998-09-02 23:08:00 +04:00
|
|
|
}
|
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
PR_IMPLEMENT(void)
|
|
|
|
AutoUpdate_LoadMainScript(MWContext * context, char* url)
|
|
|
|
{
|
|
|
|
gMainScript = PL_strdup(url);
|
|
|
|
/* May be we should download the script */
|
|
|
|
FE_SetTimeout(autoupdate_InvokeJSTimerCallback,
|
|
|
|
(void*)context, 3000);
|
|
|
|
}
|
1998-09-02 23:08:00 +04:00
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
PR_IMPLEMENT(const char*)
|
|
|
|
AutoUpdate_GetID(AutoUpdateConnnection autoupdt)
|
|
|
|
{
|
|
|
|
return autoupdt->id;
|
|
|
|
}
|
1998-09-02 23:08:00 +04:00
|
|
|
|
|
|
|
|
1998-10-28 23:16:20 +03:00
|
|
|
PR_IMPLEMENT(const char*)
|
|
|
|
AutoUpdate_GetURL(AutoUpdateConnnection autoupdt)
|
|
|
|
{
|
|
|
|
return autoupdt->url;
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_IMPLEMENT(BackgroundState)
|
|
|
|
AutoUpdate_GetState(AutoUpdateConnnection autoupdt)
|
|
|
|
{
|
|
|
|
return autoupdt->state;
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_IMPLEMENT(int32)
|
|
|
|
AutoUpdate_GetFileSize(AutoUpdateConnnection autoupdt)
|
|
|
|
{
|
|
|
|
return autoupdt->file_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_IMPLEMENT(int32)
|
|
|
|
AutoUpdate_GetCurrentFileSize(AutoUpdateConnnection autoupdt)
|
|
|
|
{
|
|
|
|
return autoupdt->start_byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_IMPLEMENT(const char*)
|
|
|
|
AutoUpdate_GetDestFile(AutoUpdateConnnection autoupdt)
|
|
|
|
{
|
|
|
|
return autoupdt->outFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_IMPLEMENT(int32)
|
|
|
|
AutoUpdate_GetBytesRange(AutoUpdateConnnection autoupdt)
|
|
|
|
{
|
|
|
|
return autoupdt->range;
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_IMPLEMENT(uint32)
|
|
|
|
AutoUpdate_GetInterval(AutoUpdateConnnection autoupdt)
|
|
|
|
{
|
|
|
|
return autoupdt->interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_IMPLEMENT(const char*)
|
|
|
|
AutoUpdate_GetErrorMessage(AutoUpdateConnnection autoupdt)
|
|
|
|
{
|
|
|
|
return autoupdt->errorMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PR_IMPLEMENT(AutoUpdateConnnection)
|
|
|
|
AutoUpdate_GetPending(char* id)
|
|
|
|
{
|
|
|
|
/* XXX: We should look up in a hash table and return the object.
|
|
|
|
* Because we have only one download going on, just return the global object
|
|
|
|
*/
|
|
|
|
return gAutoUpdateConnnection;
|
|
|
|
}
|