Skip to content

Commit

Permalink
Simplified code to track living CTF views. Changed technique to find …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
Otyr Ugla committed Feb 13, 2009
1 parent c12886a commit 16b844c
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 442 deletions.
4 changes: 0 additions & 4 deletions ClickToFlash.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
6953E43C0F3EDEB50014ECF7 /* MenubarMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6953E43A0F3EDEB50014ECF7 /* MenubarMenu.xib */; };
69A26D0C0F302C10006648BC /* NSBezierPath-RoundedRectangle.m in Sources */ = {isa = PBXBuildFile; fileRef = 69A26D0B0F302C10006648BC /* NSBezierPath-RoundedRectangle.m */; };
79A21C4D0F30B735009424B8 /* CTFWhitelistWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = 79A21C490F30B735009424B8 /* CTFWhitelistWindowController.m */; };
A4501BC80F4439FB00D04D61 /* OSAKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A4501BC70F4439FB00D04D61 /* OSAKit.framework */; };
B5028E930F413553004886D7 /* HarnessAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = B5028E910F413553004886D7 /* HarnessAppDelegate.m */; };
B5CFF17A0F40EEF1005DB9CC /* WhitelistPanel.xib in Resources */ = {isa = PBXBuildFile; fileRef = 072189BC0F30D9C3008C8944 /* WhitelistPanel.xib */; };
B5CFF1870F40F000005DB9CC /* HarnessMain.xib in Resources */ = {isa = PBXBuildFile; fileRef = B5CFF1860F40F000005DB9CC /* HarnessMain.xib */; };
Expand Down Expand Up @@ -86,7 +85,6 @@
69A26D0B0F302C10006648BC /* NSBezierPath-RoundedRectangle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSBezierPath-RoundedRectangle.m"; path = "Plugin/NSBezierPath-RoundedRectangle.m"; sourceTree = "<group>"; };
79A21C490F30B735009424B8 /* CTFWhitelistWindowController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CTFWhitelistWindowController.m; path = Plugin/CTFWhitelistWindowController.m; sourceTree = "<group>"; };
79A21C4A0F30B735009424B8 /* CTFWhitelistWindowController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CTFWhitelistWindowController.h; path = Plugin/CTFWhitelistWindowController.h; sourceTree = "<group>"; };
A4501BC70F4439FB00D04D61 /* OSAKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OSAKit.framework; path = /System/Library/Frameworks/OSAKit.framework; sourceTree = "<absolute>"; };
B5028E910F413553004886D7 /* HarnessAppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = HarnessAppDelegate.m; path = "TestFiles/Debugging Harness/HarnessAppDelegate.m"; sourceTree = "<group>"; };
B5028E920F413553004886D7 /* HarnessAppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = HarnessAppDelegate.h; path = "TestFiles/Debugging Harness/HarnessAppDelegate.h"; sourceTree = "<group>"; };
B5CFF1740F40EEDB005DB9CC /* Whitelist Panel Harness.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Whitelist Panel Harness.app"; sourceTree = BUILT_PRODUCTS_DIR; };
Expand All @@ -105,7 +103,6 @@
files = (
55EB70580E04A8B80016593D /* Cocoa.framework in Frameworks */,
55EB70590E04A8B80016593D /* WebKit.framework in Frameworks */,
A4501BC80F4439FB00D04D61 /* OSAKit.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -164,7 +161,6 @@
children = (
55EB70560E04A8B80016593D /* Cocoa.framework */,
55EB70570E04A8B80016593D /* WebKit.framework */,
A4501BC70F4439FB00D04D61 /* OSAKit.framework */,
);
name = Frameworks;
sourceTree = "<group>";
Expand Down
12 changes: 6 additions & 6 deletions Plugin/CTFMenubarMenuController.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,22 @@ THE SOFTWARE.

#import <Cocoa/Cocoa.h>


@class CTFWhitelistWindowController;


extern NSString* kCTFLoadAllFlashViews;
extern NSString* kCTFLoadFlashViewsForWindow;
extern NSString* kCTFLoadInvisibleFlashViewsForWindow;
extern NSString* sCTFNewViewNotification;
extern NSString* sCTFDestroyedViewNotification;

extern NSUInteger maxInvisibleDimension;


@interface CTFMenubarMenuController : NSObject {
IBOutlet NSMenu* menu;
CTFWhitelistWindowController *_whitelistWindowController;
NSMutableDictionary *_flashViews;
NSHashTable *_views;
}

@property (retain) NSMutableDictionary *flashViews;

+ (CTFMenubarMenuController*) sharedController;

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

- (void) registerView: (NSView*) view;
- (void) unregisterView: (NSView*) view;

@end
204 changes: 48 additions & 156 deletions Plugin/CTFMenubarMenuController.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,15 @@ of this software and associated documentation files (the "Software"), to deal

#import "CTFMenubarMenuController.h"
#import "CTFWhitelistWindowController.h"
#import <OSAKit/OSAKit.h>
#import <WebKit/WebKit.h>

#import "Plugin.h"

NSString* kCTFLoadAllFlashViews = @"CTFLoadAllFlashViews";
NSString* kCTFLoadFlashViewsForWindow = @"CTFLoadFlashViewsForWindow";
NSString* kCTFLoadInvisibleFlashViewsForWindow = @"CTFLoadInvisibleFlashViewsForWindow";
NSString *sCTFNewViewNotification = @"CTFNewFlashView";
NSString *sCTFDestroyedViewNotification = @"CTFDestroyedFlashView";

NSUInteger maxInvisibleDimension = 50;

static CTFMenubarMenuController* sSingleton = nil;

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


static CTFMenubarMenuController* sSingleton = nil;


- (id) init
{
if( sSingleton ) {
Expand All @@ -130,27 +130,19 @@ - (id) init
if( self ) {
if( ! [ NSBundle loadNibNamed: @"MenubarMenu" owner: self ] )
NSLog( @"ClickToFlash: Could not load menubar menu nib" );

_views = NSCreateHashTable( NSNonRetainedObjectHashCallBacks, 0 );
}

NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver: self
selector: @selector( _trackNewView: )
name: sCTFNewViewNotification
object: nil ];

[center addObserver: self
selector: @selector( _stopTrackingView: )
name: sCTFDestroyedViewNotification
object: nil ];

return self;
}


- (void) dealloc
{
[ _whitelistWindowController release ];

NSFreeHashTable( _views );

[ super dealloc ];
}

Expand Down Expand Up @@ -196,167 +188,69 @@ + (CTFMenubarMenuController*) sharedController
#pragma mark -
#pragma mark View Management

- (void) _trackNewView: (NSNotification*) notification
{
NSMutableDictionary *flashViewsDict = nil;
if ([self flashViews])
flashViewsDict = [[self flashViews] copy];

if (! flashViewsDict) flashViewsDict = [NSMutableDictionary dictionary];

NSString *newViewBaseURL = [[[notification userInfo] objectForKey:@"baseURL"] absoluteString];
NSString *newViewSrc = [[notification userInfo] objectForKey:@"src"];
NSNumber *newViewHeight = [[notification userInfo] objectForKey:@"height"];
NSNumber *newViewWidth = [[notification userInfo] objectForKey:@"width"];
id newTarget = [notification object];

NSDictionary *newTargetDict = [NSDictionary dictionaryWithObjectsAndKeys:newTarget,@"target",newViewSrc,@"src",newViewHeight,@"height",newViewWidth,@"width",nil];

NSMutableArray *baseURLArray = [flashViewsDict objectForKey:newViewBaseURL];

if (! baseURLArray) {
baseURLArray = [NSMutableArray arrayWithObject:newTargetDict];
[flashViewsDict setObject:baseURLArray forKey:newViewBaseURL];
} else {
[baseURLArray addObject:newTargetDict];
}

[self setFlashViews:flashViewsDict];

// not sure why, but the following lines causes crashes and unexpected behavior
//[flashViewsDict release];
}

- (void) _stopTrackingView: (NSNotification*) notification
- (void) registerView: (NSView*) view
{
NSMutableDictionary *flashViewsDict = nil;
if ([self flashViews])
flashViewsDict = [[self flashViews] copy];

if (! flashViewsDict) flashViewsDict = [NSMutableDictionary dictionary];

NSString *baseURL = [[notification userInfo] objectForKey:@"baseURL"];
NSMutableArray *baseURLArray = [flashViewsDict objectForKey:baseURL];
id flashView = [notification object];

if (! baseURLArray) {
// we're apparently not tracking this view
return;
}

NSDictionary *currentDictionary;
BOOL foundView = NO;
for (currentDictionary in baseURLArray) {
if ([currentDictionary objectForKey:@"target"] == flashView) {
foundView = YES;
break;
}
}

if (foundView) {
// only do this stuff if we actually find the view we want to stop tracking

[baseURLArray removeObject:currentDictionary];
if ([baseURLArray count] == 0) [flashViewsDict removeObjectForKey:baseURL];
[self setFlashViews:flashViewsDict];
}
NSHashInsertIfAbsent( _views, view );
}

- (NSString *)_baseURLOfKeyWindow;
{
// [[NSBundle mainBundle] bundleIdentifier|executablePath|bundlePath] all return stuff for Safari
// even if called from WebKit

// the following line crashes WebKit, so we can't use the Scripting Bridge until that is fixed
// SafariApplication *safari = [SBApplication applicationWithProcessIdentifier:getpid()];

NSString *webKitFrameworkBundlePath = [[NSBundle bundleForClass:[WebView class]] bundlePath];

BOOL isWebKit = NO;
if (! [webKitFrameworkBundlePath hasPrefix:@"/System/Library/Frameworks"]) {
// we're not using the system version of WebKit, so it's the WebKit app
isWebKit = YES;
};

// the following line doesn't seem to work reliably
// BOOL isWebKit = [[[NSProcessInfo processInfo] arguments] containsObject:@"-WebKitDeveloperExtras"];
NSString *appString = @"";
if (isWebKit) {
appString = @"WebKit";
} else {
appString = @"Safari";
}

NSString *appleScriptSourceString = [NSString stringWithFormat:@"tell application \"%@\"\nURL of current tab of front window\nend tell",appString];


// I didn't want to bring OSACrashyScript into this, but I had to; sorry guys, Scripting Bridge
// just totally crashes WebKit and that's unacceptable

NSDictionary *errorDict = nil;
OSAScript *browserNameScript = [[OSAScript alloc] initWithSource:appleScriptSourceString];
NSAppleEventDescriptor *aeDesc = [browserNameScript executeAndReturnError:&errorDict];
[browserNameScript release];

NSString *baseURL = nil;

if (! errorDict) baseURL = [aeDesc stringValue];

return baseURL;
}

- (BOOL) _atLeastOneFlashViewExists;
- (void) unregisterView: (NSView*) view
{
NSLog(@"%@",[self flashViews]);
return ([[[self flashViews] allKeys] count] >= 1);
NSHashRemove( _views, view );
}


- (BOOL) _flashViewExistsForKeyWindow;
- (BOOL) _atLeastOneFlashViewExists
{
NSString *baseURL = [self _baseURLOfKeyWindow];

// if there's an array for the base URL, there is at least one view
// with that base URL
return ([[self flashViews] objectForKey:baseURL] != nil);
return NSCountHashTable( _views ) > 0;
}

- (BOOL) _invisibleFlashViewExistsForKeyWindow;

- (BOOL) _flashViewExistsForKeyWindowWithInvisibleOnly: (BOOL) mustBeInvisible
{
BOOL returnValue = NO;
NSString *baseURL = [self _baseURLOfKeyWindow];
BOOL rslt = NO;

NSMutableArray *baseURLArray = [[self flashViews] objectForKey:baseURL];
NSWindow* keyWindow = [ NSApp keyWindow ];

if (baseURLArray) {
NSDictionary *currentDictionary = nil;

for (currentDictionary in baseURLArray) {
NSUInteger height = [[currentDictionary objectForKey:@"height"] intValue];
NSUInteger width = [[currentDictionary objectForKey:@"width"] intValue];

if ((height <= maxInvisibleDimension) && (width <= maxInvisibleDimension)) {
returnValue = YES;
NSHashEnumerator enumerator = NSEnumerateHashTable( _views );
CTFClickToFlashPlugin* item;
while( item = NSNextHashEnumeratorItem( &enumerator ) ) {
if( [ item window ] == keyWindow ) {
if( !mustBeInvisible || [ item isConsideredInvisible ] ) {
rslt = YES;
break;
}
}
}
NSEndHashTableEnumeration( &enumerator );

return returnValue;
return rslt;
}

- (BOOL)validateMenuItem:(NSMenuItem *)item {
BOOL returnValue = YES;

if ([item action] == @selector(loadAllFlash:)) {
returnValue = [self _atLeastOneFlashViewExists];
} else if ([item action] == @selector(loadKeyWindowFlash:)) {
returnValue = [self _flashViewExistsForKeyWindow];
} else if ([item action] == @selector(loadKeyWindowInvisibleFlash:)) {
returnValue = [self _invisibleFlashViewExistsForKeyWindow];
- (BOOL) _flashViewExistsForKeyWindow
{
return [ self _flashViewExistsForKeyWindowWithInvisibleOnly: NO ];
}

- (BOOL) _invisibleFlashViewExistsForKeyWindow;
{
return [ self _flashViewExistsForKeyWindowWithInvisibleOnly: YES ];
}

- (BOOL) validateMenuItem: (NSMenuItem*) item
{
if ( [ item action ] == @selector( loadAllFlash: ) ) {
return [ self _atLeastOneFlashViewExists ];
}
else if( [ item action ] == @selector( loadKeyWindowFlash: ) ) {
return [ self _flashViewExistsForKeyWindow ];
}
else if( [ item action ] == @selector(loadKeyWindowInvisibleFlash: ) ) {
return [ self _invisibleFlashViewExistsForKeyWindow ];
}

return returnValue;
return YES;
}

#pragma mark -
Expand Down Expand Up @@ -408,6 +302,4 @@ - (IBAction) showSettingsWindow: (id) sender
[ _whitelistWindowController showWindow: sender ];
}

@synthesize flashViews = _flashViews;

@end
10 changes: 2 additions & 8 deletions Plugin/CTFWhitelistWindowController.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@


@interface CTFWhitelistWindowController : NSWindowController {
IBOutlet NSArrayController *_controller;
// NSMutableArray *_sites;
IBOutlet NSArrayController *_controller;
}
//
//- (IBAction) addWhitelistSite: (id) sender;
//- (IBAction) removeWhitelistSite: (id) sender;
//
//- (void) saveWhitelist: (id) sender;
//- (void) whitelistChanged: (NSNotification *) note;

@end

Loading

0 comments on commit 16b844c

Please sign in to comment.