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"
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");
46 *ret
= This
->window
->base
.outer_window
->url
;
50 static IUri
*get_uri(HTMLLocation
*This
)
52 if(!This
->window
|| !This
->window
->base
.outer_window
)
54 return This
->window
->base
.outer_window
->uri
;
57 static HRESULT
get_url_components(HTMLLocation
*This
, URL_COMPONENTSW
*url
)
62 hres
= get_url(This
, &doc_url
);
66 if(!InternetCrackUrlW(doc_url
, 0, 0, url
)) {
67 FIXME("InternetCrackUrlW failed: 0x%08x\n", GetLastError());
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
);
86 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
87 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
88 *ppv
= &This
->IHTMLLocation_iface
;
89 }else if(IsEqualGUID(&IID_IHTMLLocation
, riid
)) {
90 TRACE("(%p)->(IID_IHTMLLocation %p)\n", This
, ppv
);
91 *ppv
= &This
->IHTMLLocation_iface
;
92 }else if(dispex_query_interface(&This
->dispex
, riid
, ppv
)) {
93 return *ppv
? S_OK
: E_NOINTERFACE
;
97 IUnknown_AddRef((IUnknown
*)*ppv
);
101 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
102 return E_NOINTERFACE
;
105 static ULONG WINAPI
HTMLLocation_AddRef(IHTMLLocation
*iface
)
107 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
108 LONG ref
= InterlockedIncrement(&This
->ref
);
110 TRACE("(%p) ref=%d\n", This
, ref
);
115 static ULONG WINAPI
HTMLLocation_Release(IHTMLLocation
*iface
)
117 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
118 LONG ref
= InterlockedDecrement(&This
->ref
);
120 TRACE("(%p) ref=%d\n", This
, ref
);
124 This
->window
->location
= NULL
;
125 release_dispex(&This
->dispex
);
132 static HRESULT WINAPI
HTMLLocation_GetTypeInfoCount(IHTMLLocation
*iface
, UINT
*pctinfo
)
134 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
135 return IDispatchEx_GetTypeInfoCount(&This
->dispex
.IDispatchEx_iface
, pctinfo
);
138 static HRESULT WINAPI
HTMLLocation_GetTypeInfo(IHTMLLocation
*iface
, UINT iTInfo
,
139 LCID lcid
, ITypeInfo
**ppTInfo
)
141 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
142 return IDispatchEx_GetTypeInfo(&This
->dispex
.IDispatchEx_iface
, iTInfo
, lcid
, ppTInfo
);
145 static HRESULT WINAPI
HTMLLocation_GetIDsOfNames(IHTMLLocation
*iface
, REFIID riid
,
146 LPOLESTR
*rgszNames
, UINT cNames
,
147 LCID lcid
, DISPID
*rgDispId
)
149 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
150 return IDispatchEx_GetIDsOfNames(&This
->dispex
.IDispatchEx_iface
, riid
, rgszNames
, cNames
,
154 static HRESULT WINAPI
HTMLLocation_Invoke(IHTMLLocation
*iface
, DISPID dispIdMember
,
155 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
156 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
158 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
159 return IDispatchEx_Invoke(&This
->dispex
.IDispatchEx_iface
, dispIdMember
, riid
, lcid
,
160 wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
163 static HRESULT WINAPI
HTMLLocation_put_href(IHTMLLocation
*iface
, BSTR v
)
165 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
167 TRACE("(%p)->(%s)\n", This
, debugstr_w(v
));
169 if(!This
->window
|| !This
->window
->base
.outer_window
) {
170 FIXME("No window available\n");
174 return navigate_url(This
->window
->base
.outer_window
, v
, This
->window
->base
.outer_window
->uri
, BINDING_NAVIGATED
);
177 static HRESULT WINAPI
HTMLLocation_get_href(IHTMLLocation
*iface
, BSTR
*p
)
179 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
180 URL_COMPONENTSW url
= {sizeof(URL_COMPONENTSW
)};
181 WCHAR
*buf
= NULL
, *url_path
= NULL
;
186 TRACE("(%p)->(%p)\n", This
, p
);
191 url
.dwSchemeLength
= 1;
192 url
.dwHostNameLength
= 1;
193 url
.dwUrlPathLength
= 1;
194 url
.dwExtraInfoLength
= 1;
195 hres
= get_url_components(This
, &url
);
199 switch(url
.nScheme
) {
200 case INTERNET_SCHEME_FILE
:
202 /* prepend a slash */
203 url_path
= HeapAlloc(GetProcessHeap(), 0, (url
.dwUrlPathLength
+ 1) * sizeof(WCHAR
));
205 return E_OUTOFMEMORY
;
207 memcpy(url_path
+ 1, url
.lpszUrlPath
, url
.dwUrlPathLength
* sizeof(WCHAR
));
208 url
.lpszUrlPath
= url_path
;
209 url
.dwUrlPathLength
= url
.dwUrlPathLength
+ 1;
213 case INTERNET_SCHEME_HTTP
:
214 case INTERNET_SCHEME_HTTPS
:
215 case INTERNET_SCHEME_FTP
:
216 if(!url
.dwUrlPathLength
) {
217 /* add a slash if it's blank */
218 url_path
= url
.lpszUrlPath
= HeapAlloc(GetProcessHeap(), 0, 1 * sizeof(WCHAR
));
220 return E_OUTOFMEMORY
;
221 url
.lpszUrlPath
[0] = '/';
222 url
.dwUrlPathLength
= 1;
230 /* replace \ with / */
231 for(i
= 0; i
< url
.dwUrlPathLength
; ++i
)
232 if(url
.lpszUrlPath
[i
] == '\\')
233 url
.lpszUrlPath
[i
] = '/';
235 if(InternetCreateUrlW(&url
, ICU_ESCAPE
, NULL
, &len
)) {
236 FIXME("InternetCreateUrl succeeded with NULL buffer?\n");
241 if(GetLastError() != ERROR_INSUFFICIENT_BUFFER
) {
242 FIXME("InternetCreateUrl failed with error: %08x\n", GetLastError());
249 buf
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
255 if(!InternetCreateUrlW(&url
, ICU_ESCAPE
, buf
, &len
)) {
256 FIXME("InternetCreateUrl failed with error: %08x\n", GetLastError());
262 *p
= SysAllocStringLen(buf
, len
);
271 HeapFree(GetProcessHeap(), 0, buf
);
272 HeapFree(GetProcessHeap(), 0, url_path
);
277 static HRESULT WINAPI
HTMLLocation_put_protocol(IHTMLLocation
*iface
, BSTR v
)
279 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
280 FIXME("(%p)->(%s)\n", This
, debugstr_w(v
));
284 static HRESULT WINAPI
HTMLLocation_get_protocol(IHTMLLocation
*iface
, BSTR
*p
)
286 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
292 TRACE("(%p)->(%p)\n", This
, p
);
297 if(!(uri
= get_uri(This
))) {
298 FIXME("No current URI\n");
302 hres
= IUri_GetSchemeName(uri
, &protocol
);
305 if(hres
== S_FALSE
) {
306 SysFreeString(protocol
);
311 len
= SysStringLen(protocol
);
312 ret
= SysAllocStringLen(protocol
, len
+1);
313 SysFreeString(protocol
);
315 return E_OUTOFMEMORY
;
322 static HRESULT WINAPI
HTMLLocation_put_host(IHTMLLocation
*iface
, BSTR v
)
324 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
325 FIXME("(%p)->(%s)\n", This
, debugstr_w(v
));
329 static HRESULT WINAPI
HTMLLocation_get_host(IHTMLLocation
*iface
, BSTR
*p
)
331 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
332 URL_COMPONENTSW url
= {sizeof(URL_COMPONENTSW
)};
335 TRACE("(%p)->(%p)\n", This
, p
);
340 url
.dwHostNameLength
= 1;
341 hres
= get_url_components(This
, &url
);
345 if(!url
.dwHostNameLength
){
351 /* <hostname>:<port> */
352 const WCHAR format
[] = {'%','u',0};
353 DWORD len
= url
.dwHostNameLength
+ 1 + 5;
356 buf
= *p
= SysAllocStringLen(NULL
, len
);
357 memcpy(buf
, url
.lpszHostName
, url
.dwHostNameLength
* sizeof(WCHAR
));
358 buf
[url
.dwHostNameLength
] = ':';
359 snprintfW(buf
+ url
.dwHostNameLength
+ 1, 6, format
, url
.nPort
);
361 *p
= SysAllocStringLen(url
.lpszHostName
, url
.dwHostNameLength
);
364 return E_OUTOFMEMORY
;
368 static HRESULT WINAPI
HTMLLocation_put_hostname(IHTMLLocation
*iface
, BSTR v
)
370 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
371 FIXME("(%p)->(%s)\n", This
, debugstr_w(v
));
375 static HRESULT WINAPI
HTMLLocation_get_hostname(IHTMLLocation
*iface
, BSTR
*p
)
377 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
382 TRACE("(%p)->(%p)\n", This
, p
);
387 if(!(uri
= get_uri(This
))) {
388 FIXME("No current URI\n");
392 hres
= IUri_GetHost(uri
, &hostname
);
395 }else if(hres
== S_FALSE
) {
396 SysFreeString(hostname
);
405 static HRESULT WINAPI
HTMLLocation_put_port(IHTMLLocation
*iface
, BSTR v
)
407 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
408 FIXME("(%p)->(%s)\n", This
, debugstr_w(v
));
412 static HRESULT WINAPI
HTMLLocation_get_port(IHTMLLocation
*iface
, BSTR
*p
)
414 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
419 TRACE("(%p)->(%p)\n", This
, p
);
424 if(!(uri
= get_uri(This
))) {
425 FIXME("No current URI\n");
429 hres
= IUri_GetPort(uri
, &port
);
434 static const WCHAR formatW
[] = {'%','u',0};
437 sprintfW(buf
, formatW
, port
);
438 *p
= SysAllocString(buf
);
440 *p
= SysAllocStringLen(NULL
, 0);
444 return E_OUTOFMEMORY
;
448 static HRESULT WINAPI
HTMLLocation_put_pathname(IHTMLLocation
*iface
, BSTR v
)
450 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
451 FIXME("(%p)->(%s)\n", This
, debugstr_w(v
));
455 static HRESULT WINAPI
HTMLLocation_get_pathname(IHTMLLocation
*iface
, BSTR
*p
)
457 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
458 URL_COMPONENTSW url
= {sizeof(URL_COMPONENTSW
)};
461 TRACE("(%p)->(%p)\n", This
, p
);
466 url
.dwUrlPathLength
= 1;
467 url
.dwExtraInfoLength
= 1;
468 hres
= get_url_components(This
, &url
);
472 if(url
.dwUrlPathLength
&& url
.lpszUrlPath
[0] == '/')
473 *p
= SysAllocStringLen(url
.lpszUrlPath
+ 1, url
.dwUrlPathLength
- 1);
475 *p
= SysAllocStringLen(url
.lpszUrlPath
, url
.dwUrlPathLength
);
478 return E_OUTOFMEMORY
;
482 static HRESULT WINAPI
HTMLLocation_put_search(IHTMLLocation
*iface
, BSTR v
)
484 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
485 FIXME("(%p)->(%s)\n", This
, debugstr_w(v
));
489 static HRESULT WINAPI
HTMLLocation_get_search(IHTMLLocation
*iface
, BSTR
*p
)
491 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
496 TRACE("(%p)->(%p)\n", This
, p
);
501 if(!(uri
= get_uri(This
))) {
502 FIXME("No current URI\n");
506 hres
= IUri_GetQuery(uri
, &query
);
509 }else if(hres
== S_FALSE
) {
510 SysFreeString(query
);
519 static HRESULT WINAPI
HTMLLocation_put_hash(IHTMLLocation
*iface
, BSTR v
)
521 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
522 FIXME("(%p)->(%s)\n", This
, debugstr_w(v
));
526 static HRESULT WINAPI
HTMLLocation_get_hash(IHTMLLocation
*iface
, BSTR
*p
)
528 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
533 TRACE("(%p)->(%p)\n", This
, p
);
538 if(!(uri
= get_uri(This
))) {
539 FIXME("No current URI\n");
543 hres
= IUri_GetFragment(uri
, &hash
);
546 }else if(hres
== S_FALSE
) {
556 static HRESULT WINAPI
HTMLLocation_reload(IHTMLLocation
*iface
, VARIANT_BOOL flag
)
558 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
559 FIXME("(%p)->(%x)\n", This
, flag
);
563 static HRESULT WINAPI
HTMLLocation_replace(IHTMLLocation
*iface
, BSTR bstr
)
565 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
567 TRACE("(%p)->(%s)\n", This
, debugstr_w(bstr
));
569 if(!This
->window
|| !This
->window
->base
.outer_window
) {
570 FIXME("No window available\n");
574 return navigate_url(This
->window
->base
.outer_window
, bstr
, This
->window
->base
.outer_window
->uri
,
575 BINDING_NAVIGATED
|BINDING_REPLACE
);
578 static HRESULT WINAPI
HTMLLocation_assign(IHTMLLocation
*iface
, BSTR bstr
)
580 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
581 FIXME("(%p)->(%s)\n", This
, debugstr_w(bstr
));
585 static HRESULT WINAPI
HTMLLocation_toString(IHTMLLocation
*iface
, BSTR
*String
)
587 HTMLLocation
*This
= impl_from_IHTMLLocation(iface
);
589 TRACE("(%p)->(%p)\n", This
, String
);
591 return IHTMLLocation_get_href(&This
->IHTMLLocation_iface
, String
);
594 static const IHTMLLocationVtbl HTMLLocationVtbl
= {
595 HTMLLocation_QueryInterface
,
597 HTMLLocation_Release
,
598 HTMLLocation_GetTypeInfoCount
,
599 HTMLLocation_GetTypeInfo
,
600 HTMLLocation_GetIDsOfNames
,
602 HTMLLocation_put_href
,
603 HTMLLocation_get_href
,
604 HTMLLocation_put_protocol
,
605 HTMLLocation_get_protocol
,
606 HTMLLocation_put_host
,
607 HTMLLocation_get_host
,
608 HTMLLocation_put_hostname
,
609 HTMLLocation_get_hostname
,
610 HTMLLocation_put_port
,
611 HTMLLocation_get_port
,
612 HTMLLocation_put_pathname
,
613 HTMLLocation_get_pathname
,
614 HTMLLocation_put_search
,
615 HTMLLocation_get_search
,
616 HTMLLocation_put_hash
,
617 HTMLLocation_get_hash
,
619 HTMLLocation_replace
,
621 HTMLLocation_toString
624 static const tid_t HTMLLocation_iface_tids
[] = {
628 static dispex_static_data_t HTMLLocation_dispex
= {
630 DispHTMLLocation_tid
,
632 HTMLLocation_iface_tids
636 HRESULT
HTMLLocation_Create(HTMLInnerWindow
*window
, HTMLLocation
**ret
)
638 HTMLLocation
*location
;
640 location
= heap_alloc(sizeof(*location
));
642 return E_OUTOFMEMORY
;
644 location
->IHTMLLocation_iface
.lpVtbl
= &HTMLLocationVtbl
;
646 location
->window
= window
;
648 init_dispex(&location
->dispex
, (IUnknown
*)&location
->IHTMLLocation_iface
, &HTMLLocation_dispex
);