-
Notifications
You must be signed in to change notification settings - Fork 432
feat(paywalls): add web_view wire envelope (2/7) #7228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
5cf1dfd
feat(paywalls): public API surface for web_view messages (1/7)
cursoragent f7f5d16
feat(paywalls): web_view wire envelope + JSON hardening (2/7)
cursoragent e671378
refactor(paywalls): keep web view bridge internal
cursoragent 41401ac
refactor(paywalls): keep web view bridge internal
cursoragent d941c01
Merge updated internal web view API base
cursoragent ca80c8f
Merge branch 'main' into webview/1-public-api
ajpallares 94982da
fix(paywalls): remove client-side web view payload limits
cursoragent ea6d24b
test(paywalls): address web view envelope review
cursoragent 0612595
refactor(paywalls): remove unused web view handler API
cursoragent d14d63c
fix(paywalls): retain internal bridge dependencies
cursoragent 19cbfe6
refactor(paywalls): remove web view message handler concept
JZDesign c3d27a9
Merge branch 'webview/1-public-api' into webview/2-envelope
JZDesign d829317
Merge branch 'main' into webview/1-public-api
ajpallares 643c32a
Merge branch 'webview/1-public-api' into webview/2-envelope
ajpallares 1a82768
Merge branch 'main-latest' into webview/2-envelope
ajpallares 328ebf9
Rename PaywallWebViewAPI.swift to PaywallWebViewValue.swift, add head…
ajpallares 4243461
Add envelope wire-key/rich-payload/rejection tests and brief Envelope…
ajpallares 306e38d
Drop reservedPayloadKeys from envelope and simplify test helper
ajpallares 3ca5794
Merge branch 'main' into webview/2-envelope
ajpallares b701470
Trim unused message types and move receiveScript out of the data model
ajpallares d5ceb16
Merge branch 'main' into webview/2-envelope
ajpallares File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...omponents/WebView/PaywallWebViewAPI.swift → ...ponents/WebView/PaywallWebViewValue.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| // | ||
| // Copyright RevenueCat Inc. All Rights Reserved. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| #if !os(tvOS) // For Paywalls V2 | ||
|
|
||
113 changes: 113 additions & 0 deletions
113
RevenueCatUI/Templates/V2/Components/WebView/WebViewEnvelope.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import Foundation | ||
|
|
||
| #if !os(tvOS) // For Paywalls V2 | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
| enum WebViewEnvelope { | ||
|
|
||
| static let channel = "rc-web-components" | ||
| static let messageHandlerName = "rcWebComponents" | ||
| static let receiveFunction = "__rcWebComponentsReceive" | ||
| static let defaultProtocolVersion = 1 | ||
|
|
||
| static let messageTypeStepLoaded = "rc:step-loaded" | ||
| static let messageTypeStepComplete = "rc:step-complete" | ||
| static let messageTypeRequestVariables = "rc:request-variables" | ||
| static let messageTypeVariables = "rc:variables" | ||
| static let messageTypeError = "rc:error" | ||
| static let messageTypeResize = "resize" | ||
| static let messageTypeFit = "fit" | ||
|
|
||
| static let maxResizePoints: CGFloat = 10_000 | ||
| static let resizeThreshold: CGFloat = 1 | ||
| static let fallbackFitHeight: CGFloat = 100 | ||
| static let fallbackFitWidth: CGFloat = 300 | ||
|
|
||
| // swiftlint:disable nesting | ||
| enum Kind: String, Codable { | ||
| case connect | ||
| case `init` | ||
| case reject | ||
| case message | ||
| case request | ||
| case response | ||
| case error | ||
| } | ||
|
|
||
| /// One JSON message exchanged with the `web_view` content. Fields used vary by ``kind``. | ||
| struct Envelope: Codable, Equatable { | ||
| let channel: String | ||
| let protocolVersion: Int | ||
| let kind: Kind | ||
| let componentID: String | ||
| let type: String? | ||
| let id: String? | ||
| let payload: [String: PaywallWebViewValue]? | ||
| let error: String? | ||
|
|
||
| init( | ||
| kind: Kind, | ||
| componentID: String, | ||
| type: String? = nil, | ||
| id: String? = nil, | ||
| payload: [String: PaywallWebViewValue]? = nil, | ||
| error: String? = nil, | ||
| protocolVersion: Int = WebViewEnvelope.defaultProtocolVersion | ||
| ) { | ||
| self.channel = WebViewEnvelope.channel | ||
| self.protocolVersion = protocolVersion | ||
| self.kind = kind | ||
| self.componentID = componentID | ||
| self.type = type | ||
| self.id = id | ||
| self.payload = payload | ||
| self.error = error | ||
| } | ||
|
|
||
| enum CodingKeys: String, CodingKey { | ||
| case channel | ||
| case protocolVersion = "protocol_version" | ||
| case kind | ||
| case componentID = "component_id" | ||
| case type | ||
| case id | ||
| case payload | ||
| case error | ||
| } | ||
| } | ||
| // swiftlint:enable nesting | ||
|
|
||
| static func decode(rawMessage: Any) -> Envelope? { | ||
| // `workflow-web-components-sdk` sends frames with `postMessage(JSON.stringify(frame))`. | ||
| guard let string = rawMessage as? String, | ||
| let data = string.data(using: .utf8) else { | ||
| return nil | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| guard let envelope = try? JSONDecoder().decode(Envelope.self, from: data), | ||
| envelope.channel == Self.channel else { | ||
| return nil | ||
| } | ||
|
|
||
| return envelope | ||
| } | ||
|
|
||
| static func receiveScript(for envelope: Envelope) -> String? { | ||
|
ajpallares marked this conversation as resolved.
Outdated
|
||
| let encoder = JSONEncoder() | ||
| guard let data = try? encoder.encode(envelope), | ||
| let json = String(data: data, encoding: .utf8) else { | ||
| return nil | ||
| } | ||
| let escaped = json | ||
| .replacingOccurrences(of: "\u{2028}", with: "\\u2028") | ||
| .replacingOccurrences(of: "\u{2029}", with: "\\u2029") | ||
|
|
||
| return """ | ||
| (function(){var m=\(escaped);if(typeof window.\(Self.receiveFunction)==='function'){\ | ||
| window.\(Self.receiveFunction)(m);}})(); | ||
| """ | ||
| } | ||
|
|
||
| } | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.