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)
54 thread_data_t *thread_data = get_thread_data(TRUE);
56 if(thread_data->task_queue_tail)
57 thread_data->task_queue_tail->next = task;
59 thread_data->task_queue_head = task;
61 thread_data->task_queue_tail = task;
63 PostMessageW(thread_data->thread_hwnd, WM_PROCESSTASK, 0, 0);
66 static task_t *pop_task(void)
68 thread_data_t *thread_data = get_thread_data(TRUE);
69 task_t *task = thread_data->task_queue_head;
74 thread_data->task_queue_head = task->next;
75 if(!thread_data->task_queue_head)
76 thread_data->task_queue_tail = NULL;
81 static void release_task_timer(HWND thread_hwnd, task_timer_t *timer)
83 list_remove(&timer->entry);
85 IDispatch_Release(timer->disp);
90 void remove_doc_tasks(const HTMLDocument *doc)
92 thread_data_t *thread_data = get_thread_data(FALSE);
93 struct list *liter, *ltmp;
97 LIST_FOR_EACH_SAFE(liter, ltmp, &thread_data->timer_list) {
98 timer = LIST_ENTRY(liter, task_timer_t, entry);
100 release_task_timer(thread_data->thread_hwnd, timer);
103 if(!list_empty(&thread_data->timer_list)) {
104 timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
105 SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time - GetTickCount(), NULL);
111 while(thread_data->task_queue_head
112 && thread_data->task_queue_head->doc == doc)
115 for(iter = thread_data->task_queue_head; iter; iter = iter->next) {
116 while(iter->next && iter->next->doc == doc) {
118 iter->next = tmp->next;
123 thread_data->task_queue_tail = iter;
127 static BOOL queue_timer(thread_data_t *thread_data, task_timer_t *timer)
131 list_remove(&timer->entry);
133 if(list_empty(&thread_data->timer_list)
134 || LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry)->time > timer->time) {
136 list_add_head(&thread_data->timer_list, &timer->entry);
140 LIST_FOR_EACH_ENTRY(iter, &thread_data->timer_list, task_timer_t, entry) {
141 if(iter->time > timer->time) {
142 list_add_tail(&iter->entry, &timer->entry);
147 list_add_tail(&thread_data->timer_list, &timer->entry);
151 DWORD set_task_timer(HTMLDocument *doc, DWORD msec, BOOL interval, IDispatch *disp)
153 thread_data_t *thread_data = get_thread_data(TRUE);
155 DWORD tc = GetTickCount();
157 static DWORD id_cnt = 0x20000000;
159 timer = heap_alloc(sizeof(task_timer_t));
160 timer->id = id_cnt++;
162 timer->time = tc + msec;
163 timer->interval = interval ? msec : 0;
164 list_init(&timer->entry);
166 IDispatch_AddRef(disp);
169 if(queue_timer(thread_data, timer))
170 SetTimer(thread_data->thread_hwnd, TIMER_ID, msec, NULL);
175 HRESULT clear_task_timer(HTMLDocument *doc, BOOL interval, DWORD id)
177 thread_data_t *thread_data = get_thread_data(FALSE);
183 LIST_FOR_EACH_ENTRY(iter, &thread_data->timer_list, task_timer_t, entry) {
184 if(iter->id == id && iter->doc == doc && (iter->interval == 0) == !interval) {
185 release_task_timer(thread_data->thread_hwnd, iter);
190 WARN("timet not found\n");
194 static void set_downloading(HTMLDocument *doc)
196 IOleCommandTarget *olecmd;
199 TRACE("(%p)\n", doc);
202 IOleInPlaceFrame_SetStatusText(doc->frame, NULL /* FIXME */);
207 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
208 if(SUCCEEDED(hres)) {
214 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETDOWNLOADSTATE, OLECMDEXECOPT_DONTPROMPTUSER,
216 IOleCommandTarget_Release(olecmd);
220 IDropTarget *drop_target = NULL;
222 hres = IDocHostUIHandler_GetDropTarget(doc->hostui, NULL /* FIXME */, &drop_target);
224 FIXME("Use IDropTarget\n");
225 IDropTarget_Release(drop_target);
230 /* Calls undocumented 69 cmd of CGID_Explorer */
231 static void call_explorer_69(HTMLDocument *doc)
233 IOleCommandTarget *olecmd;
240 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
245 hres = IOleCommandTarget_Exec(olecmd, &CGID_Explorer, 69, 0, NULL, &var);
246 IOleCommandTarget_Release(olecmd);
247 if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
248 FIXME("handle result\n");
251 static void set_parsecomplete(HTMLDocument *doc)
253 IOleCommandTarget *olecmd = NULL;
255 TRACE("(%p)\n", doc);
257 if(doc->usermode == EDITMODE)
260 call_explorer_69(doc);
261 call_property_onchanged(&doc->cp_propnotif, 1005);
262 call_explorer_69(doc);
264 doc->readystate = READYSTATE_INTERACTIVE;
265 call_property_onchanged(&doc->cp_propnotif, DISPID_READYSTATE);
268 IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
271 VARIANT state, progress;
273 V_VT(&progress) = VT_I4;
275 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS, OLECMDEXECOPT_DONTPROMPTUSER,
278 V_VT(&state) = VT_I4;
280 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETDOWNLOADSTATE, OLECMDEXECOPT_DONTPROMPTUSER,
283 IOleCommandTarget_Exec(olecmd, &CGID_ShellDocView, 103, 0, NULL, NULL);
284 IOleCommandTarget_Exec(olecmd, &CGID_MSHTML, IDM_PARSECOMPLETE, 0, NULL, NULL);
285 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_HTTPEQUIV_DONE, 0, NULL, NULL);
287 IOleCommandTarget_Release(olecmd);
290 doc->readystate = READYSTATE_COMPLETE;
291 call_property_onchanged(&doc->cp_propnotif, DISPID_READYSTATE);
294 static const WCHAR wszDone[] = {'D','o','n','e',0};
295 IOleInPlaceFrame_SetStatusText(doc->frame, wszDone);
301 static void set_progress(HTMLDocument *doc)
303 IOleCommandTarget *olecmd = NULL;
306 TRACE("(%p)\n", doc);
309 IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
312 VARIANT progress_max, progress;
314 V_VT(&progress_max) = VT_I4;
315 V_I4(&progress_max) = 0; /* FIXME */
316 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSMAX, OLECMDEXECOPT_DONTPROMPTUSER,
317 &progress_max, NULL);
319 V_VT(&progress) = VT_I4;
320 V_I4(&progress) = 0; /* FIXME */
321 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS, OLECMDEXECOPT_DONTPROMPTUSER,
325 if(doc->usermode == EDITMODE && doc->hostui) {
326 DOCHOSTUIINFO hostinfo;
328 memset(&hostinfo, 0, sizeof(DOCHOSTUIINFO));
329 hostinfo.cbSize = sizeof(DOCHOSTUIINFO);
330 hres = IDocHostUIHandler_GetHostInfo(doc->hostui, &hostinfo);
332 /* FIXME: use hostinfo */
333 TRACE("hostinfo = {%u %08x %08x %s %s}\n",
334 hostinfo.cbSize, hostinfo.dwFlags, hostinfo.dwDoubleClick,
335 debugstr_w(hostinfo.pchHostCss), debugstr_w(hostinfo.pchHostNS));
339 static void task_start_binding(HTMLDocument *doc, BSCallback *bscallback)
342 start_binding(doc, bscallback, NULL);
343 IUnknown_Release((IUnknown*)bscallback);
346 static void process_task(task_t *task)
348 switch(task->task_id) {
349 case TASK_SETDOWNLOADSTATE:
350 set_downloading(task->doc);
352 case TASK_PARSECOMPLETE:
353 set_parsecomplete(task->doc);
355 case TASK_SETPROGRESS:
356 set_progress(task->doc);
358 case TASK_START_BINDING:
359 task_start_binding(task->doc, (BSCallback*)task->bscallback);
362 ERR("Wrong task_id %d\n", task->task_id);
366 static LRESULT process_timer(void)
368 thread_data_t *thread_data = get_thread_data(TRUE);
376 while(!list_empty(&thread_data->timer_list)) {
377 timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
380 if(timer->time > tc) {
381 SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time-tc, NULL);
387 IDispatch_AddRef(disp);
389 if(timer->interval) {
390 timer->time += timer->interval;
391 queue_timer(thread_data, timer);
393 release_task_timer(thread_data->thread_hwnd, timer);
396 call_disp_func(doc, disp);
398 IDispatch_Release(disp);
401 KillTimer(thread_data->thread_hwnd, TIMER_ID);
405 static LRESULT WINAPI hidden_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
410 task_t *task = pop_task();
420 return process_timer();
424 FIXME("(%p %d %lx %lx)\n", hwnd, msg, wParam, lParam);
426 return DefWindowProcW(hwnd, msg, wParam, lParam);
429 static HWND create_thread_hwnd(void)
431 static ATOM hidden_wnd_class = 0;
432 static const WCHAR wszInternetExplorer_Hidden[] = {'I','n','t','e','r','n','e','t',
433 ' ','E','x','p','l','o','r','e','r','_','H','i','d','d','e','n',0};
435 if(!hidden_wnd_class) {
436 WNDCLASSEXW wndclass = {
437 sizeof(WNDCLASSEXW), 0,
439 0, 0, hInst, NULL, NULL, NULL, NULL,
440 wszInternetExplorer_Hidden,
444 hidden_wnd_class = RegisterClassExW(&wndclass);
447 return CreateWindowExW(0, wszInternetExplorer_Hidden, NULL, WS_POPUP,
448 0, 0, 0, 0, NULL, NULL, hInst, NULL);
451 HWND get_thread_hwnd(void)
453 thread_data_t *thread_data = get_thread_data(TRUE);
455 if(!thread_data->thread_hwnd)
456 thread_data->thread_hwnd = create_thread_hwnd();
458 return thread_data->thread_hwnd;
461 thread_data_t *get_thread_data(BOOL create)
463 thread_data_t *thread_data;
467 mshtml_tls = TlsAlloc();
472 thread_data = TlsGetValue(mshtml_tls);
473 if(!thread_data && create) {
474 thread_data = heap_alloc_zero(sizeof(thread_data_t));
475 TlsSetValue(mshtml_tls, thread_data);
476 list_init(&thread_data->timer_list);