~ubuntu-branches/ubuntu/lucid/monodevelop/lucid

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2010-01-10 14:25:59 UTC
  • mfrom: (1.2.5 upstream) (1.3.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100110142559-sorji5exvk9tyknr
Tags: 2.2+dfsg-2
* debian/rules/remove_support_for_non_debian_functionality.patch:
  + Also fix monodevelop-core-addins.pc to remove links to the
    addins we remove in Debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
27
//
28
28
 
 
29
using MonoDevelop.Core;
 
30
using MonoDevelop.Core.Gui;
 
31
using MonoDevelop.Core.ProgressMonitoring;
 
32
using MonoDevelop.Core.Execution;
29
33
using System;
30
 
using System.Threading;
31
34
 
32
 
using MonoDevelop.Core;
33
35
 
34
36
namespace MonoDevelop.AspNet.Gui
35
37
{
36
38
        
37
 
        
38
39
        public static class BrowserLauncher
39
40
        {
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.DesktopService.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) {
 
41
                public static IProcessAsyncOperation LaunchDefaultBrowser (string url)
 
42
                {
 
43
                        try {
 
44
                                DesktopService.ShowUrl (url);
 
45
                                return NullProcessAsyncOperation.Success;
133
46
                        } 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;
 
47
                                MessageService.ShowError (GettextCatalog.GetString ("Error launching web browser"), ex.ToString ());
 
48
                                return NullProcessAsyncOperation.Failure;
 
49
                        }
 
50
                }
197
51
        }
 
52
 
198
53
}