public void Then(CefV8Value promise, CefV8Context context, Action <PromiseResult> continuation) { var id = pendingPromises.Save(context.GetFrame().Identifier, new PendingPromise { Continuation = continuation }); using (new ContextHelper(context)) using (var global = context.GetGlobal()) using (var s = global.GetValue(HelperObjectName)) { var userData = s.GetUserData() as PromiseUserData; var waitForPromise = userData.WaitForPromise; using (var idValue = CefV8Value.CreateString(id.ToString())) { waitForPromise.ExecuteFunctionWithContext(context, null, new[] { promise, idValue }).Dispose(); } } }
public CefValue Serialize(object source, HashSet <object> seen, ObjectSerializer objectSerializer) { if (!CanHandle(source?.GetType())) { throw new InvalidOperationException(); } long frameId = 0; using (var context = CefV8Context.GetCurrentContext()) { frameId = context.GetFrame().Identifier; } var result = CefValue.Create(); result.SetNull(); var value = (CefV8Value)source; if (value.IsString) { result.SetString(value.GetStringValue()); } else if (value.IsBool) { result.SetBool(value.GetBoolValue()); } else if (value.IsDouble) { result.SetDouble(value.GetDoubleValue()); } else if (value.IsInt) { result.SetInt(value.GetIntValue()); } else if (value.IsUInt) { result.SetDouble(value.GetUIntValue()); } else if (value.IsDate) { result.SetTime(value.GetDateValue()); } else if (value.IsArray) { using (var list = CefListValue.Create()) { list.SetSize(value.GetArrayLength()); for (var i = 0; i < value.GetArrayLength(); i++) { list.SetValue(i, objectSerializer.Serialize(value.GetValue(i), seen)); } result.SetList(list); } } else if (value.IsFunction) { var callback = new Callback(value, promiseService, objectSerializer); var id = callbackRegistry.Save(frameId, callback); using (var list = CefDictionaryValue.Create()) using (var actualValue = CefDictionaryValue.Create()) { list.SetString(ObjectSerializer.TypeIdPropertyName, CallbackDescriptor.TypeId); actualValue.SetInt64(nameof(CallbackDescriptor.FunctionId), id); list.SetDictionary(ObjectSerializer.ValuePropertyName, actualValue); result.SetDictionary(list); } } else if (value.IsObject) { using (var dict = CefDictionaryValue.Create()) using (var actualValue = CefDictionaryValue.Create()) { dict.SetString(ObjectSerializer.TypeIdPropertyName, ObjectSerializer.DictionaryTypeId); if (value.TryGetKeys(out var keys)) { foreach (var key in keys) { actualValue.SetValue(key, objectSerializer.Serialize(value.GetValue(key), seen)); } } dict.SetDictionary(ObjectSerializer.ValuePropertyName, actualValue); result.SetDictionary(dict); } } return(result); }