Skip to content
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

[iOS] - Inject VPN subscription info on script message #26969

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import os.log

extension BraveStoreProduct {
/// The key to use when storing the receipt in WebKit's LocalStorage
fileprivate var localStorageKey: String {
var localStorageKey: String {
switch self {
case .vpnMonthly, .vpnYearly: return "braveVpn.receipt"
case .leoMonthly, .leoYearly: return "braveLeo.receipt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import AIChat
import BraveCore
import BraveShared
import BraveStore
import BraveVPN
import Foundation
import Preferences
import Shared
import WebKit
import os.log
Expand Down Expand Up @@ -36,8 +38,7 @@ class BraveSkusScriptHandler: TabContentScript {

return WKUserScript(
source: secureScript(
handlerNamesMap: ["$<message_handler>": messageHandlerName]
.merging(Method.map, uniquingKeysWith: { $1 }),
handlerNamesMap: ["$<message_handler>": messageHandlerName],
securityToken: scriptId,
script: script
),
Expand Down Expand Up @@ -109,6 +110,30 @@ class BraveSkusScriptHandler: TabContentScript {
case .credentialsSummary:
let summary = try CredentialSummaryMessage.from(message: message)
return await skusManager.credentialSummary(for: summary.domain)

case .setLocalStorageReceipt:
let storeMessage = try StoreReceiptMessage.from(message: message)
if storeMessage.message == "vpn" {
if let vpnSubscriptionProductId = Preferences.VPN.subscriptionProductId.value,
let product = BraveStoreProduct(rawValue: vpnSubscriptionProductId)
{
let storageKey = product.localStorageKey
let receipt = try BraveSkusSDK.receipt(for: product)
return ["key": storageKey, "data": receipt]
}
}

if storeMessage.message == "leo" {
if let aiChatSubscriptionProductId = Preferences.AIChat.subscriptionProductId.value,
let orderId = Preferences.AIChat.subscriptionOrderId.value,
let product = BraveStoreProduct(rawValue: aiChatSubscriptionProductId)
{
let storageKey = product.localStorageKey
let receipt = try BraveSkusSDK.receipt(for: product)
return ["key": storageKey, "data": receipt, "braveLeo.orderId": orderId]
}
}
throw SkusWebMessageError.invalidFormat
}
}
}
Expand All @@ -119,6 +144,7 @@ extension BraveSkusScriptHandler {
case fetchOrderCredentials = 2
case prepareCredentialsPresentation = 3
case credentialsSummary = 4
case setLocalStorageReceipt = 5

static var map: [String: String] {
var jsDict = [String: String]()
Expand All @@ -141,6 +167,10 @@ extension BraveSkusScriptHandler {
private struct CredentialSummaryMessage: SkusWebMessage {
let domain: String
}

private struct StoreReceiptMessage: SkusWebMessage {
let message: String
}
}

private enum SkusWebMessageError: Error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,31 @@ window.__firefox__.includeOnce("BraveSkusScript", function($) {
if (!window.chrome) {
window.chrome = {};
}

// Request VPN Receipt
sendMessage($<setLocalStorageReceipt>, { "message": "vpn" })
.then(function(jsonData) {
if (jsonData) {
window.localStorage.setItem(jsonData["key"], jsonData["data"]);
} else {
console.error("No VPN Subscription Data");
}
}).catch(function(err) {
console.error(`An error occurred setting the local storage receipt: ${err}`);
});

// Request Leo Receipt
sendMessage($<setLocalStorageReceipt>, { "message": "leo" })
.then(function(jsonData) {
if (jsonData) {
window.localStorage.setItem("braveLeo.orderId", jsonData["braveLeo.orderId"])
window.localStorage.setItem(jsonData["key"], jsonData["data"]);
} else {
console.error("No Leo Subscription Data");
}
}).catch(function(err) {
console.error(`An error occurred setting the local storage receipt: ${err}`);
});

Object.defineProperty(window.chrome, 'braveSkus', {
enumerable: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ public struct BraveVPNPaywallView: View {
.font(.body.weight(.semibold))
.foregroundColor(Color(.white))
.padding()
.frame(maxWidth: .infinity)
}
}
.frame(maxWidth: .infinity)
Brandon-T marked this conversation as resolved.
Show resolved Hide resolved
.background(
LinearGradient(
gradient:
Expand Down
Loading