Backing out Bug 583863 because it leaks. a=backout

This commit is contained in:
Kyle Huey 2010-09-05 11:34:10 -04:00
commit dc5f7104a6
27 changed files with 970 additions and 995 deletions

View File

@ -45,11 +45,6 @@ function test() {
return false;
}
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("TmpD", Components.interfaces.nsIFile);
filePath = file.path;
let fieldList = {
"//input[@name='input']": Date.now().toString(),
"//input[@name='spaced 1']": Math.random().toString(),
@ -64,7 +59,7 @@ function test() {
"//textarea[1]": "",
"//textarea[2]": "Some text... " + Math.random(),
"//textarea[3]": "Some more text\n" + new Date(),
"//input[@type='file']": filePath
"//input[@type='file']": "/dev/null"
};
function getElementByXPath(aTab, aQuery) {

View File

@ -36,19 +36,6 @@
function test() {
/** Test for Bug 346337 **/
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("TmpD", Components.interfaces.nsILocalFile);
file.append("346337_test1.file");
file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0666);
filePath1 = file.path;
file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("TmpD", Components.interfaces.nsILocalFile);
file.append("346337_test2.file");
file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0666);
filePath2 = file.path;
let fieldList = {
"//input[@name='input']": Date.now().toString(),
@ -64,8 +51,8 @@ function test() {
"//textarea[1]": "",
"//textarea[2]": "Some text... " + Math.random(),
"//textarea[3]": "Some more text\n" + new Date(),
"//input[@type='file'][1]": [filePath1],
"//input[@type='file'][2]": [filePath1, filePath2]
"//input[@type='file'][1]": ["/dev/null"],
"//input[@type='file'][2]": ["/dev/null", "/dev/stdin"]
};
function getElementByXPath(aTab, aQuery) {

View File

@ -38,16 +38,10 @@ function test() {
/** Test for Bug 466937 **/
waitForExplicitFinish();
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("TmpD", Components.interfaces.nsILocalFile);
file.append("466937_test.file");
file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0666);
filePath = file.path;
let testURL = "http://mochi.test:8888/browser/" +
"browser/components/sessionstore/test/browser/browser_466937_sample.html";
let testPath = "/home/user/regular.file";
let tab = gBrowser.addTab(testURL);
tab.linkedBrowser.addEventListener("load", function(aEvent) {

View File

@ -99,6 +99,7 @@ XPIDLSRCS = \
nsIDocumentEncoder.idl \
nsIDOMFile.idl \
nsIDOMFileReader.idl \
nsIDOMFileInternal.idl \
nsIDOMFileList.idl \
nsIDOMFileException.idl \
nsIDOMFileError.idl \

View File

@ -41,6 +41,7 @@
#include "nsICharsetDetectionObserver.h"
#include "nsIDOMFile.h"
#include "nsIDOMFileInternal.h"
#include "nsIDOMFileList.h"
#include "nsIDOMFileError.h"
#include "nsIInputStream.h"
@ -50,32 +51,24 @@
#include "nsIWeakReference.h"
#include "nsIWeakReferenceUtils.h"
#include "nsIDocument.h"
#include "nsIXMLHttpRequest.h"
class nsIDOMDocument;
class nsIFile;
class nsIInputStream;
class nsDOMFile : public nsIDOMFile,
public nsIXHRSendable,
public nsIDOMFileInternal,
public nsICharsetDetectionObserver
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIDOMFILE
NS_DECL_NSIXHRSENDABLE
nsDOMFile(nsIFile *aFile, nsIDocument* aRelatedDoc, nsAString& aContentType)
: mFile(aFile),
mRelatedDoc(do_GetWeakReference(aRelatedDoc)),
mContentType(aContentType)
{}
NS_DECL_NSIDOMFILEINTERNAL
nsDOMFile(nsIFile *aFile, nsIDocument* aRelatedDoc)
: mFile(aFile),
mRelatedDoc(do_GetWeakReference(aRelatedDoc))
{}
~nsDOMFile() {}
// from nsICharsetDetectionObserver
@ -94,30 +87,6 @@ private:
nsAString &aResult);
};
class nsDOMMemoryFile : public nsDOMFile
{
public:
nsDOMMemoryFile(void *aMemoryBuffer,
PRUint64 aLength,
nsAString& aContentType,
nsIDocument *aRelatedDoc)
: nsDOMFile(nsnull, aRelatedDoc, aContentType),
mInternalData(aMemoryBuffer), mLength(aLength)
{ }
~nsDOMMemoryFile()
{ free(mInternalData); }
NS_IMETHOD GetName(nsAString&);
NS_IMETHOD GetSize(PRUint64*);
NS_IMETHOD GetInternalStream(nsIInputStream**);
NS_IMETHOD GetMozFullPathInternal(nsAString&);
protected:
void* mInternalData;
PRUint64 mLength;
};
class nsDOMFileList : public nsIDOMFileList
{
public:

View File

@ -38,10 +38,8 @@
#include "domstubs.idl"
interface nsIDOMFileError;
interface nsIInputStream;
interface nsIURI;
[scriptable, uuid(e72e2d6e-e267-4ea1-a798-6703d3a2c362)]
[scriptable, uuid(b10293e1-d531-4bdd-9b2b-4d8c1c9bc633)]
interface nsIDOMFile : nsISupports
{
//fileName and fileSize are now deprecated attributes
@ -58,11 +56,4 @@ interface nsIDOMFile : nsISupports
DOMString getAsText(in DOMString encoding); // raises(FileException) on retrieval
DOMString getAsDataURL(); // raises(FileException) on retrieval
DOMString getAsBinary(); // raises(FileException) on retrieval
[noscript] readonly attribute nsIInputStream internalStream;
// The caller is responsible for releasing the internalUrl from the
// moz-filedata: protocol handler
[noscript] readonly attribute DOMString internalUrl;
// This performs no security checks!
[noscript] readonly attribute DOMString mozFullPathInternal;
};

View File

@ -0,0 +1,46 @@
/* -*- Mode: IDL; 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 mozilla.org code.
*
* The Initial Developer of the Original Code is
* Mozilla Corporation
* 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 "domstubs.idl"
interface nsIFile;
[scriptable, uuid(047CA6C4-52B3-46F1-8976-E198B724F72F)]
interface nsIDOMFileInternal : nsISupports
{
attribute nsIFile internalFile;
};

View File

@ -15,7 +15,7 @@
* The Original Code is mozila.org code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation
* Mozilla Corporation
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
@ -61,7 +61,6 @@
#include "nsNetUtil.h"
#include "nsIUUIDGenerator.h"
#include "nsFileDataProtocolHandler.h"
#include "nsStringStream.h"
#include "plbase64.h"
#include "prmem.h"
@ -73,7 +72,7 @@ DOMCI_DATA(File, nsDOMFile)
NS_INTERFACE_MAP_BEGIN(nsDOMFile)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMFile)
NS_INTERFACE_MAP_ENTRY(nsIDOMFile)
NS_INTERFACE_MAP_ENTRY(nsIXHRSendable)
NS_INTERFACE_MAP_ENTRY(nsIDOMFileInternal)
NS_INTERFACE_MAP_ENTRY(nsICharsetDetectionObserver)
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(File)
NS_INTERFACE_MAP_END
@ -117,18 +116,12 @@ NS_IMETHODIMP
nsDOMFile::GetMozFullPath(nsAString &aFileName)
{
if (nsContentUtils::IsCallerTrustedForCapability("UniversalFileRead")) {
return GetMozFullPathInternal(aFileName);
return mFile->GetPath(aFileName);
}
aFileName.Truncate();
return NS_OK;
}
NS_IMETHODIMP
nsDOMFile::GetMozFullPathInternal(nsAString &aFilename)
{
return mFile->GetPath(aFilename);
}
NS_IMETHODIMP
nsDOMFile::GetSize(PRUint64 *aFileSize)
{
@ -169,56 +162,36 @@ nsDOMFile::GetType(nsAString &aType)
return NS_OK;
}
NS_IMETHODIMP
nsDOMFile::GetInternalStream(nsIInputStream **aStream)
{
return NS_NewLocalFileInputStream(aStream, mFile, -1, -1,
nsIFileInputStream::CLOSE_ON_EOF |
nsIFileInputStream::REOPEN_ON_REWIND);
}
NS_IMETHODIMP
nsDOMFile::GetUrl(nsAString& aURL)
{
if (mURL.IsEmpty()) {
GetInternalUrl(mURL);
nsresult rv;
nsCOMPtr<nsIUUIDGenerator> uuidgen =
do_GetService("@mozilla.org/uuid-generator;1", &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsID id;
rv = uuidgen->GenerateUUIDInPlace(&id);
NS_ENSURE_SUCCESS(rv, rv);
char chars[NSID_LENGTH];
id.ToProvidedString(chars);
nsCString url = NS_LITERAL_CSTRING(FILEDATA_SCHEME ":") +
Substring(chars + 1, chars + NSID_LENGTH - 2);
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mRelatedDoc);
NS_LossyConvertUTF16toASCII shortURL(mURL);
doc->RegisterFileDataUri(shortURL);
if (doc) {
doc->RegisterFileDataUri(url);
nsFileDataProtocolHandler::AddFileDataEntry(url, mFile,
doc->NodePrincipal());
}
CopyASCIItoUTF16(url, mURL);
}
aURL = mURL;
return NS_OK;
}
NS_IMETHODIMP
nsDOMFile::GetInternalUrl(nsAString& aURL)
{
nsresult rv;
nsCOMPtr<nsIUUIDGenerator> uuidgen =
do_GetService("@mozilla.org/uuid-generator;1", &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsID id;
rv = uuidgen->GenerateUUIDInPlace(&id);
NS_ENSURE_SUCCESS(rv, rv);
char chars[NSID_LENGTH];
id.ToProvidedString(chars);
nsCString url = NS_LITERAL_CSTRING(FILEDATA_SCHEME ":") +
Substring(chars + 1, chars + NSID_LENGTH - 2);
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mRelatedDoc);
if (doc) {
nsFileDataProtocolHandler::AddFileDataEntry(url,
this,
doc->NodePrincipal());
}
CopyASCIItoUTF16(url, aURL);
return NS_OK;
}
@ -229,7 +202,9 @@ nsDOMFile::GetAsText(const nsAString &aCharset, nsAString &aResult)
aResult.Truncate();
nsCOMPtr<nsIInputStream> stream;
nsresult rv = GetInternalStream(getter_AddRefs(stream));
nsresult rv = NS_NewLocalFileInputStream
(getter_AddRefs(stream),
mFile, -1, -1, 0);
NS_ENSURE_SUCCESS(rv, DOMFileResult(rv));
nsCAutoString charsetGuess;
@ -256,33 +231,43 @@ nsDOMFile::GetAsText(const nsAString &aCharset, nsAString &aResult)
return ConvertStream(stream, charset.get(), aResult);
}
NS_IMETHODIMP
nsDOMFile::GetInternalFile(nsIFile **aFile)
{
NS_IF_ADDREF(*aFile = mFile);
return NS_OK;
}
NS_IMETHODIMP
nsDOMFile::SetInternalFile(nsIFile *aFile)
{
mFile = aFile;
return NS_OK;
}
NS_IMETHODIMP
nsDOMFile::GetAsDataURL(nsAString &aResult)
{
aResult.AssignLiteral("data:");
nsresult rv;
if (!mContentType.Length()) {
nsCOMPtr<nsIMIMEService> mimeService =
do_GetService(NS_MIMESERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIMIMEService> mimeService =
do_GetService(NS_MIMESERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCAutoString contentType;
rv = mimeService->GetTypeFromFile(mFile, contentType);
if (NS_SUCCEEDED(rv)) {
CopyUTF8toUTF16(contentType, mContentType);
}
}
if (mContentType.Length()) {
aResult.Append(mContentType);
nsCAutoString contentType;
rv = mimeService->GetTypeFromFile(mFile, contentType);
if (NS_SUCCEEDED(rv)) {
AppendUTF8toUTF16(contentType, aResult);
} else {
aResult.AppendLiteral("application/octet-stream");
}
aResult.AppendLiteral(";base64,");
nsCOMPtr<nsIInputStream> stream;
rv = GetInternalStream(getter_AddRefs(stream));
rv = NS_NewLocalFileInputStream(getter_AddRefs(stream),
mFile, -1, -1,
nsIFileInputStream::CLOSE_ON_EOF);
NS_ENSURE_SUCCESS(rv, DOMFileResult(rv));
char readBuf[4096];
@ -322,7 +307,10 @@ nsDOMFile::GetAsBinary(nsAString &aResult)
aResult.Truncate();
nsCOMPtr<nsIInputStream> stream;
nsresult rv = GetInternalStream(getter_AddRefs(stream));
nsresult rv = NS_NewLocalFileInputStream
(getter_AddRefs(stream),
mFile, -1, -1,
nsIFileInputStream::CLOSE_ON_EOF);
NS_ENSURE_SUCCESS(rv, DOMFileResult(rv));
PRUint32 numRead;
@ -435,29 +423,6 @@ nsDOMFile::GuessCharset(nsIInputStream *aStream,
return NS_OK;
}
NS_IMETHODIMP
nsDOMFile::GetSendInfo(nsIInputStream** aBody,
nsACString& aContentType,
nsACString& aCharset)
{
nsresult rv;
nsCOMPtr<nsIInputStream> stream;
rv = this->GetInternalStream(getter_AddRefs(stream));
NS_ENSURE_SUCCESS(rv, rv);
nsString contentType;
rv = this->GetType(contentType);
NS_ENSURE_SUCCESS(rv, rv);
CopyUTF16toUTF8(contentType, aContentType);
aCharset.Truncate();
stream.forget(aBody);
return NS_OK;
}
NS_IMETHODIMP
nsDOMFile::Notify(const char* aCharset, nsDetectionConfident aConf)
{
@ -498,39 +463,6 @@ nsDOMFile::ConvertStream(nsIInputStream *aStream,
return rv;
}
// nsDOMMemoryFile Implementation
NS_IMETHODIMP
nsDOMMemoryFile::GetName(nsAString &aFileName)
{
aFileName.Truncate();
return NS_OK;
}
NS_IMETHODIMP
nsDOMMemoryFile::GetSize(PRUint64 *aFileSize)
{
*aFileSize = mLength;
return NS_OK;
}
NS_IMETHODIMP
nsDOMMemoryFile::GetInternalStream(nsIInputStream **aStream)
{
if (mLength > PR_INT32_MAX)
return NS_ERROR_FAILURE;
PRInt32 l = mLength;
return NS_NewByteInputStream(aStream, (const char*)mInternalData, l);
}
NS_IMETHODIMP
nsDOMMemoryFile::GetMozFullPathInternal(nsAString &aFilename)
{
aFilename.Truncate();
return NS_OK;
}
// nsDOMFileList implementation
DOMCI_DATA(FileList, nsDOMFileList)

View File

@ -15,7 +15,7 @@
* The Original Code is mozila.org code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation
* Mozilla Corporation
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
@ -71,13 +71,13 @@
#include "nsJSEnvironment.h"
#include "nsIScriptGlobalObject.h"
#include "nsIDOMClassInfo.h"
#include "nsIDOMFileInternal.h"
#include "nsCExternalHandlerService.h"
#include "nsIStreamConverterService.h"
#include "nsEventDispatcher.h"
#include "nsCycleCollectionParticipant.h"
#include "nsLayoutStatics.h"
#include "nsIScriptObjectPrincipal.h"
#include "nsFileDataProtocolHandler.h"
#define LOAD_STR "load"
#define ERROR_STR "error"
@ -496,31 +496,26 @@ nsDOMFileReader::ReadFileContent(nsIDOMFile* aFile,
mReadyState = nsIDOMFileReader::EMPTY;
FreeFileData();
mFile = aFile;
mDataFormat = aDataFormat;
mCharset = aCharset;
//Establish a channel with our file
nsAutoString url;
nsresult rv = mFile->GetInternalUrl(url);
//Obtain the nsDOMFile's underlying nsIFile
nsresult rv;
nsCOMPtr<nsIDOMFileInternal> domFile(do_QueryInterface(aFile));
rv = domFile->GetInternalFile(getter_AddRefs(mFile));
NS_ENSURE_SUCCESS(rv, rv);
//Establish a channel with our file
nsCOMPtr<nsIURI> uri;
rv = NS_NewURI(getter_AddRefs(uri), url);
rv = NS_NewFileURI(getter_AddRefs(uri), mFile);
NS_ENSURE_SUCCESS(rv, rv);
rv = NS_NewChannel(getter_AddRefs(mChannel), uri);
NS_ENSURE_SUCCESS(rv, rv);
// After we open the channel we don't need the url anymore. The channel will
// hold alive whatever we're interested in.
nsCAutoString narrowUrl;
CopyUTF16toUTF8(url, narrowUrl);
nsFileDataProtocolHandler::RemoveFileDataEntry(narrowUrl);
//Obtain the total size of the file before reading
mReadTotal = -1;
mFile->GetSize(&mReadTotal);
mFile->GetFileSize(&mReadTotal);
rv = mChannel->AsyncOpen(this, nsnull);
NS_ENSURE_SUCCESS(rv, rv);
@ -609,7 +604,7 @@ nsDOMFileReader::GetAsText(const nsAString &aCharset,
}
nsresult
nsDOMFileReader::GetAsDataURL(nsIDOMFile *aFile,
nsDOMFileReader::GetAsDataURL(nsIFile *aFile,
const char *aFileData,
PRUint32 aDataLen,
nsAString& aResult)
@ -617,10 +612,14 @@ nsDOMFileReader::GetAsDataURL(nsIDOMFile *aFile,
aResult.AssignLiteral("data:");
nsresult rv;
nsString contentType;
rv = aFile->GetType(contentType);
if (NS_SUCCEEDED(rv) && !contentType.IsEmpty()) {
aResult.Append(contentType);
nsCOMPtr<nsIMIMEService> mimeService =
do_GetService(NS_MIMESERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCAutoString contentType;
rv = mimeService->GetTypeFromFile(aFile, contentType);
if (NS_SUCCEEDED(rv)) {
AppendUTF8toUTF16(contentType, aResult);
} else {
aResult.AppendLiteral("application/octet-stream");
}

View File

@ -120,7 +120,7 @@ protected:
nsresult ReadFileContent(nsIDOMFile *aFile, const nsAString &aCharset, eDataFormat aDataFormat);
nsresult GetAsText(const nsAString &aCharset,
const char *aFileData, PRUint32 aDataLen, nsAString &aResult);
nsresult GetAsDataURL(nsIDOMFile *aFile, const char *aFileData, PRUint32 aDataLen, nsAString &aResult);
nsresult GetAsDataURL(nsIFile *aFile, const char *aFileData, PRUint32 aDataLen, nsAString &aResult);
nsresult GuessCharset(const char *aFileData, PRUint32 aDataLen, nsACString &aCharset);
nsresult ConvertStream(const char *aFileData, PRUint32 aDataLen, const char *aCharset, nsAString &aResult);
void DispatchError(nsresult rv);
@ -133,7 +133,7 @@ protected:
}
char *mFileData;
nsCOMPtr<nsIDOMFile> mFile;
nsCOMPtr<nsIFile> mFile;
nsString mCharset;
PRUint32 mDataLen;
@ -150,7 +150,7 @@ protected:
nsCOMPtr<nsIPrincipal> mPrincipal;
nsCOMPtr<nsIChannel> mChannel;
PRUint64 mReadTotal;
PRInt64 mReadTotal;
PRUint64 mReadTransferred;
nsRefPtr<nsDOMEventListenerWrapper> mOnLoadEndListener;

View File

@ -42,7 +42,7 @@
#include "nsNetUtil.h"
#include "nsIURIWithPrincipal.h"
#include "nsIPrincipal.h"
#include "nsIDOMFile.h"
#include "nsIFileChannel.h"
#include "nsISerializable.h"
#include "nsIClassInfo.h"
#include "nsIObjectInputStream.h"
@ -55,15 +55,14 @@ static NS_DEFINE_CID(kSimpleURICID, NS_SIMPLEURI_CID);
// Hash table
struct FileDataInfo
{
nsCOMPtr<nsIDOMFile> mFile;
nsCOMPtr<nsIURI> mFileUri;
nsCOMPtr<nsIPrincipal> mPrincipal;
};
static nsClassHashtable<nsCStringHashKey, FileDataInfo>* gFileDataTable;
void
nsFileDataProtocolHandler::AddFileDataEntry(nsACString& aUri,
nsIDOMFile* aFile,
nsFileDataProtocolHandler::AddFileDataEntry(nsACString& aUri, nsIFile* aFile,
nsIPrincipal* aPrincipal)
{
if (!gFileDataTable) {
@ -73,7 +72,7 @@ nsFileDataProtocolHandler::AddFileDataEntry(nsACString& aUri,
FileDataInfo* info = new FileDataInfo;
info->mFile = aFile;
NS_NewFileURI(getter_AddRefs(info->mFileUri), aFile);
info->mPrincipal = aPrincipal;
gFileDataTable->Put(aUri, info);
@ -414,14 +413,8 @@ nsFileDataProtocolHandler::NewChannel(nsIURI* uri, nsIChannel* *result)
}
#endif
nsCOMPtr<nsIInputStream> stream;
nsresult rv = info->mFile->GetInternalStream(getter_AddRefs(stream));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIChannel> channel;
rv = NS_NewInputStreamChannel(getter_AddRefs(channel),
uri,
stream);
nsresult rv = NS_NewChannel(getter_AddRefs(channel), info->mFileUri);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsISupports> owner = do_QueryInterface(info->mPrincipal);

View File

@ -41,7 +41,7 @@
#define FILEDATA_SCHEME "moz-filedata"
class nsIDOMFile;
class nsIFile;
class nsIPrincipal;
class nsFileDataProtocolHandler : public nsIProtocolHandler
@ -57,8 +57,7 @@ public:
virtual ~nsFileDataProtocolHandler() {}
// Methods for managing uri->file mapping
static void AddFileDataEntry(nsACString& aUri,
nsIDOMFile* aFile,
static void AddFileDataEntry(nsACString& aUri, nsIFile* aFile,
nsIPrincipal* aPrincipal);
static void RemoveFileDataEntry(nsACString& aUri);

View File

@ -36,8 +36,9 @@
#include "nsFormData.h"
#include "nsIVariant.h"
#include "nsIDOMFileInternal.h"
#include "nsIInputStream.h"
#include "nsIDOMFile.h"
#include "nsIFile.h"
#include "nsContentUtils.h"
nsFormData::nsFormData()
@ -83,7 +84,7 @@ nsFormData::AddNameValuePair(const nsAString& aName,
nsresult
nsFormData::AddNameFilePair(const nsAString& aName,
nsIDOMFile* aFile)
nsIFile* aFile)
{
FormDataTuple* data = mFormData.AppendElement();
data->name = aName;
@ -112,9 +113,13 @@ nsFormData::Append(const nsAString& aName, nsIVariant* aValue)
nsMemory::Free(iid);
nsCOMPtr<nsIDOMFile> domFile = do_QueryInterface(supports);
nsCOMPtr<nsIDOMFileInternal> domFile = do_QueryInterface(supports);
if (domFile) {
return AddNameFilePair(aName, domFile);
nsCOMPtr<nsIFile> file;
rv = domFile->GetInternalFile(getter_AddRefs(file));
NS_ENSURE_SUCCESS(rv, rv);
return AddNameFilePair(aName, file);
}
}

View File

@ -42,8 +42,6 @@
#include "nsFormSubmission.h"
#include "nsTArray.h"
class nsIDOMFile;
class nsFormData : public nsIDOMFormData,
public nsIXHRSendable,
public nsFormSubmission
@ -61,14 +59,16 @@ public:
virtual nsresult AddNameValuePair(const nsAString& aName,
const nsAString& aValue);
virtual nsresult AddNameFilePair(const nsAString& aName,
nsIDOMFile* aFile);
nsIFile* aFile);
private:
struct FormDataTuple
{
nsString name;
nsString stringValue;
nsCOMPtr<nsIDOMFile> fileValue;
nsCOMPtr<nsIFile> fileValue;
PRBool valueIsFile;
};

View File

@ -63,6 +63,7 @@
#include "nsIScriptGlobalObject.h"
#include "nsIDOMClassInfo.h"
#include "nsIDOMElement.h"
#include "nsIDOMFileInternal.h"
#include "nsIDOMWindow.h"
#include "nsIMIMEService.h"
#include "nsCExternalHandlerService.h"
@ -2327,6 +2328,29 @@ GetRequestBody(nsIVariant* aBody, nsIInputStream** aResult,
return NS_OK;
}
// nsIDOMFile?
nsCOMPtr<nsIDOMFileInternal> file = do_QueryInterface(supports);
if (file) {
aCharset.Truncate();
nsCOMPtr<nsIFile> internalFile;
rv = file->GetInternalFile(getter_AddRefs(internalFile));
NS_ENSURE_SUCCESS(rv, rv);
// Get the mimetype
nsCOMPtr<nsIMIMEService> mimeService =
do_GetService(NS_MIMESERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = mimeService->GetTypeFromFile(internalFile, aContentType);
if (NS_FAILED(rv)) {
aContentType.Truncate();
}
// Feed local file input stream into our upload channel
return NS_NewLocalFileInputStream(aResult, internalFile);
}
// nsIXHRSendable?
nsCOMPtr<nsIXHRSendable> sendable = do_QueryInterface(supports);
if (sendable) {

View File

@ -22,8 +22,6 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=345339
/** Test for Bug 345339 **/
SimpleTest.waitForExplicitFinish();
var filePath;
function afterLoad() {
var iframeDoc = $("testframe").contentDocument;
@ -33,15 +31,8 @@ function afterLoad() {
iframeDoc.getElementById("password").value = "123456";
iframeDoc.getElementById("hidden").value = "gecko";
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("TmpD", Components.interfaces.nsILocalFile);
file.append("345339_test.file");
file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0666);
filePath = file.path;
iframeDoc.getElementById("file").value = filePath;
netscape.security.PrivilegeManager.enablePrivilege("UniversalFileRead");
iframeDoc.getElementById("file").value = "dummyfile.txt";
/* Reload the page */
$("testframe").setAttribute("onload", "afterReload()");
@ -64,7 +55,7 @@ function afterReload() {
is(iframeDoc.getElementById("hidden").value, "gecko",
"hidden field value preserved");
netscape.security.PrivilegeManager.enablePrivilege("UniversalFileRead");
is(iframeDoc.getElementById("file").value, filePath,
is(iframeDoc.getElementById("file").value, "dummyfile.txt",
"file field value preserved");
SimpleTest.finish();

View File

@ -60,6 +60,7 @@ EXPORTS = \
nsIRadioVisitor.h \
nsIRadioGroupContainer.h \
nsITextControlElement.h \
nsIFileControlElement.h \
nsFormSubmission.h \
nsIFrameSetElement.h \
nsHTMLAudioElement.h \

View File

@ -52,7 +52,6 @@ class nsIDocShell;
class nsIRequest;
class nsISaveAsCharset;
class nsIMultiplexInputStream;
class nsIDOMFile;
/**
* Class for form submissions; encompasses the function to call to submit as
@ -82,7 +81,7 @@ public:
* @param aFile the file to submit
*/
virtual nsresult AddNameFilePair(const nsAString& aName,
nsIDOMFile* aFile) = 0;
nsIFile* aFile) = 0;
/**
* Reports whether the instance supports AddIsindex().
@ -188,7 +187,7 @@ public:
virtual nsresult AddNameValuePair(const nsAString& aName,
const nsAString& aValue);
virtual nsresult AddNameFilePair(const nsAString& aName,
nsIDOMFile* aFile);
nsIFile* aFile);
virtual nsresult GetEncodedSubmission(nsIURI* aURI,
nsIInputStream** aPostDataStream);

View File

@ -0,0 +1,86 @@
/* -*- 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 mozilla.org code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation
* Portions created by the Initial Developer are Copyright (C) 2006
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Jonas Sicking <jonas@sicking.cc> (Original author)
*
* 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 ***** */
#ifndef nsIFileControlElement_h___
#define nsIFileControlElement_h___
#include "nsISupports.h"
#include "nsTArray.h"
#include "nsString.h"
#include "nsCOMArray.h"
class nsIFile;
// IID for the nsIFileControl interface
#define NS_IFILECONTROLELEMENT_IID \
{ 0x1f6a32fd, 0x9cda, 0x43e9, \
{ 0x90, 0xef, 0x18, 0x0a, 0xd5, 0xe6, 0xcd, 0xa9 } }
/**
* This interface is used for the file control frame to store its value away
* into the content.
*/
class nsIFileControlElement : public nsISupports {
public:
NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFILECONTROLELEMENT_IID)
/**
* Gets a readable string representing the list of files currently
* selected by this control. This value might not be a valid file name
* and should not be used for anything but displaying the filename to the
* user.
*/
virtual void GetDisplayFileName(nsAString& aFileName) = 0;
/**
* Sets the list of filenames currently selected by this control.
*/
virtual void SetFileNames(const nsTArray<nsString>& aFileNames) = 0;
/**
* Gets a list of nsIFile objects for the files currently selected by
* this control.
*/
virtual void GetFileArray(nsCOMArray<nsIFile>& aFiles) = 0;
};
NS_DEFINE_STATIC_IID_ACCESSOR(nsIFileControlElement,
NS_IFILECONTROLELEMENT_IID)
#endif // nsIFileControlElement_h___

View File

@ -49,8 +49,9 @@
#include "nsDOMError.h"
#include "nsGenericHTMLElement.h"
#include "nsISaveAsCharset.h"
// JBK added for submit move from content frame
#include "nsIFile.h"
#include "nsIDOMFile.h"
#include "nsDirectoryServiceDefs.h"
#include "nsStringStream.h"
#include "nsIURI.h"
@ -109,7 +110,7 @@ public:
virtual nsresult AddNameValuePair(const nsAString& aName,
const nsAString& aValue);
virtual nsresult AddNameFilePair(const nsAString& aName,
nsIDOMFile* aFile);
nsIFile* aFile);
virtual nsresult GetEncodedSubmission(nsIURI* aURI,
nsIInputStream** aPostDataStream);
@ -195,7 +196,7 @@ nsFSURLEncoded::AddIsindex(const nsAString& aValue)
nsresult
nsFSURLEncoded::AddNameFilePair(const nsAString& aName,
nsIDOMFile* aFile)
nsIFile* aFile)
{
if (!mWarnedFileControl) {
SendJSWarning(mDocument, "ForgotFileEnctypeWarning", nsnull, 0);
@ -204,7 +205,7 @@ nsFSURLEncoded::AddNameFilePair(const nsAString& aName,
nsAutoString filename;
if (aFile) {
aFile->GetName(filename);
aFile->GetLeafName(filename);
}
return AddNameValuePair(aName, filename);
@ -468,7 +469,7 @@ nsFSMultipartFormData::AddNameValuePair(const nsAString& aName,
nsresult
nsFSMultipartFormData::AddNameFilePair(const nsAString& aName,
nsIDOMFile* aFile)
nsIFile* aFile)
{
// Encode the control name
nsCAutoString nameStr;
@ -476,13 +477,12 @@ nsFSMultipartFormData::AddNameFilePair(const nsAString& aName,
NS_ENSURE_SUCCESS(rv, rv);
nsCString filenameStr;
nsAutoString contentType;
nsCAutoString contentType;
nsCOMPtr<nsIInputStream> fileStream;
if (aFile) {
// Get and encode the filename
nsAutoString filename;
rv = aFile->GetName(filename);
NS_ENSURE_SUCCESS(rv, rv);
aFile->GetLeafName(filename);
nsCAutoString encodedFileName;
rv = EncodeVal(filename, encodedFileName);
NS_ENSURE_SUCCESS(rv, rv);
@ -493,14 +493,20 @@ nsFSMultipartFormData::AddNameFilePair(const nsAString& aName,
nsLinebreakConverter::eLinebreakNet));
// Get content type
rv = aFile->GetType(contentType);
if (NS_FAILED(rv) || contentType.IsEmpty()) {
nsCOMPtr<nsIMIMEService> MIMEService =
do_GetService(NS_MIMESERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = MIMEService->GetTypeFromFile(aFile, contentType);
if (NS_FAILED(rv)) {
contentType.AssignLiteral("application/octet-stream");
}
// Get input stream
rv = aFile->GetInternalStream(getter_AddRefs(fileStream));
NS_ENSURE_SUCCESS(rv, rv);
rv = NS_NewLocalFileInputStream(getter_AddRefs(fileStream),
aFile, -1, -1,
nsIFileInputStream::CLOSE_ON_EOF |
nsIFileInputStream::REOPEN_ON_REWIND);
if (fileStream) {
// Create buffered stream (for efficiency)
nsCOMPtr<nsIInputStream> bufferedStream;
@ -528,9 +534,8 @@ nsFSMultipartFormData::AddNameFilePair(const nsAString& aName,
NS_LITERAL_CSTRING("Content-Disposition: form-data; name=\"")
+ nameStr + NS_LITERAL_CSTRING("\"; filename=\"")
+ filenameStr + NS_LITERAL_CSTRING("\"" CRLF)
+ NS_LITERAL_CSTRING("Content-Type: ");
AppendUTF16toUTF8(contentType, mPostDataChunk);
mPostDataChunk += NS_LITERAL_CSTRING(CRLF CRLF);
+ NS_LITERAL_CSTRING("Content-Type: ") + contentType
+ NS_LITERAL_CSTRING(CRLF CRLF);
// Add the file to the stream
if (fileStream) {
@ -600,7 +605,7 @@ public:
virtual nsresult AddNameValuePair(const nsAString& aName,
const nsAString& aValue);
virtual nsresult AddNameFilePair(const nsAString& aName,
nsIDOMFile* aFile);
nsIFile* aFile);
virtual nsresult GetEncodedSubmission(nsIURI* aURI,
nsIInputStream** aPostDataStream);
@ -623,11 +628,11 @@ nsFSTextPlain::AddNameValuePair(const nsAString& aName,
nsresult
nsFSTextPlain::AddNameFilePair(const nsAString& aName,
nsIDOMFile* aFile)
nsIFile* aFile)
{
nsAutoString filename;
if (aFile) {
aFile->GetName(filename);
aFile->GetLeafName(filename);
}
AddNameValuePair(aName, filename);

View File

@ -1315,18 +1315,6 @@ NS_NewHTML##_elementName##Element(already_AddRefed<nsINodeInfo> aNodeInfo, \
NS_INTERFACE_TABLE_ENTRY(_class, _i6) \
NS_OFFSET_AND_INTERFACE_TABLE_END
#define NS_HTML_CONTENT_INTERFACE_TABLE7(_class, _i1, _i2, _i3, _i4, _i5, \
_i6, _i7) \
NS_HTML_CONTENT_INTERFACE_TABLE_BEGIN(_class) \
NS_INTERFACE_TABLE_ENTRY(_class, _i1) \
NS_INTERFACE_TABLE_ENTRY(_class, _i2) \
NS_INTERFACE_TABLE_ENTRY(_class, _i3) \
NS_INTERFACE_TABLE_ENTRY(_class, _i4) \
NS_INTERFACE_TABLE_ENTRY(_class, _i5) \
NS_INTERFACE_TABLE_ENTRY(_class, _i6) \
NS_INTERFACE_TABLE_ENTRY(_class, _i7) \
NS_OFFSET_AND_INTERFACE_TABLE_END
#define NS_HTML_CONTENT_INTERFACE_TABLE8(_class, _i1, _i2, _i3, _i4, _i5, \
_i6, _i7, _i8) \
NS_HTML_CONTENT_INTERFACE_TABLE_BEGIN(_class) \

View File

@ -38,6 +38,7 @@
#include "nsIDOMHTMLInputElement.h"
#include "nsITextControlElement.h"
#include "nsIFileControlElement.h"
#include "nsIDOMNSEditableElement.h"
#include "nsIRadioVisitor.h"
#include "nsIPhonetic.h"
@ -92,8 +93,8 @@
#include "nsRuleData.h"
// input type=radio
#include "nsIRadioGroupContainer.h"
// input type=radio
#include "nsIRadioGroupContainer.h"
// input type=file
#include "nsIFile.h"
@ -206,13 +207,12 @@ class nsHTMLInputElementState : public nsISupports
mValue = aValue;
}
const nsCOMArray<nsIDOMFile>& GetFiles() {
return mFiles;
const nsTArray<nsString>& GetFilenames() {
return mFilenames;
}
void SetFiles(const nsCOMArray<nsIDOMFile> &aFiles) {
mFiles.Clear();
mFiles.AppendObjects(aFiles);
void SetFilenames(const nsTArray<nsString> &aFilenames) {
mFilenames = aFilenames;
}
nsHTMLInputElementState()
@ -223,7 +223,7 @@ class nsHTMLInputElementState : public nsISupports
protected:
nsString mValue;
nsCOMArray<nsIDOMFile> mFiles;
nsTArray<nsString> mFilenames;
PRPackedBool mChecked;
PRPackedBool mCheckedSet;
};
@ -268,21 +268,21 @@ AsyncClickHandler::Run()
// Check if page is allowed to open the popup
if (mPopupControlState != openAllowed) {
nsCOMPtr<nsIPopupWindowManager> pm =
do_GetService(NS_POPUPWINDOWMANAGER_CONTRACTID);
if (!pm) {
nsCOMPtr<nsIPopupWindowManager> pm =
do_GetService(NS_POPUPWINDOWMANAGER_CONTRACTID);
if (!pm) {
return NS_OK;
}
PRUint32 permission;
pm->TestPermission(doc->GetDocumentURI(), &permission);
if (permission == nsIPopupWindowManager::DENY_POPUP) {
nsCOMPtr<nsIDOMDocument> domDoc = do_QueryInterface(doc);
nsGlobalWindow::FirePopupBlockedEvent(domDoc, win, nsnull, EmptyString(), EmptyString());
return NS_OK;
}
PRUint32 permission;
pm->TestPermission(doc->GetDocumentURI(), &permission);
if (permission == nsIPopupWindowManager::DENY_POPUP) {
nsCOMPtr<nsIDOMDocument> domDoc = do_QueryInterface(doc);
nsGlobalWindow::FirePopupBlockedEvent(domDoc, win, nsnull, EmptyString(), EmptyString());
return NS_OK;
}
}
}
// Get Loc title
nsXPIDLString title;
@ -325,24 +325,17 @@ AsyncClickHandler::Run()
// Set default directry and filename
nsAutoString defaultName;
const nsCOMArray<nsIDOMFile>& oldFiles = mInput->GetFiles();
nsCOMArray<nsIFile> oldFiles;
mInput->GetFileArray(oldFiles);
if (oldFiles.Count()) {
nsString path;
oldFiles[0]->GetMozFullPathInternal(path);
nsCOMPtr<nsILocalFile> localFile;
rv = NS_NewLocalFile(path, PR_FALSE, getter_AddRefs(localFile));
if (NS_SUCCEEDED(rv)) {
nsCOMPtr<nsIFile> parentFile;
rv = localFile->GetParent(getter_AddRefs(parentFile));
if (NS_SUCCEEDED(rv)) {
nsCOMPtr<nsILocalFile> parentLocalFile = do_QueryInterface(parentFile, &rv);
if (parentLocalFile) {
filePicker->SetDisplayDirectory(parentLocalFile);
}
// set directory
nsCOMPtr<nsIFile> parentFile;
oldFiles[0]->GetParent(getter_AddRefs(parentFile));
if (parentFile) {
nsCOMPtr<nsILocalFile> parentLocalFile = do_QueryInterface(parentFile, &rv);
if (parentLocalFile) {
filePicker->SetDisplayDirectory(parentLocalFile);
}
}
@ -351,7 +344,7 @@ AsyncClickHandler::Run()
// one file was selected before.
if (oldFiles.Count() == 1) {
nsAutoString leafName;
oldFiles[0]->GetName(leafName);
oldFiles[0]->GetLeafName(leafName);
if (!leafName.IsEmpty()) {
filePicker->SetDefaultString(leafName);
}
@ -382,7 +375,7 @@ AsyncClickHandler::Run()
return NS_OK;
// Collect new selected filenames
nsCOMArray<nsIDOMFile> newFiles;
nsTArray<nsString> newFileNames;
if (multi) {
nsCOMPtr<nsISimpleEnumerator> iter;
rv = filePicker->GetFiles(getter_AddRefs(iter));
@ -396,9 +389,7 @@ AsyncClickHandler::Run()
nsString unicodePath;
rv = localFile->GetPath(unicodePath);
if (!unicodePath.IsEmpty()) {
nsCOMPtr<nsIDOMFile> domFile =
do_QueryObject(new nsDOMFile(localFile, doc));
newFiles.AppendObject(domFile);
newFileNames.AppendElement(unicodePath);
}
if (!prefSaved) {
// Store the last used directory using the content pref service
@ -417,9 +408,7 @@ AsyncClickHandler::Run()
nsString unicodePath;
rv = localFile->GetPath(unicodePath);
if (!unicodePath.IsEmpty()) {
nsCOMPtr<nsIDOMFile> domFile=
do_QueryObject(new nsDOMFile(localFile, doc));
newFiles.AppendObject(domFile);
newFileNames.AppendElement(unicodePath);
}
// Store the last used directory using the content pref service
rv = nsHTMLInputElement::gUploadLastDir->StoreLastUsedDirectory(doc->GetDocumentURI(),
@ -429,7 +418,7 @@ AsyncClickHandler::Run()
}
// Set new selected files
if (newFiles.Count()) {
if (!newFileNames.IsEmpty()) {
// Tell mTextFrame that this update of the value is a user initiated
// change. Otherwise it'll think that the value is being set by a script
// and not fire onchange when it should.
@ -439,7 +428,7 @@ AsyncClickHandler::Run()
textFrame->SetFireChangeEventState(PR_TRUE);
}
mInput->SetFiles(newFiles);
mInput->SetFileNames(newFileNames);
if (textFrame) {
textFrame->SetFireChangeEventState(oldState);
// May need to fire an onchange here
@ -680,9 +669,10 @@ DOMCI_NODE_DATA(HTMLInputElement, nsHTMLInputElement)
// QueryInterface implementation for nsHTMLInputElement
NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(nsHTMLInputElement)
NS_HTML_CONTENT_INTERFACE_TABLE8(nsHTMLInputElement,
NS_HTML_CONTENT_INTERFACE_TABLE9(nsHTMLInputElement,
nsIDOMHTMLInputElement,
nsITextControlElement,
nsIFileControlElement,
nsIPhonetic,
imgIDecoderObserver,
nsIImageLoadingContent,
@ -731,14 +721,7 @@ nsHTMLInputElement::Clone(nsINodeInfo *aNodeInfo, nsINode **aResult) const
}
break;
case NS_FORM_INPUT_FILE:
if (it->GetOwnerDoc()->IsStaticDocument()) {
// We're going to be used in print preview. Since the doc is static
// we can just grab the pretty string and use it as wallpaper
GetDisplayFileName(it->mStaticDocFileList);
} else {
it->mFiles.Clear();
it->mFiles.AppendObjects(mFiles);
}
it->mFileNames = mFileNames;
break;
case NS_FORM_INPUT_RADIO:
case NS_FORM_INPUT_CHECKBOX:
@ -1042,15 +1025,17 @@ nsHTMLInputElement::GetValue(nsAString& aValue)
if (mType == NS_FORM_INPUT_FILE) {
if (nsContentUtils::IsCallerTrustedForCapability("UniversalFileRead")) {
if (mFiles.Count()) {
return mFiles[0]->GetMozFullPath(aValue);
if (!mFileNames.IsEmpty()) {
aValue = mFileNames[0];
}
else {
aValue.Truncate();
}
} else {
// Just return the leaf name
if (mFiles.Count() == 0 || NS_FAILED(mFiles[0]->GetName(aValue))) {
nsCOMArray<nsIFile> files;
GetFileArray(files);
if (files.Count() == 0 || NS_FAILED(files[0]->GetLeafName(aValue))) {
aValue.Truncate();
}
}
@ -1080,11 +1065,10 @@ nsHTMLInputElement::SetValue(const nsAString& aValue)
// UniversalFileRead privilege
return NS_ERROR_DOM_SECURITY_ERR;
}
const PRUnichar *name = PromiseFlatString(aValue).get();
return MozSetFileNameArray(&name, 1);
SetSingleFileName(aValue);
}
else {
ClearFiles();
ClearFileNames();
}
}
else {
@ -1103,14 +1087,12 @@ nsHTMLInputElement::MozGetFileNameArray(PRUint32 *aLength, PRUnichar ***aFileNam
return NS_ERROR_DOM_SECURITY_ERR;
}
*aLength = mFiles.Count();
*aLength = mFileNames.Length();
PRUnichar **ret =
static_cast<PRUnichar **>(NS_Alloc(mFiles.Count() * sizeof(PRUnichar*)));
static_cast<PRUnichar **>(NS_Alloc(mFileNames.Length() * sizeof(PRUnichar*)));
for (PRUint32 i = 0; i < mFiles.Count(); i++) {
nsString str;
mFiles[i]->GetMozFullPath(str);
ret[i] = NS_strdup(str.get());
for (PRUint32 i = 0; i < mFileNames.Length(); i++) {
ret[i] = NS_strdup(mFileNames[i].get());
}
*aFileNames = ret;
@ -1127,36 +1109,12 @@ nsHTMLInputElement::MozSetFileNameArray(const PRUnichar **aFileNames, PRUint32 a
return NS_ERROR_DOM_SECURITY_ERR;
}
nsCOMArray<nsIDOMFile> files;
nsTArray<nsString> fileNames(aLength);
for (PRUint32 i = 0; i < aLength; ++i) {
nsCOMPtr<nsIFile> file;
if (StringBeginsWith(nsDependentString(aFileNames[i]),
NS_LITERAL_STRING("file:"),
nsASCIICaseInsensitiveStringComparator())) {
// Converts the URL string into the corresponding nsIFile if possible
// A local file will be created if the URL string begins with file://
NS_GetFileFromURLSpec(NS_ConvertUTF16toUTF8(aFileNames[i]),
getter_AddRefs(file));
}
if (!file) {
// this is no "file://", try as local file
nsCOMPtr<nsILocalFile> localFile;
NS_NewLocalFile(nsDependentString(aFileNames[i]),
PR_FALSE, getter_AddRefs(localFile));
file = do_QueryInterface(localFile);
}
if (file) {
nsCOMPtr<nsIDOMFile> domFile = new nsDOMFile(file, GetOwnerDoc());
files.AppendObject(domFile);
} else {
continue; // Not much we can do if the file doesn't exist
}
fileNames.AppendElement(aFileNames[i]);
}
SetFiles(files);
SetFileNames(fileNames);
return NS_OK;
}
@ -1178,8 +1136,7 @@ nsHTMLInputElement::SetUserInput(const nsAString& aValue)
if (mType == NS_FORM_INPUT_FILE)
{
const PRUnichar* name = PromiseFlatString(aValue).get();
return MozSetFileNameArray(&name, 1);
SetSingleFileName(aValue);
} else {
SetValueInternal(aValue, PR_TRUE, PR_TRUE);
}
@ -1284,32 +1241,28 @@ nsHTMLInputElement::SetPlaceholderClass(PRBool aVisible, PRBool aNotify)
}
void
nsHTMLInputElement::GetDisplayFileName(nsAString& aValue) const
nsHTMLInputElement::GetDisplayFileName(nsAString& aValue)
{
if (GetOwnerDoc()->IsStaticDocument()) {
aValue = mStaticDocFileList;
return;
}
aValue.Truncate();
for (PRUint32 i = 0; i < (PRUint32)mFiles.Count(); ++i) {
nsString str;
mFiles[i]->GetMozFullPath(str);
for (PRUint32 i = 0; i < mFileNames.Length(); ++i) {
if (i == 0) {
aValue.Append(str);
aValue.Append(mFileNames[i]);
}
else {
aValue.Append(NS_LITERAL_STRING(", ") + str);
aValue.Append(NS_LITERAL_STRING(", ") + mFileNames[i]);
}
}
}
void
nsHTMLInputElement::SetFiles(const nsCOMArray<nsIDOMFile>& aFiles)
nsHTMLInputElement::SetFileNames(const nsTArray<nsString>& aFileNames)
{
mFiles.Clear();
mFiles.AppendObjects(aFiles);
mFileNames = aFileNames;
#if DEBUG
for (PRUint32 i = 0; i < (PRUint32)aFileNames.Length(); ++i) {
NS_ASSERTION(!aFileNames[i].IsEmpty(), "Empty file name");
}
#endif
// No need to flush here, if there's no frame at this point we
// don't need to force creation of one just to tell it about this
// new value. We just want the display to update as needed.
@ -1326,10 +1279,37 @@ nsHTMLInputElement::SetFiles(const nsCOMArray<nsIDOMFile>& aFiles)
UpdateAllValidityStates(PR_TRUE);
}
const nsCOMArray<nsIDOMFile>&
nsHTMLInputElement::GetFiles()
void
nsHTMLInputElement::GetFileArray(nsCOMArray<nsIFile> &aFiles)
{
return mFiles;
aFiles.Clear();
if (mType != NS_FORM_INPUT_FILE) {
return;
}
for (PRUint32 i = 0; i < mFileNames.Length(); ++i) {
nsCOMPtr<nsIFile> file;
if (StringBeginsWith(mFileNames[i], NS_LITERAL_STRING("file:"),
nsCaseInsensitiveStringComparator())) {
// Converts the URL string into the corresponding nsIFile if possible.
// A local file will be created if the URL string begins with file://.
NS_GetFileFromURLSpec(NS_ConvertUTF16toUTF8(mFileNames[i]),
getter_AddRefs(file));
}
if (!file) {
// this is no "file://", try as local file
nsCOMPtr<nsILocalFile> localFile;
NS_NewLocalFile(mFileNames[i], PR_FALSE, getter_AddRefs(localFile));
// Wish there was a better way to downcast an already_AddRefed
file = dont_AddRef(static_cast<nsIFile*>(localFile.forget().get()));
}
if (file) {
aFiles.AppendObject(file);
}
}
}
nsresult
@ -1338,10 +1318,16 @@ nsHTMLInputElement::UpdateFileList()
if (mFileList) {
mFileList->Clear();
const nsCOMArray<nsIDOMFile>& files = GetFiles();
nsIDocument* doc = GetOwnerDoc();
nsCOMArray<nsIFile> files;
GetFileArray(files);
for (PRUint32 i = 0; i < (PRUint32)files.Count(); ++i) {
if (!mFileList->Append(files[i])) {
return NS_ERROR_FAILURE;
nsRefPtr<nsDOMFile> domFile = new nsDOMFile(files[i], doc);
if (domFile) {
if (!mFileList->Append(domFile)) {
return NS_ERROR_FAILURE;
}
}
}
}
@ -1833,7 +1819,7 @@ nsHTMLInputElement::Click()
nsEventStatus status = nsEventStatus_eIgnore;
SET_BOOLBIT(mBitField, BF_HANDLING_CLICK, PR_TRUE);
if (mType == NS_FORM_INPUT_FILE){
if (mType == NS_FORM_INPUT_FILE){
FireAsyncClickHandler();
}
nsEventDispatcher::Dispatch(static_cast<nsIContent*>(this), context,
@ -2575,7 +2561,7 @@ nsHTMLInputElement::ParseAttribute(PRInt32 aNamespaceID,
// This call isn't strictly needed any more since we'll never
// confuse values and filenames. However it's there for backwards
// compat.
ClearFiles();
ClearFileNames();
}
HandleTypeChange(newType);
@ -2896,7 +2882,7 @@ nsHTMLInputElement::SetDefaultValueAsValue()
case NS_FORM_INPUT_FILE:
{
// Resetting it to blank should not perform security check
ClearFiles();
ClearFileNames();
break;
}
// Value is the same as defaultValue for hidden inputs
@ -3021,7 +3007,8 @@ nsHTMLInputElement::SubmitNamesValues(nsFormSubmission* aFormSubmission)
if (mType == NS_FORM_INPUT_FILE) {
// Submit files
const nsCOMArray<nsIDOMFile>& files = GetFiles();
nsCOMArray<nsIFile> files;
GetFileArray(files);
for (PRUint32 i = 0; i < (PRUint32)files.Count(); ++i) {
aFormSubmission->AddNameFilePair(name, files[i]);
@ -3113,13 +3100,13 @@ nsHTMLInputElement::SaveState()
}
case NS_FORM_INPUT_FILE:
{
if (mFiles.Count()) {
if (!mFileNames.IsEmpty()) {
inputState = new nsHTMLInputElementState();
if (!inputState) {
return NS_ERROR_OUT_OF_MEMORY;
}
inputState->SetFiles(mFiles);
inputState->SetFilenames(mFileNames);
}
break;
}
@ -3266,8 +3253,7 @@ nsHTMLInputElement::RestoreState(nsPresState* aState)
}
case NS_FORM_INPUT_FILE:
{
const nsCOMArray<nsIDOMFile>& files = inputState->GetFiles();
SetFiles(files);
SetFileNames(inputState->GetFilenames());
break;
}
}
@ -3676,7 +3662,8 @@ nsHTMLInputElement::IsValueMissing()
}
case NS_FORM_INPUT_FILE:
{
const nsCOMArray<nsIDOMFile>& files = GetFiles();
nsCOMArray<nsIFile> files;
GetFileArray(files);
return !files.Count();
}
default:

File diff suppressed because it is too large Load Diff

View File

@ -43,13 +43,7 @@
// Change the data for each of the form fields, and reload.
$("text").value = "text value";
$("checkbox").checked = true;
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("TmpD", Components.interfaces.nsILocalFile);
file.append("294258_test.file");
file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0666);
filePath = file.path;
$("file").value = filePath;
$("file").value = "file value";
$("textarea").value = "textarea value";
$("radio1").checked = true;
$("select").selectedIndex = 2;
@ -62,7 +56,7 @@
// Verify that none of the form data has changed.
is($("text").value, "text value", "Text value changed");
is($("checkbox").checked, true, "Checkbox value changed");
is($("file").value, filePath, "File value changed");
is($("file").value, "file value", "File value changed");
is($("textarea").value, "textarea value", "Textarea value changed");
is($("radio1").checked, true, "Radio value changed");
is($("select").selectedIndex, 2, "Select value changed");

View File

@ -18,11 +18,6 @@ var ctx1;
var ctx2;
var counter = 0;
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("TmpD", Components.interfaces.nsILocalFile);
filePath = file.path;
function printpreview() {
gWbp = window.frames[1].QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebBrowserPrint);
@ -160,7 +155,7 @@ var emptyFormElements =
var formElements =
["<input type='text' value='text'>",
"<input type='password' value='password'>",
"<input type='file' value='" + filePath + "'>",
"<input type='file' value='file'>",
"<input type='button' value='button'>",
"<input type='submit' value='submit button'>",
"<input type='reset' value='reset button'>",

View File

@ -65,7 +65,7 @@
#include "nsINodeInfo.h"
#include "nsIDOMEventTarget.h"
#include "nsILocalFile.h"
#include "nsHTMLInputElement.h"
#include "nsIFileControlElement.h"
#include "nsNodeInfoManager.h"
#include "nsContentCreatorFunctions.h"
#include "nsContentUtils.h"
@ -90,7 +90,6 @@
#include "nsHTMLInputElement.h"
#include "nsICapturePicker.h"
#include "nsIFileURL.h"
#include "nsDOMFile.h"
#define SYNC_TEXT 0x1
#define SYNC_BUTTON 0x2
@ -236,21 +235,20 @@ nsFileControlFrame::CreateAnonymousContent(nsTArray<nsIContent*>& aElements)
mTextContent->SetAttr(kNameSpaceID_None, nsGkAtoms::type,
NS_LITERAL_STRING("text"), PR_FALSE);
nsHTMLInputElement* inputElement =
nsHTMLInputElement::FromContent(mContent);
NS_ASSERTION(inputElement, "Why is our content not a <input>?");
// Initialize value when we create the content in case the value was set
// before we got here
nsAutoString value;
inputElement->GetDisplayFileName(value);
nsCOMPtr<nsIDOMHTMLInputElement> textControl = do_QueryInterface(mTextContent);
NS_ASSERTION(textControl, "Why is the <input> we created not a <input>?");
textControl->SetValue(value);
if (textControl) {
nsCOMPtr<nsIFileControlElement> fileControl = do_QueryInterface(mContent);
if (fileControl) {
// Initialize value when we create the content in case the value was set
// before we got here
nsAutoString value;
fileControl->GetDisplayFileName(value);
textControl->SetValue(value);
}
textControl->SetTabIndex(-1);
textControl->SetReadOnly(PR_TRUE);
textControl->SetTabIndex(-1);
textControl->SetReadOnly(PR_TRUE);
}
if (!aElements.AppendElement(mTextContent))
return NS_ERROR_OUT_OF_MEMORY;
@ -400,11 +398,9 @@ nsFileControlFrame::CaptureMouseListener::MouseClick(nsIDOMEvent* aMouseEvent)
// Get parent nsIDOMWindowInternal object.
nsIContent* content = mFrame->GetContent();
if (!content)
return NS_ERROR_FAILURE;
nsHTMLInputElement* inputElement = nsHTMLInputElement::FromContent(content);
if (!inputElement)
nsCOMPtr<nsIDOMHTMLInputElement> inputElem = do_QueryInterface(content);
nsCOMPtr<nsIFileControlElement> fileControl = do_QueryInterface(content);
if (!content || !inputElem || !fileControl)
return NS_ERROR_FAILURE;
nsCOMPtr<nsIDocument> doc = content->GetDocument();
@ -447,13 +443,21 @@ nsFileControlFrame::CaptureMouseListener::MouseClick(nsIDOMEvent* aMouseEvent)
return NS_OK;
}
nsCOMPtr<nsIDOMFile> domFile;
rv = capturePicker->GetFile(getter_AddRefs(domFile));
nsCOMPtr<nsIURI> uri;
rv = capturePicker->GetUri(getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMArray<nsIDOMFile> newFiles;
if (domFile) {
newFiles.AppendObject(domFile);
nsTArray<nsString> newFileNames;
if (uri) {
nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(uri);
if (!fileURL)
return NS_ERROR_UNEXPECTED;
nsCAutoString spec;
rv = uri->GetSpec(spec);
NS_ENSURE_SUCCESS(rv, rv);
newFileNames.AppendElement(NS_ConvertUTF8toUTF16(spec));
} else {
return NS_ERROR_FAILURE;
}
@ -461,13 +465,13 @@ nsFileControlFrame::CaptureMouseListener::MouseClick(nsIDOMEvent* aMouseEvent)
// XXXkhuey we really should have a better UI story than the tired old
// uneditable text box with the file name inside.
// Set new selected files
if (newFiles.Count()) {
if (!newFileNames.IsEmpty()) {
// Tell mTextFrame that this update of the value is a user initiated
// change. Otherwise it'll think that the value is being set by a script
// and not fire onchange when it should.
PRBool oldState = mFrame->mTextFrame->GetFireChangeEventState();
mFrame->mTextFrame->SetFireChangeEventState(PR_TRUE);
inputElement->SetFiles(newFiles);
fileControl->SetFileNames(newFileNames);
mFrame->mTextFrame->SetFireChangeEventState(oldState);
// May need to fire an onchange here
@ -487,9 +491,12 @@ nsFileControlFrame::BrowseMouseListener::MouseClick(nsIDOMEvent* aMouseEvent)
if (!ShouldProcessMouseClick(aMouseEvent))
return NS_OK;
nsHTMLInputElement* input =
nsHTMLInputElement::FromContent(mFrame->GetContent());
return input ? input->FireAsyncClickHandler() : NS_OK;
nsIContent* content = mFrame->GetContent();
if (content->IsHTML() && content->Tag() == nsGkAtoms::input) {
nsHTMLInputElement* input = static_cast<nsHTMLInputElement*>(content);
return input->FireAsyncClickHandler();
}
return NS_OK;
}
nscoord
@ -636,11 +643,10 @@ nsFileControlFrame::GetFormProperty(nsIAtom* aName, nsAString& aValue) const
aValue.Truncate(); // initialize out param
if (nsGkAtoms::value == aName) {
nsHTMLInputElement* inputElement =
nsHTMLInputElement::FromContent(mContent);
if (inputElement) {
inputElement->GetDisplayFileName(aValue);
nsCOMPtr<nsIFileControlElement> fileControl =
do_QueryInterface(mContent);
if (fileControl) {
fileControl->GetDisplayFileName(aValue);
}
}
return NS_OK;

View File

@ -39,7 +39,7 @@
#include "nsISupports.idl"
interface nsIDOMWindow;
interface nsIDOMFile;
interface nsIURI;
[scriptable, uuid(a4e2b2de-5712-4f80-aabb-7de3a747f227)]
interface nsICapturePicker : nsISupports
@ -88,7 +88,7 @@ interface nsICapturePicker : nsISupports
* Get the captured image/video/audio. This may be a data URI, file URI,
* or a moz-filedata reference URI.
*/
readonly attribute nsIDOMFile file;
readonly attribute nsIURI uri;
// The MIME type of the captured content. Cannot be set after calling show()
attribute AString type;