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
29#import "config.h"
30#import "UIDelegate.h"
31
32#import "DumpRenderTree.h"
33#import "DumpRenderTreeDraggingInfo.h"
34#import "EventSendingController.h"
35#import "LayoutTestController.h"
36#import <WebKit/WebFramePrivate.h>
37#import <WebKit/WebHTMLViewPrivate.h>
38#import <WebKit/WebSecurityOriginPrivate.h>
39#import <WebKit/WebUIDelegatePrivate.h>
40#import <WebKit/WebView.h>
41#import <WebKit/WebViewPrivate.h>
42#import <wtf/Assertions.h>
43
44DumpRenderTreeDraggingInfo *draggingInfo = nil;
45
46@implementation UIDelegate
47
48- (void)webView:(WebView *)sender setFrame:(NSRect)frame
49{
50    m_frame = frame;
51}
52
53- (NSRect)webViewFrame:(WebView *)sender
54{
55    return m_frame;
56}
57
58- (void)webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary
59{
60    NSString *message = [dictionary objectForKey:@"message"];
61    NSNumber *lineNumber = [dictionary objectForKey:@"lineNumber"];
62
63    NSRange range = [message rangeOfString:@"file://"];
64    if (range.location != NSNotFound)
65        message = [[message substringToIndex:range.location] stringByAppendingString:[[message substringFromIndex:NSMaxRange(range)] lastPathComponent]];
66
67    printf ("CONSOLE MESSAGE: line %d: %s\n", [lineNumber intValue], [message UTF8String]);
68}
69
70- (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame
71{
72    if (!done)
73        printf("ALERT: %s\n", [message UTF8String]);
74}
75
76- (BOOL)webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame
77{
78    if (!done)
79        printf("CONFIRM: %s\n", [message UTF8String]);
80    return YES;
81}
82
83- (NSString *)webView:(WebView *)sender runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WebFrame *)frame
84{
85    if (!done)
86        printf("PROMPT: %s, default text: %s\n", [prompt UTF8String], [defaultText UTF8String]);
87    return defaultText;
88}
89
90- (BOOL)webView:(WebView *)c runBeforeUnloadConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame
91{
92    if (!done)
93        printf("CONFIRM NAVIGATION: %s\n", [message UTF8String]);
94    return YES;
95}
96
97
98- (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
99{
100     assert(!draggingInfo);
101     draggingInfo = [[DumpRenderTreeDraggingInfo alloc] initWithImage:anImage offset:initialOffset pasteboard:pboard source:sourceObj];
102     [sender draggingUpdated:draggingInfo];
103     [EventSendingController replaySavedEvents];
104}
105
106- (void)webViewFocus:(WebView *)webView
107{
108    gLayoutTestController->setWindowIsKey(true);
109}
110
111- (void)webViewUnfocus:(WebView *)webView
112{
113    gLayoutTestController->setWindowIsKey(false);
114}
115
116- (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request
117{
118    if (!gLayoutTestController->canOpenWindows())
119        return nil;
120
121    // Make sure that waitUntilDone has been called.
122    ASSERT(gLayoutTestController->waitToDump());
123
124    WebView *webView = createWebViewAndOffscreenWindow();
125
126    if (gLayoutTestController->newWindowsCopyBackForwardList())
127        [webView _loadBackForwardListFromOtherView:sender];
128
129    return [webView autorelease];
130}
131
132- (void)webViewClose:(WebView *)sender
133{
134    NSWindow* window = [sender window];
135
136    if (gLayoutTestController->callCloseOnWebViews())
137        [sender close];
138
139    [window close];
140}
141
142- (void)webView:(WebView *)sender frame:(WebFrame *)frame exceededDatabaseQuotaForSecurityOrigin:(WebSecurityOrigin *)origin database:(NSString *)databaseIdentifier
143{
144    if (!done && gLayoutTestController->dumpDatabaseCallbacks())
145        printf("UI DELEGATE DATABASE CALLBACK: exceededDatabaseQuotaForSecurityOrigin:{%s, %s, %i} database:%s\n", [[origin protocol] UTF8String], [[origin host] UTF8String],
146            [origin port], [databaseIdentifier UTF8String]);
147
148    static const unsigned long long defaultQuota = 5 * 1024 * 1024;
149    [origin setQuota:defaultQuota];
150}
151
152- (void)webView:(WebView *)sender setStatusText:(NSString *)text
153{
154    if (gLayoutTestController->dumpStatusCallbacks())
155        printf("UI DELEGATE STATUS CALLBACK: setStatusText:%s\n", [text UTF8String]);
156}
157
158- (void)webView:(WebView *)webView decidePolicyForGeolocationRequestFromOrigin:(WebSecurityOrigin *)origin frame:(WebFrame *)frame listener:(id<WebGeolocationPolicyListener>)listener
159{
160    if (gLayoutTestController->isGeolocationPermissionSet()) {
161        if (gLayoutTestController->geolocationPermission())
162            [listener allow];
163        else
164            [listener deny];
165    }
166}
167
168- (BOOL)webView:(WebView *)sender shouldHaltPlugin:(DOMNode *)pluginNode
169{
170    return NO;
171}
172
173- (void)dealloc
174{
175    [draggingInfo release];
176    draggingInfo = nil;
177
178    [super dealloc];
179}
180
181@end
182