public void InitializeTemplate(List <KeyValuePairEx <string, ProtectedString> > runtimeParameters, CefControl cefControl, string usedEntryName, PwUuid pwUuid) { PwUuid = pwUuid.ToString(); PwUidBytes = pwUuid.UuidBytes; UsedEntryName = usedEntryName; AvailableResources = new List <KeyValuePairEx <string, object> >(); foreach (var runtimeParameterKeyValuePair in runtimeParameters) { string identifier = runtimeParameterKeyValuePair.Key; AvailableResources.Add(new KeyValuePairEx <string, object>(identifier, new Text(runtimeParameterKeyValuePair.Value))); } UsedTries = 0; _cefControl = cefControl; }
//Returns true on success or false on timeout/fail... public bool RunTemplate(TemplateElement element, CefControl cefControl) { LastTemplateElement = element.Name + " @ TemplateElement-ID " + element.UEID; BaseObject baseObject = null; if (element.BrowserActionOrCommand is BrowserCommand) { baseObject = ((BaseObject)element.BrowserActionOrCommand); if (baseObject.TimeoutInSec != null) { baseObject.Timeout = new TimeSpan(0, 0, baseObject.TimeoutInSec.Value); } } else if (element.BrowserActionOrCommand is BrowserAction) { baseObject = ((BaseObject)((BrowserAction)element.BrowserActionOrCommand).ActionObject); if (baseObject.TimeoutInSec != null) { baseObject.Timeout = new TimeSpan(0, 0, baseObject.TimeoutInSec.Value); } } //rework anyhow? //@ requirement calculation: is required a pwdef placeholder? foreach (var requiredParameterString in element.RequiredParameters) { foreach (var availableResource in AvailableResources) { if (requiredParameterString == availableResource.Key && availableResource.Value is Text) { KeyValuePairEx <string, string> data = new KeyValuePairEx <string, string>(requiredParameterString, ((Text)availableResource.Value).Value.ReadString()); baseObject.OverloadData.Add(data); } } } if (element.BrowserActionOrCommand is BrowserCommand) { BrowserCommand command = (BrowserCommand)element.BrowserActionOrCommand; command.UID = UID; command.GenerateNewUcid(UTID); _cefControl.AddCefBrowserCommand(element.BrowserActionOrCommand); while (true) { Thread.Sleep(100); if (CefControl.BrowserCommandsCompleted.ContainsKey(command.UCID)) { BrowserCommand browserCommandCompleted = (BrowserCommand)CefControl.BrowserCommandsCompleted[command.UCID]; foreach (var outputPlacholderToValuePair in browserCommandCompleted.ReturnedOutput) { string identifier = EncodeTemplateElementIdWithOutputName(element.UEID, outputPlacholderToValuePair.Key); int?forRemoving = null; for (var i = 0; i < AvailableResources.Count; i++) { if (AvailableResources[i].Key == identifier /* && AvailableResources[i].ExpectedValue == null*/) { forRemoving = i; } } if (forRemoving != null) { AvailableResources.RemoveAt((int)forRemoving); } AvailableResources.Add( new KeyValuePairEx <string, object>( identifier, new Text(new ProtectedString(true, outputPlacholderToValuePair.Value != null ? outputPlacholderToValuePair.Value : "")))); //else //{ // //Console.WriteLine("w000T"); //} } foreach (var conditionsToTemplateElement in element.ConditionBasedAppendedTemplateElements) { bool success = true; foreach (var condition in conditionsToTemplateElement.Key) { string firstOperand = condition.FirstOperand, secondOperand = condition.SecondOperand; if (firstOperand == BaseObject.ConvertStringToPlaceholderString("successfull")) { firstOperand = browserCommandCompleted.Successful.ToString(); } else if (firstOperand == BaseObject.ConvertStringToPlaceholderString("completed")) { firstOperand = browserCommandCompleted.Completed.ToString(); } else if (firstOperand.Contains(BaseObject.ConvertStringToPlaceholderString("output"))) { firstOperand = GetValueFromEncodedTemplateOperand(firstOperand, conditionsToTemplateElement.Value); } else if (element.RequiredParameters.Contains(firstOperand)) { foreach (var availableResource in AvailableResources) { if (availableResource.Key == firstOperand) { firstOperand = availableResource.Value.ToString(); break; } } } if (secondOperand == BaseObject.ConvertStringToPlaceholderString("successfull")) { secondOperand = browserCommandCompleted.Successful.ToString(); } else if (secondOperand == BaseObject.ConvertStringToPlaceholderString("completed")) { secondOperand = browserCommandCompleted.Completed.ToString(); } else if (secondOperand.Contains(BaseObject.ConvertStringToPlaceholderString("output"))) { secondOperand = GetValueFromEncodedTemplateOperand(firstOperand, conditionsToTemplateElement.Value); } else if (element.RequiredParameters.Contains(secondOperand)) { foreach (var availableResource in AvailableResources) { if (availableResource.Key == secondOperand) { secondOperand = availableResource.Value.ToString(); break; } } } if (!condition.Compare(firstOperand, secondOperand)) { success = false; break; } } if (success) { if (!RunTemplate(conditionsToTemplateElement.Value, _cefControl)) { return(false); } } } foreach (var elementSuccessCondition in element.SuccessConditions) { string firstOperand = elementSuccessCondition.FirstOperand, secondOperand = elementSuccessCondition.SecondOperand; if (firstOperand == BaseObject.ConvertStringToPlaceholderString("successfull")) { firstOperand = browserCommandCompleted.Successful.ToString(); } else if (firstOperand == BaseObject.ConvertStringToPlaceholderString("completed")) { firstOperand = browserCommandCompleted.Completed.ToString(); } else if (firstOperand.Contains(BaseObject.ConvertStringToPlaceholderString("output"))) { firstOperand = GetValueFromEncodedTemplateOperand(firstOperand, element); } else if (element.RequiredParameters.Contains(firstOperand)) { foreach (var availableResource in AvailableResources) { if (availableResource.Key == firstOperand) { firstOperand = availableResource.Value.ToString(); break; } } } if (secondOperand == BaseObject.ConvertStringToPlaceholderString("successfull")) { secondOperand = browserCommandCompleted.Successful.ToString(); } else if (secondOperand == BaseObject.ConvertStringToPlaceholderString("completed")) { secondOperand = browserCommandCompleted.Completed.ToString(); } else if (secondOperand.Contains(BaseObject.ConvertStringToPlaceholderString("output"))) { secondOperand = GetValueFromEncodedTemplateOperand(firstOperand, element); } else if (element.RequiredParameters.Contains(secondOperand)) { foreach (var availableResource in AvailableResources) { if (availableResource.Key == secondOperand) { secondOperand = availableResource.Value.ToString(); break; } } } if (!elementSuccessCondition.Compare(firstOperand, secondOperand)) { LastTemplateElementFailureReason = "Failed @ following success condition: " + elementSuccessCondition.ConditionToString( firstOperand, secondOperand); return(false); } } if (element.AppendedTemplateElement != null) { if (!RunTemplate(element.AppendedTemplateElement, _cefControl)) { return(false); } } break; } if (CefBrowserControl.Timeout.ShouldBreakDueTimeout(baseObject)) { LastTemplateElementFailureReason = "Command Timeout"; return(false); } } if (!((BrowserCommand)CefControl.BrowserCommandsCompleted[command.UCID]).Successful) { LastTemplateElementFailureReason = "Failed Browser Command in CefBrowser"; return(false); } } else if (element.BrowserActionOrCommand is BrowserAction) { BrowserAction action = (BrowserAction)element.BrowserActionOrCommand; action.UID = UID; action.GenerateNewUCID(UTID); ((BaseObject)action.ActionObject).OverloadData = baseObject.OverloadData; cefControl.AddCefBrowserAction(element.BrowserActionOrCommand); while (true) { Thread.Sleep(100); if (CefControl.BrowserActionsCompleted.ContainsKey(action.UCID)) { BrowserAction browserActionCompleted = (BrowserAction)CefControl.BrowserActionsCompleted[action.UCID]; BaseObject subObject = (BaseObject)browserActionCompleted.ActionObject; foreach (var outputPlacholderToValuePair in subObject.ReturnedOutput) { string identifier = EncodeTemplateElementIdWithOutputName(element.UEID, outputPlacholderToValuePair.Key); int?forRemoving = null; for (var i = 0; i < AvailableResources.Count; i++) { if (AvailableResources[i].Key == identifier /* && AvailableResources[i].ExpectedValue == null*/) { forRemoving = i; } } if (forRemoving != null) { AvailableResources.RemoveAt((int)forRemoving); } AvailableResources.Add( new KeyValuePairEx <string, object>( identifier, new Text(new ProtectedString(true, outputPlacholderToValuePair.Value != null ? outputPlacholderToValuePair.Value : "")))); //else //{ // //Console.WriteLine("w000T"); //} } foreach (var templateElement in TemplateElements) { if (templateElement.UEID == element.UEID) { templateElement.BrowserActionOrCommand = CefControl.BrowserActionsCompleted[action.UCID]; } } foreach (var conditionsToTemplateElement in element.ConditionBasedAppendedTemplateElements) { bool success = true; foreach (var condition in conditionsToTemplateElement.Key) { string firstOperand = condition.FirstOperand, secondOperand = condition.SecondOperand; if (firstOperand == BaseObject.ConvertStringToPlaceholderString("successfull")) { firstOperand = subObject.Successful.ToString(); } else if (firstOperand == BaseObject.ConvertStringToPlaceholderString("completed")) { firstOperand = subObject.Completed.ToString(); } else if (firstOperand.Contains(BaseObject.ConvertStringToPlaceholderString("output"))) { firstOperand = GetValueFromEncodedTemplateOperand(firstOperand, conditionsToTemplateElement.Value); } else if (element.RequiredParameters.Contains(firstOperand)) { foreach (var availableResource in AvailableResources) { if (availableResource.Key == firstOperand) { firstOperand = availableResource.Value.ToString(); break; } } } if (secondOperand == BaseObject.ConvertStringToPlaceholderString("successfull")) { secondOperand = subObject.Successful.ToString(); } else if (secondOperand == BaseObject.ConvertStringToPlaceholderString("completed")) { secondOperand = subObject.Completed.ToString(); } else if (secondOperand.Contains(BaseObject.ConvertStringToPlaceholderString("output"))) { secondOperand = GetValueFromEncodedTemplateOperand(firstOperand, conditionsToTemplateElement.Value); } else if (element.RequiredParameters.Contains(secondOperand)) { foreach (var availableResource in AvailableResources) { if (availableResource.Key == secondOperand) { secondOperand = availableResource.Value.ToString(); break; } } } if (!condition.Compare(firstOperand, secondOperand)) { success = false; break; } } if (success) { if (!RunTemplate(conditionsToTemplateElement.Value, _cefControl)) { return(false); } } } foreach (var elementSuccessCondition in element.SuccessConditions) { string firstOperand = elementSuccessCondition.FirstOperand, secondOperand = elementSuccessCondition.SecondOperand; if (firstOperand == BaseObject.ConvertStringToPlaceholderString("successfull")) { firstOperand = subObject.Successful.ToString(); } else if (firstOperand == BaseObject.ConvertStringToPlaceholderString("completed")) { firstOperand = subObject.Completed.ToString(); } else if (firstOperand.Contains(BaseObject.ConvertStringToPlaceholderString("output"))) { firstOperand = GetValueFromEncodedTemplateOperand(firstOperand, element); } else if (element.RequiredParameters.Contains(firstOperand)) { foreach (var availableResource in AvailableResources) { if (availableResource.Key == firstOperand) { firstOperand = availableResource.Value.ToString(); break; } } } if (secondOperand == BaseObject.ConvertStringToPlaceholderString("successfull")) { secondOperand = subObject.Successful.ToString(); } else if (secondOperand == BaseObject.ConvertStringToPlaceholderString("completed")) { secondOperand = subObject.Completed.ToString(); } else if (secondOperand.Contains(BaseObject.ConvertStringToPlaceholderString("output"))) { secondOperand = GetValueFromEncodedTemplateOperand(secondOperand, element); } else if (element.RequiredParameters.Contains(secondOperand)) { foreach (var availableResource in AvailableResources) { if (availableResource.Key == secondOperand) { secondOperand = availableResource.Value.ToString(); break; } } } if (!elementSuccessCondition.Compare(firstOperand, secondOperand)) { LastTemplateElementFailureReason = "Failed @ following success condition: " + elementSuccessCondition.ConditionToString( firstOperand, secondOperand); return(false); } } if (element.AppendedTemplateElement != null) { if (!RunTemplate(element.AppendedTemplateElement, _cefControl)) { return(false); } } break; } if (CefBrowserControl.Timeout.ShouldBreakDueTimeout(baseObject)) { LastTemplateElementFailureReason = "Action Timeout"; return(false); } } if (!((BaseObject)((BrowserAction)CefControl.BrowserActionsCompleted[action.UCID]).ActionObject).Successful) { string text = ""; foreach (var keyValuePair in ((BaseObject)action.ActionObject).ReturnedOutput) { text += "Entry: " + keyValuePair.Key + " --> " + keyValuePair.Value + "."; } LastTemplateElementFailureReason = "Failed Browser Action in CefBrowser: " + text; return(false); } } return(true); }