mshtml: Added IHTMLWindow2::clearTimeout implementation.
[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     LIST_FOR_EACH_SAFE(liter, ltmp, &thread_data->timer_list) {
98         timer = LIST_ENTRY(liter, task_timer_t, entry);
99         if(timer->doc == doc)
100             release_task_timer(thread_data->thread_hwnd, timer);
101     }
102
103     if(!list_empty(&thread_data->timer_list)) {
104         timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
105         SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time - GetTickCount(), NULL);
106     }
107
108     if(!thread_data)
109         return;
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     if(list_empty(&thread_data->timer_list)
132        || LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry)->time > timer->time) {
133
134         list_add_head(&thread_data->timer_list, &timer->entry);
135         return TRUE;
136     }
137
138     LIST_FOR_EACH_ENTRY(iter, &thread_data->timer_list, task_timer_t, entry) {
139         if(iter->time > timer->time) {
140             list_add_tail(&iter->entry, &timer->entry);
141             return FALSE;
142         }
143     }
144
145     list_add_tail(&thread_data->timer_list, &timer->entry);
146     return FALSE;
147 }
148
149 DWORD set_task_timer(HTMLDocument *doc, DWORD msec, BOOL interval, IDispatch *disp)
150 {
151     thread_data_t *thread_data = get_thread_data(TRUE);
152     task_timer_t *timer;
153     DWORD tc = GetTickCount();
154
155     static DWORD id_cnt = 0x20000000;
156
157     timer = heap_alloc(sizeof(task_timer_t));
158     timer->id = id_cnt++;
159     timer->doc = doc;
160     timer->time = tc + msec;
161     timer->interval = interval ? msec : 0;
162
163     IDispatch_AddRef(disp);
164     timer->disp = disp;
165
166     if(queue_timer(thread_data, timer))
167         SetTimer(thread_data->thread_hwnd, TIMER_ID, msec, NULL);
168
169     return timer->id;
170 }
171
172 HRESULT clear_task_timer(HTMLDocument *doc, BOOL interval, DWORD id)
173 {
174     thread_data_t *thread_data = get_thread_data(FALSE);
175     task_timer_t *iter;
176
177     if(!thread_data)
178         return S_OK;
179
180     LIST_FOR_EACH_ENTRY(iter, &thread_data->timer_list, task_timer_t, entry) {
181         if(iter->id == id && iter->doc == doc && (iter->interval == 0) == !interval) {
182             release_task_timer(thread_data->thread_hwnd, iter);
183             return S_OK;
184         }
185     }
186
187     WARN("timet not found\n");
188     return S_OK;
189 }
190
191 static void set_downloading(HTMLDocument *doc)
192 {
193     IOleCommandTarget *olecmd;
194     HRESULT hres;
195
196     TRACE("(%p)\n", doc);
197
198     if(doc->frame)
199         IOleInPlaceFrame_SetStatusText(doc->frame, NULL /* FIXME */);
200
201     if(!doc->client)
202         return;
203
204     hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
205     if(SUCCEEDED(hres)) {
206         VARIANT var;
207
208         V_VT(&var) = VT_I4;
209         V_I4(&var) = 1;
210
211         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETDOWNLOADSTATE, OLECMDEXECOPT_DONTPROMPTUSER,
212                                &var, NULL);
213         IOleCommandTarget_Release(olecmd);
214     }
215
216     if(doc->hostui) {
217         IDropTarget *drop_target = NULL;
218
219         hres = IDocHostUIHandler_GetDropTarget(doc->hostui, NULL /* FIXME */, &drop_target);
220         if(drop_target) {
221             FIXME("Use IDropTarget\n");
222             IDropTarget_Release(drop_target);
223         }
224     }
225 }
226
227 /* Calls undocumented 69 cmd of CGID_Explorer */
228 static void call_explorer_69(HTMLDocument *doc)
229 {
230     IOleCommandTarget *olecmd;
231     VARIANT var;
232     HRESULT hres;
233
234     if(!doc->client)
235         return;
236
237     hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
238     if(FAILED(hres))
239         return;
240
241     VariantInit(&var);
242     hres = IOleCommandTarget_Exec(olecmd, &CGID_Explorer, 69, 0, NULL, &var);
243     IOleCommandTarget_Release(olecmd);
244     if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
245         FIXME("handle result\n");
246 }
247
248 static void set_parsecomplete(HTMLDocument *doc)
249 {
250     IOleCommandTarget *olecmd = NULL;
251
252     TRACE("(%p)\n", doc);
253
254     if(doc->usermode == EDITMODE)
255         init_editor(doc);
256
257     call_explorer_69(doc);
258     call_property_onchanged(&doc->cp_propnotif, 1005);
259     call_explorer_69(doc);
260
261     doc->readystate = READYSTATE_INTERACTIVE;
262     call_property_onchanged(&doc->cp_propnotif, DISPID_READYSTATE);
263
264     if(doc->client)
265         IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
266
267     if(olecmd) {
268         VARIANT state, progress;
269
270         V_VT(&progress) = VT_I4;
271         V_I4(&progress) = 0;
272         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS, OLECMDEXECOPT_DONTPROMPTUSER,
273                                &progress, NULL);
274
275         V_VT(&state) = VT_I4;
276         V_I4(&state) = 0;
277         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETDOWNLOADSTATE, OLECMDEXECOPT_DONTPROMPTUSER,
278                                &state, NULL);
279
280         IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 103, 0, NULL, NULL);
281         IOleCommandTarget_Exec(olecmd, &CGID_MSHTML, IDM_PARSECOMPLETE, 0, NULL, NULL);
282         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_HTTPEQUIV_DONE, 0, NULL, NULL);
283
284         IOleCommandTarget_Release(olecmd);
285     }
286
287     doc->readystate = READYSTATE_COMPLETE;
288     call_property_onchanged(&doc->cp_propnotif, DISPID_READYSTATE);
289
290     if(doc->frame) {
291         static const WCHAR wszDone[] = {'D','o','n','e',0};
292         IOleInPlaceFrame_SetStatusText(doc->frame, wszDone);
293     }
294
295     update_title(doc);
296 }
297
298 static void set_progress(HTMLDocument *doc)
299 {
300     IOleCommandTarget *olecmd = NULL;
301     HRESULT hres;
302
303     TRACE("(%p)\n", doc);
304
305     if(doc->client)
306         IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
307
308     if(olecmd) {
309         VARIANT progress_max, progress;
310
311         V_VT(&progress_max) = VT_I4;
312         V_I4(&progress_max) = 0; /* FIXME */
313         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSMAX, OLECMDEXECOPT_DONTPROMPTUSER,
314                                &progress_max, NULL);
315
316         V_VT(&progress) = VT_I4;
317         V_I4(&progress) = 0; /* FIXME */
318         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS, OLECMDEXECOPT_DONTPROMPTUSER,
319                                &progress, NULL);
320     }
321
322     if(doc->usermode == EDITMODE && doc->hostui) {
323         DOCHOSTUIINFO hostinfo;
324
325         memset(&hostinfo, 0, sizeof(DOCHOSTUIINFO));
326         hostinfo.cbSize = sizeof(DOCHOSTUIINFO);
327         hres = IDocHostUIHandler_GetHostInfo(doc->hostui, &hostinfo);
328         if(SUCCEEDED(hres))
329             /* FIXME: use hostinfo */
330             TRACE("hostinfo = {%u %08x %08x %s %s}\n",
331                     hostinfo.cbSize, hostinfo.dwFlags, hostinfo.dwDoubleClick,
332                     debugstr_w(hostinfo.pchHostCss), debugstr_w(hostinfo.pchHostNS));
333     }
334 }
335
336 static void task_start_binding(HTMLDocument *doc, BSCallback *bscallback)
337 {
338     if(doc)
339         start_binding(doc, bscallback, NULL);
340     IUnknown_Release((IUnknown*)bscallback);
341 }
342
343 static void process_task(task_t *task)
344 {
345     switch(task->task_id) {
346     case TASK_SETDOWNLOADSTATE:
347         set_downloading(task->doc);
348         break;
349     case TASK_PARSECOMPLETE:
350         set_parsecomplete(task->doc);
351         break;
352     case TASK_SETPROGRESS:
353         set_progress(task->doc);
354         break;
355     case TASK_START_BINDING:
356         task_start_binding(task->doc, (BSCallback*)task->bscallback);
357         break;
358     default:
359         ERR("Wrong task_id %d\n", task->task_id);
360     }
361 }
362
363 static LRESULT process_timer(void)
364 {
365     thread_data_t *thread_data = get_thread_data(TRUE);
366     DWORD tc;
367     task_timer_t *timer;
368
369     TRACE("\n");
370
371     while(!list_empty(&thread_data->timer_list)) {
372         timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
373
374         tc = GetTickCount();
375         if(timer->time > tc) {
376             SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time-tc, NULL);
377             return 0;
378         }
379
380         list_remove(&timer->entry);
381         list_init(&timer->entry);
382
383         call_disp_func(timer->doc, timer->disp);
384
385         if(timer->interval) {
386             timer->time += timer->interval;
387             queue_timer(thread_data, timer);
388         }else {
389             release_task_timer(thread_data->thread_hwnd, timer);
390         }
391     }
392
393     KillTimer(thread_data->thread_hwnd, TIMER_ID);
394     return 0;
395 }
396
397 static LRESULT WINAPI hidden_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
398 {
399     switch(msg) {
400     case WM_PROCESSTASK:
401         while(1) {
402             task_t *task = pop_task();
403             if(!task)
404                 break;
405
406             process_task(task);
407             heap_free(task);
408         }
409
410         return 0;
411     case WM_TIMER:
412         return process_timer();
413     }
414
415     if(msg > WM_USER)
416         FIXME("(%p %d %lx %lx)\n", hwnd, msg, wParam, lParam);
417
418     return DefWindowProcW(hwnd, msg, wParam, lParam);
419 }
420
421 static HWND create_thread_hwnd(void)
422 {
423     static ATOM hidden_wnd_class = 0;
424     static const WCHAR wszInternetExplorer_Hidden[] = {'I','n','t','e','r','n','e','t',
425             ' ','E','x','p','l','o','r','e','r','_','H','i','d','d','e','n',0};
426
427     if(!hidden_wnd_class) {
428         WNDCLASSEXW wndclass = {
429             sizeof(WNDCLASSEXW), 0,
430             hidden_proc,
431             0, 0, hInst, NULL, NULL, NULL, NULL,
432             wszInternetExplorer_Hidden,
433             NULL
434         };
435
436         hidden_wnd_class = RegisterClassExW(&wndclass);
437     }
438
439     return CreateWindowExW(0, wszInternetExplorer_Hidden, NULL, WS_POPUP,
440                            0, 0, 0, 0, NULL, NULL, hInst, NULL);
441 }
442
443 HWND get_thread_hwnd(void)
444 {
445     thread_data_t *thread_data = get_thread_data(TRUE);
446
447     if(!thread_data->thread_hwnd)
448         thread_data->thread_hwnd = create_thread_hwnd();
449
450     return thread_data->thread_hwnd;
451 }
452
453 thread_data_t *get_thread_data(BOOL create)
454 {
455     thread_data_t *thread_data;
456
457     if(!mshtml_tls) {
458         if(create)
459             mshtml_tls = TlsAlloc();
460         else
461             return NULL;
462     }
463
464     thread_data = TlsGetValue(mshtml_tls);
465     if(!thread_data && create) {
466         thread_data = heap_alloc_zero(sizeof(thread_data_t));
467         TlsSetValue(mshtml_tls, thread_data);
468         list_init(&thread_data->timer_list);
469     }
470
471     return thread_data;
472 }