d3d10: Add ID3D10EffectStringVariableVtbl.
[wine/testsucceed.git] / dlls / mshtml / task.c
blob27065237c3101674f5187134184ece7cfdd68a0d
1 /*
2 * Copyright 2006 Jacek Caban for CodeWeavers
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.
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.
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
19 #include "config.h"
21 #include <stdarg.h>
22 #include <stdio.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "ole2.h"
30 #include "mshtmcid.h"
31 #include "shlguid.h"
33 #include "wine/debug.h"
35 #include "mshtml_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
39 #define WM_PROCESSTASK 0x8008
40 #define TIMER_ID 0x3000
42 typedef struct {
43 HTMLDocument *doc;
44 DWORD id;
45 DWORD time;
46 DWORD interval;
47 IDispatch *disp;
49 struct list entry;
50 } task_timer_t;
52 void push_task(task_t *task, task_proc_t proc, LONG magic)
54 thread_data_t *thread_data = get_thread_data(TRUE);
56 task->target_magic = magic;
57 task->proc = proc;
58 task->next = NULL;
60 if(thread_data->task_queue_tail)
61 thread_data->task_queue_tail->next = task;
62 else
63 thread_data->task_queue_head = task;
65 thread_data->task_queue_tail = task;
67 PostMessageW(thread_data->thread_hwnd, WM_PROCESSTASK, 0, 0);
70 static task_t *pop_task(void)
72 thread_data_t *thread_data = get_thread_data(TRUE);
73 task_t *task = thread_data->task_queue_head;
75 if(!task)
76 return NULL;
78 thread_data->task_queue_head = task->next;
79 if(!thread_data->task_queue_head)
80 thread_data->task_queue_tail = NULL;
82 return task;
85 static void release_task_timer(HWND thread_hwnd, task_timer_t *timer)
87 list_remove(&timer->entry);
89 IDispatch_Release(timer->disp);
91 heap_free(timer);
94 void remove_target_tasks(LONG target)
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;
101 if(!thread_data)
102 return;
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);
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);
115 while(thread_data->task_queue_head
116 && thread_data->task_queue_head->target_magic == target)
117 pop_task();
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);
126 if(!iter->next)
127 thread_data->task_queue_tail = iter;
131 LONG get_task_target_magic(void)
133 static LONG magic = 0x10000000;
134 return InterlockedIncrement(&magic);
137 static BOOL queue_timer(thread_data_t *thread_data, task_timer_t *timer)
139 task_timer_t *iter;
141 list_remove(&timer->entry);
143 if(list_empty(&thread_data->timer_list)
144 || LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry)->time > timer->time) {
146 list_add_head(&thread_data->timer_list, &timer->entry);
147 return TRUE;
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;
157 list_add_tail(&thread_data->timer_list, &timer->entry);
158 return FALSE;
161 DWORD set_task_timer(HTMLDocument *doc, DWORD msec, BOOL interval, IDispatch *disp)
163 thread_data_t *thread_data = get_thread_data(TRUE);
164 task_timer_t *timer;
165 DWORD tc = GetTickCount();
167 static DWORD id_cnt = 0x20000000;
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);
176 IDispatch_AddRef(disp);
177 timer->disp = disp;
179 if(queue_timer(thread_data, timer))
180 SetTimer(thread_data->thread_hwnd, TIMER_ID, msec, NULL);
182 return timer->id;
185 HRESULT clear_task_timer(HTMLDocument *doc, BOOL interval, DWORD id)
187 thread_data_t *thread_data = get_thread_data(FALSE);
188 task_timer_t *iter;
190 if(!thread_data)
191 return S_OK;
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;
200 WARN("timet not found\n");
201 return S_OK;
204 /* Calls undocumented 69 cmd of CGID_Explorer */
205 static void call_explorer_69(HTMLDocumentObj *doc)
207 IOleCommandTarget *olecmd;
208 VARIANT var;
209 HRESULT hres;
211 if(!doc->client)
212 return;
214 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
215 if(FAILED(hres))
216 return;
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");
225 void parse_complete(HTMLDocumentObj *doc)
227 IOleCommandTarget *olecmd = NULL;
229 TRACE("(%p)\n", doc);
231 if(doc->usermode == EDITMODE)
232 init_editor(&doc->basedoc);
234 call_explorer_69(doc);
235 call_property_onchanged(&doc->basedoc.cp_propnotif, 1005);
236 call_explorer_69(doc);
238 /* FIXME: IE7 calls EnableModelless(TRUE), EnableModelless(FALSE) and sets interactive state here */
240 doc->readystate = READYSTATE_INTERACTIVE;
241 call_property_onchanged(&doc->basedoc.cp_propnotif, DISPID_READYSTATE);
243 if(doc->client)
244 IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
246 if(olecmd) {
247 VARIANT state, progress;
249 V_VT(&progress) = VT_I4;
250 V_I4(&progress) = 0;
251 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS, OLECMDEXECOPT_DONTPROMPTUSER,
252 &progress, NULL);
254 V_VT(&state) = VT_I4;
255 V_I4(&state) = 0;
256 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETDOWNLOADSTATE, OLECMDEXECOPT_DONTPROMPTUSER,
257 &state, NULL);
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);
263 IOleCommandTarget_Release(olecmd);
266 doc->readystate = READYSTATE_COMPLETE;
267 call_property_onchanged(&doc->basedoc.cp_propnotif, DISPID_READYSTATE);
269 if(doc->frame) {
270 static const WCHAR wszDone[] = {'D','o','n','e',0};
271 IOleInPlaceFrame_SetStatusText(doc->frame, wszDone);
274 update_title(doc);
277 static void call_timer_disp(IDispatch *disp)
279 DISPPARAMS dp = {NULL, NULL, 0, 0};
280 EXCEPINFO ei;
281 VARIANT res;
282 HRESULT hres;
284 V_VT(&res) = VT_EMPTY;
285 memset(&ei, 0, sizeof(ei));
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);
294 VariantClear(&res);
297 static LRESULT process_timer(void)
299 thread_data_t *thread_data = get_thread_data(TRUE);
300 HTMLDocument *doc;
301 IDispatch *disp;
302 DWORD tc;
303 task_timer_t *timer;
305 TRACE("\n");
307 while(!list_empty(&thread_data->timer_list)) {
308 timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
310 tc = GetTickCount();
311 if(timer->time > tc) {
312 SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time-tc, NULL);
313 return 0;
316 doc = timer->doc;
317 disp = timer->disp;
318 IDispatch_AddRef(disp);
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);
327 call_timer_disp(disp);
329 IDispatch_Release(disp);
332 KillTimer(thread_data->thread_hwnd, TIMER_ID);
333 return 0;
336 static LRESULT WINAPI hidden_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
338 switch(msg) {
339 case WM_PROCESSTASK:
340 while(1) {
341 task_t *task = pop_task();
342 if(!task)
343 break;
345 task->proc(task);
346 heap_free(task);
349 return 0;
350 case WM_TIMER:
351 return process_timer();
354 if(msg > WM_USER)
355 FIXME("(%p %d %lx %lx)\n", hwnd, msg, wParam, lParam);
357 return DefWindowProcW(hwnd, msg, wParam, lParam);
360 static HWND create_thread_hwnd(void)
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};
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
375 hidden_wnd_class = RegisterClassExW(&wndclass);
378 return CreateWindowExW(0, wszInternetExplorer_Hidden, NULL, WS_POPUP,
379 0, 0, 0, 0, NULL, NULL, hInst, NULL);
382 HWND get_thread_hwnd(void)
384 thread_data_t *thread_data = get_thread_data(TRUE);
386 if(!thread_data->thread_hwnd)
387 thread_data->thread_hwnd = create_thread_hwnd();
389 return thread_data->thread_hwnd;
392 thread_data_t *get_thread_data(BOOL create)
394 thread_data_t *thread_data;
396 if(mshtml_tls == TLS_OUT_OF_INDEXES) {
397 DWORD tls;
399 if(!create)
400 return NULL;
402 tls = TlsAlloc();
403 if(tls == TLS_OUT_OF_INDEXES)
404 return NULL;
406 tls = InterlockedCompareExchange((LONG*)&mshtml_tls, tls, TLS_OUT_OF_INDEXES);
407 if(tls != mshtml_tls)
408 TlsFree(tls);
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);
418 return thread_data;