-
Notifications
You must be signed in to change notification settings - Fork 279
/
MediaStrategyRegistry.m
119 lines (92 loc) · 3.25 KB
/
MediaStrategyRegistry.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// MediaStrategyRegistry.m
// BeardedSpice
//
// Created by Jose Falcon on 12/15/13.
// Copyright (c) 2013 Tyler Rhodes / Jose Falcon. All rights reserved.
//
#import "MediaStrategyRegistry.h"
#import "BSMediaStrategy.h"
#import "BSStrategyCache.h"
#import "TabAdapter.h"
#import "EHCCache.h"
#define MAX_REGISTERED_CACHE 500
@interface MediaStrategyRegistry ()
@property (nonatomic, strong) NSMutableArray *availableStrategies;
@property (nonatomic, strong) EHCCache *registeredCache;
@property (nonatomic, strong) BSStrategyCache *strategyCache;
@end
@implementation MediaStrategyRegistry {
}
static MediaStrategyRegistry *singletonMediaStrategyRegistry;
/////////////////////////////////////////////////////////////////////
#pragma mark Initialize
+ (MediaStrategyRegistry *)singleton{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
singletonMediaStrategyRegistry = [MediaStrategyRegistry alloc];
singletonMediaStrategyRegistry = [singletonMediaStrategyRegistry init];
});
return singletonMediaStrategyRegistry;
}
- (id)init{
if (singletonMediaStrategyRegistry != self) {
return nil;
}
self = [super init];
return self;
}
- (void)setUserDefaults:(NSString *)userDefaultsKey strategyCache:(BSStrategyCache *)cache
{
_strategyCache = cache;
_registeredCache = [[EHCCache alloc] initWithCapacity:MAX_REGISTERED_CACHE];
_availableStrategies = [NSMutableArray new];
NSDictionary *defaults = [[NSUserDefaults standardUserDefaults] dictionaryForKey:userDefaultsKey];
// enable strategies that are marked enabled or have no entry
for (NSString *fileName in _strategyCache.cache)
{
BSMediaStrategy *strategy = _strategyCache.cache[fileName];
NSNumber *enabled = [defaults objectForKey:[strategy displayName]];
if (!enabled || [enabled boolValue]) {
[_availableStrategies addObject:strategy];
}
}
}
/////////////////////////////////////////////////////////////////////
#pragma mark Methods
-(void) addMediaStrategy:(BSMediaStrategy *) strategy
{
[_availableStrategies addObject:strategy];
[self.registeredCache clear];
}
-(void) removeMediaStrategy:(BSMediaStrategy *) strategy
{
[_availableStrategies removeObject:strategy];
[self.registeredCache clear];
}
- (BSMediaStrategy *)getMediaStrategyForTab:(TabAdapter *)tab {
if (tab == nil)
return nil;
NSString *cacheKey = [NSString stringWithFormat:@"%@", [tab URL]];
id strat = self.registeredCache[cacheKey];
/* Return the equivalent of a full scan except we dont repeat calculations */
if (strat == [NSNull null])
return nil;
if (strat)
return strat;
if (tab.check) {
for (BSMediaStrategy *strategy in _availableStrategies) {
BOOL accepted = [strategy accepts:tab];
/* Store the result of this calculation for future use */
if (accepted) {
[self.registeredCache addValue:strategy forKey:cacheKey];
NSLog(@"%@ is valid for %@", strategy, tab);
return strategy;
}
}
}
/* Worst case, no compatible registry found */
[self.registeredCache addValue:[NSNull null] forKey:cacheKey];
return nil;
}
@end