Skip to content
Browse files

unique a ballooned whitelist if there are lots of duplicate entries; …

…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...
1 parent 33c8bcf commit 12ecfb2a7c23e5434054f3f9085a3fcda6374842 @simX simX committed
Showing with 32 additions and 24 deletions.
  1. +21 −24 Plugin/CTFWhitelist.m
  2. +1 −0 Plugin/Plugin.h
  3. +10 −0 Plugin/Plugin.m
View
45 Plugin/CTFWhitelist.m
@@ -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 )
@@ -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];
}
View
1 Plugin/Plugin.h
@@ -65,6 +65,7 @@ THE SOFTWARE.
- (id) initWithArguments:(NSDictionary *)arguments;
- (void)_migratePrefsToExternalFile;
+- (void)_uniquePrefsFileWhitelist;
- (void) _addApplicationWhitelistArrayToPrefsFile;
- (DOMElement *)container;
View
10 Plugin/Plugin.m
@@ -155,6 +155,7 @@ - (id) initWithArguments:(NSDictionary *)arguments
[self _migrateWhitelist];
[self _migratePrefsToExternalFile];
+ [self _uniquePrefsFileWhitelist];
[self _addApplicationWhitelistArrayToPrefsFile];
@@ -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];

0 comments on commit 12ecfb2

Please sign in to comment.
Something went wrong with that request. Please try again.