UIDelegate.mm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (C) 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. *
  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 "config.h"
  29. #import "UIDelegate.h"
  30. #import "DumpRenderTree.h"
  31. #import "DumpRenderTreeDraggingInfo.h"
  32. #import "EventSendingController.h"
  33. #import "MockWebNotificationProvider.h"
  34. #import "TestRunner.h"
  35. #import <WebKit/WebApplicationCache.h>
  36. #import <WebKit/WebFramePrivate.h>
  37. #import <WebKit/WebHTMLViewPrivate.h>
  38. #import <WebKit/WebQuotaManager.h>
  39. #import <WebKit/WebSecurityOriginPrivate.h>
  40. #import <WebKit/WebUIDelegatePrivate.h>
  41. #import <WebKit/WebView.h>
  42. #import <WebKit/WebViewPrivate.h>
  43. #import <wtf/Assertions.h>
  44. DumpRenderTreeDraggingInfo *draggingInfo = nil;
  45. @implementation UIDelegate
  46. - (void)webView:(WebView *)sender setFrame:(NSRect)frame
  47. {
  48. m_frame = frame;
  49. }
  50. - (NSRect)webViewFrame:(WebView *)sender
  51. {
  52. return m_frame;
  53. }
  54. - (void)webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary withSource:(NSString *)source
  55. {
  56. NSString *message = [dictionary objectForKey:@"message"];
  57. NSNumber *lineNumber = [dictionary objectForKey:@"lineNumber"];
  58. NSRange range = [message rangeOfString:@"file://"];
  59. if (range.location != NSNotFound)
  60. message = [[message substringToIndex:range.location] stringByAppendingString:[[message substringFromIndex:NSMaxRange(range)] lastPathComponent]];
  61. printf ("CONSOLE MESSAGE: ");
  62. if ([lineNumber intValue])
  63. printf ("line %d: ", [lineNumber intValue]);
  64. printf ("%s\n", [message UTF8String]);
  65. }
  66. - (void)modalWindowWillClose:(NSNotification *)notification
  67. {
  68. [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowWillCloseNotification object:nil];
  69. [NSApp abortModal];
  70. }
  71. - (void)webViewRunModal:(WebView *)sender
  72. {
  73. gTestRunner->setWindowIsKey(false);
  74. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(modalWindowWillClose:) name:NSWindowWillCloseNotification object:nil];
  75. [NSApp runModalForWindow:[sender window]];
  76. gTestRunner->setWindowIsKey(true);
  77. }
  78. - (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame
  79. {
  80. if (!done) {
  81. printf("ALERT: %s\n", [message UTF8String]);
  82. fflush(stdout);
  83. }
  84. }
  85. - (BOOL)webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame
  86. {
  87. if (!done)
  88. printf("CONFIRM: %s\n", [message UTF8String]);
  89. return YES;
  90. }
  91. - (NSString *)webView:(WebView *)sender runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WebFrame *)frame
  92. {
  93. if (!done)
  94. printf("PROMPT: %s, default text: %s\n", [prompt UTF8String], [defaultText UTF8String]);
  95. return defaultText;
  96. }
  97. - (BOOL)webView:(WebView *)c runBeforeUnloadConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame
  98. {
  99. if (!done)
  100. printf("CONFIRM NAVIGATION: %s\n", [message UTF8String]);
  101. return !gTestRunner->shouldStayOnPageAfterHandlingBeforeUnload();
  102. }
  103. - (void)webView:(WebView *)sender dragImage:(NSImage *)anImage at:(NSPoint)viewLocation offset:(NSSize)initialOffset event:(NSEvent *)event pasteboard:(NSPasteboard *)pboard source:(id)sourceObj slideBack:(BOOL)slideFlag forView:(NSView *)view
  104. {
  105. assert(!draggingInfo);
  106. draggingInfo = [[DumpRenderTreeDraggingInfo alloc] initWithImage:anImage offset:initialOffset pasteboard:pboard source:sourceObj];
  107. [sender draggingUpdated:draggingInfo];
  108. [EventSendingController replaySavedEvents];
  109. }
  110. - (void)webViewFocus:(WebView *)webView
  111. {
  112. gTestRunner->setWindowIsKey(true);
  113. }
  114. - (void)webViewUnfocus:(WebView *)webView
  115. {
  116. gTestRunner->setWindowIsKey(false);
  117. }
  118. - (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request
  119. {
  120. if (!gTestRunner->canOpenWindows())
  121. return nil;
  122. // Make sure that waitUntilDone has been called.
  123. ASSERT(gTestRunner->waitToDump());
  124. WebView *webView = createWebViewAndOffscreenWindow();
  125. if (gTestRunner->newWindowsCopyBackForwardList())
  126. [webView _loadBackForwardListFromOtherView:sender];
  127. return [webView autorelease];
  128. }
  129. - (void)webViewClose:(WebView *)sender
  130. {
  131. NSWindow* window = [sender window];
  132. if (gTestRunner->callCloseOnWebViews())
  133. [sender close];
  134. [window close];
  135. }
  136. - (void)webView:(WebView *)sender frame:(WebFrame *)frame exceededDatabaseQuotaForSecurityOrigin:(WebSecurityOrigin *)origin database:(NSString *)databaseIdentifier
  137. {
  138. if (!done && gTestRunner->dumpDatabaseCallbacks()) {
  139. printf("UI DELEGATE DATABASE CALLBACK: exceededDatabaseQuotaForSecurityOrigin:{%s, %s, %i} database:%s\n", [[origin protocol] UTF8String], [[origin host] UTF8String],
  140. [origin port], [databaseIdentifier UTF8String]);
  141. }
  142. static const unsigned long long defaultQuota = 5 * 1024 * 1024;
  143. [[origin databaseQuotaManager] setQuota:defaultQuota];
  144. }
  145. - (void)webView:(WebView *)sender exceededApplicationCacheOriginQuotaForSecurityOrigin:(WebSecurityOrigin *)origin totalSpaceNeeded:(NSUInteger)totalSpaceNeeded
  146. {
  147. if (!done && gTestRunner->dumpApplicationCacheDelegateCallbacks()) {
  148. // For example, numbers from 30000 - 39999 will output as 30000.
  149. // Rounding up or down not really matter for these tests. It's
  150. // sufficient to just get a range of 10000 to determine if we were
  151. // above or below a threshold.
  152. unsigned long truncatedSpaceNeeded = static_cast<unsigned long>((totalSpaceNeeded / 10000) * 10000);
  153. printf("UI DELEGATE APPLICATION CACHE CALLBACK: exceededApplicationCacheOriginQuotaForSecurityOrigin:{%s, %s, %i} totalSpaceNeeded:~%lu\n",
  154. [[origin protocol] UTF8String], [[origin host] UTF8String], [origin port], truncatedSpaceNeeded);
  155. }
  156. if (gTestRunner->disallowIncreaseForApplicationCacheQuota())
  157. return;
  158. static const unsigned long long defaultOriginQuota = [WebApplicationCache defaultOriginQuota];
  159. [[origin applicationCacheQuotaManager] setQuota:defaultOriginQuota];
  160. }
  161. - (void)webView:(WebView *)sender setStatusText:(NSString *)text
  162. {
  163. if (gTestRunner->dumpStatusCallbacks())
  164. printf("UI DELEGATE STATUS CALLBACK: setStatusText:%s\n", [text UTF8String]);
  165. }
  166. - (void)webView:(WebView *)webView decidePolicyForGeolocationRequestFromOrigin:(WebSecurityOrigin *)origin frame:(WebFrame *)frame listener:(id<WebAllowDenyPolicyListener>)listener
  167. {
  168. if (!gTestRunner->isGeolocationPermissionSet()) {
  169. if (!m_pendingGeolocationPermissionListeners)
  170. m_pendingGeolocationPermissionListeners = [[NSMutableSet set] retain];
  171. [m_pendingGeolocationPermissionListeners addObject:listener];
  172. return;
  173. }
  174. if (gTestRunner->geolocationPermission())
  175. [listener allow];
  176. else
  177. [listener deny];
  178. }
  179. - (void)didSetMockGeolocationPermission
  180. {
  181. ASSERT(gTestRunner->isGeolocationPermissionSet());
  182. if (m_pendingGeolocationPermissionListeners && !m_timer)
  183. m_timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(timerFired) userInfo:0 repeats:NO];
  184. }
  185. - (int)numberOfPendingGeolocationPermissionRequests
  186. {
  187. if (!m_pendingGeolocationPermissionListeners)
  188. return 0;
  189. return [m_pendingGeolocationPermissionListeners count];
  190. }
  191. - (void)timerFired
  192. {
  193. ASSERT(gTestRunner->isGeolocationPermissionSet());
  194. m_timer = 0;
  195. NSEnumerator* enumerator = [m_pendingGeolocationPermissionListeners objectEnumerator];
  196. id<WebAllowDenyPolicyListener> listener;
  197. while ((listener = [enumerator nextObject])) {
  198. if (gTestRunner->geolocationPermission())
  199. [listener allow];
  200. else
  201. [listener deny];
  202. }
  203. [m_pendingGeolocationPermissionListeners removeAllObjects];
  204. [m_pendingGeolocationPermissionListeners release];
  205. m_pendingGeolocationPermissionListeners = nil;
  206. }
  207. - (BOOL)webView:(WebView *)sender shouldHaltPlugin:(DOMNode *)pluginNode
  208. {
  209. return NO;
  210. }
  211. - (BOOL)webView:(WebView *)webView supportsFullScreenForElement:(DOMElement*)element withKeyboard:(BOOL)withKeyboard
  212. {
  213. return YES;
  214. }
  215. - (void)enterFullScreenWithListener:(NSObject<WebKitFullScreenListener>*)listener
  216. {
  217. [listener webkitWillEnterFullScreen];
  218. [listener webkitDidEnterFullScreen];
  219. }
  220. - (void)webView:(WebView *)webView enterFullScreenForElement:(DOMElement*)element listener:(NSObject<WebKitFullScreenListener>*)listener
  221. {
  222. if (!gTestRunner->hasCustomFullScreenBehavior())
  223. [self performSelector:@selector(enterFullScreenWithListener:) withObject:listener afterDelay:0];
  224. }
  225. - (void)exitFullScreenWithListener:(NSObject<WebKitFullScreenListener>*)listener
  226. {
  227. [listener webkitWillExitFullScreen];
  228. [listener webkitDidExitFullScreen];
  229. }
  230. - (void)webView:(WebView *)webView exitFullScreenForElement:(DOMElement*)element listener:(NSObject<WebKitFullScreenListener>*)listener
  231. {
  232. if (!gTestRunner->hasCustomFullScreenBehavior())
  233. [self performSelector:@selector(exitFullScreenWithListener:) withObject:listener afterDelay:0];
  234. }
  235. - (void)webView:(WebView *)sender closeFullScreenWithListener:(NSObject<WebKitFullScreenListener>*)listener
  236. {
  237. [listener webkitWillExitFullScreen];
  238. [listener webkitDidExitFullScreen];
  239. }
  240. - (BOOL)webView:(WebView *)webView didPressMissingPluginButton:(DOMElement *)element
  241. {
  242. printf("MISSING PLUGIN BUTTON PRESSED\n");
  243. return TRUE;
  244. }
  245. - (void)webView:(WebView *)webView decidePolicyForNotificationRequestFromOrigin:(WebSecurityOrigin *)origin listener:(id<WebAllowDenyPolicyListener>)listener
  246. {
  247. MockWebNotificationProvider *provider = (MockWebNotificationProvider *)[webView _notificationProvider];
  248. switch ([provider policyForOrigin:origin]) {
  249. case WebNotificationPermissionAllowed:
  250. [listener allow];
  251. break;
  252. case WebNotificationPermissionDenied:
  253. [listener deny];
  254. break;
  255. case WebNotificationPermissionNotAllowed:
  256. [provider setWebNotificationOrigin:[origin stringValue] permission:YES];
  257. [listener allow];
  258. break;
  259. }
  260. }
  261. - (void)dealloc
  262. {
  263. [draggingInfo release];
  264. draggingInfo = nil;
  265. [m_pendingGeolocationPermissionListeners release];
  266. m_pendingGeolocationPermissionListeners = nil;
  267. [super dealloc];
  268. }
  269. @end