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
//! Page constructor methods.
//!
//! This module contains factory methods for creating Page instances.
use std::sync::Arc;
use tokio::sync::RwLock;
use viewpoint_cdp::CdpConnection;
use crate::error::NetworkError;
use crate::network::{RouteHandlerRegistry, WebSocketManager};
use super::binding;
use super::events::PageEventManager;
use super::frame::ExecutionContextRegistry;
use super::keyboard::Keyboard;
use super::locator_handler::LocatorHandlerManager;
use super::mouse::Mouse;
use super::popup;
use super::touchscreen::Touchscreen;
use super::video::{Video, VideoOptions};
use super::{DEFAULT_TEST_ID_ATTRIBUTE, Page};
impl Page {
/// Create a new page.
pub(crate) fn new(
connection: Arc<CdpConnection>,
target_id: String,
session_id: String,
frame_id: String,
) -> Self {
Self::build_page(
connection, target_id, session_id, frame_id, 0, 0, None, None,
)
}
/// Create a new page with context and page indices.
pub(crate) fn new_with_indices(
connection: Arc<CdpConnection>,
target_id: String,
session_id: String,
frame_id: String,
context_index: usize,
page_index: usize,
) -> Self {
Self::build_page(
connection,
target_id,
session_id,
frame_id,
context_index,
page_index,
None,
None,
)
}
/// Create a new page with video recording enabled.
pub(crate) fn with_video(
connection: Arc<CdpConnection>,
target_id: String,
session_id: String,
frame_id: String,
video_options: VideoOptions,
) -> Self {
Self::build_page(
connection,
target_id,
session_id,
frame_id,
0,
0,
Some(video_options),
None,
)
}
/// Create a new page with video recording and indices.
pub(crate) fn with_video_and_indices(
connection: Arc<CdpConnection>,
target_id: String,
session_id: String,
frame_id: String,
context_index: usize,
page_index: usize,
video_options: VideoOptions,
) -> Self {
Self::build_page(
connection,
target_id,
session_id,
frame_id,
context_index,
page_index,
Some(video_options),
None,
)
}
/// Create a new page with an opener (for popups).
pub(crate) fn with_opener(
connection: Arc<CdpConnection>,
target_id: String,
session_id: String,
frame_id: String,
opener_target_id: String,
) -> Self {
Self::build_page(
connection,
target_id,
session_id,
frame_id,
0,
0,
None,
Some(opener_target_id),
)
}
/// Internal helper to build a page with optional video and opener.
fn build_page(
connection: Arc<CdpConnection>,
target_id: String,
session_id: String,
frame_id: String,
context_index: usize,
page_index: usize,
video_options: Option<VideoOptions>,
opener_target_id: Option<String>,
) -> Self {
let route_registry = Arc::new(RouteHandlerRegistry::new(
connection.clone(),
session_id.clone(),
));
// Note: Don't start the fetch listener here - it will be started either:
// - When with_context_routes() is called (which creates a new registry with credentials), OR
// - When route() is called on this page directly (lazy start)
// This avoids duplicate listeners when context routes are applied.
let keyboard = Keyboard::new(connection.clone(), session_id.clone(), frame_id.clone());
let mouse = Mouse::new(connection.clone(), session_id.clone());
let touchscreen = Touchscreen::new(connection.clone(), session_id.clone());
let event_manager = Arc::new(PageEventManager::new(
connection.clone(),
session_id.clone(),
));
let locator_handler_manager = Arc::new(LocatorHandlerManager::new());
let popup_manager = Arc::new(popup::PopupManager::new(
connection.clone(),
session_id.clone(),
target_id.clone(),
));
let websocket_manager = Arc::new(WebSocketManager::new(
connection.clone(),
session_id.clone(),
));
let binding_manager = Arc::new(binding::BindingManager::new(
connection.clone(),
session_id.clone(),
));
let video_controller = video_options.map(|opts| {
Arc::new(Video::with_options(
connection.clone(),
session_id.clone(),
opts,
))
});
// Create and start the execution context registry
let context_registry = Arc::new(ExecutionContextRegistry::new(
connection.clone(),
session_id.clone(),
));
context_registry.start_listening();
Self {
connection,
target_id,
session_id,
frame_id,
context_index,
page_index,
closed: false,
route_registry,
keyboard,
mouse,
touchscreen,
event_manager,
locator_handler_manager,
video_controller,
opener_target_id,
popup_manager,
websocket_manager,
binding_manager,
test_id_attribute: DEFAULT_TEST_ID_ATTRIBUTE.to_string(),
context_registry,
ref_map: std::sync::Arc::new(
parking_lot::RwLock::new(std::collections::HashMap::new()),
),
context_pages: None,
}
}
/// Create a new page with a custom test ID attribute.
pub(crate) fn with_test_id_attribute(mut self, attribute: String) -> Self {
self.test_id_attribute = attribute;
self
}
/// Set the context's pages list for this page.
///
/// This allows the page to remove itself from the context's tracking list
/// when it is closed, preventing stale sessions from accumulating.
pub(crate) fn with_context_pages(mut self, pages: Arc<RwLock<Vec<Page>>>) -> Self {
self.context_pages = Some(pages);
self
}
/// Set context-level routes for this page.
///
/// Context routes are checked as a fallback when no page-level route matches.
/// If `http_credentials` is provided, they will be used for HTTP authentication.
///
/// This method also registers the page's route registry with the context
/// so that future `context.route()` calls can synchronously enable Fetch on this page.
pub(crate) async fn with_context_routes(
self,
context_routes: Arc<crate::context::routing::ContextRouteRegistry>,
http_credentials: Option<crate::network::auth::HttpCredentials>,
) -> Self {
self.with_context_routes_and_proxy(context_routes, http_credentials, None)
.await
}
/// Set context-level routes for this page with optional proxy credentials.
///
/// Context routes are checked as a fallback when no page-level route matches.
/// If `http_credentials` is provided, they will be used for HTTP authentication.
/// If `proxy_credentials` is provided, they will be used for proxy authentication.
///
/// This method also registers the page's route registry with the context
/// so that future `context.route()` calls can synchronously enable Fetch on this page.
pub(crate) async fn with_context_routes_and_proxy(
self,
context_routes: Arc<crate::context::routing::ContextRouteRegistry>,
http_credentials: Option<crate::network::auth::HttpCredentials>,
proxy_credentials: Option<crate::network::auth::ProxyCredentials>,
) -> Self {
// Create a new registry with context routes and optional credentials
let new_registry = Arc::new(RouteHandlerRegistry::with_context_routes_and_proxy(
self.connection.clone(),
self.session_id.clone(),
context_routes.clone(),
http_credentials,
proxy_credentials,
));
// Start the fetch event listener for route handling and authentication
new_registry.start_fetch_listener();
// Register this page's registry with the context so that future
// context.route() calls can synchronously enable Fetch on this page
context_routes.register_page_registry(&new_registry).await;
Self {
route_registry: new_registry,
..self
}
}
/// Enable Fetch domain if there are context-level routes.
///
/// This is called after page creation to ensure the Fetch domain is enabled
/// when there are context-level routes that need to intercept requests.
pub(crate) async fn enable_fetch_for_context_routes(&self) -> Result<(), NetworkError> {
self.route_registry.enable_fetch_for_context_routes().await
}
/// Create an internal clone of the page for event handlers.
///
/// This creates a new Page instance that shares the underlying connection
/// and other Arc-wrapped resources. Used internally for passing pages
/// to event handlers.
pub(crate) fn clone_internal(&self) -> Self {
Self {
connection: self.connection.clone(),
target_id: self.target_id.clone(),
session_id: self.session_id.clone(),
frame_id: self.frame_id.clone(),
context_index: self.context_index,
page_index: self.page_index,
closed: self.closed,
route_registry: self.route_registry.clone(),
keyboard: Keyboard::new(
self.connection.clone(),
self.session_id.clone(),
self.frame_id.clone(),
),
mouse: Mouse::new(self.connection.clone(), self.session_id.clone()),
touchscreen: Touchscreen::new(self.connection.clone(), self.session_id.clone()),
event_manager: self.event_manager.clone(),
locator_handler_manager: self.locator_handler_manager.clone(),
video_controller: self.video_controller.clone(),
opener_target_id: self.opener_target_id.clone(),
popup_manager: self.popup_manager.clone(),
websocket_manager: self.websocket_manager.clone(),
binding_manager: self.binding_manager.clone(),
test_id_attribute: self.test_id_attribute.clone(),
context_registry: self.context_registry.clone(),
ref_map: self.ref_map.clone(),
context_pages: self.context_pages.clone(),
}
}
/// Get the execution context registry.
pub(crate) fn context_registry(&self) -> &Arc<ExecutionContextRegistry> {
&self.context_registry
}
}