2008-03-07 Mark Rowe <mrowe@apple.com>
[WebKit-https.git] / WebKitTools / DumpRenderTree / mac / ResourceLoadDelegate.mm
1 /*
2  * Copyright (C) 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 "ResourceLoadDelegate.h"
30
31 #import "DumpRenderTree.h"
32 #import "LayoutTestController.h"
33 #import <WebKit/WebKit.h>
34 #import <WebKit/WebTypesInternal.h>
35 #import <wtf/Assertions.h>
36
37 @interface NSURL (DRTExtras)
38 - (NSString *)_drt_descriptionSuitableForTestResult;
39 @end
40
41 @interface NSError (DRTExtras)
42 - (NSString *)_drt_descriptionSuitableForTestResult;
43 @end
44
45 @interface NSURLResponse (DRTExtras)
46 - (NSString *)_drt_descriptionSuitableForTestResult;
47 @end
48
49 @interface NSURLRequest (DRTExtras)
50 - (NSString *)_drt_descriptionSuitableForTestResult;
51 @end
52
53 @implementation NSError (DRTExtras)
54 - (NSString *)_drt_descriptionSuitableForTestResult 
55 {
56     NSString *str = [NSString stringWithFormat:@"<NSError domain %@, code %d", [self domain], [self code]];
57     NSURL *failingURL;
58     
59     if ((failingURL = [[self userInfo] objectForKey:@"NSErrorFailingURLKey"]))
60         str = [str stringByAppendingFormat:@", failing URL \"%@\"", [failingURL _drt_descriptionSuitableForTestResult]];
61         
62     str = [str stringByAppendingFormat:@">"];
63     
64     return str;
65 }
66
67 @end
68
69 @implementation NSURL (DRTExtras)
70
71 - (NSString *)_drt_descriptionSuitableForTestResult 
72 {
73     if (![self isFileURL])
74         return [self absoluteString];
75
76     WebDataSource *dataSource = [mainFrame dataSource];
77     if (!dataSource)
78         dataSource = [mainFrame provisionalDataSource];
79     
80     NSString *basePath = [[[[dataSource request] URL] path] stringByDeletingLastPathComponent];
81     
82     return [[self path] substringFromIndex:[basePath length] + 1];
83 }
84
85 @end
86
87 @implementation NSURLResponse (DRTExtras)
88
89 - (NSString *)_drt_descriptionSuitableForTestResult
90 {
91     return [NSString stringWithFormat:@"<NSURLResponse %@>", [[self URL] _drt_descriptionSuitableForTestResult]];
92 }
93
94 @end
95
96 @implementation NSURLRequest (DRTExtras)
97
98 - (NSString *)_drt_descriptionSuitableForTestResult
99 {
100     return [NSString stringWithFormat:@"<NSURLRequest %@>", [[self URL] _drt_descriptionSuitableForTestResult]];
101 }
102
103 @end
104
105 @implementation ResourceLoadDelegate
106
107 - webView: (WebView *)wv identifierForInitialRequest: (NSURLRequest *)request fromDataSource: (WebDataSource *)dataSource
108 {
109     ASSERT([[dataSource webFrame] dataSource] || [[dataSource webFrame] provisionalDataSource]);
110     
111     if (!done && layoutTestController->dumpResourceLoadCallbacks())
112         return [[request URL] _drt_descriptionSuitableForTestResult];
113     
114     return @"<unknown>";
115 }
116
117 -(NSURLRequest *)webView: (WebView *)wv resource:identifier willSendRequest: (NSURLRequest *)newRequest redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)dataSource
118 {
119     if (!done && layoutTestController->dumpResourceLoadCallbacks()) {
120         NSString *string = [NSString stringWithFormat:@"%@ - willSendRequest %@ redirectResponse %@", identifier, [newRequest _drt_descriptionSuitableForTestResult],
121             [redirectResponse _drt_descriptionSuitableForTestResult]];
122         printf ("%s\n", [string UTF8String]);
123     }    
124     
125     if (disallowedURLs && CFSetContainsValue(disallowedURLs, [newRequest URL]))
126         return nil;
127     
128     return newRequest;
129 }
130
131 - (void)webView:(WebView *)wv resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)dataSource
132 {
133 }
134
135 - (void)webView:(WebView *)wv resource:(id)identifier didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)dataSource
136 {
137 }
138
139 -(void)webView: (WebView *)wv resource:identifier didReceiveResponse: (NSURLResponse *)response fromDataSource:(WebDataSource *)dataSource
140 {
141     if (!done && layoutTestController->dumpResourceLoadCallbacks()) {
142         NSString *string = [NSString stringWithFormat:@"%@ - didReceiveResponse %@", identifier, [response _drt_descriptionSuitableForTestResult]];
143         printf ("%s\n", [string UTF8String]);
144     }    
145 }
146
147 -(void)webView: (WebView *)wv resource:identifier didReceiveContentLength: (NSInteger)length fromDataSource:(WebDataSource *)dataSource
148 {
149 }
150
151 -(void)webView: (WebView *)wv resource:identifier didFinishLoadingFromDataSource:(WebDataSource *)dataSource
152 {
153     if (!done && layoutTestController->dumpResourceLoadCallbacks()) {
154         NSString *string = [NSString stringWithFormat:@"%@ - didFinishLoading", identifier];
155         printf ("%s\n", [string UTF8String]);
156     }
157 }
158
159 -(void)webView: (WebView *)wv resource:identifier didFailLoadingWithError:(NSError *)error fromDataSource:(WebDataSource *)dataSource
160 {
161     if (!done && layoutTestController->dumpResourceLoadCallbacks()) {
162         NSString *string = [NSString stringWithFormat:@"%@ - didFailLoadingWithError: %@", identifier, [error _drt_descriptionSuitableForTestResult]];
163         printf ("%s\n", [string UTF8String]);
164     }
165 }
166
167 - (void)webView: (WebView *)wv plugInFailedWithError:(NSError *)error dataSource:(WebDataSource *)dataSource
168 {
169 }
170
171 -(NSCachedURLResponse *) webView: (WebView *)wv resource:(id)identifier willCacheResponse:(NSCachedURLResponse *)response fromDataSource:(WebDataSource *)dataSource
172 {
173     if (!done && layoutTestController->dumpResourceLoadCallbacks()) {
174         NSString *string = [NSString stringWithFormat:@"%@ - willCacheResponse: called", identifier];
175         printf ("%s\n", [string UTF8String]);
176     }
177     return response;
178 }
179
180 @end