WebKit:
[WebKit-https.git] / WebKit / WebView.subproj / WebDefaultPolicyDelegate.m
1 /*      
2         WebDefaultPolicyDelegate.m
3         Copyright 2002, Apple Computer, Inc.
4 */
5 #import <WebKit/WebDataSource.h>
6 #import <WebKit/WebDefaultPolicyDelegate.h>
7 #import <WebKit/WebFrame.h>
8 #import <WebKit/WebPolicyDelegatePrivate.h>
9 #import <WebKit/WebView.h>
10 #import <WebKit/WebViewPrivate.h>
11
12 #import <Foundation/NSURLConnection.h>
13 #import <Foundation/NSURLRequest.h>
14 #import <Foundation/NSURLResponse.h>
15 #import <WebKit/WebAssertions.h>
16
17
18 @implementation WebDefaultPolicyDelegate
19
20 static WebDefaultPolicyDelegate *sharedDelegate = nil;
21
22 // Return a object with vanilla implementations of the protocol's methods
23 // Note this feature relies on our default delegate being stateless
24 + (WebDefaultPolicyDelegate *)sharedPolicyDelegate
25 {
26     if (!sharedDelegate) {
27         sharedDelegate = [[WebDefaultPolicyDelegate alloc] init];
28     }
29     return sharedDelegate;
30 }
31
32 - (void)webView: (WebView *)wv unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame
33 {
34     ERROR("called unableToImplementPolicyWithError:%@ inFrame:%@", error, frame);
35 }
36
37
38 - (void)webView: (WebView *)wv decidePolicyForMIMEType:(NSString *)type
39                                                request:(NSURLRequest *)request
40                                                  frame:(WebFrame *)frame
41                                       decisionListener:(WebPolicyDecisionListener *)listener;
42 {
43     if ([[request URL] isFileURL]) {
44         BOOL isDirectory;
45         [[NSFileManager defaultManager] fileExistsAtPath:[[request URL] path] isDirectory:&isDirectory];
46         
47         if (isDirectory) {
48             [listener ignore];
49         } else if([WebView canShowMIMEType:type]) {
50             [listener use];
51         } else{
52             [listener ignore];
53         }
54     } else if ([WebView canShowMIMEType:type]) {
55         [listener use];
56     } else {
57         [listener ignore];
58     }
59 }
60
61 - (void)webView: (WebView *)wv decidePolicyForNavigationAction:(NSDictionary *)actionInformation 
62                                                        request:(NSURLRequest *)request
63                                                          frame:(WebFrame *)frame
64                                               decisionListener:(WebPolicyDecisionListener *)listener
65 {
66     WebNavigationType navType = [[actionInformation objectForKey:WebActionNavigationTypeKey] intValue];
67
68     if ([WebView _canHandleRequest:request]) {
69         [listener use];
70     } else if (navType == WebNavigationTypePlugInRequest) {
71         [listener use];
72     } else {
73         // A file URL shouldn't fall through to here, but if it did,
74         // it would be a security risk to open it.
75         if (![[request URL] isFileURL]) {
76             [[NSWorkspace sharedWorkspace] openURL:[request URL]];
77         }
78         [listener ignore];
79     }
80 }
81
82 - (void)webView: (WebView *)wv decidePolicyForNewWindowAction:(NSDictionary *)actionInformation 
83                                                       request:(NSURLRequest *)request
84                                                  newFrameName:(NSString *)frameName
85                                              decisionListener:(WebPolicyDecisionListener *)listener
86 {
87     [listener use];
88 }
89
90 // Temporary SPI needed for <rdar://problem/3951283> can view pages from the back/forward cache that should be disallowed by Parental Controls
91 - (BOOL)webView:(WebView *)webView shouldGoToHistoryItem:(WebHistoryItem *)item
92 {
93     return YES;
94 }
95
96 @end
97