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_bind (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 bool Create (IWebBrowser control) {
129 if (!isInitialized ())
131 BindingInfo info = getBinding (control);
133 gluezilla_createBrowserWindow (info.gluezilla);
137 public static void Shutdown (IWebBrowser control)
143 BindingInfo info = getBinding (control);
145 gluezilla_shutdown (info.gluezilla);
146 boundControls.Remove (control);
147 if (boundControls.Count == 0) {
155 public static void Focus (IWebBrowser control, FocusOption focus)
157 if (!isInitialized ())
159 BindingInfo info = getBinding (control);
161 gluezilla_focus (info.gluezilla, focus);
165 public static void Blur (IWebBrowser control)
167 if (!isInitialized ())
169 BindingInfo info = getBinding (control);
171 gluezilla_blur (info.gluezilla);
174 public static void Activate (IWebBrowser control)
176 if (!isInitialized ())
178 BindingInfo info = getBinding (control);
180 gluezilla_activate (info.gluezilla);
183 public static void Deactivate (IWebBrowser control)
185 if (!isInitialized ())
187 BindingInfo info = getBinding (control);
189 gluezilla_deactivate (info.gluezilla);
192 public static void Resize (IWebBrowser control, int width, int height)
194 if (!isInitialized ())
196 BindingInfo info = getBinding (control);
198 gluezilla_resize (info.gluezilla, width, height);
202 public static void Home (IWebBrowser control)
204 if (!isInitialized ())
206 BindingInfo info = getBinding (control);
208 gluezilla_home (info.gluezilla);
211 public static nsIWebNavigation GetWebNavigation (IWebBrowser control)
213 if (!isInitialized ())
215 BindingInfo info = getBinding (control);
217 return gluezilla_getWebNavigation (info.gluezilla);
220 public static IntPtr StringInit ()
222 if (!isInitialized ())
224 return gluezilla_stringInit ();
227 public static void StringFinish (HandleRef str)
229 if (!isInitialized ())
231 gluezilla_stringFinish (str);
234 public static string StringGet (HandleRef str)
236 if (!isInitialized ())
238 IntPtr p = gluezilla_stringGet (str);
239 return Marshal.PtrToStringUni (p);
242 public static void StringSet (HandleRef str, string text)
244 if (!isInitialized ())
246 gluezilla_stringSet (str, text);
250 public static object GetProxyForObject (IWebBrowser control, Guid iid, object obj)
252 if (!isInitialized ())
254 BindingInfo info = getBinding (control);
257 gluezilla_getProxyForObject (info.gluezilla, iid, obj, out ret);
259 object o = Marshal.GetObjectForIUnknown (ret);
263 public static nsIServiceManager GetServiceManager (IWebBrowser control)
265 if (!isInitialized ())
267 BindingInfo info = getBinding (control);
269 return gluezilla_getServiceManager2 (info.gluezilla);
272 public static string EvalScript (IWebBrowser control, string script)
274 if (!isInitialized ())
276 BindingInfo info = getBinding (control);
277 IntPtr p = gluezilla_evalScript (info.gluezilla, script);
278 return Marshal.PtrToStringAuto (p);
283 [DllImport("gluezilla")]
284 private static extern void gluezilla_debug(int signal);
286 [DllImport("gluezilla")]
287 private static extern short gluezilla_init (Platform platform, out Platform mozPlatform);
289 [DllImport ("gluezilla")]
290 private static extern IntPtr gluezilla_bind (IntPtr events, IntPtr hwnd,
291 Int32 width, Int32 height,
292 string startDir, string dataDir,
295 [DllImport ("gluezilla")]
296 private static extern int gluezilla_createBrowserWindow (IntPtr instance);
298 [DllImport ("gluezilla")]
299 private static extern IntPtr gluezilla_shutdown (IntPtr instance);
302 [DllImport ("gluezilla")]
303 private static extern int gluezilla_focus (IntPtr instance, FocusOption focus);
304 [DllImport ("gluezilla")]
305 private static extern int gluezilla_blur (IntPtr instance);
306 [DllImport ("gluezilla")]
307 private static extern int gluezilla_activate (IntPtr instance);
308 [DllImport ("gluezilla")]
309 private static extern int gluezilla_deactivate (IntPtr instance);
310 [DllImport ("gluezilla")]
311 private static extern int gluezilla_resize (IntPtr instance, Int32 width, Int32 height);
314 [DllImport ("gluezilla")]
315 private static extern int gluezilla_home (IntPtr instance);
318 [DllImport ("gluezilla")]
319 [return:MarshalAs(UnmanagedType.Interface)]
320 private static extern nsIWebNavigation gluezilla_getWebNavigation (IntPtr instance);
322 [DllImport ("gluezilla")]
323 private static extern IntPtr gluezilla_stringInit ();
324 [DllImport ("gluezilla")]
325 private static extern int gluezilla_stringFinish (HandleRef str);
326 [DllImport ("gluezilla")]
327 private static extern IntPtr gluezilla_stringGet (HandleRef str);
328 [DllImport ("gluezilla")]
329 private static extern void gluezilla_stringSet (HandleRef str, [MarshalAs (UnmanagedType.LPWStr)] string text);
331 [DllImport ("gluezilla")]
332 private static extern void gluezilla_getProxyForObject (
334 [MarshalAs (UnmanagedType.LPStruct)] Guid iid,
335 [MarshalAs (UnmanagedType.Interface)] object obj,
339 [DllImport ("gluezilla")]
340 public static extern uint gluezilla_StringContainerInit (HandleRef /*nsStringContainer & */aStr);
342 [DllImport ("gluezilla")]
343 public static extern void gluezilla_StringContainerFinish (HandleRef /*nsStringContainer & */aStr);
345 [DllImport ("gluezilla")]
346 public static extern uint gluezilla_StringGetData (HandleRef /*const nsAString &*/ aStr,
347 out IntPtr /*const PRUnichar ** */aBuf,
348 out bool /*PRBool * */aTerm);
350 [DllImport ("gluezilla")]
351 public static extern uint gluezilla_StringSetData (HandleRef /*nsAString &*/ aStr,
352 [MarshalAs (UnmanagedType.LPWStr)] string /*const PRUnichar * */ aBuf, uint aCount);
354 [DllImport ("gluezilla")]
355 public static extern uint gluezilla_CStringContainerInit (HandleRef /*nsCStringContainer &*/ aStr);
357 [DllImport ("gluezilla")]
358 public static extern void gluezilla_CStringContainerFinish (HandleRef /*nsCStringContainer &*/ aStr);
360 [DllImport ("gluezilla")]
361 public static extern uint gluezilla_CStringGetData (HandleRef /*const nsACString &*/ aStr,
362 out IntPtr /*const PRUnichar ** */aBuf,
363 out bool /*PRBool **/ aTerm);
365 [DllImport ("gluezilla")]
366 public static extern uint gluezilla_CStringSetData (HandleRef /*nsACString &*/ aStr,
370 [DllImport ("gluezilla")]
371 [return: MarshalAs (UnmanagedType.Interface)]
372 public static extern nsIServiceManager gluezilla_getServiceManager2 (IntPtr instance);
374 [DllImport ("gluezilla")]
375 private static extern IntPtr gluezilla_evalScript (IntPtr instance, string script);