-
Notifications
You must be signed in to change notification settings - Fork 18
/
FirefoxExtension.m
executable file
·177 lines (141 loc) · 5.59 KB
/
FirefoxExtension.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
//
// FirefoxExtension.m
// Frenzy
//
// Created by John Winter on 30/07/10.
// Copyright 2010 Aptonic Software. All rights reserved.
//
#import "FirefoxExtension.h"
NSString *const ExtensionName = @"[email protected]";
@implementation FirefoxExtension
- (FirefoxExtension *)init
{
self = [super init];
if (self) {
firefoxDir = [[NSString alloc] initWithString:[@"~/Library/Application Support/Firefox/Profiles" stringByExpandingTildeInPath]];
}
return self;
}
- (BOOL)isFirefoxInstalled
{
// If there is a ~/Library/Application Support/Firefox/Profiles folder then it is, otherwise not
if ([[NSFileManager defaultManager] fileExistsAtPath:firefoxDir])
return YES;
else
return NO;
}
- (void)installExtension
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *err = nil;
// Check Firefox is closed and if not prompt the user to close it
ProcessRunning *procRunning = [[ProcessRunning alloc] init];
[procRunning obtainFreshProcessList];
BOOL isRunning = [procRunning findProcessWithName:@"firefox-bin"];
int result;
if (isRunning) {
result = NSRunAlertPanel(@"Firefox is Running",
@"You have to quit the Firefox browser to continue.",
@"Ask Firefox to quit", @"Cancel update",@"Continue", nil);
if (result == NSAlertDefaultReturn) {
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:@"tell application \"Firefox\" to quit"];
[script executeAndReturnError:nil];
[script release];
} else if (result == NSAlertOtherReturn) {
[self installExtension];
return;
} else if (result == NSAlertAlternateReturn) {
return;
}
}
// Copy Frenzy.app/Contents/Extensions/ to ~/Library/Application Support/Frenzy/
NSArray *searchpaths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *destinationExtensionsFolder;
if ([searchpaths count] > 0) {
// Look for the library folder (and create if not there)
NSString *path = [[searchpaths objectAtIndex:0] stringByAppendingPathComponent:@"Frenzy"];
if (![fileManager fileExistsAtPath:path]) {
err = nil;
if (![fileManager createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:&err]) {
[self showError:err withDescription:@"Failed to create Frenzy folder in Application Support"];
return;
}
}
NSString *extensionsPath = [[[NSBundle mainBundle] bundlePath]
stringByAppendingPathComponent:@"Contents/Extensions"];
destinationExtensionsFolder = [path stringByAppendingPathComponent:@"Extensions"];
// Remove the Extensions folder
if ([fileManager fileExistsAtPath:destinationExtensionsFolder]) {
err = nil;
if (![fileManager removeItemAtPath:destinationExtensionsFolder error:&err])
NSLog(@"WARNING: Failed to remove old Extensions folder at path %@\n%@", destinationExtensionsFolder, [err description]);
}
if ([fileManager fileExistsAtPath:extensionsPath]) {
err = nil;
if (![fileManager copyItemAtPath:extensionsPath toPath:destinationExtensionsFolder error:&err]) {
[self showError:err withDescription:@"Failed to copy Extensions folder from application bundle to Application Support folder"];
return;
}
} else {
[self showError:nil withDescription:[@"Could not find extension to install in " stringByAppendingString:extensionsPath]];
return;
}
}
// Find the users Firefox profile directory by getting the first directory in
// ~/Library/Application Support/Firefox/Profiles/ that has a suffix of default
// and contains an extensions folder
NSString *filename;
NSString *profileDirExtensionPath;
err = nil;
NSArray *profileDirs = [fileManager contentsOfDirectoryAtPath:firefoxDir error:&err];
if (!profileDirs) {
[self showError:err withDescription:[@"Failed to get contents of Firefox profile directory at\n"
stringByAppendingPathComponent:firefoxDir]];
return;
}
BOOL profileDirFound = NO;
for (filename in profileDirs) {
profileDirExtensionPath = [[firefoxDir stringByAppendingPathComponent:filename]
stringByAppendingPathComponent:@"extensions"];
if ([filename characterAtIndex:0] == '.')
continue;
if ([filename hasSuffix:@"default"] && [fileManager fileExistsAtPath:profileDirExtensionPath]) {
profileDirFound = YES;
break;
}
}
NSString *extensionPath = [destinationExtensionsFolder stringByAppendingPathComponent:ExtensionName];
if (!profileDirFound) {
[self showError:nil withDescription:@"Firefox profile directory not found"];
return;
}
NSString *extensionFileToWrite = [profileDirExtensionPath stringByAppendingPathComponent:ExtensionName];
err = nil;
BOOL writeResult = [extensionPath writeToFile:extensionFileToWrite
atomically:YES encoding:NSUTF8StringEncoding error:&err];
if (!writeResult) {
[self showError:err withDescription:[@"Failed to write extension file to\n"
stringByAppendingPathComponent:extensionFileToWrite]];
return;
}
[procRunning release];
}
- (void)showError:(NSError *)error withDescription:(NSString *)aDescription
{
NSString *finalErrorString;
NSString *errorString = [NSString stringWithFormat:@"\n\n%@", aDescription];
// Tack on NSError description if provided
if (error != nil)
finalErrorString = [NSString stringWithFormat:@"%@\n\n%@", errorString, [error description]];
else
finalErrorString = errorString;
NSRunCriticalAlertPanel(@"Extension Installation Failed",
[@"The Firefox extension failed to install." stringByAppendingString:finalErrorString],
@"OK", nil, nil);
}
- (void)dealloc
{
[firefoxDir release];
[super dealloc];
}
@end