Skip to content

Commit 16b844c

Browse files
author
Otyr Ugla
committed
Simplified code to track living CTF views. Changed technique to find views in front tab to use same views the observations use to actually load. Removed seemingly unused InvisibleItemMenu.xib. Updated settings panel to use + and - instead of words.
1 parent c12886a commit 16b844c

8 files changed

+168
-442
lines changed

ClickToFlash.xcodeproj/project.pbxproj

-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
6953E43C0F3EDEB50014ECF7 /* MenubarMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6953E43A0F3EDEB50014ECF7 /* MenubarMenu.xib */; };
4444
69A26D0C0F302C10006648BC /* NSBezierPath-RoundedRectangle.m in Sources */ = {isa = PBXBuildFile; fileRef = 69A26D0B0F302C10006648BC /* NSBezierPath-RoundedRectangle.m */; };
4545
79A21C4D0F30B735009424B8 /* CTFWhitelistWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = 79A21C490F30B735009424B8 /* CTFWhitelistWindowController.m */; };
46-
A4501BC80F4439FB00D04D61 /* OSAKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A4501BC70F4439FB00D04D61 /* OSAKit.framework */; };
4746
B5028E930F413553004886D7 /* HarnessAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = B5028E910F413553004886D7 /* HarnessAppDelegate.m */; };
4847
B5CFF17A0F40EEF1005DB9CC /* WhitelistPanel.xib in Resources */ = {isa = PBXBuildFile; fileRef = 072189BC0F30D9C3008C8944 /* WhitelistPanel.xib */; };
4948
B5CFF1870F40F000005DB9CC /* HarnessMain.xib in Resources */ = {isa = PBXBuildFile; fileRef = B5CFF1860F40F000005DB9CC /* HarnessMain.xib */; };
@@ -86,7 +85,6 @@
8685
69A26D0B0F302C10006648BC /* NSBezierPath-RoundedRectangle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSBezierPath-RoundedRectangle.m"; path = "Plugin/NSBezierPath-RoundedRectangle.m"; sourceTree = "<group>"; };
8786
79A21C490F30B735009424B8 /* CTFWhitelistWindowController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CTFWhitelistWindowController.m; path = Plugin/CTFWhitelistWindowController.m; sourceTree = "<group>"; };
8887
79A21C4A0F30B735009424B8 /* CTFWhitelistWindowController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CTFWhitelistWindowController.h; path = Plugin/CTFWhitelistWindowController.h; sourceTree = "<group>"; };
89-
A4501BC70F4439FB00D04D61 /* OSAKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OSAKit.framework; path = /System/Library/Frameworks/OSAKit.framework; sourceTree = "<absolute>"; };
9088
B5028E910F413553004886D7 /* HarnessAppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = HarnessAppDelegate.m; path = "TestFiles/Debugging Harness/HarnessAppDelegate.m"; sourceTree = "<group>"; };
9189
B5028E920F413553004886D7 /* HarnessAppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = HarnessAppDelegate.h; path = "TestFiles/Debugging Harness/HarnessAppDelegate.h"; sourceTree = "<group>"; };
9290
B5CFF1740F40EEDB005DB9CC /* Whitelist Panel Harness.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Whitelist Panel Harness.app"; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -105,7 +103,6 @@
105103
files = (
106104
55EB70580E04A8B80016593D /* Cocoa.framework in Frameworks */,
107105
55EB70590E04A8B80016593D /* WebKit.framework in Frameworks */,
108-
A4501BC80F4439FB00D04D61 /* OSAKit.framework in Frameworks */,
109106
);
110107
runOnlyForDeploymentPostprocessing = 0;
111108
};
@@ -164,7 +161,6 @@
164161
children = (
165162
55EB70560E04A8B80016593D /* Cocoa.framework */,
166163
55EB70570E04A8B80016593D /* WebKit.framework */,
167-
A4501BC70F4439FB00D04D61 /* OSAKit.framework */,
168164
);
169165
name = Frameworks;
170166
sourceTree = "<group>";

Plugin/CTFMenubarMenuController.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,22 @@ THE SOFTWARE.
2626

2727
#import <Cocoa/Cocoa.h>
2828

29-
3029
@class CTFWhitelistWindowController;
3130

31+
3232
extern NSString* kCTFLoadAllFlashViews;
3333
extern NSString* kCTFLoadFlashViewsForWindow;
3434
extern NSString* kCTFLoadInvisibleFlashViewsForWindow;
35-
extern NSString* sCTFNewViewNotification;
36-
extern NSString* sCTFDestroyedViewNotification;
35+
3736
extern NSUInteger maxInvisibleDimension;
3837

3938

4039
@interface CTFMenubarMenuController : NSObject {
4140
IBOutlet NSMenu* menu;
4241
CTFWhitelistWindowController *_whitelistWindowController;
43-
NSMutableDictionary *_flashViews;
42+
NSHashTable *_views;
4443
}
4544

46-
@property (retain) NSMutableDictionary *flashViews;
47-
4845
+ (CTFMenubarMenuController*) sharedController;
4946

5047
- (void) loadFlashForWindow: (NSWindow*) window;
@@ -54,4 +51,7 @@ extern NSUInteger maxInvisibleDimension;
5451
- (IBAction) loadKeyWindowInvisibleFlash: (id) sender;
5552
- (IBAction) showSettingsWindow: (id) sender;
5653

54+
- (void) registerView: (NSView*) view;
55+
- (void) unregisterView: (NSView*) view;
56+
5757
@end

Plugin/CTFMenubarMenuController.m

+48-156
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,15 @@ of this software and associated documentation files (the "Software"), to deal
2626

2727
#import "CTFMenubarMenuController.h"
2828
#import "CTFWhitelistWindowController.h"
29-
#import <OSAKit/OSAKit.h>
30-
#import <WebKit/WebKit.h>
3129

30+
#import "Plugin.h"
3231

3332
NSString* kCTFLoadAllFlashViews = @"CTFLoadAllFlashViews";
3433
NSString* kCTFLoadFlashViewsForWindow = @"CTFLoadFlashViewsForWindow";
3534
NSString* kCTFLoadInvisibleFlashViewsForWindow = @"CTFLoadInvisibleFlashViewsForWindow";
36-
NSString *sCTFNewViewNotification = @"CTFNewFlashView";
37-
NSString *sCTFDestroyedViewNotification = @"CTFDestroyedFlashView";
35+
3836
NSUInteger maxInvisibleDimension = 50;
3937

40-
static CTFMenubarMenuController* sSingleton = nil;
4138

4239
static NSString* kApplicationsToInstallMenuInto[] = {
4340
@"com.apple.Safari",
@@ -116,6 +113,9 @@ - (int) applicationMenuPrefsInsertionLocation
116113
#pragma mark Lifetime management
117114

118115

116+
static CTFMenubarMenuController* sSingleton = nil;
117+
118+
119119
- (id) init
120120
{
121121
if( sSingleton ) {
@@ -130,27 +130,19 @@ - (id) init
130130
if( self ) {
131131
if( ! [ NSBundle loadNibNamed: @"MenubarMenu" owner: self ] )
132132
NSLog( @"ClickToFlash: Could not load menubar menu nib" );
133+
134+
_views = NSCreateHashTable( NSNonRetainedObjectHashCallBacks, 0 );
133135
}
134136

135-
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
136-
[center addObserver: self
137-
selector: @selector( _trackNewView: )
138-
name: sCTFNewViewNotification
139-
object: nil ];
140-
141-
[center addObserver: self
142-
selector: @selector( _stopTrackingView: )
143-
name: sCTFDestroyedViewNotification
144-
object: nil ];
145-
146137
return self;
147138
}
148139

149140

150141
- (void) dealloc
151142
{
152143
[ _whitelistWindowController release ];
153-
144+
NSFreeHashTable( _views );
145+
154146
[ super dealloc ];
155147
}
156148

@@ -196,167 +188,69 @@ + (CTFMenubarMenuController*) sharedController
196188
#pragma mark -
197189
#pragma mark View Management
198190

199-
- (void) _trackNewView: (NSNotification*) notification
200-
{
201-
NSMutableDictionary *flashViewsDict = nil;
202-
if ([self flashViews])
203-
flashViewsDict = [[self flashViews] copy];
204-
205-
if (! flashViewsDict) flashViewsDict = [NSMutableDictionary dictionary];
206-
207-
NSString *newViewBaseURL = [[[notification userInfo] objectForKey:@"baseURL"] absoluteString];
208-
NSString *newViewSrc = [[notification userInfo] objectForKey:@"src"];
209-
NSNumber *newViewHeight = [[notification userInfo] objectForKey:@"height"];
210-
NSNumber *newViewWidth = [[notification userInfo] objectForKey:@"width"];
211-
id newTarget = [notification object];
212-
213-
NSDictionary *newTargetDict = [NSDictionary dictionaryWithObjectsAndKeys:newTarget,@"target",newViewSrc,@"src",newViewHeight,@"height",newViewWidth,@"width",nil];
214-
215-
NSMutableArray *baseURLArray = [flashViewsDict objectForKey:newViewBaseURL];
216-
217-
if (! baseURLArray) {
218-
baseURLArray = [NSMutableArray arrayWithObject:newTargetDict];
219-
[flashViewsDict setObject:baseURLArray forKey:newViewBaseURL];
220-
} else {
221-
[baseURLArray addObject:newTargetDict];
222-
}
223-
224-
[self setFlashViews:flashViewsDict];
225-
226-
// not sure why, but the following lines causes crashes and unexpected behavior
227-
//[flashViewsDict release];
228-
}
229191

230-
- (void) _stopTrackingView: (NSNotification*) notification
192+
- (void) registerView: (NSView*) view
231193
{
232-
NSMutableDictionary *flashViewsDict = nil;
233-
if ([self flashViews])
234-
flashViewsDict = [[self flashViews] copy];
235-
236-
if (! flashViewsDict) flashViewsDict = [NSMutableDictionary dictionary];
237-
238-
NSString *baseURL = [[notification userInfo] objectForKey:@"baseURL"];
239-
NSMutableArray *baseURLArray = [flashViewsDict objectForKey:baseURL];
240-
id flashView = [notification object];
241-
242-
if (! baseURLArray) {
243-
// we're apparently not tracking this view
244-
return;
245-
}
246-
247-
NSDictionary *currentDictionary;
248-
BOOL foundView = NO;
249-
for (currentDictionary in baseURLArray) {
250-
if ([currentDictionary objectForKey:@"target"] == flashView) {
251-
foundView = YES;
252-
break;
253-
}
254-
}
255-
256-
if (foundView) {
257-
// only do this stuff if we actually find the view we want to stop tracking
258-
259-
[baseURLArray removeObject:currentDictionary];
260-
if ([baseURLArray count] == 0) [flashViewsDict removeObjectForKey:baseURL];
261-
[self setFlashViews:flashViewsDict];
262-
}
194+
NSHashInsertIfAbsent( _views, view );
263195
}
264196

265-
- (NSString *)_baseURLOfKeyWindow;
266-
{
267-
// [[NSBundle mainBundle] bundleIdentifier|executablePath|bundlePath] all return stuff for Safari
268-
// even if called from WebKit
269-
270-
// the following line crashes WebKit, so we can't use the Scripting Bridge until that is fixed
271-
// SafariApplication *safari = [SBApplication applicationWithProcessIdentifier:getpid()];
272-
273-
NSString *webKitFrameworkBundlePath = [[NSBundle bundleForClass:[WebView class]] bundlePath];
274-
275-
BOOL isWebKit = NO;
276-
if (! [webKitFrameworkBundlePath hasPrefix:@"/System/Library/Frameworks"]) {
277-
// we're not using the system version of WebKit, so it's the WebKit app
278-
isWebKit = YES;
279-
};
280-
281-
// the following line doesn't seem to work reliably
282-
// BOOL isWebKit = [[[NSProcessInfo processInfo] arguments] containsObject:@"-WebKitDeveloperExtras"];
283-
NSString *appString = @"";
284-
if (isWebKit) {
285-
appString = @"WebKit";
286-
} else {
287-
appString = @"Safari";
288-
}
289-
290-
NSString *appleScriptSourceString = [NSString stringWithFormat:@"tell application \"%@\"\nURL of current tab of front window\nend tell",appString];
291-
292-
293-
// I didn't want to bring OSACrashyScript into this, but I had to; sorry guys, Scripting Bridge
294-
// just totally crashes WebKit and that's unacceptable
295-
296-
NSDictionary *errorDict = nil;
297-
OSAScript *browserNameScript = [[OSAScript alloc] initWithSource:appleScriptSourceString];
298-
NSAppleEventDescriptor *aeDesc = [browserNameScript executeAndReturnError:&errorDict];
299-
[browserNameScript release];
300-
301-
NSString *baseURL = nil;
302-
303-
if (! errorDict) baseURL = [aeDesc stringValue];
304-
305-
return baseURL;
306-
}
307197

308-
- (BOOL) _atLeastOneFlashViewExists;
198+
- (void) unregisterView: (NSView*) view
309199
{
310-
NSLog(@"%@",[self flashViews]);
311-
return ([[[self flashViews] allKeys] count] >= 1);
200+
NSHashRemove( _views, view );
312201
}
313202

314203

315-
- (BOOL) _flashViewExistsForKeyWindow;
204+
- (BOOL) _atLeastOneFlashViewExists
316205
{
317-
NSString *baseURL = [self _baseURLOfKeyWindow];
318-
319-
// if there's an array for the base URL, there is at least one view
320-
// with that base URL
321-
return ([[self flashViews] objectForKey:baseURL] != nil);
206+
return NSCountHashTable( _views ) > 0;
322207
}
323208

324-
- (BOOL) _invisibleFlashViewExistsForKeyWindow;
209+
210+
- (BOOL) _flashViewExistsForKeyWindowWithInvisibleOnly: (BOOL) mustBeInvisible
325211
{
326-
BOOL returnValue = NO;
327-
NSString *baseURL = [self _baseURLOfKeyWindow];
212+
BOOL rslt = NO;
328213

329-
NSMutableArray *baseURLArray = [[self flashViews] objectForKey:baseURL];
214+
NSWindow* keyWindow = [ NSApp keyWindow ];
330215

331-
if (baseURLArray) {
332-
NSDictionary *currentDictionary = nil;
333-
334-
for (currentDictionary in baseURLArray) {
335-
NSUInteger height = [[currentDictionary objectForKey:@"height"] intValue];
336-
NSUInteger width = [[currentDictionary objectForKey:@"width"] intValue];
337-
338-
if ((height <= maxInvisibleDimension) && (width <= maxInvisibleDimension)) {
339-
returnValue = YES;
216+
NSHashEnumerator enumerator = NSEnumerateHashTable( _views );
217+
CTFClickToFlashPlugin* item;
218+
while( item = NSNextHashEnumeratorItem( &enumerator ) ) {
219+
if( [ item window ] == keyWindow ) {
220+
if( !mustBeInvisible || [ item isConsideredInvisible ] ) {
221+
rslt = YES;
340222
break;
341223
}
342224
}
343225
}
226+
NSEndHashTableEnumeration( &enumerator );
344227

345-
return returnValue;
228+
return rslt;
346229
}
347230

348-
- (BOOL)validateMenuItem:(NSMenuItem *)item {
349-
BOOL returnValue = YES;
350-
351-
if ([item action] == @selector(loadAllFlash:)) {
352-
returnValue = [self _atLeastOneFlashViewExists];
353-
} else if ([item action] == @selector(loadKeyWindowFlash:)) {
354-
returnValue = [self _flashViewExistsForKeyWindow];
355-
} else if ([item action] == @selector(loadKeyWindowInvisibleFlash:)) {
356-
returnValue = [self _invisibleFlashViewExistsForKeyWindow];
231+
- (BOOL) _flashViewExistsForKeyWindow
232+
{
233+
return [ self _flashViewExistsForKeyWindowWithInvisibleOnly: NO ];
234+
}
235+
236+
- (BOOL) _invisibleFlashViewExistsForKeyWindow;
237+
{
238+
return [ self _flashViewExistsForKeyWindowWithInvisibleOnly: YES ];
239+
}
240+
241+
- (BOOL) validateMenuItem: (NSMenuItem*) item
242+
{
243+
if ( [ item action ] == @selector( loadAllFlash: ) ) {
244+
return [ self _atLeastOneFlashViewExists ];
245+
}
246+
else if( [ item action ] == @selector( loadKeyWindowFlash: ) ) {
247+
return [ self _flashViewExistsForKeyWindow ];
248+
}
249+
else if( [ item action ] == @selector(loadKeyWindowInvisibleFlash: ) ) {
250+
return [ self _invisibleFlashViewExistsForKeyWindow ];
357251
}
358252

359-
return returnValue;
253+
return YES;
360254
}
361255

362256
#pragma mark -
@@ -408,6 +302,4 @@ - (IBAction) showSettingsWindow: (id) sender
408302
[ _whitelistWindowController showWindow: sender ];
409303
}
410304

411-
@synthesize flashViews = _flashViews;
412-
413305
@end

Plugin/CTFWhitelistWindowController.h

+2-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@
22

33

44
@interface CTFWhitelistWindowController : NSWindowController {
5-
IBOutlet NSArrayController *_controller;
6-
// NSMutableArray *_sites;
5+
IBOutlet NSArrayController *_controller;
76
}
8-
//
9-
//- (IBAction) addWhitelistSite: (id) sender;
10-
//- (IBAction) removeWhitelistSite: (id) sender;
11-
//
12-
//- (void) saveWhitelist: (id) sender;
13-
//- (void) whitelistChanged: (NSNotification *) note;
7+
148
@end
159

0 commit comments

Comments
 (0)