playwright_core/protocol/
request.rs1use crate::channel_owner::{ChannelOwner, ChannelOwnerImpl, ParentOrConnection};
7use crate::error::Result;
8use serde_json::Value;
9use std::any::Any;
10use std::sync::Arc;
11
12#[derive(Clone)]
19pub struct Request {
20 base: ChannelOwnerImpl,
21}
22
23impl Request {
24 pub fn new(
29 parent: Arc<dyn ChannelOwner>,
30 type_name: String,
31 guid: Arc<str>,
32 initializer: Value,
33 ) -> Result<Self> {
34 let base = ChannelOwnerImpl::new(
35 ParentOrConnection::Parent(parent),
36 type_name,
37 guid,
38 initializer,
39 );
40
41 Ok(Self { base })
42 }
43
44 pub fn url(&self) -> &str {
48 self.initializer()
49 .get("url")
50 .and_then(|v| v.as_str())
51 .unwrap_or("")
52 }
53
54 pub fn method(&self) -> &str {
58 self.initializer()
59 .get("method")
60 .and_then(|v| v.as_str())
61 .unwrap_or("GET")
62 }
63
64 pub fn resource_type(&self) -> &str {
68 self.initializer()
69 .get("resourceType")
70 .and_then(|v| v.as_str())
71 .unwrap_or("other")
72 }
73
74 pub fn is_navigation_request(&self) -> bool {
79 self.resource_type() == "document"
80 }
81}
82
83impl ChannelOwner for Request {
84 fn guid(&self) -> &str {
85 self.base.guid()
86 }
87
88 fn type_name(&self) -> &str {
89 self.base.type_name()
90 }
91
92 fn parent(&self) -> Option<Arc<dyn ChannelOwner>> {
93 self.base.parent()
94 }
95
96 fn connection(&self) -> Arc<dyn crate::connection::ConnectionLike> {
97 self.base.connection()
98 }
99
100 fn initializer(&self) -> &Value {
101 self.base.initializer()
102 }
103
104 fn channel(&self) -> &crate::channel::Channel {
105 self.base.channel()
106 }
107
108 fn dispose(&self, reason: crate::channel_owner::DisposeReason) {
109 self.base.dispose(reason)
110 }
111
112 fn adopt(&self, child: Arc<dyn ChannelOwner>) {
113 self.base.adopt(child)
114 }
115
116 fn add_child(&self, guid: Arc<str>, child: Arc<dyn ChannelOwner>) {
117 self.base.add_child(guid, child)
118 }
119
120 fn remove_child(&self, guid: &str) {
121 self.base.remove_child(guid)
122 }
123
124 fn on_event(&self, _method: &str, _params: Value) {
125 }
127
128 fn was_collected(&self) -> bool {
129 self.base.was_collected()
130 }
131
132 fn as_any(&self) -> &dyn Any {
133 self
134 }
135}
136
137impl std::fmt::Debug for Request {
138 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
139 f.debug_struct("Request")
140 .field("guid", &self.guid())
141 .finish()
142 }
143}