dwmapi: Add a stub for DwmGetTransportAttributes.
[wine] / dlls / mshtml / task.c
1 /*
2  * Copyright 2006 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 "config.h"
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "ole2.h"
30 #include "mshtmcid.h"
31 #include "shlguid.h"
32
33 #include "wine/debug.h"
34
35 #include "mshtml_private.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38
39 #define WM_PROCESSTASK 0x8008
40 #define TIMER_ID 0x3000
41
42 typedef struct {
43     HTMLDocument *doc;
44     DWORD id;
45     DWORD time;
46     DWORD interval;
47     IDispatch *disp;
48
49     struct list entry;
50 } task_timer_t;
51
52 void push_task(task_t *task)
53 {
54     thread_data_t *thread_data = get_thread_data(TRUE);
55
56     if(thread_data->task_queue_tail)
57         thread_data->task_queue_tail->next = task;
58     else
59         thread_data->task_queue_head = task;
60
61     thread_data->task_queue_tail = task;
62
63     PostMessageW(thread_data->thread_hwnd, WM_PROCESSTASK, 0, 0);
64 }
65
66 static task_t *pop_task(void)
67 {
68     thread_data_t *thread_data = get_thread_data(TRUE);
69     task_t *task = thread_data->task_queue_head;
70
71     if(!task)
72         return NULL;
73
74     thread_data->task_queue_head = task->next;
75     if(!thread_data->task_queue_head)
76         thread_data->task_queue_tail = NULL;
77
78     return task;
79 }
80
81 static void release_task_timer(HWND thread_hwnd, task_timer_t *timer)
82 {
83     list_remove(&timer->entry);
84
85     IDispatch_Release(timer->disp);
86
87     heap_free(timer);
88 }
89
90 void remove_doc_tasks(const HTMLDocument *doc)
91 {
92     thread_data_t *thread_data = get_thread_data(FALSE);
93     struct list *liter, *ltmp;
94     task_timer_t *timer;
95     task_t *iter, *tmp;
96
97     if(!thread_data)
98         return;
99
100     LIST_FOR_EACH_SAFE(liter, ltmp, &thread_data->timer_list) {
101         timer = LIST_ENTRY(liter, task_timer_t, entry);
102         if(timer->doc == doc)
103             release_task_timer(thread_data->thread_hwnd, timer);
104     }
105
106     if(!list_empty(&thread_data->timer_list)) {
107         timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
108         SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time - GetTickCount(), NULL);
109     }
110
111     while(thread_data->task_queue_head
112           && thread_data->task_queue_head->doc == doc)
113         pop_task();
114
115     for(iter = thread_data->task_queue_head; iter; iter = iter->next) {
116         while(iter->next && iter->next->doc == doc) {
117             tmp = iter->next;
118             iter->next = tmp->next;
119             heap_free(tmp);
120         }
121
122         if(!iter->next)
123             thread_data->task_queue_tail = iter;
124     }
125 }
126
127 static BOOL queue_timer(thread_data_t *thread_data, task_timer_t *timer)
128 {
129     task_timer_t *iter;
130
131     list_remove(&timer->entry);
132
133     if(list_empty(&thread_data->timer_list)
134        || LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry)->time > timer->time) {
135
136         list_add_head(&thread_data->timer_list, &timer->entry);
137         return TRUE;
138     }
139
140     LIST_FOR_EACH_ENTRY(iter, &thread_data->timer_list, task_timer_t, entry) {
141         if(iter->time > timer->time) {
142             list_add_tail(&iter->entry, &timer->entry);
143             return FALSE;
144         }
145     }
146
147     list_add_tail(&thread_data->timer_list, &timer->entry);
148     return FALSE;
149 }
150
151 DWORD set_task_timer(HTMLDocument *doc, DWORD msec, BOOL interval, IDispatch *disp)
152 {
153     thread_data_t *thread_data = get_thread_data(TRUE);
154     task_timer_t *timer;
155     DWORD tc = GetTickCount();
156
157     static DWORD id_cnt = 0x20000000;
158
159     timer = heap_alloc(sizeof(task_timer_t));
160     timer->id = id_cnt++;
161     timer->doc = doc;
162     timer->time = tc + msec;
163     timer->interval = interval ? msec : 0;
164     list_init(&timer->entry);
165
166     IDispatch_AddRef(disp);
167     timer->disp = disp;
168
169     if(queue_timer(thread_data, timer))
170         SetTimer(thread_data->thread_hwnd, TIMER_ID, msec, NULL);
171
172     return timer->id;
173 }
174
175 HRESULT clear_task_timer(HTMLDocument *doc, BOOL interval, DWORD id)
176 {
177     thread_data_t *thread_data = get_thread_data(FALSE);
178     task_timer_t *iter;
179
180     if(!thread_data)
181         return S_OK;
182
183     LIST_FOR_EACH_ENTRY(iter, &thread_data->timer_list, task_timer_t, entry) {
184         if(iter->id == id && iter->doc == doc && (iter->interval == 0) == !interval) {
185             release_task_timer(thread_data->thread_hwnd, iter);
186             return S_OK;
187         }
188     }
189
190     WARN("timet not found\n");
191     return S_OK;
192 }
193
194 static void set_downloading(HTMLDocumentObj *doc)
195 {
196     IOleCommandTarget *olecmd;
197     HRESULT hres;
198
199     TRACE("(%p)\n", doc);
200
201     if(doc->frame)
202         IOleInPlaceFrame_SetStatusText(doc->frame, NULL /* FIXME */);
203
204     if(!doc->client)
205         return;
206
207     hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
208     if(SUCCEEDED(hres)) {
209         VARIANT var;
210
211         V_VT(&var) = VT_I4;
212         V_I4(&var) = 1;
213
214         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETDOWNLOADSTATE, OLECMDEXECOPT_DONTPROMPTUSER,
215                                &var, NULL);
216         IOleCommandTarget_Release(olecmd);
217     }
218
219     if(doc->hostui) {
220         IDropTarget *drop_target = NULL;
221
222         hres = IDocHostUIHandler_GetDropTarget(doc->hostui, NULL /* FIXME */, &drop_target);
223         if(drop_target) {
224             FIXME("Use IDropTarget\n");
225             IDropTarget_Release(drop_target);
226         }
227     }
228 }
229
230 /* Calls undocumented 69 cmd of CGID_Explorer */
231 static void call_explorer_69(HTMLDocumentObj *doc)
232 {
233     IOleCommandTarget *olecmd;
234     VARIANT var;
235     HRESULT hres;
236
237     if(!doc->client)
238         return;
239
240     hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
241     if(FAILED(hres))
242         return;
243
244     VariantInit(&var);
245     hres = IOleCommandTarget_Exec(olecmd, &CGID_Explorer, 69, 0, NULL, &var);
246     IOleCommandTarget_Release(olecmd);
247     if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
248         FIXME("handle result\n");
249 }
250
251 static void set_parsecomplete(HTMLDocument *doc)
252 {
253     IOleCommandTarget *olecmd = NULL;
254
255     TRACE("(%p)\n", doc);
256
257     if(doc->doc_obj->usermode == EDITMODE)
258         init_editor(doc);
259
260     call_explorer_69(doc->doc_obj);
261     call_property_onchanged(&doc->cp_propnotif, 1005);
262     call_explorer_69(doc->doc_obj);
263
264     /* FIXME: IE7 calls EnableModelless(TRUE), EnableModelless(FALSE) and sets interactive state here */
265
266     doc->doc_obj->readystate = READYSTATE_INTERACTIVE;
267     call_property_onchanged(&doc->cp_propnotif, DISPID_READYSTATE);
268
269     if(doc->doc_obj->client)
270         IOleClientSite_QueryInterface(doc->doc_obj->client, &IID_IOleCommandTarget, (void**)&olecmd);
271
272     if(olecmd) {
273         VARIANT state, progress;
274
275         V_VT(&progress) = VT_I4;
276         V_I4(&progress) = 0;
277         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS, OLECMDEXECOPT_DONTPROMPTUSER,
278                                &progress, NULL);
279
280         V_VT(&state) = VT_I4;
281         V_I4(&state) = 0;
282         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETDOWNLOADSTATE, OLECMDEXECOPT_DONTPROMPTUSER,
283                                &state, NULL);
284
285         IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 103, 0, NULL, NULL);
286         IOleCommandTarget_Exec(olecmd, &CGID_MSHTML, IDM_PARSECOMPLETE, 0, NULL, NULL);
287         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_HTTPEQUIV_DONE, 0, NULL, NULL);
288
289         IOleCommandTarget_Release(olecmd);
290     }
291
292     doc->doc_obj->readystate = READYSTATE_COMPLETE;
293     call_property_onchanged(&doc->cp_propnotif, DISPID_READYSTATE);
294
295     if(doc->doc_obj->frame) {
296         static const WCHAR wszDone[] = {'D','o','n','e',0};
297         IOleInPlaceFrame_SetStatusText(doc->doc_obj->frame, wszDone);
298     }
299
300     update_title(doc->doc_obj);
301 }
302
303 static void set_progress(HTMLDocument *doc)
304 {
305     IOleCommandTarget *olecmd = NULL;
306     HRESULT hres;
307
308     TRACE("(%p)\n", doc);
309
310     if(doc->doc_obj->client)
311         IOleClientSite_QueryInterface(doc->doc_obj->client, &IID_IOleCommandTarget, (void**)&olecmd);
312
313     if(olecmd) {
314         VARIANT progress_max, progress;
315
316         V_VT(&progress_max) = VT_I4;
317         V_I4(&progress_max) = 0; /* FIXME */
318         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSMAX, OLECMDEXECOPT_DONTPROMPTUSER,
319                                &progress_max, NULL);
320
321         V_VT(&progress) = VT_I4;
322         V_I4(&progress) = 0; /* FIXME */
323         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS, OLECMDEXECOPT_DONTPROMPTUSER,
324                                &progress, NULL);
325     }
326
327     if(doc->doc_obj->usermode == EDITMODE && doc->doc_obj->hostui) {
328         DOCHOSTUIINFO hostinfo;
329
330         memset(&hostinfo, 0, sizeof(DOCHOSTUIINFO));
331         hostinfo.cbSize = sizeof(DOCHOSTUIINFO);
332         hres = IDocHostUIHandler_GetHostInfo(doc->doc_obj->hostui, &hostinfo);
333         if(SUCCEEDED(hres))
334             /* FIXME: use hostinfo */
335             TRACE("hostinfo = {%u %08x %08x %s %s}\n",
336                     hostinfo.cbSize, hostinfo.dwFlags, hostinfo.dwDoubleClick,
337                     debugstr_w(hostinfo.pchHostCss), debugstr_w(hostinfo.pchHostNS));
338     }
339 }
340
341 static void task_start_binding(HTMLDocument *doc, BSCallback *bscallback)
342 {
343     if(doc)
344         start_binding(doc, bscallback, NULL);
345     IUnknown_Release((IUnknown*)bscallback);
346 }
347
348 static void process_task(task_t *task)
349 {
350     switch(task->task_id) {
351     case TASK_SETDOWNLOADSTATE:
352         set_downloading(task->doc->doc_obj);
353         break;
354     case TASK_PARSECOMPLETE:
355         set_parsecomplete(task->doc);
356         break;
357     case TASK_SETPROGRESS:
358         set_progress(task->doc);
359         break;
360     case TASK_START_BINDING:
361         task_start_binding(task->doc, (BSCallback*)task->bscallback);
362         break;
363     default:
364         ERR("Wrong task_id %d\n", task->task_id);
365     }
366 }
367
368 static void call_timer_disp(IDispatch *disp)
369 {
370     DISPPARAMS dp = {NULL, NULL, 0, 0};
371     EXCEPINFO ei;
372     VARIANT res;
373     HRESULT hres;
374
375     V_VT(&res) = VT_EMPTY;
376     memset(&ei, 0, sizeof(ei));
377
378     TRACE(">>>\n");
379     hres = IDispatch_Invoke(disp, DISPID_VALUE, &IID_NULL, 0, DISPATCH_METHOD, &dp, &res, &ei, NULL);
380     if(hres == S_OK)
381         TRACE("<<<\n");
382     else
383         WARN("<<< %08x\n", hres);
384
385     VariantClear(&res);
386 }
387
388 static LRESULT process_timer(void)
389 {
390     thread_data_t *thread_data = get_thread_data(TRUE);
391     HTMLDocument *doc;
392     IDispatch *disp;
393     DWORD tc;
394     task_timer_t *timer;
395
396     TRACE("\n");
397
398     while(!list_empty(&thread_data->timer_list)) {
399         timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
400
401         tc = GetTickCount();
402         if(timer->time > tc) {
403             SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time-tc, NULL);
404             return 0;
405         }
406
407         doc = timer->doc;
408         disp = timer->disp;
409         IDispatch_AddRef(disp);
410
411         if(timer->interval) {
412             timer->time += timer->interval;
413             queue_timer(thread_data, timer);
414         }else {
415             release_task_timer(thread_data->thread_hwnd, timer);
416         }
417
418         call_timer_disp(disp);
419
420         IDispatch_Release(disp);
421     }
422
423     KillTimer(thread_data->thread_hwnd, TIMER_ID);
424     return 0;
425 }
426
427 static LRESULT WINAPI hidden_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
428 {
429     switch(msg) {
430     case WM_PROCESSTASK:
431         while(1) {
432             task_t *task = pop_task();
433             if(!task)
434                 break;
435
436             process_task(task);
437             heap_free(task);
438         }
439
440         return 0;
441     case WM_TIMER:
442         return process_timer();
443     }
444
445     if(msg > WM_USER)
446         FIXME("(%p %d %lx %lx)\n", hwnd, msg, wParam, lParam);
447
448     return DefWindowProcW(hwnd, msg, wParam, lParam);
449 }
450
451 static HWND create_thread_hwnd(void)
452 {
453     static ATOM hidden_wnd_class = 0;
454     static const WCHAR wszInternetExplorer_Hidden[] = {'I','n','t','e','r','n','e','t',
455             ' ','E','x','p','l','o','r','e','r','_','H','i','d','d','e','n',0};
456
457     if(!hidden_wnd_class) {
458         WNDCLASSEXW wndclass = {
459             sizeof(WNDCLASSEXW), 0,
460             hidden_proc,
461             0, 0, hInst, NULL, NULL, NULL, NULL,
462             wszInternetExplorer_Hidden,
463             NULL
464         };
465
466         hidden_wnd_class = RegisterClassExW(&wndclass);
467     }
468
469     return CreateWindowExW(0, wszInternetExplorer_Hidden, NULL, WS_POPUP,
470                            0, 0, 0, 0, NULL, NULL, hInst, NULL);
471 }
472
473 HWND get_thread_hwnd(void)
474 {
475     thread_data_t *thread_data = get_thread_data(TRUE);
476
477     if(!thread_data->thread_hwnd)
478         thread_data->thread_hwnd = create_thread_hwnd();
479
480     return thread_data->thread_hwnd;
481 }
482
483 thread_data_t *get_thread_data(BOOL create)
484 {
485     thread_data_t *thread_data;
486
487     if(mshtml_tls == TLS_OUT_OF_INDEXES) {
488         DWORD tls;
489
490         if(!create)
491             return NULL;
492
493         tls = TlsAlloc();
494         if(tls == TLS_OUT_OF_INDEXES)
495             return NULL;
496
497         tls = InterlockedCompareExchange((LONG*)&mshtml_tls, tls, TLS_OUT_OF_INDEXES);
498         if(tls != mshtml_tls)
499             TlsFree(tls);
500     }
501
502     thread_data = TlsGetValue(mshtml_tls);
503     if(!thread_data && create) {
504         thread_data = heap_alloc_zero(sizeof(thread_data_t));
505         TlsSetValue(mshtml_tls, thread_data);
506         list_init(&thread_data->timer_list);
507     }
508
509     return thread_data;
510 }