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
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
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;
60 if(thread_data->task_queue_tail)
61 thread_data->task_queue_tail->next = task;
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;
78 thread_data->task_queue_head = task->next;
79 if(!thread_data->task_queue_head)
80 thread_data->task_queue_tail = NULL;
85 static void release_task_timer(HWND thread_hwnd, task_timer_t *timer)
87 list_remove(&timer->entry);
89 IDispatch_Release(timer->disp);
94 void remove_target_tasks(LONG target)
96 thread_data_t *thread_data = get_thread_data(FALSE);
97 struct list *liter, *ltmp;
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)
119 for(iter = thread_data->task_queue_head; iter; iter = iter->next) {
120 while(iter->next && iter->next->target_magic == target) {
122 iter->next = tmp->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)
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);
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);
157 list_add_tail(&thread_data->timer_list, &timer->entry);
161 DWORD set_task_timer(HTMLDocument *doc, DWORD msec, BOOL interval, IDispatch *disp)
163 thread_data_t *thread_data = get_thread_data(TRUE);
165 DWORD tc = GetTickCount();
167 static DWORD id_cnt = 0x20000000;
169 timer = heap_alloc(sizeof(task_timer_t));
170 timer->id = id_cnt++;
172 timer->time = tc + msec;
173 timer->interval = interval ? msec : 0;
174 list_init(&timer->entry);
176 IDispatch_AddRef(disp);
179 if(queue_timer(thread_data, timer))
180 SetTimer(thread_data->thread_hwnd, TIMER_ID, msec, NULL);
185 HRESULT clear_task_timer(HTMLDocument *doc, BOOL interval, DWORD id)
187 thread_data_t *thread_data = get_thread_data(FALSE);
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);
200 WARN("timet not found\n");
204 /* Calls undocumented 69 cmd of CGID_Explorer */
205 static void call_explorer_69(HTMLDocumentObj *doc)
207 IOleCommandTarget *olecmd;
214 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
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);
244 IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
247 VARIANT state, progress;
249 V_VT(&progress) = VT_I4;
251 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS, OLECMDEXECOPT_DONTPROMPTUSER,
254 V_VT(&state) = VT_I4;
256 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETDOWNLOADSTATE, OLECMDEXECOPT_DONTPROMPTUSER,
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);
270 static const WCHAR wszDone[] = {'D','o','n','e',0};
271 IOleInPlaceFrame_SetStatusText(doc->frame, wszDone);
277 static void call_timer_disp(IDispatch *disp)
279 DISPPARAMS dp = {NULL, NULL, 0, 0};
284 V_VT(&res) = VT_EMPTY;
285 memset(&ei, 0, sizeof(ei));
288 hres = IDispatch_Invoke(disp, DISPID_VALUE, &IID_NULL, 0, DISPATCH_METHOD, &dp, &res, &ei, NULL);
292 WARN("<<< %08x\n", hres);
297 static LRESULT process_timer(void)
299 thread_data_t *thread_data = get_thread_data(TRUE);
307 while(!list_empty(&thread_data->timer_list)) {
308 timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
311 if(timer->time > tc) {
312 SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time-tc, NULL);
318 IDispatch_AddRef(disp);
320 if(timer->interval) {
321 timer->time += timer->interval;
322 queue_timer(thread_data, timer);
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);
336 static LRESULT WINAPI hidden_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
341 task_t *task = pop_task();
351 return process_timer();
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,
370 0, 0, hInst, NULL, NULL, NULL, NULL,
371 wszInternetExplorer_Hidden,
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) {
403 if(tls == TLS_OUT_OF_INDEXES)
406 tls = InterlockedCompareExchange((LONG*)&mshtml_tls, tls, TLS_OUT_OF_INDEXES);
407 if(tls != mshtml_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);