@@ -76,18 +76,26 @@ class FormAutofillHelper: TabContentScript {
7676 // to an embedded iframe on a webpage for injecting card info
7777 frame = message. frameInfo
7878
79- guard let frame = frame, frame. isFrameLoadedInSecureContext else {
79+ guard let frame = frame else { return }
80+ let isSecureContext = frame. isFrameLoadedInSecureContext
81+
82+ processMessage ( name: message. name, body: message. body, isSecureContext: isSecureContext, frame: frame)
83+ }
84+
85+ func processMessage( name: String , body: Any , isSecureContext: Bool , frame: WKFrameInfo ? ) {
86+ guard isSecureContext else {
8087 logger. log ( " Ignoring request as it came from an insecure context " ,
8188 level: . info,
8289 category: . webview)
8390 return
8491 }
8592
86- switch HandlerName ( rawValue: message . name) {
93+ switch HandlerName ( rawValue: name) {
8794 case . addressFormTelemetryMessageHandler:
88- let addressValues = message. decodeBody ( as: AddressFormData . self)
89- if addressValues? . object == " address_form " {
90- switch addressValues? . method {
95+ guard let addressValues = decode ( body, as: AddressFormData . self) else { return }
96+
97+ if addressValues. object == " address_form " {
98+ switch addressValues. method {
9199 case . detected:
92100 TelemetryWrapper . recordEvent ( category: . action, method: . detect, object: . addressForm)
93101 case . filled:
@@ -98,9 +106,9 @@ class FormAutofillHelper: TabContentScript {
98106 return
99107 }
100108 }
109+
101110 case . addressFormMessageHandler:
102- guard let fieldValues = message. decodeBody ( as: FillAddressAutofillForm . self) else {
103- // Log a warning if payload parsing fails
111+ guard let fieldValues = decode ( body, as: FillAddressAutofillForm . self) else {
104112 logger. log ( " Unable to find the payloadType for the address form JS input " ,
105113 level: . warning,
106114 category: . webview)
@@ -110,11 +118,8 @@ class FormAutofillHelper: TabContentScript {
110118 foundFieldValues ? ( getFieldTypeValues ( payload: fieldValues. payload) , fieldValues. type, frame)
111119
112120 case . creditCardFormMessageHandler:
113- // Parse message payload for credit card form autofill
114- guard let fieldValues = message. decodeBody ( as: FillCreditCardForm . self) ,
115- let payloadType = FormAutofillPayloadType ( rawValue: fieldValues. type)
116- else {
117- // Log a warning if payload parsing fails
121+ guard let fieldValues = decode ( body, as: FillCreditCardForm . self) ,
122+ let payloadType = FormAutofillPayloadType ( rawValue: fieldValues. type) else {
118123 logger. log ( " Unable to find the payloadType for the credit card form JS input " ,
119124 level: . warning,
120125 category: . webview)
@@ -125,7 +130,6 @@ class FormAutofillHelper: TabContentScript {
125130 foundFieldValues ? ( getFieldTypeValues ( payload: payloadData) , payloadType, frame)
126131
127132 case . none:
128- // Do nothing if the handler name is not recognized
129133 break
130134 }
131135 }
@@ -317,4 +321,24 @@ class FormAutofillHelper: TabContentScript {
317321 }
318322 }
319323 }
324+
325+ /// Helper method to decode message body into a Decodable type
326+ ///
327+ /// This replaces the need for WKScriptMessage.decodeBody extension by allowing
328+ /// us to decode message bodies without requiring a WKScriptMessage instance.
329+ /// This is essential for testing, as WKScriptMessage and WKFrameInfo cannot be
330+ /// safely mocked (they crash on deallocation).
331+ /// - Parameters:
332+ /// - body: The message body to decode (typically a dictionary or string)
333+ /// - type: The Decodable type to decode into
334+ /// - Returns: The decoded object, or nil if decoding fails
335+ private func decode< T: Decodable > ( _ body: Any , as type: T . Type ) -> T ? {
336+ if let dict = body as? [ String : Any ] , let data = try ? JSONSerialization . data ( withJSONObject: dict, options: [ ] ) {
337+ return try ? JSONDecoder ( ) . decode ( type, from: data)
338+ } else if let bodyString = body as? String , let data = bodyString. data ( using: . utf8) {
339+ return try ? JSONDecoder ( ) . decode ( type, from: data)
340+ } else {
341+ return nil
342+ }
343+ }
320344}
0 commit comments