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
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.Activities
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.Activities.Runtime;
using System.Runtime;
using System.Diagnostics.CodeAnalysis;
[DataContract]
public class ExclusiveHandle : Handle
{
ReadOnlyCollection<BookmarkScopeHandle> readOnlyBookmarkScopeCollection;
List<BookmarkScopeHandle> bookmarkScopes;
ActivityInstance owningInstance;
ActivityExecutor executor;
ExclusiveHandleBookmarkList importantBookmarks;
ExclusiveHandleBookmarkList unimportantBookmarks;
bool bookmarkScopesListIsDefault;
public ExclusiveHandle()
{
this.CanBeRemovedWithExecutingChildren = true;
}
public ReadOnlyCollection<BookmarkScopeHandle> RegisteredBookmarkScopes
{
get
{
if (this.bookmarkScopes == null)
{
return new ReadOnlyCollection<BookmarkScopeHandle>(new List<BookmarkScopeHandle>());
}
if (this.readOnlyBookmarkScopeCollection == null)
{
this.readOnlyBookmarkScopeCollection = new ReadOnlyCollection<BookmarkScopeHandle>(this.bookmarkScopes);
}
return this.readOnlyBookmarkScopeCollection;
}
}
[DataMember(EmitDefaultValue = false, Name = "bookmarkScopes")]
internal List<BookmarkScopeHandle> SerializedBookmarkScopes
{
get { return this.bookmarkScopes; }
set { this.bookmarkScopes = value; }
}
[DataMember(Name = "owningInstance")]
internal ActivityInstance SerializedOwningInstance
{
get { return this.owningInstance; }
set { this.owningInstance = value; }
}
[DataMember(EmitDefaultValue = false, Name = "executor")]
internal ActivityExecutor SerializedExecutor
{
get { return this.executor; }
set { this.executor = value; }
}
[DataMember(EmitDefaultValue = false, Name = "importantBookmarks")]
internal ExclusiveHandleBookmarkList SerializedImportantBookmarks
{
get { return this.importantBookmarks; }
set { this.importantBookmarks = value; }
}
[DataMember(EmitDefaultValue = false, Name = "unimportantBookmarks")]
internal ExclusiveHandleBookmarkList SerializedUnimportantBookmarks
{
get { return this.unimportantBookmarks; }
set { this.unimportantBookmarks = value; }
}
[DataMember(EmitDefaultValue = false, Name = "bookmarkScopesListIsDefault")]
internal bool SerializedBookmarkScopesListIsDefault
{
get { return this.bookmarkScopesListIsDefault; }
set { this.bookmarkScopesListIsDefault = value; }
}
[SuppressMessage(FxCop.Category.Design, FxCop.Rule.ConsiderPassingBaseTypesAsParameters,
Justification = "We are restricting the activities that can call this API.")]
public void RegisterBookmarkScope(NativeActivityContext context, BookmarkScopeHandle bookmarkScopeHandle)
{
if (context == null)
{
throw FxTrace.Exception.ArgumentNull("context");
}
context.ThrowIfDisposed();
if (bookmarkScopeHandle == null)
{
throw FxTrace.Exception.ArgumentNull("bookmarkScopeHandle");
}
if ((this.ImportantBookmarks != null && this.ImportantBookmarks.Count != 0) || (this.UnimportantBookmarks != null && this.UnimportantBookmarks.Count != 0))
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.ExclusiveHandleRegisterBookmarkScopeFailed));
}
if (this.bookmarkScopesListIsDefault)
{
this.bookmarkScopesListIsDefault = false;
this.bookmarkScopes.Clear();
}
this.bookmarkScopes.Add(bookmarkScopeHandle);
this.readOnlyBookmarkScopeCollection = null;
}
[SuppressMessage(FxCop.Category.Design, FxCop.Rule.ConsiderPassingBaseTypesAsParameters,
Justification = "We are restricting the activities that can call this API.")]
public void Reinitialize(NativeActivityContext context)
{
if (context == null)
{
throw FxTrace.Exception.ArgumentNull("context");
}
context.ThrowIfDisposed();
if ((this.ImportantBookmarks != null && this.ImportantBookmarks.Count != 0) || (this.UnimportantBookmarks != null && this.UnimportantBookmarks.Count != 0))
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.ExclusiveHandleReinitializeFailed));
}
this.bookmarkScopes.Clear();
this.readOnlyBookmarkScopeCollection = null;
this.PerformDefaultRegistration();
}
protected override void OnInitialize(HandleInitializationContext context)
{
this.owningInstance = context.OwningActivityInstance;
this.executor = context.Executor;
PerformDefaultRegistration();
}
internal ExclusiveHandleBookmarkList ImportantBookmarks
{
get
{
return this.importantBookmarks;
}
set
{
this.importantBookmarks = value;
}
}
internal ExclusiveHandleBookmarkList UnimportantBookmarks
{
get
{
return this.unimportantBookmarks;
}
set
{
this.unimportantBookmarks = value;
}
}
internal void AddToImportantBookmarks(Bookmark bookmark)
{
if (this.ImportantBookmarks == null)
{
this.ImportantBookmarks = new ExclusiveHandleBookmarkList();
}
Fx.Assert(!this.ImportantBookmarks.Contains(bookmark), "We shouldnt be here. We attempted to add the same bookmark");
this.ImportantBookmarks.Add(bookmark);
if (bookmark.ExclusiveHandles == null)
{
bookmark.ExclusiveHandles = new ExclusiveHandleList();
}
Fx.Assert(!bookmark.ExclusiveHandles.Contains(this), "We shouldnt be here. We attempted to add the bookmark to this exclusive handle already");
bookmark.ExclusiveHandles.Add(this);
}
internal void AddToUnimportantBookmarks(Bookmark bookmark)
{
if (this.UnimportantBookmarks == null)
{
this.UnimportantBookmarks = new ExclusiveHandleBookmarkList();
}
Fx.Assert(!this.UnimportantBookmarks.Contains(bookmark), "We shouldnt be here. We attempted to add the same bookmark");
this.UnimportantBookmarks.Add(bookmark);
if (bookmark.ExclusiveHandles == null)
{
bookmark.ExclusiveHandles = new ExclusiveHandleList();
}
Fx.Assert(!bookmark.ExclusiveHandles.Contains(this), "We shouldnt be here. We attempted to add the bookmark to this exclusive handle already");
bookmark.ExclusiveHandles.Add(this);
}
internal void RemoveBookmark(Bookmark bookmark)
{
Fx.Assert((this.ImportantBookmarks != null && this.ImportantBookmarks.Contains(bookmark)) ||
(this.UnimportantBookmarks != null && this.UnimportantBookmarks.Contains(bookmark)), "Internal error");
if (this.ImportantBookmarks != null)
{
if (this.ImportantBookmarks.Contains(bookmark))
{
this.ImportantBookmarks.Remove(bookmark);
return;
}
}
if (this.UnimportantBookmarks != null)
{
if (this.UnimportantBookmarks.Contains(bookmark))
{
this.UnimportantBookmarks.Remove(bookmark);
}
}
}
void PerformDefaultRegistration()
{
if (this.bookmarkScopes == null)
{
this.bookmarkScopes = new List<BookmarkScopeHandle>();
}
//First register the default subinstance
this.bookmarkScopes.Add(BookmarkScopeHandle.Default);
// Note that we are starting the LocationEnvironment traversal from the current environment's Parent. We don't
// want to include any BookmarkScopeHandles that are at the same scope level as the ExclusiveHandle. The ExclusiveHandle
// should only be dependent on BookmarkScopeHandles that are higher in the scope tree.
LocationEnvironment current = this.owningInstance.Environment;
if (current != null)
{
for (current = current.Parent; current != null; current = current.Parent)
{
//don't bother continuing if at this level there are no handles
if (!current.HasHandles)
{
continue;
}
// Look at the contained handles for the environment.
List<Handle> handles = current.Handles;
if (handles != null)
{
int count = handles.Count;
for (int i = 0; i < count; i++)
{
BookmarkScopeHandle scopeHandle = handles[i] as BookmarkScopeHandle;
if (scopeHandle != null)
{
this.bookmarkScopes.Add(scopeHandle);
}
}
}
}
}
// Also need to look in the Executor for handles that may have been created without an environment.
List<Handle> executorHandles = this.executor.Handles;
if (executorHandles != null)
{
int count = executorHandles.Count;
for (int i = 0; i < count; i++)
{
BookmarkScopeHandle scopeHandle = executorHandles[i] as BookmarkScopeHandle;
if (scopeHandle != null)
{
this.bookmarkScopes.Add(scopeHandle);
}
}
}
this.bookmarkScopesListIsDefault = true;
}
// Exclusive handle needs to track bookmarks such that it can tell the difference between two bookmarks in
// different bookmark scopes with the same name. Since we always deal in terms of the internal bookmark
// reference that we have, we can do an object.ReferenceEquals comparison to determine distinct bookmarks
// without having to add some sort of "containing scope" property to Bookmark.
[DataContract]
internal class ExclusiveHandleBookmarkList
{
List<Bookmark> bookmarks;
public ExclusiveHandleBookmarkList()
: base()
{
this.bookmarks = new List<Bookmark>();
}
public int Count
{
get { return this.bookmarks.Count; }
}
[DataMember(Name = "bookmarks")]
internal List<Bookmark> SerializedBookmarks
{
get { return this.bookmarks; }
set { this.bookmarks = value; }
}
public void Add(Bookmark bookmark)
{
Fx.Assert(bookmark != null, "A valid bookmark is expected.");
this.bookmarks.Add(bookmark);
}
public void Remove(Bookmark bookmark)
{
Fx.Assert(bookmark != null, "A valid bookmark is expected.");
for (int i = 0; i < this.bookmarks.Count; i++)
{
if (object.ReferenceEquals(this.bookmarks[i], bookmark))
{
this.bookmarks.RemoveAt(i);
return;
}
}
}
public bool Contains(Bookmark bookmark)
{
Fx.Assert(bookmark != null, "A valid bookmark is expected.");
for (int i = 0; i < this.bookmarks.Count; i++)
{
if (object.ReferenceEquals(this.bookmarks[i], bookmark))
{
return true;
}
}
return false;
}
}
}
}
|