1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2007, 2008 Novell, Inc.
23 // Andreia Gaita (avidigal@novell.com)
28 using System.Collections;
29 using System.Runtime.InteropServices;
30 using System.Diagnostics;
31 using Mono.WebBrowser;
33 namespace Mono.Mozilla
37 private static Hashtable boundControls;
38 private static bool initialized;
39 private static object initLock = new object ();
40 private static string monoMozDir;
42 private class BindingInfo
44 public CallbackBinder callback;
45 public IntPtr gluezilla;
48 private static bool isInitialized ()
55 private static BindingInfo getBinding (IWebBrowser control)
57 if (!boundControls.ContainsKey (control))
59 BindingInfo info = boundControls[control] as BindingInfo;
65 boundControls = new Hashtable ();
70 public static void Debug (int signal)
72 gluezilla_debug (signal);
75 public static bool Init (WebBrowser control, Platform platform)
82 short version = gluezilla_init (platform, out mozPlatform);
84 monoMozDir = System.IO.Path.Combine (
85 System.IO.Path.Combine (
86 Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData),
87 ".mono"), "mozilla-" + version);
89 if (!System.IO.Directory.Exists (monoMozDir))
90 System.IO.Directory.CreateDirectory (monoMozDir);
93 catch (DllNotFoundException) {
94 Console.WriteLine ("libgluezilla not found. To have webbrowser support, you need libgluezilla installed");
98 control.enginePlatform = mozPlatform;
105 public static bool Bind (WebBrowser control, IntPtr handle, int width, int height)
107 if (!isInitialized ())
110 BindingInfo info = new BindingInfo ();
111 info.callback = new CallbackBinder (control.callbacks);
112 IntPtr ptrCallback = Marshal.AllocHGlobal (Marshal.SizeOf (info.callback));
113 Marshal.StructureToPtr (info.callback, ptrCallback, true);
115 info.gluezilla = gluezilla_createBrowserWindow (ptrCallback, handle, width, height, Environment.CurrentDirectory, monoMozDir, control.platform);
117 if (info.gluezilla == IntPtr.Zero) {
118 Marshal.FreeHGlobal (ptrCallback);
124 boundControls.Add (control as IWebBrowser, info);
128 public static void Shutdown (IWebBrowser control)
134 BindingInfo info = getBinding (control);
136 gluezilla_shutdown (info.gluezilla);
137 boundControls.Remove (control);
138 if (boundControls.Count == 0) {
146 public static void Focus (IWebBrowser control, FocusOption focus)
148 if (!isInitialized ())
150 BindingInfo info = getBinding (control);
152 gluezilla_focus (info.gluezilla, focus);
156 public static void Blur (IWebBrowser control)
158 if (!isInitialized ())
160 BindingInfo info = getBinding (control);
162 gluezilla_blur (info.gluezilla);
165 public static void Activate (IWebBrowser control)
167 if (!isInitialized ())
169 BindingInfo info = getBinding (control);
171 gluezilla_activate (info.gluezilla);
174 public static void Deactivate (IWebBrowser control)
176 if (!isInitialized ())
178 BindingInfo info = getBinding (control);
180 gluezilla_deactivate (info.gluezilla);
183 public static void Resize (IWebBrowser control, int width, int height)
185 if (!isInitialized ())
187 BindingInfo info = getBinding (control);
189 gluezilla_resize (info.gluezilla, width, height);
193 public static void Home (IWebBrowser control)
195 if (!isInitialized ())
197 BindingInfo info = getBinding (control);
199 gluezilla_home (info.gluezilla);
202 public static nsIWebNavigation GetWebNavigation (IWebBrowser control)
204 if (!isInitialized ())
206 BindingInfo info = getBinding (control);
208 return gluezilla_getWebNavigation (info.gluezilla);
211 public static IntPtr StringInit ()
213 if (!isInitialized ())
215 return gluezilla_stringInit ();
218 public static void StringFinish (HandleRef str)
220 if (!isInitialized ())
222 gluezilla_stringFinish (str);
225 public static string StringGet (HandleRef str)
227 if (!isInitialized ())
229 IntPtr p = gluezilla_stringGet (str);
230 return Marshal.PtrToStringUni (p);
233 public static void StringSet (HandleRef str, string text)
235 if (!isInitialized ())
237 gluezilla_stringSet (str, text);
241 public static object GetProxyForObject (IWebBrowser control, Guid iid, object obj)
243 if (!isInitialized ())
245 BindingInfo info = getBinding (control);
248 gluezilla_getProxyForObject (info.gluezilla, iid, obj, out ret);
250 object o = Marshal.GetObjectForIUnknown (ret);
254 public static string EvalScript (IWebBrowser control, string script)
256 if (!isInitialized ())
258 BindingInfo info = getBinding (control);
259 IntPtr p = gluezilla_evalScript (info.gluezilla, script);
260 return Marshal.PtrToStringAuto (p);
265 [DllImport("gluezilla")]
266 private static extern void gluezilla_debug(int signal);
268 [DllImport("gluezilla")]
269 private static extern short gluezilla_init (Platform platform, out Platform mozPlatform);
271 [DllImport ("gluezilla")]
272 private static extern IntPtr gluezilla_shutdown (IntPtr instance);
274 [DllImport ("gluezilla")]
275 private static extern IntPtr gluezilla_createBrowserWindow (/*IntPtr instance, */IntPtr events, IntPtr hwnd, Int32 width, Int32 height, string startDir, string dataDir, Platform platform);
278 [DllImport ("gluezilla")]
279 private static extern int gluezilla_focus (IntPtr instance, FocusOption focus);
280 [DllImport ("gluezilla")]
281 private static extern int gluezilla_blur (IntPtr instance);
282 [DllImport ("gluezilla")]
283 private static extern int gluezilla_activate (IntPtr instance);
284 [DllImport ("gluezilla")]
285 private static extern int gluezilla_deactivate (IntPtr instance);
286 [DllImport ("gluezilla")]
287 private static extern int gluezilla_resize (IntPtr instance, Int32 width, Int32 height);
290 [DllImport ("gluezilla")]
291 private static extern int gluezilla_home (IntPtr instance);
294 [DllImport ("gluezilla")]
295 [return:MarshalAs(UnmanagedType.Interface)]
296 private static extern nsIWebNavigation gluezilla_getWebNavigation (IntPtr instance);
298 [DllImport ("gluezilla")]
299 private static extern IntPtr gluezilla_stringInit ();
300 [DllImport ("gluezilla")]
301 private static extern int gluezilla_stringFinish (HandleRef str);
302 [DllImport ("gluezilla")]
303 private static extern IntPtr gluezilla_stringGet (HandleRef str);
304 [DllImport ("gluezilla")]
305 private static extern void gluezilla_stringSet (HandleRef str, [MarshalAs (UnmanagedType.LPWStr)] string text);
307 [DllImport ("gluezilla")]
308 private static extern void gluezilla_getProxyForObject (
310 [MarshalAs (UnmanagedType.LPStruct)] Guid iid,
311 [MarshalAs (UnmanagedType.Interface)] object obj,
315 [DllImport ("gluezilla")]
316 public static extern uint gluezilla_StringContainerInit (HandleRef /*nsStringContainer & */aStr);
318 [DllImport ("gluezilla")]
319 public static extern void gluezilla_StringContainerFinish (HandleRef /*nsStringContainer & */aStr);
321 [DllImport ("gluezilla")]
322 public static extern uint gluezilla_StringGetData (HandleRef /*const nsAString &*/ aStr,
323 out IntPtr /*const PRUnichar ** */aBuf,
324 out bool /*PRBool * */aTerm);
326 [DllImport ("gluezilla")]
327 public static extern uint gluezilla_StringSetData (HandleRef /*nsAString &*/ aStr,
328 [MarshalAs (UnmanagedType.LPWStr)] string /*const PRUnichar * */ aBuf, uint aCount);
330 [DllImport ("gluezilla")]
331 public static extern uint gluezilla_CStringContainerInit (HandleRef /*nsCStringContainer &*/ aStr);
333 [DllImport ("gluezilla")]
334 public static extern void gluezilla_CStringContainerFinish (HandleRef /*nsCStringContainer &*/ aStr);
336 [DllImport ("gluezilla")]
337 public static extern uint gluezilla_CStringGetData (HandleRef /*const nsACString &*/ aStr,
338 out IntPtr /*const PRUnichar ** */aBuf,
339 out bool /*PRBool **/ aTerm);
341 [DllImport ("gluezilla")]
342 public static extern uint gluezilla_CStringSetData (HandleRef /*nsACString &*/ aStr,
346 [DllImport ("gluezilla")]
347 [return: MarshalAs (UnmanagedType.Interface)]
348 public static extern nsIServiceManager gluezilla_getServiceManager ();
350 [DllImport ("gluezilla")]
351 private static extern IntPtr gluezilla_evalScript (IntPtr instance, string script);