directmanipulation: Return S_OK form viewport_SetViewportOptions stub.
[wine/zf.git] / dlls / mshtml / htmllocation.c
blob7d102044cc2ce93fa7b7bd6cfd4e91a24f53d05f
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 static const WCHAR format[] = {'%','u',0};
354 DWORD len, port_len;
355 WCHAR portW[6];
356 WCHAR *buf;
358 port_len = swprintf(portW, ARRAY_SIZE(portW), format, url.nPort);
359 len = url.dwHostNameLength + 1 /* ':' */ + port_len;
360 buf = *p = SysAllocStringLen(NULL, len);
361 memcpy(buf, url.lpszHostName, url.dwHostNameLength * sizeof(WCHAR));
362 buf[url.dwHostNameLength] = ':';
363 memcpy(buf + url.dwHostNameLength + 1, portW, port_len * sizeof(WCHAR));
364 }else
365 *p = SysAllocStringLen(url.lpszHostName, url.dwHostNameLength);
367 if(!*p)
368 return E_OUTOFMEMORY;
369 return S_OK;
372 static HRESULT WINAPI HTMLLocation_put_hostname(IHTMLLocation *iface, BSTR v)
374 HTMLLocation *This = impl_from_IHTMLLocation(iface);
375 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
376 return E_NOTIMPL;
379 static HRESULT WINAPI HTMLLocation_get_hostname(IHTMLLocation *iface, BSTR *p)
381 HTMLLocation *This = impl_from_IHTMLLocation(iface);
382 BSTR hostname;
383 IUri *uri;
384 HRESULT hres;
386 TRACE("(%p)->(%p)\n", This, p);
388 if(!p)
389 return E_POINTER;
391 if(!(uri = get_uri(This))) {
392 FIXME("No current URI\n");
393 return E_NOTIMPL;
396 hres = IUri_GetHost(uri, &hostname);
397 if(hres == S_OK) {
398 *p = hostname;
399 }else if(hres == S_FALSE) {
400 SysFreeString(hostname);
401 *p = NULL;
402 }else {
403 return hres;
406 return S_OK;
409 static HRESULT WINAPI HTMLLocation_put_port(IHTMLLocation *iface, BSTR v)
411 HTMLLocation *This = impl_from_IHTMLLocation(iface);
412 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
413 return E_NOTIMPL;
416 static HRESULT WINAPI HTMLLocation_get_port(IHTMLLocation *iface, BSTR *p)
418 HTMLLocation *This = impl_from_IHTMLLocation(iface);
419 DWORD port;
420 IUri *uri;
421 HRESULT hres;
423 TRACE("(%p)->(%p)\n", This, p);
425 if(!p)
426 return E_POINTER;
428 if(!(uri = get_uri(This))) {
429 FIXME("No current URI\n");
430 return E_NOTIMPL;
433 hres = IUri_GetPort(uri, &port);
434 if(FAILED(hres))
435 return hres;
437 if(hres == S_OK) {
438 static const WCHAR formatW[] = {'%','u',0};
439 WCHAR buf[12];
441 swprintf(buf, ARRAY_SIZE(buf), formatW, port);
442 *p = SysAllocString(buf);
443 }else {
444 *p = SysAllocStringLen(NULL, 0);
447 if(!*p)
448 return E_OUTOFMEMORY;
449 return S_OK;
452 static HRESULT WINAPI HTMLLocation_put_pathname(IHTMLLocation *iface, BSTR v)
454 HTMLLocation *This = impl_from_IHTMLLocation(iface);
455 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
456 return E_NOTIMPL;
459 static HRESULT WINAPI HTMLLocation_get_pathname(IHTMLLocation *iface, BSTR *p)
461 HTMLLocation *This = impl_from_IHTMLLocation(iface);
462 BSTR path;
463 IUri *uri;
464 HRESULT hres;
466 TRACE("(%p)->(%p)\n", This, p);
468 if(!p)
469 return E_POINTER;
471 if(!(uri = get_uri(This))) {
472 FIXME("No current URI\n");
473 return E_NOTIMPL;
476 hres = IUri_GetPath(uri, &path);
477 if(FAILED(hres))
478 return hres;
480 if(hres == S_FALSE) {
481 SysFreeString(path);
482 path = NULL;
485 *p = path;
486 return S_OK;
489 static HRESULT WINAPI HTMLLocation_put_search(IHTMLLocation *iface, BSTR v)
491 HTMLLocation *This = impl_from_IHTMLLocation(iface);
492 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
493 return E_NOTIMPL;
496 static HRESULT WINAPI HTMLLocation_get_search(IHTMLLocation *iface, BSTR *p)
498 HTMLLocation *This = impl_from_IHTMLLocation(iface);
499 BSTR query;
500 IUri *uri;
501 HRESULT hres;
503 TRACE("(%p)->(%p)\n", This, p);
505 if(!p)
506 return E_POINTER;
508 if(!(uri = get_uri(This))) {
509 FIXME("No current URI\n");
510 return E_NOTIMPL;
513 hres = IUri_GetQuery(uri, &query);
514 if(hres == S_OK) {
515 *p = query;
516 }else if(hres == S_FALSE) {
517 SysFreeString(query);
518 *p = NULL;
519 }else {
520 return hres;
523 return S_OK;
526 static HRESULT WINAPI HTMLLocation_put_hash(IHTMLLocation *iface, BSTR v)
528 HTMLLocation *This = impl_from_IHTMLLocation(iface);
529 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
530 return E_NOTIMPL;
533 static HRESULT WINAPI HTMLLocation_get_hash(IHTMLLocation *iface, BSTR *p)
535 HTMLLocation *This = impl_from_IHTMLLocation(iface);
536 BSTR hash;
537 IUri *uri;
538 HRESULT hres;
540 TRACE("(%p)->(%p)\n", This, p);
542 if(!p)
543 return E_POINTER;
545 if(!(uri = get_uri(This))) {
546 FIXME("No current URI\n");
547 return E_NOTIMPL;
550 hres = IUri_GetFragment(uri, &hash);
551 if(hres == S_OK) {
552 *p = hash;
553 }else if(hres == S_FALSE) {
554 SysFreeString(hash);
555 *p = NULL;
556 }else {
557 return hres;
560 return S_OK;
563 static HRESULT WINAPI HTMLLocation_reload(IHTMLLocation *iface, VARIANT_BOOL flag)
565 HTMLLocation *This = impl_from_IHTMLLocation(iface);
566 FIXME("(%p)->(%x)\n", This, flag);
567 return E_NOTIMPL;
570 static HRESULT WINAPI HTMLLocation_replace(IHTMLLocation *iface, BSTR bstr)
572 HTMLLocation *This = impl_from_IHTMLLocation(iface);
574 TRACE("(%p)->(%s)\n", This, debugstr_w(bstr));
576 if(!This->window || !This->window->base.outer_window) {
577 FIXME("No window available\n");
578 return E_FAIL;
581 return navigate_url(This->window->base.outer_window, bstr, This->window->base.outer_window->uri,
582 BINDING_NAVIGATED|BINDING_REPLACE);
585 static HRESULT WINAPI HTMLLocation_assign(IHTMLLocation *iface, BSTR bstr)
587 HTMLLocation *This = impl_from_IHTMLLocation(iface);
588 TRACE("(%p)->(%s)\n", This, debugstr_w(bstr));
589 return IHTMLLocation_put_href(iface, bstr);
592 static HRESULT WINAPI HTMLLocation_toString(IHTMLLocation *iface, BSTR *String)
594 HTMLLocation *This = impl_from_IHTMLLocation(iface);
596 TRACE("(%p)->(%p)\n", This, String);
598 return IHTMLLocation_get_href(&This->IHTMLLocation_iface, String);
601 static const IHTMLLocationVtbl HTMLLocationVtbl = {
602 HTMLLocation_QueryInterface,
603 HTMLLocation_AddRef,
604 HTMLLocation_Release,
605 HTMLLocation_GetTypeInfoCount,
606 HTMLLocation_GetTypeInfo,
607 HTMLLocation_GetIDsOfNames,
608 HTMLLocation_Invoke,
609 HTMLLocation_put_href,
610 HTMLLocation_get_href,
611 HTMLLocation_put_protocol,
612 HTMLLocation_get_protocol,
613 HTMLLocation_put_host,
614 HTMLLocation_get_host,
615 HTMLLocation_put_hostname,
616 HTMLLocation_get_hostname,
617 HTMLLocation_put_port,
618 HTMLLocation_get_port,
619 HTMLLocation_put_pathname,
620 HTMLLocation_get_pathname,
621 HTMLLocation_put_search,
622 HTMLLocation_get_search,
623 HTMLLocation_put_hash,
624 HTMLLocation_get_hash,
625 HTMLLocation_reload,
626 HTMLLocation_replace,
627 HTMLLocation_assign,
628 HTMLLocation_toString
631 static const tid_t HTMLLocation_iface_tids[] = {
632 IHTMLLocation_tid,
635 static dispex_static_data_t HTMLLocation_dispex = {
636 NULL,
637 DispHTMLLocation_tid,
638 HTMLLocation_iface_tids
642 HRESULT HTMLLocation_Create(HTMLInnerWindow *window, HTMLLocation **ret)
644 HTMLLocation *location;
646 location = heap_alloc(sizeof(*location));
647 if(!location)
648 return E_OUTOFMEMORY;
650 location->IHTMLLocation_iface.lpVtbl = &HTMLLocationVtbl;
651 location->ref = 1;
652 location->window = window;
654 init_dispex(&location->dispex, (IUnknown*)&location->IHTMLLocation_iface, &HTMLLocation_dispex);
656 *ret = location;
657 return S_OK;