[Win] Implement Authentication dialog in MiniBrowser
[WebKit-https.git] / Tools / MiniBrowser / win / ResourceLoadDelegate.cpp
1 /*
2 * Copyright (C) 2014-2015 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 * 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.
12 *
13 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
15 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
16 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
17 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
18 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
19 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
20 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25 #include "stdafx.h"
26 #include "ResourceLoadDelegate.h"
27
28 #include "MiniBrowser.h"
29 #include "PageLoadTestClient.h"
30 #include <COMPtr.h>
31 #include <WebKit/WebKitCOMAPI.h>
32 #include <comip.h>
33 #include <commctrl.h>
34 #include <commdlg.h>
35 #include <objbase.h>
36 #include <shlwapi.h>
37 #include <string>
38 #include <wininet.h>
39
40 extern HRESULT DisplayAuthDialog(std::wstring& username, std::wstring& password);
41
42 HRESULT ResourceLoadDelegate::QueryInterface(_In_ REFIID riid, _COM_Outptr_ void** ppvObject)
43 {
44     if (!ppvObject)
45         return E_POINTER;
46     *ppvObject = nullptr;
47     if (IsEqualIID(riid, IID_IUnknown))
48         *ppvObject = static_cast<IWebResourceLoadDelegate*>(this);
49     else if (IsEqualIID(riid, IID_IWebResourceLoadDelegate))
50         *ppvObject = static_cast<IWebResourceLoadDelegate*>(this);
51     else
52         return E_NOINTERFACE;
53
54     AddRef();
55     return S_OK;
56 }
57
58 ULONG ResourceLoadDelegate::AddRef()
59 {
60     return ++m_refCount;
61 }
62
63 ULONG ResourceLoadDelegate::Release()
64 {
65     ULONG newRef = --m_refCount;
66     if (!newRef)
67         delete this;
68
69     return newRef;
70 }
71
72 HRESULT ResourceLoadDelegate::identifierForInitialRequest(_In_opt_ IWebView*, _In_opt_ IWebURLRequest*, _In_opt_ IWebDataSource*, unsigned long identifier)
73 {
74     if (!m_client)
75         return E_FAIL;
76
77     m_client->pageLoadTestClient().didInitiateResourceLoad(identifier);
78
79     return S_OK;
80 }
81
82 HRESULT ResourceLoadDelegate::willSendRequest(_In_opt_ IWebView*, unsigned long, _In_opt_ IWebURLRequest*, _In_opt_ IWebURLResponse*, _In_opt_ IWebDataSource*, _COM_Outptr_opt_ IWebURLRequest** result)
83 {
84     if (!result)
85         return E_POINTER;
86     *result = nullptr;
87     return E_NOTIMPL;
88 }
89
90 HRESULT ResourceLoadDelegate::didReceiveAuthenticationChallenge(_In_opt_ IWebView*, unsigned long, _In_opt_ IWebURLAuthenticationChallenge* challenge, _In_opt_ IWebDataSource*)
91 {
92     COMPtr<IWebURLAuthenticationChallengeSender> sender;
93     if (!challenge || FAILED(challenge->sender(&sender)))
94         return E_FAIL;
95
96     std::wstring username, password;
97     if (DisplayAuthDialog(username, password) != S_OK)
98         return E_FAIL;
99
100     COMPtr<IWebURLCredential> credential;
101     if (FAILED(WebKitCreateInstance(CLSID_WebURLCredential, 0, IID_IWebURLCredential, (void**)&credential)))
102         return E_FAIL;
103     credential->initWithUser(_bstr_t(username.c_str()), _bstr_t(password.c_str()), WebURLCredentialPersistenceForSession);
104
105     sender->useCredential(credential.get(), challenge);
106     return S_OK;
107 }
108
109 HRESULT ResourceLoadDelegate::didCancelAuthenticationChallenge(_In_opt_ IWebView*, unsigned long, _In_opt_ IWebURLAuthenticationChallenge*, _In_opt_ IWebDataSource*)
110 {
111     return E_NOTIMPL;
112 }
113
114 HRESULT ResourceLoadDelegate::didReceiveResponse(_In_opt_ IWebView*, unsigned long, _In_opt_ IWebURLResponse*, _In_opt_ IWebDataSource*)
115 {
116     return E_NOTIMPL;
117 }
118
119 HRESULT ResourceLoadDelegate::didReceiveContentLength(_In_opt_ IWebView*, unsigned long, UINT, _In_opt_ IWebDataSource*)
120 {
121     return E_NOTIMPL;
122 }
123
124 HRESULT ResourceLoadDelegate::didFinishLoadingFromDataSource(_In_opt_ IWebView*, unsigned long identifier, _In_opt_ IWebDataSource*)
125 {
126     if (!m_client)
127         return E_FAIL;
128
129     m_client->pageLoadTestClient().didEndResourceLoad(identifier);
130
131     return S_OK;
132 }
133
134 HRESULT ResourceLoadDelegate::didFailLoadingWithError(_In_opt_ IWebView*, unsigned long identifier, _In_opt_ IWebError*, _In_opt_ IWebDataSource*)
135 {
136     if (!m_client)
137         return E_FAIL;
138
139     m_client->pageLoadTestClient().didEndResourceLoad(identifier);
140
141     return S_OK;
142 }
143
144 HRESULT ResourceLoadDelegate::plugInFailedWithError(_In_opt_ IWebView*, _In_opt_ IWebError*, _In_opt_ IWebDataSource*)
145 {
146     return E_NOTIMPL;
147 }