File: WebBrowser.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (706 lines) | stat: -rw-r--r-- 18,377 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2007, 2008 Novell, Inc.
//
// Authors:
//	Andreia Gaita	<avidigal@novell.com>


using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Windows.Forms;
using System.IO;
using System.Drawing;

namespace System.Windows.Forms
{
	[DefaultProperty ("Url")]
	[DefaultEvent ("DocumentCompleted")]
	[Docking (DockingBehavior.AutoDock)]
	[ClassInterface (ClassInterfaceType.AutoDispatch)]
	[ComVisible(true)]
	[Designer("System.Windows.Forms.Design.WebBrowserDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
	public class WebBrowser : WebBrowserBase
	{
		bool allowNavigation; // if this is true, no other navigation is allowed
		
		bool allowWebBrowserDrop = true;
		bool isWebBrowserContextMenuEnabled;
		object objectForScripting;
		bool webBrowserShortcutsEnabled;
		bool scrollbarsEnabled = true;
		
		WebBrowserReadyState readyState;

		HtmlDocument document;
		
		WebBrowserEncryptionLevel securityLevel;

		Stream data;
		bool isStreamSet;

		string url;

		#region Public Properties

		[DefaultValue(true)]
		public bool AllowNavigation {
			get { return allowNavigation; }
			set { allowNavigation = value; }
		}

		[DefaultValue (true)]
		public bool AllowWebBrowserDrop {
			get { return allowWebBrowserDrop; }
			set { allowWebBrowserDrop = value; }
		}

		[BrowsableAttribute(false)]
		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
		public bool CanGoBack {
			get { return this.WebHost.Navigation.CanGoBack; }
		}

		[BrowsableAttribute(false)]
		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
		public bool CanGoForward {
			get { return this.WebHost.Navigation.CanGoForward; }
		}

		[BrowsableAttribute(false)]
		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
		public HtmlDocument Document {
			get {
				if (document == null && documentReady)
					document = new HtmlDocument (this, this.WebHost);
				return document; 
			}
		}

		[BrowsableAttribute(false)]
		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
		public Stream DocumentStream {
			get {
				if (WebHost.Document == null || WebHost.Document.DocumentElement == null)
					return null;

				return null; //WebHost.Document.DocumentElement.ContentStream;
			}
			set { 
				if (this.allowNavigation)
					return;

				this.Url = new Uri ("about:blank");

				data = value;
				isStreamSet = true;
			}
		}

		[BrowsableAttribute(false)]
		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
		public string DocumentText {
			get { 
				if (WebHost.Document == null || WebHost.Document.DocumentElement == null)
					return String.Empty;
				return WebHost.Document.DocumentElement.OuterHTML;
			}
			set {
				if (WebHost.Document != null && WebHost.Document.DocumentElement != null)
					WebHost.Document.DocumentElement.OuterHTML = value;
			}
		}

		[BrowsableAttribute(false)]
		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
		public string DocumentTitle {
			get {
				if (document != null)
					return document.Title;
				return String.Empty;
			}
		}

		[BrowsableAttribute(false)]
		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
		public string DocumentType {
			get {
				if (document != null)
					return document.DocType;
				return String.Empty;
			}
		}

		[BrowsableAttribute(false)]
		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
		public WebBrowserEncryptionLevel EncryptionLevel {
			get { return securityLevel; }
		}

		public override bool Focused {
			get { return base.Focused; }
		}

		[BrowsableAttribute(false)]
		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
		public bool IsBusy {
			get { return !documentReady; }
		}

		[BrowsableAttribute(false)]
		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
		public bool IsOffline {
			get { return WebHost.Offline; }
		}

		[MonoTODO ("Stub, not implemented")]
		[DefaultValue(true)]
		public bool IsWebBrowserContextMenuEnabled {
			get { return isWebBrowserContextMenuEnabled; }
			set { isWebBrowserContextMenuEnabled = value; }
		}

		[MonoTODO ("Stub, not implemented")]
		[BrowsableAttribute(false)]
		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
		public object ObjectForScripting {
			get { return objectForScripting; }
			set { objectForScripting = value; }
		}

		[BrowsableAttribute(false)]
		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
		public WebBrowserReadyState ReadyState {
			get { return readyState; }
		}

		[DefaultValue(false)]
		public bool ScriptErrorsSuppressed {
			get { return SuppressDialogs; }
			set { SuppressDialogs = value; }
		}
		
		[DefaultValue(true)]
		public bool ScrollBarsEnabled {
			get { return scrollbarsEnabled; }
			set {
				scrollbarsEnabled = value;
				if (document != null)
					SetScrollbars ();
			}
		}

		[BrowsableAttribute(false)]
		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
		public virtual string StatusText {
			get { return base.status; }
		}

		[BindableAttribute(true)] 
		[DefaultValue(null)]
		[TypeConverter(typeof(WebBrowserUriTypeConverter))]
		public Uri Url {
			get {
				if (url != null)
					return new Uri (url);
				if (WebHost.Document != null && WebHost.Document.Url != null)
					return new Uri (WebHost.Document.Url);
				return null;
			}
			set {
				url = null;
				this.Navigate (value); 
			}
		}

		[BrowsableAttribute(false)]
		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
		public Version Version {
			get { 
				Assembly ass = WebHost.GetType().Assembly;
				return ass.GetName().Version;
			}
		}

		[MonoTODO ("Stub, not implemented")]
		[DefaultValue(true)]
		public bool WebBrowserShortcutsEnabled {
			get { return webBrowserShortcutsEnabled; }
			set { webBrowserShortcutsEnabled = value; }
		}
		
		protected override Size DefaultSize {
			get { return base.DefaultSize; }
		}

		[BrowsableAttribute(false)]
		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
		[EditorBrowsable (EditorBrowsableState.Never)]
		public new Padding Padding {
			get { return base.Padding; }
			set { base.Padding = value; }
		}
		
		#endregion

		[MonoTODO ("WebBrowser control is only supported on Linux/Windows. No support for OSX.")]
		public WebBrowser ()
		{
		}

		#region Public Methods

		public bool GoBack ()
		{
			documentReady = false;
			document = null;
			return WebHost.Navigation.Back ();
		}

		public bool GoForward ()
		{
			documentReady = false;
			document = null;
			return WebHost.Navigation.Forward ();
		}

		public void GoHome ()
		{
			documentReady = false;
			document = null;
			WebHost.Navigation.Home ();
		}

		public void Navigate (string urlString)
		{
			documentReady = false;
			document = null;
			WebHost.Navigation.Go (urlString);
		}

		public void Navigate (Uri url)
		{
			documentReady = false;
			document = null;
			WebHost.Navigation.Go (url.ToString ());
		}

		public void Navigate (string urlString, bool newWindow)
		{
			documentReady = false;
			document = null;
			WebHost.Navigation.Go (urlString);
		}

		public void Navigate (string urlString, string targetFrameName)
		{
			documentReady = false;
			document = null;
			WebHost.Navigation.Go (urlString);
		}

		public void Navigate (Uri url, bool newWindow)
		{
			documentReady = false;
			document = null;
			WebHost.Navigation.Go (url.ToString ());
		}

		public void Navigate (Uri url, string targetFrameName)
		{
			documentReady = false;
			document = null;
			WebHost.Navigation.Go (url.ToString ());
		}

		public void Navigate (string urlString, string targetFrameName, byte[] postData, string additionalHeaders)
		{
			documentReady = false;
			document = null;
			WebHost.Navigation.Go (urlString);
		}

		public void Navigate (Uri url, string targetFrameName, byte[] postData, string additionalHeaders)
		{
			documentReady = false;
			document = null;
			WebHost.Navigation.Go (url.ToString ());
		}

		public override void Refresh ()
		{
			Refresh (WebBrowserRefreshOption.IfExpired);
		}

		public void Refresh (WebBrowserRefreshOption opt)
		{
			documentReady = false;
			document = null;
			switch (opt) {
				case WebBrowserRefreshOption.Normal:
					WebHost.Navigation.Reload (Mono.WebBrowser.ReloadOption.Proxy);
					break;
				case WebBrowserRefreshOption.IfExpired:
					WebHost.Navigation.Reload (Mono.WebBrowser.ReloadOption.None);
					break;
				case WebBrowserRefreshOption.Completely:
					WebHost.Navigation.Reload (Mono.WebBrowser.ReloadOption.Full);
					break;
			}
		}

		public void Stop ()
		{
			WebHost.Navigation.Stop ();
		}

		public void GoSearch ()
		{
			string url = "http://www.google.com";
			try {
				Microsoft.Win32.RegistryKey reg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey (@"Software\Microsoft\Internet Explorer\Main\Search Page");
				if (reg != null) {
					object searchUrl = reg.GetValue ("Default_Search_URL");
					if (searchUrl != null && searchUrl is string) {
						Uri uri;
						if (System.Uri.TryCreate (searchUrl as string, UriKind.Absolute, out uri))
							url = uri.ToString ();
					}
				}
			} catch {
			}
			Navigate (url);
		}

		public void Print ()
		{
			throw new NotImplementedException ();
		}

		public void ShowPageSetupDialog ()
		{
			throw new NotImplementedException ();
		}

		public void ShowPrintDialog()
		{
			throw new NotImplementedException ();
		}
		
		public void ShowPrintPreviewDialog()
		{
			throw new NotImplementedException ();
		}
		
		public void ShowPropertiesDialog()
		{
			throw new NotImplementedException ();
		}
		
		public void ShowSaveAsDialog()
		{
			throw new NotImplementedException ();
		}

		#endregion

		#region Protected Overridden Methods

		[MonoTODO ("Stub, not implemented")]
		protected override void AttachInterfaces (object nativeActiveXObject)
		{
			base.AttachInterfaces (nativeActiveXObject);
		}

		[MonoTODO ("Stub, not implemented")]
		protected override void CreateSink ()
		{
			base.CreateSink ();
		}

		[MonoTODO ("Stub, not implemented")]
		protected override WebBrowserSiteBase CreateWebBrowserSiteBase ()
		{
			return base.CreateWebBrowserSiteBase ();
		}

		[MonoTODO ("Stub, not implemented")]
		protected override void DetachInterfaces ()
		{
			base.DetachInterfaces ();
		}

		[MonoTODO ("Stub, not implemented")]
		protected override void DetachSink ()
		{
			base.DetachSink ();
		}

		protected override void Dispose (bool disposing)
		{
			base.Dispose (disposing);
		}

		protected override void WndProc (ref Message m)
		{
			base.WndProc (ref m);
		}

		#endregion

		#region OnXXX methods

		protected virtual void OnCanGoBackChanged(EventArgs e)
		{
			if (CanGoBackChanged != null)
				CanGoBackChanged (this, e);
		}

		protected virtual void OnCanGoForwardChanged(EventArgs e)
		{			
			if (CanGoForwardChanged != null)
				CanGoForwardChanged (this, e);
		}

		protected virtual void OnDocumentCompleted(WebBrowserDocumentCompletedEventArgs e)
		{
			if (DocumentCompleted != null)
				DocumentCompleted (this, e);
		}

		protected virtual void OnDocumentTitleChanged(EventArgs e)
		{
			if (DocumentTitleChanged != null)
				DocumentTitleChanged (this, e);
		}

		protected virtual void OnEncryptionLevelChanged(EventArgs e)
		{
			if (EncryptionLevelChanged != null)
				EncryptionLevelChanged (this, e);
		}

		protected virtual void OnFileDownload(EventArgs e)
		{
			if (FileDownload != null)
				FileDownload (this, e);
		}

		protected virtual void OnNavigated(WebBrowserNavigatedEventArgs e)
		{
			if (Navigated != null)
				Navigated (this, e);
		}

		protected virtual void OnNavigating(WebBrowserNavigatingEventArgs e)
		{
			if (Navigating != null)
				Navigating (this, e);
		}

		protected virtual void OnNewWindow(CancelEventArgs e)
		{
			if (NewWindow != null)
				NewWindow (this, e);
		}

		protected virtual void OnProgressChanged(WebBrowserProgressChangedEventArgs e)
		{
			if (ProgressChanged != null)
				ProgressChanged (this, e);
		}

		protected virtual void OnStatusTextChanged(EventArgs e)
		{
			if (StatusTextChanged != null)
				StatusTextChanged (this, e);
		}

		#endregion

		#region Events	
		[BrowsableAttribute(false)]
		public event EventHandler CanGoBackChanged;

		[BrowsableAttribute(false)]
		public event EventHandler CanGoForwardChanged;

		public event WebBrowserDocumentCompletedEventHandler DocumentCompleted;

		[BrowsableAttribute(false)]
		public event EventHandler DocumentTitleChanged;

		[BrowsableAttribute(false)]
		public event EventHandler EncryptionLevelChanged;

		public event EventHandler FileDownload;

		public event WebBrowserNavigatedEventHandler Navigated;

		public event WebBrowserNavigatingEventHandler Navigating;

		public event CancelEventHandler NewWindow;
		
		public event WebBrowserProgressChangedEventHandler ProgressChanged;

		[BrowsableAttribute(false)]
		public event EventHandler StatusTextChanged;

#pragma warning disable 0067
		[MonoTODO]
		[Browsable (false)]
		[EditorBrowsable (EditorBrowsableState.Never)]
		[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
		public new event EventHandler PaddingChanged;
#pragma warning restore 0067

		#endregion

		#region Internal

		internal override bool OnNewWindowInternal ()
		{
			CancelEventArgs c = new CancelEventArgs ();
			OnNewWindow (c);
			return c.Cancel;
		}
		
		internal override void OnWebHostLoadStarted (object sender, Mono.WebBrowser.LoadStartedEventArgs e)
		{
			documentReady = false;
			document = null;
			readyState = WebBrowserReadyState.Loading;
			WebBrowserNavigatingEventArgs n = new WebBrowserNavigatingEventArgs (new Uri (e.Uri), e.FrameName);
			OnNavigating (n);
		}

		internal override void OnWebHostLoadCommited (object sender, Mono.WebBrowser.LoadCommitedEventArgs e)
		{
			readyState = WebBrowserReadyState.Loaded;
			url = e.Uri;
			SetScrollbars ();
			WebBrowserNavigatedEventArgs n = new WebBrowserNavigatedEventArgs (new Uri (e.Uri));
			OnNavigated (n);
		}
		internal override void OnWebHostProgressChanged (object sender, Mono.WebBrowser.ProgressChangedEventArgs e)
		{
			readyState = WebBrowserReadyState.Interactive;
			WebBrowserProgressChangedEventArgs n = new WebBrowserProgressChangedEventArgs (e.Progress, e.MaxProgress);
			OnProgressChanged (n);
		}

		internal override void OnWebHostLoadFinished (object sender, Mono.WebBrowser.LoadFinishedEventArgs e)
		{
			url = null;
			documentReady = true;
			readyState = WebBrowserReadyState.Complete;
			if (isStreamSet) {
				byte[] buffer = new byte [data.Length];
				long len = data.Length;
				int count = 0;
				data.Position = 0;
				do {
					count = data.Read (buffer, (int) data.Position, (int) (len - data.Position));
				} while (count > 0);
				WebHost.Render (buffer);
				data = null;
				isStreamSet = false;
			}
			SetScrollbars ();
			WebBrowserDocumentCompletedEventArgs n = new WebBrowserDocumentCompletedEventArgs (new Uri (e.Uri));
			OnDocumentCompleted (n);
		}
		
		internal override void OnWebHostSecurityChanged (object sender, Mono.WebBrowser.SecurityChangedEventArgs e)
		{
			switch (e.State) {
				case Mono.WebBrowser.SecurityLevel.Insecure:
					securityLevel = WebBrowserEncryptionLevel.Insecure;
				break;
				case Mono.WebBrowser.SecurityLevel.Mixed:
					securityLevel = WebBrowserEncryptionLevel.Mixed;
				break;
				case Mono.WebBrowser.SecurityLevel.Secure:
					securityLevel = WebBrowserEncryptionLevel.Bit56;
				break;
			}
		}
		
		internal override void OnWebHostContextMenuShown (object sender, Mono.WebBrowser.ContextMenuEventArgs e) {
			if (!isWebBrowserContextMenuEnabled)
				return;
					
            ContextMenu menu = new ContextMenu();
                        
			MenuItem item = new MenuItem("Back", delegate { 
				GoBack(); 
			});
			item.Enabled = this.CanGoBack;
			menu.MenuItems.Add (item);
			
			item = new MenuItem("Forward", delegate { 
				GoForward(); 
			});
			item.Enabled = this.CanGoForward;
			menu.MenuItems.Add (item);
			
			item = new MenuItem("Refresh", delegate { 
				Refresh (); 
			});
			menu.MenuItems.Add (item);
            
            menu.MenuItems.Add (new MenuItem ("-"));
            
            menu.Show(this, PointToClient(MousePosition));
		}

		internal override void OnWebHostStatusChanged (object sender, Mono.WebBrowser.StatusChangedEventArgs e) {
			base.status = e.Message;
			OnStatusTextChanged (null);
		}
		
		#endregion


		void SetScrollbars () {
			//if (!scrollbarsEnabled)
			//        WebHost.ExecuteScript ("document.body.style.overflow='hidden';");
			//else
			//        WebHost.ExecuteScript ("document.body.style.overflow='auto';");
		}

		[MonoTODO ("Stub, not implemented")]
		[ComVisible (false)]
		protected class WebBrowserSite : WebBrowserSiteBase
		{
			[MonoTODO ("Stub, not implemented")]
			public WebBrowserSite (WebBrowser host)
				: base ()
			{
			}
		}
	}

	internal class WebBrowserUriTypeConverter : UriTypeConverter
	{
	}
}