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