user32: Use window height/width instead of client for combo resizing.
[wine] / dlls / ieframe / navigate.c
1 /*
2  * Copyright 2006-2007 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #define NONAMELESSUNION
20 #define NONAMELESSSTRUCT
21
22 #include "ieframe.h"
23
24 #include "exdispid.h"
25 #include "shellapi.h"
26 #include "winreg.h"
27 #include "shlwapi.h"
28 #include "wininet.h"
29 #include "mshtml.h"
30 #include "resource.h"
31
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(ieframe);
35
36 static const WCHAR emptyW[] = {0};
37
38 typedef struct {
39     IBindStatusCallback  IBindStatusCallback_iface;
40     IHttpNegotiate       IHttpNegotiate_iface;
41     IHttpSecurity        IHttpSecurity_iface;
42
43     LONG ref;
44
45     DocHost *doc_host;
46     IBinding *binding;
47
48     BSTR url;
49     HGLOBAL post_data;
50     BSTR headers;
51     ULONG post_data_len;
52 } BindStatusCallback;
53
54 static void dump_BINDINFO(BINDINFO *bi)
55 {
56     static const char * const BINDINFOF_str[] = {
57         "#0",
58         "BINDINFOF_URLENCODESTGMEDDATA",
59         "BINDINFOF_URLENCODEDEXTRAINFO"
60     };
61
62     static const char * const BINDVERB_str[] = {
63         "BINDVERB_GET",
64         "BINDVERB_POST",
65         "BINDVERB_PUT",
66         "BINDVERB_CUSTOM"
67     };
68
69     TRACE("\n"
70             "BINDINFO = {\n"
71             "    %d, %s,\n"
72             "    {%d, %p, %p},\n"
73             "    %s,\n"
74             "    %s,\n"
75             "    %s,\n"
76             "    %d, %08x, %d, %d\n"
77             "    {%d %p %x},\n"
78             "    %s\n"
79             "    %p, %d\n"
80             "}\n",
81
82             bi->cbSize, debugstr_w(bi->szExtraInfo),
83             bi->stgmedData.tymed, bi->stgmedData.u.hGlobal, bi->stgmedData.pUnkForRelease,
84             bi->grfBindInfoF > BINDINFOF_URLENCODEDEXTRAINFO
85                 ? "unknown" : BINDINFOF_str[bi->grfBindInfoF],
86             bi->dwBindVerb > BINDVERB_CUSTOM
87                 ? "unknown" : BINDVERB_str[bi->dwBindVerb],
88             debugstr_w(bi->szCustomVerb),
89             bi->cbstgmedData, bi->dwOptions, bi->dwOptionsFlags, bi->dwCodePage,
90             bi->securityAttributes.nLength,
91             bi->securityAttributes.lpSecurityDescriptor,
92             bi->securityAttributes.bInheritHandle,
93             debugstr_guid(&bi->iid),
94             bi->pUnk, bi->dwReserved
95             );
96 }
97
98 static void set_status_text(BindStatusCallback *This, ULONG statuscode, LPCWSTR str)
99 {
100     VARIANTARG arg;
101     DISPPARAMS dispparams = {&arg, NULL, 1, 0};
102     WCHAR fmt[IDS_STATUSFMT_MAXLEN];
103     WCHAR buffer[IDS_STATUSFMT_MAXLEN + INTERNET_MAX_URL_LENGTH];
104
105     if(!This->doc_host)
106         return;
107
108     TRACE("(%p, %d, %s)\n", This, statuscode, debugstr_w(str));
109     buffer[0] = 0;
110     if (statuscode && str && *str) {
111         fmt[0] = 0;
112         /* the format string must have one "%s" for the str */
113         LoadStringW(ieframe_instance, IDS_STATUSFMT_FIRST + statuscode, fmt, IDS_STATUSFMT_MAXLEN);
114         snprintfW(buffer, sizeof(buffer)/sizeof(WCHAR), fmt, str);
115     }
116
117     V_VT(&arg) = VT_BSTR;
118     V_BSTR(&arg) = str ? SysAllocString(buffer) : NULL;
119     TRACE("=> %s\n", debugstr_w(V_BSTR(&arg)));
120
121     call_sink(This->doc_host->cps.wbe2, DISPID_STATUSTEXTCHANGE, &dispparams);
122
123     if(This->doc_host->frame)
124         IOleInPlaceFrame_SetStatusText(This->doc_host->frame, buffer);
125
126     VariantClear(&arg);
127
128 }
129
130 HRESULT set_dochost_url(DocHost *This, const WCHAR *url)
131 {
132     WCHAR *new_url;
133
134     if(url) {
135         new_url = heap_strdupW(url);
136         if(!new_url)
137             return E_OUTOFMEMORY;
138     }else {
139         new_url = NULL;
140     }
141
142     heap_free(This->url);
143     This->url = new_url;
144
145     This->container_vtbl->SetURL(This, This->url);
146     return S_OK;
147 }
148
149 static inline BindStatusCallback *impl_from_IBindStatusCallback(IBindStatusCallback *iface)
150 {
151     return CONTAINING_RECORD(iface, BindStatusCallback, IBindStatusCallback_iface);
152 }
153
154 static HRESULT WINAPI BindStatusCallback_QueryInterface(IBindStatusCallback *iface,
155                                                         REFIID riid, void **ppv)
156 {
157     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
158
159     if(IsEqualGUID(&IID_IUnknown, riid)) {
160         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
161         *ppv = &This->IBindStatusCallback_iface;
162     }else if(IsEqualGUID(&IID_IBindStatusCallback, riid)) {
163         TRACE("(%p)->(IID_IBindStatusCallback %p)\n", This, ppv);
164         *ppv = &This->IBindStatusCallback_iface;
165     }else if(IsEqualGUID(&IID_IHttpNegotiate, riid)) {
166         TRACE("(%p)->(IID_IHttpNegotiate %p)\n", This, ppv);
167         *ppv = &This->IHttpNegotiate_iface;
168     }else if(IsEqualGUID(&IID_IWindowForBindingUI, riid)) {
169         TRACE("(%p)->(IID_IWindowForBindingUI %p)\n", This, ppv);
170         *ppv = &This->IHttpSecurity_iface;
171     }else if(IsEqualGUID(&IID_IHttpSecurity, riid)) {
172         TRACE("(%p)->(IID_IHttpSecurity %p)\n", This, ppv);
173         *ppv = &This->IHttpSecurity_iface;
174     }else {
175         *ppv = NULL;
176         WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
177         return E_NOINTERFACE;
178     }
179
180     IUnknown_AddRef((IUnknown*)*ppv);
181     return S_OK;
182 }
183
184 static ULONG WINAPI BindStatusCallback_AddRef(IBindStatusCallback *iface)
185 {
186     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
187     LONG ref = InterlockedIncrement(&This->ref);
188
189     TRACE("(%p) ref=%d\n", This, ref);
190
191     return ref;
192 }
193
194 static ULONG WINAPI BindStatusCallback_Release(IBindStatusCallback *iface)
195 {
196     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
197     LONG ref = InterlockedDecrement(&This->ref);
198
199     TRACE("(%p) ref=%d\n", This, ref);
200
201     if(!ref) {
202         if(This->doc_host)
203             IOleClientSite_Release(&This->doc_host->IOleClientSite_iface);
204         if(This->binding)
205             IBinding_Release(This->binding);
206         if(This->post_data)
207             GlobalFree(This->post_data);
208         SysFreeString(This->headers);
209         SysFreeString(This->url);
210         heap_free(This);
211     }
212
213     return ref;
214 }
215
216 static HRESULT WINAPI BindStatusCallback_OnStartBinding(IBindStatusCallback *iface,
217        DWORD dwReserved, IBinding *pbind)
218 {
219     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
220
221     TRACE("(%p)->(%d %p)\n", This, dwReserved, pbind);
222
223     This->binding = pbind;
224     IBinding_AddRef(This->binding);
225
226     return S_OK;
227 }
228
229 static HRESULT WINAPI BindStatusCallback_GetPriority(IBindStatusCallback *iface,
230        LONG *pnPriority)
231 {
232     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
233     FIXME("(%p)->(%p)\n", This, pnPriority);
234     return E_NOTIMPL;
235 }
236
237 static HRESULT WINAPI BindStatusCallback_OnLowResource(IBindStatusCallback *iface,
238        DWORD reserved)
239 {
240     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
241     FIXME("(%p)->(%d)\n", This, reserved);
242     return E_NOTIMPL;
243 }
244
245 static DWORD get_http_status_code(IBinding *binding)
246 {
247     IWinInetHttpInfo *http_info;
248     DWORD status, size = sizeof(DWORD);
249     HRESULT hres;
250
251     hres = IBinding_QueryInterface(binding, &IID_IWinInetHttpInfo, (void**)&http_info);
252     if(FAILED(hres))
253         return HTTP_STATUS_OK;
254
255     hres = IWinInetHttpInfo_QueryInfo(http_info, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER,
256             &status, &size, NULL, NULL);
257     IWinInetHttpInfo_Release(http_info);
258
259     if(FAILED(hres))
260         return HTTP_STATUS_OK;
261     return status;
262 }
263
264 static HRESULT WINAPI BindStatusCallback_OnProgress(IBindStatusCallback *iface,
265         ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
266 {
267     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
268     DWORD status_code;
269
270     TRACE("(%p)->(%d %d %d %s)\n", This, ulProgress, ulProgressMax, ulStatusCode,
271           debugstr_w(szStatusText));
272
273     switch(ulStatusCode) {
274     case BINDSTATUS_REDIRECTING:
275         return set_dochost_url(This->doc_host, szStatusText);
276     case BINDSTATUS_BEGINDOWNLOADDATA:
277         set_status_text(This, ulStatusCode, szStatusText);
278         status_code = get_http_status_code(This->binding);
279         if(status_code != HTTP_STATUS_OK)
280             handle_navigation_error(This->doc_host, status_code, This->url, NULL);
281         return S_OK;
282
283     case BINDSTATUS_FINDINGRESOURCE:
284     case BINDSTATUS_ENDDOWNLOADDATA:
285     case BINDSTATUS_SENDINGREQUEST:
286         set_status_text(This, ulStatusCode, szStatusText);
287         return S_OK;
288
289     case BINDSTATUS_CONNECTING:
290     case BINDSTATUS_CACHEFILENAMEAVAILABLE:
291     case BINDSTATUS_CLASSIDAVAILABLE:
292     case BINDSTATUS_MIMETYPEAVAILABLE:
293     case BINDSTATUS_BEGINSYNCOPERATION:
294     case BINDSTATUS_ENDSYNCOPERATION:
295         return S_OK;
296     default:
297         FIXME("status code %u\n", ulStatusCode);
298     }
299
300     return S_OK;
301 }
302
303 void handle_navigation_error(DocHost* doc_host, HRESULT hres, BSTR url, IHTMLWindow2 *win2)
304 {
305     VARIANT var_status_code, var_frame_name, var_url;
306     DISPPARAMS dispparams;
307     VARIANTARG params[5];
308     VARIANT_BOOL cancel = VARIANT_FALSE;
309
310     dispparams.cArgs = 5;
311     dispparams.cNamedArgs = 0;
312     dispparams.rgdispidNamedArgs = NULL;
313     dispparams.rgvarg = params;
314
315     V_VT(params) = VT_BOOL|VT_BYREF;
316     V_BOOLREF(params) = &cancel;
317
318     V_VT(params+1) = VT_VARIANT|VT_BYREF;
319     V_VARIANTREF(params+1) = &var_status_code;
320     V_VT(&var_status_code) = VT_I4;
321     V_I4(&var_status_code) = hres;
322
323     V_VT(params+2) = VT_VARIANT|VT_BYREF;
324     V_VARIANTREF(params+2) = &var_frame_name;
325     V_VT(&var_frame_name) = VT_BSTR;
326     if(win2) {
327         hres = IHTMLWindow2_get_name(win2, &V_BSTR(&var_frame_name));
328         if(FAILED(hres))
329             V_BSTR(&var_frame_name) = NULL;
330     } else
331         V_BSTR(&var_frame_name) = NULL;
332
333     V_VT(params+3) = VT_VARIANT|VT_BYREF;
334     V_VARIANTREF(params+3) = &var_url;
335     V_VT(&var_url) = VT_BSTR;
336     V_BSTR(&var_url) = url;
337
338     V_VT(params+4) = VT_DISPATCH;
339     V_DISPATCH(params+4) = (IDispatch*)doc_host->wb;
340
341     call_sink(doc_host->cps.wbe2, DISPID_NAVIGATEERROR, &dispparams);
342     SysFreeString(V_BSTR(&var_frame_name));
343
344     if(!cancel)
345         FIXME("Navigate to error page\n");
346 }
347
348 static HRESULT WINAPI BindStatusCallback_OnStopBinding(IBindStatusCallback *iface,
349         HRESULT hresult, LPCWSTR szError)
350 {
351     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
352
353     TRACE("(%p)->(%08x %s)\n", This, hresult, debugstr_w(szError));
354
355     set_status_text(This, 0, emptyW);
356
357     if(!This->doc_host)
358         return S_OK;
359
360     if(FAILED(hresult))
361         handle_navigation_error(This->doc_host, hresult, This->url, NULL);
362
363     IOleClientSite_Release(&This->doc_host->IOleClientSite_iface);
364     This->doc_host = NULL;
365
366     IBinding_Release(This->binding);
367     This->binding = NULL;
368
369     return S_OK;
370 }
371
372 static HRESULT WINAPI BindStatusCallback_GetBindInfo(IBindStatusCallback *iface,
373         DWORD *grfBINDF, BINDINFO *pbindinfo)
374 {
375     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
376
377     TRACE("(%p)->(%p %p)\n", This, grfBINDF, pbindinfo);
378
379     *grfBINDF = BINDF_ASYNCHRONOUS;
380
381     if(This->post_data) {
382         pbindinfo->dwBindVerb = BINDVERB_POST;
383
384         pbindinfo->stgmedData.tymed = TYMED_HGLOBAL;
385         pbindinfo->stgmedData.u.hGlobal = This->post_data;
386         pbindinfo->cbstgmedData = This->post_data_len;
387         pbindinfo->stgmedData.pUnkForRelease = (IUnknown*)&This->IBindStatusCallback_iface;
388         IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
389     }
390
391     return S_OK;
392 }
393
394 static HRESULT WINAPI BindStatusCallback_OnDataAvailable(IBindStatusCallback *iface,
395         DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed)
396 {
397     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
398     FIXME("(%p)->(%08x %d %p %p)\n", This, grfBSCF, dwSize, pformatetc, pstgmed);
399     return E_NOTIMPL;
400 }
401
402 static HRESULT WINAPI BindStatusCallback_OnObjectAvailable(IBindStatusCallback *iface,
403         REFIID riid, IUnknown *punk)
404 {
405     BindStatusCallback *This = impl_from_IBindStatusCallback(iface);
406
407     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), punk);
408
409     return dochost_object_available(This->doc_host, punk);
410 }
411
412 static const IBindStatusCallbackVtbl BindStatusCallbackVtbl = {
413     BindStatusCallback_QueryInterface,
414     BindStatusCallback_AddRef,
415     BindStatusCallback_Release,
416     BindStatusCallback_OnStartBinding,
417     BindStatusCallback_GetPriority,
418     BindStatusCallback_OnLowResource,
419     BindStatusCallback_OnProgress,
420     BindStatusCallback_OnStopBinding,
421     BindStatusCallback_GetBindInfo,
422     BindStatusCallback_OnDataAvailable,
423     BindStatusCallback_OnObjectAvailable
424 };
425
426 static inline BindStatusCallback *impl_from_IHttpNegotiate(IHttpNegotiate *iface)
427 {
428     return CONTAINING_RECORD(iface, BindStatusCallback, IHttpNegotiate_iface);
429 }
430
431 static HRESULT WINAPI HttpNegotiate_QueryInterface(IHttpNegotiate *iface,
432                                                    REFIID riid, void **ppv)
433 {
434     BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
435     return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
436 }
437
438 static ULONG WINAPI HttpNegotiate_AddRef(IHttpNegotiate *iface)
439 {
440     BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
441     return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
442 }
443
444 static ULONG WINAPI HttpNegotiate_Release(IHttpNegotiate *iface)
445 {
446     BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
447     return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
448 }
449
450 static HRESULT WINAPI HttpNegotiate_BeginningTransaction(IHttpNegotiate *iface,
451         LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
452 {
453     BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
454
455     TRACE("(%p)->(%s %s %d %p)\n", This, debugstr_w(szURL), debugstr_w(szHeaders),
456           dwReserved, pszAdditionalHeaders);
457
458     if(This->headers) {
459         int size = (strlenW(This->headers)+1)*sizeof(WCHAR);
460         *pszAdditionalHeaders = CoTaskMemAlloc(size);
461         memcpy(*pszAdditionalHeaders, This->headers, size);
462     }
463
464     return S_OK;
465 }
466
467 static HRESULT WINAPI HttpNegotiate_OnResponse(IHttpNegotiate *iface,
468         DWORD dwResponseCode, LPCWSTR szResponseHeaders, LPCWSTR szRequestHeaders,
469         LPWSTR *pszAdditionalRequestHeaders)
470 {
471     BindStatusCallback *This = impl_from_IHttpNegotiate(iface);
472     TRACE("(%p)->(%d %s %s %p)\n", This, dwResponseCode, debugstr_w(szResponseHeaders),
473           debugstr_w(szRequestHeaders), pszAdditionalRequestHeaders);
474     return S_OK;
475 }
476
477 static const IHttpNegotiateVtbl HttpNegotiateVtbl = {
478     HttpNegotiate_QueryInterface,
479     HttpNegotiate_AddRef,
480     HttpNegotiate_Release,
481     HttpNegotiate_BeginningTransaction,
482     HttpNegotiate_OnResponse
483 };
484
485 static inline BindStatusCallback *impl_from_IHttpSecurity(IHttpSecurity *iface)
486 {
487     return CONTAINING_RECORD(iface, BindStatusCallback, IHttpSecurity_iface);
488 }
489
490 static HRESULT WINAPI HttpSecurity_QueryInterface(IHttpSecurity *iface, REFIID riid, void **ppv)
491 {
492     BindStatusCallback *This = impl_from_IHttpSecurity(iface);
493     return IBindStatusCallback_QueryInterface(&This->IBindStatusCallback_iface, riid, ppv);
494 }
495
496 static ULONG WINAPI HttpSecurity_AddRef(IHttpSecurity *iface)
497 {
498     BindStatusCallback *This = impl_from_IHttpSecurity(iface);
499     return IBindStatusCallback_AddRef(&This->IBindStatusCallback_iface);
500 }
501
502 static ULONG WINAPI HttpSecurity_Release(IHttpSecurity *iface)
503 {
504     BindStatusCallback *This = impl_from_IHttpSecurity(iface);
505     return IBindStatusCallback_Release(&This->IBindStatusCallback_iface);
506 }
507
508 static HRESULT WINAPI HttpSecurity_GetWindow(IHttpSecurity *iface, REFGUID rguidReason, HWND *phwnd)
509 {
510     BindStatusCallback *This = impl_from_IHttpSecurity(iface);
511
512     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(rguidReason), phwnd);
513
514     if(!This->doc_host)
515         return E_FAIL;
516
517     *phwnd = This->doc_host->frame_hwnd;
518     return S_OK;
519 }
520
521 static HRESULT WINAPI HttpSecurity_OnSecurityProblem(IHttpSecurity *iface, DWORD dwProblem)
522 {
523     BindStatusCallback *This = impl_from_IHttpSecurity(iface);
524     FIXME("(%p)->(%u)\n", This, dwProblem);
525     return S_FALSE;
526 }
527
528 static const IHttpSecurityVtbl HttpSecurityVtbl = {
529     HttpSecurity_QueryInterface,
530     HttpSecurity_AddRef,
531     HttpSecurity_Release,
532     HttpSecurity_GetWindow,
533     HttpSecurity_OnSecurityProblem
534 };
535
536 static BindStatusCallback *create_callback(DocHost *doc_host, LPCWSTR url, PBYTE post_data,
537         ULONG post_data_len, LPCWSTR headers)
538 {
539     BindStatusCallback *ret = heap_alloc(sizeof(BindStatusCallback));
540
541     ret->IBindStatusCallback_iface.lpVtbl = &BindStatusCallbackVtbl;
542     ret->IHttpNegotiate_iface.lpVtbl      = &HttpNegotiateVtbl;
543     ret->IHttpSecurity_iface.lpVtbl       = &HttpSecurityVtbl;
544
545     ret->ref = 1;
546     ret->url = SysAllocString(url);
547     ret->post_data = NULL;
548     ret->post_data_len = post_data_len;
549     ret->headers = headers ? SysAllocString(headers) : NULL;
550
551     ret->doc_host = doc_host;
552     IOleClientSite_AddRef(&doc_host->IOleClientSite_iface);
553
554     ret->binding = NULL;
555
556     if(post_data) {
557         ret->post_data = GlobalAlloc(0, post_data_len);
558         memcpy(ret->post_data, post_data, post_data_len);
559     }
560
561     return ret;
562 }
563
564 static void on_before_navigate2(DocHost *This, LPCWSTR url, SAFEARRAY *post_data, LPWSTR headers, VARIANT_BOOL *cancel)
565 {
566     VARIANT var_url, var_flags, var_frame_name, var_post_data, var_post_data2, var_headers;
567     DISPPARAMS dispparams;
568     VARIANTARG params[7];
569     WCHAR file_path[MAX_PATH];
570     DWORD file_path_len = sizeof(file_path) / sizeof(*file_path);
571
572     dispparams.cArgs = 7;
573     dispparams.cNamedArgs = 0;
574     dispparams.rgdispidNamedArgs = NULL;
575     dispparams.rgvarg = params;
576
577     This->busy = VARIANT_TRUE;
578
579     V_VT(params) = VT_BOOL|VT_BYREF;
580     V_BOOLREF(params) = cancel;
581
582     V_VT(params+1) = (VT_BYREF|VT_VARIANT);
583     V_VARIANTREF(params+1) = &var_headers;
584     V_VT(&var_headers) = VT_BSTR;
585     V_BSTR(&var_headers) = headers;
586
587     V_VT(params+2) = (VT_BYREF|VT_VARIANT);
588     V_VARIANTREF(params+2) = &var_post_data2;
589     V_VT(&var_post_data2) = (VT_BYREF|VT_VARIANT);
590     V_VARIANTREF(&var_post_data2) = &var_post_data;
591
592     if(post_data) {
593         V_VT(&var_post_data) = VT_UI1|VT_ARRAY;
594         V_ARRAY(&var_post_data) = post_data;
595     }else {
596         V_VT(&var_post_data) = VT_EMPTY;
597     }
598
599     V_VT(params+3) = (VT_BYREF|VT_VARIANT);
600     V_VARIANTREF(params+3) = &var_frame_name;
601     V_VT(&var_frame_name) = VT_BSTR;
602     V_BSTR(&var_frame_name) = NULL;
603
604     V_VT(params+4) = (VT_BYREF|VT_VARIANT);
605     V_VARIANTREF(params+4) = &var_flags;
606     V_VT(&var_flags) = VT_I4;
607     V_I4(&var_flags) = 0;
608
609     V_VT(params+5) = (VT_BYREF|VT_VARIANT);
610     V_VARIANTREF(params+5) = &var_url;
611     V_VT(&var_url) = VT_BSTR;
612     if(PathCreateFromUrlW(url, file_path, &file_path_len, 0) == S_OK)
613         V_BSTR(&var_url) = SysAllocString(file_path);
614     else
615         V_BSTR(&var_url) = SysAllocString(url);
616
617     V_VT(params+6) = (VT_DISPATCH);
618     V_DISPATCH(params+6) = (IDispatch*)This->wb;
619
620     call_sink(This->cps.wbe2, DISPID_BEFORENAVIGATE2, &dispparams);
621
622     SysFreeString(V_BSTR(&var_url));
623 }
624
625 /* FIXME: urlmon should handle it */
626 static BOOL try_application_url(LPCWSTR url)
627 {
628     SHELLEXECUTEINFOW exec_info;
629     WCHAR app[64];
630     HKEY hkey;
631     DWORD res, type;
632     HRESULT hres;
633
634     static const WCHAR wszURLProtocol[] = {'U','R','L',' ','P','r','o','t','o','c','o','l',0};
635
636     hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, app, sizeof(app)/sizeof(WCHAR), NULL, 0);
637     if(FAILED(hres))
638         return FALSE;
639
640     res = RegOpenKeyW(HKEY_CLASSES_ROOT, app, &hkey);
641     if(res != ERROR_SUCCESS)
642         return FALSE;
643
644     res = RegQueryValueExW(hkey, wszURLProtocol, NULL, &type, NULL, NULL);
645     RegCloseKey(hkey);
646     if(res != ERROR_SUCCESS || type != REG_SZ)
647         return FALSE;
648
649     TRACE("opening application %s\n", debugstr_w(app));
650
651     memset(&exec_info, 0, sizeof(exec_info));
652     exec_info.cbSize = sizeof(exec_info);
653     exec_info.lpFile = url;
654     exec_info.nShow = SW_SHOW;
655
656     return ShellExecuteExW(&exec_info);
657 }
658
659 static HRESULT create_moniker(LPCWSTR url, IMoniker **mon)
660 {
661     WCHAR new_url[INTERNET_MAX_URL_LENGTH];
662     DWORD size;
663     HRESULT hres;
664
665     if(PathIsURLW(url))
666         return CreateURLMoniker(NULL, url, mon);
667
668     size = sizeof(new_url)/sizeof(WCHAR);
669     hres = UrlApplySchemeW(url, new_url, &size, URL_APPLY_GUESSSCHEME | URL_APPLY_GUESSFILE);
670     TRACE("was %s got %s\n", debugstr_w(url), debugstr_w(new_url));
671     if(FAILED(hres)) {
672         WARN("UrlApplyScheme failed: %08x\n", hres);
673         return hres;
674     }
675
676     return CreateURLMoniker(NULL, new_url, mon);
677 }
678
679 static HRESULT bind_to_object(DocHost *This, IMoniker *mon, LPCWSTR url, IBindCtx *bindctx,
680                               IBindStatusCallback *callback)
681 {
682     IUnknown *unk = NULL;
683     WCHAR *display_name;
684     HRESULT hres;
685
686     if(mon) {
687         IMoniker_AddRef(mon);
688     }else {
689         hres = create_moniker(url, &mon);
690         if(FAILED(hres))
691             return hres;
692     }
693
694     hres = IMoniker_GetDisplayName(mon, 0, NULL, &display_name);
695     if(FAILED(hres)) {
696         FIXME("GetDisplayName failed: %08x\n", hres);
697         return hres;
698     }
699
700     hres = set_dochost_url(This, display_name);
701     CoTaskMemFree(display_name);
702     if(FAILED(hres))
703         return hres;
704
705     IBindCtx_RegisterObjectParam(bindctx, (LPOLESTR)SZ_HTML_CLIENTSITE_OBJECTPARAM,
706                                  (IUnknown*)&This->IOleClientSite_iface);
707
708     hres = IMoniker_BindToObject(mon, bindctx, NULL, &IID_IUnknown, (void**)&unk);
709     if(SUCCEEDED(hres)) {
710         hres = S_OK;
711         if(unk)
712             IUnknown_Release(unk);
713     }else if(try_application_url(url)) {
714         hres = S_OK;
715     }else {
716         FIXME("BindToObject failed: %08x\n", hres);
717     }
718
719     IMoniker_Release(mon);
720     return S_OK;
721 }
722
723 static void html_window_navigate(DocHost *This, IHTMLPrivateWindow *window, BSTR url, BSTR headers, SAFEARRAY *post_data)
724 {
725     VARIANT headers_var, post_data_var;
726     BSTR empty_str;
727     HRESULT hres;
728
729     hres = set_dochost_url(This, url);
730     if(FAILED(hres))
731         return;
732
733     empty_str = SysAllocStringLen(NULL, 0);
734
735     if(headers) {
736         V_VT(&headers_var) = VT_BSTR;
737         V_BSTR(&headers_var) = headers;
738     }else {
739         V_VT(&headers_var) = VT_EMPTY;
740     }
741
742     if(post_data) {
743         V_VT(&post_data_var) = VT_UI1|VT_ARRAY;
744         V_ARRAY(&post_data_var) = post_data;
745     }else {
746         V_VT(&post_data_var) = VT_EMPTY;
747     }
748
749     set_doc_state(This, READYSTATE_LOADING);
750     hres = IHTMLPrivateWindow_SuperNavigate(window, url, empty_str, NULL, NULL, &post_data_var, &headers_var, 0);
751     SysFreeString(empty_str);
752     if(FAILED(hres))
753         WARN("SuprtNavigate failed: %08x\n", hres);
754 }
755
756 typedef struct {
757     task_header_t header;
758     BSTR url;
759     BSTR headers;
760     SAFEARRAY *post_data;
761     BOOL async_notif;
762 } task_doc_navigate_t;
763
764 static void doc_navigate_task_destr(task_header_t *t)
765 {
766     task_doc_navigate_t *task = (task_doc_navigate_t*)t;
767
768     SysFreeString(task->url);
769     SysFreeString(task->headers);
770     if(task->post_data)
771         SafeArrayDestroy(task->post_data);
772     heap_free(task);
773 }
774
775 static void doc_navigate_proc(DocHost *This, task_header_t *t)
776 {
777     task_doc_navigate_t *task = (task_doc_navigate_t*)t;
778     IHTMLPrivateWindow *priv_window;
779     HRESULT hres;
780
781     if(!This->doc_navigate) {
782         ERR("Skip nav\n");
783         return;
784     }
785
786     if(task->async_notif) {
787         VARIANT_BOOL cancel = VARIANT_FALSE;
788         on_before_navigate2(This, task->url, task->post_data, task->headers, &cancel);
789         if(cancel) {
790             TRACE("Navigation canceled\n");
791             return;
792         }
793     }
794
795     hres = IUnknown_QueryInterface(This->doc_navigate, &IID_IHTMLPrivateWindow, (void**)&priv_window);
796     if(SUCCEEDED(hres)) {
797         html_window_navigate(This, priv_window, task->url, task->headers, task->post_data);
798         IHTMLPrivateWindow_Release(priv_window);
799     }else {
800         WARN("Could not get IHTMLPrivateWindow iface: %08x\n", hres);
801     }
802 }
803
804 static HRESULT async_doc_navigate(DocHost *This, LPCWSTR url, LPCWSTR headers, PBYTE post_data, ULONG post_data_size,
805         BOOL async_notif)
806 {
807     task_doc_navigate_t *task;
808
809     TRACE("%s\n", debugstr_w(url));
810
811     task = heap_alloc_zero(sizeof(*task));
812     if(!task)
813         return E_OUTOFMEMORY;
814
815     task->url = SysAllocString(url);
816     if(!task->url) {
817         doc_navigate_task_destr(&task->header);
818         return E_OUTOFMEMORY;
819     }
820
821     if(headers) {
822         task->headers = SysAllocString(headers);
823         if(!task->headers) {
824             doc_navigate_task_destr(&task->header);
825             return E_OUTOFMEMORY;
826         }
827     }
828
829     if(post_data) {
830         task->post_data = SafeArrayCreateVector(VT_UI1, 0, post_data_size);
831         if(!task->post_data) {
832             doc_navigate_task_destr(&task->header);
833             return E_OUTOFMEMORY;
834         }
835
836         memcpy(task->post_data->pvData, post_data, post_data_size);
837     }
838
839     if(!async_notif) {
840         VARIANT_BOOL cancel = VARIANT_FALSE;
841
842         on_before_navigate2(This, task->url, task->post_data, task->headers, &cancel);
843         if(cancel) {
844             TRACE("Navigation canceled\n");
845             doc_navigate_task_destr(&task->header);
846             return S_OK;
847         }
848     }
849
850     task->async_notif = async_notif;
851     abort_dochost_tasks(This, doc_navigate_proc);
852     push_dochost_task(This, &task->header, doc_navigate_proc, doc_navigate_task_destr, FALSE);
853     return S_OK;
854 }
855
856 static HRESULT navigate_bsc(DocHost *This, BindStatusCallback *bsc, IMoniker *mon)
857 {
858     VARIANT_BOOL cancel = VARIANT_FALSE;
859     SAFEARRAY *post_data = NULL;
860     IBindCtx *bindctx;
861     HRESULT hres;
862
863     set_doc_state(This, READYSTATE_LOADING);
864
865     if(bsc->post_data) {
866         post_data = SafeArrayCreateVector(VT_UI1, 0, bsc->post_data_len);
867         memcpy(post_data->pvData, post_data, bsc->post_data_len);
868     }
869
870     on_before_navigate2(This, bsc->url, post_data, bsc->headers, &cancel);
871     if(post_data)
872         SafeArrayDestroy(post_data);
873     if(cancel) {
874         FIXME("Navigation canceled\n");
875         return S_OK;
876     }
877
878     if(This->document)
879         deactivate_document(This);
880
881     CreateAsyncBindCtx(0, &bsc->IBindStatusCallback_iface, 0, &bindctx);
882
883     if(This->frame)
884         IOleInPlaceFrame_EnableModeless(This->frame, FALSE);
885
886     hres = bind_to_object(This, mon, bsc->url, bindctx, &bsc->IBindStatusCallback_iface);
887
888     if(This->frame)
889         IOleInPlaceFrame_EnableModeless(This->frame, TRUE);
890
891     IBindCtx_Release(bindctx);
892
893     return hres;
894 }
895
896 typedef struct {
897     task_header_t header;
898     BindStatusCallback *bsc;
899 } task_navigate_bsc_t;
900
901 static void navigate_bsc_task_destr(task_header_t *t)
902 {
903     task_navigate_bsc_t *task = (task_navigate_bsc_t*)t;
904
905     IBindStatusCallback_Release(&task->bsc->IBindStatusCallback_iface);
906     heap_free(task);
907 }
908
909 static void navigate_bsc_proc(DocHost *This, task_header_t *t)
910 {
911     task_navigate_bsc_t *task = (task_navigate_bsc_t*)t;
912
913     if(!This->hwnd)
914         create_doc_view_hwnd(This);
915
916     navigate_bsc(This, task->bsc, NULL);
917 }
918
919
920 HRESULT navigate_url(DocHost *This, LPCWSTR url, const VARIANT *Flags,
921                      const VARIANT *TargetFrameName, VARIANT *PostData, VARIANT *Headers)
922 {
923     PBYTE post_data = NULL;
924     ULONG post_data_len = 0;
925     LPWSTR headers = NULL;
926     HRESULT hres = S_OK;
927
928     TRACE("navigating to %s\n", debugstr_w(url));
929
930     if((Flags && V_VT(Flags) != VT_EMPTY)
931        || (TargetFrameName && V_VT(TargetFrameName) != VT_EMPTY))
932         FIXME("Unsupported args (Flags %p:%d; TargetFrameName %p:%d)\n",
933                 Flags, Flags ? V_VT(Flags) : -1, TargetFrameName,
934                 TargetFrameName ? V_VT(TargetFrameName) : -1);
935
936     if(PostData && V_VT(PostData) == (VT_ARRAY | VT_UI1) && V_ARRAY(PostData)) {
937         SafeArrayAccessData(V_ARRAY(PostData), (void**)&post_data);
938         post_data_len = V_ARRAY(PostData)->rgsabound[0].cElements;
939     }
940
941     if(Headers && V_VT(Headers) == VT_BSTR) {
942         headers = V_BSTR(Headers);
943         TRACE("Headers: %s\n", debugstr_w(headers));
944     }
945
946     set_doc_state(This, READYSTATE_LOADING);
947     This->ready_state = READYSTATE_LOADING;
948
949     if(This->doc_navigate) {
950         WCHAR new_url[INTERNET_MAX_URL_LENGTH];
951
952         if(PathIsURLW(url)) {
953             new_url[0] = 0;
954         }else {
955             DWORD size;
956
957             size = sizeof(new_url)/sizeof(WCHAR);
958             hres = UrlApplySchemeW(url, new_url, &size,
959                     URL_APPLY_GUESSSCHEME | URL_APPLY_GUESSFILE | URL_APPLY_DEFAULT);
960             if(FAILED(hres)) {
961                 WARN("UrlApplyScheme failed: %08x\n", hres);
962                 new_url[0] = 0;
963             }
964         }
965
966         hres = async_doc_navigate(This, *new_url ? new_url : url, headers, post_data,
967                 post_data_len, TRUE);
968     }else {
969         task_navigate_bsc_t *task;
970
971         task = heap_alloc(sizeof(*task));
972         task->bsc = create_callback(This, url, post_data, post_data_len, headers);
973         push_dochost_task(This, &task->header, navigate_bsc_proc, navigate_bsc_task_destr, This->url == NULL);
974     }
975
976     if(post_data)
977         SafeArrayUnaccessData(V_ARRAY(PostData));
978
979     return hres;
980 }
981
982 static HRESULT navigate_hlink(DocHost *This, IMoniker *mon, IBindCtx *bindctx,
983                               IBindStatusCallback *callback)
984 {
985     IHttpNegotiate *http_negotiate;
986     BindStatusCallback *bsc;
987     PBYTE post_data = NULL;
988     ULONG post_data_len = 0;
989     LPWSTR headers = NULL, url;
990     BINDINFO bindinfo;
991     DWORD bindf = 0;
992     HRESULT hres;
993
994     TRACE("\n");
995
996     hres = IMoniker_GetDisplayName(mon, 0, NULL, &url);
997     if(FAILED(hres))
998         FIXME("GetDisplayName failed: %08x\n", hres);
999
1000     hres = IBindStatusCallback_QueryInterface(callback, &IID_IHttpNegotiate,
1001                                               (void**)&http_negotiate);
1002     if(SUCCEEDED(hres)) {
1003         static const WCHAR null_string[] = {0};
1004
1005         IHttpNegotiate_BeginningTransaction(http_negotiate, null_string, null_string, 0,
1006                                             &headers);
1007         IHttpNegotiate_Release(http_negotiate);
1008     }
1009
1010     memset(&bindinfo, 0, sizeof(bindinfo));
1011     bindinfo.cbSize = sizeof(bindinfo);
1012
1013     hres = IBindStatusCallback_GetBindInfo(callback, &bindf, &bindinfo);
1014     dump_BINDINFO(&bindinfo);
1015     if(bindinfo.dwBindVerb == BINDVERB_POST) {
1016         post_data_len = bindinfo.cbstgmedData;
1017         if(post_data_len)
1018             post_data = bindinfo.stgmedData.u.hGlobal;
1019     }
1020
1021     if(This->doc_navigate) {
1022         hres = async_doc_navigate(This, url, headers, post_data, post_data_len, FALSE);
1023     }else {
1024         bsc = create_callback(This, url, post_data, post_data_len, headers);
1025         hres = navigate_bsc(This, bsc, mon);
1026         IBindStatusCallback_Release(&bsc->IBindStatusCallback_iface);
1027     }
1028
1029     CoTaskMemFree(url);
1030     CoTaskMemFree(headers);
1031     ReleaseBindInfo(&bindinfo);
1032
1033     return hres;
1034 }
1035
1036 HRESULT go_home(DocHost *This)
1037 {
1038     HKEY hkey;
1039     DWORD res, type, size;
1040     WCHAR wszPageName[MAX_PATH];
1041     static const WCHAR wszAboutBlank[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
1042     static const WCHAR wszStartPage[] = {'S','t','a','r','t',' ','P','a','g','e',0};
1043     static const WCHAR wszSubKey[] = {'S','o','f','t','w','a','r','e','\\',
1044                                       'M','i','c','r','o','s','o','f','t','\\',
1045                                       'I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r','\\',
1046                                       'M','a','i','n',0};
1047
1048     res = RegOpenKeyW(HKEY_CURRENT_USER, wszSubKey, &hkey);
1049     if (res != ERROR_SUCCESS)
1050         return navigate_url(This, wszAboutBlank, NULL, NULL, NULL, NULL);
1051
1052     size = sizeof(wszPageName);
1053     res = RegQueryValueExW(hkey, wszStartPage, NULL, &type, (LPBYTE)wszPageName, &size);
1054     RegCloseKey(hkey);
1055     if (res != ERROR_SUCCESS || type != REG_SZ)
1056         return navigate_url(This, wszAboutBlank, NULL, NULL, NULL, NULL);
1057
1058     return navigate_url(This, wszPageName, NULL, NULL, NULL, NULL);
1059 }
1060
1061 HRESULT go_back(DocHost *This)
1062 {
1063     WCHAR *url;
1064     HRESULT hres;
1065
1066     if(!This->travellog_position) {
1067         WARN("No history available\n");
1068         return E_FAIL;
1069     }
1070
1071     url = This->travellog[--This->travellog_position].url;
1072
1073     if(This->doc_navigate) {
1074         hres = async_doc_navigate(This, url, NULL, NULL, 0, FALSE);
1075     }else {
1076         FIXME("unsupported doc_navigate FALSE\n");
1077         hres = E_NOTIMPL;
1078     }
1079
1080     heap_free(url);
1081     return hres;
1082 }
1083
1084 HRESULT get_location_url(DocHost *This, BSTR *ret)
1085 {
1086     FIXME("semi-stub\n");
1087
1088     *ret = This->url ? SysAllocString(This->url) : SysAllocStringLen(NULL, 0);
1089     if(!*ret)
1090         return E_OUTOFMEMORY;
1091
1092     return This->url ? S_OK : S_FALSE;
1093 }
1094
1095 static inline HlinkFrame *impl_from_IHlinkFrame(IHlinkFrame *iface)
1096 {
1097     return CONTAINING_RECORD(iface, HlinkFrame, IHlinkFrame_iface);
1098 }
1099
1100 static HRESULT WINAPI HlinkFrame_QueryInterface(IHlinkFrame *iface, REFIID riid, void **ppv)
1101 {
1102     HlinkFrame *This = impl_from_IHlinkFrame(iface);
1103     return IUnknown_QueryInterface(This->outer, riid, ppv);
1104 }
1105
1106 static ULONG WINAPI HlinkFrame_AddRef(IHlinkFrame *iface)
1107 {
1108     HlinkFrame *This = impl_from_IHlinkFrame(iface);
1109     return IUnknown_AddRef(This->outer);
1110 }
1111
1112 static ULONG WINAPI HlinkFrame_Release(IHlinkFrame *iface)
1113 {
1114     HlinkFrame *This = impl_from_IHlinkFrame(iface);
1115     return IUnknown_Release(This->outer);
1116 }
1117
1118 static HRESULT WINAPI HlinkFrame_SetBrowseContext(IHlinkFrame *iface,
1119                                                   IHlinkBrowseContext *pihlbc)
1120 {
1121     HlinkFrame *This = impl_from_IHlinkFrame(iface);
1122     FIXME("(%p)->(%p)\n", This, pihlbc);
1123     return E_NOTIMPL;
1124 }
1125
1126 static HRESULT WINAPI HlinkFrame_GetBrowseContext(IHlinkFrame *iface,
1127                                                   IHlinkBrowseContext **ppihlbc)
1128 {
1129     HlinkFrame *This = impl_from_IHlinkFrame(iface);
1130     FIXME("(%p)->(%p)\n", This, ppihlbc);
1131     return E_NOTIMPL;
1132 }
1133
1134 static HRESULT WINAPI HlinkFrame_Navigate(IHlinkFrame *iface, DWORD grfHLNF, LPBC pbc,
1135                                           IBindStatusCallback *pibsc, IHlink *pihlNavigate)
1136 {
1137     HlinkFrame *This = impl_from_IHlinkFrame(iface);
1138     IMoniker *mon;
1139     LPWSTR location = NULL;
1140
1141     TRACE("(%p)->(%08x %p %p %p)\n", This, grfHLNF, pbc, pibsc, pihlNavigate);
1142
1143     if(grfHLNF)
1144         FIXME("unsupported grfHLNF=%08x\n", grfHLNF);
1145
1146     /* Windows calls GetTargetFrameName here. */
1147
1148     IHlink_GetMonikerReference(pihlNavigate, 1, &mon, &location);
1149
1150     if(location) {
1151         FIXME("location = %s\n", debugstr_w(location));
1152         CoTaskMemFree(location);
1153     }
1154
1155     /* Windows calls GetHlinkSite here */
1156
1157     if(grfHLNF & HLNF_OPENINNEWWINDOW) {
1158         FIXME("Not supported HLNF_OPENINNEWWINDOW\n");
1159         return E_NOTIMPL;
1160     }
1161
1162     return navigate_hlink(This->doc_host, mon, pbc, pibsc);
1163 }
1164
1165 static HRESULT WINAPI HlinkFrame_OnNavigate(IHlinkFrame *iface, DWORD grfHLNF,
1166         IMoniker *pimkTarget, LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName, DWORD dwreserved)
1167 {
1168     HlinkFrame *This = impl_from_IHlinkFrame(iface);
1169     FIXME("(%p)->(%08x %p %s %s %d)\n", This, grfHLNF, pimkTarget, debugstr_w(pwzLocation),
1170           debugstr_w(pwzFriendlyName), dwreserved);
1171     return E_NOTIMPL;
1172 }
1173
1174 static HRESULT WINAPI HlinkFrame_UpdateHlink(IHlinkFrame *iface, ULONG uHLID,
1175         IMoniker *pimkTarget, LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName)
1176 {
1177     HlinkFrame *This = impl_from_IHlinkFrame(iface);
1178     FIXME("(%p)->(%u %p %s %s)\n", This, uHLID, pimkTarget, debugstr_w(pwzLocation),
1179           debugstr_w(pwzFriendlyName));
1180     return E_NOTIMPL;
1181 }
1182
1183 static const IHlinkFrameVtbl HlinkFrameVtbl = {
1184     HlinkFrame_QueryInterface,
1185     HlinkFrame_AddRef,
1186     HlinkFrame_Release,
1187     HlinkFrame_SetBrowseContext,
1188     HlinkFrame_GetBrowseContext,
1189     HlinkFrame_Navigate,
1190     HlinkFrame_OnNavigate,
1191     HlinkFrame_UpdateHlink
1192 };
1193
1194 static inline HlinkFrame *impl_from_ITargetFrame2(ITargetFrame2 *iface)
1195 {
1196     return CONTAINING_RECORD(iface, HlinkFrame, IHlinkFrame_iface);
1197 }
1198
1199 static HRESULT WINAPI TargetFrame2_QueryInterface(ITargetFrame2 *iface, REFIID riid, void **ppv)
1200 {
1201     HlinkFrame *This = impl_from_ITargetFrame2(iface);
1202     return IUnknown_QueryInterface(This->outer, riid, ppv);
1203 }
1204
1205 static ULONG WINAPI TargetFrame2_AddRef(ITargetFrame2 *iface)
1206 {
1207     HlinkFrame *This = impl_from_ITargetFrame2(iface);
1208     return IUnknown_AddRef(This->outer);
1209 }
1210
1211 static ULONG WINAPI TargetFrame2_Release(ITargetFrame2 *iface)
1212 {
1213     HlinkFrame *This = impl_from_ITargetFrame2(iface);
1214     return IUnknown_Release(This->outer);
1215 }
1216
1217 static HRESULT WINAPI TargetFrame2_SetFrameName(ITargetFrame2 *iface, LPCWSTR pszFrameName)
1218 {
1219     HlinkFrame *This = impl_from_ITargetFrame2(iface);
1220     FIXME("(%p)->(%s)\n", This, debugstr_w(pszFrameName));
1221     return E_NOTIMPL;
1222 }
1223
1224 static HRESULT WINAPI TargetFrame2_GetFrameName(ITargetFrame2 *iface, LPWSTR *ppszFrameName)
1225 {
1226     HlinkFrame *This = impl_from_ITargetFrame2(iface);
1227     FIXME("(%p)->(%p)\n", This, ppszFrameName);
1228     return E_NOTIMPL;
1229 }
1230
1231 static HRESULT WINAPI TargetFrame2_GetParentFrame(ITargetFrame2 *iface, IUnknown **ppunkParent)
1232 {
1233     HlinkFrame *This = impl_from_ITargetFrame2(iface);
1234     FIXME("(%p)->(%p)\n", This, ppunkParent);
1235     return E_NOTIMPL;
1236 }
1237
1238 static HRESULT WINAPI TargetFrame2_SetFrameSrc(ITargetFrame2 *iface, LPCWSTR pszFrameSrc)
1239 {
1240     HlinkFrame *This = impl_from_ITargetFrame2(iface);
1241     FIXME("(%p)->(%s)\n", This, debugstr_w(pszFrameSrc));
1242     return E_NOTIMPL;
1243 }
1244
1245 static HRESULT WINAPI TargetFrame2_GetFrameSrc(ITargetFrame2 *iface, LPWSTR *ppszFrameSrc)
1246 {
1247     HlinkFrame *This = impl_from_ITargetFrame2(iface);
1248     FIXME("(%p)->()\n", This);
1249     return E_NOTIMPL;
1250 }
1251
1252 static HRESULT WINAPI TargetFrame2_GetFramesContainer(ITargetFrame2 *iface, IOleContainer **ppContainer)
1253 {
1254     HlinkFrame *This = impl_from_ITargetFrame2(iface);
1255     FIXME("(%p)->(%p)\n", This, ppContainer);
1256     return E_NOTIMPL;
1257 }
1258
1259 static HRESULT WINAPI TargetFrame2_SetFrameOptions(ITargetFrame2 *iface, DWORD dwFlags)
1260 {
1261     HlinkFrame *This = impl_from_ITargetFrame2(iface);
1262     FIXME("(%p)->(%x)\n", This, dwFlags);
1263     return E_NOTIMPL;
1264 }
1265
1266 static HRESULT WINAPI TargetFrame2_GetFrameOptions(ITargetFrame2 *iface, DWORD *pdwFlags)
1267 {
1268     HlinkFrame *This = impl_from_ITargetFrame2(iface);
1269     FIXME("(%p)->(%p)\n", This, pdwFlags);
1270     return E_NOTIMPL;
1271 }
1272
1273 static HRESULT WINAPI TargetFrame2_SetFrameMargins(ITargetFrame2 *iface, DWORD dwWidth, DWORD dwHeight)
1274 {
1275     HlinkFrame *This = impl_from_ITargetFrame2(iface);
1276     FIXME("(%p)->(%d %d)\n", This, dwWidth, dwHeight);
1277     return E_NOTIMPL;
1278 }
1279
1280 static HRESULT WINAPI TargetFrame2_GetFrameMargins(ITargetFrame2 *iface, DWORD *pdwWidth, DWORD *pdwHeight)
1281 {
1282     HlinkFrame *This = impl_from_ITargetFrame2(iface);
1283     FIXME("(%p)->(%p %p)\n", This, pdwWidth, pdwHeight);
1284     return E_NOTIMPL;
1285 }
1286
1287 static HRESULT WINAPI TargetFrame2_FindFrame(ITargetFrame2 *iface, LPCWSTR pszTargetName, DWORD dwFlags, IUnknown **ppunkTargetFrame)
1288 {
1289     HlinkFrame *This = impl_from_ITargetFrame2(iface);
1290     FIXME("(%p)->(%s %x %p)\n", This, debugstr_w(pszTargetName), dwFlags, ppunkTargetFrame);
1291     return E_NOTIMPL;
1292 }
1293
1294 static HRESULT WINAPI TargetFrame2_GetTargetAlias(ITargetFrame2 *iface, LPCWSTR pszTargetName, LPWSTR *ppszTargetAlias)
1295 {
1296     HlinkFrame *This = impl_from_ITargetFrame2(iface);
1297     FIXME("(%p)->(%s %p)\n", This, debugstr_w(pszTargetName), ppszTargetAlias);
1298     return E_NOTIMPL;
1299 }
1300
1301 static const ITargetFrame2Vtbl TargetFrame2Vtbl = {
1302     TargetFrame2_QueryInterface,
1303     TargetFrame2_AddRef,
1304     TargetFrame2_Release,
1305     TargetFrame2_SetFrameName,
1306     TargetFrame2_GetFrameName,
1307     TargetFrame2_GetParentFrame,
1308     TargetFrame2_SetFrameSrc,
1309     TargetFrame2_GetFrameSrc,
1310     TargetFrame2_GetFramesContainer,
1311     TargetFrame2_SetFrameOptions,
1312     TargetFrame2_GetFrameOptions,
1313     TargetFrame2_SetFrameMargins,
1314     TargetFrame2_GetFrameMargins,
1315     TargetFrame2_FindFrame,
1316     TargetFrame2_GetTargetAlias
1317 };
1318
1319 static inline HlinkFrame *impl_from_ITargetFramePriv2(ITargetFramePriv2 *iface)
1320 {
1321     return CONTAINING_RECORD(iface, HlinkFrame, ITargetFramePriv2_iface);
1322 }
1323
1324 static HRESULT WINAPI TargetFramePriv2_QueryInterface(ITargetFramePriv2 *iface, REFIID riid, void **ppv)
1325 {
1326     HlinkFrame *This = impl_from_ITargetFramePriv2(iface);
1327     return IUnknown_QueryInterface(This->outer, riid, ppv);
1328 }
1329
1330 static ULONG WINAPI TargetFramePriv2_AddRef(ITargetFramePriv2 *iface)
1331 {
1332     HlinkFrame *This = impl_from_ITargetFramePriv2(iface);
1333     return IUnknown_AddRef(This->outer);
1334 }
1335
1336 static ULONG WINAPI TargetFramePriv2_Release(ITargetFramePriv2 *iface)
1337 {
1338     HlinkFrame *This = impl_from_ITargetFramePriv2(iface);
1339     return IUnknown_Release(This->outer);
1340 }
1341
1342 static HRESULT WINAPI TargetFramePriv2_FindFrameDownwards(ITargetFramePriv2 *iface,
1343         LPCWSTR pszTargetName, DWORD dwFlags, IUnknown **ppunkTargetFrame)
1344 {
1345     HlinkFrame *This = impl_from_ITargetFramePriv2(iface);
1346     FIXME("(%p)->(%s %x %p)\n", This, debugstr_w(pszTargetName), dwFlags, ppunkTargetFrame);
1347     return E_NOTIMPL;
1348 }
1349
1350 static HRESULT WINAPI TargetFramePriv2_FindFrameInContext(ITargetFramePriv2 *iface,
1351         LPCWSTR pszTargetName, IUnknown *punkContextFrame, DWORD dwFlags, IUnknown **ppunkTargetFrame)
1352 {
1353     HlinkFrame *This = impl_from_ITargetFramePriv2(iface);
1354     FIXME("(%p)->(%s %p %x %p)\n", This, debugstr_w(pszTargetName), punkContextFrame, dwFlags, ppunkTargetFrame);
1355     return E_NOTIMPL;
1356 }
1357
1358 static HRESULT WINAPI TargetFramePriv2_OnChildFrameActivate(ITargetFramePriv2 *iface, IUnknown *pUnkChildFrame)
1359 {
1360     HlinkFrame *This = impl_from_ITargetFramePriv2(iface);
1361     FIXME("(%p)->(%p)\n", This, pUnkChildFrame);
1362     return E_NOTIMPL;
1363 }
1364
1365 static HRESULT WINAPI TargetFramePriv2_OnChildFrameDeactivate(ITargetFramePriv2 *iface, IUnknown *pUnkChildFrame)
1366 {
1367     HlinkFrame *This = impl_from_ITargetFramePriv2(iface);
1368     FIXME("(%p)->(%p)\n", This, pUnkChildFrame);
1369     return E_NOTIMPL;
1370 }
1371
1372 static HRESULT WINAPI TargetFramePriv2_NavigateHack(ITargetFramePriv2 *iface, DWORD grfHLNF, LPBC pbc,
1373         IBindStatusCallback *pibsc, LPCWSTR pszTargetName, LPCWSTR pszUrl, LPCWSTR pszLocation)
1374 {
1375     HlinkFrame *This = impl_from_ITargetFramePriv2(iface);
1376     FIXME("(%p)->(%x %p %p %s %s %s)\n", This, grfHLNF, pbc, pibsc, debugstr_w(pszTargetName),
1377           debugstr_w(pszUrl), debugstr_w(pszLocation));
1378     return E_NOTIMPL;
1379 }
1380
1381 static HRESULT WINAPI TargetFramePriv2_FindBrowserByIndex(ITargetFramePriv2 *iface, DWORD dwID, IUnknown **ppunkBrowser)
1382 {
1383     HlinkFrame *This = impl_from_ITargetFramePriv2(iface);
1384     FIXME("(%p)->(%d %p)\n", This, dwID, ppunkBrowser);
1385     return E_NOTIMPL;
1386 }
1387
1388 static HRESULT WINAPI TargetFramePriv2_AggregatedNavigation2(ITargetFramePriv2 *iface, DWORD grfHLNF, LPBC pbc,
1389         IBindStatusCallback *pibsc, LPCWSTR pszTargetName, IUri *pUri, LPCWSTR pszLocation)
1390 {
1391     HlinkFrame *This = impl_from_ITargetFramePriv2(iface);
1392     IMoniker *mon;
1393     HRESULT hres;
1394
1395     TRACE("(%p)->(%x %p %p %s %p %s)\n", This, grfHLNF, pbc, pibsc, debugstr_w(pszTargetName),
1396           pUri, debugstr_w(pszLocation));
1397
1398     /*
1399      * NOTE: This is an undocumented function. It seems to be working the way it's implemented,
1400      * but I couldn't get its tests working. It's used by mshtml to load content in a new
1401      * instance of browser.
1402      */
1403
1404     hres = CreateURLMonikerEx2(NULL, pUri, &mon, 0);
1405     if(FAILED(hres))
1406         return hres;
1407
1408     hres = navigate_hlink(This->doc_host, mon, pbc, pibsc);
1409     IMoniker_Release(mon);
1410     return hres;
1411 }
1412
1413 static const ITargetFramePriv2Vtbl TargetFramePriv2Vtbl = {
1414     TargetFramePriv2_QueryInterface,
1415     TargetFramePriv2_AddRef,
1416     TargetFramePriv2_Release,
1417     TargetFramePriv2_FindFrameDownwards,
1418     TargetFramePriv2_FindFrameInContext,
1419     TargetFramePriv2_OnChildFrameActivate,
1420     TargetFramePriv2_OnChildFrameDeactivate,
1421     TargetFramePriv2_NavigateHack,
1422     TargetFramePriv2_FindBrowserByIndex,
1423     TargetFramePriv2_AggregatedNavigation2
1424 };
1425
1426 BOOL HlinkFrame_QI(HlinkFrame *This, REFIID riid, void **ppv)
1427 {
1428     if(IsEqualGUID(&IID_IHlinkFrame, riid)) {
1429         TRACE("(%p)->(IID_IHlinkFrame %p)\n", This, ppv);
1430         *ppv = &This->IHlinkFrame_iface;
1431     }else if(IsEqualGUID(&IID_ITargetFrame2, riid)) {
1432         TRACE("(%p)->(IID_ITargetFrame2 %p)\n", This, ppv);
1433         *ppv = &This->ITargetFrame2_iface;
1434     }else if(IsEqualGUID(&IID_ITargetFramePriv, riid)) {
1435         TRACE("(%p)->(IID_ITargetFramePriv %p)\n", This, ppv);
1436         *ppv = &This->ITargetFramePriv2_iface;
1437     }else if(IsEqualGUID(&IID_ITargetFramePriv2, riid)) {
1438         TRACE("(%p)->(IID_ITargetFramePriv2 %p)\n", This, ppv);
1439         *ppv = &This->ITargetFramePriv2_iface;
1440     }else {
1441         return FALSE;
1442     }
1443
1444     IUnknown_AddRef((IUnknown*)*ppv);
1445     return TRUE;
1446 }
1447
1448 void HlinkFrame_Init(HlinkFrame *This, IUnknown *outer, DocHost *doc_host)
1449 {
1450     This->IHlinkFrame_iface.lpVtbl   = &HlinkFrameVtbl;
1451     This->ITargetFrame2_iface.lpVtbl = &TargetFrame2Vtbl;
1452     This->ITargetFramePriv2_iface.lpVtbl = &TargetFramePriv2Vtbl;
1453
1454     This->outer = outer;
1455     This->doc_host = doc_host;
1456 }