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
31 #include "wine/debug.h"
33 #include "mshtml_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38 static HRESULT get_url(HTMLLocation *This, const WCHAR **ret)
40 if(!This->window || !This->window->base.outer_window || !This->window->base.outer_window->url) {
41 FIXME("No current URL\n");
45 *ret = This->window->base.outer_window->url;
49 static IUri *get_uri(HTMLLocation *This)
51 if(!This->window || !This->window->base.outer_window)
53 return This->window->base.outer_window->uri;
56 static HRESULT get_url_components(HTMLLocation *This, URL_COMPONENTSW *url)
61 hres = get_url(This, &doc_url);
65 if(!InternetCrackUrlW(doc_url, 0, 0, url)) {
66 FIXME("InternetCrackUrlW failed: 0x%08x\n", GetLastError());
74 static inline HTMLLocation *impl_from_IHTMLLocation(IHTMLLocation *iface)
76 return CONTAINING_RECORD(iface, HTMLLocation, IHTMLLocation_iface);
79 static HRESULT WINAPI HTMLLocation_QueryInterface(IHTMLLocation *iface, REFIID riid, void **ppv)
81 HTMLLocation *This = impl_from_IHTMLLocation(iface);
85 if(IsEqualGUID(&IID_IUnknown, riid)) {
86 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
87 *ppv = &This->IHTMLLocation_iface;
88 }else if(IsEqualGUID(&IID_IHTMLLocation, riid)) {
89 TRACE("(%p)->(IID_IHTMLLocation %p)\n", This, ppv);
90 *ppv = &This->IHTMLLocation_iface;
91 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
92 return *ppv ? S_OK : E_NOINTERFACE;
96 IUnknown_AddRef((IUnknown*)*ppv);
100 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
101 return E_NOINTERFACE;
104 static ULONG WINAPI HTMLLocation_AddRef(IHTMLLocation *iface)
106 HTMLLocation *This = impl_from_IHTMLLocation(iface);
107 LONG ref = InterlockedIncrement(&This->ref);
109 TRACE("(%p) ref=%d\n", This, ref);
114 static ULONG WINAPI HTMLLocation_Release(IHTMLLocation *iface)
116 HTMLLocation *This = impl_from_IHTMLLocation(iface);
117 LONG ref = InterlockedDecrement(&This->ref);
119 TRACE("(%p) ref=%d\n", This, ref);
123 This->window->location = NULL;
124 release_dispex(&This->dispex);
131 static HRESULT WINAPI HTMLLocation_GetTypeInfoCount(IHTMLLocation *iface, UINT *pctinfo)
133 HTMLLocation *This = impl_from_IHTMLLocation(iface);
134 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
137 static HRESULT WINAPI HTMLLocation_GetTypeInfo(IHTMLLocation *iface, UINT iTInfo,
138 LCID lcid, ITypeInfo **ppTInfo)
140 HTMLLocation *This = impl_from_IHTMLLocation(iface);
141 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
144 static HRESULT WINAPI HTMLLocation_GetIDsOfNames(IHTMLLocation *iface, REFIID riid,
145 LPOLESTR *rgszNames, UINT cNames,
146 LCID lcid, DISPID *rgDispId)
148 HTMLLocation *This = impl_from_IHTMLLocation(iface);
149 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
153 static HRESULT WINAPI HTMLLocation_Invoke(IHTMLLocation *iface, DISPID dispIdMember,
154 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
155 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
157 HTMLLocation *This = impl_from_IHTMLLocation(iface);
158 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
159 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
162 static HRESULT WINAPI HTMLLocation_put_href(IHTMLLocation *iface, BSTR v)
164 HTMLLocation *This = impl_from_IHTMLLocation(iface);
166 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
168 if(!This->window || !This->window->base.outer_window) {
169 FIXME("No window available\n");
173 return navigate_url(This->window->base.outer_window, v, This->window->base.outer_window->uri);
176 static HRESULT WINAPI HTMLLocation_get_href(IHTMLLocation *iface, BSTR *p)
178 HTMLLocation *This = impl_from_IHTMLLocation(iface);
179 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
180 WCHAR *buf = NULL, *url_path = NULL;
185 TRACE("(%p)->(%p)\n", This, p);
190 url.dwSchemeLength = 1;
191 url.dwHostNameLength = 1;
192 url.dwUrlPathLength = 1;
193 url.dwExtraInfoLength = 1;
194 hres = get_url_components(This, &url);
198 switch(url.nScheme) {
199 case INTERNET_SCHEME_FILE:
201 /* prepend a slash */
202 url_path = HeapAlloc(GetProcessHeap(), 0, (url.dwUrlPathLength + 1) * sizeof(WCHAR));
204 return E_OUTOFMEMORY;
206 memcpy(url_path + 1, url.lpszUrlPath, url.dwUrlPathLength * sizeof(WCHAR));
207 url.lpszUrlPath = url_path;
208 url.dwUrlPathLength = url.dwUrlPathLength + 1;
212 case INTERNET_SCHEME_HTTP:
213 case INTERNET_SCHEME_HTTPS:
214 case INTERNET_SCHEME_FTP:
215 if(!url.dwUrlPathLength) {
216 /* add a slash if it's blank */
217 url_path = url.lpszUrlPath = HeapAlloc(GetProcessHeap(), 0, 1 * sizeof(WCHAR));
219 return E_OUTOFMEMORY;
220 url.lpszUrlPath[0] = '/';
221 url.dwUrlPathLength = 1;
229 /* replace \ with / */
230 for(i = 0; i < url.dwUrlPathLength; ++i)
231 if(url.lpszUrlPath[i] == '\\')
232 url.lpszUrlPath[i] = '/';
234 if(InternetCreateUrlW(&url, ICU_ESCAPE, NULL, &len)) {
235 FIXME("InternetCreateUrl succeeded with NULL buffer?\n");
240 if(GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
241 FIXME("InternetCreateUrl failed with error: %08x\n", GetLastError());
248 buf = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
254 if(!InternetCreateUrlW(&url, ICU_ESCAPE, buf, &len)) {
255 FIXME("InternetCreateUrl failed with error: %08x\n", GetLastError());
261 *p = SysAllocStringLen(buf, len);
270 HeapFree(GetProcessHeap(), 0, buf);
271 HeapFree(GetProcessHeap(), 0, url_path);
276 static HRESULT WINAPI HTMLLocation_put_protocol(IHTMLLocation *iface, BSTR v)
278 HTMLLocation *This = impl_from_IHTMLLocation(iface);
279 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
283 static HRESULT WINAPI HTMLLocation_get_protocol(IHTMLLocation *iface, BSTR *p)
285 HTMLLocation *This = impl_from_IHTMLLocation(iface);
291 TRACE("(%p)->(%p)\n", This, p);
296 if(!(uri = get_uri(This))) {
297 FIXME("No current URI\n");
301 hres = IUri_GetSchemeName(uri, &protocol);
304 if(hres == S_FALSE) {
305 SysFreeString(protocol);
310 len = SysStringLen(protocol);
311 ret = SysAllocStringLen(protocol, len+1);
312 SysFreeString(protocol);
314 return E_OUTOFMEMORY;
321 static HRESULT WINAPI HTMLLocation_put_host(IHTMLLocation *iface, BSTR v)
323 HTMLLocation *This = impl_from_IHTMLLocation(iface);
324 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
328 static HRESULT WINAPI HTMLLocation_get_host(IHTMLLocation *iface, BSTR *p)
330 HTMLLocation *This = impl_from_IHTMLLocation(iface);
331 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
334 TRACE("(%p)->(%p)\n", This, p);
339 url.dwHostNameLength = 1;
340 hres = get_url_components(This, &url);
344 if(!url.dwHostNameLength){
350 /* <hostname>:<port> */
351 const WCHAR format[] = {'%','u',0};
352 DWORD len = url.dwHostNameLength + 1 + 5;
355 buf = *p = SysAllocStringLen(NULL, len);
356 memcpy(buf, url.lpszHostName, url.dwHostNameLength * sizeof(WCHAR));
357 buf[url.dwHostNameLength] = ':';
358 snprintfW(buf + url.dwHostNameLength + 1, 6, format, url.nPort);
360 *p = SysAllocStringLen(url.lpszHostName, url.dwHostNameLength);
363 return E_OUTOFMEMORY;
367 static HRESULT WINAPI HTMLLocation_put_hostname(IHTMLLocation *iface, BSTR v)
369 HTMLLocation *This = impl_from_IHTMLLocation(iface);
370 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
374 static HRESULT WINAPI HTMLLocation_get_hostname(IHTMLLocation *iface, BSTR *p)
376 HTMLLocation *This = impl_from_IHTMLLocation(iface);
381 TRACE("(%p)->(%p)\n", This, p);
386 if(!(uri = get_uri(This))) {
387 FIXME("No current URI\n");
391 hres = IUri_GetHost(uri, &hostname);
394 }else if(hres == S_FALSE) {
395 SysFreeString(hostname);
404 static HRESULT WINAPI HTMLLocation_put_port(IHTMLLocation *iface, BSTR v)
406 HTMLLocation *This = impl_from_IHTMLLocation(iface);
407 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
411 static HRESULT WINAPI HTMLLocation_get_port(IHTMLLocation *iface, BSTR *p)
413 HTMLLocation *This = impl_from_IHTMLLocation(iface);
418 TRACE("(%p)->(%p)\n", This, p);
423 if(!(uri = get_uri(This))) {
424 FIXME("No current URI\n");
428 hres = IUri_GetPort(uri, &port);
433 static const WCHAR formatW[] = {'%','u',0};
436 sprintfW(buf, formatW, port);
437 *p = SysAllocString(buf);
439 *p = SysAllocStringLen(NULL, 0);
443 return E_OUTOFMEMORY;
447 static HRESULT WINAPI HTMLLocation_put_pathname(IHTMLLocation *iface, BSTR v)
449 HTMLLocation *This = impl_from_IHTMLLocation(iface);
450 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
454 static HRESULT WINAPI HTMLLocation_get_pathname(IHTMLLocation *iface, BSTR *p)
456 HTMLLocation *This = impl_from_IHTMLLocation(iface);
457 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
460 TRACE("(%p)->(%p)\n", This, p);
465 url.dwUrlPathLength = 1;
466 url.dwExtraInfoLength = 1;
467 hres = get_url_components(This, &url);
471 if(url.dwUrlPathLength && url.lpszUrlPath[0] == '/')
472 *p = SysAllocStringLen(url.lpszUrlPath + 1, url.dwUrlPathLength - 1);
474 *p = SysAllocStringLen(url.lpszUrlPath, url.dwUrlPathLength);
477 return E_OUTOFMEMORY;
481 static HRESULT WINAPI HTMLLocation_put_search(IHTMLLocation *iface, BSTR v)
483 HTMLLocation *This = impl_from_IHTMLLocation(iface);
484 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
488 static HRESULT WINAPI HTMLLocation_get_search(IHTMLLocation *iface, BSTR *p)
490 HTMLLocation *This = impl_from_IHTMLLocation(iface);
495 TRACE("(%p)->(%p)\n", This, p);
500 if(!(uri = get_uri(This))) {
501 FIXME("No current URI\n");
505 hres = IUri_GetQuery(uri, &query);
508 }else if(hres == S_FALSE) {
509 SysFreeString(query);
518 static HRESULT WINAPI HTMLLocation_put_hash(IHTMLLocation *iface, BSTR v)
520 HTMLLocation *This = impl_from_IHTMLLocation(iface);
521 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
525 static HRESULT WINAPI HTMLLocation_get_hash(IHTMLLocation *iface, BSTR *p)
527 HTMLLocation *This = impl_from_IHTMLLocation(iface);
532 TRACE("(%p)->(%p)\n", This, p);
537 if(!(uri = get_uri(This))) {
538 FIXME("No current URI\n");
542 hres = IUri_GetFragment(uri, &hash);
545 }else if(hres == S_FALSE) {
555 static HRESULT WINAPI HTMLLocation_reload(IHTMLLocation *iface, VARIANT_BOOL flag)
557 HTMLLocation *This = impl_from_IHTMLLocation(iface);
558 FIXME("(%p)->(%x)\n", This, flag);
562 static HRESULT WINAPI HTMLLocation_replace(IHTMLLocation *iface, BSTR bstr)
564 HTMLLocation *This = impl_from_IHTMLLocation(iface);
566 TRACE("(%p)->(%s)\n", This, debugstr_w(bstr));
568 if(!This->window || !This->window->base.outer_window) {
569 FIXME("No window available\n");
573 return navigate_url(This->window->base.outer_window, bstr, This->window->base.outer_window->uri);
576 static HRESULT WINAPI HTMLLocation_assign(IHTMLLocation *iface, BSTR bstr)
578 HTMLLocation *This = impl_from_IHTMLLocation(iface);
579 FIXME("(%p)->(%s)\n", This, debugstr_w(bstr));
583 static HRESULT WINAPI HTMLLocation_toString(IHTMLLocation *iface, BSTR *String)
585 HTMLLocation *This = impl_from_IHTMLLocation(iface);
586 FIXME("(%p)->(%p)\n", This, String);
590 static const IHTMLLocationVtbl HTMLLocationVtbl = {
591 HTMLLocation_QueryInterface,
593 HTMLLocation_Release,
594 HTMLLocation_GetTypeInfoCount,
595 HTMLLocation_GetTypeInfo,
596 HTMLLocation_GetIDsOfNames,
598 HTMLLocation_put_href,
599 HTMLLocation_get_href,
600 HTMLLocation_put_protocol,
601 HTMLLocation_get_protocol,
602 HTMLLocation_put_host,
603 HTMLLocation_get_host,
604 HTMLLocation_put_hostname,
605 HTMLLocation_get_hostname,
606 HTMLLocation_put_port,
607 HTMLLocation_get_port,
608 HTMLLocation_put_pathname,
609 HTMLLocation_get_pathname,
610 HTMLLocation_put_search,
611 HTMLLocation_get_search,
612 HTMLLocation_put_hash,
613 HTMLLocation_get_hash,
615 HTMLLocation_replace,
617 HTMLLocation_toString
620 static const tid_t HTMLLocation_iface_tids[] = {
624 static dispex_static_data_t HTMLLocation_dispex = {
626 DispHTMLLocation_tid,
628 HTMLLocation_iface_tids
632 HRESULT HTMLLocation_Create(HTMLInnerWindow *window, HTMLLocation **ret)
634 HTMLLocation *location;
636 location = heap_alloc(sizeof(*location));
638 return E_OUTOFMEMORY;
640 location->IHTMLLocation_iface.lpVtbl = &HTMLLocationVtbl;
642 location->window = window;
644 init_dispex(&location->dispex, (IUnknown*)&location->IHTMLLocation_iface, &HTMLLocation_dispex);