Bug 339144 - Expose a custom factory type for simple nsIUnicharInputStream instances, r=darin

This commit is contained in:
benjamin%smedbergs.us 2006-07-21 16:28:51 +00:00
Родитель 595ec7c6b8
Коммит f4fa701ec6
15 изменённых файлов: 237 добавлений и 106 удалений

Просмотреть файл

@ -168,6 +168,7 @@ INCLUDES += \
-I$(srcdir)/../../../layout/style \
-I$(srcdir)/../../../dom/src/base \
-I$(srcdir)/../../xml/document/src \
-I$(topsrcdir)/xpcom/io \
$(NULL)
DEFINES += -D_IMPL_NS_LAYOUT

Просмотреть файл

@ -55,10 +55,13 @@
#include "nsIDOMStyleSheet.h"
#include "nsIDOMText.h"
#include "nsIUnicharInputStream.h"
#include "nsISimpleUnicharStreamFactory.h"
#include "nsNetUtil.h"
#include "nsUnicharUtils.h"
#include "nsVoidArray.h"
#include "nsCRT.h"
#include "nsXPCOMCIDInternal.h"
#include "nsUnicharInputStream.h"
nsStyleLinkElement::nsStyleLinkElement()
: mDontLoadStyle(PR_FALSE)
@ -297,11 +300,9 @@ nsStyleLinkElement::UpdateStyleSheet(nsIDocument *aOldDocument,
content.Append(tcString);
}
// Use of the stream will be done before parsing returns. So it will go
// out of scope before |content| does.
nsCOMPtr<nsIUnicharInputStream> uin;
rv = NS_NewStringUnicharInputStream(getter_AddRefs(uin), &content,
PR_FALSE);
rv = nsSimpleUnicharStreamFactory::GetInstance()->
CreateInstanceFromString(content, getter_AddRefs(uin));
if (NS_FAILED(rv)) {
return rv;
}

Просмотреть файл

@ -86,4 +86,7 @@ EXTRA_DSO_LDOPTS = \
include $(topsrcdir)/config/rules.mk
LOCAL_INCLUDES += \
-I$(topsrcdir)/xpcom/io \
$(NULL)

Просмотреть файл

@ -50,6 +50,7 @@
#include "nsCRT.h"
#include "nsNetUtil.h"
#include "nsStringEnumerator.h"
#include "nsUnicharInputStream.h"
#define MOZ_PERSONAL_DICT_NAME "persdict.dat"
@ -120,8 +121,10 @@ NS_IMETHODIMP mozPersonalDictionary::Load()
nsCOMPtr<nsIInputStream> inStream;
NS_NewLocalFileInputStream(getter_AddRefs(inStream), theFile);
nsCOMPtr<nsIUnicharInputStream> convStream;
res = NS_NewUTF8ConverterStream(getter_AddRefs(convStream), inStream, 0);
res = nsSimpleUnicharStreamFactory::GetInstance()->
CreateInstanceFromUTF8Stream(inStream, getter_AddRefs(convStream));
if(NS_FAILED(res)) return res;
// we're rereading to get rid of the old data -- we shouldn't have any, but...

Просмотреть файл

@ -117,3 +117,5 @@ EXTRA_DSO_LDOPTS += \
$(NULL)
include $(topsrcdir)/config/rules.mk
LOCAL_INCLUDES += -I$(topsrcdir)/xpcom/io

Просмотреть файл

@ -46,6 +46,7 @@
#include "nsParserMsgUtils.h"
#include "nsIURL.h"
#include "nsIUnicharInputStream.h"
#include "nsISimpleUnicharStreamFactory.h"
#include "nsNetUtil.h"
#include "prprf.h"
#include "prmem.h"
@ -54,6 +55,8 @@
#include "nsCRT.h"
#include "nsIConsoleService.h"
#include "nsIScriptError.h"
#include "nsXPCOMCIDInternal.h"
#include "nsUnicharInputStream.h"
#define kExpatSeparatorChar 0xFFFF
@ -705,7 +708,8 @@ nsExpatDriver::HandleExternalEntityRef(const PRUnichar *openEntityNames,
NS_ENSURE_SUCCESS(rv, 1);
nsCOMPtr<nsIUnicharInputStream> uniIn;
rv = NS_NewUTF8ConverterStream(getter_AddRefs(uniIn), in, 4096);
rv = nsSimpleUnicharStreamFactory::GetInstance()->
CreateInstanceFromUTF8Stream(in, getter_AddRefs(uniIn));
NS_ENSURE_SUCCESS(rv, 1);
int result = 1;

Просмотреть файл

@ -50,6 +50,15 @@
{ 0xb9, 0xb8, 0xc8, 0x11, 0x75, 0x95, 0x51, 0x99 } }
#define NS_HASH_PROPERTY_BAG_CONTRACTID "@mozilla.org/hash-property-bag;1"
/**
* Factory for creating nsIUnicharInputStream
* @implements nsIUnicharInputStreamFactory
* @note nsIUnicharInputStream instances cannot be created via
* createInstance. Code must use one of the custom factory methods.
*/
#define NS_SIMPLE_UNICHAR_STREAM_FACTORY_CONTRACTID \
"@mozilla.org/xpcom/simple-unichar-stream-factory;1"
/**
* The global thread manager service. This component is a singleton.
* @implements nsIThreadManager

Просмотреть файл

@ -117,6 +117,7 @@ NS_DECL_CLASSINFO(nsStringInputStream)
#include "nsHashPropertyBag.h"
#include "nsUnicharInputStream.h"
#include "nsVariant.h"
#include "nsUUIDGenerator.h"
@ -155,6 +156,7 @@ extern void _FreeAutoLockStatics();
static NS_DEFINE_CID(kComponentManagerCID, NS_COMPONENTMANAGER_CID);
static NS_DEFINE_CID(kMemoryCID, NS_MEMORY_CID);
static NS_DEFINE_CID(kINIParserFactoryCID, NS_INIPARSERFACTORY_CID);
static NS_DEFINE_CID(kSimpleUnicharStreamFactoryCID, NS_SIMPLE_UNICHAR_STREAM_FACTORY_CID);
NS_GENERIC_FACTORY_CONSTRUCTOR(nsProcess)
@ -627,6 +629,12 @@ NS_InitXPCOM3(nsIServiceManager* *result,
"nsINIParserFactory",
NS_INIPARSERFACTORY_CONTRACTID,
iniParserFactory);
registrar->
RegisterFactory(kSimpleUnicharStreamFactoryCID,
"nsSimpleUnicharStreamFactory",
NS_SIMPLE_UNICHAR_STREAM_FACTORY_CONTRACTID,
nsSimpleUnicharStreamFactory::GetInstance());
}
// Pay the cost at startup time of starting this singleton.

Просмотреть файл

@ -164,5 +164,6 @@ FORCE_USE_PIC = 1
include $(topsrcdir)/config/rules.mk
LOCAL_INCLUDES += -I$(srcdir)/../io
DEFINES += -D_IMPL_NS_COM

Просмотреть файл

@ -40,7 +40,7 @@
#include "nsCRT.h"
#include "nsReadableUtils.h"
#include "nsIInputStream.h"
#include "nsIUnicharInputStream.h"
#include "nsUnicharInputStream.h"
#include "pratom.h"
#include "nsEnumeratorUtils.h"
#include "nsReadableUtils.h"
@ -147,7 +147,8 @@ NS_IMETHODIMP
nsPersistentProperties::Load(nsIInputStream *aIn)
{
PRInt32 c;
nsresult ret = NS_NewUTF8ConverterStream(&mIn, aIn, 0);
nsresult ret = nsSimpleUnicharStreamFactory::GetInstance()->
CreateInstanceFromUTF8Stream(aIn, &mIn);
if (ret != NS_OK) {
NS_WARNING("NS_NewUTF8ConverterStream failed");

Просмотреть файл

@ -137,6 +137,7 @@ XPIDLSRCS = \
nsILocalFileWin.idl \
nsILineInputStream.idl \
nsIUnicharLineInputStream.idl \
nsISimpleUnicharStreamFactory.idl \
nsIMultiplexInputStream.idl \
nsIObjectInputStream.idl \
nsIObjectOutputStream.idl \

Просмотреть файл

@ -0,0 +1,60 @@
/* ***** 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 XPCOM.
*
* The Initial Developer of the Original Code is
* the Mozilla Foundation <http://www.mozilla.org/>.
*
* Portions created by the Initial Developer are Copyright (C) 2006
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Benjamin Smedberg <benjamin@smedbergs.us> (Initial commit)
*
* 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 "nsISupports.idl"
interface nsIInputStream;
interface nsIUnicharInputStream;
/**
* Factory to create objects that implement nsIUnicharInputStream,
* converting from a unicode string or a UTF-8 stream.
*/
[scriptable, uuid(8238cd2e-e8e3-43e8-b556-56e21389c766)]
interface nsISimpleUnicharStreamFactory : nsISupports
{
/**
* Create a unicode input stream from a unicode string.
*/
nsIUnicharInputStream createInstanceFromString(in AString aString);
/**
* Create a unicode stream from an input stream in UTF8.
*/
nsIUnicharInputStream createInstanceFromUTF8Stream(in nsIInputStream aStream);
};

Просмотреть файл

@ -38,6 +38,7 @@
#include "nsISupports.idl"
interface nsIUnicharInputStream;
interface nsIInputStream;
%{C++
/**
@ -125,31 +126,3 @@ interface nsIUnicharInputStream : nsISupports {
*/
void close();
};
%{C++
#include "nsStringFwd.h"
class nsIInputStream;
/**
* Create a nsIUnicharInputStream that wraps up a string. Data is fed
* from the string out until the done. When this object is destroyed
* it destroys the string by calling |delete| on the pointer if
* aTakeOwnership is set. If aTakeOwnership is not set, you must
* ensure that the string outlives the stream!
*/
extern NS_COM nsresult
NS_NewStringUnicharInputStream(nsIUnicharInputStream** aInstancePtrResult,
const nsAString* aString,
PRBool aTakeOwnership);
/**
* Create a new nsUnicharInputStream that provides a converter for the
* byte input stream aStreamToWrap. If no converter can be found then
* nsnull is returned and the error code is set to
* NS_INPUTSTREAM_NO_CONVERTER.
*/
extern NS_COM nsresult
NS_NewUTF8ConverterStream(nsIUnicharInputStream** aInstancePtrResult,
nsIInputStream* aStreamToWrap,
PRInt32 aBufferSize = 0);
%}

Просмотреть файл

@ -35,12 +35,13 @@
*
* ***** END LICENSE BLOCK ***** */
#include "nsIUnicharInputStream.h"
#include "nsUnicharInputStream.h"
#include "nsIInputStream.h"
#include "nsIByteBuffer.h"
#include "nsIUnicharBuffer.h"
#include "nsIServiceManager.h"
#include "nsString.h"
#include "nsAutoPtr.h"
#include "nsCRT.h"
#include "nsUTF8Utils.h"
#include <fcntl.h>
@ -50,41 +51,24 @@
#include <unistd.h>
#endif
#define STRING_BUFFER_SIZE 8192
class StringUnicharInputStream : public nsIUnicharInputStream {
public:
StringUnicharInputStream(const nsAString* aString,
PRBool aTakeOwnership);
StringUnicharInputStream(const nsAString& aString) :
mString(aString), mPos(0), mLen(aString.Length()) { }
NS_DECL_ISUPPORTS
NS_DECL_NSIUNICHARINPUTSTREAM
const nsAString* mString;
nsString mString;
PRUint32 mPos;
PRUint32 mLen;
PRBool mOwnsString;
private:
~StringUnicharInputStream();
~StringUnicharInputStream() { }
};
StringUnicharInputStream::StringUnicharInputStream(const nsAString* aString,
PRBool aTakeOwnership)
: mString(aString),
mPos(0),
mLen(aString->Length()),
mOwnsString(aTakeOwnership)
{
}
StringUnicharInputStream::~StringUnicharInputStream()
{
if (mString && mOwnsString) {
// Some compilers dislike deleting const pointers
nsAString* mutable_string = NS_CONST_CAST(nsAString*, mString);
delete mutable_string;
}
}
NS_IMETHODIMP
StringUnicharInputStream::Read(PRUnichar* aBuf,
PRUint32 aCount,
@ -95,7 +79,7 @@ StringUnicharInputStream::Read(PRUnichar* aBuf,
return NS_OK;
}
nsAString::const_iterator iter;
mString->BeginReading(iter);
mString.BeginReading(iter);
const PRUnichar* us = iter.get();
PRUint32 amount = mLen - mPos;
if (amount > aCount) {
@ -116,10 +100,10 @@ StringUnicharInputStream::ReadSegments(nsWriteUnicharSegmentFun aWriter,
PRUint32 totalBytesWritten = 0;
nsresult rv;
aCount = PR_MIN(mString->Length() - mPos, aCount);
aCount = PR_MIN(mString.Length() - mPos, aCount);
nsAString::const_iterator iter;
mString->BeginReading(iter);
mString.BeginReading(iter);
while (aCount) {
rv = aWriter(this, aClosure, iter.get() + mPos,
@ -152,7 +136,7 @@ StringUnicharInputStream::ReadString(PRUint32 aCount, nsAString& aString,
if (amount > aCount) {
amount = aCount;
}
aString = Substring(*mString, mPos, amount);
aString = Substring(mString, mPos, amount);
mPos += amount;
*aReadCount = amount;
return NS_OK;
@ -161,41 +145,17 @@ StringUnicharInputStream::ReadString(PRUint32 aCount, nsAString& aString,
nsresult StringUnicharInputStream::Close()
{
mPos = mLen;
if (mString && mOwnsString) {
// Some compilers dislike deleting const pointers
nsAString* mutable_string = NS_CONST_CAST(nsAString*, mString);
delete mutable_string;
}
mString = nsnull;
return NS_OK;
}
NS_IMPL_ISUPPORTS1(StringUnicharInputStream, nsIUnicharInputStream)
NS_COM nsresult
NS_NewStringUnicharInputStream(nsIUnicharInputStream** aInstancePtrResult,
const nsAString* aString,
PRBool aTakeOwnership)
{
NS_ENSURE_ARG_POINTER(aString);
NS_PRECONDITION(aInstancePtrResult, "null ptr");
StringUnicharInputStream* it = new StringUnicharInputStream(aString,
aTakeOwnership);
if (!it) {
return NS_ERROR_OUT_OF_MEMORY;
}
NS_ADDREF(*aInstancePtrResult = it);
return NS_OK;
}
//----------------------------------------------------------------------
class UTF8InputStream : public nsIUnicharInputStream {
public:
UTF8InputStream();
nsresult Init(nsIInputStream* aStream, PRUint32 aBufSize);
nsresult Init(nsIInputStream* aStream);
NS_DECL_ISUPPORTS
NS_DECL_NSIUNICHARINPUTSTREAM
@ -225,15 +185,13 @@ UTF8InputStream::UTF8InputStream() :
}
nsresult
UTF8InputStream::Init(nsIInputStream* aStream, PRUint32 aBufferSize)
UTF8InputStream::Init(nsIInputStream* aStream)
{
if (aBufferSize == 0) {
aBufferSize = 8192;
}
nsresult rv = NS_NewByteBuffer(getter_AddRefs(mByteData), nsnull, aBufferSize);
nsresult rv = NS_NewByteBuffer(getter_AddRefs(mByteData), nsnull,
STRING_BUFFER_SIZE);
if (NS_FAILED(rv)) return rv;
rv = NS_NewUnicharBuffer(getter_AddRefs(mUnicharData), nsnull, aBufferSize);
rv = NS_NewUnicharBuffer(getter_AddRefs(mUnicharData), nsnull,
STRING_BUFFER_SIZE);
if (NS_FAILED(rv)) return rv;
mInput = aStream;
@ -442,21 +400,63 @@ UTF8InputStream::CountValidUTF8Bytes(const char* aBuffer, PRUint32 aMaxBytes, PR
aValidUTF16CodeUnits = utf16length;
}
NS_COM nsresult
NS_NewUTF8ConverterStream(nsIUnicharInputStream** aInstancePtrResult,
nsIInputStream* aStreamToWrap,
PRInt32 aBufferSize)
NS_IMPL_QUERY_INTERFACE2(nsSimpleUnicharStreamFactory,
nsIFactory,
nsISimpleUnicharStreamFactory)
NS_IMETHODIMP_(nsrefcnt) nsSimpleUnicharStreamFactory::AddRef() { return 2; }
NS_IMETHODIMP_(nsrefcnt) nsSimpleUnicharStreamFactory::Release() { return 1; }
NS_IMETHODIMP
nsSimpleUnicharStreamFactory::CreateInstance(nsISupports* aOuter, REFNSIID aIID,
void **aResult)
{
// Create converter input stream
UTF8InputStream* it = new UTF8InputStream();
if (nsnull == it) {
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsSimpleUnicharStreamFactory::LockFactory(PRBool aLock)
{
return NS_OK;
}
NS_IMETHODIMP
nsSimpleUnicharStreamFactory::CreateInstanceFromString(const nsAString& aString,
nsIUnicharInputStream* *aResult)
{
StringUnicharInputStream* it = new StringUnicharInputStream(aString);
if (!it) {
return NS_ERROR_OUT_OF_MEMORY;
}
nsresult rv = it->Init(aStreamToWrap, aBufferSize);
NS_ADDREF(*aResult = it);
return NS_OK;
}
NS_IMETHODIMP
nsSimpleUnicharStreamFactory::CreateInstanceFromUTF8Stream(nsIInputStream* aStreamToWrap,
nsIUnicharInputStream* *aResult)
{
*aResult = nsnull;
// Create converter input stream
nsRefPtr<UTF8InputStream> it = new UTF8InputStream();
if (!it)
return NS_ERROR_OUT_OF_MEMORY;
nsresult rv = it->Init(aStreamToWrap);
if (NS_FAILED(rv))
return rv;
return it->QueryInterface(NS_GET_IID(nsIUnicharInputStream),
(void **) aInstancePtrResult);
NS_ADDREF(*aResult = it);
return NS_OK;
}
nsSimpleUnicharStreamFactory*
nsSimpleUnicharStreamFactory::GetInstance()
{
return NS_CONST_CAST(nsSimpleUnicharStreamFactory*, &kInstance);
}
const nsSimpleUnicharStreamFactory
nsSimpleUnicharStreamFactory::kInstance;

64
xpcom/io/nsUnicharInputStream.h Executable file
Просмотреть файл

@ -0,0 +1,64 @@
/* ***** 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 XPCOM.
*
* The Initial Developer of the Original Code is
* the Mozilla Foundation <http://www.mozilla.org/>.
*
* Portions created by the Initial Developer are Copyright (C) 2006
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Benjamin Smedberg <benjamin@smedbergs.us> (Initial 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 nsUnicharInputStream_h__
#define nsUnicharInputStream_h__
#include "nsISimpleUnicharStreamFactory.h"
#include "nsIUnicharInputStream.h"
#include "nsIFactory.h"
// {428DCA6F-1A0F-4cda-B516-0D5244745A6A}
#define NS_SIMPLE_UNICHAR_STREAM_FACTORY_CID \
{ 0x428dca6f, 0x1a0f, 0x4cda, { 0xb5, 0x16, 0xd, 0x52, 0x44, 0x74, 0x5a, 0x6a } }
class nsSimpleUnicharStreamFactory :
public nsIFactory, nsISimpleUnicharStreamFactory
{
public:
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_NSIFACTORY
NS_DECL_NSISIMPLEUNICHARSTREAMFACTORY
static NS_COM nsSimpleUnicharStreamFactory* GetInstance();
private:
static const nsSimpleUnicharStreamFactory kInstance;
};
#endif // nsUnicharInputStream_h__