2000-06-17 01:35:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public
|
|
|
|
* License Version 1.1 (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/MPL/
|
|
|
|
*
|
|
|
|
* 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 the Mozilla browser.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
|
|
|
* Communications, Inc. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1999, Mozilla. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Scott MacGregor <mscott@netscape.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsExternalHelperAppService.h"
|
|
|
|
#include "nsIURI.h"
|
2000-06-17 04:06:26 +04:00
|
|
|
#include "nsIURL.h"
|
2000-06-17 01:35:13 +04:00
|
|
|
#include "nsIFile.h"
|
2000-06-17 04:06:26 +04:00
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsXPIDLString.h"
|
2000-06-17 23:53:44 +04:00
|
|
|
#include "nsMemory.h"
|
2000-06-17 01:35:13 +04:00
|
|
|
#include "nsIStreamListener.h"
|
2000-06-18 03:04:53 +04:00
|
|
|
#include "nsCExternalHelperApp.h" // contains progids for the helper app service
|
2000-06-17 01:35:13 +04:00
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ADDREF(nsExternalHelperAppService)
|
|
|
|
NS_IMPL_THREADSAFE_RELEASE(nsExternalHelperAppService)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsExternalHelperAppService)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIExternalHelperAppService)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIExternalHelperAppService)
|
2000-06-18 03:04:53 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsPIExternalAppLauncher)
|
2000-06-17 01:35:13 +04:00
|
|
|
NS_INTERFACE_MAP_END_THREADSAFE
|
|
|
|
|
|
|
|
nsExternalHelperAppService::nsExternalHelperAppService()
|
|
|
|
{
|
|
|
|
NS_INIT_ISUPPORTS();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsExternalHelperAppService::~nsExternalHelperAppService()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/* boolean canHandleContent (in string aMimeContentType); */
|
2000-06-17 04:06:26 +04:00
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::CanHandleContent(const char *aMimeContentType, nsIURI * aURI, PRBool *_retval)
|
2000-06-17 01:35:13 +04:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* nsIStreamListener doContent (in string aMimeContentType, in nsIURI aURI, in nsISupports aWindowContext, out boolean aAbortProcess); */
|
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::DoContent(const char *aMimeContentType, nsIURI *aURI, nsISupports *aWindowContext, PRBool *aAbortProcess, nsIStreamListener **_retval)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2000-06-18 03:04:53 +04:00
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::LaunchAppWithTempFile(nsIFile * aTempFile, nsISupports * aAppCookie)
|
|
|
|
{
|
|
|
|
// this method should only be implemented by each OS specific implementation of this service.
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2000-06-18 05:13:04 +04:00
|
|
|
nsExternalAppHandler * nsExternalHelperAppService::CreateNewExternalHandler(nsISupports * aAppCookie,
|
|
|
|
const char * aTempFileExtension)
|
2000-06-17 04:06:26 +04:00
|
|
|
{
|
|
|
|
nsExternalAppHandler* handler = nsnull;
|
|
|
|
NS_NEWXPCOM(handler, nsExternalAppHandler);
|
|
|
|
// add any XP intialization code for an external handler that we may need here...
|
|
|
|
// right now we don't have any but i bet we will before we are done.
|
2000-06-18 03:04:53 +04:00
|
|
|
|
2000-06-18 05:13:04 +04:00
|
|
|
handler->Init(aAppCookie, aTempFileExtension);
|
2000-06-17 04:06:26 +04:00
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
2000-06-17 01:35:13 +04:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// begin external app handler implementation
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ADDREF(nsExternalAppHandler)
|
|
|
|
NS_IMPL_THREADSAFE_RELEASE(nsExternalAppHandler)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsExternalAppHandler)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIStreamListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStreamObserver)
|
|
|
|
NS_INTERFACE_MAP_END_THREADSAFE
|
|
|
|
|
|
|
|
nsExternalAppHandler::nsExternalAppHandler()
|
|
|
|
{
|
|
|
|
NS_INIT_ISUPPORTS();
|
2000-06-17 23:53:44 +04:00
|
|
|
|
|
|
|
mDataBuffer = (char *) nsMemory::Alloc((sizeof(char) * DATA_BUFFER_SIZE));
|
2000-06-17 01:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsExternalAppHandler::~nsExternalAppHandler()
|
|
|
|
{
|
2000-06-17 23:53:44 +04:00
|
|
|
if (mDataBuffer)
|
|
|
|
nsMemory::Free(mDataBuffer);
|
2000-06-17 01:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsExternalAppHandler::OnStartRequest(nsIChannel * aChannel, nsISupports * aCtxt)
|
|
|
|
{
|
2000-06-17 04:06:26 +04:00
|
|
|
NS_ENSURE_ARG(aChannel);
|
|
|
|
|
2000-06-17 01:35:13 +04:00
|
|
|
// create a temp file for the data...and open it for writing.
|
2000-06-17 04:06:26 +04:00
|
|
|
NS_GetSpecialDirectory("system.OS_TemporaryDirectory", getter_AddRefs(mTempFile));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
aChannel->GetURI(getter_AddRefs(uri));
|
|
|
|
nsCOMPtr<nsIURL> url = do_QueryInterface(uri);
|
|
|
|
|
2000-06-18 05:13:04 +04:00
|
|
|
nsCAutoString tempLeafName ("test"); // WARNING THIS IS TEMPORARY CODE!!!
|
|
|
|
tempLeafName.Append(mTempFileExtension);
|
|
|
|
|
|
|
|
#if 0
|
2000-06-17 04:06:26 +04:00
|
|
|
if (url)
|
|
|
|
{
|
|
|
|
// try to extract the file name from the url and use that as a first pass as the
|
|
|
|
// leaf name of our temp file...
|
|
|
|
nsXPIDLCString leafName;
|
|
|
|
url->GetFileName(getter_Copies(leafName));
|
|
|
|
if (leafName)
|
|
|
|
{
|
2000-06-18 05:13:04 +04:00
|
|
|
nsCAutoString
|
2000-06-17 04:06:26 +04:00
|
|
|
mTempFile->Append(leafName); // WARNING --> neeed a make Unique routine on nsIFile!!
|
|
|
|
}
|
|
|
|
else
|
2000-06-18 05:13:04 +04:00
|
|
|
mTempFile->Append("test"); // WARNING THIS IS TEMPORARY CODE!!!
|
2000-06-17 04:06:26 +04:00
|
|
|
}
|
|
|
|
else
|
2000-06-18 05:13:04 +04:00
|
|
|
mTempFile->Append("test"); // WARNING THIS IS TEMPORARY CODE!!!
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mTempFile->Append(tempLeafName); // make this file unique!!!
|
2000-06-17 04:06:26 +04:00
|
|
|
|
2000-06-17 06:34:44 +04:00
|
|
|
nsCOMPtr<nsIFileChannel> mFileChannel = do_CreateInstance(NS_LOCALFILECHANNEL_PROGID);
|
|
|
|
if (mFileChannel)
|
|
|
|
{
|
2000-06-17 23:53:44 +04:00
|
|
|
mFileChannel->Init(mTempFile, -1, 0);
|
|
|
|
mFileChannel->OpenOutputStream(getter_AddRefs(mOutStream));
|
2000-06-17 06:34:44 +04:00
|
|
|
}
|
|
|
|
|
2000-06-17 01:35:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsExternalAppHandler::OnDataAvailable(nsIChannel * aChannel, nsISupports * aCtxt,
|
|
|
|
nsIInputStream * inStr, PRUint32 sourceOffset, PRUint32 count)
|
|
|
|
{
|
|
|
|
// read the data out of the stream and write it to the temp file.
|
2000-06-17 06:34:44 +04:00
|
|
|
PRUint32 numBytesRead = 0;
|
2000-06-17 23:53:44 +04:00
|
|
|
if (mOutStream && mDataBuffer && count > 0)
|
|
|
|
{
|
|
|
|
PRUint32 numBytesRead = 0;
|
|
|
|
PRUint32 numBytesWritten = 0;
|
|
|
|
while (count > 0) // while we still have bytes to copy...
|
|
|
|
{
|
|
|
|
inStr->Read(mDataBuffer, PR_MIN(count, DATA_BUFFER_SIZE - 1), &numBytesRead);
|
|
|
|
if (count >= numBytesRead)
|
|
|
|
count -= numBytesRead; // subtract off the number of bytes we just read
|
|
|
|
else
|
|
|
|
count = 0;
|
|
|
|
mOutStream->Write(mDataBuffer, numBytesRead, &numBytesWritten);
|
|
|
|
}
|
|
|
|
}
|
2000-06-17 01:35:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsExternalAppHandler::OnStopRequest(nsIChannel * aChannel, nsISupports *aCtxt,
|
|
|
|
nsresult aStatus, const PRUnichar * errorMsg)
|
|
|
|
{
|
2000-06-18 03:04:53 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2000-06-17 01:35:13 +04:00
|
|
|
// go ahead and execute the application passing in our temp file as an argument
|
2000-06-17 04:06:26 +04:00
|
|
|
// this may involve us calling back into the OS external app service to make the call
|
|
|
|
// for actually launching the helper app. It'd be great if nsIFile::spawn could be made to work
|
|
|
|
// on the mac...right now the mac implementation ignores all arguments passed in.
|
2000-06-17 06:34:44 +04:00
|
|
|
|
|
|
|
// close the stream...
|
2000-06-17 23:53:44 +04:00
|
|
|
if (mOutStream)
|
|
|
|
mOutStream->Close();
|
2000-06-17 06:34:44 +04:00
|
|
|
|
2000-06-18 03:04:53 +04:00
|
|
|
nsCOMPtr<nsPIExternalAppLauncher> helperAppService (do_GetService(NS_EXTERNALHELPERAPPSERVICE_PROGID));
|
|
|
|
if (helperAppService)
|
|
|
|
{
|
|
|
|
rv = helperAppService->LaunchAppWithTempFile(mTempFile, mExternalApplication);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2000-06-17 01:35:13 +04:00
|
|
|
}
|
|
|
|
|
2000-06-18 05:13:04 +04:00
|
|
|
nsresult nsExternalAppHandler::Init(nsISupports * aAppCookie, const char * aTempFileExtension)
|
2000-06-17 06:34:44 +04:00
|
|
|
{
|
2000-06-18 03:04:53 +04:00
|
|
|
mExternalApplication = aAppCookie;
|
2000-06-18 05:13:04 +04:00
|
|
|
mTempFileExtension = aTempFileExtension;
|
2000-06-17 06:34:44 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|