gecko-dev/content/base/src/nsDOMFileReader.cpp

655 lines
19 KiB
C++
Raw Normal View History

/* -*- 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
* Mozilla Foundation
* 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 ***** */
#include "nsDOMFileReader.h"
#include "nsContentCID.h"
#include "nsContentUtils.h"
#include "nsDOMClassInfoID.h"
#include "nsDOMFile.h"
#include "nsDOMError.h"
#include "nsICharsetAlias.h"
#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"
#include "nsFileDataProtocolHandler.h"
#include "mozilla/Preferences.h"
#include "xpcprivate.h"
#include "xpcpublic.h"
Bug 691411 - Move js/src/xpconnect to js/xpconnect and rename source files while we're at it. r=khuey, sr=mrbkap --HG-- rename : js/src/xpconnect/Makefile.in => js/xpconnect/Makefile.in rename : js/src/xpconnect/crashtests/117307-1.html => js/xpconnect/crashtests/117307-1.html rename : js/src/xpconnect/crashtests/193710.html => js/xpconnect/crashtests/193710.html rename : js/src/xpconnect/crashtests/290162-1.html => js/xpconnect/crashtests/290162-1.html rename : js/src/xpconnect/crashtests/326615-1.html => js/xpconnect/crashtests/326615-1.html rename : js/src/xpconnect/crashtests/328553-1.html => js/xpconnect/crashtests/328553-1.html rename : js/src/xpconnect/crashtests/346258-1.html => js/xpconnect/crashtests/346258-1.html rename : js/src/xpconnect/crashtests/346512-1-frame1.xhtml => js/xpconnect/crashtests/346512-1-frame1.xhtml rename : js/src/xpconnect/crashtests/346512-1-frame2.xhtml => js/xpconnect/crashtests/346512-1-frame2.xhtml rename : js/src/xpconnect/crashtests/346512-1.xhtml => js/xpconnect/crashtests/346512-1.xhtml rename : js/src/xpconnect/crashtests/382133-1.html => js/xpconnect/crashtests/382133-1.html rename : js/src/xpconnect/crashtests/386680-1.html => js/xpconnect/crashtests/386680-1.html rename : js/src/xpconnect/crashtests/394810-1.html => js/xpconnect/crashtests/394810-1.html rename : js/src/xpconnect/crashtests/400349-1.html => js/xpconnect/crashtests/400349-1.html rename : js/src/xpconnect/crashtests/403356-1.html => js/xpconnect/crashtests/403356-1.html rename : js/src/xpconnect/crashtests/418139-1.svg => js/xpconnect/crashtests/418139-1.svg rename : js/src/xpconnect/crashtests/420513-1.html => js/xpconnect/crashtests/420513-1.html rename : js/src/xpconnect/crashtests/453935-1.html => js/xpconnect/crashtests/453935-1.html rename : js/src/xpconnect/crashtests/462926.html => js/xpconnect/crashtests/462926.html rename : js/src/xpconnect/crashtests/467693-1.html => js/xpconnect/crashtests/467693-1.html rename : js/src/xpconnect/crashtests/468552-1.html => js/xpconnect/crashtests/468552-1.html rename : js/src/xpconnect/crashtests/471366-1.html => js/xpconnect/crashtests/471366-1.html rename : js/src/xpconnect/crashtests/475185-1.html => js/xpconnect/crashtests/475185-1.html rename : js/src/xpconnect/crashtests/475291-1.html => js/xpconnect/crashtests/475291-1.html rename : js/src/xpconnect/crashtests/503286-1.html => js/xpconnect/crashtests/503286-1.html rename : js/src/xpconnect/crashtests/504000-1.html => js/xpconnect/crashtests/504000-1.html rename : js/src/xpconnect/crashtests/509075-1.html => js/xpconnect/crashtests/509075-1.html rename : js/src/xpconnect/crashtests/512815-1.html => js/xpconnect/crashtests/512815-1.html rename : js/src/xpconnect/crashtests/515726-1.html => js/xpconnect/crashtests/515726-1.html rename : js/src/xpconnect/crashtests/545291-1.html => js/xpconnect/crashtests/545291-1.html rename : js/src/xpconnect/crashtests/558979.html => js/xpconnect/crashtests/558979.html rename : js/src/xpconnect/crashtests/582649.html => js/xpconnect/crashtests/582649.html rename : js/src/xpconnect/crashtests/601284-1.html => js/xpconnect/crashtests/601284-1.html rename : js/src/xpconnect/crashtests/603146-1.html => js/xpconnect/crashtests/603146-1.html rename : js/src/xpconnect/crashtests/603858-1.html => js/xpconnect/crashtests/603858-1.html rename : js/src/xpconnect/crashtests/608963.html => js/xpconnect/crashtests/608963.html rename : js/src/xpconnect/crashtests/616930-1.html => js/xpconnect/crashtests/616930-1.html rename : js/src/xpconnect/crashtests/639737-1.html => js/xpconnect/crashtests/639737-1.html rename : js/src/xpconnect/crashtests/648206-1.html => js/xpconnect/crashtests/648206-1.html rename : js/src/xpconnect/crashtests/crashtests.list => js/xpconnect/crashtests/crashtests.list rename : js/src/xpconnect/idl/Makefile.in => js/xpconnect/idl/Makefile.in rename : js/src/xpconnect/idl/mozIJSSubScriptLoader.idl => js/xpconnect/idl/mozIJSSubScriptLoader.idl rename : js/src/xpconnect/idl/nsIJSContextStack.idl => js/xpconnect/idl/nsIJSContextStack.idl rename : js/src/xpconnect/idl/nsIJSEngineTelemetryStats.idl => js/xpconnect/idl/nsIJSEngineTelemetryStats.idl rename : js/src/xpconnect/idl/nsIJSRuntimeService.idl => js/xpconnect/idl/nsIJSRuntimeService.idl rename : js/src/xpconnect/idl/nsIScriptError.idl => js/xpconnect/idl/nsIScriptError.idl rename : js/src/xpconnect/idl/nsIXPCScriptNotify.idl => js/xpconnect/idl/nsIXPCScriptNotify.idl rename : js/src/xpconnect/idl/nsIXPCScriptable.idl => js/xpconnect/idl/nsIXPCScriptable.idl rename : js/src/xpconnect/idl/nsIXPCSecurityManager.idl => js/xpconnect/idl/nsIXPCSecurityManager.idl rename : js/src/xpconnect/idl/nsIXPConnect.idl => js/xpconnect/idl/nsIXPConnect.idl rename : js/src/xpconnect/idl/xpcIJSGetFactory.idl => js/xpconnect/idl/xpcIJSGetFactory.idl rename : js/src/xpconnect/idl/xpcIJSModuleLoader.idl => js/xpconnect/idl/xpcIJSModuleLoader.idl rename : js/src/xpconnect/idl/xpcIJSWeakReference.idl => js/xpconnect/idl/xpcIJSWeakReference.idl rename : js/src/xpconnect/idl/xpccomponents.idl => js/xpconnect/idl/xpccomponents.idl rename : js/src/xpconnect/idl/xpcexception.idl => js/xpconnect/idl/xpcexception.idl rename : js/src/xpconnect/idl/xpcjsid.idl => js/xpconnect/idl/xpcjsid.idl rename : js/src/xpconnect/loader/Makefile.in => js/xpconnect/loader/Makefile.in rename : js/src/xpconnect/loader/XPCOMUtils.jsm => js/xpconnect/loader/XPCOMUtils.jsm rename : js/src/xpconnect/loader/mozJSComponentLoader.cpp => js/xpconnect/loader/mozJSComponentLoader.cpp rename : js/src/xpconnect/loader/mozJSComponentLoader.h => js/xpconnect/loader/mozJSComponentLoader.h rename : js/src/xpconnect/loader/mozJSLoaderUtils.cpp => js/xpconnect/loader/mozJSLoaderUtils.cpp rename : js/src/xpconnect/loader/mozJSLoaderUtils.h => js/xpconnect/loader/mozJSLoaderUtils.h rename : js/src/xpconnect/loader/mozJSSubScriptLoader.cpp => js/xpconnect/loader/mozJSSubScriptLoader.cpp rename : js/src/xpconnect/loader/mozJSSubScriptLoader.h => js/xpconnect/loader/mozJSSubScriptLoader.h rename : js/src/xpconnect/public/Makefile.in => js/xpconnect/public/Makefile.in rename : js/src/xpconnect/public/nsAXPCNativeCallContext.h => js/xpconnect/public/nsAXPCNativeCallContext.h rename : js/src/xpconnect/public/nsAutoJSValHolder.h => js/xpconnect/public/nsAutoJSValHolder.h rename : js/src/xpconnect/public/xpc_map_end.h => js/xpconnect/public/xpc_map_end.h rename : js/src/xpconnect/shell/Makefile.in => js/xpconnect/shell/Makefile.in rename : js/src/xpconnect/shell/jsshell.msg => js/xpconnect/shell/jsshell.msg rename : js/src/xpconnect/shell/xpcshell.cpp => js/xpconnect/shell/xpcshell.cpp rename : js/src/xpconnect/shell/xpcshellMacUtils.h => js/xpconnect/shell/xpcshellMacUtils.h rename : js/src/xpconnect/shell/xpcshellMacUtils.mm => js/xpconnect/shell/xpcshellMacUtils.mm rename : js/src/xpconnect/src/Makefile.in => js/xpconnect/src/Makefile.in rename : js/src/xpconnect/src/README => js/xpconnect/src/README rename : js/src/xpconnect/src/xpccallcontext.cpp => js/xpconnect/src/XPCCallContext.cpp rename : js/src/xpconnect/src/xpccomponents.cpp => js/xpconnect/src/XPCComponents.cpp rename : js/src/xpconnect/src/xpccontext.cpp => js/xpconnect/src/XPCContext.cpp rename : js/src/xpconnect/src/xpcconvert.cpp => js/xpconnect/src/XPCConvert.cpp rename : js/src/xpconnect/src/xpcdebug.cpp => js/xpconnect/src/XPCDebug.cpp rename : js/src/xpconnect/src/xpcexception.cpp => js/xpconnect/src/XPCException.cpp rename : js/src/xpconnect/src/xpcforwards.h => js/xpconnect/src/XPCForwards.h rename : js/src/xpconnect/src/xpcinlines.h => js/xpconnect/src/XPCInlines.h rename : js/src/xpconnect/src/xpcjsid.cpp => js/xpconnect/src/XPCJSID.cpp rename : js/src/xpconnect/src/xpcjsruntime.cpp => js/xpconnect/src/XPCJSRuntime.cpp rename : js/src/xpconnect/src/xpcJSWeakReference.cpp => js/xpconnect/src/XPCJSWeakReference.cpp rename : js/src/xpconnect/src/xpcJSWeakReference.h => js/xpconnect/src/XPCJSWeakReference.h rename : js/src/xpconnect/src/xpclocale.cpp => js/xpconnect/src/XPCLocale.cpp rename : js/src/xpconnect/src/xpclog.cpp => js/xpconnect/src/XPCLog.cpp rename : js/src/xpconnect/src/xpclog.h => js/xpconnect/src/XPCLog.h rename : js/src/xpconnect/src/xpcmaps.cpp => js/xpconnect/src/XPCMaps.cpp rename : js/src/xpconnect/src/xpcmaps.h => js/xpconnect/src/XPCMaps.h rename : js/src/xpconnect/src/xpcmodule.cpp => js/xpconnect/src/XPCModule.cpp rename : js/src/xpconnect/src/xpcmodule.h => js/xpconnect/src/XPCModule.h rename : js/src/xpconnect/src/xpcquickstubs.cpp => js/xpconnect/src/XPCQuickStubs.cpp rename : js/src/xpconnect/src/xpcquickstubs.h => js/xpconnect/src/XPCQuickStubs.h rename : js/src/xpconnect/src/xpcruntimesvc.cpp => js/xpconnect/src/XPCRuntimeService.cpp rename : js/src/xpconnect/src/xpcstack.cpp => js/xpconnect/src/XPCStack.cpp rename : js/src/xpconnect/src/xpcstring.cpp => js/xpconnect/src/XPCString.cpp rename : js/src/xpconnect/src/xpcthreadcontext.cpp => js/xpconnect/src/XPCThreadContext.cpp rename : js/src/xpconnect/src/xpcthrower.cpp => js/xpconnect/src/XPCThrower.cpp rename : js/src/xpconnect/src/xpcvariant.cpp => js/xpconnect/src/XPCVariant.cpp rename : js/src/xpconnect/src/xpcwrappedjs.cpp => js/xpconnect/src/XPCWrappedJS.cpp rename : js/src/xpconnect/src/xpcwrappedjsclass.cpp => js/xpconnect/src/XPCWrappedJSClass.cpp rename : js/src/xpconnect/src/xpcwrappednative.cpp => js/xpconnect/src/XPCWrappedNative.cpp rename : js/src/xpconnect/src/xpcwrappednativeinfo.cpp => js/xpconnect/src/XPCWrappedNativeInfo.cpp rename : js/src/xpconnect/src/xpcwrappednativejsops.cpp => js/xpconnect/src/XPCWrappedNativeJSOps.cpp rename : js/src/xpconnect/src/xpcwrappednativeproto.cpp => js/xpconnect/src/XPCWrappedNativeProto.cpp rename : js/src/xpconnect/src/xpcwrappednativescope.cpp => js/xpconnect/src/XPCWrappedNativeScope.cpp rename : js/src/xpconnect/src/XPCWrapper.cpp => js/xpconnect/src/XPCWrapper.cpp rename : js/src/xpconnect/src/XPCWrapper.h => js/xpconnect/src/XPCWrapper.h rename : js/src/xpconnect/src/codegen.py => js/xpconnect/src/codegen.py rename : js/src/xpconnect/src/dom_quickstubs.qsconf => js/xpconnect/src/dom_quickstubs.qsconf rename : js/src/xpconnect/src/dombindings.conf => js/xpconnect/src/dombindings.conf rename : js/src/xpconnect/src/dombindings.cpp => js/xpconnect/src/dombindings.cpp rename : js/src/xpconnect/src/dombindings.h => js/xpconnect/src/dombindings.h rename : js/src/xpconnect/src/dombindingsgen.py => js/xpconnect/src/dombindingsgen.py rename : js/src/xpconnect/src/nsCSSPropertiesQS.h => js/xpconnect/src/nsCSSPropertiesQS.h rename : js/src/xpconnect/src/nsDOMQS.h => js/xpconnect/src/nsDOMQS.h rename : js/src/xpconnect/src/nsScriptError.cpp => js/xpconnect/src/nsScriptError.cpp rename : js/src/xpconnect/src/nsXPConnect.cpp => js/xpconnect/src/nsXPConnect.cpp rename : js/src/xpconnect/src/qsWinUndefs.h => js/xpconnect/src/qsWinUndefs.h rename : js/src/xpconnect/src/qsgen.py => js/xpconnect/src/qsgen.py rename : js/src/xpconnect/src/xpc.msg => js/xpconnect/src/xpc.msg rename : js/src/xpconnect/src/xpcprivate.h => js/xpconnect/src/xpcprivate.h rename : js/src/xpconnect/src/xpcpublic.h => js/xpconnect/src/xpcpublic.h rename : js/src/xpconnect/tests/Makefile.in => js/xpconnect/tests/Makefile.in rename : js/src/xpconnect/tests/chrome/Makefile.in => js/xpconnect/tests/chrome/Makefile.in rename : js/src/xpconnect/tests/chrome/bug503926.xul => js/xpconnect/tests/chrome/bug503926.xul rename : js/src/xpconnect/tests/chrome/file_bug618176.xul => js/xpconnect/tests/chrome/file_bug618176.xul rename : js/src/xpconnect/tests/chrome/file_evalInSandbox.html => js/xpconnect/tests/chrome/file_evalInSandbox.html rename : js/src/xpconnect/tests/chrome/test_APIExposer.xul => js/xpconnect/tests/chrome/test_APIExposer.xul rename : js/src/xpconnect/tests/chrome/test_bug448587.xul => js/xpconnect/tests/chrome/test_bug448587.xul rename : js/src/xpconnect/tests/chrome/test_bug484459.xul => js/xpconnect/tests/chrome/test_bug484459.xul rename : js/src/xpconnect/tests/chrome/test_bug500931.xul => js/xpconnect/tests/chrome/test_bug500931.xul rename : js/src/xpconnect/tests/chrome/test_bug503926.xul => js/xpconnect/tests/chrome/test_bug503926.xul rename : js/src/xpconnect/tests/chrome/test_bug517163.xul => js/xpconnect/tests/chrome/test_bug517163.xul rename : js/src/xpconnect/tests/chrome/test_bug533596.xul => js/xpconnect/tests/chrome/test_bug533596.xul rename : js/src/xpconnect/tests/chrome/test_bug571849.xul => js/xpconnect/tests/chrome/test_bug571849.xul rename : js/src/xpconnect/tests/chrome/test_bug596580.xul => js/xpconnect/tests/chrome/test_bug596580.xul rename : js/src/xpconnect/tests/chrome/test_bug601803.xul => js/xpconnect/tests/chrome/test_bug601803.xul rename : js/src/xpconnect/tests/chrome/test_bug610390.xul => js/xpconnect/tests/chrome/test_bug610390.xul rename : js/src/xpconnect/tests/chrome/test_bug614757.xul => js/xpconnect/tests/chrome/test_bug614757.xul rename : js/src/xpconnect/tests/chrome/test_bug616992.xul => js/xpconnect/tests/chrome/test_bug616992.xul rename : js/src/xpconnect/tests/chrome/test_bug618176.xul => js/xpconnect/tests/chrome/test_bug618176.xul rename : js/src/xpconnect/tests/chrome/test_bug654370.xul => js/xpconnect/tests/chrome/test_bug654370.xul rename : js/src/xpconnect/tests/chrome/test_bug658560.xul => js/xpconnect/tests/chrome/test_bug658560.xul rename : js/src/xpconnect/tests/chrome/test_bug664689.xul => js/xpconnect/tests/chrome/test_bug664689.xul rename : js/src/xpconnect/tests/chrome/test_bug679861.xul => js/xpconnect/tests/chrome/test_bug679861.xul rename : js/src/xpconnect/tests/chrome/test_cows.xul => js/xpconnect/tests/chrome/test_cows.xul rename : js/src/xpconnect/tests/chrome/test_doublewrappedcompartments.xul => js/xpconnect/tests/chrome/test_doublewrappedcompartments.xul rename : js/src/xpconnect/tests/chrome/test_evalInSandbox.xul => js/xpconnect/tests/chrome/test_evalInSandbox.xul rename : js/src/xpconnect/tests/chrome/test_getweakmapkeys.xul => js/xpconnect/tests/chrome/test_getweakmapkeys.xul rename : js/src/xpconnect/tests/chrome/test_nodelists.xul => js/xpconnect/tests/chrome/test_nodelists.xul rename : js/src/xpconnect/tests/chrome/test_precisegc.xul => js/xpconnect/tests/chrome/test_precisegc.xul rename : js/src/xpconnect/tests/chrome/test_sandboxImport.xul => js/xpconnect/tests/chrome/test_sandboxImport.xul rename : js/src/xpconnect/tests/chrome/test_wrappers-2.xul => js/xpconnect/tests/chrome/test_wrappers-2.xul rename : js/src/xpconnect/tests/chrome/test_wrappers.xul => js/xpconnect/tests/chrome/test_wrappers.xul rename : js/src/xpconnect/tests/components/js/Makefile.in => js/xpconnect/tests/components/js/Makefile.in rename : js/src/xpconnect/tests/components/js/xpctest.manifest => js/xpconnect/tests/components/js/xpctest.manifest rename : js/src/xpconnect/tests/components/js/xpctest_attributes.js => js/xpconnect/tests/components/js/xpctest_attributes.js rename : js/src/xpconnect/tests/components/js/xpctest_params.js => js/xpconnect/tests/components/js/xpctest_params.js rename : js/src/xpconnect/tests/components/native/Makefile.in => js/xpconnect/tests/components/native/Makefile.in rename : js/src/xpconnect/tests/components/native/xpctest.manifest => js/xpconnect/tests/components/native/xpctest.manifest rename : js/src/xpconnect/tests/components/native/xpctest_attributes.cpp => js/xpconnect/tests/components/native/xpctest_attributes.cpp rename : js/src/xpconnect/tests/components/native/xpctest_module.cpp => js/xpconnect/tests/components/native/xpctest_module.cpp rename : js/src/xpconnect/tests/components/native/xpctest_params.cpp => js/xpconnect/tests/components/native/xpctest_params.cpp rename : js/src/xpconnect/tests/components/native/xpctest_private.h => js/xpconnect/tests/components/native/xpctest_private.h rename : js/src/xpconnect/tests/idl/Makefile.in => js/xpconnect/tests/idl/Makefile.in rename : js/src/xpconnect/tests/idl/xpctest_attributes.idl => js/xpconnect/tests/idl/xpctest_attributes.idl rename : js/src/xpconnect/tests/idl/xpctest_params.idl => js/xpconnect/tests/idl/xpctest_params.idl rename : js/src/xpconnect/tests/mochitest/Makefile.in => js/xpconnect/tests/mochitest/Makefile.in rename : js/src/xpconnect/tests/mochitest/bug500931_helper.html => js/xpconnect/tests/mochitest/bug500931_helper.html rename : js/src/xpconnect/tests/mochitest/bug504877_helper.html => js/xpconnect/tests/mochitest/bug504877_helper.html rename : js/src/xpconnect/tests/mochitest/bug571849_helper.html => js/xpconnect/tests/mochitest/bug571849_helper.html rename : js/src/xpconnect/tests/mochitest/bug589028_helper.html => js/xpconnect/tests/mochitest/bug589028_helper.html rename : js/src/xpconnect/tests/mochitest/bug657267.jar => js/xpconnect/tests/mochitest/bug657267.jar rename : js/src/xpconnect/tests/mochitest/bug92773_helper.html => js/xpconnect/tests/mochitest/bug92773_helper.html rename : js/src/xpconnect/tests/mochitest/chrome_wrappers_helper.html => js/xpconnect/tests/mochitest/chrome_wrappers_helper.html rename : js/src/xpconnect/tests/mochitest/file1_bug629227.html => js/xpconnect/tests/mochitest/file1_bug629227.html rename : js/src/xpconnect/tests/mochitest/file2_bug629227.html => js/xpconnect/tests/mochitest/file2_bug629227.html rename : js/src/xpconnect/tests/mochitest/file_bug505915.html => js/xpconnect/tests/mochitest/file_bug505915.html rename : js/src/xpconnect/tests/mochitest/file_bug650273.html => js/xpconnect/tests/mochitest/file_bug650273.html rename : js/src/xpconnect/tests/mochitest/file_bug658560.html => js/xpconnect/tests/mochitest/file_bug658560.html rename : js/src/xpconnect/tests/mochitest/file_doublewrappedcompartments.html => js/xpconnect/tests/mochitest/file_doublewrappedcompartments.html rename : js/src/xpconnect/tests/mochitest/file_evalInSandbox.html => js/xpconnect/tests/mochitest/file_evalInSandbox.html rename : js/src/xpconnect/tests/mochitest/file_nodelists.html => js/xpconnect/tests/mochitest/file_nodelists.html rename : js/src/xpconnect/tests/mochitest/file_wrappers-2.html => js/xpconnect/tests/mochitest/file_wrappers-2.html rename : js/src/xpconnect/tests/mochitest/inner.html => js/xpconnect/tests/mochitest/inner.html rename : js/src/xpconnect/tests/mochitest/test1_bug629331.html => js/xpconnect/tests/mochitest/test1_bug629331.html rename : js/src/xpconnect/tests/mochitest/test2_bug629331.html => js/xpconnect/tests/mochitest/test2_bug629331.html rename : js/src/xpconnect/tests/mochitest/test_bug361111.xul => js/xpconnect/tests/mochitest/test_bug361111.xul rename : js/src/xpconnect/tests/mochitest/test_bug384632.html => js/xpconnect/tests/mochitest/test_bug384632.html rename : js/src/xpconnect/tests/mochitest/test_bug390488.html => js/xpconnect/tests/mochitest/test_bug390488.html rename : js/src/xpconnect/tests/mochitest/test_bug393269.html => js/xpconnect/tests/mochitest/test_bug393269.html rename : js/src/xpconnect/tests/mochitest/test_bug396851.html => js/xpconnect/tests/mochitest/test_bug396851.html rename : js/src/xpconnect/tests/mochitest/test_bug428021.html => js/xpconnect/tests/mochitest/test_bug428021.html rename : js/src/xpconnect/tests/mochitest/test_bug446584.html => js/xpconnect/tests/mochitest/test_bug446584.html rename : js/src/xpconnect/tests/mochitest/test_bug462428.html => js/xpconnect/tests/mochitest/test_bug462428.html rename : js/src/xpconnect/tests/mochitest/test_bug478438.html => js/xpconnect/tests/mochitest/test_bug478438.html rename : js/src/xpconnect/tests/mochitest/test_bug484107.html => js/xpconnect/tests/mochitest/test_bug484107.html rename : js/src/xpconnect/tests/mochitest/test_bug500691.html => js/xpconnect/tests/mochitest/test_bug500691.html rename : js/src/xpconnect/tests/mochitest/test_bug502959.html => js/xpconnect/tests/mochitest/test_bug502959.html rename : js/src/xpconnect/tests/mochitest/test_bug503926.html => js/xpconnect/tests/mochitest/test_bug503926.html rename : js/src/xpconnect/tests/mochitest/test_bug504877.html => js/xpconnect/tests/mochitest/test_bug504877.html rename : js/src/xpconnect/tests/mochitest/test_bug505915.html => js/xpconnect/tests/mochitest/test_bug505915.html rename : js/src/xpconnect/tests/mochitest/test_bug553407.html => js/xpconnect/tests/mochitest/test_bug553407.html rename : js/src/xpconnect/tests/mochitest/test_bug560351.html => js/xpconnect/tests/mochitest/test_bug560351.html rename : js/src/xpconnect/tests/mochitest/test_bug564330.html => js/xpconnect/tests/mochitest/test_bug564330.html rename : js/src/xpconnect/tests/mochitest/test_bug585745.html => js/xpconnect/tests/mochitest/test_bug585745.html rename : js/src/xpconnect/tests/mochitest/test_bug589028.html => js/xpconnect/tests/mochitest/test_bug589028.html rename : js/src/xpconnect/tests/mochitest/test_bug601299.html => js/xpconnect/tests/mochitest/test_bug601299.html rename : js/src/xpconnect/tests/mochitest/test_bug605167.html => js/xpconnect/tests/mochitest/test_bug605167.html rename : js/src/xpconnect/tests/mochitest/test_bug618017.html => js/xpconnect/tests/mochitest/test_bug618017.html rename : js/src/xpconnect/tests/mochitest/test_bug623437.html => js/xpconnect/tests/mochitest/test_bug623437.html rename : js/src/xpconnect/tests/mochitest/test_bug628410.html => js/xpconnect/tests/mochitest/test_bug628410.html rename : js/src/xpconnect/tests/mochitest/test_bug628794.html => js/xpconnect/tests/mochitest/test_bug628794.html rename : js/src/xpconnect/tests/mochitest/test_bug629227.html => js/xpconnect/tests/mochitest/test_bug629227.html rename : js/src/xpconnect/tests/mochitest/test_bug629331.html => js/xpconnect/tests/mochitest/test_bug629331.html rename : js/src/xpconnect/tests/mochitest/test_bug636097.html => js/xpconnect/tests/mochitest/test_bug636097.html rename : js/src/xpconnect/tests/mochitest/test_bug650273.html => js/xpconnect/tests/mochitest/test_bug650273.html rename : js/src/xpconnect/tests/mochitest/test_bug655297.html => js/xpconnect/tests/mochitest/test_bug655297.html rename : js/src/xpconnect/tests/mochitest/test_bug657267.html => js/xpconnect/tests/mochitest/test_bug657267.html rename : js/src/xpconnect/tests/mochitest/test_bug661980.html => js/xpconnect/tests/mochitest/test_bug661980.html rename : js/src/xpconnect/tests/mochitest/test_bug691059.html => js/xpconnect/tests/mochitest/test_bug691059.html rename : js/src/xpconnect/tests/mochitest/test_bug92773.html => js/xpconnect/tests/mochitest/test_bug92773.html rename : js/src/xpconnect/tests/mochitest/test_frameWrapping.html => js/xpconnect/tests/mochitest/test_frameWrapping.html rename : js/src/xpconnect/tests/unit/CatRegistrationComponents.manifest => js/xpconnect/tests/unit/CatRegistrationComponents.manifest rename : js/src/xpconnect/tests/unit/bogus_element_type.jsm => js/xpconnect/tests/unit/bogus_element_type.jsm rename : js/src/xpconnect/tests/unit/bogus_exports_type.jsm => js/xpconnect/tests/unit/bogus_exports_type.jsm rename : js/src/xpconnect/tests/unit/bug451678_subscript.js => js/xpconnect/tests/unit/bug451678_subscript.js rename : js/src/xpconnect/tests/unit/bug596580_versioned.js => js/xpconnect/tests/unit/bug596580_versioned.js rename : js/src/xpconnect/tests/unit/component-file.js => js/xpconnect/tests/unit/component-file.js rename : js/src/xpconnect/tests/unit/component-file.manifest => js/xpconnect/tests/unit/component-file.manifest rename : js/src/xpconnect/tests/unit/component_import.js => js/xpconnect/tests/unit/component_import.js rename : js/src/xpconnect/tests/unit/component_import.manifest => js/xpconnect/tests/unit/component_import.manifest rename : js/src/xpconnect/tests/unit/recursive_importA.jsm => js/xpconnect/tests/unit/recursive_importA.jsm rename : js/src/xpconnect/tests/unit/recursive_importB.jsm => js/xpconnect/tests/unit/recursive_importB.jsm rename : js/src/xpconnect/tests/unit/syntax_error.jsm => js/xpconnect/tests/unit/syntax_error.jsm rename : js/src/xpconnect/tests/unit/test_attributes.js => js/xpconnect/tests/unit/test_attributes.js rename : js/src/xpconnect/tests/unit/test_bogus_files.js => js/xpconnect/tests/unit/test_bogus_files.js rename : js/src/xpconnect/tests/unit/test_bug408412.js => js/xpconnect/tests/unit/test_bug408412.js rename : js/src/xpconnect/tests/unit/test_bug451678.js => js/xpconnect/tests/unit/test_bug451678.js rename : js/src/xpconnect/tests/unit/test_bug596580.js => js/xpconnect/tests/unit/test_bug596580.js rename : js/src/xpconnect/tests/unit/test_bug604362.js => js/xpconnect/tests/unit/test_bug604362.js rename : js/src/xpconnect/tests/unit/test_bug608142.js => js/xpconnect/tests/unit/test_bug608142.js rename : js/src/xpconnect/tests/unit/test_bug641378.js => js/xpconnect/tests/unit/test_bug641378.js rename : js/src/xpconnect/tests/unit/test_bug677864.js => js/xpconnect/tests/unit/test_bug677864.js rename : js/src/xpconnect/tests/unit/test_bug_442086.js => js/xpconnect/tests/unit/test_bug_442086.js rename : js/src/xpconnect/tests/unit/test_file.js => js/xpconnect/tests/unit/test_file.js rename : js/src/xpconnect/tests/unit/test_import.js => js/xpconnect/tests/unit/test_import.js rename : js/src/xpconnect/tests/unit/test_js_weak_references.js => js/xpconnect/tests/unit/test_js_weak_references.js rename : js/src/xpconnect/tests/unit/test_localeCompare.js => js/xpconnect/tests/unit/test_localeCompare.js rename : js/src/xpconnect/tests/unit/test_params.js => js/xpconnect/tests/unit/test_params.js rename : js/src/xpconnect/tests/unit/test_recursive_import.js => js/xpconnect/tests/unit/test_recursive_import.js rename : js/src/xpconnect/tests/unit/test_reflect_parse.js => js/xpconnect/tests/unit/test_reflect_parse.js rename : js/src/xpconnect/tests/unit/test_unload.js => js/xpconnect/tests/unit/test_unload.js rename : js/src/xpconnect/tests/unit/test_xpcomutils.js => js/xpconnect/tests/unit/test_xpcomutils.js rename : js/src/xpconnect/tests/unit/xpcshell.ini => js/xpconnect/tests/unit/xpcshell.ini rename : js/src/xpconnect/wrappers/AccessCheck.cpp => js/xpconnect/wrappers/AccessCheck.cpp rename : js/src/xpconnect/wrappers/AccessCheck.h => js/xpconnect/wrappers/AccessCheck.h rename : js/src/xpconnect/wrappers/CrossOriginWrapper.cpp => js/xpconnect/wrappers/CrossOriginWrapper.cpp rename : js/src/xpconnect/wrappers/CrossOriginWrapper.h => js/xpconnect/wrappers/CrossOriginWrapper.h rename : js/src/xpconnect/wrappers/FilteringWrapper.cpp => js/xpconnect/wrappers/FilteringWrapper.cpp rename : js/src/xpconnect/wrappers/FilteringWrapper.h => js/xpconnect/wrappers/FilteringWrapper.h rename : js/src/xpconnect/wrappers/Makefile.in => js/xpconnect/wrappers/Makefile.in rename : js/src/xpconnect/wrappers/WrapperFactory.cpp => js/xpconnect/wrappers/WrapperFactory.cpp rename : js/src/xpconnect/wrappers/WrapperFactory.h => js/xpconnect/wrappers/WrapperFactory.h rename : js/src/xpconnect/wrappers/XrayWrapper.cpp => js/xpconnect/wrappers/XrayWrapper.cpp rename : js/src/xpconnect/wrappers/XrayWrapper.h => js/xpconnect/wrappers/XrayWrapper.h
2011-10-14 17:52:47 +00:00
#include "XPCQuickStubs.h"
#include "jstypedarray.h"
#include "nsDOMJSUtils.h"
using namespace mozilla;
#define LOAD_STR "load"
#define LOADSTART_STR "loadstart"
#define LOADEND_STR "loadend"
using mozilla::dom::FileIOObject;
NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMFileReader)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsDOMFileReader,
FileIOObject)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mFile)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mPrincipal)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mChannel)
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(load)
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(loadstart)
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(loadend)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsDOMFileReader,
FileIOObject)
tmp->mResultArrayBuffer = nsnull;
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mFile)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mPrincipal)
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(load)
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(loadstart)
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(loadend)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(nsDOMFileReader,
nsDOMEventTargetWrapperCache)
if(tmp->mResultArrayBuffer) {
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_CALLBACK(tmp->mResultArrayBuffer,
"mResultArrayBuffer")
}
NS_IMPL_CYCLE_COLLECTION_TRACE_END
DOMCI_DATA(FileReader, nsDOMFileReader)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsDOMFileReader)
NS_INTERFACE_MAP_ENTRY(nsIDOMFileReader)
NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_ENTRY(nsIJSNativeInitializer)
NS_INTERFACE_MAP_ENTRY(nsICharsetDetectionObserver)
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(FileReader)
NS_INTERFACE_MAP_END_INHERITING(FileIOObject)
NS_IMPL_ADDREF_INHERITED(nsDOMFileReader, FileIOObject)
NS_IMPL_RELEASE_INHERITED(nsDOMFileReader, FileIOObject)
void
nsDOMFileReader::RootResultArrayBuffer()
{
nsContentUtils::PreserveWrapper(
static_cast<nsIDOMEventTarget*>(
static_cast<nsDOMEventTargetHelper*>(this)), this);
}
//nsICharsetDetectionObserver
NS_IMETHODIMP
nsDOMFileReader::Notify(const char *aCharset, nsDetectionConfident aConf)
{
mCharset = aCharset;
return NS_OK;
}
//nsDOMFileReader constructors/initializers
nsDOMFileReader::nsDOMFileReader()
: mFileData(nsnull),
mDataLen(0), mDataFormat(FILE_AS_BINARY),
mResultArrayBuffer(nsnull)
{
nsLayoutStatics::AddRef();
SetDOMStringToNull(mResult);
}
nsDOMFileReader::~nsDOMFileReader()
{
FreeFileData();
nsLayoutStatics::Release();
}
nsresult
nsDOMFileReader::Init()
{
nsDOMEventTargetWrapperCache::Init();
nsIScriptSecurityManager *secMan = nsContentUtils::GetSecurityManager();
nsCOMPtr<nsIPrincipal> subjectPrincipal;
if (secMan) {
2011-06-21 16:52:22 +00:00
nsresult rv = secMan->GetSubjectPrincipal(getter_AddRefs(subjectPrincipal));
NS_ENSURE_SUCCESS(rv, rv);
}
NS_ENSURE_STATE(subjectPrincipal);
mPrincipal.swap(subjectPrincipal);
return NS_OK;
}
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)
NS_IMETHODIMP
nsDOMFileReader::Initialize(nsISupports* aOwner, JSContext* cx, JSObject* obj,
PRUint32 argc, jsval *argv)
{
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
nsDOMFileReader::GetInterface(const nsIID & aIID, void **aResult)
{
return QueryInterface(aIID, aResult);
}
// nsIDOMFileReader
NS_IMETHODIMP
nsDOMFileReader::GetReadyState(PRUint16 *aReadyState)
{
return FileIOObject::GetReadyState(aReadyState);
}
NS_IMETHODIMP
nsDOMFileReader::GetResult(JSContext* aCx, jsval* aResult)
{
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;
if (!xpc_qsStringToJsval(aCx, tmpResult, aResult)) {
return NS_ERROR_FAILURE;
}
return NS_OK;
}
NS_IMETHODIMP
nsDOMFileReader::GetError(nsIDOMFileError** aError)
{
return FileIOObject::GetError(aError);
}
NS_IMETHODIMP
nsDOMFileReader::ReadAsArrayBuffer(nsIDOMBlob* aFile, JSContext* aCx)
{
return ReadFileContent(aCx, aFile, EmptyString(), FILE_AS_ARRAYBUFFER);
}
NS_IMETHODIMP
nsDOMFileReader::ReadAsBinaryString(nsIDOMBlob* aFile)
{
return ReadFileContent(nsnull, aFile, EmptyString(), FILE_AS_BINARY);
}
NS_IMETHODIMP
nsDOMFileReader::ReadAsText(nsIDOMBlob* aFile,
const nsAString &aCharset)
{
return ReadFileContent(nsnull, aFile, aCharset, FILE_AS_TEXT);
}
NS_IMETHODIMP
nsDOMFileReader::ReadAsDataURL(nsIDOMBlob* aFile)
{
return ReadFileContent(nsnull, aFile, EmptyString(), FILE_AS_DATAURL);
}
NS_IMETHODIMP
nsDOMFileReader::Abort()
{
return FileIOObject::Abort();
}
nsresult
nsDOMFileReader::DoAbort(nsAString& aEvent)
{
// Revert status and result attributes
SetDOMStringToNull(mResult);
mResultArrayBuffer = nsnull;
// Non-null channel indicates a read is currently active
if (mChannel) {
// Cancel request requires an error status
mChannel->Cancel(NS_ERROR_FAILURE);
mChannel = nsnull;
}
mFile = nsnull;
//Clean up memory buffer
FreeFileData();
// Tell the base class which event to dispatch
aEvent = NS_LITERAL_STRING(LOADEND_STR);
return NS_OK;
}
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;
}
nsresult
nsDOMFileReader::DoOnDataAvailable(nsIRequest *aRequest,
nsISupports *aContext,
nsIInputStream *aInputStream,
PRUint32 aOffset,
PRUint32 aCount)
{
if (mDataFormat == FILE_AS_BINARY) {
//Continuously update our binary string as data comes in
NS_ASSERTION(mResult.Length() == aOffset,
"unexpected mResult length");
PRUint32 oldLen = mResult.Length();
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");
}
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");
}
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);
PRUint32 bytesRead = 0;
aInputStream->Read(mFileData + aOffset, aCount, &bytesRead);
NS_ASSERTION(bytesRead == aCount, "failed to read data");
mDataLen += aCount;
}
return NS_OK;
}
nsresult
nsDOMFileReader::DoOnStopRequest(nsIRequest *aRequest,
nsISupports *aContext,
nsresult aStatus,
nsAString& aSuccessEvent,
nsAString& aTerminationEvent)
{
aSuccessEvent = NS_LITERAL_STRING(LOAD_STR);
aTerminationEvent = NS_LITERAL_STRING(LOADEND_STR);
// Clear out the data if necessary
if (NS_FAILED(aStatus)) {
FreeFileData();
return NS_OK;
}
nsresult rv = NS_OK;
switch (mDataFormat) {
case FILE_AS_ARRAYBUFFER:
break; //Already accumulated mResultArrayBuffer
case FILE_AS_BINARY:
break; //Already accumulated mResult
case FILE_AS_TEXT:
rv = GetAsText(mCharset, mFileData, mDataLen, mResult);
break;
case FILE_AS_DATAURL:
rv = GetAsDataURL(mFile, mFileData, mDataLen, mResult);
break;
}
mResult.SetIsVoid(PR_FALSE);
FreeFileData();
return rv;
}
// Helper methods
nsresult
nsDOMFileReader::ReadFileContent(JSContext* aCx,
nsIDOMBlob* aFile,
const nsAString &aCharset,
eDataFormat aDataFormat)
{
nsresult rv;
NS_ENSURE_TRUE(aFile, NS_ERROR_NULL_POINTER);
//Implicit abort to clear any other activity going on
Abort();
mError = nsnull;
SetDOMStringToNull(mResult);
mTransferred = 0;
mTotal = 0;
mReadyState = nsIDOMFileReader::EMPTY;
FreeFileData();
mFile = aFile;
mDataFormat = aDataFormat;
CopyUTF16toUTF8(aCharset, mCharset);
2010-09-05 15:32:57 +00:00
//Establish a channel with our file
{
// Hold the internal URL alive only as long as necessary
// After the channel is created it will own whatever is backing
// the DOMFile.
nsDOMFileInternalUrlHolder urlHolder(mFile, mPrincipal);
nsCOMPtr<nsIURI> uri;
rv = NS_NewURI(getter_AddRefs(uri), urlHolder.mUrl);
NS_ENSURE_SUCCESS(rv, rv);
rv = NS_NewChannel(getter_AddRefs(mChannel), uri);
NS_ENSURE_SUCCESS(rv, rv);
}
//Obtain the total size of the file before reading
mTotal = mozilla::dom::kUnknownSize;
mFile->GetSize(&mTotal);
rv = mChannel->AsyncOpen(this, nsnull);
NS_ENSURE_SUCCESS(rv, rv);
//FileReader should be in loading state here
mReadyState = nsIDOMFileReader::LOADING;
DispatchProgressEvent(NS_LITERAL_STRING(LOADSTART_STR));
if (mDataFormat == FILE_AS_ARRAYBUFFER) {
RootResultArrayBuffer();
mResultArrayBuffer = js_CreateArrayBuffer(aCx, mTotal);
if (!mResultArrayBuffer) {
NS_WARNING("Failed to create JS array buffer");
return NS_ERROR_FAILURE;
}
}
return NS_OK;
}
nsresult
nsDOMFileReader::GetAsText(const nsACString &aCharset,
const char *aFileData,
PRUint32 aDataLen,
nsAString& aResult)
{
nsresult rv;
nsCAutoString charsetGuess;
if (!aCharset.IsEmpty()) {
charsetGuess = aCharset;
} else {
rv = GuessCharset(aFileData, aDataLen, charsetGuess);
NS_ENSURE_SUCCESS(rv, rv);
}
nsCAutoString charset;
nsCOMPtr<nsICharsetAlias> alias = do_GetService(NS_CHARSETALIAS_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = alias->GetPreferred(charsetGuess, charset);
NS_ENSURE_SUCCESS(rv, rv);
rv = ConvertStream(aFileData, aDataLen, charset.get(), aResult);
return NS_OK;
}
nsresult
nsDOMFileReader::GetAsDataURL(nsIDOMBlob *aFile,
const char *aFileData,
PRUint32 aDataLen,
nsAString& aResult)
{
aResult.AssignLiteral("data:");
nsresult rv;
nsString contentType;
rv = aFile->GetType(contentType);
if (NS_SUCCEEDED(rv) && !contentType.IsEmpty()) {
aResult.Append(contentType);
} else {
aResult.AppendLiteral("application/octet-stream");
}
aResult.AppendLiteral(";base64,");
PRUint32 totalRead = 0;
while (aDataLen > totalRead) {
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;
}
return NS_OK;
}
nsresult
nsDOMFileReader::ConvertStream(const char *aFileData,
PRUint32 aDataLen,
const char *aCharset,
nsAString &aResult)
{
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
nsDOMFileReader::GuessCharset(const char *aFileData,
PRUint32 aDataLen,
nsACString &aCharset)
{
// 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
const nsAdoptingCString& detectorName =
Preferences::GetLocalizedCString("intl.charset.detector");
if (!detectorName.IsEmpty()) {
nsCAutoString detectorContractID;
detectorContractID.AssignLiteral(NS_CHARSET_DETECTOR_CONTRACTID_BASE);
detectorContractID += detectorName;
detector = do_CreateInstance(detectorContractID.get());
}
}
nsresult rv;
// 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) {
mCharset.Truncate();
detector->Init(this);
bool done;
rv = detector->DoIt(aFileData, aDataLen, &done);
NS_ENSURE_SUCCESS(rv, rv);
rv = detector->Done();
NS_ENSURE_SUCCESS(rv, rv);
aCharset = mCharset;
} else {
// no charset detector available, check the BOM
unsigned char sniffBuf[3];
PRUint32 numRead = (aDataLen >= sizeof(sniffBuf) ? sizeof(sniffBuf) : aDataLen);
memcpy(sniffBuf, aFileData, numRead);
if (numRead >= 2 &&
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;
}