1/*
2 * Copyright (C) 2005, 2008 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 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 "WebDefaultPolicyDelegate.h"
30
31#import "WebDataSource.h"
32#import "WebFrame.h"
33#import "WebPolicyDelegatePrivate.h"
34#import "WebViewInternal.h"
35#import <Foundation/NSURLConnection.h>
36#import <Foundation/NSURLRequest.h>
37#import <Foundation/NSURLResponse.h>
38#import <wtf/Assertions.h>
39
40@implementation WebDefaultPolicyDelegate
41
42static WebDefaultPolicyDelegate *sharedDelegate = nil;
43
44// Return a object with vanilla implementations of the protocol's methods
45// Note this feature relies on our default delegate being stateless
46+ (WebDefaultPolicyDelegate *)sharedPolicyDelegate
47{
48    if (!sharedDelegate) {
49        sharedDelegate = [[WebDefaultPolicyDelegate alloc] init];
50    }
51    return sharedDelegate;
52}
53
54- (void)webView: (WebView *)wv unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame
55{
56    LOG_ERROR("called unableToImplementPolicyWithError:%@ inFrame:%@", error, frame);
57}
58
59
60- (void)webView: (WebView *)wv decidePolicyForMIMEType:(NSString *)type
61                                               request:(NSURLRequest *)request
62                                                 frame:(WebFrame *)frame
63                                      decisionListener:(id <WebPolicyDecisionListener>)listener
64{
65    if ([[request URL] isFileURL]) {
66        BOOL isDirectory = NO;
67        BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:[[request URL] path] isDirectory:&isDirectory];
68
69        if (exists && !isDirectory && [wv _canShowMIMEType:type])
70            [listener use];
71        else
72            [listener ignore];
73    } else if ([wv _canShowMIMEType:type])
74        [listener use];
75    else
76        [listener ignore];
77}
78
79- (void)webView: (WebView *)wv decidePolicyForNavigationAction:(NSDictionary *)actionInformation
80                                                       request:(NSURLRequest *)request
81                                                         frame:(WebFrame *)frame
82                                              decisionListener:(id <WebPolicyDecisionListener>)listener
83{
84    WebNavigationType navType = [[actionInformation objectForKey:WebActionNavigationTypeKey] intValue];
85
86    if ([WebView _canHandleRequest:request forMainFrame:frame == [wv mainFrame]]) {
87        [listener use];
88    } else if (navType == WebNavigationTypePlugInRequest) {
89        [listener use];
90    } else {
91        // A file URL shouldn't fall through to here, but if it did,
92        // it would be a security risk to open it.
93        if (![[request URL] isFileURL]) {
94#if !PLATFORM(IOS)
95            [[NSWorkspace sharedWorkspace] openURL:[request URL]];
96#endif
97        }
98        [listener ignore];
99    }
100}
101
102- (void)webView: (WebView *)wv decidePolicyForNewWindowAction:(NSDictionary *)actionInformation
103                                                      request:(NSURLRequest *)request
104                                                 newFrameName:(NSString *)frameName
105                                             decisionListener:(id <WebPolicyDecisionListener>)listener
106{
107    [listener use];
108}
109
110// Temporary SPI needed for <rdar://problem/3951283> can view pages from the back/forward cache that should be disallowed by Parental Controls
111- (BOOL)webView:(WebView *)webView shouldGoToHistoryItem:(WebHistoryItem *)item
112{
113    return YES;
114}
115
116@end
117
118