Skip to content

Commit a078eae

Browse files
ChrisBardsleyamaitland
authored andcommitted
Refactored changes from build#1644 to account for discussed changes.
1 parent f08e05a commit a078eae

File tree

8 files changed

+73
-51
lines changed

8 files changed

+73
-51
lines changed

CefSharp.Example/BoundObject.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
44

55
using System;
6+
using System.Linq;
67
using System.Threading.Tasks;
78

89
namespace CefSharp.Example
@@ -270,5 +271,25 @@ public SubBoundObject GetSubObject()
270271
{
271272
return SubObject;
272273
}
274+
275+
/// <summary>
276+
/// Demonstrates the use of params as an argument in a bound object
277+
/// </summary>
278+
/// <param name="name">Dummy Argument</param>
279+
/// <param name="args">Params Argument</param>
280+
public string MethodWithParams(string name, params object[] args)
281+
{
282+
return String.Join(", ", args.ToArray());
283+
}
284+
285+
public string MethodWithoutParams(string name, string arg2)
286+
{
287+
return String.Format("{0}, {1}", name, arg2);
288+
}
289+
290+
public string methodWithoutAnything()
291+
{
292+
return "Method without anything called and returned successfully.";
293+
}
273294
}
274295
}

CefSharp.Example/Resources/BindingTest.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,5 +237,34 @@
237237
}
238238
</script>
239239
</ul>
240+
<p>
241+
Javascript function sending variable number of parameters to Bound Object. (ParamArray Test)
242+
<script type="text/javascript">
243+
function invokeParamArrayMethod(func) {
244+
var result = func();
245+
document.getElementById('paramArrayResults').innerHTML = result;
246+
}
247+
</script>
248+
<ul>
249+
<li style="list-style:none;display:inline-block;">
250+
<button onclick="invokeParamArrayMethod(function(){return bound.methodWithParams('test', 'hello-world')})">With 1 Params</button>
251+
</li>
252+
<li style="list-style:none;display:inline-block;">
253+
<button onclick="invokeParamArrayMethod(function(){return bound.methodWithParams('test', 'hello-world', 'chris was here')})">With 2 Params</button>
254+
</li>
255+
<li style="list-style:none;display:inline-block;">
256+
<button onclick="invokeParamArrayMethod(function(){return bound.methodWithParams('test')})">With no Params</button>
257+
</li>
258+
<li style="list-style:none;display:inline-block;">
259+
<button onclick="invokeParamArrayMethod(function(){return bound.methodWithoutParams('test', 'hello')})">Normal Method</button>
260+
</li>
261+
<li style="list-style:none;display:inline-block;">
262+
<button onclick="invokeParamArrayMethod(function(){return bound.methodWithoutAnything()})">Normal Method No Params</button>
263+
</li>
264+
</ul>
265+
<span>ParamArray Results</span>
266+
<br />
267+
<span id="paramArrayResults"></span>
268+
</p>
240269
</body>
241270
</html>

CefSharp.Example/Resources/Home.html

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,6 @@ <h3 id="features-javascript-integration">JavaScript integration</h3>
351351
objects.
352352
</p>
353353
<h3>Hooks by which you can modify and/or override certain features of the web browsing</h3>
354-
<p>
355-
A fix to allow params to be used by the .NET methods called by the javascript<br />
356-
<button onclick="boundEvent.methodWithParams('test', 'hello-world')">With 1 Params</button>
357-
<button onclick="boundEvent.methodWithParams('test', 'hello-world', 'chris was here')">With 2 Params</button>
358-
<button onclick="boundEvent.methodWithParams('test')">With no Params</button>
359-
<button onclick="boundEvent.methodWithoutParams('test', 'hello')">Normal Method</button>
360-
<button onclick="boundEvent.methodWithoutAnything()">Normal Method No Params</button>
361-
</p>
362354
</div>
363355
<div class="bs-docs-section">
364356
<div class="page-header">

CefSharp.Example/ScriptedMethodsBoundObject.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,5 @@ public void RaiseEvent(string eventName, object eventData = null)
3636
EventArrived(eventName, eventData);
3737
}
3838
}
39-
40-
/// <summary>
41-
/// Demonstrates the use of params as an argument in a bound object
42-
/// </summary>
43-
/// <param name="name">Dummy Argument</param>
44-
/// <param name="args">Params Argument</param>
45-
public void MethodWithParams(string name, params object[] args)
46-
{
47-
var test = args.ElementAtOrDefault(0);
48-
var test2 = args.ElementAtOrDefault(1);
49-
}
50-
51-
public void MethodWithoutParams(string name, string arg2)
52-
{
53-
var test = arg2;
54-
}
55-
56-
public void methodWithoutAnything()
57-
{
58-
var test = "";
59-
}
6039
}
6140
}

CefSharp/Internals/JavascriptMethod.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public class JavascriptMethod
3434
[DataMember]
3535
public string JavascriptName { get; set; }
3636

37-
[DataMember]
3837
public List<MethodParameter> MethodParameters { get; set; }
3938

4039
/// <summary>

CefSharp/Internals/JavascriptObjectRepository.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,28 +104,37 @@ public bool TryCallMethod(long objectId, string name, object[] parameters, out o
104104

105105
try
106106
{
107-
//Fix for C# paramArray and refactor on arguments mismatch 03/22/2016
107+
//Added param array support. Current implementation support param Object only. 04/04/2016
108108
var missingParams = method.ParameterCount - parameters.Length;
109-
if (missingParams > 0 || method.MethodParameters.Any(t => t.IsParamArray))
109+
//CONDITION #1 : Check for parameter count missmatch between the parameters on the javascript function and the
110+
// number of parameters on the bound object method.
111+
//CONDITION #2 : Check if the bound object method contains a ParamArray as the last parameter on the method signature.
112+
if (missingParams > 0 || method.MethodParameters.LastOrDefault(t => t.IsParamArray) != null)
110113
{
111-
var paramList = new List<object>();
114+
var paramList = new List<object>(method.MethodParameters.Count);
112115

113-
for (int i = 0; i < method.MethodParameters.Count; i++)
116+
//Loop through all of the method parameters on the bound object.
117+
for (var i = 0; i < method.MethodParameters.Count; i++)
114118
{
119+
//Attempt to get the javascript function param at the current bound object parameter index.
120+
//If the javascript function param is missing IE: NULL, then add Type.Missing.
121+
//This will allow for default bound object parameters. IE: (string someParameter = "someValue")
115122
object jsParam = parameters.ElementAtOrDefault(i);
116123
if (jsParam == null && !method.MethodParameters[i].IsParamArray)
117124
{
118125
paramList.Add(Type.Missing);
119126
}
127+
//If the method parameter is a paramArray IE: (params Object[] someParameter)
128+
//grab the parameters from the javascript function starting at the current bound object parameter index
129+
//and add create an array that will be passed in as the last bound object method parameter.
120130
else if (method.MethodParameters[i].IsParamArray)
121131
{
122132
List<object> convertedParams = new List<object>();
123-
for (int s = i; s < parameters.Count(); s++)
133+
for (var s = i; s < parameters.Length; s++)
124134
{
125135
convertedParams.Add(parameters[s]);
126136
}
127137
paramList.Add(convertedParams.ToArray());
128-
break;
129138
}
130139
else
131140
{
@@ -307,9 +316,7 @@ private static JavascriptMethod CreateJavaScriptMethod(MethodInfo methodInfo, bo
307316
jsMethod.ParameterCount = methodInfo.GetParameters().Length;
308317
jsMethod.MethodParameters = methodInfo.GetParameters()
309318
.Select(t => new MethodParameter()
310-
{ Name = t.Name,
311-
ParameterType = t.ParameterType,
312-
Position = t.Position,
319+
{
313320
IsParamArray = t.GetCustomAttributes(typeof(ParamArrayAttribute), false).Length > 0
314321
}).ToList();
315322

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Runtime.Serialization;
5-
using System.Text;
1+
// Copyright © 2010-2016 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
using System;
65

76
namespace CefSharp.Internals
87
{
9-
[DataContract]
108
public class MethodParameter
119
{
12-
public String Name { get; set; }
13-
14-
public Type ParameterType { get; set; }
15-
1610
public Boolean IsParamArray { get; set; }
17-
18-
public Int32 Position { get; set; }
1911
}
2012
}

NuGet.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77
<add key="cefsharp-myget" value="https://www.myget.org/F/cefsharp/" />
88
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
99
</packageSources>
10+
<disabledPackageSources>
11+
<add key="nuget.org" value="true" />
12+
</disabledPackageSources>
1013
</configuration>

0 commit comments

Comments
 (0)