Release 1.5.29.
[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 #include <assert.h>
24
25 #define COBJMACROS
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "ole2.h"
31
32 #include "wine/debug.h"
33
34 #include "mshtml_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37
38 #define WM_PROCESSTASK 0x8008
39 #define TIMER_ID 0x3000
40
41 typedef struct {
42     HTMLInnerWindow *window;
43     DWORD id;
44     DWORD time;
45     DWORD interval;
46     IDispatch *disp;
47
48     struct list entry;
49 } task_timer_t;
50
51 static void default_task_destr(task_t *task)
52 {
53     heap_free(task);
54 }
55
56 HRESULT push_task(task_t *task, task_proc_t proc, task_proc_t destr, LONG magic)
57 {
58     thread_data_t *thread_data;
59
60     thread_data = get_thread_data(TRUE);
61     if(!thread_data) {
62         if(destr)
63             destr(task);
64         else
65             heap_free(task);
66         return E_OUTOFMEMORY;
67     }
68
69     task->target_magic = magic;
70     task->proc = proc;
71     task->destr = destr ? destr : default_task_destr;
72     task->next = NULL;
73
74     if(thread_data->task_queue_tail)
75         thread_data->task_queue_tail->next = task;
76     else
77         thread_data->task_queue_head = task;
78
79     thread_data->task_queue_tail = task;
80
81     PostMessageW(thread_data->thread_hwnd, WM_PROCESSTASK, 0, 0);
82     return S_OK;
83 }
84
85 static task_t *pop_task(void)
86 {
87     thread_data_t *thread_data;
88     task_t *task;
89
90     thread_data = get_thread_data(FALSE);
91     if(!thread_data)
92         return NULL;
93
94     task = thread_data->task_queue_head;
95     if(!task)
96         return NULL;
97
98     thread_data->task_queue_head = task->next;
99     if(!thread_data->task_queue_head)
100         thread_data->task_queue_tail = NULL;
101
102     return task;
103 }
104
105 static void release_task_timer(HWND thread_hwnd, task_timer_t *timer)
106 {
107     list_remove(&timer->entry);
108
109     IDispatch_Release(timer->disp);
110
111     heap_free(timer);
112 }
113
114 void remove_target_tasks(LONG target)
115 {
116     thread_data_t *thread_data = get_thread_data(FALSE);
117     struct list *liter, *ltmp;
118     task_timer_t *timer;
119     task_t *iter, *tmp;
120
121     if(!thread_data)
122         return;
123
124     LIST_FOR_EACH_SAFE(liter, ltmp, &thread_data->timer_list) {
125         timer = LIST_ENTRY(liter, task_timer_t, entry);
126         if(timer->window->task_magic == target)
127             release_task_timer(thread_data->thread_hwnd, timer);
128     }
129
130     if(!list_empty(&thread_data->timer_list)) {
131         timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
132         SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time - GetTickCount(), NULL);
133     }
134
135     while(thread_data->task_queue_head && thread_data->task_queue_head->target_magic == target) {
136         iter = pop_task();
137         iter->destr(iter);
138     }
139
140     for(iter = thread_data->task_queue_head; iter; iter = iter->next) {
141         while(iter->next && iter->next->target_magic == target) {
142             tmp = iter->next;
143             iter->next = tmp->next;
144             tmp->destr(tmp);
145         }
146
147         if(!iter->next)
148             thread_data->task_queue_tail = iter;
149     }
150 }
151
152 LONG get_task_target_magic(void)
153 {
154     static LONG magic = 0x10000000;
155     return InterlockedIncrement(&magic);
156 }
157
158 static BOOL queue_timer(thread_data_t *thread_data, task_timer_t *timer)
159 {
160     task_timer_t *iter;
161
162     list_remove(&timer->entry);
163
164     if(list_empty(&thread_data->timer_list)
165        || LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry)->time > timer->time) {
166
167         list_add_head(&thread_data->timer_list, &timer->entry);
168         return TRUE;
169     }
170
171     LIST_FOR_EACH_ENTRY(iter, &thread_data->timer_list, task_timer_t, entry) {
172         if(iter->time > timer->time) {
173             list_add_tail(&iter->entry, &timer->entry);
174             return FALSE;
175         }
176     }
177
178     list_add_tail(&thread_data->timer_list, &timer->entry);
179     return FALSE;
180 }
181
182 HRESULT set_task_timer(HTMLInnerWindow *window, DWORD msec, BOOL interval, IDispatch *disp, LONG *id)
183 {
184     thread_data_t *thread_data;
185     task_timer_t *timer;
186     DWORD tc = GetTickCount();
187
188     static DWORD id_cnt = 0x20000000;
189
190     thread_data = get_thread_data(TRUE);
191     if(!thread_data)
192         return E_OUTOFMEMORY;
193
194     timer = heap_alloc(sizeof(task_timer_t));
195     if(!timer)
196         return E_OUTOFMEMORY;
197
198     timer->id = id_cnt++;
199     timer->window = window;
200     timer->time = tc + msec;
201     timer->interval = interval ? msec : 0;
202     list_init(&timer->entry);
203
204     IDispatch_AddRef(disp);
205     timer->disp = disp;
206
207     if(queue_timer(thread_data, timer))
208         SetTimer(thread_data->thread_hwnd, TIMER_ID, msec, NULL);
209
210     *id = timer->id;
211     return S_OK;
212 }
213
214 HRESULT clear_task_timer(HTMLInnerWindow *window, BOOL interval, DWORD id)
215 {
216     thread_data_t *thread_data = get_thread_data(FALSE);
217     task_timer_t *iter;
218
219     if(!thread_data)
220         return S_OK;
221
222     LIST_FOR_EACH_ENTRY(iter, &thread_data->timer_list, task_timer_t, entry) {
223         if(iter->id == id && iter->window == window && !iter->interval == !interval) {
224             release_task_timer(thread_data->thread_hwnd, iter);
225             return S_OK;
226         }
227     }
228
229     WARN("timet not found\n");
230     return S_OK;
231 }
232
233 static void call_timer_disp(IDispatch *disp)
234 {
235     DISPPARAMS dp = {NULL, NULL, 0, 0};
236     EXCEPINFO ei;
237     VARIANT res;
238     HRESULT hres;
239
240     V_VT(&res) = VT_EMPTY;
241     memset(&ei, 0, sizeof(ei));
242
243     TRACE(">>>\n");
244     hres = IDispatch_Invoke(disp, DISPID_VALUE, &IID_NULL, 0, DISPATCH_METHOD, &dp, &res, &ei, NULL);
245     if(hres == S_OK)
246         TRACE("<<<\n");
247     else
248         WARN("<<< %08x\n", hres);
249
250     VariantClear(&res);
251 }
252
253 static LRESULT process_timer(void)
254 {
255     thread_data_t *thread_data;
256     IDispatch *disp;
257     DWORD tc;
258     task_timer_t *timer=NULL, *last_timer;
259
260     TRACE("\n");
261
262     thread_data = get_thread_data(FALSE);
263     assert(thread_data != NULL);
264
265     if(list_empty(&thread_data->timer_list)) {
266         KillTimer(thread_data->thread_hwnd, TIMER_ID);
267         return 0;
268     }
269
270     last_timer = LIST_ENTRY(list_tail(&thread_data->timer_list), task_timer_t, entry);
271     do {
272         tc = GetTickCount();
273         if(timer == last_timer) {
274             timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
275             SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time>tc ? timer->time-tc : 0, NULL);
276             return 0;
277         }
278
279         timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
280
281         if(timer->time > tc) {
282             SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time-tc, NULL);
283             return 0;
284         }
285
286         disp = timer->disp;
287         IDispatch_AddRef(disp);
288
289         if(timer->interval) {
290             timer->time += timer->interval;
291             queue_timer(thread_data, timer);
292         }else {
293             release_task_timer(thread_data->thread_hwnd, timer);
294         }
295
296         call_timer_disp(disp);
297
298         IDispatch_Release(disp);
299     }while(!list_empty(&thread_data->timer_list));
300
301     KillTimer(thread_data->thread_hwnd, TIMER_ID);
302     return 0;
303 }
304
305 static LRESULT WINAPI hidden_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
306 {
307     switch(msg) {
308     case WM_PROCESSTASK:
309         while(1) {
310             task_t *task = pop_task();
311             if(!task)
312                 break;
313
314             task->proc(task);
315             task->destr(task);
316         }
317
318         return 0;
319     case WM_TIMER:
320         return process_timer();
321     }
322
323     if(msg > WM_USER)
324         FIXME("(%p %d %lx %lx)\n", hwnd, msg, wParam, lParam);
325
326     return DefWindowProcW(hwnd, msg, wParam, lParam);
327 }
328
329 static HWND create_thread_hwnd(void)
330 {
331     static ATOM hidden_wnd_class = 0;
332     static const WCHAR wszInternetExplorer_Hidden[] = {'I','n','t','e','r','n','e','t',
333             ' ','E','x','p','l','o','r','e','r','_','H','i','d','d','e','n',0};
334
335     if(!hidden_wnd_class) {
336         WNDCLASSEXW wndclass = {
337             sizeof(WNDCLASSEXW), 0,
338             hidden_proc,
339             0, 0, hInst, NULL, NULL, NULL, NULL,
340             wszInternetExplorer_Hidden,
341             NULL
342         };
343
344         hidden_wnd_class = RegisterClassExW(&wndclass);
345     }
346
347     return CreateWindowExW(0, wszInternetExplorer_Hidden, NULL, WS_POPUP,
348                            0, 0, 0, 0, NULL, NULL, hInst, NULL);
349 }
350
351 HWND get_thread_hwnd(void)
352 {
353     thread_data_t *thread_data;
354
355     thread_data = get_thread_data(TRUE);
356     if(!thread_data)
357         return NULL;
358
359     if(!thread_data->thread_hwnd)
360         thread_data->thread_hwnd = create_thread_hwnd();
361
362     return thread_data->thread_hwnd;
363 }
364
365 thread_data_t *get_thread_data(BOOL create)
366 {
367     thread_data_t *thread_data;
368
369     if(mshtml_tls == TLS_OUT_OF_INDEXES) {
370         DWORD tls;
371
372         if(!create)
373             return NULL;
374
375         tls = TlsAlloc();
376         if(tls == TLS_OUT_OF_INDEXES)
377             return NULL;
378
379         tls = InterlockedCompareExchange((LONG*)&mshtml_tls, tls, TLS_OUT_OF_INDEXES);
380         if(tls != mshtml_tls)
381             TlsFree(tls);
382     }
383
384     thread_data = TlsGetValue(mshtml_tls);
385     if(!thread_data && create) {
386         thread_data = heap_alloc_zero(sizeof(thread_data_t));
387         if(!thread_data)
388             return NULL;
389
390         TlsSetValue(mshtml_tls, thread_data);
391         list_init(&thread_data->timer_list);
392     }
393
394     return thread_data;
395 }