svn path=/branches/mono-1-1-9/mcs/; revision=51206
[mono.git] / mcs / class / System.Web / System.Web / BrowserCapabilities.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 #if NET_2_0
37 namespace System.Web.Configuration {
38         public partial class HttpCapabilitiesBase
39 #else
40
41 namespace System.Web {
42         public partial class HttpBrowserCapabilities : HttpCapabilitiesBase
43 #endif
44         {
45                 const int HaveActiveXControls = 1;
46                 const int HaveAOL = 2;
47                 const int HaveBackGroundSounds = 3;
48                 const int HaveBeta = 4;
49                 const int HaveBrowser = 5;
50                 const int HaveCDF = 6;
51                 //const int HaveClrVersion = 7;
52                 const int HaveCookies = 8;
53                 const int HaveCrawler = 9;
54                 const int HaveEcmaScriptVersion = 10;
55                 const int HaveFrames = 11;
56                 const int HaveJavaApplets = 12;
57                 const int HaveJavaScript = 13;
58                 const int HaveMajorVersion = 14;
59                 const int HaveMinorVersion = 15;
60                 //const int HaveMSDomVersion = 16;
61                 const int HavePlatform = 17;
62                 const int HaveTables = 18;
63                 //const int HaveTagWriter = 19;
64                 const int HaveVBScript = 20;
65                 const int HaveVersion = 21;
66                 const int HaveW3CDomVersion = 22;
67                 const int HaveWin16 = 23;
68                 const int HaveWin32 = 24;
69
70                 int flags;
71                 bool activeXControls;
72                 bool aol;
73                 bool backgroundSounds;
74                 bool beta;
75                 string browser;
76                 bool cdf;
77                 Version clrVersion;
78                 bool cookies;
79                 bool crawler;
80                 Version ecmaScriptVersion;
81                 bool frames;
82                 bool javaApplets;
83                 bool javaScript;
84                 int majorVersion;
85                 double minorVersion;
86                 //Version msDomVersion;
87                 string platform;
88                 bool tables;
89                 //Type tagWriter;
90                 bool vbscript;
91                 string version;
92                 Version w3CDomVersion;
93                 bool win16;
94                 bool win32;
95                 Version [] clrVersions;
96                 internal string useragent;
97
98                 public bool ActiveXControls {
99                         get {
100                                 if (!Get (HaveActiveXControls)) {
101                                         Set (HaveActiveXControls);
102                                         activeXControls = ReadBoolean ("activexcontrols", false);
103                                 }
104
105                                 return activeXControls;
106                         }
107                 }
108
109                 public bool AOL {
110                         get {
111                                 if (!Get (HaveAOL)) {
112                                         Set (HaveAOL);
113                                         aol = ReadBoolean ("aol", false);
114                                 }
115
116                                 return aol;
117                         }
118                 }
119
120                 public bool BackgroundSounds {
121                         get {
122                                 if (!Get (HaveBackGroundSounds)) {
123                                         Set (HaveBackGroundSounds);
124                                         backgroundSounds = ReadBoolean ("backgroundsounds", false);
125                                 }
126
127                                 return backgroundSounds;
128                         }
129                 }
130
131                 public bool Beta {
132                         get {
133                                 if (!Get (HaveBeta)) {
134                                         Set (HaveBeta);
135                                         beta = ReadBoolean ("beta", false);
136                                 }
137
138                                 return beta;
139                         }
140                 }
141
142                 public string Browser {
143                         get {
144                                 if (!Get (HaveBrowser)) {
145                                         Set (HaveBrowser);
146                                         browser = this ["browser"];
147                                         if (browser == null)
148                                                 browser = "Unknown";
149                                 }
150
151                                 return browser;
152                         }
153                 }
154
155                 public bool CDF {
156                         get {
157                                 if (!Get (HaveCDF)) {
158                                         Set (HaveCDF);
159                                         cdf = ReadBoolean ("cdf", false);
160                                 }
161
162                                 return cdf;
163                         }
164                 }
165
166                 public Version ClrVersion {
167                         get {
168                                 if (clrVersion == null)
169                                         InternalGetClrVersions ();
170
171                                 return clrVersion;
172                         }
173                 }
174
175                 public bool Cookies {
176                         get {
177                                 if (!Get (HaveCookies)) {
178                                         Set (HaveCookies);
179                                         cookies = ReadBoolean ("cookies", false);
180                                 }
181
182                                 return cookies;
183                         }
184                 }
185
186                 public bool Crawler {
187                         get {
188                                 if (!Get (HaveCrawler)) {
189                                         Set (HaveCrawler);
190                                         crawler = ReadBoolean ("crawler", false);
191                                 }
192
193                                 return crawler;
194                         }
195                 }
196
197                 public Version EcmaScriptVersion {
198                         get {
199                                 if (!Get (HaveEcmaScriptVersion)) {
200                                         string ver_str;
201                                         Set (HaveEcmaScriptVersion);
202                                         ver_str = this ["ecmascriptversion"];
203                                         if (ver_str == null)
204                                                 ecmaScriptVersion = new Version (0, 0);
205                                         else
206                                                 ecmaScriptVersion = new Version (ver_str);
207                                 }
208
209                                 return ecmaScriptVersion;
210                         }
211                 }
212
213                 public bool Frames {
214                         get {
215                                 if (!Get (HaveFrames)) {
216                                         Set (HaveFrames);
217                                         frames = ReadBoolean ("frames", false);
218                                 }
219
220                                 return frames;
221                         }
222                 }
223
224                 public bool JavaApplets {
225                         get {
226                                 if (!Get (HaveJavaApplets)) {
227                                         Set (HaveJavaApplets);
228                                         javaApplets = ReadBoolean ("javaapplets", false);
229                                 }
230
231                                 return javaApplets;
232                         }
233                 }
234
235                 public bool JavaScript {
236                         get {
237                                 if (!Get (HaveJavaScript)) {
238                                         Set (HaveJavaScript);
239                                         javaScript = ReadBoolean ("javascript", false);
240                                 }
241
242                                 return javaScript;
243                         }
244                 }
245
246                 public int MajorVersion {
247                         get {
248                                 if (!Get (HaveMajorVersion)) {
249                                         Set (HaveMajorVersion);
250                                         majorVersion = ReadInt32 ("majorver", 0);
251                                 }
252
253                                 return majorVersion;
254                         }
255                 }
256
257                 public double MinorVersion {
258                         get {
259                                 if (!Get (HaveMinorVersion)) {
260                                         Set (HaveMinorVersion);
261                                         minorVersion = ReadDouble ("minorver", 0);
262                                 }
263
264                                 return minorVersion;
265                         }
266                 }
267
268                 public Version MSDomVersion {
269                         get {
270                                 return new Version (0, 0);
271                         }
272                 }
273
274                 public string Platform {
275                         get {
276                                 if (!Get (HavePlatform)) {
277                                         Set (HavePlatform);
278                                         platform = this ["platform"];
279                                         if (platform == null)
280                                                 platform = "";
281                                 }
282
283                                 return platform;
284                         }
285                 }
286
287                 public bool Tables {
288                         get {
289                                 if (!Get (HaveTables)) {
290                                         Set (HaveTables);
291                                         tables = ReadBoolean ("tables", false);
292                                 }
293
294                                 return tables;
295                         }
296                 }
297
298                 public Type TagWriter {
299                         get {
300                                 return typeof (HtmlTextWriter);
301                         }
302                 }
303
304                 public string Type {
305                         get {
306                                 return Browser + MajorVersion;
307                         }
308                 }
309
310                 public bool VBScript {
311                         get {
312                                 if (!Get (HaveVBScript)) {
313                                         Set (HaveVBScript);
314                                         vbscript = ReadBoolean ("vbscript", false);
315                                 }
316
317                                 return vbscript;
318                         }
319                 }
320
321                 public string Version {
322                         get {
323                                 if (!Get (HaveVersion)) {
324                                         Set (HaveVersion);
325                                         version = this ["version"];
326                                         if (version == null)
327                                                 version = "";
328                                 }
329
330                                 return version;
331                         }
332                 }
333
334                 public Version W3CDomVersion {
335                         get {
336                                 if (!Get (HaveW3CDomVersion)) {
337                                         string ver_str;
338                                         Set (HaveW3CDomVersion);
339                                         ver_str = this ["w3cdomversion"];
340                                         if (ver_str == null)
341                                                 w3CDomVersion = new Version (0, 0);
342                                         else
343                                                 w3CDomVersion = new Version (ver_str);
344                                 }
345
346                                 return w3CDomVersion;
347                         }
348                 }
349
350                 public bool Win16 {
351                         get {
352                                 if (!Get (HaveWin16)) {
353                                         Set (HaveWin16);
354                                         win16 = ReadBoolean ("win16", false);
355                                 }
356
357                                 return win16;
358                         }
359                 }
360
361                 public bool Win32 {
362                         get {
363                                 // This is the list of different windows platforms that browscap.ini has.
364                                 // Win16 Win2000 Win2003 Win32 Win95 Win98 WinME WinNT WinVI WinXP
365                                 if (!Get (HaveWin32)) {
366                                         Set (HaveWin32);
367                                         string platform = Platform;
368                                         win32 = (platform != "Win16" && platform.StartsWith ("Win"));
369                                 }
370                                 return win32;
371                         }
372                 }
373
374 #if NET_1_1
375                 public Version [] GetClrVersions ()
376                 {
377                         if (clrVersions == null)
378                                 InternalGetClrVersions ();
379
380                         return clrVersions;
381                 }
382 #endif
383
384                 void InternalGetClrVersions ()
385                 {
386                         char [] anychars = new char [] { ';', ')' };
387                         string s = useragent;
388                         ArrayList list = null;
389                         int idx;
390                         while ((idx = s.IndexOf (".NET CLR ")) != -1) {
391                                 int end = s.IndexOfAny (anychars, idx + 9);
392                                 if (end == -1)
393                                         break;
394
395                                 string ver = s.Substring (idx + 9, end - idx - 9);
396                                 Version v = null;
397                                 try {
398                                         v = new Version (ver);
399                                         if (clrVersion == null || v > clrVersion)
400                                                 clrVersion = v;
401
402                                         if (list == null)
403                                                 list = new ArrayList (4);
404
405                                         list.Add (v);
406                                 } catch { }
407                                 s = s.Substring (idx + 9);
408                         }
409                         
410                         if (list == null || list.Count == 0) {
411                                 clrVersion = new Version ();
412                                 clrVersions = new Version [] { clrVersion };
413                         } else {
414                                 list.Sort ();
415                                 clrVersions = (Version []) list.ToArray (typeof (Version));
416                         }
417                 }
418
419                 bool ReadBoolean (string key, bool dflt)
420                 {
421                         string v = this [key];
422                         if (v == null)
423                                 return dflt;
424
425                         return (v == "True");
426                 }
427
428                 int ReadInt32 (string key, int dflt)
429                 {
430                         string v = this [key];
431                         if (v == null)
432                                 return dflt;
433
434                         try {
435                                 return Int32.Parse (v);
436                         } catch {
437                                 return dflt;
438                         }
439                 }
440
441                 double ReadDouble (string key, double dflt)
442                 {
443                         string v = this [key];
444                         if (v == null)
445                                 return dflt;
446
447                         try {
448                                 return Double.Parse (v);
449                         } catch {
450                                 return dflt;
451                         }
452                 }
453
454                 bool Get (int idx)
455                 {
456                         return (flags & (1 << idx)) != 0;
457                 }
458
459                 void Set (int idx)
460                 {
461                         flags |= (1 << idx);
462                 }
463         }
464 }