mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-07 20:17:37 +00:00
300 lines
9.3 KiB
Plaintext
300 lines
9.3 KiB
Plaintext
/* ***** 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 Chimera code.
|
|
*
|
|
* The Initial Developer of the Original Code is
|
|
* Netscape Communications Corporation.
|
|
* Portions created by the Initial Developer are Copyright (C) 2002
|
|
* the Initial Developer. All Rights Reserved.
|
|
*
|
|
* Contributor(s):
|
|
* Simon Fraser <sfraser@netscape.com>
|
|
*
|
|
* 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 ***** */
|
|
|
|
#import "NSString+Utils.h"
|
|
|
|
#import "RemoteDataProvider.h"
|
|
|
|
#include "nsISupports.h"
|
|
#include "nsHashtable.h"
|
|
#include "nsNetUtil.h"
|
|
#include "nsICacheSession.h"
|
|
#include "nsICacheService.h"
|
|
#include "nsICacheEntryDescriptor.h"
|
|
|
|
NSString* RemoteDataLoadRequestNotificationName = @"remoteload_notification_name";
|
|
NSString* RemoteDataLoadRequestURIKey = @"remoteload_uri_key";
|
|
NSString* RemoteDataLoadRequestDataKey = @"remoteload_data_key";
|
|
NSString* RemoteDataLoadRequestUserDataKey = @"remoteload_user_data_key";
|
|
NSString* RemoteDataLoadRequestResultKey = @"remoteload_result_key";
|
|
|
|
|
|
// this has to retain the load listener, to ensure that the listener lives long
|
|
// enough to receive notifications. We have to be careful to avoid ref cycles.
|
|
class StreamLoaderContext : public nsISupports
|
|
{
|
|
public:
|
|
StreamLoaderContext(id<RemoteLoadListener> inLoadListener, id inUserData, id inTarget, const nsAString& inURI)
|
|
: mLoadListener(inLoadListener)
|
|
, mTarget(inTarget)
|
|
, mUserData(inUserData)
|
|
, mURI(inURI)
|
|
{
|
|
NS_INIT_ISUPPORTS();
|
|
[mLoadListener retain];
|
|
}
|
|
|
|
virtual ~StreamLoaderContext()
|
|
{
|
|
[mLoadListener release];
|
|
}
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
void LoadComplete(nsresult inLoadStatus, const void* inData, unsigned int inDataLength);
|
|
const nsAString& GetURI() { return mURI; }
|
|
|
|
protected:
|
|
|
|
id<RemoteLoadListener> mLoadListener; // retained
|
|
id mTarget; // not retained
|
|
id mUserData; // not retained
|
|
nsString mURI;
|
|
|
|
};
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(StreamLoaderContext, nsISupports)
|
|
|
|
void StreamLoaderContext::LoadComplete(nsresult inLoadStatus, const void* inData, unsigned int inDataLength)
|
|
{
|
|
if (mLoadListener)
|
|
{
|
|
NSData* loadData = nil;
|
|
if (NS_SUCCEEDED(inLoadStatus))
|
|
loadData = [NSData dataWithBytes:inData length:inDataLength];
|
|
|
|
[mLoadListener doneRemoteLoad:[NSString stringWith_nsAString:mURI] forTarget:mTarget withUserData:mUserData data:loadData status:inLoadStatus];
|
|
}
|
|
}
|
|
|
|
|
|
class RemoteURILoadManager : public nsIStreamLoaderObserver
|
|
{
|
|
public:
|
|
|
|
RemoteURILoadManager();
|
|
virtual ~RemoteURILoadManager();
|
|
|
|
NS_DECL_ISUPPORTS
|
|
NS_DECL_NSISTREAMLOADEROBSERVER
|
|
|
|
nsresult Init();
|
|
nsresult RequestURILoad(const nsAString& inURI, id<RemoteLoadListener> loadListener, id userData, id target, PRBool allowNetworking);
|
|
|
|
protected:
|
|
|
|
nsSupportsHashtable mStreamLoaderHash; // hash of active stream loads, keyed on URI
|
|
nsCOMPtr<nsICacheSession> mCacheSession;
|
|
|
|
};
|
|
|
|
RemoteURILoadManager::RemoteURILoadManager()
|
|
{
|
|
NS_INIT_ISUPPORTS();
|
|
}
|
|
|
|
RemoteURILoadManager::~RemoteURILoadManager()
|
|
{
|
|
}
|
|
|
|
NS_IMPL_ISUPPORTS1(RemoteURILoadManager, nsIStreamLoaderObserver)
|
|
|
|
NS_IMETHODIMP RemoteURILoadManager::OnStreamComplete(nsIStreamLoader *loader, nsISupports *ctxt, nsresult status, PRUint32 resultLength, const char *result)
|
|
{
|
|
StreamLoaderContext* loaderContext = NS_STATIC_CAST(StreamLoaderContext*, ctxt);
|
|
if (loaderContext)
|
|
{
|
|
loaderContext->LoadComplete(status, (const void*)result, resultLength);
|
|
|
|
// remove the stream loader from the hash table
|
|
nsStringKey uriKey(loaderContext->GetURI());
|
|
PRBool removed = mStreamLoaderHash.Remove(&uriKey);
|
|
}
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID);
|
|
|
|
nsresult RemoteURILoadManager::Init()
|
|
{
|
|
nsresult rv;
|
|
nsCOMPtr<nsICacheService> cacheService = do_GetService(kCacheServiceCID, &rv);
|
|
if (NS_FAILED(rv))
|
|
return rv;
|
|
|
|
rv = cacheService->CreateSession("HTTP", nsICache::STORE_ANYWHERE,
|
|
nsICache::STREAM_BASED, getter_AddRefs(mCacheSession));
|
|
|
|
return rv;
|
|
}
|
|
|
|
nsresult RemoteURILoadManager::RequestURILoad(const nsAString& inURI, id<RemoteLoadListener> loadListener,
|
|
id userData, id target, PRBool allowNetworking)
|
|
{
|
|
nsresult rv;
|
|
|
|
#if 0
|
|
// if no networking is allowed, make sure it's in the cache
|
|
if (!allowNetworking)
|
|
{
|
|
if (!mCacheSession)
|
|
return NS_ERROR_FAILURE;
|
|
|
|
nsCOMPtr<nsICacheEntryDescriptor> entryDesc;
|
|
rv = mCacheSession->OpenCacheEntry(NS_ConvertUCS2toUTF8(inURI).get(), nsICache::ACCESS_READ, nsICache::NON_BLOCKING, getter_AddRefs(entryDesc));
|
|
if (NS_FAILED(rv) || !entryDesc)
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
#endif
|
|
|
|
nsStringKey uriKey(inURI);
|
|
|
|
// first make sure that there isn't another entry in the hash for this
|
|
nsCOMPtr<nsISupports> foundStreamSupports = mStreamLoaderHash.Get(&uriKey);
|
|
if (foundStreamSupports)
|
|
return NS_OK;
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
rv = NS_NewURI(getter_AddRefs(uri), inURI);
|
|
if (NS_FAILED(rv))
|
|
return rv;
|
|
|
|
nsCOMPtr<nsISupports> loaderContext = new StreamLoaderContext(loadListener, userData, target, inURI);
|
|
|
|
nsLoadFlags loadFlags = (allowNetworking) ? nsIRequest::LOAD_NORMAL : nsIRequest::LOAD_FROM_CACHE;
|
|
loadFlags |= nsIRequest::LOAD_BACKGROUND; // don't show progress or cookie dialogs
|
|
nsCOMPtr<nsIStreamLoader> streamLoader;
|
|
rv = NS_NewStreamLoader(getter_AddRefs(streamLoader), uri, this, loaderContext, nsnull, nsnull, loadFlags);
|
|
if (NS_FAILED(rv))
|
|
{
|
|
NSLog(@"NS_NewStreamLoader for favicon failed");
|
|
return rv;
|
|
}
|
|
|
|
#ifdef DEBUG_smfr
|
|
NSLog(@"RequestURILoad called for %@", [NSString stringWith_nsAString: inURI]);
|
|
#endif
|
|
|
|
// put the stream loader into the hash table
|
|
nsCOMPtr<nsISupports> streamLoaderAsSupports = do_QueryInterface(streamLoader);
|
|
mStreamLoaderHash.Put(&uriKey, streamLoaderAsSupports);
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
@implementation RemoteDataProvider
|
|
|
|
|
|
+ (RemoteDataProvider*)sharedRemoteDataProvider
|
|
{
|
|
static RemoteDataProvider* sIconProvider = nil;
|
|
if (!sIconProvider)
|
|
{
|
|
sIconProvider = [[RemoteDataProvider alloc] init];
|
|
|
|
// we probably need to register for NSApplicationWillTerminateNotification notifications
|
|
// and delete this then.
|
|
}
|
|
|
|
return sIconProvider;
|
|
}
|
|
|
|
- (id)init
|
|
{
|
|
if ((self = [super init]))
|
|
{
|
|
mLoadManager = new RemoteURILoadManager;
|
|
NS_ADDREF(mLoadManager);
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
- (void)dealloc
|
|
{
|
|
NS_IF_RELEASE(mLoadManager);
|
|
[super dealloc];
|
|
}
|
|
|
|
- (BOOL)loadURI:(NSString*)inURI forTarget:(id)target withListener:(id<RemoteLoadListener>)inListener
|
|
withUserData:(id)userData allowNetworking:(BOOL)inNetworkOK
|
|
{
|
|
//NSLog(@"loadURI called with %@", inURI);
|
|
if (mLoadManager && [inURI length] > 0)
|
|
{
|
|
nsAutoString uriString;
|
|
[inURI assignTo_nsAString:uriString];
|
|
|
|
nsresult rv = mLoadManager->RequestURILoad(uriString, inListener, userData, target, (PRBool)inNetworkOK);
|
|
if (NS_FAILED(rv))
|
|
{
|
|
NSLog(@"RequestURILoad failed for @%", inURI);
|
|
return NO;
|
|
}
|
|
}
|
|
|
|
return YES;
|
|
}
|
|
|
|
- (BOOL)postURILoadRequest:(NSString*)inURI forTarget:(id)target withUserData:(id)userData allowNetworking:(BOOL)inNetworkOK
|
|
{
|
|
return [self loadURI:inURI forTarget:target withListener:self withUserData:userData allowNetworking:inNetworkOK];
|
|
}
|
|
|
|
// our own load listener callback
|
|
- (void)doneRemoteLoad:(NSString*)inURI forTarget:(id)target withUserData:(id)userData data:(NSData*)data status:(nsresult)status
|
|
{
|
|
NSDictionary* notificationData = [NSDictionary dictionaryWithObjectsAndKeys:
|
|
inURI, RemoteDataLoadRequestURIKey,
|
|
data, RemoteDataLoadRequestDataKey,
|
|
userData, RemoteDataLoadRequestUserDataKey,
|
|
[NSNumber numberWithInt:status], RemoteDataLoadRequestResultKey,
|
|
nil];
|
|
|
|
// NSLog(@"remoteLoadDone with status %d and length %d", status, [data length]);
|
|
[[NSNotificationCenter defaultCenter] postNotificationName: RemoteDataLoadRequestNotificationName
|
|
object:target userInfo:notificationData];
|
|
}
|
|
|
|
@end
|