KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > browser > WebSite


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.browser;
12
13 import org.eclipse.swt.*;
14 import org.eclipse.swt.internal.ole.win32.*;
15 import org.eclipse.swt.ole.win32.*;
16 import org.eclipse.swt.widgets.*;
17 import org.eclipse.swt.internal.win32.*;
18
19 class WebSite extends OleControlSite {
20     COMObject iDocHostUIHandler;
21     COMObject iDocHostShowUI;
22     COMObject iServiceProvider;
23     COMObject iInternetSecurityManager;
24     COMObject iOleCommandTarget;
25
26     static final int OLECMDID_SHOWSCRIPTERROR = 40;
27     static final String JavaDoc CONSUME_KEY = "org.eclipse.swt.OleFrame.ConsumeKey"; //$NON-NLS-1$
28

29 public WebSite(Composite parent, int style, String JavaDoc progId) {
30     super(parent, style, progId);
31 }
32
33 protected void createCOMInterfaces () {
34     super.createCOMInterfaces();
35     iDocHostUIHandler = new COMObject(new int[]{2, 0, 0, 4, 1, 5, 0, 0, 1, 1, 1, 3, 3, 2, 2, 1, 3, 2}){
36         public int method0(int[] args) {return QueryInterface(args[0], args[1]);}
37         public int method1(int[] args) {return AddRef();}
38         public int method2(int[] args) {return Release();}
39         public int method3(int[] args) {return ShowContextMenu(args[0], args[1], args[2], args[3]);}
40         public int method4(int[] args) {return GetHostInfo(args[0]);}
41         public int method5(int[] args) {return ShowUI(args[0], args[1], args[2], args[3], args[4]);}
42         public int method6(int[] args) {return HideUI();}
43         public int method7(int[] args) {return UpdateUI();}
44         public int method8(int[] args) {return EnableModeless(args[0]);}
45         public int method9(int[] args) {return OnDocWindowActivate(args[0]);}
46         public int method10(int[] args) {return OnFrameWindowActivate(args[0]);}
47         public int method11(int[] args) {return ResizeBorder(args[0], args[1], args[2]);}
48         public int method12(int[] args) {return TranslateAccelerator(args[0], args[1], args[2]);}
49         public int method13(int[] args) {return GetOptionKeyPath(args[0], args[1]);}
50         public int method14(int[] args) {return GetDropTarget(args[0], args[1]);}
51         public int method15(int[] args) {return GetExternal(args[0]);}
52         public int method16(int[] args) {return TranslateUrl(args[0], args[1], args[2]);}
53         public int method17(int[] args) {return FilterDataObject(args[0], args[1]);}
54     };
55     iDocHostShowUI = new COMObject(new int[]{2, 0, 0, 7, 7}){
56         public int method0(int[] args) {return QueryInterface(args[0], args[1]);}
57         public int method1(int[] args) {return AddRef();}
58         public int method2(int[] args) {return Release();}
59         public int method3(int[] args) {return ShowMessage(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);}
60         public int method4(int[] args) {return ShowHelp(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);}
61     };
62     iServiceProvider = new COMObject(new int[]{2, 0, 0, 3}){
63         public int method0(int[] args) {return QueryInterface(args[0], args[1]);}
64         public int method1(int[] args) {return AddRef();}
65         public int method2(int[] args) {return Release();}
66         public int method3(int[] args) {return QueryService(args[0], args[1], args[2]);}
67     };
68     iInternetSecurityManager = new COMObject(new int[]{2, 0, 0, 1, 1, 3, 4, 8, 7, 3, 3}){
69         public int method0(int[] args) {return QueryInterface(args[0], args[1]);}
70         public int method1(int[] args) {return AddRef();}
71         public int method2(int[] args) {return Release();}
72         public int method3(int[] args) {return SetSecuritySite(args[0]);}
73         public int method4(int[] args) {return GetSecuritySite(args[0]);}
74         public int method5(int[] args) {return MapUrlToZone(args[0], args[1], args[2]);}
75         public int method6(int[] args) {return GetSecurityId(args[0], args[1], args[2], args[3]);}
76         public int method7(int[] args) {return ProcessUrlAction(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);}
77         public int method8(int[] args) {return QueryCustomPolicy(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);}
78         public int method9(int[] args) {return SetZoneMapping(args[0], args[1], args[2]);}
79         public int method10(int[] args) {return GetZoneMappings(args[0], args[1], args[2]);}
80     };
81     iOleCommandTarget = new COMObject(new int[]{2, 0, 0, 4, 5}) {
82         public int method0(int[] args) {return QueryInterface(args[0], args[1]);}
83         public int method1(int[] args) {return AddRef();}
84         public int method2(int[] args) {return Release();}
85         public int method3(int[] args) {return QueryStatus(args[0], args[1], args[2], args[3]);}
86         public int method4(int[] args) {return Exec(args[0], args[1], args[2], args[3], args[4]);}
87     };
88 }
89
90 protected void disposeCOMInterfaces() {
91     super.disposeCOMInterfaces();
92     if (iDocHostUIHandler != null) {
93         iDocHostUIHandler.dispose();
94         iDocHostUIHandler = null;
95     }
96     if (iDocHostShowUI != null) {
97         iDocHostShowUI.dispose();
98         iDocHostShowUI = null;
99     }
100     if (iServiceProvider != null) {
101         iServiceProvider.dispose();
102         iServiceProvider = null;
103     }
104     if (iInternetSecurityManager != null) {
105         iInternetSecurityManager.dispose();
106         iInternetSecurityManager = null;
107     }
108     if (iOleCommandTarget != null) {
109         iOleCommandTarget.dispose();
110         iOleCommandTarget = null;
111     }
112 }
113
114 protected int AddRef() {
115     /* Workaround for javac 1.1.8 bug */
116     return super.AddRef();
117 }
118
119 protected int QueryInterface(int riid, int ppvObject) {
120     int result = super.QueryInterface(riid, ppvObject);
121     if (result == COM.S_OK) return result;
122     if (riid == 0 || ppvObject == 0) return COM.E_INVALIDARG;
123     GUID guid = new GUID();
124     COM.MoveMemory(guid, riid, GUID.sizeof);
125     if (COM.IsEqualGUID(guid, COM.IIDIDocHostUIHandler)) {
126         COM.MoveMemory(ppvObject, new int[] {iDocHostUIHandler.getAddress()}, 4);
127         AddRef();
128         return COM.S_OK;
129     }
130     if (COM.IsEqualGUID(guid, COM.IIDIDocHostShowUI)) {
131         COM.MoveMemory(ppvObject, new int[] {iDocHostShowUI.getAddress()}, 4);
132         AddRef();
133         return COM.S_OK;
134     }
135     if (COM.IsEqualGUID(guid, COM.IIDIServiceProvider)) {
136         COM.MoveMemory(ppvObject, new int[] {iServiceProvider.getAddress()}, 4);
137         AddRef();
138         return COM.S_OK;
139     }
140     if (COM.IsEqualGUID(guid, COM.IIDIInternetSecurityManager)) {
141         COM.MoveMemory(ppvObject, new int[] {iInternetSecurityManager.getAddress()}, 4);
142         AddRef();
143         return COM.S_OK;
144     }
145     if (COM.IsEqualGUID(guid, COM.IIDIOleCommandTarget)) {
146         COM.MoveMemory(ppvObject, new int[] {iOleCommandTarget.getAddress()}, 4);
147         AddRef();
148         return COM.S_OK;
149     }
150     COM.MoveMemory(ppvObject, new int[] {0}, 4);
151     return COM.E_NOINTERFACE;
152 }
153
154 /* IDocHostUIHandler */
155
156 int EnableModeless(int EnableModeless) {
157     return COM.E_NOTIMPL;
158 }
159
160 int FilterDataObject(int pDO, int ppDORet) {
161     return COM.E_NOTIMPL;
162 }
163
164 int GetDropTarget(int pDropTarget, int ppDropTarget) {
165     return COM.E_NOTIMPL;
166 }
167
168 int GetExternal(int ppDispatch) {
169     OS.MoveMemory(ppDispatch, new int[] {0}, 4);
170     return COM.S_FALSE;
171 }
172
173 int GetHostInfo(int pInfo) {
174     IE browser = (IE)((Browser)getParent().getParent()).webBrowser;
175     OS.MoveMemory(pInfo + 4, new int[] {browser.info}, 4);
176     return COM.S_OK;
177 }
178
179 int GetOptionKeyPath(int pchKey, int dw) {
180     return COM.E_NOTIMPL;
181 }
182
183 int HideUI() {
184     return COM.E_NOTIMPL;
185 }
186
187 int OnDocWindowActivate(int fActivate) {
188     return COM.E_NOTIMPL;
189 }
190
191 int OnFrameWindowActivate(int fActivate) {
192     return COM.E_NOTIMPL;
193 }
194
195 protected int Release() {
196     /* Workaround for javac 1.1.8 bug */
197     return super.Release();
198 }
199
200 int ResizeBorder(int prcBorder, int pUIWindow, int fFrameWindow) {
201     return COM.E_NOTIMPL;
202 }
203
204 int ShowContextMenu(int dwID, int ppt, int pcmdtReserved, int pdispReserved) {
205     Browser browser = (Browser)getParent().getParent();
206     Event event = new Event();
207     POINT pt = new POINT();
208     OS.MoveMemory(pt, ppt, POINT.sizeof);
209     event.x = pt.x;
210     event.y = pt.y;
211     browser.notifyListeners(SWT.MenuDetect, event);
212     if (!event.doit) return COM.S_OK;
213     Menu menu = browser.getMenu();
214     if (menu != null && !menu.isDisposed ()) {
215         if (pt.x != event.x || pt.y != event.y) {
216             menu.setLocation (event.x, event.y);
217         }
218         menu.setVisible (true);
219         return COM.S_OK;
220     }
221     /* Show default IE popup menu */
222     return COM.S_FALSE;
223 }
224
225 int ShowUI(int dwID, int pActiveObject, int pCommandTarget, int pFrame, int pDoc) {
226     return COM.S_FALSE;
227 }
228
229 int TranslateAccelerator(int lpMsg, int pguidCmdGroup, int nCmdID) {
230     /*
231     * Feature on Internet Explorer. By default the embedded Internet Explorer control runs
232     * the Internet Explorer shortcuts (e.g. F5 for refresh). This overrides the shortcuts
233     * defined by SWT. The workaround is to forward the accelerator keys to the parent window
234     * and have Internet Explorer ignore the ones handled by the parent window.
235     */

236     Menu menubar = getShell().getMenuBar();
237     if (menubar != null && !menubar.isDisposed() && menubar.isEnabled()) {
238         Shell shell = menubar.getShell();
239         int hwnd = shell.handle;
240         int hAccel = OS.SendMessage(hwnd, OS.WM_APP+1, 0, 0);
241         if (hAccel != 0) {
242             MSG msg = new MSG();
243             OS.MoveMemory(msg, lpMsg, MSG.sizeof);
244             if (OS.TranslateAccelerator(hwnd, hAccel, msg) != 0) return COM.S_OK;
245         }
246     }
247     /*
248     * Feature on Internet Explorer. By default the embedded Internet Explorer control runs
249     * the Internet Explorer shortcuts. CTRL-N opens a standalone IE, which is undesirable
250     * and can cause a crash in some contexts. CTRL-O is being intercepted by IE, but this
251     * accelerator should be handled by Eclipse. F5 causes a refresh, which is not appropriate
252     * when rendering HTML from memory. The workaround is to block the handling of these
253     * shortcuts by IE when necessary, and in some cases ensure that Eclipse has an opportunity
254     * to handle these accelerators.
255     */

256     int result = COM.S_FALSE;
257     MSG msg = new MSG();
258     OS.MoveMemory(msg, lpMsg, MSG.sizeof);
259     if (msg.message == OS.WM_KEYDOWN) {
260         switch (msg.wParam) {
261             case OS.VK_N:
262             case OS.VK_O:
263                 if (OS.GetKeyState (OS.VK_CONTROL) < 0) {
264                     getParent().setData(CONSUME_KEY, "false"); //$NON-NLS-1$
265
result = COM.S_OK;
266                 }
267                 break;
268             case OS.VK_F5:
269                 OleAutomation auto = new OleAutomation(this);
270                 int[] rgdispid = auto.getIDsOfNames(new String JavaDoc[] { "LocationURL" }); //$NON-NLS-1$
271
Variant pVarResult = auto.getProperty(rgdispid[0]);
272                 auto.dispose();
273                 if (pVarResult != null) {
274                     if (pVarResult.getType() == OLE.VT_BSTR) {
275                         String JavaDoc url = pVarResult.getString();
276                         if (url.equals(IE.ABOUT_BLANK)) result = COM.S_OK;
277                     }
278                     pVarResult.dispose();
279                 }
280                 break;
281         }
282     }
283     return result;
284 }
285
286 int TranslateUrl(int dwTranslate, int pchURLIn, int ppchURLOut) {
287     return COM.E_NOTIMPL;
288 }
289
290 int UpdateUI() {
291     return COM.E_NOTIMPL;
292 }
293
294 /* IDocHostShowUI */
295
296 int ShowMessage(int hwnd, int lpstrText, int lpstrCaption, int dwType, int lpstrHelpFile, int dwHelpContext, int plResult) {
297     /*
298     * Feature on IE. When IE navigates to a website that contains an ActiveX that is prevented from
299     * being executed, IE displays a message "Your current security settings prohibit running ActiveX
300     * controls on this page ...". The workaround is to selectively block this alert as indicated
301     * in the MSDN article "WebBrowser customization".
302     */

303     /* resource identifier in shdoclc.dll for window caption "Your current security settings prohibit
304      * running ActiveX controls on this page ..."
305      */

306     int IDS_MESSAGE_BOX_CAPTION = 8033;
307         if (lpstrText != 0) {
308         TCHAR lpLibFileName = new TCHAR (0, "SHDOCLC.DLL", true); //$NON-NLS-1$
309
int hModule = OS.LoadLibrary(lpLibFileName);
310         if (hModule != 0) {
311             /*
312             * Note. lpstrText is a LPOLESTR, i.e. a null terminated unicode string LPWSTR, i.e. a WCHAR*.
313             * It is not a BSTR. A BSTR is a null terminated unicode string that contains its length
314             * at the beginning.
315             */

316             int cnt = OS.wcslen(lpstrText);
317             char[] buffer = new char[cnt];
318             /*
319             * Note. lpstrText is unicode on both unicode and ansi platforms.
320             * The nbr of chars is multiplied by the constant 2 and not by TCHAR.sizeof since
321             * TCHAR.sizeof returns 1 on ansi platforms.
322             */

323             OS.MoveMemory(buffer, lpstrText, cnt * 2);
324             String JavaDoc text = new String JavaDoc(buffer);
325             /* provide a buffer large enough to hold the string to compare to and a null terminated character */
326             int length = (OS.IsUnicode ? cnt : OS.WideCharToMultiByte (OS.CP_ACP, 0, buffer, cnt, 0, 0, null, null)) + 1;
327
328             TCHAR lpBuffer = new TCHAR(0, length);
329             int result = OS.LoadString(hModule, IDS_MESSAGE_BOX_CAPTION, lpBuffer, length);
330             OS.FreeLibrary(hModule);
331             return result > 0 && text.equals(lpBuffer.toString(0, result)) ? COM.S_OK : COM.S_FALSE;
332         }
333     }
334     return COM.S_FALSE;
335 }
336
337 /* Note. One of the arguments of ShowHelp is a POINT struct and not a pointer to a POINT struct. Because
338  * of the way Callback gets int parameters from a va_list of C arguments 2 integer arguments must be declared,
339  * ptMouse_x and ptMouse_y. Otherwise the Browser crashes when the user presses F1 to invoke
340  * the help.
341  */

342 int ShowHelp(int hwnd, int pszHelpFile, int uCommand, int dwData, int ptMouse_x, int ptMouse_y, int pDispatchObjectHit) {
343     Browser browser = (Browser)getParent().getParent();
344     Event event = new Event();
345     event.type = SWT.Help;
346     event.display = getDisplay();
347     event.widget = browser;
348     Shell shell = browser.getShell();
349     Control control = browser;
350     do {
351         if (control.isListening(SWT.Help)) {
352             control.notifyListeners(SWT.Help, event);
353             break;
354         }
355         if (control == shell) break;
356         control = control.getParent();
357     } while (true);
358     return COM.S_OK;
359 }
360
361 /* IServiceProvider */
362
363 int QueryService(int guidService, int riid, int ppvObject) {
364     if (riid == 0 || ppvObject == 0) return COM.E_INVALIDARG;
365     GUID guid = new GUID();
366     COM.MoveMemory(guid, riid, GUID.sizeof);
367     if (COM.IsEqualGUID(guid, COM.IIDIInternetSecurityManager)) {
368         COM.MoveMemory(ppvObject, new int[] {iInternetSecurityManager.getAddress()}, 4);
369         AddRef();
370         return COM.S_OK;
371     }
372     COM.MoveMemory(ppvObject, new int[] {0}, 4);
373     return COM.E_NOINTERFACE;
374 }
375
376 /* IInternetSecurityManager */
377
378 int SetSecuritySite(int pSite) {
379     return IE.INET_E_DEFAULT_ACTION;
380 }
381
382 int GetSecuritySite(int ppSite) {
383     return IE.INET_E_DEFAULT_ACTION;
384 }
385
386 int MapUrlToZone(int pwszUrl, int pdwZone, int dwFlags) {
387     /*
388     * Feature in IE 6 sp1. HTML rendered in memory
389     * does not enable local links but the exact same
390     * HTML document loaded through a local file is
391     * permitted to follow local links. The workaround is
392     * to return URLZONE_INTRANET instead of the default
393     * value URLZONE_LOCAL_MACHINE.
394     */

395     COM.MoveMemory(pdwZone, new int[] {IE.URLZONE_INTRANET}, 4);
396     return COM.S_OK;
397 }
398
399 int GetSecurityId(int pwszUrl, int pbSecurityId, int pcbSecurityId, int dwReserved) {
400     return IE.INET_E_DEFAULT_ACTION;
401 }
402
403 int ProcessUrlAction(int pwszUrl, int dwAction, int pPolicy, int cbPolicy, int pContext, int cbContext, int dwFlags, int dwReserved) {
404     /*
405     * Feature in IE 6 sp1. HTML rendered in memory
406     * containing an OBJECT tag referring to a local file
407     * brings up a warning dialog asking the user whether
408     * it should proceed or not. The workaround is to
409     * set the policy to URLPOLICY_ALLOW in this case (dwAction
410     * value of 0x1406).
411     *
412     * Feature in IE. Security Patches and user settings
413     * affect the way the embedded web control behaves. The current
414     * approach is to consider the content trusted and allow
415     * all URLs by default.
416     */

417     int policy = IE.URLPOLICY_ALLOW;
418     /*
419     * The URLACTION_JAVA flags refer to the <applet> tag, which resolves to
420     * the Microsoft VM if the applet is java 1.1.x compliant, or to the OS's
421     * java plug-in VM otherwise. Applets launched with the MS VM work in the
422     * Browser, but applets launched with the OS's java plug-in VM crash as a
423     * result of the VM failing to load. Set the policy to URLPOLICY_JAVA_PROHIBIT
424     * so that applets compiled with java compliance > 1.1.x will not crash.
425     */

426     if (dwAction >= IE.URLACTION_JAVA_MIN && dwAction <= IE.URLACTION_JAVA_MAX) {
427         policy = IE.URLPOLICY_JAVA_PROHIBIT;
428     }
429     /*
430     * Note. Some ActiveX plugins crash when executing
431     * inside the embedded explorer itself running into
432     * a JVM. The current workaround is to detect when
433     * such ActiveX is about to be started and refuse
434     * to execute it.
435     */

436     if (dwAction == IE.URLACTION_ACTIVEX_RUN) {
437         GUID guid = new GUID();
438         COM.MoveMemory(guid, pContext, GUID.sizeof);
439         if (COM.IsEqualGUID(guid, COM.IIDJavaBeansBridge) || COM.IsEqualGUID(guid, COM.IIDShockwaveActiveXControl)) {
440             policy = IE.URLPOLICY_DISALLOW;
441         }
442     }
443     if (cbPolicy >= 4) COM.MoveMemory(pPolicy, new int[] {policy}, 4);
444     return policy == IE.URLPOLICY_ALLOW ? COM.S_OK : COM.S_FALSE;
445 }
446
447 int QueryCustomPolicy(int pwszUrl, int guidKey, int ppPolicy, int pcbPolicy, int pContext, int cbContext, int dwReserved) {
448     return IE.INET_E_DEFAULT_ACTION;
449 }
450
451 int SetZoneMapping(int dwZone, int lpszPattern, int dwFlags) {
452     return IE.INET_E_DEFAULT_ACTION;
453 }
454
455 int GetZoneMappings(int dwZone, int ppenumString, int dwFlags) {
456     return COM.E_NOTIMPL;
457 }
458
459 /* IOleCommandTarget */
460 int QueryStatus(int pguidCmdGroup, int cCmds, int prgCmds, int pCmdText) {
461     return COM.E_NOTSUPPORTED;
462 }
463
464 int Exec(int pguidCmdGroup, int nCmdID, int nCmdExecOpt, int pvaIn, int pvaOut) {
465     if (pguidCmdGroup != 0) {
466         GUID guid = new GUID();
467         COM.MoveMemory(guid, pguidCmdGroup, GUID.sizeof);
468
469         /*
470         * If a javascript error occurred then suppress IE's default script error dialog.
471         */

472         if (COM.IsEqualGUID(guid, COM.CGID_DocHostCommandHandler)) {
473             if (nCmdID == OLECMDID_SHOWSCRIPTERROR) return COM.S_OK;
474         }
475
476         /*
477         * Bug in Internet Explorer. OnToolBar TRUE is also fired when any of the
478         * address bar or menu bar are requested but not the tool bar. A workaround
479         * has been posted by a Microsoft developer on the public webbrowser_ctl
480         * newsgroup. The workaround is to implement the IOleCommandTarget interface
481         * to test the argument of an undocumented command.
482         */

483         if (nCmdID == 1 && COM.IsEqualGUID(guid, COM.CGID_Explorer) && ((nCmdExecOpt & 0xFFFF) == 0xA)) {
484             IE browser = (IE)((Browser)getParent().getParent()).webBrowser;
485             browser.toolBar = (nCmdExecOpt & 0xFFFF0000) != 0;
486         }
487     }
488     return COM.E_NOTSUPPORTED;
489 }
490
491 }
492
Popular Tags