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