Skip to content

Commit 6d954aa

Browse files
committed
Proof of concept for brave/brave-browser#33031
Needs more work (UI mock ups, tests, etc)
1 parent d67b8d0 commit 6d954aa

File tree

8 files changed

+62
-19
lines changed

8 files changed

+62
-19
lines changed

components/brave_vpn/browser/brave_vpn_service.cc

+21-17
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,8 @@ void BraveVpnService::UpdatePurchasedStateForSessionExpired(
383383
return;
384384
}
385385

386-
SetPurchasedState(env, PurchasedState::SESSION_EXPIRED);
386+
SetPurchasedState(env, out_of_credentials ? PurchasedState::OUT_OF_CREDENTIALS
387+
: PurchasedState::SESSION_EXPIRED);
387388
}
388389

389390
bool BraveVpnService::IsCurrentRegionSelectedAutomatically(
@@ -572,6 +573,8 @@ void BraveVpnService::OnCredentialSummary(const std::string& domain,
572573
std::string summary_string_trimmed;
573574
base::TrimWhitespaceASCII(summary->message, base::TrimPositions::TRIM_ALL,
574575
&summary_string_trimmed);
576+
//SetPurchasedState(env, PurchasedState::OUT_OF_CREDENTIALS);
577+
//return;
575578
if (summary_string_trimmed.length() == 0) {
576579
// no credential found; person needs to login
577580
VLOG(1) << __func__ << " : No credential found; user needs to login!";
@@ -678,6 +681,7 @@ void BraveVpnService::OnPrepareCredentialsPresentation(
678681
return;
679682
}
680683

684+
out_of_credentials = false;
681685
SetSkusCredential(local_prefs_, credential, time);
682686

683687
if (GetCurrentEnvironment() != env) {
@@ -716,28 +720,28 @@ void BraveVpnService::OnGetSubscriberCredentialV12(
716720
return;
717721
}
718722

719-
// If we get here, we've already tried two credentials (the retry failed).
723+
// We can set the state as FAILED and do not attempt to get another
724+
// credential. The cached credential will eventually expire and user will
725+
// fetch a new one.
726+
//
727+
// There could be two reasons for this.
728+
729+
// 1. We've already tried two credentials (the retry failed).
720730
if (token_no_longer_valid && IsRetriedSkusCredential(local_prefs_)) {
721731
VLOG(2) << __func__
722732
<< " : Got TokenNoLongerValid again with retried skus credential";
733+
out_of_credentials = true;
734+
SetPurchasedState(
735+
GetCurrentEnvironment(), PurchasedState::FAILED,
736+
l10n_util::GetStringUTF8(IDS_BRAVE_VPN_PURCHASE_TOKEN_NOT_VALID));
737+
return;
723738
}
724739

725-
// When this path is reached:
726-
// - The cached credential is considered good but vendor side has an error.
727-
// That could be a network outage or a server side error on vendor side.
728-
// OR
729-
// - The cached credential is consumed and we've now tried two different
730-
// credentials.
731-
//
732-
// We set the state as FAILED and do not attempt to get another credential.
733-
// Cached credential will eventually expire and user will fetch a new one.
734-
//
735-
// This logic can be updated if we issue more than two credentials per day.
736-
auto message_id = token_no_longer_valid
737-
? IDS_BRAVE_VPN_PURCHASE_TOKEN_NOT_VALID
738-
: IDS_BRAVE_VPN_PURCHASE_CREDENTIALS_FETCH_FAILED;
740+
// 2. The cached credential is considered good but vendor side has an error.
741+
// That could be a network outage or a server side error on vendor side.
739742
SetPurchasedState(GetCurrentEnvironment(), PurchasedState::FAILED,
740-
l10n_util::GetStringUTF8(message_id));
743+
l10n_util::GetStringUTF8(
744+
IDS_BRAVE_VPN_PURCHASE_CREDENTIALS_FETCH_FAILED));
741745
#endif
742746
return;
743747
}

components/brave_vpn/browser/brave_vpn_service.h

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ class BraveVpnService :
253253
std::unique_ptr<BraveVPNServiceDelegate> delegate_;
254254
base::RepeatingTimer p3a_timer_;
255255
base::OneShotTimer subs_cred_refresh_timer_;
256+
bool out_of_credentials = false;
256257
base::WeakPtrFactory<BraveVpnService> weak_ptr_factory_{this};
257258
};
258259

components/brave_vpn/common/mojom/brave_vpn.mojom

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ enum PurchasedState {
114114
LOADING,
115115
SESSION_EXPIRED,
116116
FAILED,
117+
OUT_OF_CREDENTIALS,
117118
};
118119

119120
struct PurchasedInfo {

components/brave_vpn/resources/panel/components/main-panel/index.tsx

+10
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ function MainPanel() {
106106
const isSelectingRegion = useSelector((state) => state.isSelectingRegion)
107107
const connectionStatus = useSelector((state) => state.connectionStatus)
108108
const expired = useSelector((state) => state.expired)
109+
const outOfCredentials = useSelector((state) => state.outOfCredentials)
109110
const regions = useSelector((state) => state.regions)
110111

111112
const onSelectRegionButtonClick = () => {
@@ -184,6 +185,15 @@ function MainPanel() {
184185
<SessionExpiredContent />
185186
</S.StyledAlert>
186187
)}
188+
{outOfCredentials && (
189+
<S.StyledAlert
190+
type='warning'
191+
mode='full'
192+
hideIcon
193+
>
194+
<div slot='title'>OUT OF CREDENTIALS</div>
195+
</S.StyledAlert>
196+
)}
187197
<S.RegionSelectorButton
188198
type='button'
189199
onClick={onSelectRegionButtonClick}

components/brave_vpn/resources/panel/state/actions.ts

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export type showMainViewPayload = {
2424
regions: Region[]
2525
connectionStatus: ConnectionState
2626
expired: boolean
27+
outOfCredentials: boolean
2728
}
2829

2930
export type initializedPayload = {
@@ -45,6 +46,7 @@ export const connectionFailed = createAction('connectionFailed')
4546
export const initialize = createAction('initialize')
4647
export const purchaseConfirmed = createAction('purchaseConfirmed')
4748
export const purchaseExpired = createAction('purchaseExpired')
49+
export const outOfCredentials = createAction('outOfCredentials')
4850
export const showSellView = createAction('showSellView')
4951
export const showLoadingView = createAction('showLoadingView')
5052
export const resetConnectionState = createAction('resetConnectionState')

components/brave_vpn/resources/panel/state/async.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ handler.on(Actions.purchaseConfirmed.getType(), async (store) => {
7070
state === ConnectionState.CONNECT_FAILED
7171
? ConnectionState.DISCONNECTED
7272
: state /* Treat connection failure on startup as disconnected */,
73-
expired: false
73+
expired: false,
74+
outOfCredentials: false
7475
})
7576
)
7677
})
@@ -86,7 +87,25 @@ handler.on(Actions.purchaseExpired.getType(), async (store) => {
8687
currentRegion,
8788
regions,
8889
connectionStatus: ConnectionState.DISCONNECTED,
89-
expired: true
90+
expired: true,
91+
outOfCredentials: false
92+
})
93+
)
94+
})
95+
96+
handler.on(Actions.outOfCredentials.getType(), async (store) => {
97+
const [{ currentRegion }, { regions }] = await Promise.all([
98+
getPanelBrowserAPI().serviceHandler.getSelectedRegion(),
99+
getPanelBrowserAPI().serviceHandler.getAllRegions()
100+
])
101+
102+
store.dispatch(
103+
Actions.showMainView({
104+
currentRegion,
105+
regions,
106+
connectionStatus: ConnectionState.DISCONNECTED,
107+
expired: false,
108+
outOfCredentials: true
90109
})
91110
)
92111
})

components/brave_vpn/resources/panel/state/reducer.ts

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type RootState = {
1313
hasError: boolean
1414
isSelectingRegion: boolean
1515
expired: boolean
16+
outOfCredentials: boolean
1617
connectionStatus: ConnectionState
1718
regions: Region[]
1819
currentRegion: Region
@@ -25,6 +26,7 @@ const defaultState: RootState = {
2526
hasError: false,
2627
isSelectingRegion: false,
2728
expired: false,
29+
outOfCredentials: false,
2830
connectionStatus: ConnectionState.DISCONNECTED,
2931
regions: [],
3032
currentRegion: new Region(),
@@ -148,6 +150,7 @@ reducer.on(Actions.showMainView, (state, payload): RootState => {
148150
return {
149151
...state,
150152
expired: payload.expired,
153+
outOfCredentials: payload.outOfCredentials,
151154
currentRegion: payload.currentRegion,
152155
regions: payload.regions,
153156
connectionStatus: payload.connectionStatus,

components/brave_vpn/resources/panel/state/store.ts

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ const observer = {
3232
case PurchasedState.SESSION_EXPIRED:
3333
store.dispatch(Actions.purchaseExpired())
3434
break
35+
case PurchasedState.OUT_OF_CREDENTIALS:
36+
store.dispatch(Actions.outOfCredentials())
37+
break
3538
case PurchasedState.FAILED:
3639
store.dispatch(Actions.purchaseFailed({
3740
state: PurchasedState.FAILED, stateDescription: description

0 commit comments

Comments
 (0)