Skip to content

Commit

Permalink
unique a ballooned whitelist if there are lots of duplicate entries; …
Browse files Browse the repository at this point in the history
…this seems to be pretty fast, since it basically just loads the array from prefs into a set and then writes the set back out again; on a 1.5 MB prefs file with 77673 items, on a MacBook Core 2 Duo 2 GHz, it takes only a second or two

(cherry picked from commit fa4536e)
  • Loading branch information
simX committed Oct 1, 2009
1 parent 33c8bcf commit 12ecfb2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
45 changes: 21 additions & 24 deletions Plugin/CTFWhitelist.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,18 @@ of this software and associated documentation files (the "Software"), to deal
CTFSiteKindWhitelist = 0
} CTGSiteKind;


static NSUInteger indexOfItemForSite( NSArray* arr, NSString* site )
static NSDictionary* itemForSite( NSSet* set, NSString* site )
{
NSDictionary *specificWhitelistItem = nil;

int i = 0;
CTFForEachObject( NSDictionary, item, arr ) {
CTFForEachObject( NSDictionary, item, set ) {
if( [ [ item objectForKey: @"site" ] isEqualToString: site ] )
return i;
specificWhitelistItem = item;
++i;
}

return NSNotFound;
}

static NSDictionary* itemForSite( NSArray* arr, NSString* site )
{
NSUInteger index = indexOfItemForSite( arr, site );

if( index != NSNotFound )
return [ arr objectAtIndex: index ];

return nil;
return specificWhitelistItem;
}

static NSDictionary* whitelistItemForSite( NSString* site )
Expand Down Expand Up @@ -166,25 +157,31 @@ - (BOOL) _isHostWhitelisted

- (BOOL) _isWhiteListedForHostString:(NSString *)hostString
{
NSArray *hostWhitelist = [[CTFUserDefaultsController standardUserDefaults] arrayForKey: sHostSiteInfoDefaultsKey];
return hostWhitelist && itemForSite(hostWhitelist, hostString) != nil;
NSArray *hostWhitelistArray = [[CTFUserDefaultsController standardUserDefaults] arrayForKey: sHostSiteInfoDefaultsKey];
NSSet *hostWhitelistSet = [NSSet setWithArray:hostWhitelistArray];
return hostWhitelistArray && itemForSite(hostWhitelistSet, hostString) != nil;
}

- (NSMutableArray *) _mutableSiteInfo
- (NSMutableSet *) _mutableSiteInfo
{
NSMutableArray *hostWhitelist = [[[[CTFUserDefaultsController standardUserDefaults] arrayForKey: sHostSiteInfoDefaultsKey] mutableCopy] autorelease];
if (hostWhitelist == nil) {
hostWhitelist = [NSMutableArray array];
}
NSMutableArray *hostWhitelistArray = [[[[CTFUserDefaultsController standardUserDefaults] arrayForKey: sHostSiteInfoDefaultsKey] mutableCopy] autorelease];

NSMutableSet *hostWhitelist;
if (hostWhitelistArray == nil) {
hostWhitelist = [NSMutableSet setWithCapacity:0];
} else {
hostWhitelist = [NSMutableSet setWithArray:hostWhitelistArray];
}

return hostWhitelist;
}

- (void) _addHostToWhitelist
{
NSMutableArray *siteInfo = [self _mutableSiteInfo];
NSMutableSet *siteInfo = [self _mutableSiteInfo];
[siteInfo addObject: whitelistItemForSite([self host])];

[[CTFUserDefaultsController standardUserDefaults] setValue:siteInfo forKeyPath:@"values.siteInfo"];
[[CTFUserDefaultsController standardUserDefaults] setValue:[siteInfo allObjects] forKeyPath:@"values.siteInfo"];

[[NSNotificationCenter defaultCenter] postNotificationName: sCTFWhitelistAdditionMade object: self];
}
Expand Down
1 change: 1 addition & 0 deletions Plugin/Plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ THE SOFTWARE.

- (id) initWithArguments:(NSDictionary *)arguments;
- (void)_migratePrefsToExternalFile;
- (void)_uniquePrefsFileWhitelist;
- (void) _addApplicationWhitelistArrayToPrefsFile;

- (DOMElement *)container;
Expand Down
10 changes: 10 additions & 0 deletions Plugin/Plugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ - (id) initWithArguments:(NSDictionary *)arguments

[self _migrateWhitelist];
[self _migratePrefsToExternalFile];
[self _uniquePrefsFileWhitelist];
[self _addApplicationWhitelistArrayToPrefsFile];


Expand Down Expand Up @@ -521,6 +522,15 @@ - (void) _migratePrefsToExternalFile
[[NSUserDefaults standardUserDefaults] removeSuiteNamed:@"com.github.rentzsch.clicktoflash"];
}

- (void) _uniquePrefsFileWhitelist
{
NSArray *siteInfoArray = [[CTFUserDefaultsController standardUserDefaults] arrayForKey:@"siteInfo"];
NSSet *siteInfoSet = [NSSet setWithArray:siteInfoArray];

[[CTFUserDefaultsController standardUserDefaults] setValue:[siteInfoSet allObjects] forKeyPath:@"values.siteInfo"];
}


- (void) _addApplicationWhitelistArrayToPrefsFile
{
CTFUserDefaultsController *standardUserDefaults = [CTFUserDefaultsController standardUserDefaults];
Expand Down

0 comments on commit 12ecfb2

Please sign in to comment.