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