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