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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//! Exposed function bindings.
//!
//! This module provides functionality for exposing Rust functions to JavaScript
//! code running in the browser. These functions can be called from JavaScript
//! and will execute Rust code, returning the result back to JavaScript.
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{debug, trace, warn};
use viewpoint_cdp::CdpConnection;
use viewpoint_cdp::protocol::runtime::{AddBindingParams, BindingCalledEvent};
use viewpoint_js::js;
use crate::error::PageError;
/// Type alias for the binding callback function.
///
/// The callback receives a vector of JSON arguments and returns a JSON result.
pub type BindingCallback = Box<
dyn Fn(
Vec<serde_json::Value>,
) -> Pin<Box<dyn Future<Output = Result<serde_json::Value, String>> + Send>>
+ Send
+ Sync,
>;
/// Manager for exposed function bindings on a page.
pub struct BindingManager {
/// CDP connection.
connection: Arc<CdpConnection>,
/// Session ID.
session_id: String,
/// Registered bindings indexed by function name.
bindings: Arc<RwLock<HashMap<String, BindingCallback>>>,
/// Whether the manager is listening for binding calls.
is_listening: std::sync::atomic::AtomicBool,
}
impl BindingManager {
/// Create a new binding manager for a page.
pub fn new(connection: Arc<CdpConnection>, session_id: String) -> Self {
Self {
connection,
session_id,
bindings: Arc::new(RwLock::new(HashMap::new())),
is_listening: std::sync::atomic::AtomicBool::new(false),
}
}
/// Expose a function to JavaScript.
///
/// The function will be available as `window.<name>()` in JavaScript.
/// Arguments are passed as JSON values and the return value is also JSON.
///
/// # Example
///
/// ```no_run
/// use viewpoint_core::page::binding::BindingManager;
///
/// # async fn example(manager: BindingManager) -> Result<(), viewpoint_core::CoreError> {
/// manager.expose_function("compute", |args| async move {
/// let x: i64 = serde_json::from_value(args[0].clone()).map_err(|e| e.to_string())?;
/// let y: i64 = serde_json::from_value(args[1].clone()).map_err(|e| e.to_string())?;
/// serde_json::to_value(x + y).map_err(|e| e.to_string())
/// }).await?;
/// # Ok(())
/// # }
/// ```
pub async fn expose_function<F, Fut>(&self, name: &str, callback: F) -> Result<(), PageError>
where
F: Fn(Vec<serde_json::Value>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<serde_json::Value, String>> + Send + 'static,
{
debug!("Exposing function: {}", name);
// Add the binding via CDP
self.connection
.send_command::<_, serde_json::Value>(
"Runtime.addBinding",
Some(AddBindingParams {
name: name.to_string(),
execution_context_id: None,
execution_context_name: None,
}),
Some(&self.session_id),
)
.await?;
// Create a wrapper script that sets up the JavaScript function
let name_json = serde_json::to_string(name).unwrap();
let wrapper_script = js! {
(function() {
const bindingName = @{name_json};
const bindingFn = window[bindingName];
if (!bindingFn) return;
// Replace the binding with a proper async function
window[bindingName] = async function(...args) {
const seq = (window.__viewpoint_seq = (window.__viewpoint_seq || 0) + 1);
const payload = JSON.stringify({ seq, args });
return new Promise((resolve, reject) => {
window.__viewpoint_callbacks = window.__viewpoint_callbacks || {};
window.__viewpoint_callbacks[seq] = { resolve, reject };
bindingFn(payload);
});
};
})()
};
// Inject the wrapper script
self.connection
.send_command::<_, serde_json::Value>(
"Runtime.evaluate",
Some(viewpoint_cdp::protocol::runtime::EvaluateParams {
expression: wrapper_script,
object_group: None,
include_command_line_api: None,
silent: Some(true),
context_id: None,
return_by_value: Some(true),
await_promise: Some(false),
}),
Some(&self.session_id),
)
.await?;
// Store the callback
let boxed_callback: BindingCallback = Box::new(move |args| Box::pin(callback(args)));
{
let mut bindings = self.bindings.write().await;
bindings.insert(name.to_string(), boxed_callback);
}
// Start listening for binding calls if not already
self.start_listening().await;
debug!("Function exposed: {}", name);
Ok(())
}
/// Remove an exposed function.
pub async fn remove_function(&self, name: &str) -> Result<(), PageError> {
debug!("Removing exposed function: {}", name);
// Remove the binding via CDP
self.connection
.send_command::<_, serde_json::Value>(
"Runtime.removeBinding",
Some(viewpoint_cdp::protocol::runtime::RemoveBindingParams {
name: name.to_string(),
}),
Some(&self.session_id),
)
.await?;
// Remove from our registry
{
let mut bindings = self.bindings.write().await;
bindings.remove(name);
}
Ok(())
}
/// Start listening for binding call events.
async fn start_listening(&self) {
if self
.is_listening
.swap(true, std::sync::atomic::Ordering::SeqCst)
{
// Already listening
return;
}
let mut events = self.connection.subscribe_events();
let session_id = self.session_id.clone();
let bindings = self.bindings.clone();
let connection = self.connection.clone();
tokio::spawn(async move {
debug!("Binding manager started listening for events");
while let Ok(event) = events.recv().await {
// Filter events for this session
if event.session_id.as_deref() != Some(&session_id) {
continue;
}
if event.method == "Runtime.bindingCalled" {
if let Some(params) = &event.params {
if let Ok(binding_event) =
serde_json::from_value::<BindingCalledEvent>(params.clone())
{
trace!("Binding called: {}", binding_event.name);
// Parse the payload
let payload: Result<BindingPayload, _> =
serde_json::from_str(&binding_event.payload);
if let Ok(payload) = payload {
let bindings_guard = bindings.read().await;
if let Some(callback) = bindings_guard.get(&binding_event.name) {
// Execute the callback
let result = callback(payload.args).await;
drop(bindings_guard);
// Send the result back to JavaScript
let seq = payload.seq.to_string();
let resolve_script = match result {
Ok(value) => {
let value_json = serde_json::to_string(&value)
.unwrap_or_else(|_| "null".to_string());
js! {
(function() {
const callbacks = window.__viewpoint_callbacks;
if (callbacks && callbacks[@{seq}]) {
callbacks[@{seq}].resolve(@{value_json});
delete callbacks[@{seq}];
}
})()
}
}
Err(error) => {
let error_json = serde_json::to_string(&error)
.unwrap_or_else(|_| {
"\"Unknown error\"".to_string()
});
js! {
(function() {
const callbacks = window.__viewpoint_callbacks;
if (callbacks && callbacks[@{seq}]) {
callbacks[@{seq}].reject(new Error(@{error_json}));
delete callbacks[@{seq}];
}
})()
}
}
};
let _ = connection
.send_command::<_, serde_json::Value>(
"Runtime.evaluate",
Some(
viewpoint_cdp::protocol::runtime::EvaluateParams {
expression: resolve_script,
object_group: None,
include_command_line_api: None,
silent: Some(true),
context_id: None,
return_by_value: Some(true),
await_promise: Some(false),
},
),
Some(&session_id),
)
.await;
}
} else {
warn!("Failed to parse binding payload: {}", binding_event.payload);
}
}
}
}
}
debug!("Binding manager stopped listening");
});
}
/// Re-bind all functions after navigation.
///
/// This should be called after page navigation to re-inject the wrapper scripts.
pub async fn rebind_all(&self) -> Result<(), PageError> {
let bindings = self.bindings.read().await;
let names: Vec<String> = bindings.keys().cloned().collect();
drop(bindings);
for name in names {
// Re-inject the wrapper script
let name_json = serde_json::to_string(&name).unwrap();
let wrapper_script = js! {
(function() {
const bindingName = @{name_json};
const bindingFn = window[bindingName];
if (!bindingFn) return;
// Replace the binding with a proper async function
window[bindingName] = async function(...args) {
const seq = (window.__viewpoint_seq = (window.__viewpoint_seq || 0) + 1);
const payload = JSON.stringify({ seq, args });
return new Promise((resolve, reject) => {
window.__viewpoint_callbacks = window.__viewpoint_callbacks || {};
window.__viewpoint_callbacks[seq] = { resolve, reject };
bindingFn(payload);
});
};
})()
};
self.connection
.send_command::<_, serde_json::Value>(
"Runtime.evaluate",
Some(viewpoint_cdp::protocol::runtime::EvaluateParams {
expression: wrapper_script,
object_group: None,
include_command_line_api: None,
silent: Some(true),
context_id: None,
return_by_value: Some(true),
await_promise: Some(false),
}),
Some(&self.session_id),
)
.await?;
}
Ok(())
}
}
/// Payload structure for binding calls.
#[derive(Debug, serde::Deserialize)]
struct BindingPayload {
/// Sequence number for matching responses.
seq: u64,
/// Function arguments.
args: Vec<serde_json::Value>,
}
// Page impl for exposed function methods
impl super::Page {
/// Expose a Rust function to JavaScript.
///
/// The function will be available as `window.<name>()` in JavaScript.
/// When called from JavaScript, the function arguments are serialized to JSON,
/// the Rust callback is executed, and the result is returned to JavaScript.
///
/// # Example
///
/// ```no_run
/// use viewpoint_core::Page;
///
/// # async fn example(page: Page) -> Result<(), viewpoint_core::CoreError> {
/// // Expose a simple function
/// page.expose_function("add", |args| async move {
/// let x = args[0].as_i64().unwrap_or(0);
/// let y = args[1].as_i64().unwrap_or(0);
/// Ok(serde_json::json!(x + y))
/// }).await?;
///
/// // Call from JavaScript:
/// // const result = await window.add(1, 2); // returns 3
///
/// // Expose a function with string processing
/// page.expose_function("sha256", |args| async move {
/// let input = args[0].as_str().unwrap_or("");
/// // ... compute hash ...
/// let hash_string = "example_hash";
/// Ok(serde_json::json!(hash_string))
/// }).await?;
/// # Ok(())
/// # }
/// ```
///
/// # Notes
///
/// - The function is re-bound after each navigation
/// - Arguments and return values must be JSON-serializable
/// - Errors returned from the callback will reject the JavaScript promise
pub async fn expose_function<F, Fut>(
&self,
name: &str,
callback: F,
) -> Result<(), crate::error::PageError>
where
F: Fn(Vec<serde_json::Value>) -> Fut + Send + Sync + 'static,
Fut: std::future::Future<Output = Result<serde_json::Value, String>> + Send + 'static,
{
self.binding_manager.expose_function(name, callback).await
}
/// Remove an exposed function.
///
/// The function will no longer be available in JavaScript after this call.
pub async fn remove_exposed_function(&self, name: &str) -> Result<(), crate::error::PageError> {
self.binding_manager.remove_function(name).await
}
}
#[cfg(test)]
mod tests;