2009-10-07 01:30:26 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* 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 mozila.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
2010-09-05 11:00:05 -07:00
|
|
|
* Mozilla Foundation
|
2009-10-07 01:30:26 -07:00
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2009-11-09 12:05:16 -08:00
|
|
|
#include "nsDOMFileReader.h"
|
2009-10-07 01:30:26 -07:00
|
|
|
|
|
|
|
#include "nsContentCID.h"
|
|
|
|
#include "nsContentUtils.h"
|
2011-10-03 12:11:31 -07:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2009-10-07 01:30:26 -07:00
|
|
|
#include "nsDOMFile.h"
|
|
|
|
#include "nsDOMError.h"
|
2012-03-04 19:57:51 -08:00
|
|
|
#include "nsCharsetAlias.h"
|
2009-10-07 01:30:26 -07:00
|
|
|
#include "nsICharsetDetector.h"
|
|
|
|
#include "nsICharsetConverterManager.h"
|
|
|
|
#include "nsIConverterInputStream.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsIFileStreams.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIMIMEService.h"
|
|
|
|
#include "nsIPlatformCharset.h"
|
|
|
|
#include "nsIUnicodeDecoder.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
|
|
|
#include "plbase64.h"
|
|
|
|
#include "prmem.h"
|
|
|
|
|
|
|
|
#include "nsLayoutCID.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsIPrivateDOMEvent.h"
|
|
|
|
#include "nsIDOMEventListener.h"
|
|
|
|
#include "nsIJSContextStack.h"
|
|
|
|
#include "nsJSEnvironment.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsCExternalHandlerService.h"
|
|
|
|
#include "nsIStreamConverterService.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsLayoutStatics.h"
|
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
2012-01-12 02:36:03 -08:00
|
|
|
#include "nsBlobProtocolHandler.h"
|
2011-05-26 21:53:03 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2011-09-29 09:06:36 -07:00
|
|
|
#include "xpcpublic.h"
|
2011-12-24 00:27:04 -08:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2011-10-03 12:11:31 -07:00
|
|
|
#include "nsDOMJSUtils.h"
|
2012-02-07 18:53:33 -08:00
|
|
|
#include "nsDOMEventTargetHelper.h"
|
2011-05-26 21:53:03 -07:00
|
|
|
|
2011-12-24 00:27:04 -08:00
|
|
|
#include "jstypedarray.h"
|
|
|
|
|
2011-05-26 21:53:03 -07:00
|
|
|
using namespace mozilla;
|
2009-10-07 01:30:26 -07:00
|
|
|
|
|
|
|
#define LOAD_STR "load"
|
|
|
|
#define LOADSTART_STR "loadstart"
|
|
|
|
#define LOADEND_STR "loadend"
|
|
|
|
|
2011-09-29 09:06:35 -07:00
|
|
|
using mozilla::dom::FileIOObject;
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2009-11-09 12:05:16 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMFileReader)
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2009-11-09 12:05:16 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsDOMFileReader,
|
2011-09-29 09:06:35 -07:00
|
|
|
FileIOObject)
|
2009-10-07 01:30:26 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mFile)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mPrincipal)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mChannel)
|
2011-09-29 12:18:58 -07:00
|
|
|
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(load)
|
|
|
|
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(loadstart)
|
|
|
|
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(loadend)
|
2009-10-07 01:30:26 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2009-11-09 12:05:16 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsDOMFileReader,
|
2011-09-29 09:06:35 -07:00
|
|
|
FileIOObject)
|
2011-06-30 17:50:44 -07:00
|
|
|
tmp->mResultArrayBuffer = nsnull;
|
2009-10-07 01:30:26 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mFile)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mPrincipal)
|
2011-09-29 12:18:58 -07:00
|
|
|
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(load)
|
|
|
|
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(loadstart)
|
|
|
|
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(loadend)
|
2009-10-07 01:30:26 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2011-06-30 17:50:44 -07:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(nsDOMFileReader,
|
2012-02-07 18:53:33 -08:00
|
|
|
nsDOMEventTargetHelper)
|
2011-06-30 17:50:44 -07:00
|
|
|
if(tmp->mResultArrayBuffer) {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_CALLBACK(tmp->mResultArrayBuffer,
|
|
|
|
"mResultArrayBuffer")
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2010-01-12 05:08:43 -08:00
|
|
|
DOMCI_DATA(FileReader, nsDOMFileReader)
|
|
|
|
|
2009-11-09 12:05:16 -08:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsDOMFileReader)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMFileReader)
|
2009-10-07 01:30:26 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIJSNativeInitializer)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsICharsetDetectionObserver)
|
2010-03-17 08:09:05 -07:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(FileReader)
|
2011-09-29 09:06:35 -07:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(FileIOObject)
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2011-09-29 09:06:35 -07:00
|
|
|
NS_IMPL_ADDREF_INHERITED(nsDOMFileReader, FileIOObject)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsDOMFileReader, FileIOObject)
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2011-06-30 17:50:44 -07:00
|
|
|
void
|
|
|
|
nsDOMFileReader::RootResultArrayBuffer()
|
|
|
|
{
|
|
|
|
nsContentUtils::PreserveWrapper(
|
|
|
|
static_cast<nsIDOMEventTarget*>(
|
|
|
|
static_cast<nsDOMEventTargetHelper*>(this)), this);
|
|
|
|
}
|
|
|
|
|
2009-10-07 01:30:26 -07:00
|
|
|
//nsICharsetDetectionObserver
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-11-09 12:05:16 -08:00
|
|
|
nsDOMFileReader::Notify(const char *aCharset, nsDetectionConfident aConf)
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
2010-10-07 09:36:23 -07:00
|
|
|
mCharset = aCharset;
|
2009-10-07 01:30:26 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-09 12:05:16 -08:00
|
|
|
//nsDOMFileReader constructors/initializers
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2009-11-09 12:05:16 -08:00
|
|
|
nsDOMFileReader::nsDOMFileReader()
|
2009-11-21 03:56:31 -08:00
|
|
|
: mFileData(nsnull),
|
2009-11-24 17:53:17 -08:00
|
|
|
mDataLen(0), mDataFormat(FILE_AS_BINARY),
|
2011-06-30 17:50:44 -07:00
|
|
|
mResultArrayBuffer(nsnull)
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
|
|
|
nsLayoutStatics::AddRef();
|
2011-06-30 17:50:44 -07:00
|
|
|
SetDOMStringToNull(mResult);
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
|
|
|
|
2009-11-09 12:05:16 -08:00
|
|
|
nsDOMFileReader::~nsDOMFileReader()
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
2009-11-22 22:23:23 -08:00
|
|
|
FreeFileData();
|
|
|
|
|
2009-10-07 01:30:26 -07:00
|
|
|
nsLayoutStatics::Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2009-11-09 12:05:16 -08:00
|
|
|
nsDOMFileReader::Init()
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
2012-02-07 18:53:33 -08:00
|
|
|
nsDOMEventTargetHelper::Init();
|
2009-10-07 01:30:26 -07:00
|
|
|
|
|
|
|
nsIScriptSecurityManager *secMan = nsContentUtils::GetSecurityManager();
|
|
|
|
nsCOMPtr<nsIPrincipal> subjectPrincipal;
|
|
|
|
if (secMan) {
|
2011-06-21 09:52:22 -07:00
|
|
|
nsresult rv = secMan->GetSubjectPrincipal(getter_AddRefs(subjectPrincipal));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
|
|
|
NS_ENSURE_STATE(subjectPrincipal);
|
2011-09-29 09:06:35 -07:00
|
|
|
mPrincipal.swap(subjectPrincipal);
|
2009-10-07 01:30:26 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 09:06:35 -07:00
|
|
|
NS_IMPL_EVENT_HANDLER(nsDOMFileReader, load)
|
|
|
|
NS_IMPL_EVENT_HANDLER(nsDOMFileReader, loadstart)
|
|
|
|
NS_IMPL_EVENT_HANDLER(nsDOMFileReader, loadend)
|
|
|
|
NS_IMPL_FORWARD_EVENT_HANDLER(nsDOMFileReader, abort, FileIOObject)
|
|
|
|
NS_IMPL_FORWARD_EVENT_HANDLER(nsDOMFileReader, progress, FileIOObject)
|
|
|
|
NS_IMPL_FORWARD_EVENT_HANDLER(nsDOMFileReader, error, FileIOObject)
|
|
|
|
|
2009-10-07 01:30:26 -07:00
|
|
|
NS_IMETHODIMP
|
2009-11-09 12:05:16 -08:00
|
|
|
nsDOMFileReader::Initialize(nsISupports* aOwner, JSContext* cx, JSObject* obj,
|
|
|
|
PRUint32 argc, jsval *argv)
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
|
|
|
mOwner = do_QueryInterface(aOwner);
|
|
|
|
if (!mOwner) {
|
|
|
|
NS_WARNING("Unexpected nsIJSNativeInitializer owner");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This object is bound to a |window|,
|
|
|
|
// so reset the principal and script context.
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> scriptPrincipal = do_QueryInterface(aOwner);
|
|
|
|
NS_ENSURE_STATE(scriptPrincipal);
|
|
|
|
mPrincipal = scriptPrincipal->GetPrincipal();
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(aOwner);
|
|
|
|
NS_ENSURE_STATE(sgo);
|
|
|
|
mScriptContext = sgo->GetContext();
|
|
|
|
NS_ENSURE_STATE(mScriptContext);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIInterfaceRequestor
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-11-09 12:05:16 -08:00
|
|
|
nsDOMFileReader::GetInterface(const nsIID & aIID, void **aResult)
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
|
|
|
return QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
2009-11-09 12:05:16 -08:00
|
|
|
// nsIDOMFileReader
|
2009-10-07 01:30:26 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-11-09 12:05:16 -08:00
|
|
|
nsDOMFileReader::GetReadyState(PRUint16 *aReadyState)
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
2011-09-29 09:06:35 -07:00
|
|
|
return FileIOObject::GetReadyState(aReadyState);
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-06-30 17:50:44 -07:00
|
|
|
nsDOMFileReader::GetResult(JSContext* aCx, jsval* aResult)
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
2011-06-30 17:50:44 -07:00
|
|
|
if (mDataFormat == FILE_AS_ARRAYBUFFER) {
|
|
|
|
if (mReadyState == nsIDOMFileReader::DONE && mResultArrayBuffer) {
|
|
|
|
JSObject* tmp = mResultArrayBuffer;
|
|
|
|
*aResult = OBJECT_TO_JSVAL(tmp);
|
|
|
|
} else {
|
|
|
|
*aResult = JSVAL_NULL;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString tmpResult = mResult;
|
2011-12-24 00:27:04 -08:00
|
|
|
if (!xpc::StringToJsval(aCx, tmpResult, aResult)) {
|
2011-06-30 17:50:44 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2009-10-07 01:30:26 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-03-11 00:47:38 -08:00
|
|
|
nsDOMFileReader::GetError(nsIDOMDOMError** aError)
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
2011-09-29 09:06:35 -07:00
|
|
|
return FileIOObject::GetError(aError);
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
|
|
|
|
2011-06-30 17:50:44 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMFileReader::ReadAsArrayBuffer(nsIDOMBlob* aFile, JSContext* aCx)
|
|
|
|
{
|
|
|
|
return ReadFileContent(aCx, aFile, EmptyString(), FILE_AS_ARRAYBUFFER);
|
|
|
|
}
|
|
|
|
|
2009-10-07 01:30:26 -07:00
|
|
|
NS_IMETHODIMP
|
2010-10-13 16:25:33 -07:00
|
|
|
nsDOMFileReader::ReadAsBinaryString(nsIDOMBlob* aFile)
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
2011-06-30 17:50:44 -07:00
|
|
|
return ReadFileContent(nsnull, aFile, EmptyString(), FILE_AS_BINARY);
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-10-13 16:25:33 -07:00
|
|
|
nsDOMFileReader::ReadAsText(nsIDOMBlob* aFile,
|
2009-11-09 12:05:16 -08:00
|
|
|
const nsAString &aCharset)
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
2011-06-30 17:50:44 -07:00
|
|
|
return ReadFileContent(nsnull, aFile, aCharset, FILE_AS_TEXT);
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-10-13 16:25:33 -07:00
|
|
|
nsDOMFileReader::ReadAsDataURL(nsIDOMBlob* aFile)
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
2011-06-30 17:50:44 -07:00
|
|
|
return ReadFileContent(nsnull, aFile, EmptyString(), FILE_AS_DATAURL);
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-11-09 12:05:16 -08:00
|
|
|
nsDOMFileReader::Abort()
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
2011-09-29 09:06:35 -07:00
|
|
|
return FileIOObject::Abort();
|
|
|
|
}
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2011-09-29 09:06:35 -07:00
|
|
|
nsresult
|
|
|
|
nsDOMFileReader::DoAbort(nsAString& aEvent)
|
|
|
|
{
|
|
|
|
// Revert status and result attributes
|
2009-11-09 12:05:16 -08:00
|
|
|
SetDOMStringToNull(mResult);
|
2011-06-30 17:50:44 -07:00
|
|
|
mResultArrayBuffer = nsnull;
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2011-09-29 09:06:35 -07:00
|
|
|
// Non-null channel indicates a read is currently active
|
2009-10-07 01:30:26 -07:00
|
|
|
if (mChannel) {
|
2011-09-29 09:06:35 -07:00
|
|
|
// Cancel request requires an error status
|
2009-10-07 01:30:26 -07:00
|
|
|
mChannel->Cancel(NS_ERROR_FAILURE);
|
|
|
|
mChannel = nsnull;
|
|
|
|
}
|
|
|
|
mFile = nsnull;
|
|
|
|
|
|
|
|
//Clean up memory buffer
|
2009-11-22 22:23:23 -08:00
|
|
|
FreeFileData();
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2011-09-29 09:06:35 -07:00
|
|
|
// Tell the base class which event to dispatch
|
|
|
|
aEvent = NS_LITERAL_STRING(LOADEND_STR);
|
2009-10-07 01:30:26 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-21 03:56:31 -08:00
|
|
|
static
|
|
|
|
NS_METHOD
|
|
|
|
ReadFuncBinaryString(nsIInputStream* in,
|
|
|
|
void* closure,
|
|
|
|
const char* fromRawSegment,
|
|
|
|
PRUint32 toOffset,
|
|
|
|
PRUint32 count,
|
|
|
|
PRUint32 *writeCount)
|
|
|
|
{
|
|
|
|
PRUnichar* dest = static_cast<PRUnichar*>(closure) + toOffset;
|
|
|
|
PRUnichar* end = dest + count;
|
|
|
|
const unsigned char* source = (const unsigned char*)fromRawSegment;
|
|
|
|
while (dest != end) {
|
|
|
|
*dest = *source;
|
|
|
|
++dest;
|
|
|
|
++source;
|
|
|
|
}
|
|
|
|
*writeCount = count;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 09:06:35 -07:00
|
|
|
nsresult
|
|
|
|
nsDOMFileReader::DoOnDataAvailable(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsIInputStream *aInputStream,
|
|
|
|
PRUint32 aOffset,
|
|
|
|
PRUint32 aCount)
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
2009-11-21 01:27:02 -08:00
|
|
|
if (mDataFormat == FILE_AS_BINARY) {
|
2009-11-21 03:56:31 -08:00
|
|
|
//Continuously update our binary string as data comes in
|
|
|
|
NS_ASSERTION(mResult.Length() == aOffset,
|
|
|
|
"unexpected mResult length");
|
2009-11-21 01:27:02 -08:00
|
|
|
PRUint32 oldLen = mResult.Length();
|
2009-11-21 03:56:31 -08:00
|
|
|
PRUnichar *buf = nsnull;
|
|
|
|
mResult.GetMutableData(&buf, oldLen + aCount);
|
|
|
|
NS_ENSURE_TRUE(buf, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
PRUint32 bytesRead = 0;
|
|
|
|
aInputStream->ReadSegments(ReadFuncBinaryString, buf + oldLen, aCount,
|
|
|
|
&bytesRead);
|
|
|
|
NS_ASSERTION(bytesRead == aCount, "failed to read data");
|
|
|
|
}
|
2011-06-30 17:50:44 -07:00
|
|
|
else if (mDataFormat == FILE_AS_ARRAYBUFFER) {
|
|
|
|
JSObject* abuf = js::ArrayBuffer::getArrayBuffer(mResultArrayBuffer);
|
|
|
|
NS_ASSERTION(abuf, "What happened?");
|
|
|
|
|
|
|
|
PRUint32 bytesRead = 0;
|
|
|
|
aInputStream->Read((char*)JS_GetArrayBufferData(abuf) + aOffset, aCount, &bytesRead);
|
|
|
|
NS_ASSERTION(bytesRead == aCount, "failed to read data");
|
|
|
|
}
|
2009-11-21 03:56:31 -08:00
|
|
|
else {
|
|
|
|
//Update memory buffer to reflect the contents of the file
|
|
|
|
mFileData = (char *)PR_Realloc(mFileData, aOffset + aCount);
|
|
|
|
NS_ENSURE_TRUE(mFileData, NS_ERROR_OUT_OF_MEMORY);
|
2009-11-21 01:27:02 -08:00
|
|
|
|
2009-11-21 03:56:31 -08:00
|
|
|
PRUint32 bytesRead = 0;
|
|
|
|
aInputStream->Read(mFileData + aOffset, aCount, &bytesRead);
|
|
|
|
NS_ASSERTION(bytesRead == aCount, "failed to read data");
|
2009-11-21 01:27:02 -08:00
|
|
|
|
2009-11-21 03:56:31 -08:00
|
|
|
mDataLen += aCount;
|
2009-11-21 01:27:02 -08:00
|
|
|
}
|
|
|
|
|
2009-10-07 01:30:26 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 09:06:35 -07:00
|
|
|
nsresult
|
|
|
|
nsDOMFileReader::DoOnStopRequest(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsresult aStatus,
|
|
|
|
nsAString& aSuccessEvent,
|
|
|
|
nsAString& aTerminationEvent)
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
2011-09-29 09:06:35 -07:00
|
|
|
aSuccessEvent = NS_LITERAL_STRING(LOAD_STR);
|
|
|
|
aTerminationEvent = NS_LITERAL_STRING(LOADEND_STR);
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2011-09-29 09:06:35 -07:00
|
|
|
// Clear out the data if necessary
|
2009-10-07 01:30:26 -07:00
|
|
|
if (NS_FAILED(aStatus)) {
|
2009-11-22 22:23:23 -08:00
|
|
|
FreeFileData();
|
2009-10-07 01:30:26 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-24 17:53:17 -08:00
|
|
|
nsresult rv = NS_OK;
|
2009-10-07 01:30:26 -07:00
|
|
|
switch (mDataFormat) {
|
2011-06-30 17:50:44 -07:00
|
|
|
case FILE_AS_ARRAYBUFFER:
|
|
|
|
break; //Already accumulated mResultArrayBuffer
|
2009-10-07 01:30:26 -07:00
|
|
|
case FILE_AS_BINARY:
|
2009-11-09 12:05:16 -08:00
|
|
|
break; //Already accumulated mResult
|
2009-10-07 01:30:26 -07:00
|
|
|
case FILE_AS_TEXT:
|
2009-11-09 12:05:16 -08:00
|
|
|
rv = GetAsText(mCharset, mFileData, mDataLen, mResult);
|
2009-10-07 01:30:26 -07:00
|
|
|
break;
|
|
|
|
case FILE_AS_DATAURL:
|
2009-11-09 12:05:16 -08:00
|
|
|
rv = GetAsDataURL(mFile, mFileData, mDataLen, mResult);
|
2009-10-07 01:30:26 -07:00
|
|
|
break;
|
|
|
|
}
|
2010-10-07 09:36:23 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mResult.SetIsVoid(false);
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2009-11-22 22:23:23 -08:00
|
|
|
FreeFileData();
|
|
|
|
|
2011-09-29 09:06:35 -07:00
|
|
|
return rv;
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper methods
|
|
|
|
|
|
|
|
nsresult
|
2011-06-30 17:50:44 -07:00
|
|
|
nsDOMFileReader::ReadFileContent(JSContext* aCx,
|
|
|
|
nsIDOMBlob* aFile,
|
2009-11-09 12:05:16 -08:00
|
|
|
const nsAString &aCharset,
|
2009-11-24 17:53:17 -08:00
|
|
|
eDataFormat aDataFormat)
|
|
|
|
{
|
2010-09-05 11:00:05 -07:00
|
|
|
nsresult rv;
|
2009-10-07 01:30:26 -07:00
|
|
|
NS_ENSURE_TRUE(aFile, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
//Implicit abort to clear any other activity going on
|
|
|
|
Abort();
|
|
|
|
mError = nsnull;
|
2009-11-22 11:05:12 -08:00
|
|
|
SetDOMStringToNull(mResult);
|
2011-09-29 09:06:35 -07:00
|
|
|
mTransferred = 0;
|
|
|
|
mTotal = 0;
|
2009-11-22 22:23:23 -08:00
|
|
|
mReadyState = nsIDOMFileReader::EMPTY;
|
|
|
|
FreeFileData();
|
|
|
|
|
2010-09-05 11:00:05 -07:00
|
|
|
mFile = aFile;
|
2009-11-22 22:23:23 -08:00
|
|
|
mDataFormat = aDataFormat;
|
2010-10-07 09:36:23 -07:00
|
|
|
CopyUTF16toUTF8(aCharset, mCharset);
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2010-09-05 08:32:57 -07:00
|
|
|
//Establish a channel with our file
|
2010-09-05 11:00:05 -07:00
|
|
|
{
|
|
|
|
// Hold the internal URL alive only as long as necessary
|
|
|
|
// After the channel is created it will own whatever is backing
|
|
|
|
// the DOMFile.
|
2010-09-17 18:22:44 -07:00
|
|
|
nsDOMFileInternalUrlHolder urlHolder(mFile, mPrincipal);
|
2010-09-05 11:00:05 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), urlHolder.mUrl);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2010-09-05 11:00:05 -07:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(mChannel), uri);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2009-10-07 01:30:26 -07:00
|
|
|
|
|
|
|
//Obtain the total size of the file before reading
|
2011-09-29 09:06:35 -07:00
|
|
|
mTotal = mozilla::dom::kUnknownSize;
|
|
|
|
mFile->GetSize(&mTotal);
|
2009-10-07 01:30:26 -07:00
|
|
|
|
|
|
|
rv = mChannel->AsyncOpen(this, nsnull);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2009-11-09 12:05:16 -08:00
|
|
|
//FileReader should be in loading state here
|
|
|
|
mReadyState = nsIDOMFileReader::LOADING;
|
2009-10-07 01:30:26 -07:00
|
|
|
DispatchProgressEvent(NS_LITERAL_STRING(LOADSTART_STR));
|
2011-06-30 17:50:44 -07:00
|
|
|
|
|
|
|
if (mDataFormat == FILE_AS_ARRAYBUFFER) {
|
|
|
|
RootResultArrayBuffer();
|
2011-09-29 09:06:35 -07:00
|
|
|
mResultArrayBuffer = js_CreateArrayBuffer(aCx, mTotal);
|
2011-06-30 17:50:44 -07:00
|
|
|
if (!mResultArrayBuffer) {
|
|
|
|
NS_WARNING("Failed to create JS array buffer");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2009-10-07 01:30:26 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-10-07 09:36:23 -07:00
|
|
|
nsDOMFileReader::GetAsText(const nsACString &aCharset,
|
2009-11-09 12:05:16 -08:00
|
|
|
const char *aFileData,
|
|
|
|
PRUint32 aDataLen,
|
|
|
|
nsAString& aResult)
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCAutoString charsetGuess;
|
|
|
|
if (!aCharset.IsEmpty()) {
|
2010-10-07 09:36:23 -07:00
|
|
|
charsetGuess = aCharset;
|
2009-10-07 01:30:26 -07:00
|
|
|
} else {
|
|
|
|
rv = GuessCharset(aFileData, aDataLen, charsetGuess);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCAutoString charset;
|
2012-03-04 19:57:51 -08:00
|
|
|
rv = nsCharsetAlias::GetPreferred(charsetGuess, charset);
|
2009-10-07 01:30:26 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = ConvertStream(aFileData, aDataLen, charset.get(), aResult);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-10-13 16:25:33 -07:00
|
|
|
nsDOMFileReader::GetAsDataURL(nsIDOMBlob *aFile,
|
2009-11-09 12:05:16 -08:00
|
|
|
const char *aFileData,
|
|
|
|
PRUint32 aDataLen,
|
|
|
|
nsAString& aResult)
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
|
|
|
aResult.AssignLiteral("data:");
|
|
|
|
|
|
|
|
nsresult rv;
|
2010-09-05 11:00:05 -07:00
|
|
|
nsString contentType;
|
|
|
|
rv = aFile->GetType(contentType);
|
|
|
|
if (NS_SUCCEEDED(rv) && !contentType.IsEmpty()) {
|
|
|
|
aResult.Append(contentType);
|
2009-10-07 01:30:26 -07:00
|
|
|
} else {
|
|
|
|
aResult.AppendLiteral("application/octet-stream");
|
|
|
|
}
|
|
|
|
aResult.AppendLiteral(";base64,");
|
|
|
|
|
|
|
|
PRUint32 totalRead = 0;
|
2010-10-07 09:36:23 -07:00
|
|
|
while (aDataLen > totalRead) {
|
2009-10-07 01:30:26 -07:00
|
|
|
PRUint32 numEncode = 4096;
|
|
|
|
PRUint32 amtRemaining = aDataLen - totalRead;
|
|
|
|
if (numEncode > amtRemaining)
|
|
|
|
numEncode = amtRemaining;
|
|
|
|
|
|
|
|
//Unless this is the end of the file, encode in multiples of 3
|
|
|
|
if (numEncode > 3) {
|
|
|
|
PRUint32 leftOver = numEncode % 3;
|
|
|
|
numEncode -= leftOver;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Out buffer should be at least 4/3rds the read buf, plus a terminator
|
|
|
|
char *base64 = PL_Base64Encode(aFileData + totalRead, numEncode, nsnull);
|
|
|
|
AppendASCIItoUTF16(nsDependentCString(base64), aResult);
|
|
|
|
PR_Free(base64);
|
|
|
|
|
|
|
|
totalRead += numEncode;
|
2010-10-07 09:36:23 -07:00
|
|
|
}
|
2009-10-07 01:30:26 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2009-11-09 12:05:16 -08:00
|
|
|
nsDOMFileReader::ConvertStream(const char *aFileData,
|
|
|
|
PRUint32 aDataLen,
|
|
|
|
const char *aCharset,
|
|
|
|
nsAString &aResult)
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsICharsetConverterManager> charsetConverter =
|
|
|
|
do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder;
|
|
|
|
rv = charsetConverter->GetUnicodeDecoder(aCharset, getter_AddRefs(unicodeDecoder));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRInt32 destLength;
|
|
|
|
rv = unicodeDecoder->GetMaxLength(aFileData, aDataLen, &destLength);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
aResult.SetLength(destLength); //Make sure we have enough space for the conversion
|
|
|
|
destLength = aResult.Length();
|
|
|
|
|
|
|
|
PRInt32 srcLength = aDataLen;
|
|
|
|
rv = unicodeDecoder->Convert(aFileData, &srcLength, aResult.BeginWriting(), &destLength);
|
|
|
|
aResult.SetLength(destLength); //Trim down to the correct size
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2009-11-09 12:05:16 -08:00
|
|
|
nsDOMFileReader::GuessCharset(const char *aFileData,
|
|
|
|
PRUint32 aDataLen,
|
|
|
|
nsACString &aCharset)
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
|
|
|
// First try the universal charset detector
|
|
|
|
nsCOMPtr<nsICharsetDetector> detector
|
|
|
|
= do_CreateInstance(NS_CHARSET_DETECTOR_CONTRACTID_BASE
|
|
|
|
"universal_charset_detector");
|
|
|
|
if (!detector) {
|
|
|
|
// No universal charset detector, try the default charset detector
|
2011-05-26 21:53:03 -07:00
|
|
|
const nsAdoptingCString& detectorName =
|
|
|
|
Preferences::GetLocalizedCString("intl.charset.detector");
|
2009-10-07 01:30:26 -07:00
|
|
|
if (!detectorName.IsEmpty()) {
|
|
|
|
nsCAutoString detectorContractID;
|
|
|
|
detectorContractID.AssignLiteral(NS_CHARSET_DETECTOR_CONTRACTID_BASE);
|
2011-05-26 21:53:03 -07:00
|
|
|
detectorContractID += detectorName;
|
2009-10-07 01:30:26 -07:00
|
|
|
detector = do_CreateInstance(detectorContractID.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
2010-10-07 09:36:23 -07:00
|
|
|
// The charset detector doesn't work for empty (null) aFileData. Testing
|
|
|
|
// aDataLen instead of aFileData so that we catch potential errors.
|
|
|
|
if (detector && aDataLen != 0) {
|
2009-10-07 01:30:26 -07:00
|
|
|
mCharset.Truncate();
|
|
|
|
detector->Init(this);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool done;
|
2009-10-07 01:30:26 -07:00
|
|
|
|
|
|
|
rv = detector->DoIt(aFileData, aDataLen, &done);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = detector->Done();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-10-07 09:36:23 -07:00
|
|
|
aCharset = mCharset;
|
2009-10-07 01:30:26 -07:00
|
|
|
} else {
|
|
|
|
// no charset detector available, check the BOM
|
2011-03-29 23:35:34 -07:00
|
|
|
unsigned char sniffBuf[3];
|
2009-10-07 01:30:26 -07:00
|
|
|
PRUint32 numRead = (aDataLen >= sizeof(sniffBuf) ? sizeof(sniffBuf) : aDataLen);
|
|
|
|
memcpy(sniffBuf, aFileData, numRead);
|
|
|
|
|
2011-03-29 23:35:34 -07:00
|
|
|
if (numRead >= 2 &&
|
2009-10-07 01:30:26 -07:00
|
|
|
sniffBuf[0] == 0xfe &&
|
|
|
|
sniffBuf[1] == 0xff) {
|
|
|
|
aCharset = "UTF-16BE";
|
|
|
|
} else if (numRead >= 2 &&
|
|
|
|
sniffBuf[0] == 0xff &&
|
|
|
|
sniffBuf[1] == 0xfe) {
|
|
|
|
aCharset = "UTF-16LE";
|
|
|
|
} else if (numRead >= 3 &&
|
|
|
|
sniffBuf[0] == 0xef &&
|
|
|
|
sniffBuf[1] == 0xbb &&
|
|
|
|
sniffBuf[2] == 0xbf) {
|
|
|
|
aCharset = "UTF-8";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aCharset.IsEmpty()) {
|
|
|
|
// no charset detected, default to the system charset
|
|
|
|
nsCOMPtr<nsIPlatformCharset> platformCharset =
|
|
|
|
do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = platformCharset->GetCharset(kPlatformCharsetSel_PlainTextInFile,
|
|
|
|
aCharset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aCharset.IsEmpty()) {
|
|
|
|
// no sniffed or default charset, try UTF-8
|
|
|
|
aCharset.AssignLiteral("UTF-8");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|