Same thing as in HEAD.
[mono.git] / mcs / class / System.Web / System.Web / HttpBrowserCapabilities.cs
1 // 
2 // System.Web.HttpBrowserCapabilities
3 //
4 // Authors:
5 //   Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (c) 2003 Novell, Inc. (http://www.novell.com)
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31 using System;
32 using System.Collections;
33 using System.Web.Configuration;
34 using System.Web.UI;
35
36 namespace System.Web
37 {
38         public class HttpBrowserCapabilities : HttpCapabilitiesBase
39         {
40                 const int HaveActiveXControls = 1;
41                 const int HaveAOL = 2;
42                 const int HaveBackGroundSounds = 3;
43                 const int HaveBeta = 4;
44                 const int HaveBrowser = 5;
45                 const int HaveCDF = 6;
46                 const int HaveClrVersion = 7;
47                 const int HaveCookies = 8;
48                 const int HaveCrawler = 9;
49                 const int HaveEcmaScriptVersion = 10;
50                 const int HaveFrames = 11;
51                 const int HaveJavaApplets = 12;
52                 const int HaveJavaScript = 13;
53                 const int HaveMajorVersion = 14;
54                 const int HaveMinorVersion = 15;
55                 const int HaveMSDomVersion = 16;
56                 const int HavePlatform = 17;
57                 const int HaveTables = 18;
58                 const int HaveTagWriter = 19;
59                 const int HaveVBScript = 20;
60                 const int HaveVersion = 21;
61                 const int HaveW3CDomVersion = 22;
62                 const int HaveWin16 = 23;
63                 const int HaveWin32 = 24;
64
65                 int flags;
66                 bool activeXControls;
67                 bool aol;
68                 bool backgroundSounds;
69                 bool beta;
70                 string browser;
71                 bool cdf;
72                 Version clrVersion;
73                 bool cookies;
74                 bool crawler;
75                 Version ecmaScriptVersion;
76                 bool frames;
77                 bool javaApplets;
78                 bool javaScript;
79                 int majorVersion;
80                 double minorVersion;
81                 Version msDomVersion;
82                 string platform;
83                 bool tables;
84                 Type tagWriter;
85                 bool vbscript;
86                 string version;
87                 Version w3CDomVersion;
88                 bool win16;
89                 Version [] clrVersions;
90                 internal string useragent;
91
92                 public HttpBrowserCapabilities () { }
93
94                 public bool ActiveXControls {
95                         get {
96                                 if (!Get (HaveActiveXControls)) {
97                                         Set (HaveActiveXControls);
98                                         activeXControls = ReadBoolean ("activexcontrols", false);
99                                 }
100
101                                 return activeXControls;
102                         }
103                 }
104
105                 public bool AOL {
106                         get {
107                                 if (!Get (HaveAOL)) {
108                                         Set (HaveAOL);
109                                         aol = ReadBoolean ("aol", false);
110                                 }
111
112                                 return aol;
113                         }
114                 }
115
116                 public bool BackgroundSounds {
117                         get {
118                                 if (!Get (HaveBackGroundSounds)) {
119                                         Set (HaveBackGroundSounds);
120                                         backgroundSounds = ReadBoolean ("backgroundsounds", false);
121                                 }
122
123                                 return backgroundSounds;
124                         }
125                 }
126
127                 public bool Beta {
128                         get {
129                                 if (!Get (HaveBeta)) {
130                                         Set (HaveBeta);
131                                         beta = ReadBoolean ("beta", false);
132                                 }
133
134                                 return beta;
135                         }
136                 }
137
138                 public string Browser {
139                         get {
140                                 if (!Get (HaveBrowser)) {
141                                         Set (HaveBrowser);
142                                         browser = this ["browser"];
143                                         if (browser == null)
144                                                 browser = "Unknown";
145                                 }
146
147                                 return browser;
148                         }
149                 }
150
151                 public bool CDF {
152                         get {
153                                 if (!Get (HaveCDF)) {
154                                         Set (HaveCDF);
155                                         cdf = ReadBoolean ("cdf", false);
156                                 }
157
158                                 return cdf;
159                         }
160                 }
161
162                 public Version ClrVersion {
163                         get {
164                                 if (clrVersion == null)
165                                         InternalGetClrVersions ();
166
167                                 return clrVersion;
168                         }
169                 }
170
171                 public bool Cookies {
172                         get {
173                                 if (!Get (HaveCookies)) {
174                                         Set (HaveCookies);
175                                         cookies = ReadBoolean ("cookies", false);
176                                 }
177
178                                 return cookies;
179                         }
180                 }
181
182                 public bool Crawler {
183                         get {
184                                 if (!Get (HaveCrawler)) {
185                                         Set (HaveCrawler);
186                                         crawler = ReadBoolean ("crawler", false);
187                                 }
188
189                                 return crawler;
190                         }
191                 }
192
193                 [MonoTODO]
194                 public Version EcmaScriptVersion {
195                         get {
196                                 return new Version (0, 0);
197                         }
198                 }
199
200                 public bool Frames {
201                         get {
202                                 if (!Get (HaveFrames)) {
203                                         Set (HaveFrames);
204                                         frames = ReadBoolean ("frames", false);
205                                 }
206
207                                 return frames;
208                         }
209                 }
210
211                 public bool JavaApplets {
212                         get {
213                                 if (!Get (HaveJavaApplets)) {
214                                         Set (HaveJavaApplets);
215                                         javaApplets = ReadBoolean ("javaapplets", false);
216                                 }
217
218                                 return javaApplets;
219                         }
220                 }
221
222                 public bool JavaScript {
223                         get {
224                                 if (!Get (HaveJavaScript)) {
225                                         Set (HaveJavaScript);
226                                         javaScript = ReadBoolean ("javascript", false);
227                                 }
228
229                                 return javaScript;
230                         }
231                 }
232
233                 public int MajorVersion {
234                         get {
235                                 if (!Get (HaveMajorVersion)) {
236                                         Set (HaveMajorVersion);
237                                         majorVersion = ReadInt32 ("majorver", 0);
238                                 }
239
240                                 return majorVersion;
241                         }
242                 }
243
244                 public double MinorVersion {
245                         get {
246                                 if (!Get (HaveMinorVersion)) {
247                                         Set (HaveMinorVersion);
248                                         minorVersion = ReadDouble ("minorver", 0);
249                                 }
250
251                                 return minorVersion;
252                         }
253                 }
254
255                 [MonoTODO]
256                 public Version MSDomVersion {
257                         get {
258                                 return new Version (0, 0);
259                         }
260                 }
261
262                 public string Platform {
263                         get {
264                                 if (!Get (HavePlatform)) {
265                                         Set (HavePlatform);
266                                         platform = this ["platform"];
267                                         if (platform == null)
268                                                 platform = "";
269                                 }
270
271                                 return platform;
272                         }
273                 }
274
275                 public bool Tables {
276                         get {
277                                 if (!Get (HaveTables)) {
278                                         Set (HaveTables);
279                                         tables = ReadBoolean ("tables", false);
280                                 }
281
282                                 return tables;
283                         }
284                 }
285
286                 [MonoTODO]
287                 public Type TagWriter {
288                         get {
289                                 return typeof (HtmlTextWriter);
290                         }
291                 }
292
293                 public string Type {
294                         get {
295                                 return Browser + MajorVersion;
296                         }
297                 }
298
299                 public bool VBScript {
300                         get {
301                                 if (!Get (HaveVBScript)) {
302                                         Set (HaveVBScript);
303                                         vbscript = ReadBoolean ("vbscript", false);
304                                 }
305
306                                 return vbscript;
307                         }
308                 }
309
310                 public string Version {
311                         get {
312                                 if (!Get (HaveVersion)) {
313                                         Set (HaveVersion);
314                                         version = this ["version"];
315                                         if (version == null)
316                                                 version = "";
317                                 }
318
319                                 return version;
320                         }
321                 }
322
323                 [MonoTODO]
324                 public Version W3CDomVersion {
325                         get {
326                                 return new Version (0, 0);
327                         }
328                 }
329
330                 public bool Win16 {
331                         get {
332                                 if (!Get (HaveWin16)) {
333                                         Set (HaveWin16);
334                                         win16 = ReadBoolean ("win16", false);
335                                 }
336
337                                 return win16;
338                         }
339                 }
340
341                 public bool Win32 {
342                         get {
343                                 return !Win16;
344                         }
345                 }
346
347 #if NET_1_1
348                 public Version [] GetClrVersions ()
349                 {
350                         if (clrVersions == null)
351                                 InternalGetClrVersions ();
352
353                         return clrVersions;
354                 }
355 #endif
356
357                 void InternalGetClrVersions ()
358                 {
359                         char [] anychars = new char [] { ';', ')' };
360                         string s = useragent;
361                         ArrayList list = null;
362                         int idx;
363                         while ((idx = s.IndexOf (".NET CLR ")) != -1) {
364                                 int end = s.IndexOfAny (anychars, idx + 9);
365                                 if (end == -1)
366                                         break;
367
368                                 string ver = s.Substring (idx + 9, end - idx - 9);
369                                 Version v = null;
370                                 try {
371                                         v = new Version (ver);
372                                         if (clrVersion == null || v > clrVersion)
373                                                 clrVersion = v;
374
375                                         if (list == null)
376                                                 list = new ArrayList (4);
377
378                                         list.Add (v);
379                                 } catch { }
380                                 s = s.Substring (idx + 9);
381                         }
382                         
383                         if (list == null || list.Count == 0) {
384                                 clrVersion = new Version ();
385                                 clrVersions = new Version [] { clrVersion };
386                         } else {
387                                 list.Sort ();
388                                 clrVersions = (Version []) list.ToArray (typeof (Version));
389                         }
390                 }
391
392                 bool ReadBoolean (string key, bool dflt)
393                 {
394                         string v = this [key];
395                         if (v == null)
396                                 return dflt;
397
398                         return (v == "True");
399                 }
400
401                 int ReadInt32 (string key, int dflt)
402                 {
403                         string v = this [key];
404                         if (v == null)
405                                 return dflt;
406
407                         try {
408                                 return Int32.Parse (v);
409                         } catch {
410                                 return dflt;
411                         }
412                 }
413
414                 double ReadDouble (string key, double dflt)
415                 {
416                         string v = this [key];
417                         if (v == null)
418                                 return dflt;
419
420                         try {
421                                 return Double.Parse (v);
422                         } catch {
423                                 return dflt;
424                         }
425                 }
426
427                 bool Get (int idx)
428                 {
429                         return (flags & (1 << idx)) != 0;
430                 }
431
432                 void Set (int idx)
433                 {
434                         flags |= (1 << idx);
435                 }
436         }
437 }
438