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
29 #include "wine/debug.h"
31 #include "mshtml_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
35 #define WM_PROCESSTASK 0x8008
36 #define TIMER_ID 0x3000
39 HTMLInnerWindow
*window
;
49 HRESULT
push_task(task_t
*task
, task_proc_t proc
, task_proc_t destr
, LONG magic
)
51 thread_data_t
*thread_data
;
53 thread_data
= get_thread_data(TRUE
);
60 task
->target_magic
= magic
;
64 list_add_tail(&thread_data
->task_list
, &task
->entry
);
66 PostMessageW(thread_data
->thread_hwnd
, WM_PROCESSTASK
, 0, 0);
70 static void release_event_task(event_task_t
*task
)
73 IHTMLWindow2_Release(&task
->window
->base
.IHTMLWindow2_iface
);
77 HRESULT
push_event_task(event_task_t
*task
, HTMLInnerWindow
*window
, event_task_proc_t proc
, event_task_proc_t destr
, LONG magic
)
79 thread_data_t
*thread_data
;
81 task
->target_magic
= magic
;
84 task
->window
= window
;
85 IHTMLWindow2_AddRef(&window
->base
.IHTMLWindow2_iface
);
87 thread_data
= get_thread_data(TRUE
);
89 release_event_task(task
);
93 list_add_tail(&thread_data
->event_task_list
, &task
->entry
);
95 PostMessageW(thread_data
->thread_hwnd
, WM_PROCESSTASK
, 0, 0);
99 static void release_task_timer(HWND thread_hwnd
, task_timer_t
*timer
)
101 list_remove(&timer
->entry
);
103 IDispatch_Release(timer
->disp
);
108 void remove_target_tasks(LONG target
)
110 thread_data_t
*thread_data
= get_thread_data(FALSE
);
111 struct list
*liter
, *ltmp
;
118 LIST_FOR_EACH_SAFE(liter
, ltmp
, &thread_data
->timer_list
) {
119 timer
= LIST_ENTRY(liter
, task_timer_t
, entry
);
120 if(timer
->window
->task_magic
== target
)
121 release_task_timer(thread_data
->thread_hwnd
, timer
);
124 if(!list_empty(&thread_data
->timer_list
)) {
125 DWORD tc
= GetTickCount();
127 timer
= LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
);
128 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, max( (int)(timer
->time
- tc
), 0 ), NULL
);
131 LIST_FOR_EACH_SAFE(liter
, ltmp
, &thread_data
->task_list
) {
132 task
= LIST_ENTRY(liter
, task_t
, entry
);
133 if(task
->target_magic
== target
) {
134 list_remove(&task
->entry
);
140 LIST_FOR_EACH_SAFE(liter
, ltmp
, &thread_data
->event_task_list
) {
141 event_task_t
*task
= LIST_ENTRY(liter
, event_task_t
, entry
);
142 if(task
->target_magic
== target
) {
143 list_remove(&task
->entry
);
144 release_event_task(task
);
149 LONG
get_task_target_magic(void)
151 static LONG magic
= 0x10000000;
152 return InterlockedIncrement(&magic
);
155 static BOOL
queue_timer(thread_data_t
*thread_data
, task_timer_t
*timer
)
159 list_remove(&timer
->entry
);
161 if(list_empty(&thread_data
->timer_list
)
162 || LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
)->time
> timer
->time
) {
164 list_add_head(&thread_data
->timer_list
, &timer
->entry
);
168 LIST_FOR_EACH_ENTRY(iter
, &thread_data
->timer_list
, task_timer_t
, entry
) {
169 if(iter
->time
> timer
->time
) {
170 list_add_tail(&iter
->entry
, &timer
->entry
);
175 list_add_tail(&thread_data
->timer_list
, &timer
->entry
);
179 HRESULT
set_task_timer(HTMLInnerWindow
*window
, LONG msec
, enum timer_type timer_type
, IDispatch
*disp
, LONG
*id
)
181 thread_data_t
*thread_data
;
183 DWORD tc
= GetTickCount();
185 static DWORD id_cnt
= 0x20000000;
187 thread_data
= get_thread_data(TRUE
);
189 return E_OUTOFMEMORY
;
191 timer
= malloc(sizeof(task_timer_t
));
193 return E_OUTOFMEMORY
;
198 timer
->id
= id_cnt
++;
199 timer
->window
= window
;
200 timer
->time
= tc
+ msec
;
201 timer
->interval
= timer_type
== TIMER_INTERVAL
? msec
: 0;
202 timer
->type
= timer_type
;
203 list_init(&timer
->entry
);
205 IDispatch_AddRef(disp
);
208 if(queue_timer(thread_data
, timer
))
209 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, msec
, NULL
);
215 HRESULT
clear_task_timer(HTMLInnerWindow
*window
, DWORD id
)
217 thread_data_t
*thread_data
= get_thread_data(FALSE
);
223 LIST_FOR_EACH_ENTRY(iter
, &thread_data
->timer_list
, task_timer_t
, entry
) {
224 if(iter
->id
== id
&& iter
->window
== window
) {
225 if(iter
->type
!= TIMER_ANIMATION_FRAME
)
226 release_task_timer(thread_data
->thread_hwnd
, iter
);
231 WARN("timet not found\n");
235 HRESULT
clear_animation_timer(HTMLInnerWindow
*window
, DWORD id
)
237 thread_data_t
*thread_data
= get_thread_data(FALSE
);
243 LIST_FOR_EACH_ENTRY(iter
, &thread_data
->timer_list
, task_timer_t
, entry
) {
244 if(iter
->id
== id
&& iter
->window
== window
) {
245 if(iter
->type
== TIMER_ANIMATION_FRAME
)
246 release_task_timer(thread_data
->thread_hwnd
, iter
);
251 WARN("timer not found\n");
255 static const char *debugstr_timer_type(enum timer_type type
)
258 case TIMER_TIMEOUT
: return "timeout";
259 case TIMER_INTERVAL
: return "interval";
260 case TIMER_ANIMATION_FRAME
: return "animation-frame";
265 static void call_timer_disp(IDispatch
*disp
, enum timer_type timer_type
)
267 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
273 V_VT(&res
) = VT_EMPTY
;
274 memset(&ei
, 0, sizeof(ei
));
276 if(timer_type
== TIMER_ANIMATION_FRAME
) {
278 dp
.rgvarg
= ×tamp
;
279 V_VT(×tamp
) = VT_R8
;
280 V_R8(×tamp
) = get_time_stamp();
283 TRACE("%p %s >>>\n", disp
, debugstr_timer_type(timer_type
));
284 hres
= IDispatch_Invoke(disp
, DISPID_VALUE
, &IID_NULL
, 0, DISPATCH_METHOD
, &dp
, &res
, &ei
, NULL
);
286 TRACE("%p %s <<<\n", disp
, debugstr_timer_type(timer_type
));
288 WARN("%p %s <<< %08lx\n", disp
, debugstr_timer_type(timer_type
), hres
);
293 static LRESULT
process_timer(void)
295 thread_data_t
*thread_data
;
296 enum timer_type timer_type
;
299 task_timer_t
*timer
=NULL
, *last_timer
;
303 thread_data
= get_thread_data(FALSE
);
304 assert(thread_data
!= NULL
);
306 if(list_empty(&thread_data
->timer_list
)) {
307 KillTimer(thread_data
->thread_hwnd
, TIMER_ID
);
311 last_timer
= LIST_ENTRY(list_tail(&thread_data
->timer_list
), task_timer_t
, entry
);
314 if(timer
== last_timer
) {
315 timer
= LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
);
316 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, timer
->time
>tc
? timer
->time
-tc
: 0, NULL
);
320 timer
= LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
);
322 if(timer
->time
> tc
) {
323 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, timer
->time
-tc
, NULL
);
328 IDispatch_AddRef(disp
);
329 timer_type
= timer
->type
;
331 if(timer
->interval
) {
332 timer
->time
+= timer
->interval
;
333 queue_timer(thread_data
, timer
);
335 release_task_timer(thread_data
->thread_hwnd
, timer
);
338 call_timer_disp(disp
, timer_type
);
340 IDispatch_Release(disp
);
341 }while(!list_empty(&thread_data
->timer_list
));
343 KillTimer(thread_data
->thread_hwnd
, TIMER_ID
);
347 static LRESULT WINAPI
hidden_proc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
349 thread_data_t
*thread_data
;
353 thread_data
= get_thread_data(FALSE
);
358 struct list
*head
= list_head(&thread_data
->task_list
);
361 task_t
*task
= LIST_ENTRY(head
, task_t
, entry
);
362 list_remove(&task
->entry
);
369 head
= list_head(&thread_data
->event_task_list
);
371 event_task_t
*task
= LIST_ENTRY(head
, event_task_t
, entry
);
372 list_remove(&task
->entry
);
374 release_event_task(task
);
382 return process_timer();
386 FIXME("(%p %d %Ix %Ix)\n", hwnd
, msg
, wParam
, lParam
);
388 return DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
391 static HWND
create_thread_hwnd(void)
393 static ATOM hidden_wnd_class
= 0;
395 if(!hidden_wnd_class
) {
396 WNDCLASSEXW wndclass
= {
397 sizeof(WNDCLASSEXW
), 0,
399 0, 0, hInst
, NULL
, NULL
, NULL
, NULL
,
400 L
"Internet Explorer_Hidden",
404 hidden_wnd_class
= RegisterClassExW(&wndclass
);
407 return CreateWindowExW(0, L
"Internet Explorer_Hidden", NULL
, WS_POPUP
,
408 0, 0, 0, 0, NULL
, NULL
, hInst
, NULL
);
411 HWND
get_thread_hwnd(void)
413 thread_data_t
*thread_data
;
415 thread_data
= get_thread_data(TRUE
);
419 if(!thread_data
->thread_hwnd
)
420 thread_data
->thread_hwnd
= create_thread_hwnd();
422 return thread_data
->thread_hwnd
;
425 thread_data_t
*get_thread_data(BOOL create
)
427 thread_data_t
*thread_data
;
429 if(mshtml_tls
== TLS_OUT_OF_INDEXES
) {
436 if(tls
== TLS_OUT_OF_INDEXES
)
439 tls
= InterlockedCompareExchange((LONG
*)&mshtml_tls
, tls
, TLS_OUT_OF_INDEXES
);
440 if(tls
!= mshtml_tls
)
444 thread_data
= TlsGetValue(mshtml_tls
);
445 if(!thread_data
&& create
) {
446 thread_data
= calloc(1, sizeof(thread_data_t
));
450 TlsSetValue(mshtml_tls
, thread_data
);
451 list_init(&thread_data
->task_list
);
452 list_init(&thread_data
->event_task_list
);
453 list_init(&thread_data
->timer_list
);
454 wine_rb_init(&thread_data
->session_storage_map
, session_storage_map_cmp
);
460 ULONGLONG
get_time_stamp(void)
464 /* 1601 to 1970 is 369 years plus 89 leap days */
465 const ULONGLONG time_epoch
= (ULONGLONG
)(369 * 365 + 89) * 86400 * 1000;
467 GetSystemTimeAsFileTime(&time
);
468 return (((ULONGLONG
)time
.dwHighDateTime
<< 32) + time
.dwLowDateTime
) / 10000 - time_epoch
;