зеркало из https://github.com/mozilla/gecko-dev.git
Eliminating netwerk/util
This commit is contained in:
Родитель
105bf31e01
Коммит
501fc9ffdf
|
@ -0,0 +1,57 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
|
||||
#include "nsISupports.idl"
|
||||
|
||||
interface nsIURI;
|
||||
interface nsILoadGroup;
|
||||
interface nsIStreamObserver;
|
||||
interface nsIUnicharStreamLoader;
|
||||
|
||||
[scriptable, uuid(c75e57d0-8e5a-11d3-93ad-00104ba0fd40)]
|
||||
interface nsIUnicharStreamLoaderObserver : nsISupports
|
||||
{
|
||||
void onUnicharStreamComplete(in nsIUnicharStreamLoader loader,
|
||||
in nsresult status,
|
||||
in wstring result);
|
||||
};
|
||||
|
||||
[scriptable, uuid(31d37360-8e5a-11d3-93ad-00104ba0fd40)]
|
||||
interface nsIUnicharStreamLoader : nsISupports
|
||||
{
|
||||
void init(in nsIURI uri,
|
||||
in nsILoadGroup loadGroup,
|
||||
in nsIUnicharStreamLoaderObserver completionObserver);
|
||||
|
||||
/**
|
||||
* Gets the number of bytes read so far.
|
||||
*/
|
||||
readonly attribute unsigned long numCharsRead;
|
||||
};
|
||||
|
||||
%{C++
|
||||
|
||||
#define NS_UNICHARSTREAMLOADER_CID \
|
||||
{ /* 32fd6cf0-8e5a-11d3-93ad-00104ba0fd40 */ \
|
||||
0x32fd6cf0, \
|
||||
0x8e5a, \
|
||||
0x11d3, \
|
||||
{0x93, 0xad, 0x00, 0x10, 0x4b, 0xa0, 0xfd, 0x40} \
|
||||
}
|
||||
|
||||
%}
|
|
@ -0,0 +1,178 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
|
||||
#ifndef nsNetUtil_h__
|
||||
#define nsNetUtil_h__
|
||||
|
||||
#include "nsIURI.h"
|
||||
#include "netCore.h"
|
||||
#include "nsIInputStream.h"
|
||||
#include "nsIStreamListener.h"
|
||||
#include "nsILoadGroup.h"
|
||||
#include "nsIEventSinkGetter.h"
|
||||
#include "nsString.h"
|
||||
|
||||
#include "nsIIOService.h"
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsIChannel.h"
|
||||
#include "nsIAllocator.h"
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsIHTTPProtocolHandler.h"
|
||||
#include "nsIUnicharStreamLoader.h"
|
||||
|
||||
inline nsresult
|
||||
NS_NewURI(nsIURI* *result, const char* spec, nsIURI* baseURI = nsnull)
|
||||
{
|
||||
nsresult rv;
|
||||
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
|
||||
NS_WITH_SERVICE(nsIIOService, serv, kIOServiceCID, &rv);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
rv = serv->NewURI(spec, baseURI, result);
|
||||
return rv;
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
NS_NewURI(nsIURI* *result, const nsString& spec, nsIURI* baseURI = nsnull)
|
||||
{
|
||||
// XXX if the string is unicode, GetBuffer() returns null.
|
||||
// XXX we need a strategy to deal w/ unicode specs (if there should
|
||||
// XXX even be such a thing)
|
||||
char* specStr = spec.ToNewCString(); // this forces a single byte char*
|
||||
if (specStr == nsnull)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
nsresult rv = NS_NewURI(result, specStr, baseURI);
|
||||
nsAllocator::Free(specStr);
|
||||
return rv;
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
NS_OpenURI(nsIChannel* *result, nsIURI* uri, nsILoadGroup *aGroup,
|
||||
nsIEventSinkGetter *eventSinkGetter = nsnull)
|
||||
{
|
||||
nsresult rv;
|
||||
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
|
||||
NS_WITH_SERVICE(nsIIOService, serv, kIOServiceCID, &rv);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
nsIChannel* channel;
|
||||
rv = serv->NewChannelFromURI("load", uri, aGroup, eventSinkGetter,
|
||||
nsnull, &channel);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
*result = channel;
|
||||
return rv;
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
NS_OpenURI(nsIInputStream* *result, nsIURI* uri)
|
||||
{
|
||||
nsresult rv;
|
||||
nsIChannel* channel;
|
||||
|
||||
rv = NS_OpenURI(&channel, uri, nsnull);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
nsIInputStream* inStr;
|
||||
rv = channel->OpenInputStream(0, -1, &inStr);
|
||||
NS_RELEASE(channel);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
*result = inStr;
|
||||
return rv;
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
NS_OpenURI(nsIStreamListener* aConsumer, nsISupports* context, nsIURI* uri,
|
||||
nsILoadGroup *aGroup)
|
||||
{
|
||||
nsresult rv;
|
||||
nsIChannel* channel;
|
||||
|
||||
rv = NS_OpenURI(&channel, uri, aGroup);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
rv = channel->AsyncRead(0, -1, context, aConsumer);
|
||||
NS_RELEASE(channel);
|
||||
return rv;
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
NS_MakeAbsoluteURI(const char* spec, nsIURI* baseURI, char* *result)
|
||||
{
|
||||
return baseURI->Resolve(spec, result);
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
NS_MakeAbsoluteURI(const nsString& spec, nsIURI* baseURI, nsString& result)
|
||||
{
|
||||
char* resultStr;
|
||||
char* specStr = spec.ToNewCString();
|
||||
if (!specStr) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
nsresult rv = NS_MakeAbsoluteURI(specStr, baseURI, &resultStr);
|
||||
nsAllocator::Free(specStr);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
result = resultStr;
|
||||
nsAllocator::Free(resultStr);
|
||||
return rv;
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
NS_NewPostDataStream(PRBool isFile, const char *data, PRUint32 encodeFlags,
|
||||
nsIInputStream **result)
|
||||
{
|
||||
nsresult rv;
|
||||
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
|
||||
NS_WITH_SERVICE(nsIIOService, serv, kIOServiceCID, &rv);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
nsCOMPtr<nsIProtocolHandler> handler;
|
||||
rv = serv->GetProtocolHandler("http", getter_AddRefs(handler));
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
nsCOMPtr<nsIHTTPProtocolHandler> http = do_QueryInterface(handler, &rv);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
return http->NewPostDataStream(isFile, data, encodeFlags, result);
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
NS_NewUnicharStreamLoader(nsIUnicharStreamLoader* *result,
|
||||
nsIURI* uri,
|
||||
nsILoadGroup* loadGroup,
|
||||
nsIUnicharStreamLoaderObserver* observer)
|
||||
{
|
||||
nsresult rv;
|
||||
nsCOMPtr<nsIUnicharStreamLoader> loader;
|
||||
static NS_DEFINE_CID(kUnicharStreamLoaderCID, NS_UNICHARSTREAMLOADER_CID);
|
||||
rv = nsComponentManager::CreateInstance(kUnicharStreamLoaderCID,
|
||||
nsnull,
|
||||
NS_GET_IID(nsIUnicharStreamLoader),
|
||||
getter_AddRefs(loader));
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
rv = loader->Init(uri, loadGroup, observer);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
*result = loader;
|
||||
NS_ADDREF(*result);
|
||||
return rv;
|
||||
}
|
||||
|
||||
#endif // nsNetUtil_h__
|
|
@ -0,0 +1,143 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
|
||||
#include "nsUnicharStreamLoader.h"
|
||||
#include "nsIInputStream.h"
|
||||
#include "nsIURL.h"
|
||||
#include "nsNeckoUtil.h"
|
||||
#include "nsIBufferInputStream.h"
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsILoadGroup.h"
|
||||
#include "nsIChannel.h"
|
||||
|
||||
static NS_DEFINE_IID(kIStreamListenerIID, NS_ISTREAMLISTENER_IID);
|
||||
static NS_DEFINE_IID(kIUnicharStreamLoaderIID, NS_IUNICHARSTREAMLOADER_IID);
|
||||
|
||||
nsUnicharStreamLoader::nsUnicharStreamLoader()
|
||||
: mData(nsnull)
|
||||
{
|
||||
NS_INIT_REFCNT();
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsUnicharStreamLoader::Init(nsIURI* aURL, nsILoadGroup* aLoadGroup,
|
||||
nsIUnicharStreamLoaderObserver* observer)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
mObserver = observer;
|
||||
mData = new nsString();
|
||||
/// mLoadGroup = aLoadGroup;
|
||||
|
||||
/// rv = mLoadGroup->AddChannel(channel, nsnull);
|
||||
/// if (NS_FAILED(rv)) return;
|
||||
|
||||
rv = NS_OpenURI(this, nsnull, aURL, aLoadGroup);
|
||||
if (NS_FAILED(rv) && mObserver) {
|
||||
nsresult rv2 = mObserver->OnUnicharStreamComplete(this, rv, mData->GetUnicode());
|
||||
if (NS_FAILED(rv2))
|
||||
rv = rv2; // take the user's error instead
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsUnicharStreamLoader::~nsUnicharStreamLoader()
|
||||
{
|
||||
if (nsnull != mData) {
|
||||
delete mData;
|
||||
}
|
||||
}
|
||||
|
||||
NS_METHOD
|
||||
nsUnicharStreamLoader::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
|
||||
{
|
||||
if (aOuter)
|
||||
return NS_ERROR_NO_AGGREGATION;
|
||||
|
||||
nsUnicharStreamLoader* it = new nsUnicharStreamLoader();
|
||||
if (it == nsnull)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
NS_ADDREF(it);
|
||||
nsresult rv = it->QueryInterface(aIID, aResult);
|
||||
NS_RELEASE(it);
|
||||
return rv;
|
||||
}
|
||||
|
||||
NS_IMPL_ISUPPORTS3(nsUnicharStreamLoader, nsIUnicharStreamLoader,
|
||||
nsIStreamObserver, nsIStreamListener)
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsUnicharStreamLoader::GetNumCharsRead(PRUint32* aNumBytes)
|
||||
{
|
||||
if (nsnull != mData) {
|
||||
*aNumBytes = mData->Length();
|
||||
}
|
||||
else {
|
||||
*aNumBytes = 0;
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsUnicharStreamLoader::OnStartRequest(nsIChannel* channel, nsISupports *ctxt)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsUnicharStreamLoader::OnStopRequest(nsIChannel* channel, nsISupports *ctxt,
|
||||
nsresult status, const PRUnichar *errorMsg)
|
||||
{
|
||||
nsresult rv = mObserver->OnUnicharStreamComplete(this, status, mData->GetUnicode());
|
||||
/// return mLoadGroup->RemoveChannel(channel, ctxt, status, errorMsg);
|
||||
return rv;
|
||||
}
|
||||
|
||||
#define BUF_SIZE 1024
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsUnicharStreamLoader::OnDataAvailable(nsIChannel* channel, nsISupports *ctxt,
|
||||
nsIInputStream *inStr,
|
||||
PRUint32 sourceOffset, PRUint32 count)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
char buffer[BUF_SIZE];
|
||||
PRUint32 len, lenRead;
|
||||
|
||||
inStr->Available(&len);
|
||||
|
||||
while (len > 0) {
|
||||
if (len < BUF_SIZE) {
|
||||
lenRead = len;
|
||||
}
|
||||
else {
|
||||
lenRead = BUF_SIZE;
|
||||
}
|
||||
|
||||
rv = inStr->Read(buffer, lenRead, &lenRead);
|
||||
if (NS_FAILED(rv) || lenRead == 0) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
mData->Append(buffer, lenRead);
|
||||
len -= lenRead;
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
|
||||
#ifndef nsUnicharStreamLoader_h__
|
||||
#define nsUnicharStreamLoader_h__
|
||||
|
||||
#include "nsIUnicharStreamLoader.h"
|
||||
#include "nsIStreamListener.h"
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsString.h"
|
||||
|
||||
class nsUnicharStreamLoader : public nsIUnicharStreamLoader,
|
||||
public nsIStreamListener
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
NS_DECL_NSIUNICHARSTREAMLOADER
|
||||
NS_DECL_NSISTREAMOBSERVER
|
||||
NS_DECL_NSISTREAMLISTENER
|
||||
|
||||
nsUnicharStreamLoader();
|
||||
virtual ~nsUnicharStreamLoader();
|
||||
|
||||
static NS_METHOD
|
||||
Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
|
||||
|
||||
protected:
|
||||
nsCOMPtr<nsIUnicharStreamLoaderObserver> mObserver;
|
||||
nsString* mData;
|
||||
/// nsCOMPtr<nsILoadGroup> mLoadGroup;
|
||||
};
|
||||
|
||||
#endif // nsUnicharStreamLoader_h__
|
Загрузка…
Ссылка в новой задаче