/
CTFMenubarMenuController.m
413 lines (299 loc) · 11.5 KB
/
CTFMenubarMenuController.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
The MIT License
Copyright (c) 2008-2009 Click to Flash Developers
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#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;
static NSString* kApplicationsToInstallMenuInto[] = {
@"com.apple.Safari",
nil
};
static NSMenu* appMenu()
{
return [ [ [ NSApp mainMenu ] itemAtIndex: 0 ] submenu ];
}
@implementation CTFMenubarMenuController
#pragma mark -
#pragma mark Main menu item setup
- (BOOL) shouldLoadMainMenuItemIntoCurrentProcess
{
NSBundle* appBundle = [ NSBundle mainBundle ];
NSString* currentAppId = [ appBundle bundleIdentifier ];
if( [ appBundle objectForInfoDictionaryKey: @"ClickToFlashPrefsAppMenuItemIndex" ] != nil )
return YES;
int i;
NSString* appId = kApplicationsToInstallMenuInto[ 0 ];
for( i = 0 ; appId != nil ; appId = kApplicationsToInstallMenuInto[ ++i ] ) {
if( [ appId isEqualToString: currentAppId ] )
return YES;
}
return NO;
}
- (int) applicationMenuPrefsInsertionLocation
{
NSBundle* appBundle = [ NSBundle mainBundle ];
NSNumber* indx = [ appBundle objectForInfoDictionaryKey: @"ClickToFlashPrefsAppMenuItemIndex" ];
if( indx )
return [ indx intValue ];
NSMenu* applicationMenu = appMenu();
int insertLocation = -1, showPrefsItem = -1, lastSeenSep = -1;
int i, count = [ applicationMenu numberOfItems ];
for( i = 0 ; i < count ; ++i ) {
// Put it before the first separator after the preferences item.
NSMenuItem* item = [ applicationMenu itemAtIndex: i ];
if( [ item action ] == @selector( showPreferences: ) )
showPrefsItem = i;
if( showPrefsItem >= 0 && [ item isSeparatorItem ] ) {
insertLocation = i;
break;
}
}
if( insertLocation == -1 ) {
if( showPrefsItem >= 0 )
insertLocation = showPrefsItem + 1;
else
insertLocation = 4; // didn't find it, assume it's item 3 (the default for most apps)
}
return insertLocation;
}
#pragma mark -
#pragma mark Lifetime management
- (id) init
{
if( sSingleton ) {
[ self release ];
return sSingleton;
}
self = [ super init ];
sSingleton = self;
if( self ) {
if( ! [ NSBundle loadNibNamed: @"MenubarMenu" owner: self ] )
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;
}
- (void) dealloc
{
[ _whitelistWindowController release ];
[ super dealloc ];
}
- (void) awakeFromNib
{
if( ![ self shouldLoadMainMenuItemIntoCurrentProcess ] )
return;
if( !menu ) {
NSLog( @"ClickToFlash: Could not load menubar menu" );
return;
}
// We need a submenu item to wrap this loaded menu:
NSMenuItem* ctfMenuItem = [ [ [ NSMenuItem alloc ] initWithTitle: [ menu title ]
action: nil
keyEquivalent: @"" ] autorelease ];
[ ctfMenuItem setSubmenu: menu ];
// Find the location to insert the item:
int insertLocation = [ self applicationMenuPrefsInsertionLocation ];
// Insert the submenu there:
NSMenu* applicationMenu = appMenu();
[ applicationMenu insertItem: ctfMenuItem atIndex: insertLocation ];
}
+ (CTFMenubarMenuController*) sharedController
{
if( !sSingleton )
[ [ CTFMenubarMenuController alloc ] init ];
return sSingleton;
}
#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
- (void) loadFlashForWindow: (NSWindow*) window
{
[ [ NSNotificationCenter defaultCenter ] postNotificationName: kCTFLoadFlashViewsForWindow
object: window ];
}
- (void) loadInvisibleFlashForWindow: (NSWindow*) window
{
[ [ NSNotificationCenter defaultCenter ] postNotificationName: kCTFLoadInvisibleFlashViewsForWindow
object: window ];
}
- (IBAction) loadAllFlash: (id) sender
{
[ [ NSNotificationCenter defaultCenter ] postNotificationName: kCTFLoadAllFlashViews
object: self ];
}
- (IBAction) loadKeyWindowFlash: (id) sender
{
NSWindow* window = [ NSApp keyWindow ];
if( window )
[ self loadFlashForWindow: window ];
}
- (IBAction) loadKeyWindowInvisibleFlash: (id) sender
{
NSWindow* window = [ NSApp keyWindow ];
if( window )
[ self loadInvisibleFlashForWindow: window ];
}
- (IBAction) showSettingsWindow: (id) sender
{
if( _whitelistWindowController == nil )
_whitelistWindowController = [ [ CTFWhitelistWindowController alloc ] init ];
[ _whitelistWindowController showWindow: sender ];
}
@synthesize flashViews = _flashViews;
@end