99426fc3d8c74831388e44c4ed46b5e34fe1ec29
[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     if ([WebView _canHandleRequest:request]) {
67         [listener use];
68     } else {
69         // A file URL shouldn't fall through to here, but if it did,
70         // it would be a security risk to open it.
71         if (![[request URL] isFileURL]) {
72             [[NSWorkspace sharedWorkspace] openURL:[request URL]];
73         }
74         [listener ignore];
75     }
76 }
77
78 - (void)webView: (WebView *)wv decidePolicyForNewWindowAction:(NSDictionary *)actionInformation 
79                                                       request:(NSURLRequest *)request
80                                                  newFrameName:(NSString *)frameName
81                                              decisionListener:(WebPolicyDecisionListener *)listener
82 {
83     [listener use];
84 }
85
86 // Temporary SPI needed for <rdar://problem/3951283> can view pages from the back/forward cache that should be disallowed by Parental Controls
87 - (BOOL)webView:(WebView *)webView shouldGoToHistoryItem:(WebHistoryItem *)item
88 {
89     return YES;
90 }
91
92 @end
93