1/* 2 * Copyright (C) 2004, 2006, 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 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#ifndef WebScriptObject_h 27#define WebScriptObject_h 28 29#import <Foundation/Foundation.h> 30#import <JavaScriptCore/JSBase.h> 31#import <JavaScriptCore/WebKitAvailability.h> 32 33#if WEBKIT_VERSION_MAX_ALLOWED >= WEBKIT_VERSION_1_3 34 35// NSObject (WebScripting) ----------------------------------------------------- 36 37/* 38 Classes may implement one or more methods in WebScripting to export interfaces 39 to WebKit's JavaScript environment. 40 41 By default, no properties or functions are exported. A class must implement 42 +isKeyExcludedFromWebScript: and/or +isSelectorExcludedFromWebScript: to 43 expose selected properties and methods, respectively, to JavaScript. 44 45 Access to exported properties is done using KVC -- specifically, the following 46 KVC methods: 47 48 - (void)setValue:(id)value forKey:(NSString *)key 49 - (id)valueForKey:(NSString *)key 50 51 Clients may also intercept property set/get operations that are made by the 52 scripting environment for properties that are not exported. This is done using 53 the KVC methods: 54 55 - (void)setValue:(id)value forUndefinedKey:(NSString *)key 56 - (id)valueForUndefinedKey:(NSString *)key 57 58 Similarly, clients may intercept method invocations that are made by the 59 scripting environment for methods that are not exported. This is done using 60 the method: 61 62 - (id)invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)args; 63 64 If clients need to raise an exception in the script environment 65 they can call [WebScriptObject throwException:]. Note that throwing an 66 exception using this method will only succeed if the method that throws the exception 67 is being called within the scope of a script invocation. 68 69 Not all methods are exposed. Only those methods whose parameters and return 70 type meets the export criteria are exposed. Valid types are Objective-C instances 71 and scalars. Other types are not allowed. 72 73 Types will be converted automatically between JavaScript and Objective-C in 74 the following manner: 75 76 JavaScript ObjC 77 ---------- ---------- 78 null => nil 79 undefined => WebUndefined 80 number => NSNumber 81 boolean => CFBoolean 82 string => NSString 83 object => id 84 85 The object => id conversion occurs as follows: if the object wraps an underlying 86 Objective-C object (i.e., if it was created by a previous ObjC => JavaScript conversion), 87 then the underlying Objective-C object is returned. Otherwise, a new WebScriptObject 88 is created and returned. 89 90 The above conversions occur only if the declared ObjC type is an object type. 91 For primitive types like int and char, a numeric cast is performed. 92 93 ObjC JavaScript 94 ---- ---------- 95 NSNull => null 96 nil => undefined 97 WebUndefined => undefined 98 CFBoolean => boolean 99 NSNumber => number 100 NSString => string 101 NSArray => array object 102 WebScriptObject => object 103 104 The above conversions occur only if the declared ObjC type is an object type. 105 For primitive type like int and char, a numeric cast is performed. 106*/ 107@interface NSObject (WebScripting) 108 109/*! 110 @method webScriptNameForSelector: 111 @param selector The selector that will be exposed to the script environment. 112 @discussion Use the returned string as the exported name for the selector 113 in the script environment. It is the responsibility of the class to ensure 114 uniqueness of the returned name. If nil is returned or this 115 method is not implemented the default name for the selector will 116 be used. The default name concatenates the components of the 117 Objective-C selector name and replaces ':' with '_'. '_' characters 118 are escaped with an additional '$', i.e. '_' becomes "$_". '$' are 119 also escaped, i.e. 120 Objective-C name Default script name 121 moveTo:: move__ 122 moveTo_ moveTo$_ 123 moveTo$_ moveTo$$$_ 124 @result Returns the name to be used to represent the specified selector in the 125 scripting environment. 126*/ 127+ (NSString *)webScriptNameForSelector:(SEL)selector; 128 129/*! 130 @method isSelectorExcludedFromWebScript: 131 @param selector The selector the will be exposed to the script environment. 132 @discussion Return NO to export the selector to the script environment. 133 Return YES to prevent the selector from being exported to the script environment. 134 If this method is not implemented on the class no selectors will be exported. 135 @result Returns YES to hide the selector, NO to export the selector. 136*/ 137+ (BOOL)isSelectorExcludedFromWebScript:(SEL)selector; 138 139/*! 140 @method webScriptNameForKey: 141 @param name The name of the instance variable that will be exposed to the 142 script environment. Only instance variables that meet the export criteria will 143 be exposed. 144 @discussion Provide an alternate name for a property. 145 @result Returns the name to be used to represent the specified property in the 146 scripting environment. 147*/ 148+ (NSString *)webScriptNameForKey:(const char *)name; 149 150/*! 151 @method isKeyExcludedFromWebScript: 152 @param name The name of the instance variable that will be exposed to the 153 script environment. 154 @discussion Return NO to export the property to the script environment. 155 Return YES to prevent the property from being exported to the script environment. 156 @result Returns YES to hide the property, NO to export the property. 157*/ 158+ (BOOL)isKeyExcludedFromWebScript:(const char *)name; 159 160/*! 161 @method invokeUndefinedMethodFromWebScript:withArguments: 162 @param name The name of the method to invoke. 163 @param arguments The arguments to pass the method. 164 @discussion If a script attempts to invoke a method that is not exported, 165 invokeUndefinedMethodFromWebScript:withArguments: will be called. 166 @result The return value of the invocation. The value will be converted as appropriate 167 for the script environment. 168*/ 169- (id)invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)arguments; 170 171/*! 172 @method invokeDefaultMethodWithArguments: 173 @param arguments The arguments to pass the method. 174 @discussion If a script attempts to call an exposed object as a function, 175 this method will be called. 176 @result The return value of the call. The value will be converted as appropriate 177 for the script environment. 178*/ 179- (id)invokeDefaultMethodWithArguments:(NSArray *)arguments; 180 181/*! 182 @method finalizeForWebScript 183 @discussion finalizeForScript is called on objects exposed to the script 184 environment just before the script environment garbage collects the object. 185 Subsequently, any references to WebScriptObjects made by the exposed object will 186 be invalid and have undefined consequences. 187*/ 188- (void)finalizeForWebScript; 189 190@end 191 192 193// WebScriptObject -------------------------------------------------- 194 195@class JSValue; 196@class WebScriptObjectPrivate; 197@class WebFrame; 198 199/*! 200 @class WebScriptObject 201 @discussion WebScriptObjects are used to wrap script objects passed from 202 script environments to Objective-C. WebScriptObjects cannot be created 203 directly. In normal uses of WebKit, you gain access to the script 204 environment using the "windowScriptObject" method on WebView. 205 206 The following KVC methods are commonly used to access properties of the 207 WebScriptObject: 208 209 - (void)setValue:(id)value forKey:(NSString *)key 210 - (id)valueForKey:(NSString *)key 211 212 As it possible to remove attributes from web script objects, the following 213 additional method augments the basic KVC methods: 214 215 - (void)removeWebScriptKey:(NSString *)name; 216 217 Also, since the sparse array access allowed in script objects doesn't map well 218 to NSArray, the following methods can be used to access index based properties: 219 220 - (id)webScriptValueAtIndex:(unsigned)index; 221 - (void)setWebScriptValueAtIndex:(unsigned)index value:(id)value; 222*/ 223@interface WebScriptObject : NSObject 224{ 225 WebScriptObjectPrivate *_private; 226} 227 228/*! 229 @method throwException: 230 @discussion Throws an exception in the current script execution context. 231 @result Either NO if an exception could not be raised, YES otherwise. 232*/ 233+ (BOOL)throwException:(NSString *)exceptionMessage; 234 235/*! 236 @method JSObject 237 @result The equivalent JSObjectRef for this WebScriptObject. 238 @discussion Use this method to bridge between the WebScriptObject and 239 JavaScriptCore APIs. 240*/ 241- (JSObjectRef)JSObject WEBKIT_OBJC_METHOD_ANNOTATION(AVAILABLE_WEBKIT_VERSION_3_0_AND_LATER); 242 243/*! 244 @method callWebScriptMethod:withArguments: 245 @param name The name of the method to call in the script environment. 246 @param arguments The arguments to pass to the script environment. 247 @discussion Calls the specified method in the script environment using the 248 specified arguments. 249 @result Returns the result of calling the script method. 250 Returns WebUndefined when an exception is thrown in the script environment. 251*/ 252- (id)callWebScriptMethod:(NSString *)name withArguments:(NSArray *)arguments; 253 254/*! 255 @method evaluateWebScript: 256 @param script The script to execute in the target script environment. 257 @discussion The script will be executed in the target script environment. The format 258 of the script is dependent of the target script environment. 259 @result Returns the result of evaluating the script in the script environment. 260 Returns WebUndefined when an exception is thrown in the script environment. 261*/ 262- (id)evaluateWebScript:(NSString *)script; 263 264/*! 265 @method removeWebScriptKey: 266 @param name The name of the property to remove. 267 @discussion Removes the property from the object in the script environment. 268*/ 269- (void)removeWebScriptKey:(NSString *)name; 270 271/*! 272 @method stringRepresentation 273 @discussion Converts the target object to a string representation. The coercion 274 of non string objects type is dependent on the script environment. 275 @result Returns the string representation of the object. 276*/ 277- (NSString *)stringRepresentation; 278 279/*! 280 @method webScriptValueAtIndex: 281 @param index The index of the property to return. 282 @discussion Gets the value of the property at the specified index. 283 @result The value of the property. Returns WebUndefined when an exception is 284 thrown in the script environment. 285*/ 286- (id)webScriptValueAtIndex:(unsigned)index; 287 288/*! 289 @method setWebScriptValueAtIndex:value: 290 @param index The index of the property to set. 291 @param value The value of the property to set. 292 @discussion Sets the property value at the specified index. 293*/ 294- (void)setWebScriptValueAtIndex:(unsigned)index value:(id)value; 295 296/*! 297 @method setException: 298 @param description The description of the exception. 299 @discussion Raises an exception in the script environment in the context of the 300 current object. 301*/ 302- (void)setException:(NSString *)description; 303 304 305#if JSC_OBJC_API_ENABLED 306/*! 307 @method JSValue 308 @result The equivalent Objective-C JSValue for this WebScriptObject. 309 @discussion Use this method to bridge between the WebScriptObject and 310 JavaScriptCore Objective-C APIs. 311*/ 312- (JSValue *)JSValue; 313#endif 314 315@end 316 317 318// WebUndefined -------------------------------------------------------------- 319 320/*! 321 @class WebUndefined 322*/ 323@interface WebUndefined : NSObject <NSCoding, NSCopying> 324 325/*! 326 @method undefined 327 @result The WebUndefined shared instance. 328*/ 329+ (WebUndefined *)undefined; 330 331@end 332 333#endif 334 335#endif // WebScriptObject_h 336