|
1 /* |
|
2 * Copyright (C) 2007 Apple Inc. All rights reserved. |
|
3 * |
|
4 * Redistribution and use in source and binary forms, with or without |
|
5 * modification, are permitted provided that the following conditions |
|
6 * are met: |
|
7 * |
|
8 * 1. Redistributions of source code must retain the above copyright |
|
9 * notice, this list of conditions and the following disclaimer. |
|
10 * 2. Redistributions in binary form must reproduce the above copyright |
|
11 * notice, this list of conditions and the following disclaimer in the |
|
12 * documentation and/or other materials provided with the distribution. |
|
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of |
|
14 * its contributors may be used to endorse or promote products derived |
|
15 * from this software without specific prior written permission. |
|
16 * |
|
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
|
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
|
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
|
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #import "ObjCController.h" |
|
30 |
|
31 #import <JavaScriptCore/Assertions.h> |
|
32 #import <WebKit/WebScriptObject.h> |
|
33 #import <WebKit/WebView.h> |
|
34 #import <WebKit/DOMAbstractView.h> |
|
35 |
|
36 @implementation ObjCController |
|
37 |
|
38 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector |
|
39 { |
|
40 if (0 |
|
41 || aSelector == @selector(classNameOf:) |
|
42 || aSelector == @selector(objectOfClass:) |
|
43 || aSelector == @selector(identityIsEqual::) |
|
44 || aSelector == @selector(longLongRoundTrip:) |
|
45 || aSelector == @selector(unsignedLongLongRoundTrip:) |
|
46 || aSelector == @selector(testWrapperRoundTripping:) |
|
47 || aSelector == @selector(accessStoredWebScriptObject) |
|
48 || aSelector == @selector(storeWebScriptObject:) |
|
49 ) |
|
50 return NO; |
|
51 return YES; |
|
52 } |
|
53 |
|
54 + (NSString *)webScriptNameForSelector:(SEL)aSelector |
|
55 { |
|
56 if (aSelector == @selector(classNameOf:)) |
|
57 return @"className"; |
|
58 if (aSelector == @selector(objectOfClass:)) |
|
59 return @"objectOfClass"; |
|
60 if (aSelector == @selector(identityIsEqual::)) |
|
61 return @"identityIsEqual"; |
|
62 if (aSelector == @selector(longLongRoundTrip:)) |
|
63 return @"longLongRoundTrip"; |
|
64 if (aSelector == @selector(unsignedLongLongRoundTrip:)) |
|
65 return @"unsignedLongLongRoundTrip"; |
|
66 if (aSelector == @selector(testWrapperRoundTripping:)) |
|
67 return @"testWrapperRoundTripping"; |
|
68 if (aSelector == @selector(storeWebScriptObject:)) |
|
69 return @"storeWebScriptObject"; |
|
70 |
|
71 return nil; |
|
72 } |
|
73 |
|
74 - (NSString *)classNameOf:(id)object |
|
75 { |
|
76 if (!object) |
|
77 return @"nil"; |
|
78 return NSStringFromClass([object class]); |
|
79 } |
|
80 |
|
81 - (id)objectOfClass:(NSString *)aClass |
|
82 { |
|
83 if ([aClass isEqualToString:@"NSNull"]) |
|
84 return [NSNull null]; |
|
85 if ([aClass isEqualToString:@"WebUndefined"]) |
|
86 return [WebUndefined undefined]; |
|
87 if ([aClass isEqualToString:@"NSCFBoolean"]) |
|
88 return [NSNumber numberWithBool:true]; |
|
89 if ([aClass isEqualToString:@"NSCFNumber"]) |
|
90 return [NSNumber numberWithInt:1]; |
|
91 if ([aClass isEqualToString:@"NSCFString"]) |
|
92 return @""; |
|
93 if ([aClass isEqualToString:@"WebScriptObject"]) |
|
94 return self; |
|
95 if ([aClass isEqualToString:@"NSArray"]) |
|
96 return [NSArray array]; |
|
97 |
|
98 return nil; |
|
99 } |
|
100 |
|
101 - (BOOL)identityIsEqual:(WebScriptObject *)a :(WebScriptObject *)b |
|
102 { |
|
103 return a == b; |
|
104 } |
|
105 |
|
106 - (long long)longLongRoundTrip:(long long)num |
|
107 { |
|
108 return num; |
|
109 } |
|
110 |
|
111 - (unsigned long long)unsignedLongLongRoundTrip:(unsigned long long)num |
|
112 { |
|
113 return num; |
|
114 } |
|
115 |
|
116 - (BOOL)testWrapperRoundTripping:(WebScriptObject *)webScriptObject |
|
117 { |
|
118 JSObjectRef jsObject = [webScriptObject JSObject]; |
|
119 |
|
120 if (!jsObject) |
|
121 return false; |
|
122 |
|
123 if (!webScriptObject) |
|
124 return false; |
|
125 |
|
126 if ([[webScriptObject evaluateWebScript:@"({ })"] class] != [webScriptObject class]) |
|
127 return false; |
|
128 |
|
129 [webScriptObject setValue:[NSNumber numberWithInt:666] forKey:@"key"]; |
|
130 if (![[webScriptObject valueForKey:@"key"] isKindOfClass:[NSNumber class]] || |
|
131 ![[webScriptObject valueForKey:@"key"] isEqualToNumber:[NSNumber numberWithInt:666]]) |
|
132 return false; |
|
133 |
|
134 [webScriptObject removeWebScriptKey:@"key"]; |
|
135 @try { |
|
136 if ([webScriptObject valueForKey:@"key"]) |
|
137 return false; |
|
138 } @catch(NSException *exception) { |
|
139 // NSObject throws an exception if the key doesn't exist. |
|
140 } |
|
141 |
|
142 [webScriptObject setWebScriptValueAtIndex:0 value:webScriptObject]; |
|
143 if ([webScriptObject webScriptValueAtIndex:0] != webScriptObject) |
|
144 return false; |
|
145 |
|
146 if ([[webScriptObject stringRepresentation] isEqualToString:@"[Object object]"]) |
|
147 return false; |
|
148 |
|
149 if ([webScriptObject callWebScriptMethod:@"returnThis" withArguments:nil] != webScriptObject) |
|
150 return false; |
|
151 |
|
152 return true; |
|
153 } |
|
154 |
|
155 - (void)accessStoredWebScriptObject |
|
156 { |
|
157 #if !ASSERT_DISABLED |
|
158 BOOL isWindowObject = [storedWebScriptObject isKindOfClass:[DOMAbstractView class]]; |
|
159 #endif |
|
160 JSObjectRef jsObject = [storedWebScriptObject JSObject]; |
|
161 ASSERT((jsObject && isWindowObject) || (!jsObject && !isWindowObject)); |
|
162 |
|
163 [storedWebScriptObject callWebScriptMethod:@"" withArguments:nil]; |
|
164 [storedWebScriptObject evaluateWebScript:@""]; |
|
165 [storedWebScriptObject setValue:[WebUndefined undefined] forKey:@"key"]; |
|
166 [storedWebScriptObject valueForKey:@"key"]; |
|
167 [storedWebScriptObject removeWebScriptKey:@"key"]; |
|
168 [storedWebScriptObject stringRepresentation]; |
|
169 [storedWebScriptObject webScriptValueAtIndex:0]; |
|
170 [storedWebScriptObject setWebScriptValueAtIndex:0 value:[WebUndefined undefined]]; |
|
171 [storedWebScriptObject setException:@"exception"]; |
|
172 } |
|
173 |
|
174 - (void)storeWebScriptObject:(WebScriptObject *)webScriptObject |
|
175 { |
|
176 if (webScriptObject == storedWebScriptObject) |
|
177 return; |
|
178 |
|
179 [storedWebScriptObject release]; |
|
180 storedWebScriptObject = [webScriptObject retain]; |
|
181 } |
|
182 |
|
183 - (void)dealloc |
|
184 { |
|
185 [storedWebScriptObject release]; |
|
186 [super dealloc]; |
|
187 } |
|
188 |
|
189 - (id)invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)args |
|
190 { |
|
191 // FIXME: Perhaps we should log that this has been called. |
|
192 return nil; |
|
193 } |
|
194 |
|
195 @end |