WebScriptObject.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. #ifndef WebScriptObject_h
  26. #define WebScriptObject_h
  27. #import <Foundation/Foundation.h>
  28. #import <JavaScriptCore/JSBase.h>
  29. #import <JavaScriptCore/WebKitAvailability.h>
  30. #if WEBKIT_VERSION_MAX_ALLOWED >= WEBKIT_VERSION_1_3
  31. // NSObject (WebScripting) -----------------------------------------------------
  32. /*
  33. Classes may implement one or more methods in WebScripting to export interfaces
  34. to WebKit's JavaScript environment.
  35. By default, no properties or functions are exported. A class must implement
  36. +isKeyExcludedFromWebScript: and/or +isSelectorExcludedFromWebScript: to
  37. expose selected properties and methods, respectively, to JavaScript.
  38. Access to exported properties is done using KVC -- specifically, the following
  39. KVC methods:
  40. - (void)setValue:(id)value forKey:(NSString *)key
  41. - (id)valueForKey:(NSString *)key
  42. Clients may also intercept property set/get operations that are made by the
  43. scripting environment for properties that are not exported. This is done using
  44. the KVC methods:
  45. - (void)setValue:(id)value forUndefinedKey:(NSString *)key
  46. - (id)valueForUndefinedKey:(NSString *)key
  47. Similarly, clients may intercept method invocations that are made by the
  48. scripting environment for methods that are not exported. This is done using
  49. the method:
  50. - (id)invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)args;
  51. If clients need to raise an exception in the script environment
  52. they can call [WebScriptObject throwException:]. Note that throwing an
  53. exception using this method will only succeed if the method that throws the exception
  54. is being called within the scope of a script invocation.
  55. Not all methods are exposed. Only those methods whose parameters and return
  56. type meets the export criteria are exposed. Valid types are Objective-C instances
  57. and scalars. Other types are not allowed.
  58. Types will be converted automatically between JavaScript and Objective-C in
  59. the following manner:
  60. JavaScript ObjC
  61. ---------- ----------
  62. null => nil
  63. undefined => WebUndefined
  64. number => NSNumber
  65. boolean => CFBoolean
  66. string => NSString
  67. object => id
  68. The object => id conversion occurs as follows: if the object wraps an underlying
  69. Objective-C object (i.e., if it was created by a previous ObjC => JavaScript conversion),
  70. then the underlying Objective-C object is returned. Otherwise, a new WebScriptObject
  71. is created and returned.
  72. The above conversions occur only if the declared ObjC type is an object type.
  73. For primitive types like int and char, a numeric cast is performed.
  74. ObjC JavaScript
  75. ---- ----------
  76. NSNull => null
  77. nil => undefined
  78. WebUndefined => undefined
  79. CFBoolean => boolean
  80. NSNumber => number
  81. NSString => string
  82. NSArray => array object
  83. WebScriptObject => object
  84. The above conversions occur only if the declared ObjC type is an object type.
  85. For primitive type like int and char, a numeric cast is performed.
  86. */
  87. @interface NSObject (WebScripting)
  88. /*!
  89. @method webScriptNameForSelector:
  90. @param selector The selector that will be exposed to the script environment.
  91. @discussion Use the returned string as the exported name for the selector
  92. in the script environment. It is the responsibility of the class to ensure
  93. uniqueness of the returned name. If nil is returned or this
  94. method is not implemented the default name for the selector will
  95. be used. The default name concatenates the components of the
  96. Objective-C selector name and replaces ':' with '_'. '_' characters
  97. are escaped with an additional '$', i.e. '_' becomes "$_". '$' are
  98. also escaped, i.e.
  99. Objective-C name Default script name
  100. moveTo:: move__
  101. moveTo_ moveTo$_
  102. moveTo$_ moveTo$$$_
  103. @result Returns the name to be used to represent the specified selector in the
  104. scripting environment.
  105. */
  106. + (NSString *)webScriptNameForSelector:(SEL)selector;
  107. /*!
  108. @method isSelectorExcludedFromWebScript:
  109. @param selector The selector the will be exposed to the script environment.
  110. @discussion Return NO to export the selector to the script environment.
  111. Return YES to prevent the selector from being exported to the script environment.
  112. If this method is not implemented on the class no selectors will be exported.
  113. @result Returns YES to hide the selector, NO to export the selector.
  114. */
  115. + (BOOL)isSelectorExcludedFromWebScript:(SEL)selector;
  116. /*!
  117. @method webScriptNameForKey:
  118. @param name The name of the instance variable that will be exposed to the
  119. script environment. Only instance variables that meet the export criteria will
  120. be exposed.
  121. @discussion Provide an alternate name for a property.
  122. @result Returns the name to be used to represent the specified property in the
  123. scripting environment.
  124. */
  125. + (NSString *)webScriptNameForKey:(const char *)name;
  126. /*!
  127. @method isKeyExcludedFromWebScript:
  128. @param name The name of the instance variable that will be exposed to the
  129. script environment.
  130. @discussion Return NO to export the property to the script environment.
  131. Return YES to prevent the property from being exported to the script environment.
  132. @result Returns YES to hide the property, NO to export the property.
  133. */
  134. + (BOOL)isKeyExcludedFromWebScript:(const char *)name;
  135. /*!
  136. @method invokeUndefinedMethodFromWebScript:withArguments:
  137. @param name The name of the method to invoke.
  138. @param arguments The arguments to pass the method.
  139. @discussion If a script attempts to invoke a method that is not exported,
  140. invokeUndefinedMethodFromWebScript:withArguments: will be called.
  141. @result The return value of the invocation. The value will be converted as appropriate
  142. for the script environment.
  143. */
  144. - (id)invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)arguments;
  145. /*!
  146. @method invokeDefaultMethodWithArguments:
  147. @param arguments The arguments to pass the method.
  148. @discussion If a script attempts to call an exposed object as a function,
  149. this method will be called.
  150. @result The return value of the call. The value will be converted as appropriate
  151. for the script environment.
  152. */
  153. - (id)invokeDefaultMethodWithArguments:(NSArray *)arguments;
  154. /*!
  155. @method finalizeForWebScript
  156. @discussion finalizeForScript is called on objects exposed to the script
  157. environment just before the script environment garbage collects the object.
  158. Subsequently, any references to WebScriptObjects made by the exposed object will
  159. be invalid and have undefined consequences.
  160. */
  161. - (void)finalizeForWebScript;
  162. @end
  163. // WebScriptObject --------------------------------------------------
  164. @class JSValue;
  165. @class WebScriptObjectPrivate;
  166. @class WebFrame;
  167. /*!
  168. @class WebScriptObject
  169. @discussion WebScriptObjects are used to wrap script objects passed from
  170. script environments to Objective-C. WebScriptObjects cannot be created
  171. directly. In normal uses of WebKit, you gain access to the script
  172. environment using the "windowScriptObject" method on WebView.
  173. The following KVC methods are commonly used to access properties of the
  174. WebScriptObject:
  175. - (void)setValue:(id)value forKey:(NSString *)key
  176. - (id)valueForKey:(NSString *)key
  177. As it possible to remove attributes from web script objects, the following
  178. additional method augments the basic KVC methods:
  179. - (void)removeWebScriptKey:(NSString *)name;
  180. Also, since the sparse array access allowed in script objects doesn't map well
  181. to NSArray, the following methods can be used to access index based properties:
  182. - (id)webScriptValueAtIndex:(unsigned)index;
  183. - (void)setWebScriptValueAtIndex:(unsigned)index value:(id)value;
  184. */
  185. @interface WebScriptObject : NSObject
  186. {
  187. WebScriptObjectPrivate *_private;
  188. }
  189. /*!
  190. @method throwException:
  191. @discussion Throws an exception in the current script execution context.
  192. @result Either NO if an exception could not be raised, YES otherwise.
  193. */
  194. + (BOOL)throwException:(NSString *)exceptionMessage;
  195. /*!
  196. @method JSObject
  197. @result The equivalent JSObjectRef for this WebScriptObject.
  198. @discussion Use this method to bridge between the WebScriptObject and
  199. JavaScriptCore APIs.
  200. */
  201. - (JSObjectRef)JSObject WEBKIT_OBJC_METHOD_ANNOTATION(AVAILABLE_WEBKIT_VERSION_3_0_AND_LATER);
  202. /*!
  203. @method callWebScriptMethod:withArguments:
  204. @param name The name of the method to call in the script environment.
  205. @param arguments The arguments to pass to the script environment.
  206. @discussion Calls the specified method in the script environment using the
  207. specified arguments.
  208. @result Returns the result of calling the script method.
  209. Returns WebUndefined when an exception is thrown in the script environment.
  210. */
  211. - (id)callWebScriptMethod:(NSString *)name withArguments:(NSArray *)arguments;
  212. /*!
  213. @method evaluateWebScript:
  214. @param script The script to execute in the target script environment.
  215. @discussion The script will be executed in the target script environment. The format
  216. of the script is dependent of the target script environment.
  217. @result Returns the result of evaluating the script in the script environment.
  218. Returns WebUndefined when an exception is thrown in the script environment.
  219. */
  220. - (id)evaluateWebScript:(NSString *)script;
  221. /*!
  222. @method removeWebScriptKey:
  223. @param name The name of the property to remove.
  224. @discussion Removes the property from the object in the script environment.
  225. */
  226. - (void)removeWebScriptKey:(NSString *)name;
  227. /*!
  228. @method stringRepresentation
  229. @discussion Converts the target object to a string representation. The coercion
  230. of non string objects type is dependent on the script environment.
  231. @result Returns the string representation of the object.
  232. */
  233. - (NSString *)stringRepresentation;
  234. /*!
  235. @method webScriptValueAtIndex:
  236. @param index The index of the property to return.
  237. @discussion Gets the value of the property at the specified index.
  238. @result The value of the property. Returns WebUndefined when an exception is
  239. thrown in the script environment.
  240. */
  241. - (id)webScriptValueAtIndex:(unsigned)index;
  242. /*!
  243. @method setWebScriptValueAtIndex:value:
  244. @param index The index of the property to set.
  245. @param value The value of the property to set.
  246. @discussion Sets the property value at the specified index.
  247. */
  248. - (void)setWebScriptValueAtIndex:(unsigned)index value:(id)value;
  249. /*!
  250. @method setException:
  251. @param description The description of the exception.
  252. @discussion Raises an exception in the script environment in the context of the
  253. current object.
  254. */
  255. - (void)setException:(NSString *)description;
  256. #if JSC_OBJC_API_ENABLED
  257. /*!
  258. @method JSValue
  259. @result The equivalent Objective-C JSValue for this WebScriptObject.
  260. @discussion Use this method to bridge between the WebScriptObject and
  261. JavaScriptCore Objective-C APIs.
  262. */
  263. - (JSValue *)JSValue;
  264. #endif
  265. @end
  266. // WebUndefined --------------------------------------------------------------
  267. /*!
  268. @class WebUndefined
  269. */
  270. @interface WebUndefined : NSObject <NSCoding, NSCopying>
  271. /*!
  272. @method undefined
  273. @result The WebUndefined shared instance.
  274. */
  275. + (WebUndefined *)undefined;
  276. @end
  277. #endif
  278. #endif // WebScriptObject_h