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)
29 using System
.Collections
;
30 using System
.Reflection
;
31 using System
.Runtime
.InteropServices
;
33 using System
.Diagnostics
;
34 using Mono
.WebBrowser
;
35 using Mono
.WebBrowser
.DOM
;
37 namespace Mono
.Mozilla
39 internal class WebBrowser
: IWebBrowser
42 internal bool created
= false;
43 bool creating
= false;
45 internal DOM
.Document document
;
47 internal DOM
.Navigation navigation
;
48 internal Platform platform
;
49 internal Platform enginePlatform
;
50 internal Callback callbacks
;
51 System
.ComponentModel
.EventHandlerList events
;
52 System
.ComponentModel
.EventHandlerList domEvents
;
58 internal Hashtable documents
;
64 public WebBrowser (Platform platform
)
66 this.platform
= platform
;
67 callbacks
= new Callback(this);
68 loaded
= Base
.Init (this, platform
);
69 documents
= new Hashtable ();
72 public bool Load (IntPtr handle
, int width
, int height
)
74 loaded
= Base
.Bind (this, handle
, width
, height
);
80 if (!creating
&& !created
) {
82 created
= Base
.Create (this);
83 if (created
&& isDirty
) {
85 Base
.Resize (this, width
, height
);
92 public void Shutdown ()
97 internal void Reset ()
100 this.DomEvents
.Dispose ();
101 this.domEvents
= null;
102 this.documents
.Clear ();
105 public bool Initialized
{
106 get { return this.loaded; }
109 public IWindow Window
{
111 if (Navigation
!= null) {
112 nsIWebBrowserFocus webBrowserFocus
= (nsIWebBrowserFocus
) (navigation
.navigation
);
114 webBrowserFocus
.getFocusedWindow (out window
);
116 ((nsIWebBrowser
) navigation
.navigation
).getContentDOMWindow (out window
);
118 return new DOM
.Window (this, window
) as IWindow
;
124 public IDocument Document
{
126 if (Navigation
!= null && document
== null) {
127 document
= navigation
.Document
;
129 return document
as IDocument
;
133 public INavigation Navigation
{
135 if (!Created
) return null;
137 if (navigation
== null) {
139 nsIWebNavigation webNav
= Base
.GetWebNavigation (this);
140 navigation
= new DOM
.Navigation (this, webNav
);
142 return navigation
as INavigation
;
146 public string StatusText
{
147 get { return statusText; }
150 public bool Offline
{
153 if (!Created
) return true;
154 IOService
.getOffline (out ret
);
158 IOService
.setOffline (value);
162 internal System
.ComponentModel
.EventHandlerList DomEvents
{
164 if (domEvents
== null)
165 domEvents
= new System
.ComponentModel
.EventHandlerList();
171 internal System
.ComponentModel
.EventHandlerList Events
{
174 events
= new System
.ComponentModel
.EventHandlerList();
180 Mono
.Mozilla
.DOM
.ContentListener contentListener
;
181 Mono
.Mozilla
.DOM
.ContentListener ContentListener
{
183 if (contentListener
== null)
184 contentListener
= new Mono
.Mozilla
.DOM
.ContentListener (this);
185 return contentListener
;
190 nsIServiceManager servMan
;
191 internal nsIServiceManager ServiceManager
{
193 if (servMan
== null) {
194 servMan
= Base
.GetServiceManager (this);
200 nsIIOService ioService
;
201 internal nsIIOService IOService
{
203 if (ioService
== null) {
204 IntPtr ioServicePtr
= IntPtr
.Zero
;
206 ServiceManager
.getServiceByContractID ("@mozilla.org/network/io-service;1", typeof (nsIIOService
).GUID
,
208 if (ioServicePtr
== IntPtr
.Zero
)
209 throw new Mono
.WebBrowser
.Exception (Mono
.WebBrowser
.Exception
.ErrorCodes
.IOService
);
212 ioService
= (nsIIOService
)Marshal
.GetObjectForIUnknown (ioServicePtr
);
213 } catch (System
.Exception ex
) {
214 throw new Mono
.WebBrowser
.Exception (Mono
.WebBrowser
.Exception
.ErrorCodes
.IOService
, ex
);
221 nsIAccessibilityService accessibilityService
;
222 internal nsIAccessibilityService AccessibilityService
{
224 if (accessibilityService
== null) {
225 IntPtr accessibilityServicePtr
= IntPtr
.Zero
;
226 ServiceManager
.getServiceByContractID ("@mozilla.org/accessibilityService;1", typeof (nsIAccessibilityService
).GUID
,
227 out accessibilityServicePtr
);
228 if (accessibilityServicePtr
== IntPtr
.Zero
) {
229 throw new Mono
.WebBrowser
.Exception (Mono
.WebBrowser
.Exception
.ErrorCodes
.AccessibilityService
);
233 accessibilityService
= (nsIAccessibilityService
)Marshal
.GetObjectForIUnknown (accessibilityServicePtr
);
234 } catch (System
.Exception ex
) {
235 throw new Mono
.WebBrowser
.Exception (Mono
.WebBrowser
.Exception
.ErrorCodes
.AccessibilityService
, ex
);
238 return accessibilityService
;
242 nsIErrorService errorService
;
243 internal nsIErrorService ErrorService
{
245 if (errorService
== null) {
246 IntPtr errorServicePtr
= IntPtr
.Zero
;
248 ServiceManager
.getServiceByContractID ("@mozilla.org/xpcom/error-service;1", typeof (nsIErrorService
).GUID
,
249 out errorServicePtr
);
250 if (errorServicePtr
== IntPtr
.Zero
)
254 errorService
= (nsIErrorService
)Marshal
.GetObjectForIUnknown (errorServicePtr
);
255 } catch (System
.Exception ex
) {
263 DocumentEncoder docEncoder
;
264 internal DocumentEncoder DocEncoder
{
266 if (docEncoder
== null) {
267 docEncoder
= new DocumentEncoder (this);
274 public void FocusIn (FocusOption focus
)
276 if (!created
) return;
277 Base
.Focus (this, focus
);
279 public void FocusOut ()
281 if (!created
) return;
285 public void Activate ()
287 if (!Created
) return;
288 Base
.Activate (this);
290 public void Deactivate ()
292 if (!created
) return;
293 Base
.Deactivate (this);
296 public void Resize (int width
, int height
)
299 this.height
= height
;
301 if (!created
) return;
302 Base
.Resize (this, width
, height
);
305 public void Render (byte[] data
)
307 if (!Created
) return;
309 throw new ArgumentNullException ("data");
310 string html
= System
.Text
.ASCIIEncoding
.UTF8
.GetString (data
);
314 public void Render (string html
)
316 if (!Created
) return;
317 Render (html
, "file:///", "text/html");
321 public void Render (string html
, string uri
, string contentType
)
323 if (!Created
) return;
324 nsIWebBrowserStream stream
;
325 if (Navigation
!= null) {
326 stream
= (nsIWebBrowserStream
) navigation
.navigation
;
328 throw new Mono
.WebBrowser
.Exception (Mono
.WebBrowser
.Exception
.ErrorCodes
.Navigation
);
329 AsciiString asciiUri
= new AsciiString (uri
);
331 IOService
.newURI (asciiUri
.Handle
, null, null, out ret
);
333 AsciiString ctype
= new AsciiString(contentType
);
335 HandleRef han
= ctype
.Handle
;
337 stream
.openStream (ret
, han
);
339 IntPtr native_html
= Marshal
.StringToHGlobalAnsi (html
);
340 stream
.appendToStream (native_html
, (uint)html
.Length
);
341 Marshal
.FreeHGlobal (native_html
);
343 stream
.closeStream ();
347 public void ExecuteScript (string script
) {
348 if (!Created
) return;
349 Base
.EvalScript (this, script
);
352 internal void AttachEvent (INode node
, string eve
, EventHandler handler
) {
353 string key
= String
.Intern (node
.GetHashCode() + ":" + eve
);
355 Console
.Error
.WriteLine ("Event Attached: " + key
);
357 DomEvents
.AddHandler (key
, handler
);
360 internal void DetachEvent (INode node
, string eve
, EventHandler handler
) {
361 string key
= String
.Intern (node
.GetHashCode() + ":" + eve
);
363 Console
.Error
.WriteLine ("Event Detached: " + key
);
365 DomEvents
.RemoveHandler (key
, handler
);
371 internal static object KeyDownEvent
= new object ();
372 internal static object KeyPressEvent
= new object ();
373 internal static object KeyUpEvent
= new object ();
374 internal static object MouseClickEvent
= new object ();
375 internal static object MouseDoubleClickEvent
= new object ();
376 internal static object MouseDownEvent
= new object ();
377 internal static object MouseEnterEvent
= new object ();
378 internal static object MouseLeaveEvent
= new object ();
379 internal static object MouseMoveEvent
= new object ();
380 internal static object MouseUpEvent
= new object ();
381 internal static object FocusEvent
= new object ();
382 internal static object BlurEvent
= new object ();
383 internal static object CreateNewWindowEvent
= new object ();
384 internal static object AlertEvent
= new object ();
386 internal static object LoadStartedEvent
= new object ();
387 internal static object LoadCommitedEvent
= new object ();
388 internal static object ProgressChangedEvent
= new object ();
389 internal static object LoadFinishedEvent
= new object ();
391 internal static object LoadEvent
= new object ();
392 internal static object UnloadEvent
= new object ();
393 internal static object StatusChangedEvent
= new object ();
394 internal static object SecurityChangedEvent
= new object ();
395 internal static object ProgressEvent
= new object ();
396 internal static object ContextMenuEvent
= new object ();
398 internal static object NavigationRequestedEvent
= new object ();
400 public event NodeEventHandler KeyDown
402 add { Events.AddHandler (KeyDownEvent, value); }
403 remove { Events.RemoveHandler (KeyDownEvent, value); }
406 public event NodeEventHandler KeyPress
408 add { Events.AddHandler (KeyPressEvent, value); }
409 remove { Events.RemoveHandler (KeyPressEvent, value); }
411 public event NodeEventHandler KeyUp
413 add { Events.AddHandler (KeyUpEvent, value); }
414 remove { Events.RemoveHandler (KeyUpEvent, value); }
416 public event NodeEventHandler MouseClick
418 add { Events.AddHandler (MouseClickEvent, value); }
419 remove { Events.RemoveHandler (MouseClickEvent, value); }
421 public event NodeEventHandler MouseDoubleClick
423 add { Events.AddHandler (MouseDoubleClickEvent, value); }
424 remove { Events.RemoveHandler (MouseDoubleClickEvent, value); }
426 public event NodeEventHandler MouseDown
428 add { Events.AddHandler (MouseDownEvent, value); }
429 remove { Events.RemoveHandler (MouseDownEvent, value); }
431 public event NodeEventHandler MouseEnter
433 add { Events.AddHandler (MouseEnterEvent, value); }
434 remove { Events.RemoveHandler (MouseEnterEvent, value); }
436 public event NodeEventHandler MouseLeave
438 add { Events.AddHandler (MouseLeaveEvent, value); }
439 remove { Events.RemoveHandler (MouseLeaveEvent, value); }
441 public event NodeEventHandler MouseMove
443 add { Events.AddHandler (MouseMoveEvent, value); }
444 remove { Events.RemoveHandler (MouseMoveEvent, value); }
446 public event NodeEventHandler MouseUp
448 add { Events.AddHandler (MouseUpEvent, value); }
449 remove { Events.RemoveHandler (MouseUpEvent, value); }
451 public event EventHandler Focus
453 add { Events.AddHandler (FocusEvent, value); }
454 remove { Events.RemoveHandler (FocusEvent, value); }
456 public event EventHandler Blur
458 add { Events.AddHandler (BlurEvent, value); }
459 remove { Events.RemoveHandler (BlurEvent, value); }
461 public event CreateNewWindowEventHandler CreateNewWindow
463 add { Events.AddHandler (CreateNewWindowEvent, value); }
464 remove { Events.RemoveHandler (CreateNewWindowEvent, value); }
466 public event AlertEventHandler Alert
468 add { Events.AddHandler (AlertEvent, value); }
469 remove { Events.RemoveHandler (AlertEvent, value); }
471 public event EventHandler Loaded
473 add { Events.AddHandler (LoadEvent, value); }
474 remove { Events.RemoveHandler (LoadEvent, value); }
476 public event EventHandler Unloaded
478 add { Events.AddHandler (UnloadEvent, value); }
479 remove { Events.RemoveHandler (UnloadEvent, value); }
482 public event StatusChangedEventHandler StatusChanged
484 add { Events.AddHandler (StatusChangedEvent, value); }
485 remove { Events.RemoveHandler (StatusChangedEvent, value); }
488 public event SecurityChangedEventHandler SecurityChanged
490 add { Events.AddHandler (SecurityChangedEvent, value); }
491 remove { Events.RemoveHandler (SecurityChangedEvent, value); }
494 public event LoadStartedEventHandler LoadStarted
496 add { Events.AddHandler (LoadStartedEvent, value); }
497 remove { Events.RemoveHandler (LoadStartedEvent, value); }
500 public event LoadCommitedEventHandler LoadCommited
502 add { Events.AddHandler (LoadCommitedEvent, value); }
503 remove { Events.RemoveHandler (LoadCommitedEvent, value); }
506 public event ProgressChangedEventHandler ProgressChanged
508 add { Events.AddHandler (ProgressChangedEvent, value); }
509 remove { Events.RemoveHandler (ProgressChangedEvent, value); }
512 public event LoadFinishedEventHandler LoadFinished
514 add { Events.AddHandler (LoadFinishedEvent, value); }
515 remove { Events.RemoveHandler (LoadFinishedEvent, value); }
518 public event ContextMenuEventHandler ContextMenuShown
520 add { Events.AddHandler (ContextMenuEvent, value); }
521 remove { Events.RemoveHandler (ContextMenuEvent, value); }
524 public event NavigationRequestedEventHandler NavigationRequested
526 add { ContentListener.AddHandler (value); }
527 remove { ContentListener.RemoveHandler (value); }
530 internal static object GenericEvent
= new object ();
531 internal event EventHandler Generic
533 add { Events.AddHandler (GenericEvent, value); }
534 remove { Events.RemoveHandler (GenericEvent, value); }