From d82232241ffcc55b9e0ec92bc0615a4278cce822 Mon Sep 17 00:00:00 2001 From: amaitland Date: Fri, 11 Jul 2014 16:30:28 +1000 Subject: [PATCH] Remove Description classes - simplify code --- .../JavascriptMethodWrapper.cpp | 2 +- .../JavascriptMethodWrapper.h | 5 ++- .../JavascriptPropertyWrapper.cpp | 4 +- .../JavascriptPropertyWrapper.h | 5 ++- CefSharp/CefSharp.csproj | 3 -- CefSharp/Internals/JavascriptMember.cs | 29 ++++++++++++- .../Internals/JavascriptMemberDescription.cs | 43 ------------------- CefSharp/Internals/JavascriptMethod.cs | 19 ++++---- .../Internals/JavascriptMethodDescription.cs | 24 ----------- CefSharp/Internals/JavascriptObject.cs | 6 +-- .../Internals/JavascriptObjectRepository.cs | 19 ++++---- CefSharp/Internals/JavascriptProperty.cs | 33 ++++++++------ .../JavascriptPropertyDescription.cs | 30 ------------- 13 files changed, 79 insertions(+), 143 deletions(-) delete mode 100644 CefSharp/Internals/JavascriptMemberDescription.cs delete mode 100644 CefSharp/Internals/JavascriptMethodDescription.cs delete mode 100644 CefSharp/Internals/JavascriptPropertyDescription.cs diff --git a/CefSharp.BrowserSubprocess.Core/JavascriptMethodWrapper.cpp b/CefSharp.BrowserSubprocess.Core/JavascriptMethodWrapper.cpp index 1db4a8482..40cd69b9f 100644 --- a/CefSharp.BrowserSubprocess.Core/JavascriptMethodWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/JavascriptMethodWrapper.cpp @@ -16,6 +16,6 @@ namespace CefSharp { auto browserProxy = CefAppWrapper::Instance->CreateBrowserProxy(); - return browserProxy->CallMethod(_owner->Id, Description->ManagedName, parameters); + return browserProxy->CallMethod(_owner->Id, ManagedName, parameters); } } \ No newline at end of file diff --git a/CefSharp.BrowserSubprocess.Core/JavascriptMethodWrapper.h b/CefSharp.BrowserSubprocess.Core/JavascriptMethodWrapper.h index e3ac21798..a6a15a016 100644 --- a/CefSharp.BrowserSubprocess.Core/JavascriptMethodWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/JavascriptMethodWrapper.h @@ -30,7 +30,7 @@ namespace CefSharp { _owner = static_cast(owner); - auto methodName = StringUtils::ToNative(Description->JavascriptName); + auto methodName = StringUtils::ToNative(JavascriptName); auto v8Value = CefV8Value::CreateFunction(methodName, _javascriptMethodHandler.get()); _owner->V8Value->SetValue(methodName, v8Value, V8_PROPERTY_ATTRIBUTE_NONE); @@ -38,7 +38,8 @@ namespace CefSharp void Clone(JavascriptMethod^ obj) { - Description = obj->Description; + JavascriptName = obj->JavascriptName; + ManagedName = obj->ManagedName; } Object^ Execute(array^ parameters); diff --git a/CefSharp.BrowserSubprocess.Core/JavascriptPropertyWrapper.cpp b/CefSharp.BrowserSubprocess.Core/JavascriptPropertyWrapper.cpp index 9fe721de5..f85f3c3a7 100644 --- a/CefSharp.BrowserSubprocess.Core/JavascriptPropertyWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/JavascriptPropertyWrapper.cpp @@ -16,13 +16,13 @@ namespace CefSharp { auto browserProxy = CefAppWrapper::Instance->CreateBrowserProxy(); - return browserProxy->SetProperty(_owner->Id, Description->ManagedName, value); + return browserProxy->SetProperty(_owner->Id, ManagedName, value); }; Object^ JavascriptPropertyWrapper::GetProperty() { auto browserProxy = CefAppWrapper::Instance->CreateBrowserProxy(); - return browserProxy->GetProperty(_owner->Id, Description->ManagedName); + return browserProxy->GetProperty(_owner->Id, ManagedName); }; } \ No newline at end of file diff --git a/CefSharp.BrowserSubprocess.Core/JavascriptPropertyWrapper.h b/CefSharp.BrowserSubprocess.Core/JavascriptPropertyWrapper.h index eeb032a54..8f6dc1997 100644 --- a/CefSharp.BrowserSubprocess.Core/JavascriptPropertyWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/JavascriptPropertyWrapper.h @@ -38,7 +38,7 @@ namespace CefSharp virtual void Bind(JavascriptObject^ owner) { _owner = static_cast(owner); - auto methodName = StringUtils::ToNative(Description->JavascriptName); + auto methodName = StringUtils::ToNative(JavascriptName); auto v8Value = _owner->V8Value->CreateObject(_javascriptPropertyHandler.get()); JsObject->V8Value = v8Value; @@ -50,7 +50,8 @@ namespace CefSharp void Clone(JavascriptProperty^ obj) { - Description = obj->Description; + JavascriptName = obj->JavascriptName; + ManagedName = obj->ManagedName; JsObject->Clone(obj->Value); } diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index 03f5083b2..29551ab99 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -104,14 +104,11 @@ - - - diff --git a/CefSharp/Internals/JavascriptMember.cs b/CefSharp/Internals/JavascriptMember.cs index 6dee1e4cb..50ac2ba4f 100644 --- a/CefSharp/Internals/JavascriptMember.cs +++ b/CefSharp/Internals/JavascriptMember.cs @@ -7,15 +7,40 @@ namespace CefSharp.Internals [DataContract] public abstract class JavascriptMember { + /// + /// Identifies the for BrowserProcess to RenderProcess communication + /// [DataMember] - public JavascriptMemberDescription Description { get; set; } + public long Id { get; set; } + + /// + /// Gets or sets the name of the managed property. + /// + [DataMember] + public string ManagedName { get; set; } + + /// + /// Gets or sets the name of the property in the JavaScript runtime. + /// + [DataMember] + public string JavascriptName { get; set; } [DataMember] public long DescriptionId { get; set; } public override string ToString() { - return Description != null ? Description.ToString() : base.ToString(); + return ManagedName ?? base.ToString(); + } + + public static string LowercaseFirst(string str) + { + if (string.IsNullOrEmpty(str)) + { + return string.Empty; + } + + return char.ToLower(str[0]) + str.Substring(1); } } } diff --git a/CefSharp/Internals/JavascriptMemberDescription.cs b/CefSharp/Internals/JavascriptMemberDescription.cs deleted file mode 100644 index a2a64f040..000000000 --- a/CefSharp/Internals/JavascriptMemberDescription.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Runtime.Serialization; - -namespace CefSharp.Internals -{ - [KnownType(typeof(JavascriptMethodDescription))] - [KnownType(typeof(JavascriptPropertyDescription))] - [DataContract] - public abstract class JavascriptMemberDescription - { - /// - /// Identifies the for BrowserProcess to RenderProcess communication - /// - [DataMember] - public long Id { get; set; } - - /// - /// Gets or sets the name of the managed property. - /// - [DataMember] - public string ManagedName { get; set; } - - /// - /// Gets or sets the name of the property in the JavaScript runtime. - /// - [DataMember] - public string JavascriptName { get; set; } - - public static string LowercaseFirst(string str) - { - if (string.IsNullOrEmpty(str)) - { - return string.Empty; - } - - return char.ToLower(str[0]) + str.Substring(1); - } - - public override string ToString() - { - return ManagedName; - } - } -} \ No newline at end of file diff --git a/CefSharp/Internals/JavascriptMethod.cs b/CefSharp/Internals/JavascriptMethod.cs index 7414b87a1..38d232e42 100644 --- a/CefSharp/Internals/JavascriptMethod.cs +++ b/CefSharp/Internals/JavascriptMethod.cs @@ -1,19 +1,22 @@ -using System.Runtime.Serialization; +using System; +using System.Reflection; +using System.Runtime.Serialization; namespace CefSharp.Internals { [DataContract] public class JavascriptMethod : JavascriptMember { - public JavascriptMethod() - { - Description = new JavascriptMethodDescription(); - } + /// + /// Gets or sets a delegate which is used to invoke the method if the member is a method. + /// + public Func Function { get; set; } - public new JavascriptMethodDescription Description + public void Analyse(MethodInfo method) { - get { return (JavascriptMethodDescription)base.Description; } - set { base.Description = value; } + ManagedName = method.Name; + JavascriptName = LowercaseFirst(method.Name); + Function = method.Invoke; } } } diff --git a/CefSharp/Internals/JavascriptMethodDescription.cs b/CefSharp/Internals/JavascriptMethodDescription.cs deleted file mode 100644 index 07cace98c..000000000 --- a/CefSharp/Internals/JavascriptMethodDescription.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Reflection; -using System.Runtime.Serialization; - -namespace CefSharp.Internals -{ - [DataContract] - public class JavascriptMethodDescription : JavascriptMemberDescription - { - // We cache the reflected delegate to improve performance a bit. - - /// - /// Gets or sets a delegate which is used to invoke the method if the member is a method. - /// - public Func Function { get; set; } - - public void Analyse(MethodInfo method) - { - ManagedName = method.Name; - JavascriptName = LowercaseFirst(method.Name); - Function = method.Invoke; - } - } -} \ No newline at end of file diff --git a/CefSharp/Internals/JavascriptObject.cs b/CefSharp/Internals/JavascriptObject.cs index 880da457b..43e0b08be 100644 --- a/CefSharp/Internals/JavascriptObject.cs +++ b/CefSharp/Internals/JavascriptObject.cs @@ -81,7 +81,7 @@ namespace CefSharp.Internals continue; } var jsMethod = new JavascriptMethod(); - jsMethod.Description.Analyse(methodInfo); + jsMethod.Analyse(methodInfo); Members.Add(jsMethod); } @@ -97,8 +97,8 @@ namespace CefSharp.Internals Value = repository.CreateJavascriptObject() }; - jsProperty.Description.Analyse(propertyInfo); - jsProperty.Value.Value = jsProperty.Description.GetValue(value); + jsProperty.Analyse(propertyInfo); + jsProperty.Value.Value = jsProperty.GetValue(value); jsProperty.Value.Analyse(repository); Members.Add(jsProperty); } diff --git a/CefSharp/Internals/JavascriptObjectRepository.cs b/CefSharp/Internals/JavascriptObjectRepository.cs index 7dfb61b18..c8930388c 100644 --- a/CefSharp/Internals/JavascriptObjectRepository.cs +++ b/CefSharp/Internals/JavascriptObjectRepository.cs @@ -27,11 +27,8 @@ namespace CefSharp.Internals var member = new JavascriptProperty { Value = CreateJavascriptObject(), - Description = - { - ManagedName = name, - JavascriptName = JavascriptMemberDescription.LowercaseFirst(name) - } + ManagedName = name, + JavascriptName = JavascriptMember.LowercaseFirst(name) }; member.Value.Value = value; member.Value.Analyse(this); @@ -48,13 +45,13 @@ namespace CefSharp.Internals return false; } - var method = obj.Members.OfType().FirstOrDefault(p => p.Description.ManagedName == name); + var method = obj.Members.OfType().FirstOrDefault(p => p.ManagedName == name); if (method == null) { throw new InvalidOperationException(string.Format("Method {0} not found on Object of Type {1}", name, obj.Value.GetType())); } - result = method.Description.Function(obj.Value, parameters); + result = method.Function(obj.Value, parameters); return true; } @@ -67,13 +64,13 @@ namespace CefSharp.Internals return false; } - var property = obj.Members.OfType().FirstOrDefault(p => p.Description.ManagedName == name); + var property = obj.Members.OfType().FirstOrDefault(p => p.ManagedName == name); if (property == null) { throw new InvalidOperationException(string.Format("Property {0} not found on Object of Type {1}", name, obj.Value.GetType())); } - result = property.Description.GetValue(obj.Value); + result = property.GetValue(obj.Value); return true; } @@ -85,13 +82,13 @@ namespace CefSharp.Internals return false; } - var property = obj.Members.OfType().FirstOrDefault(p => p.Description.ManagedName == name); + var property = obj.Members.OfType().FirstOrDefault(p => p.ManagedName == name); if (property == null) { throw new InvalidOperationException(string.Format("Property {0} not found on Object of Type {1}", name, obj.Value.GetType())); } - property.Description.SetValue(obj.Value, value); + property.SetValue(obj.Value, value); return true; } diff --git a/CefSharp/Internals/JavascriptProperty.cs b/CefSharp/Internals/JavascriptProperty.cs index 1e380cd18..6394fe902 100644 --- a/CefSharp/Internals/JavascriptProperty.cs +++ b/CefSharp/Internals/JavascriptProperty.cs @@ -1,22 +1,31 @@ -using System.Runtime.Serialization; +using System; +using System.Reflection; +using System.Runtime.Serialization; namespace CefSharp.Internals { [DataContract] public class JavascriptProperty : JavascriptMember { - public JavascriptProperty() - { - Description = new JavascriptPropertyDescription(); - } - - public new JavascriptPropertyDescription Description - { - get { return (JavascriptPropertyDescription)base.Description; } - set { base.Description = value; } - } - [DataMember] public JavascriptObject Value { get; set; } + + /// + /// Gets or sets a delegate which is used to set the property / field value in the managed object. + /// + public Action SetValue { get; set; } + + /// + /// Gets or sets a delegate which is used to get the property / field value from the managed object. + /// + public Func GetValue { get; set; } + + public void Analyse(PropertyInfo propertyInfo) + { + ManagedName = propertyInfo.Name; + JavascriptName = LowercaseFirst(propertyInfo.Name); + SetValue = (o, v) => propertyInfo.SetValue(o, v, null); + GetValue = (o) => propertyInfo.GetValue(o, null); + } } } diff --git a/CefSharp/Internals/JavascriptPropertyDescription.cs b/CefSharp/Internals/JavascriptPropertyDescription.cs deleted file mode 100644 index 7ae308ad0..000000000 --- a/CefSharp/Internals/JavascriptPropertyDescription.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using System.Reflection; -using System.Runtime.Serialization; - -namespace CefSharp.Internals -{ - [DataContract] - public class JavascriptPropertyDescription : JavascriptMemberDescription - { - // The following members are used to cache reflection code to improve performance - - /// - /// Gets or sets a delegate which is used to set the property / field value in the managed object. - /// - public Action SetValue { get; set; } - - /// - /// Gets or sets a delegate which is used to get the property / field value from the managed object. - /// - public Func GetValue { get; set; } - - public void Analyse(PropertyInfo propertyInfo) - { - ManagedName = propertyInfo.Name; - JavascriptName = LowercaseFirst(propertyInfo.Name); - SetValue = (o, v) => propertyInfo.SetValue(o, v, null); - GetValue = (o) => propertyInfo.GetValue(o, null); - } - } -} \ No newline at end of file