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 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
|
//------------------------------------------------------------------------------
// <copyright file="StateManagedCollection.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.UI {
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Util;
/// <devdoc>
/// Manages state for an arbitrary collection of items that implement IStateManager.
/// The collection differentiates between known types and unknown types.
/// Known types take up less space in ViewState because only an index needs to be stored instead of a fully qualified type name.
/// Unknown types need to have their fully qualified type name stored in ViewState so they take up more space.
/// </devdoc>
public abstract class StateManagedCollection : IList, IStateManager {
private ArrayList _collectionItems;
private bool _tracking;
private bool _saveAll;
// We want to know if the collection had items to begin with
// so we don't put empty collections in the ViewState unnecessarily
private bool _hadItems;
/// <devdoc>
/// Creates a new instance of StateManagedCollection.
/// </devdoc>
protected StateManagedCollection() {
_collectionItems = new ArrayList();
}
/// <devdoc>
/// Returns the number of items in the collection.
/// </devdoc>
public int Count {
get {
return _collectionItems.Count;
}
}
/// <devdoc>
/// Removes all the items from the collection.
/// </devdoc>
public void Clear() {
OnClear();
_collectionItems.Clear();
OnClearComplete();
if (_tracking) {
_saveAll = true;
}
}
public void CopyTo(Array array, int index) {
_collectionItems.CopyTo(array, index);
}
/// <devdoc>
/// Creates an object of a known type based on an index into an array of types.
/// Indexes passed into CreateKnownType() must mach the indexes of the ArrayList returned
/// by GetKnownTypes().
/// </devdoc>
protected virtual object CreateKnownType(int index) {
throw new InvalidOperationException(SR.GetString(SR.StateManagedCollection_NoKnownTypes));
}
/// <devdoc>
/// Returns the IEnumerator for the collection.
/// </devdoc>
public IEnumerator GetEnumerator() {
return _collectionItems.GetEnumerator();
}
/// <devdoc>
/// Returns an ordered list of known types.
/// </devdoc>
protected virtual Type[] GetKnownTypes() {
return null;
}
/// <devdoc>
/// Returns the number of known types.
/// </devdoc>
private int GetKnownTypeCount() {
Type[] types = GetKnownTypes();
if (types == null)
return 0;
return types.Length;
}
/// <devdoc>
/// Inserts a new object into the collection at a given index.
/// If the index is -1 then the object is appended to the end of the collection.
/// </devdoc>
private void InsertInternal(int index, object o) {
Debug.Assert(index >= -1 && index <= Count, "Expected index to be at least -1 and less than or equal to Count.");
if (o == null) {
throw new ArgumentNullException("o");
}
if (((IStateManager)this).IsTrackingViewState) {
((IStateManager)o).TrackViewState();
SetDirtyObject(o);
}
OnInsert(index, o);
int trueIndex;
if (index == -1) {
trueIndex = _collectionItems.Add(o);
}
else {
trueIndex = index;
_collectionItems.Insert(index, o);
}
try {
OnInsertComplete(index, o);
}
catch {
_collectionItems.RemoveAt(trueIndex);
throw;
}
}
/// <devdoc>
/// Loads all items from view state.
/// </devdoc>
private void LoadAllItemsFromViewState(object savedState) {
Debug.Assert(savedState != null);
Debug.Assert(savedState is Pair);
Pair p1 = (Pair)savedState;
if (p1.Second is Pair) {
Pair p2 = (Pair)p1.Second;
// save all mode; some objects are typed
object[] states = (object[])p1.First;
int[] typeIndices = (int[])p2.First;
ArrayList typedObjectTypeNames = (ArrayList)p2.Second;
Clear();
for (int i = 0; i < states.Length; i++) {
object o;
// If there is only one known type, we don't need type indices
if (typeIndices == null) {
// Create known type
o = CreateKnownType(0);
}
else {
int typeIndex = typeIndices[i];
if (typeIndex < GetKnownTypeCount()) {
// Create known type
o = CreateKnownType(typeIndex);
}
else {
string typeName = (string)typedObjectTypeNames[typeIndex - GetKnownTypeCount()];
Type type = Type.GetType(typeName);
o = Activator.CreateInstance(type);
}
}
((IStateManager)o).TrackViewState();
((IStateManager)o).LoadViewState(states[i]);
((IList)this).Add(o);
}
}
else {
Debug.Assert(p1.First is object[]);
// save all mode; all objects are instances of known types
object[] states = (object[])p1.First;
int[] typeIndices = (int[])p1.Second;
Clear();
for (int i = 0; i < states.Length; i++) {
// Create known type
int typeIndex = 0;
if (typeIndices != null) {
typeIndex = (int)typeIndices[i];
}
object o = CreateKnownType(typeIndex);
((IStateManager)o).TrackViewState();
((IStateManager)o).LoadViewState(states[i]);
((IList)this).Add(o);
}
}
}
/// <devdoc>
/// Loads only changed items from view state.
/// </devdoc>
private void LoadChangedItemsFromViewState(object savedState) {
Debug.Assert(savedState != null);
Debug.Assert(savedState is Triplet);
Triplet t = (Triplet)savedState;
if (t.Third is Pair) {
// save some mode; some new objects are typed
Pair p = (Pair)t.Third;
ArrayList indices = (ArrayList)t.First;
ArrayList states = (ArrayList)t.Second;
ArrayList typeIndices = (ArrayList)p.First;
ArrayList typedObjectTypeNames = (ArrayList)p.Second;
for (int i = 0; i < indices.Count; i++) {
int index = (int)indices[i];
if (index < Count) {
((IStateManager)((IList)this)[index]).LoadViewState(states[i]);
}
else {
object o;
// If there is only one known type, we don't need type indices
if (typeIndices == null) {
// Create known type
o = CreateKnownType(0);
}
else {
int typeIndex = (int)typeIndices[i];
if (typeIndex < GetKnownTypeCount()) {
// Create known type
o = CreateKnownType(typeIndex);
}
else {
string typeName = (string)typedObjectTypeNames[typeIndex - GetKnownTypeCount()];
Type type = Type.GetType(typeName);
o = Activator.CreateInstance(type);
}
}
((IStateManager)o).TrackViewState();
((IStateManager)o).LoadViewState(states[i]);
((IList)this).Add(o);
}
}
}
else {
// save some mode; all new objects are instances of known types
ArrayList indices = (ArrayList)t.First;
ArrayList states = (ArrayList)t.Second;
ArrayList typeIndices = (ArrayList)t.Third;
for (int i = 0; i < indices.Count; i++) {
int index = (int)indices[i];
if (index < Count) {
((IStateManager)((IList)this)[index]).LoadViewState(states[i]);
}
else {
// Create known type
int typeIndex = 0;
if (typeIndices != null) {
typeIndex = (int)typeIndices[i];
}
object o = CreateKnownType(typeIndex);
((IStateManager)o).TrackViewState();
((IStateManager)o).LoadViewState(states[i]);
((IList)this).Add(o);
}
}
}
}
/// <devdoc>
/// Called when the Clear() method is starting.
/// </devdoc>
protected virtual void OnClear() {
}
/// <devdoc>
/// Called when the Clear() method is complete.
/// </devdoc>
protected virtual void OnClearComplete() {
}
/// <devdoc>
/// Called when an object must be validated.
/// </devdoc>
protected virtual void OnValidate(object value) {
if (value == null) throw new ArgumentNullException("value");
}
/// <devdoc>
/// Called when the Insert() method is starting.
/// </devdoc>
protected virtual void OnInsert(int index, object value) {
}
/// <devdoc>
/// Called when the Insert() method is complete.
/// </devdoc>
protected virtual void OnInsertComplete(int index, object value) {
}
/// <devdoc>
/// Called when the Remove() method is starting.
/// </devdoc>
protected virtual void OnRemove(int index, object value) {
}
/// <devdoc>
/// Called when the Remove() method is complete.
/// </devdoc>
protected virtual void OnRemoveComplete(int index, object value) {
}
/// <devdoc>
/// Saves all items in the collection to view state.
/// </devdoc>
private object SaveAllItemsToViewState() {
Debug.Assert(_saveAll);
bool hasState = false;
int count = _collectionItems.Count;
int[] typeIndices = new int[count];
object[] states = new object[count];
ArrayList typedObjectTypeNames = null;
IDictionary typedObjectTracker = null;
int knownTypeCount = GetKnownTypeCount();
for (int i = 0; i < count; i++) {
object o = _collectionItems[i];
SetDirtyObject(o);
states[i] = ((IStateManager)o).SaveViewState();
if (states[i] != null)
hasState = true;
Type objectType = o.GetType();
int knownTypeIndex = -1;
// If there are known types, find index
if (knownTypeCount != 0) {
knownTypeIndex = ((IList)GetKnownTypes()).IndexOf(objectType);
}
if (knownTypeIndex != -1) {
// Type is known
typeIndices[i] = knownTypeIndex;
}
else {
// Type is unknown
if (typedObjectTypeNames == null) {
typedObjectTypeNames = new ArrayList();
typedObjectTracker = new HybridDictionary();
}
// Get index of type
object index = typedObjectTracker[objectType];
// If type is not in list, add it to the list
if (index == null) {
typedObjectTypeNames.Add(objectType.AssemblyQualifiedName);
// Offset the index by the known type count
index = typedObjectTypeNames.Count + knownTypeCount - 1;
typedObjectTracker[objectType] = index;
}
typeIndices[i] = (int)index;
}
}
// If the collection didn't have items to begin with don't save the state
if (!_hadItems && !hasState) {
return null;
}
if (typedObjectTypeNames == null) {
// all objects are instances known types
// If there is only one known type, then all objects are of that type so the indices are not needed
if (knownTypeCount == 1)
typeIndices = null;
return new Pair(states, typeIndices);
}
else {
return new Pair(states, new Pair(typeIndices, typedObjectTypeNames));
}
}
/// <devdoc>
/// Saves changed items to view state.
/// </devdoc>
private object SaveChangedItemsToViewState() {
Debug.Assert(_saveAll == false);
bool hasState = false;
int count = _collectionItems.Count;
ArrayList indices = new ArrayList();
ArrayList states = new ArrayList();
ArrayList typeIndices = new ArrayList();
ArrayList typedObjectTypeNames = null;
IDictionary typedObjectTracker = null;
int knownTypeCount = GetKnownTypeCount();
for (int i = 0; i < count; i++) {
object o = _collectionItems[i];
object state = ((IStateManager)o).SaveViewState();
if (state != null) {
hasState = true;
indices.Add(i);
states.Add(state);
Type objectType = o.GetType();
int knownTypeIndex = -1;
// If there are known types, find index
if (knownTypeCount != 0) {
knownTypeIndex = ((IList)GetKnownTypes()).IndexOf(objectType);
}
if (knownTypeIndex != -1) {
// Type is known
typeIndices.Add(knownTypeIndex);
}
else {
// Type is unknown
if (typedObjectTypeNames == null) {
typedObjectTypeNames = new ArrayList();
typedObjectTracker = new HybridDictionary();
}
object index = typedObjectTracker[objectType];
if (index == null) {
typedObjectTypeNames.Add(objectType.AssemblyQualifiedName);
// Offset the index by the known type count
index = typedObjectTypeNames.Count + knownTypeCount - 1;
typedObjectTracker[objectType] = index;
}
typeIndices.Add(index);
}
}
}
// If the collection didn't have items to begin with don't save the state
if (!_hadItems && !hasState) {
return null;
}
if (typedObjectTypeNames == null) {
// all objects are instances known types
// If there is only one known type, then all objects are of that type so the indices are not needed
if (knownTypeCount == 1)
typeIndices = null;
return new Triplet(indices, states, typeIndices);
}
else {
return new Triplet(indices, states, new Pair(typeIndices, typedObjectTypeNames));
}
}
/// <devdoc>
/// Forces the entire collection to be serialized into viewstate, not just
/// the change-information. This is useful when a collection has changed in
/// a significant way and change information would be insufficient to
/// recreate the object.
/// </devdoc>
public void SetDirty() {
_saveAll = true;
}
/// <devdoc>
/// Flags an object to record its entire state instead of just changed parts.
/// </devdoc>
protected abstract void SetDirtyObject(object o);
/// <internalonly/>
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
/// <internalonly/>
int ICollection.Count {
get {
return Count;
}
}
/// <internalonly/>
bool ICollection.IsSynchronized {
get {
return false;
}
}
/// <internalonly/>
object ICollection.SyncRoot {
get {
return null;
}
}
/// <internalonly/>
bool IList.IsFixedSize {
get {
return false;
}
}
/// <internalonly/>
bool IList.IsReadOnly {
get {
return _collectionItems.IsReadOnly;
}
}
/// <internalonly/>
object IList.this[int index] {
get {
return _collectionItems[index];
}
set {
if (index < 0 || index >= Count) {
throw new ArgumentOutOfRangeException("index", SR.GetString(SR.StateManagedCollection_InvalidIndex));
}
((IList)this).RemoveAt(index);
((IList)this).Insert(index, value);
}
}
/// <internalonly/>
int IList.Add(object value) {
OnValidate(value);
InsertInternal(-1, value);
return _collectionItems.Count - 1;
}
/// <internalonly/>
void IList.Clear() {
Clear();
}
/// <internalonly/>
bool IList.Contains(object value) {
if (value == null) {
return false;
}
OnValidate(value);
return _collectionItems.Contains(value);
}
/// <internalonly/>
int IList.IndexOf(object value) {
if (value == null) {
return -1;
}
OnValidate(value);
return _collectionItems.IndexOf(value);
}
/// <internalonly/>
void IList.Insert(int index, object value) {
if (value == null) {
throw new ArgumentNullException("value");
}
if (index < 0 || index > Count) {
throw new ArgumentOutOfRangeException("index", SR.GetString(SR.StateManagedCollection_InvalidIndex));
}
OnValidate(value);
InsertInternal(index, value);
if (_tracking) {
_saveAll = true;
}
}
/// <internalonly/>
void IList.Remove(object value) {
if (value == null) {
return;
}
OnValidate(value);
((IList)this).RemoveAt(((IList)this).IndexOf(value));
}
/// <internalonly/>
void IList.RemoveAt(int index) {
object o = _collectionItems[index];
OnRemove(index, o);
_collectionItems.RemoveAt(index);
try {
OnRemoveComplete(index, o);
}
catch {
_collectionItems.Insert(index, o);
throw;
}
if (_tracking) {
_saveAll = true;
}
}
/// <internalonly/>
bool IStateManager.IsTrackingViewState {
get {
return _tracking;
}
}
/// <internalonly/>
void IStateManager.LoadViewState(object savedState) {
if (savedState != null) {
if (savedState is Triplet) {
LoadChangedItemsFromViewState(savedState);
}
else {
LoadAllItemsFromViewState(savedState);
}
}
}
/// <internalonly/>
object IStateManager.SaveViewState() {
if (_saveAll) {
return SaveAllItemsToViewState();
}
else {
return SaveChangedItemsToViewState();
}
}
/// <internalonly/>
void IStateManager.TrackViewState() {
if (!((IStateManager)this).IsTrackingViewState) {
_hadItems = Count > 0;
_tracking = true;
foreach (IStateManager o in _collectionItems) {
o.TrackViewState();
}
}
}
}
}
|