1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
//------------------------------------------------------------------------------
// <copyright file="IndividualDeviceConfig.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
// Comment this out to get a version that doesn't need synchronized
// access. This can be used for profiling, to compare whether the lock
// or the late writing is more useful.
using System;
using System.Web.Configuration;
using System.Configuration;
using System.Xml;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Web;
using System.Web.Util;
using System.Threading;
using System.Web.Mobile;
namespace System.Web.UI.MobileControls
{
// Data structure for an individual device configuration.
// Included predicates, page adapter type, and a list of
// control/controlAdapter pairs.
[Obsolete("The System.Web.Mobile.dll assembly has been deprecated and should no longer be used. For information about how to develop ASP.NET mobile applications, see http://go.microsoft.com/fwlink/?LinkId=157231.")]
internal class IndividualDeviceConfig
{
internal delegate bool DeviceQualifiesDelegate(HttpContext context);
private String _name;
private readonly ControlsConfig _controlsConfig;
private DeviceQualifiesDelegate _deviceQualifiesPredicate;
private Type _pageAdapterType;
private IWebObjectFactory _pageAdapterFactory;
// Parent device configuration.
private IndividualDeviceConfig _parentConfig;
private String _parentConfigName;
// ControlType --> ControlAdapterType mapping (one of these
// per individual device config)
private readonly Hashtable _controlAdapterTypes = new Hashtable();
// ControlType --> ControlAdapterType mapping cache, used to
// store mappings that are derived from a complex lookup (one of these
// per individual device config)
private readonly Hashtable _controlAdapterLookupCache = new Hashtable();
// Provide synchronized access to the hashtable, allowing
// multiple readers but just one writer. Here we have one per
// device config.
private readonly ReaderWriterLock _controlAdapterTypesLock = new ReaderWriterLock();
// The highest level to check.
private static readonly Type _baseControlType = typeof(System.Web.UI.Control);
// This constructor takes both a delegate that chooses this
// device, and a Type to instantiate the appropriate page
// adapter with.
internal IndividualDeviceConfig(ControlsConfig controlsConfig,
String name,
DeviceQualifiesDelegate deviceQualifiesDelegate,
Type pageAdapterType,
String parentConfigName)
{
_controlsConfig = controlsConfig;
_name = name;
_deviceQualifiesPredicate = deviceQualifiesDelegate;
_parentConfigName = parentConfigName;
_parentConfig = null;
PageAdapterType = pageAdapterType;
}
// This constructor takes just a page adapter for situations
// where device selection isn't necessary (e.g., the designer).
internal IndividualDeviceConfig(Type pageAdapterType) : this(null, null, null, pageAdapterType, null)
{
}
// Given a context, see if this device config should handle
// the given device. If there is no predicate, return true.
internal /*public*/ bool DeviceQualifies(HttpContext context)
{
return _deviceQualifiesPredicate == null ?
true :
_deviceQualifiesPredicate(context);
}
// Register an adapter with the given control.
internal /*public*/ void AddControl(Type controlType,
Type adapterType)
{
// Don't need to synchronize, as this is only being called
// from one thread -- the configuration section handler.
_controlAdapterTypes[controlType] = FactoryGenerator.StaticFactoryGenerator.GetFactory(adapterType);
}
private Type PageAdapterType
{
get
{
return _pageAdapterType;
}
set {
_pageAdapterType = value;
if (value != null) {
Debug.Assert(typeof(IPageAdapter).IsAssignableFrom(value));
_pageAdapterFactory =
(IWebObjectFactory)FactoryGenerator.StaticFactoryGenerator.GetFactory(_pageAdapterType);
}
}
}
internal DeviceQualifiesDelegate DeviceQualifiesPredicate
{
get
{
return _deviceQualifiesPredicate;
}
set
{
_deviceQualifiesPredicate = value;
}
}
protected IWebObjectFactory LookupControl(Type controlType)
{
return LookupControl(controlType, false);
}
private IWebObjectFactory LookupControl(Type controlType, bool lookInTypeCache)
{
IWebObjectFactory factory;
factory = (IWebObjectFactory)_controlAdapterTypes[controlType];
if (factory == null && lookInTypeCache)
{
// Grab reader lock...
using (new ReaderWriterLockResource(_controlAdapterTypesLock,
false))
{
factory = (IWebObjectFactory)_controlAdapterLookupCache[controlType];
}
}
return factory;
}
// Create a new page adapter for the device.
internal /*public*/ IPageAdapter NewPageAdapter()
{
IPageAdapter a = _pageAdapterFactory.CreateInstance() as IPageAdapter;
if (a == null)
{
throw new Exception(
SR.GetString(SR.IndividualDeviceConfig_TypeMustSupportInterface,
_pageAdapterType.FullName, "IPageAdapter"));
}
return a;
}
// Given a control's type, create a control adapter for it.
internal virtual IControlAdapter NewControlAdapter(Type originalControlType)
{
IWebObjectFactory factory = GetAdapterFactory(originalControlType);
// Should return non-null, or throw an exception.
Debug.Assert(factory != null);
IControlAdapter a = (IControlAdapter) factory.CreateInstance();
return a;
}
// Given a control's type, returns the adapter type to be used.
// Note that it's legal to not register an adapter type for each
// control type.
//
// This lookup uses the following steps:
//
// (1) Look up the control type directly, to see if an adapter type
// has been registered for it.
// (2) Walk up the control inheritance chain, to see if an adapter type
// has been registered for the class. For example, if the passed
// control type is a validator, check BaseValidator, Label,
// TextControl, and finally MobileControl.
// (3) If no adapter type has still been found, call the parent configuration,
// if any, to look up the adapter type. For example, the CHTML device
// configuration would call the HTML device configuration.
// (4) If an adapter type is found, but is not explicitly registered for
// the passed control type, add an entry to the table, so that
// subsequent requests do not need to walk the hierarchy.
protected IWebObjectFactory GetAdapterFactory(Type originalControlType)
{
Debug.Assert(_parentConfigName == null);
Type controlType = originalControlType;
IWebObjectFactory factory = LookupControl(controlType, true); // Look in type cache
// Walk up hierarchy looking for registered adapters.
// Stop when we get to the base control.
while (factory == null && controlType != _baseControlType)
{
factory = LookupControl(controlType);
if (factory == null)
{
controlType = controlType.BaseType;
}
}
// Could not find one in the current hierarchy. So, look it up in
// the parent config if there is one.
if (factory == null && _parentConfig != null)
{
factory = _parentConfig.GetAdapterFactory(originalControlType);
}
if (factory == null)
{
throw new Exception(
SR.GetString(SR.IndividualDeviceConfig_ControlWithIncorrectPageAdapter,
controlType.FullName, _pageAdapterType.FullName));
}
if (controlType != originalControlType)
{
// Add to lookup cache, so the next lookup won't require
// traversing the hierarchy.
// Grab writer lock...
using (new ReaderWriterLockResource(_controlAdapterTypesLock,
true))
{
_controlAdapterLookupCache[originalControlType] = factory;
}
}
return factory;
}
internal /*public*/ String Name
{
get
{
return _name;
}
}
internal /*public*/ String ParentConfigName
{
get
{
return _parentConfigName;
}
set
{
_parentConfigName = null;
}
}
internal /*public*/ IndividualDeviceConfig ParentConfig
{
get
{
return _parentConfig;
}
set
{
_parentConfig = value;
}
}
private enum FixupState { NotFixedUp, FixingUp, FixedUp };
private FixupState _fixup = FixupState.NotFixedUp;
internal /*public*/ void FixupInheritance(IndividualDeviceConfig referrer, XmlNode configNode)
{
if (_fixup == FixupState.FixedUp)
{
return;
}
if (_fixup == FixupState.FixingUp)
{
Debug.Assert(referrer != null);
// Circular reference
throw new Exception(SR.GetString(SR.MobileControlsSectionHandler_CircularReference,
referrer.Name));
}
_fixup = FixupState.FixingUp;
if (ParentConfigName != null)
{
Debug.Assert(ParentConfigName.Length != 0 && ParentConfig == null);
ParentConfig = _controlsConfig.GetDeviceConfig(ParentConfigName);
if (ParentConfig == null)
{
throw new ConfigurationErrorsException(
SR.GetString(SR.MobileControlsSectionHandler_DeviceConfigNotFound,
ParentConfigName),
configNode);
}
// Make sure parent is fixed up.
ParentConfig.FixupInheritance(this, configNode);
if (PageAdapterType == null)
{
PageAdapterType = ParentConfig.PageAdapterType;
}
if (DeviceQualifiesPredicate == null)
{
DeviceQualifiesPredicate = ParentConfig.DeviceQualifiesPredicate;
}
Debug.Assert(PageAdapterType != null);
Debug.Assert(DeviceQualifiesPredicate != null);
// Reset this since we don't need it any longer.
ParentConfigName = null;
}
_fixup = FixupState.FixedUp;
}
}
[Obsolete("The System.Web.Mobile.dll assembly has been deprecated and should no longer be used. For information about how to develop ASP.NET mobile applications, see http://go.microsoft.com/fwlink/?LinkId=157231.")]
internal class ReaderWriterLockResource : IDisposable
{
private ReaderWriterLock _lock;
private bool _writerLock;
internal /*public*/ ReaderWriterLockResource(ReaderWriterLock theLock, bool writerLock)
{
_lock = theLock;
_writerLock = writerLock;
if (_writerLock)
{
_lock.AcquireWriterLock(Timeout.Infinite);
}
else
{
_lock.AcquireReaderLock(Timeout.Infinite);
}
}
/*public*/ void IDisposable.Dispose()
{
if (_writerLock)
{
_lock.ReleaseWriterLock();
}
else
{
_lock.ReleaseReaderLock();
}
}
}
}
|