2 * Copyright (C) 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "WebKitDLL.h"
28 #include "DefaultPolicyDelegate.h"
30 #include <WebCore/COMPtr.h>
31 #include <WebCore/PlatformString.h>
33 using namespace WebCore;
35 // FIXME: move this enum to a separate header file when other code begins to use it.
36 typedef enum WebExtraNavigationType {
37 WebNavigationTypePlugInRequest = WebNavigationTypeOther + 1
38 } WebExtraNavigationType;
40 // DefaultPolicyDelegate ----------------------------------------------------------------
42 DefaultPolicyDelegate::DefaultPolicyDelegate()
46 gClassNameCount.add("DefaultPolicyDelegate");
49 DefaultPolicyDelegate::~DefaultPolicyDelegate()
52 gClassNameCount.remove("DefaultPolicyDelegate");
55 DefaultPolicyDelegate* DefaultPolicyDelegate::sharedInstance()
57 static COMPtr<DefaultPolicyDelegate> shared;
59 shared.adoptRef(DefaultPolicyDelegate::createInstance());
63 DefaultPolicyDelegate* DefaultPolicyDelegate::createInstance()
65 DefaultPolicyDelegate* instance = new DefaultPolicyDelegate();
70 // IUnknown -------------------------------------------------------------------
72 HRESULT STDMETHODCALLTYPE DefaultPolicyDelegate::QueryInterface(REFIID riid, void** ppvObject)
75 if (IsEqualGUID(riid, IID_IUnknown))
76 *ppvObject = static_cast<IUnknown*>(this);
77 else if (IsEqualGUID(riid, IID_IWebPolicyDelegate))
78 *ppvObject = static_cast<IWebPolicyDelegate*>(this);
86 ULONG STDMETHODCALLTYPE DefaultPolicyDelegate::AddRef()
91 ULONG STDMETHODCALLTYPE DefaultPolicyDelegate::Release()
93 ULONG newRef = --m_refCount;
100 HRESULT STDMETHODCALLTYPE DefaultPolicyDelegate::decidePolicyForNavigationAction(
101 /*[in]*/ IWebView* webView,
102 /*[in]*/ IPropertyBag* actionInformation,
103 /*[in]*/ IWebURLRequest* request,
104 /*[in]*/ IWebFrame* /*frame*/,
105 /*[in]*/ IWebPolicyDecisionListener* listener)
109 if (SUCCEEDED(actionInformation->Read(WebActionNavigationTypeKey, &var, 0))) {
111 navType = V_I4(&var);
113 COMPtr<IWebViewPrivate> wvPrivate(Query, webView);
115 BOOL canHandleRequest = FALSE;
116 if (SUCCEEDED(wvPrivate->canHandleRequest(request, &canHandleRequest)) && canHandleRequest)
118 else if (navType == WebNavigationTypePlugInRequest)
122 // A file URL shouldn't fall through to here, but if it did,
123 // it would be a security risk to open it.
124 if (SUCCEEDED(request->URL(&url)) && !String(url, SysStringLen(url)).startsWith("file:")) {
125 // FIXME: Open the URL not by means of a webframe, but by handing it over to the system and letting it determine how to open that particular URL scheme. See documentation for [NSWorkspace openURL]
135 HRESULT STDMETHODCALLTYPE DefaultPolicyDelegate::decidePolicyForNewWindowAction(
136 /*[in]*/ IWebView* /*webView*/,
137 /*[in]*/ IPropertyBag* /*actionInformation*/,
138 /*[in]*/ IWebURLRequest* /*request*/,
139 /*[in]*/ BSTR /*frameName*/,
140 /*[in]*/ IWebPolicyDecisionListener* listener)
146 HRESULT STDMETHODCALLTYPE DefaultPolicyDelegate::decidePolicyForMIMEType(
147 /*[in]*/ IWebView* webView,
149 /*[in]*/ IWebURLRequest* request,
150 /*[in]*/ IWebFrame* /*frame*/,
151 /*[in]*/ IWebPolicyDecisionListener* listener)
153 BOOL canShowMIMEType;
154 if (FAILED(webView->canShowMIMEType(type, &canShowMIMEType)))
155 canShowMIMEType = FALSE;
160 if (String(url, SysStringLen(url)).startsWith("file:")) {
161 BOOL isDirectory = FALSE;
162 WIN32_FILE_ATTRIBUTE_DATA attrs;
163 if (GetFileAttributesEx(url, GetFileExInfoStandard, &attrs))
164 isDirectory = !!(attrs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
168 else if(canShowMIMEType)
172 } else if (canShowMIMEType)
180 HRESULT STDMETHODCALLTYPE DefaultPolicyDelegate::unableToImplementPolicyWithError(
181 /*[in]*/ IWebView* /*webView*/,
182 /*[in]*/ IWebError* error,
183 /*[in]*/ IWebFrame* frame)
186 error->localizedDescription(&errorStr);
189 frame->name(&frameName);
191 LOG_ERROR("called unableToImplementPolicyWithError:%S inFrame:%S", errorStr ? errorStr : TEXT(""), frameName ? frameName : TEXT(""));
192 SysFreeString(errorStr);
193 SysFreeString(frameName);