if (contextType == nsIInternetSearchContext::ENGINE_DOWNLOAD_CONTEXT || contextType == nsIInternetSearchContext::ENGINE_UPDATE_CONTEXT)
if (contextType == nsIInternetSearchContext::ENGINE_DOWNLOAD_CONTEXT)
InternetSearchDataSource::saveContents(nsIChannel* channel, nsIInternetSearchContext *context, PRUint32	contextType){	nsresult	rv = NS_OK;	if (!channel)	return(NS_ERROR_UNEXPECTED);	if (!context)	return(NS_ERROR_UNEXPECTED);	// get real URI	nsCOMPtr<nsIURI>	channelURI;	if (NS_FAILED(rv = channel->GetURI(getter_AddRefs(channelURI))))		return(rv);	if (!channelURI)		return(NS_ERROR_NULL_POINTER);	nsCAutoString baseName;	if (NS_FAILED(rv = channelURI->GetSpec(baseName)))		return(rv);	PRInt32			slashOffset = baseName.RFindChar(PRUnichar('/'));	if (slashOffset < 0)		return(NS_ERROR_UNEXPECTED);	baseName.Cut(0, slashOffset+1);	if (baseName.Length() < 1)	return(NS_ERROR_UNEXPECTED);	// make sure that search engines are .src files	PRInt32	extensionOffset;	if (contextType == nsIInternetSearchContext::ENGINE_DOWNLOAD_CONTEXT ||		contextType == nsIInternetSearchContext::ENGINE_UPDATE_CONTEXT)	{		extensionOffset = baseName.RFind(".src", PR_TRUE);		if ((extensionOffset < 0) || (extensionOffset != (PRInt32)(baseName.Length()-4)))		{			return(NS_ERROR_UNEXPECTED);		}	}	nsCOMPtr<nsIFile>	outFile;	if (NS_FAILED(rv = GetSearchFolder(getter_AddRefs(outFile))))		return(rv);	const PRUnichar	*dataBuf = nsnull;	if (NS_FAILED(rv = context->GetBufferConst(&dataBuf)))	return(rv);	// if no data, then nothing to do	// Note: do this before opening file, as it would be truncated	PRInt32		bufferLength = 0;	if (NS_FAILED(context->GetBufferLength(&bufferLength)))	return(rv);	if (bufferLength < 1)	return(NS_OK);		rv = outFile->Append(NS_ConvertUTF8toUCS2(baseName));	if (NS_FAILED(rv)) return rv;		// save data to file	// Note: write out one character at a time, as we might be dealing	//       with binary data (such as 0x00) [especially for images]    //    // XXX - It appears that this is done in order to discard the upper    // byte of each PRUnichar.  I hope that's OK!!    //	outFile->Remove(PR_FALSE);    nsCOMPtr<nsIOutputStream> outputStream, fileOutputStream;    rv = NS_NewLocalFileOutputStream(getter_AddRefs(fileOutputStream), outFile);    if (NS_FAILED(rv)) return rv;    rv = NS_NewBufferedOutputStream(getter_AddRefs(outputStream), fileOutputStream, 4096);    if (NS_FAILED(rv)) return rv;    PRUint32 bytesWritten;    for (PRInt32 loop=0; loop < bufferLength; loop++)    {        const char b = (const char) dataBuf[loop];        outputStream->Write(&b, 1, &bytesWritten);    }    outputStream->Flush();		    outputStream->Close();    if (contextType == nsIInternetSearchContext::ENGINE_DOWNLOAD_CONTEXT ||        contextType == nsIInternetSearchContext::ENGINE_UPDATE_CONTEXT)    {#ifdef	XP_MAC        // set appropriate Mac file type/creator for search engine files        nsCOMPtr<nsILocalFileMac> macFile(do_QueryInterface(outFile));        if (macFile) {          macFile->SetFileType('issp');          macFile->SetFileCreator('fndf');        }#endif        // check suggested category hint        const PRUnichar	*hintUni = nsnull;        rv = context->GetHintConst(&hintUni);        // update graph with various required info        SaveEngineInfoIntoGraph(outFile, nsnull, hintUni, dataBuf, PR_FALSE, PR_FALSE);    }    else if (contextType == nsIInternetSearchContext::ICON_DOWNLOAD_CONTEXT)    {        // update graph with icon info        SaveEngineInfoIntoGraph(nsnull, outFile, nsnull, nsnull, PR_FALSE, PR_FALSE);    }	// after we're all done with the data buffer, get rid of it	context->Truncate();	return(rv);}