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