wmvcore: Print the debug string and not the pointer to it.
[wine.git] / dlls / mshtml / htmllocation.c
blobde66ff5670771702ddc3a7ac52b2be4cc5aad2ac
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
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.
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.
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
19 #include <stdarg.h>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "winreg.h"
27 #include "ole2.h"
28 #include "wininet.h"
29 #include "shlwapi.h"
31 #include "wine/debug.h"
33 #include "mshtml_private.h"
34 #include "binding.h"
35 #include "resource.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
39 static HRESULT get_url(HTMLLocation *This, const WCHAR **ret)
41 if(!This->window || !This->window->base.outer_window || !This->window->base.outer_window->url) {
42 FIXME("No current URL\n");
43 return E_NOTIMPL;
46 *ret = This->window->base.outer_window->url;
47 return S_OK;
50 static IUri *get_uri(HTMLLocation *This)
52 if(!This->window || !This->window->base.outer_window)
53 return NULL;
54 return This->window->base.outer_window->uri;
57 static HRESULT get_url_components(HTMLLocation *This, URL_COMPONENTSW *url)
59 const WCHAR *doc_url;
60 HRESULT hres;
62 hres = get_url(This, &doc_url);
63 if(FAILED(hres))
64 return hres;
66 if(!InternetCrackUrlW(doc_url, 0, 0, url)) {
67 FIXME("InternetCrackUrlW failed: 0x%08x\n", GetLastError());
68 SetLastError(0);
69 return E_FAIL;
72 return S_OK;
75 static inline HTMLLocation *impl_from_IHTMLLocation(IHTMLLocation *iface)
77 return CONTAINING_RECORD(iface, HTMLLocation, IHTMLLocation_iface);
80 static HRESULT WINAPI HTMLLocation_QueryInterface(IHTMLLocation *iface, REFIID riid, void **ppv)
82 HTMLLocation *This = impl_from_IHTMLLocation(iface);
84 TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
86 if(IsEqualGUID(&IID_IUnknown, riid)) {
87 *ppv = &This->IHTMLLocation_iface;
88 }else if(IsEqualGUID(&IID_IHTMLLocation, riid)) {
89 *ppv = &This->IHTMLLocation_iface;
90 }else if(IsEqualGUID(&IID_IMarshal, riid)) {
91 *ppv = NULL;
92 FIXME("(%p)->(IID_IMarshal %p)\n", This, ppv);
93 return E_NOINTERFACE;
94 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
95 return *ppv ? S_OK : E_NOINTERFACE;
96 }else {
97 *ppv = NULL;
98 WARN("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
99 return E_NOINTERFACE;
102 IUnknown_AddRef((IUnknown*)*ppv);
103 return S_OK;
106 static ULONG WINAPI HTMLLocation_AddRef(IHTMLLocation *iface)
108 HTMLLocation *This = impl_from_IHTMLLocation(iface);
109 LONG ref = InterlockedIncrement(&This->ref);
111 TRACE("(%p) ref=%d\n", This, ref);
113 return ref;
116 static ULONG WINAPI HTMLLocation_Release(IHTMLLocation *iface)
118 HTMLLocation *This = impl_from_IHTMLLocation(iface);
119 LONG ref = InterlockedDecrement(&This->ref);
121 TRACE("(%p) ref=%d\n", This, ref);
123 if(!ref) {
124 if(This->window)
125 This->window->location = NULL;
126 release_dispex(&This->dispex);
127 heap_free(This);
130 return ref;
133 static HRESULT WINAPI HTMLLocation_GetTypeInfoCount(IHTMLLocation *iface, UINT *pctinfo)
135 HTMLLocation *This = impl_from_IHTMLLocation(iface);
136 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
139 static HRESULT WINAPI HTMLLocation_GetTypeInfo(IHTMLLocation *iface, UINT iTInfo,
140 LCID lcid, ITypeInfo **ppTInfo)
142 HTMLLocation *This = impl_from_IHTMLLocation(iface);
143 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
146 static HRESULT WINAPI HTMLLocation_GetIDsOfNames(IHTMLLocation *iface, REFIID riid,
147 LPOLESTR *rgszNames, UINT cNames,
148 LCID lcid, DISPID *rgDispId)
150 HTMLLocation *This = impl_from_IHTMLLocation(iface);
151 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
152 lcid, rgDispId);
155 static HRESULT WINAPI HTMLLocation_Invoke(IHTMLLocation *iface, DISPID dispIdMember,
156 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
157 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
159 HTMLLocation *This = impl_from_IHTMLLocation(iface);
160 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
161 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
164 static HRESULT WINAPI HTMLLocation_put_href(IHTMLLocation *iface, BSTR v)
166 HTMLLocation *This = impl_from_IHTMLLocation(iface);
168 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
170 if(!This->window || !This->window->base.outer_window) {
171 FIXME("No window available\n");
172 return E_FAIL;
175 return navigate_url(This->window->base.outer_window, v, This->window->base.outer_window->uri, BINDING_NAVIGATED);
178 static HRESULT WINAPI HTMLLocation_get_href(IHTMLLocation *iface, BSTR *p)
180 HTMLLocation *This = impl_from_IHTMLLocation(iface);
181 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
182 WCHAR *buf = NULL, *url_path = NULL;
183 HRESULT hres, ret;
184 DWORD len = 0;
185 int i;
187 TRACE("(%p)->(%p)\n", This, p);
189 if(!p)
190 return E_POINTER;
192 url.dwSchemeLength = 1;
193 url.dwHostNameLength = 1;
194 url.dwUrlPathLength = 1;
195 url.dwExtraInfoLength = 1;
196 hres = get_url_components(This, &url);
197 if(FAILED(hres))
198 return hres;
200 switch(url.nScheme) {
201 case INTERNET_SCHEME_FILE:
203 /* prepend a slash */
204 url_path = HeapAlloc(GetProcessHeap(), 0, (url.dwUrlPathLength + 1) * sizeof(WCHAR));
205 if(!url_path)
206 return E_OUTOFMEMORY;
207 url_path[0] = '/';
208 memcpy(url_path + 1, url.lpszUrlPath, url.dwUrlPathLength * sizeof(WCHAR));
209 url.lpszUrlPath = url_path;
210 url.dwUrlPathLength = url.dwUrlPathLength + 1;
212 break;
214 case INTERNET_SCHEME_HTTP:
215 case INTERNET_SCHEME_HTTPS:
216 case INTERNET_SCHEME_FTP:
217 if(!url.dwUrlPathLength) {
218 /* add a slash if it's blank */
219 url_path = url.lpszUrlPath = HeapAlloc(GetProcessHeap(), 0, 1 * sizeof(WCHAR));
220 if(!url.lpszUrlPath)
221 return E_OUTOFMEMORY;
222 url.lpszUrlPath[0] = '/';
223 url.dwUrlPathLength = 1;
225 break;
227 default:
228 break;
231 /* replace \ with / */
232 for(i = 0; i < url.dwUrlPathLength; ++i)
233 if(url.lpszUrlPath[i] == '\\')
234 url.lpszUrlPath[i] = '/';
236 if(InternetCreateUrlW(&url, ICU_ESCAPE, NULL, &len)) {
237 FIXME("InternetCreateUrl succeeded with NULL buffer?\n");
238 ret = E_FAIL;
239 goto cleanup;
242 if(GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
243 FIXME("InternetCreateUrl failed with error: %08x\n", GetLastError());
244 SetLastError(0);
245 ret = E_FAIL;
246 goto cleanup;
248 SetLastError(0);
250 buf = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
251 if(!buf) {
252 ret = E_OUTOFMEMORY;
253 goto cleanup;
256 if(!InternetCreateUrlW(&url, ICU_ESCAPE, buf, &len)) {
257 FIXME("InternetCreateUrl failed with error: %08x\n", GetLastError());
258 SetLastError(0);
259 ret = E_FAIL;
260 goto cleanup;
263 *p = SysAllocStringLen(buf, len);
264 if(!*p) {
265 ret = E_OUTOFMEMORY;
266 goto cleanup;
269 ret = S_OK;
271 cleanup:
272 HeapFree(GetProcessHeap(), 0, buf);
273 HeapFree(GetProcessHeap(), 0, url_path);
275 return ret;
278 static HRESULT WINAPI HTMLLocation_put_protocol(IHTMLLocation *iface, BSTR v)
280 HTMLLocation *This = impl_from_IHTMLLocation(iface);
281 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
282 return E_NOTIMPL;
285 static HRESULT WINAPI HTMLLocation_get_protocol(IHTMLLocation *iface, BSTR *p)
287 HTMLLocation *This = impl_from_IHTMLLocation(iface);
288 BSTR protocol, ret;
289 unsigned len;
290 IUri *uri;
291 HRESULT hres;
293 TRACE("(%p)->(%p)\n", This, p);
295 if(!p)
296 return E_POINTER;
298 if(!(uri = get_uri(This))) {
299 FIXME("No current URI\n");
300 return E_NOTIMPL;
303 hres = IUri_GetSchemeName(uri, &protocol);
304 if(FAILED(hres))
305 return hres;
306 if(hres == S_FALSE) {
307 SysFreeString(protocol);
308 *p = NULL;
309 return S_OK;
312 len = SysStringLen(protocol);
313 ret = SysAllocStringLen(protocol, len+1);
314 SysFreeString(protocol);
315 if(!ret)
316 return E_OUTOFMEMORY;
318 ret[len] = ':';
319 *p = ret;
320 return S_OK;
323 static HRESULT WINAPI HTMLLocation_put_host(IHTMLLocation *iface, BSTR v)
325 HTMLLocation *This = impl_from_IHTMLLocation(iface);
326 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
327 return E_NOTIMPL;
330 static HRESULT WINAPI HTMLLocation_get_host(IHTMLLocation *iface, BSTR *p)
332 HTMLLocation *This = impl_from_IHTMLLocation(iface);
333 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
334 HRESULT hres;
336 TRACE("(%p)->(%p)\n", This, p);
338 if(!p)
339 return E_POINTER;
341 url.dwHostNameLength = 1;
342 hres = get_url_components(This, &url);
343 if(FAILED(hres))
344 return hres;
346 if(!url.dwHostNameLength){
347 *p = NULL;
348 return S_OK;
351 if(url.nPort) {
352 /* <hostname>:<port> */
353 const WCHAR format[] = {'%','u',0};
354 DWORD len = url.dwHostNameLength + 1 + 5;
355 WCHAR *buf;
357 buf = *p = SysAllocStringLen(NULL, len);
358 memcpy(buf, url.lpszHostName, url.dwHostNameLength * sizeof(WCHAR));
359 buf[url.dwHostNameLength] = ':';
360 snprintfW(buf + url.dwHostNameLength + 1, 6, format, url.nPort);
361 }else
362 *p = SysAllocStringLen(url.lpszHostName, url.dwHostNameLength);
364 if(!*p)
365 return E_OUTOFMEMORY;
366 return S_OK;
369 static HRESULT WINAPI HTMLLocation_put_hostname(IHTMLLocation *iface, BSTR v)
371 HTMLLocation *This = impl_from_IHTMLLocation(iface);
372 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
373 return E_NOTIMPL;
376 static HRESULT WINAPI HTMLLocation_get_hostname(IHTMLLocation *iface, BSTR *p)
378 HTMLLocation *This = impl_from_IHTMLLocation(iface);
379 BSTR hostname;
380 IUri *uri;
381 HRESULT hres;
383 TRACE("(%p)->(%p)\n", This, p);
385 if(!p)
386 return E_POINTER;
388 if(!(uri = get_uri(This))) {
389 FIXME("No current URI\n");
390 return E_NOTIMPL;
393 hres = IUri_GetHost(uri, &hostname);
394 if(hres == S_OK) {
395 *p = hostname;
396 }else if(hres == S_FALSE) {
397 SysFreeString(hostname);
398 *p = NULL;
399 }else {
400 return hres;
403 return S_OK;
406 static HRESULT WINAPI HTMLLocation_put_port(IHTMLLocation *iface, BSTR v)
408 HTMLLocation *This = impl_from_IHTMLLocation(iface);
409 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
410 return E_NOTIMPL;
413 static HRESULT WINAPI HTMLLocation_get_port(IHTMLLocation *iface, BSTR *p)
415 HTMLLocation *This = impl_from_IHTMLLocation(iface);
416 DWORD port;
417 IUri *uri;
418 HRESULT hres;
420 TRACE("(%p)->(%p)\n", This, p);
422 if(!p)
423 return E_POINTER;
425 if(!(uri = get_uri(This))) {
426 FIXME("No current URI\n");
427 return E_NOTIMPL;
430 hres = IUri_GetPort(uri, &port);
431 if(FAILED(hres))
432 return hres;
434 if(hres == S_OK) {
435 static const WCHAR formatW[] = {'%','u',0};
436 WCHAR buf[12];
438 sprintfW(buf, formatW, port);
439 *p = SysAllocString(buf);
440 }else {
441 *p = SysAllocStringLen(NULL, 0);
444 if(!*p)
445 return E_OUTOFMEMORY;
446 return S_OK;
449 static HRESULT WINAPI HTMLLocation_put_pathname(IHTMLLocation *iface, BSTR v)
451 HTMLLocation *This = impl_from_IHTMLLocation(iface);
452 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
453 return E_NOTIMPL;
456 static HRESULT WINAPI HTMLLocation_get_pathname(IHTMLLocation *iface, BSTR *p)
458 HTMLLocation *This = impl_from_IHTMLLocation(iface);
459 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
460 HRESULT hres;
462 TRACE("(%p)->(%p)\n", This, p);
464 if(!p)
465 return E_POINTER;
467 url.dwUrlPathLength = 1;
468 url.dwExtraInfoLength = 1;
469 hres = get_url_components(This, &url);
470 if(FAILED(hres))
471 return hres;
473 if(url.dwUrlPathLength && url.lpszUrlPath[0] == '/')
474 *p = SysAllocStringLen(url.lpszUrlPath + 1, url.dwUrlPathLength - 1);
475 else
476 *p = SysAllocStringLen(url.lpszUrlPath, url.dwUrlPathLength);
478 if(!*p)
479 return E_OUTOFMEMORY;
480 return S_OK;
483 static HRESULT WINAPI HTMLLocation_put_search(IHTMLLocation *iface, BSTR v)
485 HTMLLocation *This = impl_from_IHTMLLocation(iface);
486 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
487 return E_NOTIMPL;
490 static HRESULT WINAPI HTMLLocation_get_search(IHTMLLocation *iface, BSTR *p)
492 HTMLLocation *This = impl_from_IHTMLLocation(iface);
493 BSTR query;
494 IUri *uri;
495 HRESULT hres;
497 TRACE("(%p)->(%p)\n", This, p);
499 if(!p)
500 return E_POINTER;
502 if(!(uri = get_uri(This))) {
503 FIXME("No current URI\n");
504 return E_NOTIMPL;
507 hres = IUri_GetQuery(uri, &query);
508 if(hres == S_OK) {
509 *p = query;
510 }else if(hres == S_FALSE) {
511 SysFreeString(query);
512 *p = NULL;
513 }else {
514 return hres;
517 return S_OK;
520 static HRESULT WINAPI HTMLLocation_put_hash(IHTMLLocation *iface, BSTR v)
522 HTMLLocation *This = impl_from_IHTMLLocation(iface);
523 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
524 return E_NOTIMPL;
527 static HRESULT WINAPI HTMLLocation_get_hash(IHTMLLocation *iface, BSTR *p)
529 HTMLLocation *This = impl_from_IHTMLLocation(iface);
530 BSTR hash;
531 IUri *uri;
532 HRESULT hres;
534 TRACE("(%p)->(%p)\n", This, p);
536 if(!p)
537 return E_POINTER;
539 if(!(uri = get_uri(This))) {
540 FIXME("No current URI\n");
541 return E_NOTIMPL;
544 hres = IUri_GetFragment(uri, &hash);
545 if(hres == S_OK) {
546 *p = hash;
547 }else if(hres == S_FALSE) {
548 SysFreeString(hash);
549 *p = NULL;
550 }else {
551 return hres;
554 return S_OK;
557 static HRESULT WINAPI HTMLLocation_reload(IHTMLLocation *iface, VARIANT_BOOL flag)
559 HTMLLocation *This = impl_from_IHTMLLocation(iface);
560 FIXME("(%p)->(%x)\n", This, flag);
561 return E_NOTIMPL;
564 static HRESULT WINAPI HTMLLocation_replace(IHTMLLocation *iface, BSTR bstr)
566 HTMLLocation *This = impl_from_IHTMLLocation(iface);
568 TRACE("(%p)->(%s)\n", This, debugstr_w(bstr));
570 if(!This->window || !This->window->base.outer_window) {
571 FIXME("No window available\n");
572 return E_FAIL;
575 return navigate_url(This->window->base.outer_window, bstr, This->window->base.outer_window->uri,
576 BINDING_NAVIGATED|BINDING_REPLACE);
579 static HRESULT WINAPI HTMLLocation_assign(IHTMLLocation *iface, BSTR bstr)
581 HTMLLocation *This = impl_from_IHTMLLocation(iface);
582 TRACE("(%p)->(%s)\n", This, debugstr_w(bstr));
583 return IHTMLLocation_put_href(iface, bstr);
586 static HRESULT WINAPI HTMLLocation_toString(IHTMLLocation *iface, BSTR *String)
588 HTMLLocation *This = impl_from_IHTMLLocation(iface);
590 TRACE("(%p)->(%p)\n", This, String);
592 return IHTMLLocation_get_href(&This->IHTMLLocation_iface, String);
595 static const IHTMLLocationVtbl HTMLLocationVtbl = {
596 HTMLLocation_QueryInterface,
597 HTMLLocation_AddRef,
598 HTMLLocation_Release,
599 HTMLLocation_GetTypeInfoCount,
600 HTMLLocation_GetTypeInfo,
601 HTMLLocation_GetIDsOfNames,
602 HTMLLocation_Invoke,
603 HTMLLocation_put_href,
604 HTMLLocation_get_href,
605 HTMLLocation_put_protocol,
606 HTMLLocation_get_protocol,
607 HTMLLocation_put_host,
608 HTMLLocation_get_host,
609 HTMLLocation_put_hostname,
610 HTMLLocation_get_hostname,
611 HTMLLocation_put_port,
612 HTMLLocation_get_port,
613 HTMLLocation_put_pathname,
614 HTMLLocation_get_pathname,
615 HTMLLocation_put_search,
616 HTMLLocation_get_search,
617 HTMLLocation_put_hash,
618 HTMLLocation_get_hash,
619 HTMLLocation_reload,
620 HTMLLocation_replace,
621 HTMLLocation_assign,
622 HTMLLocation_toString
625 static const tid_t HTMLLocation_iface_tids[] = {
626 IHTMLLocation_tid,
629 static dispex_static_data_t HTMLLocation_dispex = {
630 NULL,
631 DispHTMLLocation_tid,
632 HTMLLocation_iface_tids
636 HRESULT HTMLLocation_Create(HTMLInnerWindow *window, HTMLLocation **ret)
638 HTMLLocation *location;
640 location = heap_alloc(sizeof(*location));
641 if(!location)
642 return E_OUTOFMEMORY;
644 location->IHTMLLocation_iface.lpVtbl = &HTMLLocationVtbl;
645 location->ref = 1;
646 location->window = window;
648 init_dispex(&location->dispex, (IUnknown*)&location->IHTMLLocation_iface, &HTMLLocation_dispex);
650 *ret = location;
651 return S_OK;