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
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.Activities.Runtime
{
using System;
using System.Activities.Hosting;
using System.Collections.Generic;
using System.Runtime;
using System.Runtime.Serialization;
[DataContract(Name = XD.Runtime.BookmarkManager, Namespace = XD.Runtime.Namespace)]
class BookmarkManager
{
long nextId;
Dictionary<Bookmark, BookmarkCallbackWrapper> bookmarks;
BookmarkScope scope;
BookmarkScopeHandle scopeHandle;
public BookmarkManager()
{
this.nextId = 1;
}
internal BookmarkManager(BookmarkScope scope, BookmarkScopeHandle scopeHandle)
: this()
{
this.scope = scope;
this.scopeHandle = scopeHandle;
}
public bool HasBookmarks
{
get
{
return this.bookmarks != null && this.bookmarks.Count > 0;
}
}
[DataMember(Name = "nextId")]
internal long SerializedNextId
{
get { return this.nextId; }
set { this.nextId = value; }
}
[DataMember(EmitDefaultValue = false, Name = "bookmarks")]
internal Dictionary<Bookmark, BookmarkCallbackWrapper> SerializedBookmarks
{
get { return this.bookmarks; }
set { this.bookmarks = value; }
}
[DataMember(EmitDefaultValue = false, Name = "scope")]
internal BookmarkScope SerializedScope
{
get { return this.scope; }
set { this.scope = value; }
}
[DataMember(EmitDefaultValue = false, Name = "scopeHandle")]
internal BookmarkScopeHandle SerializedScopeHandle
{
get { return this.scopeHandle; }
set { this.scopeHandle = value; }
}
public Bookmark CreateBookmark(string name, BookmarkCallback callback, ActivityInstance owningInstance, BookmarkOptions options)
{
Bookmark toAdd = new Bookmark(name);
if (this.bookmarks != null && this.bookmarks.ContainsKey(toAdd))
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.BookmarkAlreadyExists(name)));
}
AddBookmark(toAdd, callback, owningInstance, options);
//Regular bookmarks are never important
UpdateAllExclusiveHandles(toAdd, owningInstance);
return toAdd;
}
public Bookmark CreateBookmark(BookmarkCallback callback, ActivityInstance owningInstance, BookmarkOptions options)
{
Fx.Assert(this.scope == null, "We only support named bookmarks within bookmark scopes right now.");
Bookmark bookmark = Bookmark.Create(GetNextBookmarkId());
AddBookmark(bookmark, callback, owningInstance, options);
//Regular bookmarks are never important
UpdateAllExclusiveHandles(bookmark, owningInstance);
return bookmark;
}
void UpdateAllExclusiveHandles(Bookmark bookmark, ActivityInstance owningInstance)
{
Fx.Assert(bookmark != null, "Invalid call to UpdateAllExclusiveHandles. Bookmark was null");
Fx.Assert(owningInstance != null, "Invalid call to UpdateAllExclusiveHandles. ActivityInstance was null");
if (owningInstance.PropertyManager == null)
{
return;
}
if (!owningInstance.PropertyManager.HasExclusiveHandlesInScope)
{
return;
}
List<ExclusiveHandle> handles = owningInstance.PropertyManager.FindAll<ExclusiveHandle>();
if (handles == null)
{
return;
}
for (int i = 0; i < handles.Count; i++)
{
ExclusiveHandle handle = handles[i];
if (handle != null)
{
if (this.scopeHandle != null)
{
bool found = false;
foreach (BookmarkScopeHandle bookmarkScopeHandle in handle.RegisteredBookmarkScopes)
{
if (bookmarkScopeHandle == this.scopeHandle)
{
handle.AddToImportantBookmarks(bookmark);
found = true;
break;
}
}
if (!found)
{
handle.AddToUnimportantBookmarks(bookmark);
}
}
else
{
handle.AddToUnimportantBookmarks(bookmark);
}
}
}
}
public Bookmark GenerateTempBookmark()
{
return Bookmark.Create(GetNextBookmarkId());
}
void AddBookmark(Bookmark bookmark, BookmarkCallback callback, ActivityInstance owningInstance, BookmarkOptions options)
{
if (this.bookmarks == null)
{
this.bookmarks = new Dictionary<Bookmark, BookmarkCallbackWrapper>(Bookmark.Comparer);
}
bookmark.Scope = this.scope;
BookmarkCallbackWrapper bookmarkCallbackWrapper = new BookmarkCallbackWrapper(callback, owningInstance, options)
{
Bookmark = bookmark
};
this.bookmarks.Add(bookmark, bookmarkCallbackWrapper);
owningInstance.AddBookmark(bookmark, options);
if (TD.CreateBookmarkIsEnabled())
{
TD.CreateBookmark(owningInstance.Activity.GetType().ToString(), owningInstance.Activity.DisplayName, owningInstance.Id, ActivityUtilities.GetTraceString(bookmark), ActivityUtilities.GetTraceString((BookmarkScope)bookmark.Scope));
}
}
long GetNextBookmarkId()
{
if (this.nextId == long.MaxValue)
{
throw FxTrace.Exception.AsError(new NotSupportedException(SR.OutOfInternalBookmarks));
}
long result = this.nextId;
this.nextId++;
return result;
}
// This method translates from a bookmark that we may have received from the outside world (IE - new Bookmark(someName))
// to our internal Bookmark object. This is necessary because we use bookmark objects as the key to our dictionary
// (hence our ability to resolve an externally created one), but we keep a lot of important information on our internal
// instance of that bookmark. We must always perform this translation when doing exclusive handle housekeeping.
public bool TryGetBookmarkFromInternalList(Bookmark bookmark, out Bookmark internalBookmark, out BookmarkCallbackWrapper callbackWrapper)
{
internalBookmark = null;
callbackWrapper = null;
if (this.bookmarks == null)
{
return false;
}
BookmarkCallbackWrapper wrapper;
if (this.bookmarks.TryGetValue(bookmark, out wrapper))
{
internalBookmark = wrapper.Bookmark;
callbackWrapper = wrapper;
return true;
}
return false;
}
public BookmarkResumptionResult TryGenerateWorkItem(ActivityExecutor executor, bool isExternal, ref Bookmark bookmark, object value, ActivityInstance isolationInstance, out ActivityExecutionWorkItem workItem)
{
Bookmark internalBookmark = null;
BookmarkCallbackWrapper callbackWrapper = null;
if (!this.TryGetBookmarkFromInternalList(bookmark, out internalBookmark, out callbackWrapper))
{
workItem = null;
return BookmarkResumptionResult.NotFound;
}
bookmark = internalBookmark;
if (!ActivityUtilities.IsInScope(callbackWrapper.ActivityInstance, isolationInstance))
{
workItem = null;
// We know about the bookmark, but we can't resume it yet
return BookmarkResumptionResult.NotReady;
}
workItem = callbackWrapper.CreateWorkItem(executor, isExternal, bookmark, value);
if (!BookmarkOptionsHelper.SupportsMultipleResumes(callbackWrapper.Options))
{
// cleanup bookmark on resumption unless the user opts into multi-resume
Remove(bookmark, callbackWrapper);
}
return BookmarkResumptionResult.Success;
}
public void PopulateBookmarkInfo(List<BookmarkInfo> bookmarks)
{
Fx.Assert(this.HasBookmarks, "Should only be called if this actually has bookmarks.");
foreach (KeyValuePair<Bookmark, BookmarkCallbackWrapper> bookmarkEntry in this.bookmarks)
{
if (bookmarkEntry.Key.IsNamed)
{
bookmarks.Add(bookmarkEntry.Key.GenerateBookmarkInfo(bookmarkEntry.Value));
}
}
}
// No need to translate using TryGetBookmarkFromInternalList because we already have
// internal instances since this call comes from bookmarks hanging off of the
// ActivityInstance and not from an external source
public void PurgeBookmarks(Bookmark singleBookmark, IList<Bookmark> multipleBookmarks)
{
if (singleBookmark != null)
{
PurgeSingleBookmark(singleBookmark);
}
else
{
Fx.Assert(multipleBookmarks != null, "caller should never pass null");
for (int i = 0; i < multipleBookmarks.Count; i++)
{
Bookmark bookmark = multipleBookmarks[i];
PurgeSingleBookmark(bookmark);
}
}
}
internal void PurgeSingleBookmark(Bookmark bookmark)
{
Fx.Assert(this.bookmarks.ContainsKey(bookmark) && object.ReferenceEquals(bookmark, this.bookmarks[bookmark].Bookmark), "Something went wrong with our housekeeping - it must exist and must be our intenral reference");
UpdateExclusiveHandleList(bookmark);
this.bookmarks.Remove(bookmark);
}
public bool Remove(Bookmark bookmark, ActivityInstance instanceAttemptingRemove)
{
// We need to translate to our internal instance of the bookmark. See TryGetBookmarkFromInternalList
// for more details.
BookmarkCallbackWrapper callbackWrapper;
Bookmark internalBookmark;
if (TryGetBookmarkFromInternalList(bookmark, out internalBookmark, out callbackWrapper))
{
if (callbackWrapper.ActivityInstance != instanceAttemptingRemove)
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.OnlyBookmarkOwnerCanRemove));
}
Remove(internalBookmark, callbackWrapper);
return true;
}
else
{
return false;
}
}
void Remove(Bookmark bookmark, BookmarkCallbackWrapper callbackWrapper)
{
callbackWrapper.ActivityInstance.RemoveBookmark(bookmark, callbackWrapper.Options);
UpdateExclusiveHandleList(bookmark);
this.bookmarks.Remove(bookmark);
}
void UpdateExclusiveHandleList(Bookmark bookmark)
{
if (bookmark.ExclusiveHandles != null)
{
for (int i = 0; i < bookmark.ExclusiveHandles.Count; i++)
{
ExclusiveHandle handle = bookmark.ExclusiveHandles[i];
Fx.Assert(handle != null, "Internal error.. ExclusiveHandle was null");
handle.RemoveBookmark(bookmark);
}
}
}
}
}
|