1/*
2 * Copyright (C) 2007, 2009 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 "PolicyDelegate.h"
31
32#import "DumpRenderTree.h"
33#import "LayoutTestController.h"
34#import <WebKit/DOMElement.h>
35#import <WebKit/WebPolicyDelegate.h>
36#import <WebKit/WebView.h>
37
38@interface NSURL (DRTExtras)
39- (NSString *)_drt_descriptionSuitableForTestResult;
40@end
41
42@interface DOMNode (dumpPath)
43- (NSString *)dumpPath;
44@end
45
46@implementation PolicyDelegate
47
48- (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation
49                                                           request:(NSURLRequest *)request
50                                                             frame:(WebFrame *)frame
51                                                  decisionListener:(id<WebPolicyDecisionListener>)listener
52{
53    WebNavigationType navType = (WebNavigationType)[[actionInformation objectForKey:WebActionNavigationTypeKey] intValue];
54
55    const char* typeDescription;
56    switch (navType) {
57        case WebNavigationTypeLinkClicked:
58            typeDescription = "link clicked";
59            break;
60        case WebNavigationTypeFormSubmitted:
61            typeDescription = "form submitted";
62            break;
63        case WebNavigationTypeBackForward:
64            typeDescription = "back/forward";
65            break;
66        case WebNavigationTypeReload:
67            typeDescription = "reload";
68            break;
69        case WebNavigationTypeFormResubmitted:
70            typeDescription = "form resubmitted";
71            break;
72        case WebNavigationTypeOther:
73            typeDescription = "other";
74            break;
75        default:
76            typeDescription = "illegal value";
77    }
78
79    NSString *message = [NSString stringWithFormat:@"Policy delegate: attempt to load %@ with navigation type '%s'", [[request URL] _drt_descriptionSuitableForTestResult], typeDescription];
80
81    if (DOMElement *originatingNode = [[actionInformation objectForKey:WebActionElementKey] objectForKey:WebElementDOMNodeKey])
82        message = [message stringByAppendingFormat:@" originating from %@", [originatingNode dumpPath]];
83
84    printf("%s\n", [message UTF8String]);
85
86    if (permissiveDelegate)
87        [listener use];
88    else
89        [listener ignore];
90
91    if (controllerToNotifyDone) {
92        controllerToNotifyDone->notifyDone();
93        controllerToNotifyDone = 0;
94    }
95}
96
97- (void)webView:(WebView *)webView unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame
98{
99    NSString *message = [NSString stringWithFormat:@"Policy delegate: unable to implement policy with error domain '%@', error code %d, in frame '%@'", [error domain], [error code], [frame name]];
100    printf("%s\n", [message UTF8String]);
101}
102
103- (void)setPermissive:(BOOL)permissive
104{
105    permissiveDelegate = permissive;
106}
107
108- (void)setControllerToNotifyDone:(LayoutTestController*)controller
109{
110    controllerToNotifyDone = controller;
111}
112
113@end
114