forked from MicrosoftEdge/WebView2Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBridgeAddRemoteObject.cs
181 lines (164 loc) · 6.76 KB
/
BridgeAddRemoteObject.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (C) Microsoft Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Reflection;
using System;
namespace WebView2WpfBrowser
{
// BridgeAddRemoteObject is a .NET object that implements IDispatch and works with AddRemoteObject.
// See the AddRemoteObjectCmdExecute method that demonstrates how to use it from JavaScript.
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class AnotherRemoteObject
{
// Sample property.
public string Prop { get; set; } = "AnotherRemoteObject.Prop";
}
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class BridgeAddRemoteObject
{
// Sample function that takes a parameter.
public string Func(string param)
{
return "BridgeAddRemoteObject.Func(" + param + ")";
}
// Sample function that takes no parameters.
public string Func2()
{
return "BridgeAddRemoteObject.Func2()";
}
// Get type of an object.
public string GetObjectType(object obj)
{
return obj.GetType().Name;
}
// Sample property.
public string Prop { get; set; } = "BridgeAddRemoteObject.Prop";
public AnotherRemoteObject AnotherObject { get; set; } = new AnotherRemoteObject();
// Sample indexed property.
[System.Runtime.CompilerServices.IndexerName("Items")]
public string this[int index]
{
get { return m_dictionary[index]; }
set { m_dictionary[index] = value; }
}
private Dictionary<int, string> m_dictionary = new Dictionary<int, string>();
static public string scenarioHtml =
@"<!DOCTYPE html>
<button id='runTest'>Run Test</button>
<button id='method'>Method with param</button>
<button id='methodNoParam'>Method no param</button>
<button id='propertyGet'>Property Get</button>
<button id='propertySet'>Property Set</button>
<button id='indexerGet'>Get object[index]</button>
<button id='indexerSet'>Set object[index]</button>
<button id='indexerGetAlias'>Get Items[index]</button>
<button id='indexerSetAlias'>Set Items[index]</button>
<script>
function getStack() {
try {
throw new Error('');
} catch (error) {
return error.stack;
}
}
function valueToString(value) {
return '(' + JSON.stringify(value) + ', ' + (typeof value) + ')';
}
async function assert(condition, text, message) {
if (!condition) {
alert('Assertion failed, ' + text + ': ' + message);
} else {
console.log('assert passed: ' + text);
}
}
function assertEqual(actual, expected, text)
{
assert(expected === actual, text, ('Equality assertion failed. ' +
'Expected ' + valueToString(expected) + ', ' +
'Actual ' + valueToString(actual) + ', ' + getStack()));
}
document.getElementById('runTest').addEventListener('click', async () => {
const bridge = chrome.webview.hostObjects.bridge;
let expected_result = 'value1';
bridge.Prop = expected_result;
let result = await bridge.Prop;
assertEqual(result, expected_result, 'property on bridge');
const value2 = 'value2';
result = await bridge.Func(value2);
expected_result = 'BridgeAddRemoteObject.Func(' + value2 + ')';
assertEqual(result, expected_result, 'method with parameter');
result = await bridge.Func2();
expected_result = 'BridgeAddRemoteObject.Func2()';
assertEqual(result, expected_result, 'method with no parameter');
const another_object = bridge.AnotherObject;
another_object.Prop = value2;
result = await another_object.Prop;
expected_result = value2;
assertEqual(result, expected_result, 'property on another_object');
let index = 123;
expected_result = 'aa';
bridge[index] = expected_result;
result = await bridge[index];
assertEqual(result, expected_result, 'bridge[index]');
index = 321;
expected_result = 'bb';
bridge.Items[index] = expected_result;
result = await bridge.Items[index];
assertEqual(result, expected_result, 'bridge.Items[index]');
let resolved_bridge = await bridge;
result = await bridge.GetObjectType(resolved_bridge);
expected_result = 'BridgeAddRemoteObject';
assertEqual(result, expected_result, 'type of resolved_bridge');
result = await bridge.GetObjectType(bridge);
expected_result = 'BridgeAddRemoteObject';
assertEqual(result, expected_result, 'type of bridge');
result = await bridge.GetObjectType(another_object);
expected_result = 'AnotherRemoteObject';
assertEqual(result, expected_result, 'type of another_object');
alert('Test End');
});
document.getElementById('method').addEventListener('click', async () => {
const bridge = chrome.webview.hostObjects.bridge;
const result = await bridge.Func(prompt('Method parameter text', 'Method parameter text'));
alert(result);
});
document.getElementById('methodNoParam').addEventListener('click', async () => {
const bridge = chrome.webview.hostObjects.bridge;
const result = await bridge.Func2();
alert(result);
});
document.getElementById('propertyGet').addEventListener('click', async () => {
const bridge = chrome.webview.hostObjects.bridge;
const result = await bridge.Prop;
alert(result);
});
document.getElementById('propertySet').addEventListener('click', async () => {
const bridge = chrome.webview.hostObjects.bridge;
bridge.Prop = prompt('Property text', 'Property text');
});
document.getElementById('indexerGet').addEventListener('click', async () => {
const bridge = chrome.webview.hostObjects.bridge;
const result = await bridge[1];
alert(result);
});
document.getElementById('indexerSet').addEventListener('click', async () => {
const bridge = chrome.webview.hostObjects.bridge;
bridge[1] = prompt('Property text', 'Property text');
});
document.getElementById('indexerGetAlias').addEventListener('click', async () => {
const bridge = chrome.webview.hostObjects.bridge;
const result = await bridge.Items[12];
alert(result);
});
document.getElementById('indexerSetAlias').addEventListener('click', async () => {
const bridge = chrome.webview.hostObjects.bridge;
bridge.Items[12] = prompt('Property text', 'Property text');
});
</script>
";
}
}