kernel32: Add a structure to store all the information about an executable.
[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, task_proc_t proc, LONG magic)
53 {
54     thread_data_t *thread_data = get_thread_data(TRUE);
55
56     task->target_magic = magic;
57     task->proc = proc;
58     task->next = NULL;
59
60     if(thread_data->task_queue_tail)
61         thread_data->task_queue_tail->next = task;
62     else
63         thread_data->task_queue_head = task;
64
65     thread_data->task_queue_tail = task;
66
67     PostMessageW(thread_data->thread_hwnd, WM_PROCESSTASK, 0, 0);
68 }
69
70 static task_t *pop_task(void)
71 {
72     thread_data_t *thread_data = get_thread_data(TRUE);
73     task_t *task = thread_data->task_queue_head;
74
75     if(!task)
76         return NULL;
77
78     thread_data->task_queue_head = task->next;
79     if(!thread_data->task_queue_head)
80         thread_data->task_queue_tail = NULL;
81
82     return task;
83 }
84
85 static void release_task_timer(HWND thread_hwnd, task_timer_t *timer)
86 {
87     list_remove(&timer->entry);
88
89     IDispatch_Release(timer->disp);
90
91     heap_free(timer);
92 }
93
94 void remove_target_tasks(LONG target)
95 {
96     thread_data_t *thread_data = get_thread_data(FALSE);
97     struct list *liter, *ltmp;
98     task_timer_t *timer;
99     task_t *iter, *tmp;
100
101     if(!thread_data)
102         return;
103
104     LIST_FOR_EACH_SAFE(liter, ltmp, &thread_data->timer_list) {
105         timer = LIST_ENTRY(liter, task_timer_t, entry);
106         if(timer->doc->task_magic == target)
107             release_task_timer(thread_data->thread_hwnd, timer);
108     }
109
110     if(!list_empty(&thread_data->timer_list)) {
111         timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
112         SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time - GetTickCount(), NULL);
113     }
114
115     while(thread_data->task_queue_head
116           && thread_data->task_queue_head->target_magic == target)
117         pop_task();
118
119     for(iter = thread_data->task_queue_head; iter; iter = iter->next) {
120         while(iter->next && iter->next->target_magic == target) {
121             tmp = iter->next;
122             iter->next = tmp->next;
123             heap_free(tmp);
124         }
125
126         if(!iter->next)
127             thread_data->task_queue_tail = iter;
128     }
129 }
130
131 LONG get_task_target_magic(void)
132 {
133     static LONG magic = 0x10000000;
134     return InterlockedIncrement(&magic);
135 }
136
137 static BOOL queue_timer(thread_data_t *thread_data, task_timer_t *timer)
138 {
139     task_timer_t *iter;
140
141     list_remove(&timer->entry);
142
143     if(list_empty(&thread_data->timer_list)
144        || LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry)->time > timer->time) {
145
146         list_add_head(&thread_data->timer_list, &timer->entry);
147         return TRUE;
148     }
149
150     LIST_FOR_EACH_ENTRY(iter, &thread_data->timer_list, task_timer_t, entry) {
151         if(iter->time > timer->time) {
152             list_add_tail(&iter->entry, &timer->entry);
153             return FALSE;
154         }
155     }
156
157     list_add_tail(&thread_data->timer_list, &timer->entry);
158     return FALSE;
159 }
160
161 DWORD set_task_timer(HTMLDocument *doc, DWORD msec, BOOL interval, IDispatch *disp)
162 {
163     thread_data_t *thread_data = get_thread_data(TRUE);
164     task_timer_t *timer;
165     DWORD tc = GetTickCount();
166
167     static DWORD id_cnt = 0x20000000;
168
169     timer = heap_alloc(sizeof(task_timer_t));
170     timer->id = id_cnt++;
171     timer->doc = doc;
172     timer->time = tc + msec;
173     timer->interval = interval ? msec : 0;
174     list_init(&timer->entry);
175
176     IDispatch_AddRef(disp);
177     timer->disp = disp;
178
179     if(queue_timer(thread_data, timer))
180         SetTimer(thread_data->thread_hwnd, TIMER_ID, msec, NULL);
181
182     return timer->id;
183 }
184
185 HRESULT clear_task_timer(HTMLDocument *doc, BOOL interval, DWORD id)
186 {
187     thread_data_t *thread_data = get_thread_data(FALSE);
188     task_timer_t *iter;
189
190     if(!thread_data)
191         return S_OK;
192
193     LIST_FOR_EACH_ENTRY(iter, &thread_data->timer_list, task_timer_t, entry) {
194         if(iter->id == id && iter->doc == doc && (iter->interval == 0) == !interval) {
195             release_task_timer(thread_data->thread_hwnd, iter);
196             return S_OK;
197         }
198     }
199
200     WARN("timet not found\n");
201     return S_OK;
202 }
203
204 /* Calls undocumented 69 cmd of CGID_Explorer */
205 static void call_explorer_69(HTMLDocumentObj *doc)
206 {
207     IOleCommandTarget *olecmd;
208     VARIANT var;
209     HRESULT hres;
210
211     if(!doc->client)
212         return;
213
214     hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
215     if(FAILED(hres))
216         return;
217
218     VariantInit(&var);
219     hres = IOleCommandTarget_Exec(olecmd, &CGID_Explorer, 69, 0, NULL, &var);
220     IOleCommandTarget_Release(olecmd);
221     if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
222         FIXME("handle result\n");
223 }
224
225 void parse_complete(HTMLDocumentObj *doc)
226 {
227     IOleCommandTarget *olecmd = NULL;
228
229     TRACE("(%p)\n", doc);
230
231     if(doc->usermode == EDITMODE)
232         init_editor(&doc->basedoc);
233
234     call_explorer_69(doc);
235     call_property_onchanged(&doc->basedoc.cp_propnotif, 1005);
236     call_explorer_69(doc);
237
238     /* FIXME: IE7 calls EnableModelless(TRUE), EnableModelless(FALSE) and sets interactive state here */
239
240     doc->readystate = READYSTATE_INTERACTIVE;
241     call_property_onchanged(&doc->basedoc.cp_propnotif, DISPID_READYSTATE);
242
243     if(doc->client)
244         IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
245
246     if(olecmd) {
247         VARIANT state, progress;
248
249         V_VT(&progress) = VT_I4;
250         V_I4(&progress) = 0;
251         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS, OLECMDEXECOPT_DONTPROMPTUSER,
252                                &progress, NULL);
253
254         V_VT(&state) = VT_I4;
255         V_I4(&state) = 0;
256         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETDOWNLOADSTATE, OLECMDEXECOPT_DONTPROMPTUSER,
257                                &state, NULL);
258
259         IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 103, 0, NULL, NULL);
260         IOleCommandTarget_Exec(olecmd, &CGID_MSHTML, IDM_PARSECOMPLETE, 0, NULL, NULL);
261         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_HTTPEQUIV_DONE, 0, NULL, NULL);
262
263         IOleCommandTarget_Release(olecmd);
264     }
265
266     doc->readystate = READYSTATE_COMPLETE;
267     call_property_onchanged(&doc->basedoc.cp_propnotif, DISPID_READYSTATE);
268
269     if(doc->frame) {
270         static const WCHAR wszDone[] = {'D','o','n','e',0};
271         IOleInPlaceFrame_SetStatusText(doc->frame, wszDone);
272     }
273
274     update_title(doc);
275 }
276
277 static void call_timer_disp(IDispatch *disp)
278 {
279     DISPPARAMS dp = {NULL, NULL, 0, 0};
280     EXCEPINFO ei;
281     VARIANT res;
282     HRESULT hres;
283
284     V_VT(&res) = VT_EMPTY;
285     memset(&ei, 0, sizeof(ei));
286
287     TRACE(">>>\n");
288     hres = IDispatch_Invoke(disp, DISPID_VALUE, &IID_NULL, 0, DISPATCH_METHOD, &dp, &res, &ei, NULL);
289     if(hres == S_OK)
290         TRACE("<<<\n");
291     else
292         WARN("<<< %08x\n", hres);
293
294     VariantClear(&res);
295 }
296
297 static LRESULT process_timer(void)
298 {
299     thread_data_t *thread_data = get_thread_data(TRUE);
300     HTMLDocument *doc;
301     IDispatch *disp;
302     DWORD tc;
303     task_timer_t *timer;
304
305     TRACE("\n");
306
307     while(!list_empty(&thread_data->timer_list)) {
308         timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
309
310         tc = GetTickCount();
311         if(timer->time > tc) {
312             SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time-tc, NULL);
313             return 0;
314         }
315
316         doc = timer->doc;
317         disp = timer->disp;
318         IDispatch_AddRef(disp);
319
320         if(timer->interval) {
321             timer->time += timer->interval;
322             queue_timer(thread_data, timer);
323         }else {
324             release_task_timer(thread_data->thread_hwnd, timer);
325         }
326
327         call_timer_disp(disp);
328
329         IDispatch_Release(disp);
330     }
331
332     KillTimer(thread_data->thread_hwnd, TIMER_ID);
333     return 0;
334 }
335
336 static LRESULT WINAPI hidden_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
337 {
338     switch(msg) {
339     case WM_PROCESSTASK:
340         while(1) {
341             task_t *task = pop_task();
342             if(!task)
343                 break;
344
345             task->proc(task);
346             heap_free(task);
347         }
348
349         return 0;
350     case WM_TIMER:
351         return process_timer();
352     }
353
354     if(msg > WM_USER)
355         FIXME("(%p %d %lx %lx)\n", hwnd, msg, wParam, lParam);
356
357     return DefWindowProcW(hwnd, msg, wParam, lParam);
358 }
359
360 static HWND create_thread_hwnd(void)
361 {
362     static ATOM hidden_wnd_class = 0;
363     static const WCHAR wszInternetExplorer_Hidden[] = {'I','n','t','e','r','n','e','t',
364             ' ','E','x','p','l','o','r','e','r','_','H','i','d','d','e','n',0};
365
366     if(!hidden_wnd_class) {
367         WNDCLASSEXW wndclass = {
368             sizeof(WNDCLASSEXW), 0,
369             hidden_proc,
370             0, 0, hInst, NULL, NULL, NULL, NULL,
371             wszInternetExplorer_Hidden,
372             NULL
373         };
374
375         hidden_wnd_class = RegisterClassExW(&wndclass);
376     }
377
378     return CreateWindowExW(0, wszInternetExplorer_Hidden, NULL, WS_POPUP,
379                            0, 0, 0, 0, NULL, NULL, hInst, NULL);
380 }
381
382 HWND get_thread_hwnd(void)
383 {
384     thread_data_t *thread_data = get_thread_data(TRUE);
385
386     if(!thread_data->thread_hwnd)
387         thread_data->thread_hwnd = create_thread_hwnd();
388
389     return thread_data->thread_hwnd;
390 }
391
392 thread_data_t *get_thread_data(BOOL create)
393 {
394     thread_data_t *thread_data;
395
396     if(mshtml_tls == TLS_OUT_OF_INDEXES) {
397         DWORD tls;
398
399         if(!create)
400             return NULL;
401
402         tls = TlsAlloc();
403         if(tls == TLS_OUT_OF_INDEXES)
404             return NULL;
405
406         tls = InterlockedCompareExchange((LONG*)&mshtml_tls, tls, TLS_OUT_OF_INDEXES);
407         if(tls != mshtml_tls)
408             TlsFree(tls);
409     }
410
411     thread_data = TlsGetValue(mshtml_tls);
412     if(!thread_data && create) {
413         thread_data = heap_alloc_zero(sizeof(thread_data_t));
414         TlsSetValue(mshtml_tls, thread_data);
415         list_init(&thread_data->timer_list);
416     }
417
418     return thread_data;
419 }