~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric-updates

« back to all changes in this revision

Viewing changes to src/addins/AspNetAddIn/MonoDevelop.AspNet.Gui/BrowserLauncher.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-18 08:40:51 UTC
  • mfrom: (1.2.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090218084051-gh8m6ukvokbwj7cf
Tags: 1.9.2+dfsg-1ubuntu1
* Merge from Debian Experimental (LP: #330519), remaining Ubuntu changes:
  + debian/control:
    - Update for Gnome# 2.24
    - Add libmono-cairo1.0-cil to build-deps to fool pkg-config check

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// BrowserLauncher.cs
 
3
// 
 
4
// Author:
 
5
//   Michael Hutchinson <mhutchinson@novell.com>
 
6
// 
 
7
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
using System;
 
30
using System.Threading;
 
31
 
 
32
using MonoDevelop.Core;
 
33
 
 
34
namespace MonoDevelop.AspNet.Gui
 
35
{
 
36
        
 
37
        
 
38
        public static class BrowserLauncher
 
39
        {
 
40
                public static BrowserLauncherOperation LaunchWhenReady (string url, BrowserLaunchHandler browserHandler)
 
41
                {
 
42
                        return new BrowserLauncherOperation (url, browserHandler);
 
43
                }
 
44
                
 
45
                public static void LaunchDefaultBrowser (string url)
 
46
                {
 
47
                        MonoDevelop.Core.Gui.Services.PlatformService.ShowUrl (url);
 
48
                }
 
49
                
 
50
                public static BrowserLauncherOperation LaunchWhenReady (string url)
 
51
                {
 
52
                        return new BrowserLauncherOperation (url, LaunchDefaultBrowser);
 
53
                }
 
54
        }
 
55
        
 
56
        public delegate void BrowserLaunchHandler (string url);
 
57
        
 
58
        public class BrowserLauncherOperation : IAsyncOperation 
 
59
        {
 
60
                bool completed;
 
61
                bool successful;
 
62
                readonly string url;
 
63
                readonly BrowserLaunchHandler browserHandler;
 
64
                Thread launcherThread;
 
65
                Exception error;
 
66
                
 
67
                public BrowserLauncherOperation (string url, BrowserLaunchHandler browserHandler)
 
68
                {
 
69
                        if (browserHandler == null)
 
70
                                throw new ArgumentNullException ("browserHandler");
 
71
                        if (url == null)
 
72
                                throw new ArgumentNullException ("url");
 
73
                        
 
74
                        this.url = url;
 
75
                        this.browserHandler = browserHandler;
 
76
                        if (url.StartsWith ("http://")) {
 
77
                                launcherThread = new Thread (new ThreadStart (LaunchWebBrowser));
 
78
                                launcherThread.Start ();
 
79
                        } else {
 
80
                                browserHandler (url);
 
81
                                successful = true;
 
82
                                IsCompleted = true;
 
83
                        }
 
84
                }
 
85
                
 
86
                //confirm we can connect to server before opening browser; wait up to ten seconds
 
87
                void LaunchWebBrowser ()
 
88
                {
 
89
                        try {                           
 
90
                                //wait a bit for server to start
 
91
                                Thread.Sleep (2000);
 
92
                                
 
93
                                //try to contact web server several times, because server may take a while to start
 
94
                                int noOfRequests = 5;
 
95
                                int timeout = 8000; //ms
 
96
                                int wait = 1000; //ms
 
97
                                
 
98
                                for (int i = 0; i < noOfRequests; i++) {
 
99
                                        System.Net.WebRequest req = null;
 
100
                                        System.Net.WebResponse resp = null;
 
101
                                        
 
102
                                        try {
 
103
                                                req = System.Net.HttpWebRequest.Create (url);
 
104
                                                req.Timeout = timeout;
 
105
                                                resp = req.GetResponse ();
 
106
                                        } catch (System.Net.WebException exp) {
 
107
                                                
 
108
                                                // server has returned 404, 500 etc, which user will still want to see
 
109
                                                if (exp.Status == System.Net.WebExceptionStatus.ProtocolError) {
 
110
                                                        resp = exp.Response;
 
111
                                                        
 
112
                                                //final request has failed
 
113
                                                } else if (i >= (noOfRequests - 1)) {
 
114
                                                        string message = GettextCatalog.GetString ("Could not connect to webserver {0}", url);
 
115
                                                        throw new UserException (message, exp.ToString ());
 
116
                                                        
 
117
                                                //we still have requests to go, so cancel the current one and sleep for a bit
 
118
                                                } else {
 
119
                                                        req.Abort ();
 
120
                                                        Thread.Sleep (wait);
 
121
                                                        continue;
 
122
                                                }
 
123
                                        }
 
124
                                
 
125
                                        if (resp != null) {
 
126
                                                //TODO: a choice of browsers
 
127
                                                browserHandler (url);
 
128
                                                successful = true;
 
129
                                                break;
 
130
                                        }
 
131
                                }
 
132
                        } catch (ThreadAbortException) {
 
133
                        } catch (Exception ex) {
 
134
                                //don't want any exceptions leaking out the top of the thread, as they'd crash the runtime
 
135
                                LoggingService.LogError ("Unhandled error in browser launcher thread", ex);
 
136
                                error = ex;
 
137
                        }
 
138
                        IsCompleted = true;
 
139
                }
 
140
                
 
141
                public Exception Error {
 
142
                        get { return error; }
 
143
                }
 
144
 
 
145
                public void Cancel ()
 
146
                {
 
147
                        //FIXME: should we try something more graceful than a thread abort? Tricky with the 2s waits...
 
148
                        if (launcherThread != null && launcherThread.IsAlive && launcherThread.ThreadState != ThreadState.AbortRequested)
 
149
                                launcherThread.Abort ();
 
150
                }
 
151
 
 
152
                public void WaitForCompleted ()
 
153
                {
 
154
                        if (launcherThread != null && launcherThread.IsAlive)
 
155
                                launcherThread.Join ();
 
156
                }
 
157
                
 
158
                public event OperationHandler Completed {
 
159
                        add {
 
160
                                bool raiseNow = false;
 
161
                                lock (this) {
 
162
                                        if (IsCompleted)
 
163
                                                raiseNow = true;
 
164
                                        else
 
165
                                                completedEvent += value;
 
166
                                }
 
167
                                if (raiseNow)
 
168
                                        value (this);
 
169
                        }
 
170
                        remove {
 
171
                                lock (this) {
 
172
                                        completedEvent -= value;
 
173
                                }
 
174
                        }
 
175
                }
 
176
        
 
177
                public bool Success {
 
178
                        get { return successful; }
 
179
                }
 
180
                
 
181
                public bool SuccessWithWarnings {
 
182
                        get { return false; }
 
183
                }
 
184
                
 
185
                public bool IsCompleted {
 
186
                        get { return completed; }
 
187
                        private set {
 
188
                                if (!value)
 
189
                                        throw new InvalidOperationException ();
 
190
                                completed = true;
 
191
                                if (completedEvent != null)
 
192
                                        completedEvent (this);
 
193
                        }
 
194
                }
 
195
                
 
196
                event OperationHandler completedEvent;
 
197
        }
 
198
}