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