Minor URL modifications

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@7912 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 2000-10-27 15:58:11 +00:00
parent 0289b26003
commit 7341fc3d79
5 changed files with 73 additions and 18 deletions

View file

@ -1,3 +1,8 @@
2000-10-27 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSURLHandle.m: Make class registration thread safe.
Tidy initialisation to use designated initialiser.
2000-10-27 Adam Fedor <fedor@gnu.org> 2000-10-27 Adam Fedor <fedor@gnu.org>
* Added localization support * Added localization support

View file

@ -51,6 +51,22 @@
<sel>usingCache:</sel> <sel>usingCache:</sel>
<arg type="BOOL">shouldUseCache</arg> <arg type="BOOL">shouldUseCache</arg>
<desc> <desc>
Loads the resource data for the specified URL.
<p>
If <em>shouldUseCache</em> is <code>YES</code> then an attempt
will be made to locate a cached NSURLHandle to provide the
resource data, otherwise a new handle will be created and
cached.
</p>
<p>
If the handle does not have the data available, it will be
asked to load the data in the background by calling its
loadInBackground method.
</p>
<p>
The specified client (if non-nil) will be set up to recieve
notifications of the progress fo the background load process.
</p>
</desc> </desc>
</method> </method>

View file

@ -71,6 +71,28 @@
<h3><a name ="method-3">loadResourceDataNotifyingClient:usingCache:</a></h3> <h3><a name ="method-3">loadResourceDataNotifyingClient:usingCache:</a></h3>
- (void) <b>loadResourceDataNotifyingClient:</b> (id)client <b>usingCache:</b> (BOOL)shouldUseCache;<br> - (void) <b>loadResourceDataNotifyingClient:</b> (id)client <b>usingCache:</b> (BOOL)shouldUseCache;<br>
Loads the resource data for the specified URL.
<p>
If <em>shouldUseCache</em> is <code>YES</code> then an attempt
will be made to locate a cached NSURLHandle to provide the
resource data, otherwise a new handle will be created and
cached.
</p>
<p>
If the handle does not have the data available, it will be
asked to load the data in the background by calling its
loadInBackground method.
</p>
<p>
The specified client (if non-nil) will be set up to recieve
notifications of the progress fo the background load process.
</p>
<hr> <hr>
<h3><a name ="method-4">parameterString</a></h3> <h3><a name ="method-4">parameterString</a></h3>

View file

@ -591,7 +591,6 @@ NSString *NSURLPartKey_query = @"query";
NSLog(@"*_baseURL: %@", [[url baseURL] description]); NSLog(@"*_baseURL: %@", [[url baseURL] description]);
} }
//-----------------------------------------------------------------------------
- (void) loadResourceDataNotifyingClient: (id)client - (void) loadResourceDataNotifyingClient: (id)client
usingCache: (BOOL)shouldUseCache usingCache: (BOOL)shouldUseCache
{ {
@ -635,7 +634,6 @@ NSString *NSURLPartKey_query = @"query";
return data; return data;
} }
//-----------------------------------------------------------------------------
- (NSURLHandle*) URLHandleUsingCache: (BOOL)shouldUseCache - (NSURLHandle*) URLHandleUsingCache: (BOOL)shouldUseCache
{ {
NSURLHandle *handle = nil; NSURLHandle *handle = nil;
@ -657,15 +655,13 @@ NSString *NSURLPartKey_query = @"query";
return handle; return handle;
} }
//-----------------------------------------------------------------------------
- (BOOL) setResourceData: (NSData*)data - (BOOL) setResourceData: (NSData*)data
{ {
NSURLHandle *handle = [self URLHandleUsingCache: YES]; NSURLHandle *handle = [self URLHandleUsingCache: YES];
return [handle writeData: data]; return (handle == nil) ? NO : [handle writeData: data];
} }
//-----------------------------------------------------------------------------
- (id) propertyForKey: (NSString*)propertyKey - (id) propertyForKey: (NSString*)propertyKey
{ {
NSURLHandle *handle = [self URLHandleUsingCache: YES]; NSURLHandle *handle = [self URLHandleUsingCache: YES];
@ -673,7 +669,6 @@ NSString *NSURLPartKey_query = @"query";
return [handle propertyForKey: propertyKey]; return [handle propertyForKey: propertyKey];
} }
//-----------------------------------------------------------------------------
- (BOOL) setProperty: (id)property - (BOOL) setProperty: (id)property
forKey: (NSString*)propertyKey; forKey: (NSString*)propertyKey;
{ {

View file

@ -31,6 +31,7 @@
#include <Foundation/NSString.h> #include <Foundation/NSString.h>
#include <Foundation/NSException.h> #include <Foundation/NSException.h>
#include <Foundation/NSConcreteNumber.h> #include <Foundation/NSConcreteNumber.h>
#include <Foundation/NSLock.h>
#include <Foundation/NSURLHandle.h> #include <Foundation/NSURLHandle.h>
#include <Foundation/NSURL.h> #include <Foundation/NSURL.h>
#include <Foundation/NSMapTable.h> #include <Foundation/NSMapTable.h>
@ -39,7 +40,9 @@
@implementation NSURLHandle @implementation NSURLHandle
static NSLock *registryLock = nil;
static NSMutableArray *registry = nil; static NSMutableArray *registry = nil;
static Class NSURLHandleClass = 0;
+ (NSURLHandle*) cachedHandleForURL: (NSURL*)url + (NSURLHandle*) cachedHandleForURL: (NSURL*)url
{ {
@ -47,11 +50,18 @@ static NSMutableArray *registry = nil;
* Each subclass is supposed to do its own caching, so we must * Each subclass is supposed to do its own caching, so we must
* find the correct subclass and ask it for its cached handle. * find the correct subclass and ask it for its cached handle.
*/ */
if (self == [NSURLHandle class]) if (self == NSURLHandleClass)
{ {
Class c = [self URLHandleClassForURL: url]; Class c = [self URLHandleClassForURL: url];
return [c cachedHandleForURL: url]; if (c == self || c == 0)
{
return nil;
}
else
{
return [c cachedHandleForURL: url];
}
} }
else else
{ {
@ -72,7 +82,9 @@ static NSMutableArray *registry = nil;
{ {
if (self == [NSURLHandle class]) if (self == [NSURLHandle class])
{ {
NSURLHandleClass = self;
registry = [NSMutableArray new]; registry = [NSMutableArray new];
registryLock = [NSLock new];
[self registerURLHandleClass: [GSFileURLHandle class]]; [self registerURLHandleClass: [GSFileURLHandle class]];
} }
} }
@ -84,13 +96,18 @@ static NSMutableArray *registry = nil;
* Re-adding a class moves it to the end of the registry - so it will * Re-adding a class moves it to the end of the registry - so it will
* be used in preference to any class added earlier. * be used in preference to any class added earlier.
*/ */
[registryLock lock];
[registry removeObjectIdenticalTo: urlHandleSubclass]; [registry removeObjectIdenticalTo: urlHandleSubclass];
[registry addObject: urlHandleSubclass]; [registry addObject: urlHandleSubclass];
[registryLock unlock];
} }
+ (Class) URLHandleClassForURL: (NSURL*)url + (Class) URLHandleClassForURL: (NSURL*)url
{ {
unsigned count = [registry count]; unsigned count;
[registryLock lock];
count = [registry count];
/* /*
* Find a class to handle the URL, try most recently registered first. * Find a class to handle the URL, try most recently registered first.
@ -101,9 +118,11 @@ static NSMutableArray *registry = nil;
if ([found canInitWithURL: url] == YES) if ([found canInitWithURL: url] == YES)
{ {
[registryLock unlock];
return (Class)found; return (Class)found;
} }
} }
[registryLock unlock];
return 0; return 0;
} }
@ -245,17 +264,16 @@ static NSMutableArray *registry = nil;
- (id) init - (id) init
{ {
_status = NSURLHandleNotLoaded; return [self initWithURL: nil cached: NO];
_clients = [NSMutableArray new];
_data = [NSMutableData new];
return self;
} }
- (id) initWithURL: (NSURL*)url - (id) initWithURL: (NSURL*)url
cached: (BOOL)cached cached: (BOOL)cached
{ {
[self subclassResponsibility: _cmd]; _status = NSURLHandleNotLoaded;
return nil; _clients = [NSMutableArray new];
_data = [NSMutableData new];
return self;
} }
- (void) loadInBackground - (void) loadInBackground
@ -394,14 +412,13 @@ static NSMutableDictionary *fileCache = nil;
return self; return self;
} }
} }
self = [super init];
if (self != nil) if ((self = [super initWithURL: url cached: cached]) != nil)
{ {
_path = [path copy]; _path = [path copy];
if (cached == YES) if (cached == YES)
{ {
[fileCache setObject: self forKey: _path]; [fileCache setObject: self forKey: _path];
RELEASE(self);
} }
} }
return self; return self;