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
|
#include "AEGetURLEventHandler.h"
#include <jvmti.h>
#include <stdlib.h>
#import <Foundation/Foundation.h>
@interface AEGetURLEventHandler : NSObject
{
jobject _listener;
char *_url;
JavaVM *_vm;
}
+ (AEGetURLEventHandler *)sharedAEGetURLEventHandler;
- (void)handleAEGetURLEvent:(NSAppleEventDescriptor *)event
withReplyEvent:(NSAppleEventDescriptor *)replyEvent;
- (void)setAEGetURLListener:(jobject)listener jniEnv:(JNIEnv *)jniEnv;
- (void)setURL:(char *)url;
- (void)setVM:(JavaVM *)vm;
- (void)tryFireAEGetURLEvent;
@end
static AEGetURLEventHandler *g_sharedAEGetURLEventHandler = NULL;
@implementation AEGetURLEventHandler
+ (AEGetURLEventHandler *)sharedAEGetURLEventHandler {
if (!g_sharedAEGetURLEventHandler) {
g_sharedAEGetURLEventHandler = [[AEGetURLEventHandler alloc] init];
}
return g_sharedAEGetURLEventHandler;
}
- (void)dealloc {
[self setURL:NULL];
[self setAEGetURLListener:NULL jniEnv:NULL];
[super dealloc];
}
- (void)handleAEGetURLEvent:(NSAppleEventDescriptor *)event
withReplyEvent:(NSAppleEventDescriptor *)replyEvent {
NSAutoreleasePool *autoreleasePool = [[NSAutoreleasePool alloc] init];
NSString *str;
str = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
if (str) {
const NSStringEncoding encoding = NSNonLossyASCIIStringEncoding;
NSUInteger length;
char *chars;
length = [str lengthOfBytesUsingEncoding:encoding];
if (length) {
length++; // Account for the NULL termination byte.
chars = (char *) malloc (length * sizeof (char));
if (chars
&& (YES == [str getCString:chars maxLength:length encoding:encoding])) {
[[AEGetURLEventHandler sharedAEGetURLEventHandler]
setURL:chars];
} else {
free (chars);
}
}
}
[autoreleasePool release];
}
- (AEGetURLEventHandler *)init {
self = [super init];
if (self) {
_listener = NULL;
_url = NULL;
_vm = NULL;
}
return self;
}
- (void)setAEGetURLListener:(jobject)listener jniEnv:(JNIEnv *)jniEnv {
if (!jniEnv
&& (!_vm || (JNI_OK != (*_vm)->GetEnv (_vm, (void **) &jniEnv, JNI_VERSION_1_2)))) {
return; // TODO Don't swallow the failure.
}
if (_listener) {
(*jniEnv)->DeleteGlobalRef (jniEnv, _listener);
_listener = NULL;
}
if (listener) {
_listener = (*jniEnv)->NewGlobalRef (jniEnv, listener);
[self tryFireAEGetURLEvent];
}
}
- (void)setURL:(char *)url {
if (_url != url) {
if (_url) {
free (_url);
}
_url = url;
[self tryFireAEGetURLEvent];
}
}
- (void)setVM:(JavaVM *)vm {
_vm = vm;
}
- (void)tryFireAEGetURLEvent {
JNIEnv *jniEnv;
if (_vm
&& _listener
&& _url
&& (JNI_OK == (*_vm)->GetEnv (_vm, (void **) &jniEnv, JNI_VERSION_1_2))) {
jclass clazz = (*jniEnv)->GetObjectClass (jniEnv, _listener);
jmethodID methodID =
(*jniEnv)->GetMethodID (
jniEnv, clazz, "handleAEGetURLEvent", "(Ljava/lang/String;)V");
if (methodID) {
jstring url = (*jniEnv)->NewStringUTF (jniEnv, _url);
if (url) {
(*jniEnv)->CallVoidMethod (jniEnv, _listener, methodID, url);
/*
* The URL should not be reported again after it has been
* handled.
*/
[self setURL:NULL];
}
}
}
}
@end
JNIEXPORT jint JNICALL
Agent_OnLoad (JavaVM *vm, char *options, void *reserved) {
NSAutoreleasePool *autoreleasePool = [[NSAutoreleasePool alloc] init];
AEGetURLEventHandler *aeGetURLEventHandler
= [AEGetURLEventHandler sharedAEGetURLEventHandler];
[aeGetURLEventHandler setVM:vm];
[[NSAppleEventManager sharedAppleEventManager]
setEventHandler:aeGetURLEventHandler
andSelector:@selector(handleAEGetURLEvent:withReplyEvent:)
forEventClass:kInternetEventClass
andEventID:kAEGetURL];
[autoreleasePool release];
/*
* Non-zero will terminate the VM and we don't want that even if we fail to
* install the event handler.
*/
return 0;
}
JNIEXPORT void JNICALL
Java_net_java_sip_communicator_util_launchutils_AEGetURLEventHandler_setAEGetURLListener
(JNIEnv *jniEnv, jclass clazz, jobject listener) {
NSAutoreleasePool *autoreleasePool = [[NSAutoreleasePool alloc] init];
[[AEGetURLEventHandler sharedAEGetURLEventHandler]
setAEGetURLListener:listener jniEnv:jniEnv];
[autoreleasePool release];
}
|