1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
IPPStartupCache: "resource:///modules/ipprotection/IPPStartupCache.sys.mjs",
IPProtectionService:
"resource:///modules/ipprotection/IPProtectionService.sys.mjs",
IPPSignInWatcher: "resource:///modules/ipprotection/IPPSignInWatcher.sys.mjs",
});
const LOG_PREF = "browser.ipProtection.log";
ChromeUtils.defineLazyGetter(lazy, "logConsole", function () {
return console.createInstance({
prefix: "IPPEnrollAndEntitleManager",
maxLogLevel: Services.prefs.getBoolPref(LOG_PREF, false) ? "Debug" : "Warn",
});
});
/**
* This class manages the enrolling and entitlement.
*/
class IPPEnrollAndEntitleManagerSingleton extends EventTarget {
#runningPromise = null;
#entitlement = null;
constructor() {
super();
this.handleEvent = this.#handleEvent.bind(this);
}
init() {
// We will use data from the cache until we are fully functional. Then we
// will recompute the state in `initOnStartupCompleted`.
this.#entitlement = lazy.IPPStartupCache.entitlement;
lazy.IPPSignInWatcher.addEventListener(
"IPPSignInWatcher:StateChanged",
this.handleEvent
);
}
initOnStartupCompleted() {
if (!lazy.IPPSignInWatcher.isSignedIn) {
return;
}
try {
// This bit must be async because we want to trigger the updateState at
// the end of the rest of the initialization.
lazy.IPProtectionService.guardian
.isLinkedToGuardian(/* only cache: */ true)
.then(
async isLinked => {
if (isLinked) {
const { status, entitlement } =
await lazy.IPProtectionService.guardian.fetchUserInfo();
if (status === 200) {
this.#setEntitlement(entitlement);
return;
}
}
this.#setEntitlement(null);
},
() => {
// In case we were using cached values, it's time to reset them.
this.#setEntitlement(null);
}
);
} catch (_) {
// In case we were using cached values, it's time to reset them.
this.#setEntitlement(null);
}
}
uninit() {
lazy.IPPSignInWatcher.removeEventListener(
"IPPSignInWatcher:StateChanged",
this.handleEvent
);
this.#entitlement = null;
}
#handleEvent(_event) {
if (!lazy.IPPSignInWatcher.isSignedIn) {
this.#setEntitlement(null);
return;
}
this.maybeEnrollAndEntitle();
}
maybeEnrollAndEntitle(forceRefetch = false) {
if (this.#runningPromise) {
return this.#runningPromise;
}
if (this.#entitlement && !forceRefetch) {
return Promise.resolve({ isEnrolledAndEntitled: true });
}
const enrollAndEntitle = async () => {
const data =
await IPPEnrollAndEntitleManagerSingleton.#maybeEnrollAndEntitle();
if (!data.entitlement) {
// Unset the entitlement if not available.
this.#setEntitlement(null);
return { isEnrolledAndEntitled: false, error: data.error };
}
this.#setEntitlement(data.entitlement);
return { isEnrolledAndEntitled: true };
};
this.#runningPromise = enrollAndEntitle().finally(() => {
this.#runningPromise = null;
});
return this.#runningPromise;
}
// This method is static because we don't want to change the internal state
// of the singleton.
static async #maybeEnrollAndEntitle() {
let isLinked = false;
try {
isLinked = await lazy.IPProtectionService.guardian.isLinkedToGuardian(
/* only cache: */ false
);
} catch (error) {
// If not linked, it's not an issue.
}
if (isLinked) {
// Linked does not mean enrolled: it could be that the link comes from a
// previous MozillaVPN subscription. Let's see if `fetchUserInfo` is able
// to obtain the entitlement.
const { status, entitlement } =
await lazy.IPProtectionService.guardian.fetchUserInfo();
if (status === 200) {
return { entitlement };
}
}
try {
const enrollment = await lazy.IPProtectionService.guardian.enroll();
if (!enrollment?.ok) {
return { entitlement: null, error: enrollment?.error };
}
} catch (error) {
return { enrollment: null, error: error?.message };
}
const { status, entitlement, error } =
await lazy.IPProtectionService.guardian.fetchUserInfo();
lazy.logConsole.debug("Entitlement:", { status, entitlement, error });
// If we see an error during the READY state, let's trigger an error state.
if (error || !entitlement || status != 200) {
return { entitlement: null, error: error || `Status: ${status}` };
}
return { entitlement };
}
#setEntitlement(entitlement) {
this.#entitlement = entitlement;
lazy.IPPStartupCache.storeEntitlement(this.#entitlement);
lazy.IPProtectionService.updateState();
this.dispatchEvent(
new CustomEvent("IPPEnrollAndEntitleManager:StateChanged", {
bubbles: true,
composed: true,
})
);
}
get isEnrolledAndEntitled() {
return !!this.#entitlement;
}
/**
* Checks if a user has upgraded.
*
* @returns {boolean}
*/
get hasUpgraded() {
return this.#entitlement?.subscribed;
}
/**
* Checks if the entitlement exists and it contains a UUID
*/
get hasEntitlementUid() {
return !!this.#entitlement?.uid;
}
/**
* Checks if we have the entitlement
*/
get hasEntitlement() {
return !!this.#entitlement;
}
/**
* Checks if we're running the Alpha variant based on
* available features
*/
get isAlpha() {
return (
!this.#entitlement?.autostart &&
!this.#entitlement?.website_inclusion &&
!this.#entitlement?.location_controls
);
}
async refetchEntitlement() {
await this.maybeEnrollAndEntitle(true);
}
resetEntitlement() {
this.#setEntitlement(null);
}
}
const IPPEnrollAndEntitleManager = new IPPEnrollAndEntitleManagerSingleton();
export { IPPEnrollAndEntitleManager };
|