Skip to content

Commit 793b135

Browse files
Links IAP Leo purchase to desktop (uplift to 1.63.x) (#22022)
* Uplift of #22012 (squashed) to beta * Adds Leo staging switch for Android --------- Co-authored-by: Serg <[email protected]>
1 parent 180c9d5 commit 793b135

File tree

10 files changed

+98
-27
lines changed

10 files changed

+98
-27
lines changed

android/java/org/chromium/base/BraveCommandLineInitUtil.java

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public abstract class BraveCommandLineInitUtil {
2323
// Duplicate constant to avoid pull dependancy into base
2424
private static final String PREF_QA_VLOG_REWARDS = "qa_vlog_rewards";
2525
private static final String PREF_QA_COMMAND_LINE = "qa_command_line";
26+
private static final String PREF_USE_LEO_STAGING_SERVER = "use_leo_staging_server";
2627
private static final String TEST_VARIATIONS_SERVER_URL_FILE =
2728
"/data/local/tmp/brave-test-variations-server-url";
2829

@@ -59,6 +60,11 @@ private static void appendBraveSwitchesAndArguments() {
5960
}
6061
}
6162

63+
if (sharedPreferences.getBoolean(PREF_USE_LEO_STAGING_SERVER, false)) {
64+
qaCommandLine +=
65+
" --env-leo=staging --env-ai-chat.bsg=dev --env-ai-chat-premium.bsg=dev";
66+
}
67+
6268
@SuppressLint("VisibleForTests")
6369
String[] args = CommandLine.tokenizeQuotedArguments(qaCommandLine.toCharArray());
6470
CommandLine.getInstance().appendSwitchesAndArguments(args);

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.chromium.chrome.browser.brave_leo.BraveLeoPrefUtils;
2020
import org.chromium.chrome.browser.brave_leo.BraveLeoUtils;
2121
import org.chromium.chrome.browser.preferences.ChromeSharedPreferences;
22+
import org.chromium.chrome.browser.settings.developer.BraveQAPreferences;
2223
import org.chromium.components.browser_ui.settings.ChromeBasePreference;
2324
import org.chromium.components.browser_ui.settings.ChromeSwitchPreference;
2425
import org.chromium.components.browser_ui.settings.SettingsUtils;
@@ -33,7 +34,9 @@ public class BraveLeoPreferences extends BravePreferenceFragment
3334
private static final String PREF_SUBSCRIPTION_CATEGORY = "subscription_category";
3435
private static final String PREF_DEFAULT_MODEL = "default_model";
3536
private static final String LINK_SUBSCRIPTION_URL =
36-
"https://account.brave.com?intent=connect-receipt&product=leo";
37+
"https://account.brave.com?intent=link-order&product=leo";
38+
private static final String LINK_SUBSCRIPTION_URL_STAGING =
39+
"https://account.bravesoftware.com?intent=link-order&product=leo";
3740

3841
@Override
3942
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
@@ -114,7 +117,7 @@ private void checkLinkPurchase() {
114117
manageSubscription.setVisible(true);
115118
linkSubscription.setOnPreferenceClickListener(
116119
preference -> {
117-
BraveLeoUtils.openURL(LINK_SUBSCRIPTION_URL);
120+
BraveLeoUtils.openURL(getLinkURL());
118121
return true;
119122
});
120123
manageSubscription.setOnPreferenceClickListener(
@@ -141,4 +144,12 @@ public void onResume() {
141144
setModel();
142145
checkLinkPurchase();
143146
}
147+
148+
private String getLinkURL() {
149+
if (BraveQAPreferences.isLeoStagingUsed()) {
150+
return LINK_SUBSCRIPTION_URL_STAGING;
151+
}
152+
153+
return LINK_SUBSCRIPTION_URL;
154+
}
144155
}

android/java/org/chromium/chrome/browser/settings/developer/BraveQAPreferences.java

+13
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public class BraveQAPreferences extends BravePreferenceFragment
5757
implements OnPreferenceChangeListener, BraveRewardsObserver {
5858
private static final String PREF_USE_REWARDS_STAGING_SERVER = "use_rewards_staging_server";
5959
private static final String PREF_USE_SYNC_STAGING_SERVER = "use_sync_staging_server";
60+
private static final String PREF_USE_LEO_STAGING_SERVER = "use_leo_staging_server";
6061
private static final String PREF_QA_MAXIMIZE_INITIAL_ADS_NUMBER =
6162
"qa_maximize_initial_ads_number";
6263
private static final String PREF_QA_DEBUG_NTP = "qa_debug_ntp";
@@ -75,6 +76,7 @@ public class BraveQAPreferences extends BravePreferenceFragment
7576
private ChromeSwitchPreference mVpnLinkSubscriptionOnDev;
7677
private ChromeSwitchPreference mBraveDormantFeatureEngagement;
7778
private ChromeSwitchPreference mIsStagingServer;
79+
private ChromeSwitchPreference mIsLeoStagingServer;
7880
private ChromeSwitchPreference mIsSyncStagingServer;
7981
private ChromeSwitchPreference mMaximizeAdsNumber;
8082
private ChromeSwitchPreference mDebugNTP;
@@ -118,6 +120,12 @@ public void onCreate(Bundle savedInstanceState) {
118120
}
119121
mIsSyncStagingServer.setChecked(isSyncStagingUsed());
120122

123+
mIsLeoStagingServer = (ChromeSwitchPreference) findPreference(PREF_USE_LEO_STAGING_SERVER);
124+
if (mIsLeoStagingServer != null) {
125+
mIsLeoStagingServer.setOnPreferenceChangeListener(this);
126+
}
127+
mIsLeoStagingServer.setChecked(isLeoStagingUsed());
128+
121129
mMaximizeAdsNumber =
122130
(ChromeSwitchPreference) findPreference(PREF_QA_MAXIMIZE_INITIAL_ADS_NUMBER);
123131
if (mMaximizeAdsNumber != null) {
@@ -256,6 +264,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
256264
enableMaximumAdsNumber((boolean) newValue);
257265
} else if (PREF_QA_DEBUG_NTP.equals(preference.getKey())
258266
|| PREF_USE_SYNC_STAGING_SERVER.equals(preference.getKey())
267+
|| PREF_USE_LEO_STAGING_SERVER.equals(preference.getKey())
259268
|| PREF_QA_VLOG_REWARDS.equals(preference.getKey())
260269
|| BraveVpnPrefUtils.PREF_BRAVE_VPN_LINK_SUBSCRIPTION_ON_STAGING.equals(
261270
preference.getKey())
@@ -296,6 +305,10 @@ public static boolean isSyncStagingUsed() {
296305
return getPreferenceValue(PREF_USE_SYNC_STAGING_SERVER);
297306
}
298307

308+
public static boolean isLeoStagingUsed() {
309+
return getPreferenceValue(PREF_USE_LEO_STAGING_SERVER);
310+
}
311+
299312
public static boolean shouldVlogRewards() {
300313
return getPreferenceValue(PREF_QA_VLOG_REWARDS);
301314
}

android/java/res/xml/qa_preferences.xml

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
android:summaryOn="@string/text_on"
3232
android:summaryOff="@string/text_off" />
3333

34+
<org.chromium.components.browser_ui.settings.ChromeSwitchPreference
35+
android:key="use_leo_staging_server"
36+
android:title="Use Leo staging server"
37+
android:summaryOn="@string/text_on"
38+
android:summaryOff="@string/text_off" />
39+
3440
<org.chromium.components.browser_ui.settings.ChromeSwitchPreference
3541
android:key="qa_maximize_initial_ads_number"
3642
android:title="Maximize initial ads number"

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
@@ -197,5 +197,5 @@ interface AIChatAndroidHelper {
197197

198198
[EnableIf=is_android]
199199
interface IAPSubscription {
200-
GetPurchaseToken() => (string token);
200+
GetPurchaseTokenOrderId() => (string token, string order_id);
201201
};

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)