forked from onflapp/gs-webbrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChromeController.m
171 lines (138 loc) · 4.36 KB
/
ChromeController.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
#import "ChromeController.h"
static LocalFileServer* fileServer = nil;
static ChromeController* chromeController = nil;
@implementation ChromeController
- (id) init {
self = [super init];
NSString* config = [NSStandardLibraryPaths() firstObject];
appname = [[[[NSBundle mainBundle] bundlePath] lastPathComponent] stringByDeletingPathExtension];
[appname retain];
running = YES;
pidfile = [config stringByAppendingPathComponent:appname];
pidfile = [pidfile stringByAppendingPathComponent:@"controller.pid"];
[pidfile retain];
currentDelegate = nil;
delegates = [[NSMutableArray alloc] init];
return self;
}
+ (ChromeController*) sharedInstance {
if (!chromeController) chromeController = [[ChromeController alloc] init];
return chromeController;
}
- (void) dealloc {
chromeController = nil;
currentDelegate = nil;
[delegates release];
delegates = nil;
[task release];
task = nil;
[appname release];
appname = nil;
[pidfile release];
pidfile = nil;
running = NO;
NSLog(@"dealloc controller");
[super dealloc];
}
- (void) ensureChromeControllerIsReady:(ChromeControllerDelegate*) del {
if (fileServer == nil) {
fileServer = [[LocalFileServer alloc] init];
[fileServer start];
}
if (!pidfile || !running) return;
if (currentDelegate == nil) {
NSLog(@"first delegate trying to connect");
currentDelegate = del;
}
else if (currentDelegate != del) {
NSLog(@"other delegate is trying to connect, queue");
[delegates addObject:del];
return;
}
NSInteger p = [self processPort];
if (p > 0) {
NSLog(@"try to connect to %d", p);
NSFileHandle* remote = [NSFileHandle fileHandleAsClientAtAddress:@"localhost" service:[NSString stringWithFormat:@"%ld", p] protocol:@"tcp"];
if (remote) {
NSLog(@"connected");
[del chromeController:self isReady:remote];
currentDelegate = nil;
if ([delegates count] > 0) {
id ndel = [delegates objectAtIndex:0];
if (ndel) {
[delegates removeObjectAtIndex:0];
[self performSelector:@selector(ensureChromeControllerIsReady:) withObject: ndel afterDelay:1.0];
}
}
}
else {
NSLog(@"did not connect, try again");
[self launchProcess];
[self performSelector:@selector(ensureChromeControllerIsReady:) withObject: del afterDelay:1.0];
}
}
else {
[self launchProcess];
NSLog(@"try again");
[self performSelector:@selector(ensureChromeControllerIsReady:) withObject: del afterDelay:1.0];
}
}
- (NSInteger) processPort {
NSString* str = [NSString stringWithContentsOfFile:pidfile];
NSLog(@">>>> %@ [%@]", pidfile, str);
if (!str) return -1;
else {
return [str integerValue];
}
}
- (void) stopTrying {
running = NO;
}
- (NSInteger) fileServerPort {
return [fileServer serverPort];
}
- (void) launchProcess {
NSString* wp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"webview"];
NSString* path = [wp stringByAppendingPathComponent:@"start.sh"];
task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/bash"];
[task setArguments:[NSArray arrayWithObjects:path, appname, nil]];
[task setCurrentDirectoryPath:wp];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(taskDidTerminate:)
name:NSTaskDidTerminateNotification
object:task];
[task launch];
}
- (void) taskDidTerminate:(NSNotification*) not {
NSInteger rv = [task terminationStatus];
NSLog(@"task has terminated %d", rv);
if (rv == 10) {
NSRunAlertPanel(@"Unable to start the chrome process",@"Web Browser app expects to find one of these commands:\n google-chrome, chromium, chromium-browser or chrome",nil,nil,nil);
running = NO;
}
[task release];
task = nil;
}
- (void) sendCommand:(NSString*) cmd {
if (!pidfile || !running) return;
NSInteger p = [self processPort];
if (p > 0) {
NSFileHandle* remote = [NSFileHandle fileHandleAsClientAtAddress:@"localhost" service:[NSString stringWithFormat:@"%ld", p] protocol:@"tcp"];
if (remote) {
NSString* ss = [NSString stringWithFormat:@"%@\n", cmd];
NSData* data = [ss dataUsingEncoding:NSUTF8StringEncoding];
[remote writeData:data];
[remote closeFile];
}
}
}
- (void) showDebugWindow {
[self sendCommand:@"SHOW_DEBUG:"];
}
- (void) closeProcess {
NSLog(@"terminate!");
[self sendCommand:@"TERMINATE:"];
}
@end