Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/rentzsch/clicktoflash
Browse files Browse the repository at this point in the history
Conflicts:
	Plugin/English.lproj/WhitelistPanel.xib
  • Loading branch information
Otyr Ugla committed Feb 13, 2009
2 parents 1da9679 + cc72537 commit c12886a
Show file tree
Hide file tree
Showing 8 changed files with 776 additions and 16 deletions.
5 changes: 5 additions & 0 deletions ClickToFlash.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
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 @@ -85,6 +86,7 @@
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 @@ -103,6 +105,7 @@
files = (
55EB70580E04A8B80016593D /* Cocoa.framework in Frameworks */,
55EB70590E04A8B80016593D /* WebKit.framework in Frameworks */,
A4501BC80F4439FB00D04D61 /* OSAKit.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -161,6 +164,7 @@
children = (
55EB70560E04A8B80016593D /* Cocoa.framework */,
55EB70570E04A8B80016593D /* WebKit.framework */,
A4501BC70F4439FB00D04D61 /* OSAKit.framework */,
);
name = Frameworks;
sourceTree = "<group>";
Expand Down Expand Up @@ -437,6 +441,7 @@
GCC_MODEL_TUNING = G5;
GCC_PRECOMPILE_PREFIX_HEADER = NO;
GCC_PREFIX_HEADER = "";
GCC_PREPROCESSOR_DEFINITIONS = "";
INFOPLIST_FILE = "Plugin/Info-Plugin.plist";
INSTALL_PATH = "$(HOME)/Library/Bundles";
OTHER_LDFLAGS = (
Expand Down
8 changes: 8 additions & 0 deletions Plugin/CTFMenubarMenuController.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,27 @@ THE SOFTWARE.

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;
}

@property (retain) NSMutableDictionary *flashViews;

+ (CTFMenubarMenuController*) sharedController;

- (void) loadFlashForWindow: (NSWindow*) window;

- (IBAction) loadAllFlash: (id) sender;
- (IBAction) loadKeyWindowFlash: (id) sender;
- (IBAction) loadKeyWindowInvisibleFlash: (id) sender;
- (IBAction) showSettingsWindow: (id) sender;

@end
200 changes: 199 additions & 1 deletion Plugin/CTFMenubarMenuController.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ 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>


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

static CTFMenubarMenuController* sSingleton = nil;

Expand Down Expand Up @@ -127,6 +132,17 @@ - (id) init
NSLog( @"ClickToFlash: Could not load menubar menu nib" );
}

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;
}

Expand Down Expand Up @@ -177,6 +193,172 @@ + (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
{
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];
}
}

- (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;
{
NSLog(@"%@",[self flashViews]);
return ([[[self flashViews] allKeys] count] >= 1);
}


- (BOOL) _flashViewExistsForKeyWindow;
{
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);
}

- (BOOL) _invisibleFlashViewExistsForKeyWindow;
{
BOOL returnValue = NO;
NSString *baseURL = [self _baseURLOfKeyWindow];

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

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;
break;
}
}
}

return returnValue;
}

- (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];
}

return returnValue;
}

#pragma mark -
#pragma mark Actions

Expand All @@ -188,6 +370,13 @@ - (void) loadFlashForWindow: (NSWindow*) window
}


- (void) loadInvisibleFlashForWindow: (NSWindow*) window
{
[ [ NSNotificationCenter defaultCenter ] postNotificationName: kCTFLoadInvisibleFlashViewsForWindow
object: window ];
}


- (IBAction) loadAllFlash: (id) sender
{
[ [ NSNotificationCenter defaultCenter ] postNotificationName: kCTFLoadAllFlashViews
Expand All @@ -203,6 +392,14 @@ - (IBAction) loadKeyWindowFlash: (id) sender
}


- (IBAction) loadKeyWindowInvisibleFlash: (id) sender
{
NSWindow* window = [ NSApp keyWindow ];
if( window )
[ self loadInvisibleFlashForWindow: window ];
}


- (IBAction) showSettingsWindow: (id) sender
{
if( _whitelistWindowController == nil )
Expand All @@ -211,5 +408,6 @@ - (IBAction) showSettingsWindow: (id) sender
[ _whitelistWindowController showWindow: sender ];
}

@synthesize flashViews = _flashViews;

@end
Loading

0 comments on commit c12886a

Please sign in to comment.