mshtml: Added get_readyState implementation.
[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
32 #include "wine/debug.h"
33
34 #include "mshtml_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37
38 #define WM_PROCESSTASK 0x8008
39
40 void push_task(task_t *task)
41 {
42     thread_data_t *thread_data = get_thread_data(TRUE);
43
44     if(thread_data->task_queue_tail)
45         thread_data->task_queue_tail->next = task;
46     else
47         thread_data->task_queue_head = task;
48
49     thread_data->task_queue_tail = task;
50
51     PostMessageW(thread_data->thread_hwnd, WM_PROCESSTASK, 0, 0);
52 }
53
54 static task_t *pop_task(void)
55 {
56     thread_data_t *thread_data = get_thread_data(TRUE);
57     task_t *task = thread_data->task_queue_head;
58
59     if(!task)
60         return NULL;
61
62     thread_data->task_queue_head = task->next;
63     if(!thread_data->task_queue_head)
64         thread_data->task_queue_tail = NULL;
65
66     return task;
67 }
68
69 void remove_doc_tasks(HTMLDocument *doc)
70 {
71     thread_data_t *thread_data = get_thread_data(FALSE);
72     task_t *iter, *tmp;
73
74     while(thread_data->task_queue_head
75           && thread_data->task_queue_head->doc == doc)
76         pop_task();
77
78     for(iter = thread_data->task_queue_head; iter; iter = iter->next) {
79         while(iter->next && iter->next->doc == doc) {
80             tmp = iter->next;
81             iter->next = tmp->next;
82             mshtml_free(tmp);
83         }
84
85         if(!iter->next)
86             thread_data->task_queue_tail = iter;
87     }
88 }
89
90 static void set_downloading(HTMLDocument *doc)
91 {
92     IOleCommandTarget *olecmd;
93     HRESULT hres;
94
95     TRACE("(%p)\n", doc);
96
97     if(doc->frame) 
98         IOleInPlaceFrame_SetStatusText(doc->frame, NULL /* FIXME */);
99
100     if(!doc->client)
101         return;
102
103     hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
104     if(SUCCEEDED(hres)) {
105         VARIANT var;
106
107         V_VT(&var) = VT_I4;
108         V_I4(&var) = 1;
109
110         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETDOWNLOADSTATE, OLECMDEXECOPT_DONTPROMPTUSER,
111                                &var, NULL);
112         IOleCommandTarget_Release(olecmd);
113     }
114
115     if(doc->hostui) {
116         IDropTarget *drop_target = NULL;
117
118         hres = IDocHostUIHandler_GetDropTarget(doc->hostui, NULL /* FIXME */, &drop_target);
119         if(drop_target) {
120             FIXME("Use IDropTarget\n");
121             IDropTarget_Release(drop_target);
122         }
123     }
124 }
125
126 static void set_parsecomplete(HTMLDocument *doc)
127 {
128     IOleCommandTarget *olecmd = NULL;
129
130     TRACE("(%p)\n", doc);
131
132     call_property_onchanged(doc->cp_propnotif, 1005);
133
134     doc->readystate = READYSTATE_INTERACTIVE;
135     call_property_onchanged(doc->cp_propnotif, DISPID_READYSTATE);
136
137     if(doc->client)
138         IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
139
140     if(olecmd) {
141         VARIANT state, progress;
142
143         V_VT(&progress) = VT_I4;
144         V_I4(&progress) = 0;
145         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS, OLECMDEXECOPT_DONTPROMPTUSER,
146                                &progress, NULL);
147
148         V_VT(&state) = VT_I4;
149         V_I4(&state) = 0;
150         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETDOWNLOADSTATE, OLECMDEXECOPT_DONTPROMPTUSER,
151                                &state, NULL);
152
153         IOleCommandTarget_Exec(olecmd, &CGID_MSHTML, IDM_PARSECOMPLETE, 0, NULL, NULL);
154         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_HTTPEQUIV_DONE, 0, NULL, NULL);
155     }
156
157     doc->readystate = READYSTATE_COMPLETE;
158     call_property_onchanged(doc->cp_propnotif, DISPID_READYSTATE);
159
160     if(olecmd) {
161         VARIANT title;
162         WCHAR empty[] = {0};
163         
164         V_VT(&title) = VT_BSTR;
165         V_BSTR(&title) = SysAllocString(empty);
166         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETTITLE, OLECMDEXECOPT_DONTPROMPTUSER,
167                                &title, NULL);
168         SysFreeString(V_BSTR(&title));
169
170         IOleCommandTarget_Release(olecmd);
171     }
172 }
173
174 static void process_task(task_t *task)
175 {
176     switch(task->task_id) {
177     case TASK_SETDOWNLOADSTATE:
178         return set_downloading(task->doc);
179     case TASK_PARSECOMPLETE:
180         return set_parsecomplete(task->doc);
181     default:
182         ERR("Wrong task_id %d\n", task->task_id);
183     }
184 }
185
186 static LRESULT WINAPI hidden_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
187 {
188     switch(msg) {
189     case WM_PROCESSTASK:
190         while(1) {
191             task_t *task = pop_task();
192             if(!task)
193                 break;
194
195             process_task(task);
196             mshtml_free(task);
197         }
198
199         return 0;
200     }
201
202     if(msg > WM_USER)
203         FIXME("(%p %d %x %lx)\n", hwnd, msg, wParam, lParam);
204
205     return DefWindowProcW(hwnd, msg, wParam, lParam);
206 }
207
208 static HWND create_thread_hwnd(void)
209 {
210     static ATOM hidden_wnd_class = 0;
211     static const WCHAR wszInternetExplorer_Hidden[] = {'I','n','t','e','r','n','e','t',
212             ' ','E','x','p','l','o','r','e','r','_','H','i','d','d','e','n',0};
213
214     if(!hidden_wnd_class) {
215         WNDCLASSEXW wndclass = {
216             sizeof(WNDCLASSEXW), 0,
217             hidden_proc,
218             0, 0, hInst, NULL, NULL, NULL, NULL,
219             wszInternetExplorer_Hidden,
220             NULL
221         };
222
223         hidden_wnd_class = RegisterClassExW(&wndclass);
224     }
225
226     return CreateWindowExW(0, wszInternetExplorer_Hidden, NULL, WS_POPUP,
227                            0, 0, 0, 0, NULL, NULL, hInst, NULL);
228 }
229
230 HWND get_thread_hwnd(void)
231 {
232     thread_data_t *thread_data = get_thread_data(TRUE);
233
234     if(!thread_data->thread_hwnd)
235         thread_data->thread_hwnd = create_thread_hwnd();
236
237     return thread_data->thread_hwnd;
238 }
239
240 thread_data_t *get_thread_data(BOOL create)
241 {
242     thread_data_t *thread_data;
243
244     if(!mshtml_tls) {
245         if(create)
246             mshtml_tls = TlsAlloc();
247         else
248             return NULL;
249     }
250
251     thread_data = TlsGetValue(mshtml_tls);
252     if(!thread_data && create) {
253         thread_data = mshtml_alloc_zero(sizeof(thread_data_t));
254         TlsSetValue(mshtml_tls, thread_data);
255     }
256
257     return thread_data;
258 }