Synchronize up to trunk's revision r57784.
[reactos.git] / dll / win32 / mshtml / htmllocation.c
1 /*
2 * Copyright 2008 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 #include <stdarg.h>
20
21 #define COBJMACROS
22
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"
30
31 #include "wine/debug.h"
32
33 #include "mshtml_private.h"
34 #include "resource.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37
38 static HRESULT get_url(HTMLLocation *This, const WCHAR **ret)
39 {
40 if(!This->window || !This->window->url) {
41 FIXME("No current URL\n");
42 return E_NOTIMPL;
43 }
44
45 *ret = This->window->url;
46 return S_OK;
47 }
48
49 static HRESULT get_url_components(HTMLLocation *This, URL_COMPONENTSW *url)
50 {
51 const WCHAR *doc_url;
52 HRESULT hres;
53
54 hres = get_url(This, &doc_url);
55 if(FAILED(hres))
56 return hres;
57
58 if(!InternetCrackUrlW(doc_url, 0, 0, url)) {
59 FIXME("InternetCrackUrlW failed: 0x%08x\n", GetLastError());
60 SetLastError(0);
61 return E_FAIL;
62 }
63
64 return S_OK;
65 }
66
67 #define HTMLLOCATION_THIS(iface) DEFINE_THIS(HTMLLocation, HTMLLocation, iface)
68
69 static HRESULT WINAPI HTMLLocation_QueryInterface(IHTMLLocation *iface, REFIID riid, void **ppv)
70 {
71 HTMLLocation *This = HTMLLOCATION_THIS(iface);
72
73 *ppv = NULL;
74
75 if(IsEqualGUID(&IID_IUnknown, riid)) {
76 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
77 *ppv = HTMLLOCATION(This);
78 }else if(IsEqualGUID(&IID_IHTMLLocation, riid)) {
79 TRACE("(%p)->(IID_IHTMLLocation %p)\n", This, ppv);
80 *ppv = HTMLLOCATION(This);
81 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
82 return *ppv ? S_OK : E_NOINTERFACE;
83 }
84
85 if(*ppv) {
86 IUnknown_AddRef((IUnknown*)*ppv);
87 return S_OK;
88 }
89
90 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
91 return E_NOINTERFACE;
92 }
93
94 static ULONG WINAPI HTMLLocation_AddRef(IHTMLLocation *iface)
95 {
96 HTMLLocation *This = HTMLLOCATION_THIS(iface);
97 LONG ref = InterlockedIncrement(&This->ref);
98
99 TRACE("(%p) ref=%d\n", This, ref);
100
101 return ref;
102 }
103
104 static ULONG WINAPI HTMLLocation_Release(IHTMLLocation *iface)
105 {
106 HTMLLocation *This = HTMLLOCATION_THIS(iface);
107 LONG ref = InterlockedDecrement(&This->ref);
108
109 TRACE("(%p) ref=%d\n", This, ref);
110
111 if(!ref) {
112 if(This->window)
113 This->window->location = NULL;
114 release_dispex(&This->dispex);
115 heap_free(This);
116 }
117
118 return ref;
119 }
120
121 static HRESULT WINAPI HTMLLocation_GetTypeInfoCount(IHTMLLocation *iface, UINT *pctinfo)
122 {
123 HTMLLocation *This = HTMLLOCATION_THIS(iface);
124 return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This->dispex), pctinfo);
125 }
126
127 static HRESULT WINAPI HTMLLocation_GetTypeInfo(IHTMLLocation *iface, UINT iTInfo,
128 LCID lcid, ITypeInfo **ppTInfo)
129 {
130 HTMLLocation *This = HTMLLOCATION_THIS(iface);
131 return IDispatchEx_GetTypeInfo(DISPATCHEX(&This->dispex), iTInfo, lcid, ppTInfo);
132 }
133
134 static HRESULT WINAPI HTMLLocation_GetIDsOfNames(IHTMLLocation *iface, REFIID riid,
135 LPOLESTR *rgszNames, UINT cNames,
136 LCID lcid, DISPID *rgDispId)
137 {
138 HTMLLocation *This = HTMLLOCATION_THIS(iface);
139 return IDispatchEx_GetIDsOfNames(DISPATCHEX(&This->dispex), riid, rgszNames, cNames, lcid, rgDispId);
140 }
141
142 static HRESULT WINAPI HTMLLocation_Invoke(IHTMLLocation *iface, DISPID dispIdMember,
143 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
144 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
145 {
146 HTMLLocation *This = HTMLLOCATION_THIS(iface);
147 return IDispatchEx_Invoke(DISPATCHEX(&This->dispex), dispIdMember, riid, lcid,
148 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
149 }
150
151 static HRESULT WINAPI HTMLLocation_put_href(IHTMLLocation *iface, BSTR v)
152 {
153 HTMLLocation *This = HTMLLOCATION_THIS(iface);
154
155 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
156
157 if(!This->window) {
158 FIXME("No window available\n");
159 return E_FAIL;
160 }
161
162 return navigate_url(This->window, v, This->window->url);
163 }
164
165 static HRESULT WINAPI HTMLLocation_get_href(IHTMLLocation *iface, BSTR *p)
166 {
167 HTMLLocation *This = HTMLLOCATION_THIS(iface);
168 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
169 WCHAR *buf = NULL, *url_path = NULL;
170 HRESULT hres, ret;
171 DWORD len = 0;
172 int i;
173
174 TRACE("(%p)->(%p)\n", This, p);
175
176 if(!p)
177 return E_POINTER;
178
179 url.dwSchemeLength = 1;
180 url.dwHostNameLength = 1;
181 url.dwUrlPathLength = 1;
182 url.dwExtraInfoLength = 1;
183 hres = get_url_components(This, &url);
184 if(FAILED(hres))
185 return hres;
186
187 switch(url.nScheme) {
188 case INTERNET_SCHEME_FILE:
189 {
190 /* prepend a slash */
191 url_path = HeapAlloc(GetProcessHeap(), 0, (url.dwUrlPathLength + 1) * sizeof(WCHAR));
192 if(!url_path)
193 return E_OUTOFMEMORY;
194 url_path[0] = '/';
195 memcpy(url_path + 1, url.lpszUrlPath, url.dwUrlPathLength * sizeof(WCHAR));
196 url.lpszUrlPath = url_path;
197 url.dwUrlPathLength = url.dwUrlPathLength + 1;
198 }
199 break;
200
201 case INTERNET_SCHEME_HTTP:
202 case INTERNET_SCHEME_HTTPS:
203 case INTERNET_SCHEME_FTP:
204 if(!url.dwUrlPathLength) {
205 /* add a slash if it's blank */
206 url_path = url.lpszUrlPath = HeapAlloc(GetProcessHeap(), 0, 1 * sizeof(WCHAR));
207 if(!url.lpszUrlPath)
208 return E_OUTOFMEMORY;
209 url.lpszUrlPath[0] = '/';
210 url.dwUrlPathLength = 1;
211 }
212 break;
213
214 default:
215 break;
216 }
217
218 /* replace \ with / */
219 for(i = 0; i < url.dwUrlPathLength; ++i)
220 if(url.lpszUrlPath[i] == '\\')
221 url.lpszUrlPath[i] = '/';
222
223 if(InternetCreateUrlW(&url, ICU_ESCAPE, NULL, &len)) {
224 FIXME("InternetCreateUrl succeeded with NULL buffer?\n");
225 ret = E_FAIL;
226 goto cleanup;
227 }
228
229 if(GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
230 FIXME("InternetCreateUrl failed with error: %08x\n", GetLastError());
231 SetLastError(0);
232 ret = E_FAIL;
233 goto cleanup;
234 }
235 SetLastError(0);
236
237 buf = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
238 if(!buf) {
239 ret = E_OUTOFMEMORY;
240 goto cleanup;
241 }
242
243 if(!InternetCreateUrlW(&url, ICU_ESCAPE, buf, &len)) {
244 FIXME("InternetCreateUrl failed with error: %08x\n", GetLastError());
245 SetLastError(0);
246 ret = E_FAIL;
247 goto cleanup;
248 }
249
250 *p = SysAllocStringLen(buf, len);
251 if(!*p) {
252 ret = E_OUTOFMEMORY;
253 goto cleanup;
254 }
255
256 ret = S_OK;
257
258 cleanup:
259 HeapFree(GetProcessHeap(), 0, buf);
260 HeapFree(GetProcessHeap(), 0, url_path);
261
262 return ret;
263 }
264
265 static HRESULT WINAPI HTMLLocation_put_protocol(IHTMLLocation *iface, BSTR v)
266 {
267 HTMLLocation *This = HTMLLOCATION_THIS(iface);
268 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
269 return E_NOTIMPL;
270 }
271
272 static HRESULT WINAPI HTMLLocation_get_protocol(IHTMLLocation *iface, BSTR *p)
273 {
274 HTMLLocation *This = HTMLLOCATION_THIS(iface);
275 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
276 HRESULT hres;
277
278 TRACE("(%p)->(%p)\n", This, p);
279
280 if(!p)
281 return E_POINTER;
282
283 url.dwSchemeLength = 1;
284 hres = get_url_components(This, &url);
285 if(FAILED(hres))
286 return hres;
287
288 if(!url.dwSchemeLength) {
289 FIXME("Unexpected blank protocol\n");
290 return E_NOTIMPL;
291 }else {
292 WCHAR buf[url.dwSchemeLength + 1];
293 memcpy(buf, url.lpszScheme, url.dwSchemeLength * sizeof(WCHAR));
294 buf[url.dwSchemeLength] = ':';
295 *p = SysAllocStringLen(buf, url.dwSchemeLength + 1);
296 }
297 if(!*p)
298 return E_OUTOFMEMORY;
299 return S_OK;
300 }
301
302 static HRESULT WINAPI HTMLLocation_put_host(IHTMLLocation *iface, BSTR v)
303 {
304 HTMLLocation *This = HTMLLOCATION_THIS(iface);
305 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
306 return E_NOTIMPL;
307 }
308
309 static HRESULT WINAPI HTMLLocation_get_host(IHTMLLocation *iface, BSTR *p)
310 {
311 HTMLLocation *This = HTMLLOCATION_THIS(iface);
312 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
313 HRESULT hres;
314
315 TRACE("(%p)->(%p)\n", This, p);
316
317 if(!p)
318 return E_POINTER;
319
320 url.dwHostNameLength = 1;
321 hres = get_url_components(This, &url);
322 if(FAILED(hres))
323 return hres;
324
325 if(!url.dwHostNameLength){
326 *p = NULL;
327 return S_OK;
328 }
329
330 if(url.nPort) {
331 /* <hostname>:<port> */
332 const WCHAR format[] = {'%','d',0};
333 DWORD len = url.dwHostNameLength + 1 + 5 + 1;
334 WCHAR buf[len];
335
336 memcpy(buf, url.lpszHostName, url.dwHostNameLength * sizeof(WCHAR));
337 buf[url.dwHostNameLength] = ':';
338 snprintfW(buf + url.dwHostNameLength + 1, 6, format, url.nPort);
339 *p = SysAllocString(buf);
340 }else
341 *p = SysAllocStringLen(url.lpszHostName, url.dwHostNameLength);
342
343 if(!*p)
344 return E_OUTOFMEMORY;
345 return S_OK;
346 }
347
348 static HRESULT WINAPI HTMLLocation_put_hostname(IHTMLLocation *iface, BSTR v)
349 {
350 HTMLLocation *This = HTMLLOCATION_THIS(iface);
351 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
352 return E_NOTIMPL;
353 }
354
355 static HRESULT WINAPI HTMLLocation_get_hostname(IHTMLLocation *iface, BSTR *p)
356 {
357 HTMLLocation *This = HTMLLOCATION_THIS(iface);
358 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
359 HRESULT hres;
360
361 TRACE("(%p)->(%p)\n", This, p);
362
363 if(!p)
364 return E_POINTER;
365
366 url.dwHostNameLength = 1;
367 hres = get_url_components(This, &url);
368 if(FAILED(hres))
369 return hres;
370
371 if(!url.dwHostNameLength){
372 *p = NULL;
373 return S_OK;
374 }
375
376 *p = SysAllocStringLen(url.lpszHostName, url.dwHostNameLength);
377 if(!*p)
378 return E_OUTOFMEMORY;
379 return S_OK;
380 }
381
382 static HRESULT WINAPI HTMLLocation_put_port(IHTMLLocation *iface, BSTR v)
383 {
384 HTMLLocation *This = HTMLLOCATION_THIS(iface);
385 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
386 return E_NOTIMPL;
387 }
388
389 static HRESULT WINAPI HTMLLocation_get_port(IHTMLLocation *iface, BSTR *p)
390 {
391 HTMLLocation *This = HTMLLOCATION_THIS(iface);
392 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
393 HRESULT hres;
394
395 TRACE("(%p)->(%p)\n", This, p);
396
397 if(!p)
398 return E_POINTER;
399
400 hres = get_url_components(This, &url);
401 if(FAILED(hres))
402 return hres;
403
404 if(url.nPort) {
405 const WCHAR format[] = {'%','d',0};
406 WCHAR buf[6];
407 snprintfW(buf, 6, format, url.nPort);
408 *p = SysAllocString(buf);
409 }else {
410 const WCHAR empty[] = {0};
411 *p = SysAllocString(empty);
412 }
413
414 if(!*p)
415 return E_OUTOFMEMORY;
416 return S_OK;
417 }
418
419 static HRESULT WINAPI HTMLLocation_put_pathname(IHTMLLocation *iface, BSTR v)
420 {
421 HTMLLocation *This = HTMLLOCATION_THIS(iface);
422 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
423 return E_NOTIMPL;
424 }
425
426 static HRESULT WINAPI HTMLLocation_get_pathname(IHTMLLocation *iface, BSTR *p)
427 {
428 HTMLLocation *This = HTMLLOCATION_THIS(iface);
429 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
430 HRESULT hres;
431
432 TRACE("(%p)->(%p)\n", This, p);
433
434 if(!p)
435 return E_POINTER;
436
437 url.dwUrlPathLength = 1;
438 url.dwExtraInfoLength = 1;
439 hres = get_url_components(This, &url);
440 if(FAILED(hres))
441 return hres;
442
443 if(url.dwUrlPathLength && url.lpszUrlPath[0] == '/')
444 *p = SysAllocStringLen(url.lpszUrlPath + 1, url.dwUrlPathLength - 1);
445 else
446 *p = SysAllocStringLen(url.lpszUrlPath, url.dwUrlPathLength);
447
448 if(!*p)
449 return E_OUTOFMEMORY;
450 return S_OK;
451 }
452
453 static HRESULT WINAPI HTMLLocation_put_search(IHTMLLocation *iface, BSTR v)
454 {
455 HTMLLocation *This = HTMLLOCATION_THIS(iface);
456 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
457 return E_NOTIMPL;
458 }
459
460 static HRESULT WINAPI HTMLLocation_get_search(IHTMLLocation *iface, BSTR *p)
461 {
462 HTMLLocation *This = HTMLLOCATION_THIS(iface);
463 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
464 HRESULT hres;
465 const WCHAR hash[] = {'#',0};
466
467 TRACE("(%p)->(%p)\n", This, p);
468
469 if(!p)
470 return E_POINTER;
471
472 url.dwExtraInfoLength = 1;
473 hres = get_url_components(This, &url);
474 if(FAILED(hres))
475 return hres;
476
477 if(!url.dwExtraInfoLength){
478 *p = NULL;
479 return S_OK;
480 }
481
482 url.dwExtraInfoLength = strcspnW(url.lpszExtraInfo, hash);
483
484 *p = SysAllocStringLen(url.lpszExtraInfo, url.dwExtraInfoLength);
485
486 if(!*p)
487 return E_OUTOFMEMORY;
488 return S_OK;
489 }
490
491 static HRESULT WINAPI HTMLLocation_put_hash(IHTMLLocation *iface, BSTR v)
492 {
493 HTMLLocation *This = HTMLLOCATION_THIS(iface);
494 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
495 return E_NOTIMPL;
496 }
497
498 static HRESULT WINAPI HTMLLocation_get_hash(IHTMLLocation *iface, BSTR *p)
499 {
500 HTMLLocation *This = HTMLLOCATION_THIS(iface);
501 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
502 const WCHAR hash[] = {'#',0};
503 DWORD hash_pos = 0;
504 HRESULT hres;
505
506 TRACE("(%p)->(%p)\n", This, p);
507
508 if(!p)
509 return E_POINTER;
510
511 url.dwExtraInfoLength = 1;
512 hres = get_url_components(This, &url);
513 if(FAILED(hres))
514 return hres;
515
516 if(!url.dwExtraInfoLength){
517 *p = NULL;
518 return S_OK;
519 }
520
521 hash_pos = strcspnW(url.lpszExtraInfo, hash);
522 url.dwExtraInfoLength -= hash_pos;
523
524 *p = SysAllocStringLen(url.lpszExtraInfo + hash_pos, url.dwExtraInfoLength);
525
526 if(!*p)
527 return E_OUTOFMEMORY;
528 return S_OK;
529 }
530
531 static HRESULT WINAPI HTMLLocation_reload(IHTMLLocation *iface, VARIANT_BOOL flag)
532 {
533 HTMLLocation *This = HTMLLOCATION_THIS(iface);
534 FIXME("(%p)->(%x)\n", This, flag);
535 return E_NOTIMPL;
536 }
537
538 static HRESULT WINAPI HTMLLocation_replace(IHTMLLocation *iface, BSTR bstr)
539 {
540 HTMLLocation *This = HTMLLOCATION_THIS(iface);
541 FIXME("(%p)->(%s)\n", This, debugstr_w(bstr));
542 return E_NOTIMPL;
543 }
544
545 static HRESULT WINAPI HTMLLocation_assign(IHTMLLocation *iface, BSTR bstr)
546 {
547 HTMLLocation *This = HTMLLOCATION_THIS(iface);
548 FIXME("(%p)->(%s)\n", This, debugstr_w(bstr));
549 return E_NOTIMPL;
550 }
551
552 static HRESULT WINAPI HTMLLocation_toString(IHTMLLocation *iface, BSTR *String)
553 {
554 HTMLLocation *This = HTMLLOCATION_THIS(iface);
555 FIXME("(%p)->(%p)\n", This, String);
556 return E_NOTIMPL;
557 }
558
559 #undef HTMLLOCATION_THIS
560
561 static const IHTMLLocationVtbl HTMLLocationVtbl = {
562 HTMLLocation_QueryInterface,
563 HTMLLocation_AddRef,
564 HTMLLocation_Release,
565 HTMLLocation_GetTypeInfoCount,
566 HTMLLocation_GetTypeInfo,
567 HTMLLocation_GetIDsOfNames,
568 HTMLLocation_Invoke,
569 HTMLLocation_put_href,
570 HTMLLocation_get_href,
571 HTMLLocation_put_protocol,
572 HTMLLocation_get_protocol,
573 HTMLLocation_put_host,
574 HTMLLocation_get_host,
575 HTMLLocation_put_hostname,
576 HTMLLocation_get_hostname,
577 HTMLLocation_put_port,
578 HTMLLocation_get_port,
579 HTMLLocation_put_pathname,
580 HTMLLocation_get_pathname,
581 HTMLLocation_put_search,
582 HTMLLocation_get_search,
583 HTMLLocation_put_hash,
584 HTMLLocation_get_hash,
585 HTMLLocation_reload,
586 HTMLLocation_replace,
587 HTMLLocation_assign,
588 HTMLLocation_toString
589 };
590
591 static const tid_t HTMLLocation_iface_tids[] = {
592 IHTMLLocation_tid,
593 0
594 };
595 static dispex_static_data_t HTMLLocation_dispex = {
596 NULL,
597 DispHTMLLocation_tid,
598 NULL,
599 HTMLLocation_iface_tids
600 };
601
602
603 HRESULT HTMLLocation_Create(HTMLWindow *window, HTMLLocation **ret)
604 {
605 HTMLLocation *location;
606
607 location = heap_alloc(sizeof(*location));
608 if(!location)
609 return E_OUTOFMEMORY;
610
611 location->lpHTMLLocationVtbl = &HTMLLocationVtbl;
612 location->ref = 1;
613 location->window = window;
614
615 init_dispex(&location->dispex, (IUnknown*)HTMLLOCATION(location), &HTMLLocation_dispex);
616
617 *ret = location;
618 return S_OK;
619 }