-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebBrowserHost.cpp
More file actions
171 lines (156 loc) · 5.37 KB
/
WebBrowserHost.cpp
File metadata and controls
171 lines (156 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
// WebBrowserHost.cpp: Subclass CAxHostWindow to create a specialized
// control host to provide IOleCommandTarget and IDocHostUIHandler.
//
// Copyright (C) 2012 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http:www.gnu.org/licenses/>.
//
#include "stdafx.h"
#include "WebBrowserHost.h"
#include <atlstr.h>
#include <MLang.h>
#include <Shlguid.h>
#include <Mshtmcid.h>
CWebBrowserHost::CWebBrowserHost(void) : m_pSecurityMgr(NULL) {
}
CWebBrowserHost::~CWebBrowserHost(void) {
}
// IDocHostUIHandler
STDMETHODIMP CWebBrowserHost::GetHostInfo(DOCHOSTUIINFO FAR* pInfo) {
if(pInfo != NULL) {
pInfo->dwFlags = DOCHOSTUIFLAG_NO3DBORDER
| DOCHOSTUIFLAG_SCROLL_NO
| DOCHOSTUIFLAG_THEME
| DOCHOSTUIFLAG_ENABLE_FORMS_AUTOCOMPLETE
| DOCHOSTUIFLAG_LOCAL_MACHINE_ACCESS_CHECK
| DOCHOSTUIFLAG_ENABLE_REDIRECT_NOTIFICATION
| DOCHOSTUIFLAG_ENABLE_INPLACE_NAVIGATION;
pInfo->dwDoubleClick = DOCHOSTUIDBLCLK_DEFAULT;
}
return S_OK;
}
// IOleCommandTarget
STDMETHODIMP CWebBrowserHost::QueryStatus(
/* [unique][in] */ const GUID *pguidCmdGroup,
/* [in] */ ULONG /*cCmds*/,
/* [out][in][size_is] */ OLECMD * /*prgCmds*/,
/* [unique][out][in] */ OLECMDTEXT * /*pCmdText*/) {
return pguidCmdGroup ? OLECMDERR_E_UNKNOWNGROUP : OLECMDERR_E_NOTSUPPORTED;
}
STDMETHODIMP CWebBrowserHost::Exec(
/* [unique][in] */ const GUID *pguidCmdGroup,
/* [in] */ DWORD nCmdID,
/* [in] */ DWORD /*nCmdexecopt*/,
/* [unique][in] */ VARIANT * pvaIn,
/* [unique][out][in] */ VARIANT * pvaOut) {
HRESULT hr = pguidCmdGroup ? OLECMDERR_E_UNKNOWNGROUP : OLECMDERR_E_NOTSUPPORTED;
if(pguidCmdGroup && IsEqualGUID(*pguidCmdGroup, CGID_DocHostCommandHandler)) {
if(nCmdID == OLECMDID_SHOWSCRIPTERROR) {
hr = S_OK;
IHTMLDocument2* pDoc = NULL;
IHTMLWindow2* pWindow = NULL;
IHTMLEventObj* pEventObj = NULL;
BSTR rgwszNames[5] =
{
SysAllocString(L"errorLine"),
SysAllocString(L"errorCharacter"),
SysAllocString(L"errorCode"),
SysAllocString(L"errorMessage"),
SysAllocString(L"errorUrl")
};
DISPID rgDispIDs[5];
VARIANT rgvaEventInfo[5];
DISPPARAMS params;
BOOL fContinueRunningScripts = true;
int i;
params.cArgs = 0;
params.cNamedArgs = 0;
// Get the document that is currently being viewed.
hr = pvaIn->punkVal->QueryInterface(IID_IHTMLDocument2, (void **)&pDoc);
// Get document.parentWindow.
hr = pDoc->get_parentWindow(&pWindow);
pDoc->Release();
// Get the window.event object.
hr = pWindow->get_event(&pEventObj);
// Get the error info from the window.event object.
for (i = 0; i < 5; i++)
{
// Get the property's dispID.
hr = pEventObj->GetIDsOfNames(IID_NULL, &rgwszNames[i], 1,
LOCALE_SYSTEM_DEFAULT, &rgDispIDs[i]);
// Get the value of the property.
hr = pEventObj->Invoke(rgDispIDs[i], IID_NULL,
LOCALE_SYSTEM_DEFAULT,
DISPATCH_PROPERTYGET, ¶ms, &rgvaEventInfo[i],
NULL, NULL);
SysFreeString(rgwszNames[i]);
}
// At this point, you would normally alert the user with
// the information about the error, which is now contained
// in rgvaEventInfo[]. Or, you could just exit silently.
(*pvaOut).vt = VT_BOOL;
if (fContinueRunningScripts)
{
// Continue running scripts on the page.
(*pvaOut).boolVal = VARIANT_TRUE;
}
else
{
// Stop running scripts on the page.
(*pvaOut).boolVal = VARIANT_FALSE;
}
}
}
return hr;
}
HRESULT CWebBrowserHost::AxCreateControlLicEx(LPCOLESTR lpszName, HWND hWnd, IStream* pStream,
IUnknown** ppUnkContainer, IUnknown** ppUnkControl, REFIID iidSink, IUnknown* punkSink, BSTR bstrLic)
{
AtlAxWinInit();
HRESULT hr;
CComPtr<IUnknown> spUnkContainer;
CComPtr<IUnknown> spUnkControl;
hr = CWebBrowserHost::_CreatorClass::CreateInstance(NULL, __uuidof(IUnknown), (void**)&spUnkContainer);
if (SUCCEEDED(hr)) {
CComPtr<IAxWinHostWindowLic> pAxWindow;
spUnkContainer->QueryInterface(__uuidof(IAxWinHostWindow), (void**)&pAxWindow);
CComBSTR bstrName(lpszName);
hr = pAxWindow->CreateControlLicEx(bstrName, hWnd, pStream, &spUnkControl, iidSink, punkSink, bstrLic);
IJavaScriptFunction * javaScriptFunction;
pAxWindow->QueryInterface(IID_IJavaScriptFunction, (void**)&javaScriptFunction);
if (javaScriptFunction)
{
pAxWindow->SetExternalDispatch(javaScriptFunction);
javaScriptFunction->Release();
}
}
if (ppUnkContainer != NULL) {
if (SUCCEEDED(hr)) {
*ppUnkContainer = spUnkContainer.p;
spUnkContainer.p = NULL;
}
else
*ppUnkContainer = NULL;
}
if (ppUnkControl != NULL) {
if (SUCCEEDED(hr)) {
*ppUnkControl = SUCCEEDED(hr) ? spUnkControl.p : NULL;
spUnkControl.p = NULL;
}
else
*ppUnkControl = NULL;
}
return hr;
}