2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2006 James G. Speth (speth@end.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #import "ObjCPlugin.h"
28 #import <objc/objc-runtime.h>
31 // === NSObject category to expose almost everything to JavaScript ===
33 // Warning: this class introduces huge security weaknesses, and should only be used
34 // for testing inside of DumpRenderTree, and only with trusted code. By default, it has
35 // the same restrictive behavior as the standard WebKit setup. However, scripts can use the
36 // plugin's removeBridgeRestrictions: method to open up almost total access to the Cocoa
39 static BOOL _allowsScriptsFullAccess = NO;
41 @interface NSObject (ObjCScriptAccess)
43 + (void)setAllowsScriptsFullAccess:(BOOL)value;
44 + (BOOL)allowsScriptsFullAccess;
48 @implementation NSObject (ObjCScriptAccess)
50 + (void)setAllowsScriptsFullAccess:(BOOL)value
52 _allowsScriptsFullAccess = value;
55 + (BOOL)allowsScriptsFullAccess
57 return _allowsScriptsFullAccess;
60 + (BOOL)isSelectorExcludedFromWebScript:(SEL)selector
62 return !_allowsScriptsFullAccess;
65 + (NSString *)webScriptNameForSelector:(SEL)selector
72 @interface JSObjC : NSObject {
75 // expose some useful objc functions to the scripting environment
76 - (id)lookUpClass:(NSString *)name;
77 - (void)log:(NSString *)message;
78 - (id)retainObject:(id)obj;
79 - (id)classOfObject:(id)obj;
80 - (NSString *)classNameOfObject:(id)obj;
84 @implementation JSObjC
86 + (BOOL)isSelectorExcludedFromWebScript:(SEL)selector
91 + (NSString *)webScriptNameForSelector:(SEL)selector
96 - (id)invokeDefaultMethodWithArguments:(NSArray *)args
98 // this is a useful shortcut for accessing objective-c classes from the scripting
99 // environment, e.g. 'var myObject = objc("NSObject").alloc().init();'
100 if ([args count] == 1)
101 return [self lookUpClass:[args objectAtIndex:0]];
105 - (id)lookUpClass:(NSString *)name
107 return NSClassFromString(name);
110 - (void)log:(NSString *)message
115 - (id)retainObject:(id)obj
120 - (id)classOfObject:(id)obj
122 return (id)[obj class];
125 - (NSString *)classNameOfObject:(id)obj
127 return [obj className];
132 @implementation ObjCPlugin
134 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
136 if (aSelector == @selector(removeBridgeRestrictions:))
139 if (aSelector == @selector(echo:))
145 + (NSString *)webScriptNameForSelector:(SEL)aSelector
147 if (aSelector == @selector(echo:))
153 - (void)removeBridgeRestrictions:(id)container
155 // let scripts invoke any selector
156 [NSObject setAllowsScriptsFullAccess:YES];
158 // store a JSObjC instance into the provided container
159 JSObjC *objc = [[JSObjC alloc] init];
160 [container setValue:objc forKey:@"objc"];