Skip to content

Commit ad9e6f1

Browse files
Links IAP Leo purchase to desktop
1 parent 98a54e6 commit ad9e6f1

File tree

7 files changed

+61
-26
lines changed

7 files changed

+61
-26
lines changed

android/java/org/chromium/chrome/browser/settings/BraveLeoPreferences.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class BraveLeoPreferences extends BravePreferenceFragment
3333
private static final String PREF_SUBSCRIPTION_CATEGORY = "subscription_category";
3434
private static final String PREF_DEFAULT_MODEL = "default_model";
3535
private static final String LINK_SUBSCRIPTION_URL =
36-
"https://account.brave.com?intent=connect-receipt&product=leo";
36+
"https://account.brave.com?intent=link-order&product=leo";
3737

3838
@Override
3939
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

components/ai_chat/core/browser/android/ai_chat_iap_subscription_android.cc

+9-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ AIChatIAPSubscription::AIChatIAPSubscription(PrefService* prefs)
2727

2828
AIChatIAPSubscription::~AIChatIAPSubscription() = default;
2929

30-
void AIChatIAPSubscription::GetPurchaseToken(
31-
GetPurchaseTokenCallback callback) {
30+
void AIChatIAPSubscription::GetPurchaseTokenOrderId(
31+
GetPurchaseTokenOrderIdCallback callback) {
32+
std::string order_id_string = "";
3233
std::string purchase_token_string = "";
3334
std::string package_string = kDefaultPackage;
3435
std::string product_id_string = kProductId;
@@ -52,6 +53,11 @@ void AIChatIAPSubscription::GetPurchaseToken(
5253
product_id_string = prefs_->GetString(prefs::kBraveChatProductIdAndroid);
5354
}
5455

56+
auto* order_id = prefs_->FindPreference(prefs::kBraveChatOrderIdAndroid);
57+
if (order_id && !product_id->IsDefaultValue()) {
58+
order_id_string = prefs_->GetString(prefs::kBraveChatOrderIdAndroid);
59+
}
60+
5561
base::Value::Dict response;
5662
response.Set("type", "android");
5763
response.Set("raw_receipt", purchase_token_string);
@@ -63,7 +69,7 @@ void AIChatIAPSubscription::GetPurchaseToken(
6369

6470
std::string encoded_response_json;
6571
base::Base64Encode(response_json, &encoded_response_json);
66-
std::move(callback).Run(encoded_response_json);
72+
std::move(callback).Run(encoded_response_json, order_id_string);
6773
}
6874

6975
} // namespace ai_chat

components/ai_chat/core/browser/android/ai_chat_iap_subscription_android.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class AIChatIAPSubscription final : public ai_chat::mojom::IAPSubscription {
2424
~AIChatIAPSubscription() override;
2525

2626
// ai_chat::mojom::IAPSubscription
27-
void GetPurchaseToken(GetPurchaseTokenCallback callback) override;
27+
void GetPurchaseTokenOrderId(
28+
GetPurchaseTokenOrderIdCallback callback) override;
2829

2930
private:
3031
raw_ptr<PrefService> prefs_ = nullptr;

components/ai_chat/core/common/mojom/ai_chat.mojom

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,5 +211,5 @@ interface AIChatAndroidHelper {
211211

212212
[EnableIf=is_android]
213213
interface IAPSubscription {
214-
GetPurchaseToken() => (string token);
214+
GetPurchaseTokenOrderId() => (string token, string order_id);
215215
};

components/brave_mobile_subscription/renderer/android/subscription_render_frame_observer.cc

+41-17
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ inline constexpr char kIntentParamTestValue[] = "connect-receipt-test";
3939
inline constexpr char kProductParamName[] = "product";
4040
inline constexpr char kProductVPNParamValue[] = "vpn";
4141
inline constexpr char kProductLeoParamValue[] = "leo";
42+
inline constexpr char kIntentParamValueLeo[] = "link-order";
4243

4344
} // namespace
4445

@@ -61,8 +62,7 @@ bool SubscriptionRenderFrameObserver::EnsureConnected() {
6162
}
6263
#endif
6364
#if BUILDFLAG(ENABLE_AI_CHAT)
64-
if (ai_chat::features::IsAIChatHistoryEnabled() &&
65-
product_ == Product::kLeo) {
65+
if (ai_chat::features::IsAIChatEnabled() && product_ == Product::kLeo) {
6666
if (!ai_chat_subscription_.is_bound()) {
6767
render_frame()->GetBrowserInterfaceBroker()->GetInterface(
6868
ai_chat_subscription_.BindNewPipeAndPassReceiver());
@@ -105,36 +105,59 @@ void SubscriptionRenderFrameObserver::DidCreateScriptContext(
105105
} else if (product_ == Product::kLeo) {
106106
#if BUILDFLAG(ENABLE_AI_CHAT)
107107
if (ai_chat_subscription_.is_bound()) {
108-
ai_chat_subscription_->GetPurchaseToken(
109-
base::BindOnce(&SubscriptionRenderFrameObserver::OnGetPurchaseToken,
110-
weak_factory_.GetWeakPtr()));
108+
ai_chat_subscription_->GetPurchaseTokenOrderId(base::BindOnce(
109+
&SubscriptionRenderFrameObserver::OnGetPurchaseTokenOrderId,
110+
weak_factory_.GetWeakPtr()));
111111
}
112112
#endif
113113
}
114114
}
115115

116+
std::string SubscriptionRenderFrameObserver::GetPurchaseTokenJSString(
117+
const std::string& purchase_token) {
118+
if (!IsValueAllowed(purchase_token)) {
119+
return "";
120+
}
121+
122+
std::string_view receipt_var_name;
123+
if (product_ == Product::kVPN) {
124+
receipt_var_name = "braveVpn.receipt";
125+
} else if (product_ == Product::kLeo) {
126+
receipt_var_name = "braveLeo.receipt";
127+
}
128+
129+
return base::StrCat({"window.localStorage.setItem(\"", receipt_var_name,
130+
"\", \"", purchase_token, "\");"});
131+
}
132+
116133
void SubscriptionRenderFrameObserver::OnGetPurchaseToken(
117134
const std::string& purchase_token) {
118135
if (!IsAllowed()) {
119136
return;
120137
}
121138
auto* frame = render_frame();
122139
if (frame) {
123-
if (IsValueAllowed(purchase_token)) {
124-
std::string_view receipt_var_name;
125-
if (product_ == Product::kVPN) {
126-
receipt_var_name = "braveVpn.receipt";
127-
} else if (product_ == Product::kLeo) {
128-
receipt_var_name = "braveLeo.receipt";
129-
}
130-
std::u16string set_local_storage = base::UTF8ToUTF16(
131-
base::StrCat({"window.localStorage.setItem(\"", receipt_var_name,
132-
"\", \"", purchase_token, "\");"}));
133-
frame->ExecuteJavaScript(set_local_storage);
140+
std::string set_local_storage = GetPurchaseTokenJSString(purchase_token);
141+
if (!set_local_storage.empty()) {
142+
frame->ExecuteJavaScript(base::UTF8ToUTF16(set_local_storage));
134143
}
135144
}
136145
}
137146

147+
void SubscriptionRenderFrameObserver::OnGetPurchaseTokenOrderId(
148+
const std::string& purchase_token,
149+
const std::string& order_id) {
150+
if (!IsAllowed()) {
151+
return;
152+
}
153+
auto* frame = render_frame();
154+
if (frame && !order_id.empty() && !purchase_token.empty()) {
155+
frame->ExecuteJavaScript(base::UTF8ToUTF16(base::StrCat(
156+
{"window.localStorage.setItem(\"braveLeo.orderId\", \"", order_id,
157+
"\");", GetPurchaseTokenJSString(purchase_token)})));
158+
}
159+
}
160+
138161
std::string SubscriptionRenderFrameObserver::ExtractParam(
139162
const GURL& url,
140163
const std::string& name) const {
@@ -182,7 +205,8 @@ bool SubscriptionRenderFrameObserver::IsAllowed() {
182205
} else {
183206
product_ = std::nullopt;
184207
}
185-
return (intent == kIntentParamValue || intent == kIntentParamTestValue) &&
208+
return (intent == kIntentParamValue || intent == kIntentParamTestValue ||
209+
intent == kIntentParamValueLeo) &&
186210
product_.has_value();
187211
}
188212

components/brave_mobile_subscription/renderer/android/subscription_render_frame_observer.h

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class SubscriptionRenderFrameObserver : public content::RenderFrameObserver {
5959

6060
bool EnsureConnected();
6161
void OnGetPurchaseToken(const std::string& purchase_token);
62+
void OnGetPurchaseTokenOrderId(const std::string& purchase_token,
63+
const std::string& order_id);
6264
std::string ExtractParam(const GURL& url, const std::string& name) const;
6365
bool IsValueAllowed(const std::string& purchase_token) const;
6466

@@ -67,6 +69,8 @@ class SubscriptionRenderFrameObserver : public content::RenderFrameObserver {
6769

6870
bool IsAllowed();
6971

72+
std::string GetPurchaseTokenJSString(const std::string& purchase_token);
73+
7074
const int32_t world_id_;
7175
std::optional<Product> product_ = std::nullopt;
7276
#if BUILDFLAG(ENABLE_BRAVE_VPN)

components/brave_mobile_subscription/renderer/android/subscription_render_frame_observer_browsertest.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class SubscriptionRenderFrameObserverBrowserTest :
2424
SubscriptionRenderFrameObserverBrowserTest() {
2525
scoped_feature_list_.InitWithFeatures(
2626
{skus::features::kSkusFeature, brave_vpn::features::kBraveVPN,
27-
ai_chat::features::kAIChatHistory
28-
}, {});
27+
ai_chat::features::kAIChat},
28+
{});
2929
}
3030
~SubscriptionRenderFrameObserverBrowserTest() override = default;
3131

@@ -46,7 +46,7 @@ TEST_F(SubscriptionRenderFrameObserverBrowserTest, IsAllowed) {
4646
// Leo
4747
LoadHTMLWithUrlOverride(
4848
R"(<html><body></body></html>)",
49-
"https://account.brave.com/?intent=connect-receipt&product=leo");
49+
"https://account.brave.com/?intent=link-order&product=leo");
5050

5151
EXPECT_TRUE(observer.IsAllowed());
5252
// http

0 commit comments

Comments
 (0)