WebElementDictionary.mm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (C) 2006, 2007, 2008, 2010 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. #import "WebElementDictionary.h"
  29. #import "DOMNodeInternal.h"
  30. #import "WebDOMOperations.h"
  31. #import "WebFrame.h"
  32. #import "WebFrameInternal.h"
  33. #import "WebKitLogging.h"
  34. #import "WebTypesInternal.h"
  35. #import "WebView.h"
  36. #import "WebViewPrivate.h"
  37. #import <WebCore/Frame.h>
  38. #import <WebCore/HitTestResult.h>
  39. #import <WebCore/Image.h>
  40. #import <WebCore/RunLoop.h>
  41. #import <WebCore/WebCoreObjCExtras.h>
  42. #import <WebKit/DOMCore.h>
  43. #import <WebKit/DOMExtensions.h>
  44. #import <runtime/InitializeThreading.h>
  45. #import <wtf/MainThread.h>
  46. using namespace WebCore;
  47. static CFMutableDictionaryRef lookupTable = NULL;
  48. static void addLookupKey(NSString *key, SEL selector)
  49. {
  50. CFDictionaryAddValue(lookupTable, key, selector);
  51. }
  52. static void cacheValueForKey(const void *key, const void *value, void *self)
  53. {
  54. // calling objectForKey will cache the value in our _cache dictionary
  55. [(WebElementDictionary *)self objectForKey:(NSString *)key];
  56. }
  57. @implementation WebElementDictionary
  58. + (void)initialize
  59. {
  60. JSC::initializeThreading();
  61. WTF::initializeMainThreadToProcessMainThread();
  62. WebCore::RunLoop::initializeMainRunLoop();
  63. WebCoreObjCFinalizeOnMainThread(self);
  64. }
  65. + (void)initializeLookupTable
  66. {
  67. if (lookupTable)
  68. return;
  69. lookupTable = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFCopyStringDictionaryKeyCallBacks, NULL);
  70. addLookupKey(WebElementDOMNodeKey, @selector(_domNode));
  71. addLookupKey(WebElementFrameKey, @selector(_webFrame));
  72. addLookupKey(WebElementImageAltStringKey, @selector(_altDisplayString));
  73. addLookupKey(WebElementImageKey, @selector(_image));
  74. addLookupKey(WebElementImageRectKey, @selector(_imageRect));
  75. addLookupKey(WebElementImageURLKey, @selector(_absoluteImageURL));
  76. addLookupKey(WebElementIsSelectedKey, @selector(_isSelected));
  77. addLookupKey(WebElementMediaURLKey, @selector(_absoluteMediaURL));
  78. addLookupKey(WebElementSpellingToolTipKey, @selector(_spellingToolTip));
  79. addLookupKey(WebElementTitleKey, @selector(_title));
  80. addLookupKey(WebElementLinkURLKey, @selector(_absoluteLinkURL));
  81. addLookupKey(WebElementLinkTargetFrameKey, @selector(_targetWebFrame));
  82. addLookupKey(WebElementLinkTitleKey, @selector(_titleDisplayString));
  83. addLookupKey(WebElementLinkLabelKey, @selector(_textContent));
  84. addLookupKey(WebElementLinkIsLiveKey, @selector(_isLiveLink));
  85. addLookupKey(WebElementIsContentEditableKey, @selector(_isContentEditable));
  86. addLookupKey(WebElementIsInScrollBarKey, @selector(_isInScrollBar));
  87. }
  88. - (id)initWithHitTestResult:(const HitTestResult&)result
  89. {
  90. [[self class] initializeLookupTable];
  91. self = [super init];
  92. if (!self)
  93. return nil;
  94. _result = new HitTestResult(result);
  95. return self;
  96. }
  97. - (void)dealloc
  98. {
  99. if (WebCoreObjCScheduleDeallocateOnMainThread([WebElementDictionary class], self))
  100. return;
  101. delete _result;
  102. [_cache release];
  103. [_nilValues release];
  104. [super dealloc];
  105. }
  106. - (void)finalize
  107. {
  108. ASSERT_MAIN_THREAD();
  109. delete _result;
  110. [super finalize];
  111. }
  112. - (void)_fillCache
  113. {
  114. CFDictionaryApplyFunction(lookupTable, cacheValueForKey, self);
  115. _cacheComplete = YES;
  116. }
  117. - (NSUInteger)count
  118. {
  119. if (!_cacheComplete)
  120. [self _fillCache];
  121. return [_cache count];
  122. }
  123. - (NSEnumerator *)keyEnumerator
  124. {
  125. if (!_cacheComplete)
  126. [self _fillCache];
  127. return [_cache keyEnumerator];
  128. }
  129. - (id)objectForKey:(id)key
  130. {
  131. id value = [_cache objectForKey:key];
  132. if (value || _cacheComplete || [_nilValues containsObject:key])
  133. return value;
  134. SEL selector = (SEL)CFDictionaryGetValue(lookupTable, key);
  135. if (!selector)
  136. return nil;
  137. value = [self performSelector:selector];
  138. unsigned lookupTableCount = CFDictionaryGetCount(lookupTable);
  139. if (value) {
  140. if (!_cache)
  141. _cache = [[NSMutableDictionary alloc] initWithCapacity:lookupTableCount];
  142. [_cache setObject:value forKey:key];
  143. } else {
  144. if (!_nilValues)
  145. _nilValues = [[NSMutableSet alloc] initWithCapacity:lookupTableCount];
  146. [_nilValues addObject:key];
  147. }
  148. _cacheComplete = ([_cache count] + [_nilValues count]) == lookupTableCount;
  149. return value;
  150. }
  151. - (DOMNode *)_domNode
  152. {
  153. return kit(_result->innerNonSharedNode());
  154. }
  155. - (WebFrame *)_webFrame
  156. {
  157. return [[[self _domNode] ownerDocument] webFrame];
  158. }
  159. // String's NSString* operator converts null Strings to empty NSStrings for compatibility
  160. // with AppKit. We need to work around that here.
  161. static NSString* NSStringOrNil(String coreString)
  162. {
  163. if (coreString.isNull())
  164. return nil;
  165. return coreString;
  166. }
  167. - (NSString *)_altDisplayString
  168. {
  169. return NSStringOrNil(_result->altDisplayString());
  170. }
  171. - (NSString *)_spellingToolTip
  172. {
  173. TextDirection dir;
  174. return NSStringOrNil(_result->spellingToolTip(dir));
  175. }
  176. - (NSImage *)_image
  177. {
  178. Image* image = _result->image();
  179. return image ? image->getNSImage() : nil;
  180. }
  181. - (NSValue *)_imageRect
  182. {
  183. IntRect rect = _result->imageRect();
  184. return rect.isEmpty() ? nil : [NSValue valueWithRect:rect];
  185. }
  186. - (NSURL *)_absoluteImageURL
  187. {
  188. return _result->absoluteImageURL();
  189. }
  190. - (NSURL *)_absoluteMediaURL
  191. {
  192. return _result->absoluteMediaURL();
  193. }
  194. - (NSNumber *)_isSelected
  195. {
  196. return [NSNumber numberWithBool:_result->isSelected()];
  197. }
  198. - (NSString *)_title
  199. {
  200. TextDirection dir;
  201. return NSStringOrNil(_result->title(dir));
  202. }
  203. - (NSURL *)_absoluteLinkURL
  204. {
  205. return _result->absoluteLinkURL();
  206. }
  207. - (WebFrame *)_targetWebFrame
  208. {
  209. return kit(_result->targetFrame());
  210. }
  211. - (NSString *)_titleDisplayString
  212. {
  213. return NSStringOrNil(_result->titleDisplayString());
  214. }
  215. - (NSString *)_textContent
  216. {
  217. return NSStringOrNil(_result->textContent());
  218. }
  219. - (NSNumber *)_isLiveLink
  220. {
  221. return [NSNumber numberWithBool:_result->isLiveLink()];
  222. }
  223. - (NSNumber *)_isContentEditable
  224. {
  225. return [NSNumber numberWithBool:_result->isContentEditable()];
  226. }
  227. - (NSNumber *)_isInScrollBar
  228. {
  229. return [NSNumber numberWithBool:_result->scrollbar() != 0];
  230. }
  231. @end