summaryrefslogtreecommitdiff
path: root/protocols/MSN/src/msn_ieembed.cpp
blob: 996a5310934e1ac00b3fbfd996d931b3cde326ad (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
Plugin of Miranda IM for communicating with users of the MSN Messenger protocol.

Copyright (c) 2012-2014 Miranda NG Team
Copyright (c) 2007-2012 Boris Krasnovskiy.

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 2
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 <exdispid.h>
#include "msn_proto.h"
#include "msn_ieembed.h"

IEEmbedSink::IEEmbedSink(IEEmbed *smptr)
{
	ieWindow = smptr;
}

IEEmbedSink::~IEEmbedSink() {}

STDMETHODIMP IEEmbedSink::QueryInterface(REFIID riid, PVOID *ppv)
{
	*ppv = NULL;
	if (IID_IUnknown == riid)
		*ppv = (IUnknown *)this;

	if (IID_IDispatch == riid)
		*ppv = (IDispatch *)this;

	if (DIID_DWebBrowserEvents2 == riid)
		*ppv = (DWebBrowserEvents2*)this;

	if (NULL != *ppv) {
		((LPUNKNOWN)*ppv)->AddRef();
		return NOERROR;
	}
	return E_NOINTERFACE;
}

STDMETHODIMP_(ULONG) IEEmbedSink::AddRef(void)
{
	++m_cRef;
	return m_cRef;
}

STDMETHODIMP_(ULONG) IEEmbedSink::Release(void)
{
	--m_cRef;
	return m_cRef;
}

STDMETHODIMP IEEmbedSink::GetTypeInfoCount(UINT *) { return E_NOTIMPL; }
STDMETHODIMP IEEmbedSink::GetTypeInfo(UINT, LCID, LPTYPEINFO*) { return S_OK; }
STDMETHODIMP IEEmbedSink::GetIDsOfNames(REFIID, LPOLESTR*, UINT, LCID, DISPID*) { return S_OK; }

STDMETHODIMP IEEmbedSink::Invoke(DISPID dispIdMember, REFIID, LCID, WORD, DISPPARAMS* pDispParams, VARIANT*, EXCEPINFO*, UINT*)
{
	if (!pDispParams) return E_INVALIDARG;
	switch (dispIdMember) {
	case DISPID_DOCUMENTCOMPLETE:
		DocumentComplete(
			pDispParams->rgvarg[1].pdispVal,
			pDispParams->rgvarg[0].pvarVal);
		return S_OK;
	}

	return DISP_E_MEMBERNOTFOUND;
}
// DWebBrowserEvents2

void IEEmbedSink::StatusTextChange(BSTR) {}
void IEEmbedSink::ProgressChange(long, long) {}
void IEEmbedSink::CommandStateChange(long, VARIANT_BOOL) {}
void IEEmbedSink::DownloadBegin() {}
void IEEmbedSink::DownloadComplete() {}
void IEEmbedSink::TitleChange(BSTR) {}
void IEEmbedSink::PropertyChange(BSTR) {}
void IEEmbedSink::BeforeNavigate2(IDispatch*, VARIANT* , VARIANT*, VARIANT*, VARIANT*, VARIANT*, VARIANT_BOOL*) { }
void IEEmbedSink::NewWindow2(IDispatch**, VARIANT_BOOL*) {}
void IEEmbedSink::NavigateComplete(IDispatch*, VARIANT*) {}
void IEEmbedSink::DocumentComplete(IDispatch* , VARIANT* url) 
{
	HWND hWnd;
	ieWindow->GetWindow(&hWnd);
	SendMessage(hWnd, UM_DOCCOMPLETE, 0, (LPARAM)url->bstrVal);
}
void IEEmbedSink::OnQuit() {}
void IEEmbedSink::OnVisible(VARIANT_BOOL) {}
void IEEmbedSink::OnToolBar(VARIANT_BOOL) {}
void IEEmbedSink::OnMenuBar(VARIANT_BOOL) {}
void IEEmbedSink::OnStatusBar(VARIANT_BOOL) {}
void IEEmbedSink::OnFullScreen(VARIANT_BOOL) {}
void IEEmbedSink::OnTheaterMode(VARIANT_BOOL) {}
void IEEmbedSink::WindowSetResizable(VARIANT_BOOL) {}
void IEEmbedSink::WindowSetLeft(long) {}
void IEEmbedSink::WindowSetTop(long) {}
void IEEmbedSink::WindowSetWidth(long) {}
void IEEmbedSink::WindowSetHeight(long) {}
void IEEmbedSink::WindowClosing(VARIANT_BOOL, VARIANT_BOOL*) {}
void IEEmbedSink::ClientToHostWindow(long *, long *) {}
void IEEmbedSink::SetSecureLockIcon(long) {}
void IEEmbedSink::FileDownload(VARIANT_BOOL*) {}


static LRESULT CALLBACK IEEmbedServerWindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	IEEmbed **view = (IEEmbed **)GetWindowLongPtr(GetParent(GetParent(GetParent(hwnd))), GWLP_USERDATA);
	if (view && *view) {
		switch (message) {
		/*
		case WM_KEYUP:
			if (LOWORD(wParam) == VK_ESCAPE && !(GetKeyState(VK_SHIFT) & 0x8000) && !(GetKeyState(VK_CONTROL) & 0x8000) && !(GetKeyState(VK_MENU) & 0x8000))
				SendMessage(GetParent(GetParent(GetParent(hwnd))), WM_COMMAND, IDCANCEL, 0);
			break; */
		
		case WM_KEYDOWN:
			if (LOWORD(wParam) != VK_BACK)
				(*view)->translateAccelerator(message, wParam, lParam);
			break;
		
			/*
		case WM_SETFOCUS:
			RECT rcWindow;
			POINT cursor;
			GetWindowRect(hwnd, &rcWindow);
			GetCursorPos(&cursor);
			if (cursor.y <= rcWindow.bottom && cursor.y >= rcWindow.top && cursor.x <= rcWindow.right && cursor.x >= rcWindow.left)
				view->mouseActivate();

			if (view->setFocus((HWND)wParam))
				return TRUE;
			break;
			*/
		
		}
		return CallWindowProc((*view)->getServerWndProc(), hwnd, message, wParam, lParam);
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}

static LRESULT CALLBACK IEEmbedDocWindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	IEEmbed **view = (IEEmbed **)GetWindowLongPtr(GetParent(GetParent(hwnd)), GWLP_USERDATA);
	if (view && *view) {
		WNDPROC oldWndProc = (*view)->getDocWndProc();
		if (message == WM_PARENTNOTIFY && wParam == WM_CREATE) {
			SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)oldWndProc);
			(*view)->setServerWndProc((WNDPROC)SetWindowLongPtr((HWND)lParam, GWLP_WNDPROC, (LONG_PTR)IEEmbedServerWindowProcedure));
		}
		return CallWindowProc(oldWndProc, hwnd, message, wParam, lParam);
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}

static LRESULT CALLBACK IEEmbedWindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	IEEmbed **view = (IEEmbed **)GetWindowLongPtr(GetParent(hwnd), GWLP_USERDATA);
	if (view && *view) {
		WNDPROC oldWndProc = (*view)->getMainWndProc();
		if (message == WM_PARENTNOTIFY && wParam == WM_CREATE) {
			SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)oldWndProc);
			(*view)->setDocWndProc((WNDPROC)SetWindowLongPtr((HWND)lParam, GWLP_WNDPROC, (LONG_PTR)IEEmbedDocWindowProcedure));
		}
		return CallWindowProc(oldWndProc, hwnd, message, wParam, lParam);
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}


IEEmbed::IEEmbed(HWND _parent)
{
	MSG msg;
	parent = _parent;
	GetClientRect(_parent, &rcClient);
	if (SUCCEEDED(pWebBrowser.CoCreateInstance(CLSID_WebBrowser, NULL, CLSCTX_INPROC))) {
		CComPtr<IOleObject> pOleObject;
		if (SUCCEEDED(pWebBrowser.QueryInterface(&pOleObject))) {
			pOleObject->SetClientSite(this);
			pOleObject->DoVerb(OLEIVERB_INPLACEACTIVATE, &msg, this, 0, this->parent, &rcClient);
		}
		else MessageBox(NULL, TranslateT("IID_IOleObject failed."), TranslateT("RESULT"), MB_OK);

		CComPtr<IOleInPlaceObject> pOleInPlace;
		if (SUCCEEDED(pWebBrowser.QueryInterface(&pOleInPlace)))
			pOleInPlace->GetWindow(&hwnd);
		else
			MessageBox(NULL, TranslateT("IID_IOleInPlaceObject failed."), TranslateT("RESULT"), MB_OK);

		//setBorder();
		CComPtr<IConnectionPointContainer> pCPContainer;
		// Step 1: Get a pointer to the connection point container.
		if (SUCCEEDED(pWebBrowser.QueryInterface(&pCPContainer))) {
			// m_pConnectionPoint is defined like this:
			// Step 2: Find the connection point.
			if (SUCCEEDED(pCPContainer->FindConnectionPoint(DIID_DWebBrowserEvents2, &m_pConnectionPoint))) {
				// Step 3: Advise the connection point that you
				// want to sink its events.
				sink = new IEEmbedSink(this);
				if (FAILED(m_pConnectionPoint->Advise(sink, &m_dwCookie)))
					MessageBox(NULL, TranslateT("Failed to Advise"), TranslateT("C++ Event Sink"), MB_OK);
			}
		}
		setMainWndProc((WNDPROC)SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)IEEmbedWindowProcedure));
		SetFocus(hwnd);
	}

	pWebBrowser->put_RegisterAsDropTarget(VARIANT_FALSE);
}

IEEmbed::~IEEmbed()
{
	CComPtr<IOleObject> pOleObject;
	if (SUCCEEDED(pWebBrowser.QueryInterface(&pOleObject)))
		pOleObject->SetClientSite(NULL);
	else
		MessageBox(NULL, TranslateT("IID_IOleObject failed."), TranslateT("RESULT"), MB_OK);

	if (m_pConnectionPoint != NULL)
		m_pConnectionPoint->Unadvise(m_dwCookie);

	if (sink != NULL)
		delete sink;
	DestroyWindow(hwnd);
}

void IEEmbed::ResizeBrowser()
{
	CComPtr<IOleInPlaceObject> pOleInPlace;

	GetClientRect(parent, &rcClient);
	if (SUCCEEDED(pWebBrowser.QueryInterface(&pOleInPlace)))
		pOleInPlace->SetObjectRects(&rcClient, &rcClient);
}


// IUnknown
STDMETHODIMP IEEmbed::QueryInterface(REFIID riid, PVOID *ppv)
{
	*ppv = NULL;
	if (IID_IUnknown == riid)
		*ppv = this;
	if (IID_IOleClientSite == riid)
		*ppv = (IOleClientSite*)this;//Unknown)m_pIOleClientSite;
	if (IID_IOleWindow == riid || IID_IOleInPlaceSite == riid)
		*ppv = (IOleInPlaceSite*)this;//m_pIOleIPSite;

	if (NULL != *ppv) {
		((LPUNKNOWN)*ppv)->AddRef();
		return NOERROR;
	}
	return E_NOINTERFACE;
}

STDMETHODIMP_(ULONG) IEEmbed::AddRef(void)
{
	++m_cRef;
	return m_cRef;
}

STDMETHODIMP_(ULONG) IEEmbed::Release(void)
{
	--m_cRef;
	return m_cRef;
}

// IDispatch
STDMETHODIMP IEEmbed::GetTypeInfoCount(UINT *) { return E_NOTIMPL; }
STDMETHODIMP IEEmbed::GetTypeInfo(UINT, LCID, LPTYPEINFO*) { return S_OK; }
STDMETHODIMP IEEmbed::GetIDsOfNames(REFIID, LPOLESTR*, UINT, LCID, DISPID*) { return S_OK; }
STDMETHODIMP IEEmbed::Invoke(DISPID, REFIID, LCID, WORD, DISPPARAMS*, VARIANT*, EXCEPINFO*, UINT*) { return DISP_E_MEMBERNOTFOUND; }

// IOleWindow
STDMETHODIMP IEEmbed::GetWindow(HWND *phwnd)
{
	*phwnd = parent;
	return S_OK;
}

STDMETHODIMP IEEmbed::ContextSensitiveHelp(BOOL) { return E_NOTIMPL; }
STDMETHODIMP IEEmbed::CanInPlaceActivate(void) { return S_OK; }
STDMETHODIMP IEEmbed::OnInPlaceActivate(void) {	return S_OK; }
STDMETHODIMP IEEmbed::OnUIActivate(void) { return E_NOTIMPL; }

STDMETHODIMP IEEmbed::GetWindowContext(IOleInPlaceFrame **, IOleInPlaceUIWindow **, LPRECT lprcPosRect, LPRECT lprcClipRect, LPOLEINPLACEFRAMEINFO)
{
	lprcPosRect->left = rcClient.left;
	lprcPosRect->top = rcClient.top;
	lprcPosRect->right = rcClient.right;
	lprcPosRect->bottom = rcClient.bottom;
	lprcClipRect->left = rcClient.left;
	lprcClipRect->top = rcClient.top;
	lprcClipRect->right = rcClient.right;
	lprcClipRect->bottom = rcClient.bottom;
	return S_OK;
}

STDMETHODIMP IEEmbed::Scroll(SIZE) { return E_NOTIMPL; }
STDMETHODIMP IEEmbed::OnUIDeactivate(BOOL) { return E_NOTIMPL; }
STDMETHODIMP IEEmbed::OnInPlaceDeactivate(void) { return S_OK; }
STDMETHODIMP IEEmbed::DiscardUndoState(void) { return E_NOTIMPL; }
STDMETHODIMP IEEmbed::DeactivateAndUndo(void) { return E_NOTIMPL; }
STDMETHODIMP IEEmbed::OnPosRectChange(LPCRECT) { return E_NOTIMPL; }
STDMETHODIMP IEEmbed::SaveObject(void) { return E_NOTIMPL; }
STDMETHODIMP IEEmbed::GetMoniker(DWORD, DWORD, IMoniker **) { return E_NOTIMPL; }
STDMETHODIMP IEEmbed::GetContainer(IOleContainer **) { return E_NOTIMPL; }
STDMETHODIMP IEEmbed::ShowObject(void) { return E_NOTIMPL; }
STDMETHODIMP IEEmbed::OnShowWindow(BOOL) { return E_NOTIMPL; }
STDMETHODIMP IEEmbed::RequestNewObjectLayout(void) { return E_NOTIMPL; }


void IEEmbed::setMainWndProc(WNDPROC wndProc)
{
	mainWndProc = wndProc;
}

WNDPROC IEEmbed::getMainWndProc()
{
	return mainWndProc;
}

void IEEmbed::setDocWndProc(WNDPROC wndProc)
{
	docWndProc = wndProc;
}

WNDPROC IEEmbed::getDocWndProc()
{
	return docWndProc;
}

void IEEmbed::setServerWndProc(WNDPROC wndProc)
{
	serverWndProc = wndProc;
}

WNDPROC IEEmbed::getServerWndProc()
{
	return serverWndProc;
}

void IEEmbed::write(const wchar_t *text)
{
	CComPtr<IHTMLDocument2> document = getDocument();
	if (!document) return;

	SAFEARRAY *safe_array = ::SafeArrayCreateVector(VT_VARIANT, 0, 1);
	if (safe_array != NULL) {
		VARIANT *variant;
		::SafeArrayAccessData(safe_array, (LPVOID *)&variant);
		variant->vt = VT_BSTR;
		variant->bstrVal = ::SysAllocString(text);
		::SafeArrayUnaccessData(safe_array);
		document->write(safe_array);
		// ::SysFreeString(bstr); // don't free it !!!!!!!
		::SafeArrayDestroy(safe_array);
		document->close();
	}
}

void IEEmbed::addCookie(const wchar_t *cookieString)
{
	CComPtr<IHTMLDocument2> document = getDocument();
	if (!document) return;
	BSTR cookie = SysAllocString(cookieString);

	document->put_cookie(cookie);

	SysFreeString(cookie);
}

BSTR IEEmbed::getCookies()
{
	CComPtr<IHTMLDocument2> document = getDocument();
	BSTR cookie = NULL;

	if (!document) return NULL;
	document->get_cookie(&cookie);
	return cookie;
}

IHTMLDocument2* IEEmbed::getDocument()
{
	CComPtr<IDispatch> dispatch;
	if (SUCCEEDED(pWebBrowser->get_Document(&dispatch)) && dispatch != NULL) {
		CComPtr<IHTMLDocument2> document;
		dispatch.QueryInterface(&document);
		return document.Detach();
	}

	return NULL;
}

void IEEmbed::navigate(const wchar_t *url)
{
	pWebBrowser->Navigate((WCHAR *)url, NULL, NULL, NULL, NULL);
}

void IEEmbed::navigate(char *url)
{
	wchar_t *pwszUrl = mir_a2u(url);
	navigate(pwszUrl);
	mir_free(pwszUrl);
}

void IEEmbed::navigate(NETLIBHTTPREQUEST *nlhr)
{
	WCHAR *szUrl = mir_a2u(nlhr->szUrl);
	BSTR bstrHeaders;
    LPSAFEARRAY psa;
	LPSTR pPostData;
    VARIANT vPostData = {0}, vHeaders = {0};

	bstrHeaders = SysAllocString(L"Content-Type: application/x-www-form-urlencoded\r\n");
    V_VT(&vHeaders) = VT_BSTR;
    V_BSTR(&vHeaders) = bstrHeaders;
    VariantInit(&vPostData);
    psa = SafeArrayCreateVector(VT_UI1, 0, nlhr->dataLength);
    SafeArrayAccessData(psa, (LPVOID*)&pPostData);
	memcpy(pPostData, nlhr->pData, nlhr->dataLength);
    SafeArrayUnaccessData(psa);
    V_VT(&vPostData) = VT_ARRAY | VT_UI1;
    V_ARRAY(&vPostData) = psa;
	pWebBrowser->Navigate(szUrl, NULL, NULL, &vPostData, &vHeaders);
	SysFreeString(bstrHeaders);
	VariantClear(&vPostData);
	mir_free(szUrl);
}

char *IEEmbed::GetHTMLDoc() {
	CComPtr<IDispatch> spDispDoc;
	char *pszRet = NULL;

	if (SUCCEEDED(pWebBrowser->get_Document(&spDispDoc))) {
		CComPtr<IHTMLDocument3> spDoc;

        if (SUCCEEDED(spDispDoc->QueryInterface(IID_IHTMLDocument3, (void**)&spDoc))) {
			CComPtr<IHTMLElement> spRootElement;
			if (SUCCEEDED(spDoc->get_documentElement(&spRootElement)))
			{
				BSTR bstrDoc;
				if (SUCCEEDED(spRootElement->get_outerHTML(&bstrDoc)))
				{
					pszRet = mir_u2a(bstrDoc);
					SysFreeString(bstrDoc);
				}
			}
		}
	}
	return pszRet;
}

void IEEmbed::translateAccelerator(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	CComPtr<IOleInPlaceActiveObject> pIOIPAO;
	if (SUCCEEDED(pWebBrowser.QueryInterface(&pIOIPAO))) {
		MSG msg;
		msg.message = uMsg;
		msg.wParam = wParam;
		msg.lParam = lParam;
		pIOIPAO->TranslateAccelerator(&msg);
	}
}