Type.registerNamespace("Microsoft.UpdateServices.Catalog");
Microsoft.UpdateServices.Catalog.DownloadBasket = function()
{
var UpdateSeparator = "#";
var EscapeCharacter = "%";
var SeparatorReplacement = "@";
var StorageKey = "STORAGE_KEY";
var StorageAttribute = "name";
this._cachedEntries = new ActiveXObject("Scripting.Dictionary");
this._count = 0;
this._stamp = 0;
this._exceptionHelper = new Microsoft.UpdateServices.Catalog.BasketExceptionHelper();
this.addUpdates = function(updateList)
{
if(!(updateList instanceof Array) || typeof(updateList) == typeof(Microsoft.UpdateServices.Catalog.Constants.UndefinedValue) || updateList == null)
{
this._exceptionHelper.throwArgumentException("updateList parameter is either null, undefined or is not an array.");
}
var newUpdatesCount = 0;
var dataChunks = new Array();
for (var i=0; i < updateList.length; i++)
{
if(!this.containsUpdate(updateList[i].get_uid()))
{
dataChunks[newUpdatesCount++] = this._escapeData(updateList[i].getSerializationData());
this._cachedEntries.Add(updateList[i].get_uid(), updateList[i]);
}
}
if(newUpdatesCount > 0)
{
try
{
this._appendData(dataChunks.join(UpdateSeparator));
this._count = this._count + newUpdatesCount;
}
catch(exception)
{
if(this._exceptionHelper.isBasketFullException(exception))
this._intializeCache();
throw exception;
}
this.contentChanged.invoke(this, null);
}
}
this.removeUpdates = function(updateIdList)
{
if(!(updateIdList instanceof Array) || typeof(updateIdList) == typeof(Microsoft.UpdateServices.Catalog.Constants.UndefinedValue) || updateIdList == null)
{
this._exceptionHelper.throwArgumentException("updateIdList parameter is either null, undefined or is not an array.");
}
var removedUpdatesCount = 0;
for (var i=0; i < updateIdList.length; i++)
{
if(this._cachedEntries.Exists(updateIdList[i]))
{
this._cachedEntries.Remove(updateIdList[i]);
removedUpdatesCount++;
}
}
if( removedUpdatesCount > 0 )
{
var dataChunks = new Array();
var cachedEntriesItemsArray = (new VBArray(this._cachedEntries.Items())).toArray();
for (var j=0; j < cachedEntriesItemsArray.length; j++)
{
var update = cachedEntriesItemsArray[j];
dataChunks[j] = this._escapeData(update.getSerializationData());
}
try
{
this._saveData(dataChunks.join(UpdateSeparator));
this._count = this._count - removedUpdatesCount;
}
catch(exception)
{
if(this._exceptionHelper.isBasketFullException(exception))
this._intializeCache();
throw exception;
}
this.contentChanged.invoke(this, null);
}
}
this.getUpdates = function(updateIdList)
{
if(!(updateIdList instanceof Array) || typeof(updateIdList) == typeof(Microsoft.UpdateServices.Catalog.Constants.UndefinedValue) || updateIdList == null)
{
this._exceptionHelper.throwArgumentException("updateIdList parameter is either null, undefined or is not an array.");
}
var updateList = new Array();
for (var i=0; i < updateIdList.length; i++)
{
updateList[i] = this._cachedEntries.Item(updateIdList[i]);
}
return updateList;
}
this.getAllUpdates = function()
{
var updateList = new Array();
var cachedUpdates = (new VBArray(this._cachedEntries.Items())).toArray();
for (var i=0; i < cachedUpdates.length; i++)
{
updateList[i] = cachedUpdates[i];
}
return updateList;
}
this.containsUpdate = function(UID)
{
if(typeof(UID) == typeof(Microsoft.UpdateServices.Catalog.Constants.UndefinedValue) || UID == null)
{
this._exceptionHelper.throwArgumentException("UID parameter is either null or undefined.");
}
return this._cachedEntries.Exists(UID);
}
this.removeAllUpdates = function()
{
var oldCount = this._count;
this._saveData(Microsoft.UpdateServices.Catalog.Constants.EmptyString);
this._clearCache();
if(oldCount > 0)
{
this.contentChanged.invoke(this, null);
}
}
this.get_count = function()
{
return this._count;
}
this.syncCache = function()
{
this._intializeCache();
}
this.contentChanged = new Microsoft.UpdateServices.Catalog.Event(this, false);
this._saveData = function(data)
{
try
{
var newStamp = (this._stamp + 1) % Microsoft.UpdateServices.Catalog.Constants.MaxInt;
this._storageElement.setAttribute(StorageAttribute, Microsoft.UpdateServices.Catalog.Update.VERSION + UpdateSeparator + newStamp + UpdateSeparator + data);
this._storageElement.save(StorageKey);
this._stamp = newStamp;
}
catch(exception)
{
if(exception.number == Microsoft.UpdateServices.Catalog.Constants.ErrorUserDataFull)
{
this._exceptionHelper.throwBasketFullException("Basket is full.");
}
else
{
this._exceptionHelper.throwBasketAccessDeniedException("Data could not be written to userData.");
}
}
}
this._loadData = function()
{
var version = Microsoft.UpdateServices.Catalog.Update.VERSION;
var payLoad = Microsoft.UpdateServices.Catalog.Constants.EmptyString;
var intermediaryPayLoad;
var stamp = 0;
try
{
this._storageElement.load(StorageKey);
}
catch(exception)
{
this._exceptionHelper.throwBasketAccessDeniedException("Data could not be read from userData.");
}
var storedData = this._storageElement.getAttribute(StorageAttribute);
if(storedData == null || storedData == Microsoft.UpdateServices.Catalog.Constants.EmptyString)
{
return [version, stamp, Microsoft.UpdateServices.Catalog.Constants.EmptyString];
}
var firstSeparatorIndex = storedData.indexOf(UpdateSeparator);
if(firstSeparatorIndex != -1)
{
version = parseInt(storedData.substr(0, firstSeparatorIndex));
if(isNaN(version))
this._exceptionHelper.throwBasketCorruptedVersionException("Version data missing from userData.");
intermediaryPayLoad = storedData.substring(firstSeparatorIndex + 1);
}
else
{
this._exceptionHelper.throwBasketCorruptedVersionException("Version data missing from userData.");
}
var secondSeparatorIndex = intermediaryPayLoad.indexOf(UpdateSeparator);
if(secondSeparatorIndex != -1)
{
stamp = parseInt(intermediaryPayLoad.substr(0, secondSeparatorIndex));
if(isNaN(stamp))
this._exceptionHelper.throwBasketCorruptedStampException("Stamp missing from userData.");
payLoad = intermediaryPayLoad.substring(secondSeparatorIndex + 1);
}
else
{
this._exceptionHelper.throwBasketCorruptedStampException("Stamp missing from userData.");
}
return [version, stamp, payLoad];
}
this._appendData = function(data)
{
var storedData = this._loadData();
var version = storedData[0];
var stamp = storedData[1];
var currentData = storedData[2];
if(currentData == Microsoft.UpdateServices.Catalog.Constants.EmptyString)
{
this._saveData(data);
}
else
{
this._saveData(currentData + UpdateSeparator + data);
}
}
this._createUserDataElement = function()
{
this._storageElement = document.createElement("input");
this._storageElement.type = "hidden";
this._storageElement.style.behavior = "url(#default#userData)";
document.body.appendChild(this._storageElement);
}
this._attachToOnWindowsActivateEvent = function()
{
window.attachEvent('onfocus', Function.createDelegate(this, this._onWindowActivate));
}
this._onWindowActivate = function()
{
this.syncCache();
}
this._escapeData = function(serializationData)
{
var escapedData = serializationData.replace(new RegExp(EscapeCharacter, "g"), EscapeCharacter + EscapeCharacter);
return escapedData.replace(new RegExp(UpdateSeparator, "g"), EscapeCharacter + SeparatorReplacement);
}
this._unescapeData = function(serializationData)
{
var regExpString = EscapeCharacter + "([" + EscapeCharacter + SeparatorReplacement + "])";
var regularExpression = new RegExp(regExpString, "g");
var escapedData = serializationData.replace
(
regularExpression,
function(match)
{
if (match == EscapeCharacter + SeparatorReplacement)
{
return UpdateSeparator;
}
else
{
return EscapeCharacter;
}
}
);
return escapedData;
}
this._intializeCache = function()
{
try
{
var storedData = this._loadData();
var version = storedData[0];
var stamp = storedData[1];
var payLoad = storedData[2];
if(stamp <= this._stamp)
return;
this._stamp = stamp;
this._clearCache();
this._count = 0;
if(payLoad == null || payLoad.length == 0)
return;
var updateEntryList = payLoad.split(UpdateSeparator);
var updatesToBeCached = new Array();
for (var i=0; i < updateEntryList.length; i++)
{
var serializedData = this._unescapeData(updateEntryList[i]);
updatesToBeCached[i] = Microsoft.UpdateServices.Catalog.Update.fromSerializationData(serializedData, version);
}
for (var j=0; j < updatesToBeCached.length; j++)
{
this._cachedEntries.Add(updatesToBeCached[j].get_uid(), updatesToBeCached[j]);
}
this._count = updatesToBeCached.length;
}
catch(exception)
{
this.removeAllUpdates();
if(this._exceptionHelper.isUpdatePersistenceCorrupted(exception) || this._exceptionHelper.isUpdateVersionUnknown(exception))
{
this._exceptionHelper.throwBasketCorruptedDataException(exception.message);
}
throw exception;
}
}
this._clearCache = function()
{
this._cachedEntries.RemoveAll();
this._count = 0;
}
this._createUserDataElement();
this._attachToOnWindowsActivateEvent();
this._intializeCache();
}
Microsoft.UpdateServices.Catalog.DownloadBasket.registerClass("Microsoft.UpdateServices.Catalog.DownloadBasket");
Microsoft.UpdateServices.Catalog.DownloadBasket._instance = null;
Microsoft.UpdateServices.Catalog.DownloadBasket.get_instance = function()
{
if(Microsoft.UpdateServices.Catalog.DownloadBasket._instance == null)
Microsoft.UpdateServices.Catalog.DownloadBasket._instance = new Microsoft.UpdateServices.Catalog.DownloadBasket();
return Microsoft.UpdateServices.Catalog.DownloadBasket._instance;
}
Microsoft.UpdateServices.Catalog.DownloadBasketException = function(sMessage, iCode)
{
Microsoft.UpdateServices.Catalog.DownloadBasketException.initializeBase(this);
}
Microsoft.UpdateServices.Catalog.DownloadBasketException.registerClass("Microsoft.UpdateServices.Catalog.DownloadBasketException", Microsoft.UpdateServices.Catalog.Exception);
Microsoft.UpdateServices.Catalog.BasketExceptionHelper = function()
{
this.isBasketPersistenceCorrupted = function(exception)
{
if(this.isBasketPersistenceCorruptedDataException(exception) || this.isBasketPersistenceCorruptedVersionException(exception))
return true;
return false;
}
this.isBasketPersistenceCorruptedVersionException = function(exception)
{
var isRequiredType = (exception instanceof Microsoft.UpdateServices.Catalog.DownloadBasketException);
var isRequiredCode = (exception.code == Microsoft.UpdateServices.Catalog.Constants.ErrorPersistenceStorageVersionCorrupted);
return isRequiredType && isRequiredCode;
}
this.isBasketPersistenceCorruptedDataException = function(exception)
{
var isRequiredType = (exception instanceof Microsoft.UpdateServices.Catalog.DownloadBasketException);
var isRequiredCode = (exception.code == Microsoft.UpdateServices.Catalog.Constants.ErrorPersistenceStorageDataCorrupted);
return isRequiredType && isRequiredCode;
}
this.isBasketFullException = function(exception)
{
var isRequiredType = (exception instanceof Microsoft.UpdateServices.Catalog.DownloadBasketException);
var isRequiredCode = (exception.code == Microsoft.UpdateServices.Catalog.Constants.ErrorPersistenceStorageFull);
return isRequiredType && isRequiredCode;
}
this.isBasketAccessDeniedException = function(exception)
{
var isRequiredType = (exception instanceof Microsoft.UpdateServices.Catalog.DownloadBasketException);
var isRequiredCode = (exception.code == Microsoft.UpdateServices.Catalog.Constants.ErrorPersistenceStorageAccessDenied);
return isRequiredType && isRequiredCode;
}
this.isUpdatePersistenceCorrupted = function(exception)
{
var isRequiredType = (exception instanceof Microsoft.UpdateServices.Catalog.UpdateException );
var isRequiredCode = (exception.code == Microsoft.UpdateServices.Catalog.Constants.ErrorPersistenceUpdateDataCorrupted);
return isRequiredType && isRequiredCode;
}
this.isUpdateVersionUnknown = function(exception)
{
var isRequiredType = (exception instanceof Microsoft.UpdateServices.Catalog.UpdateException);
var isRequiredCode = (exception.code == Microsoft.UpdateServices.Catalog.Constants.ErrorPersistenceUpdateVersionUnknown);
return isRequiredType && isRequiredCode;
}
this.throwBasketCorruptedDataException = function(sMessage)
{
throw new Microsoft.UpdateServices.Catalog.DownloadBasketException(sMessage, Microsoft.UpdateServices.Catalog.Constants.ErrorPersistenceStorageDataCorrupted);
}
this.throwBasketCorruptedVersionException = function(sMessage)
{
throw new Microsoft.UpdateServices.Catalog.DownloadBasketException(sMessage, Microsoft.UpdateServices.Catalog.Constants.ErrorPersistenceStorageVersionCorrupted);
}
this.throwBasketCorruptedStampException = function(sMessage)
{
throw new Microsoft.UpdateServices.Catalog.DownloadBasketException(sMessage, Microsoft.UpdateServices.Catalog.Constants.ErrorPersistenceStorageStampCorrupted);
}
this.throwBasketFullException = function(sMessage)
{
throw new Microsoft.UpdateServices.Catalog.DownloadBasketException(sMessage, Microsoft.UpdateServices.Catalog.Constants.ErrorPersistenceStorageFull);
}
this.throwBasketAccessDeniedException = function(sMessage)
{
throw new Microsoft.UpdateServices.Catalog.DownloadBasketException(sMessage, Microsoft.UpdateServices.Catalog.Constants.ErrorPersistenceStorageAccessDenied);
}
this.throwArgumentException = function(sMessage)
{
throw new Microsoft.UpdateServices.Catalog.ArgumentException(sMessage, Microsoft.UpdateServices.Catalog.Constants.ErrorArgumentInvalid);
}
}
Microsoft.UpdateServices.Catalog.BasketExceptionHelper.registerClass("Microsoft.UpdateServices.Catalog.BasketExceptionHelper");
