|
| 1 | +// Copyright 2025 The Chromium Authors |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import {InspectorFrontendHostInstance} from './InspectorFrontendHost.js'; |
| 6 | +import type {DispatchHttpRequestRequest, DispatchHttpRequestResult} from './InspectorFrontendHostAPI.js'; |
| 7 | + |
| 8 | +enum SubscriptionStatus { |
| 9 | + ENABLED = 'SUBSCRIPTION_STATE_ENABLED', |
| 10 | + PENDING = 'SUBSCRIPTION_STATE_PENDING', |
| 11 | + CANCELED = 'SUBSCRIPTION_STATE_CANCELED', |
| 12 | + REFUNDED = 'SUBSCRIPTION_STATE_REFUNDED', |
| 13 | + AWAITING_FIX = 'SUBSCRIPTION_STATE_AWAITING_FIX', |
| 14 | + ON_HOLD = 'SUBSCRIPTION_STATE_ACCOUNT_ON_HOLD', |
| 15 | +} |
| 16 | + |
| 17 | +enum SubscriptionTier { |
| 18 | + PREMIUM_ANNUAL = 'SUBSCRIPTION_TIER_PREMIUM_ANNUAL', |
| 19 | + PREMIUM_MONTHLY = 'SUBSCRIPTION_TIER_PREMIUM_MONTHLY', |
| 20 | + PRO_ANNUAL = 'SUBSCRIPTION_TIER_PRO_ANNUAL', |
| 21 | + PRO_MONTHLY = 'SUBSCRIPTION_TIER_PRO_MONTHLY', |
| 22 | +} |
| 23 | + |
| 24 | +enum EligibilityStatus { |
| 25 | + ELIGIBLE = 'ELIGIBLE', |
| 26 | + NOT_ELIGIBLE = 'NOT_ELIGIBLE', |
| 27 | +} |
| 28 | + |
| 29 | +enum EmailPreference { |
| 30 | + ENABLED = 'ENABLED', |
| 31 | + DISABLED = 'DISABLED', |
| 32 | +} |
| 33 | + |
| 34 | +interface CheckElibigilityResponse { |
| 35 | + createProfile: EligibilityStatus; |
| 36 | +} |
| 37 | + |
| 38 | +interface Profile { |
| 39 | + // Resource name of the profile. |
| 40 | + // Format: profiles/{obfuscated_profile_id} |
| 41 | + name: string; |
| 42 | + activeSubscription?: { |
| 43 | + subscriptionStatus: SubscriptionStatus, |
| 44 | + subscrionTier: SubscriptionTier, |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +async function dispatchHttpRequestPromise<R extends object>(request: DispatchHttpRequestRequest): Promise<R|null> { |
| 49 | + const response = await new Promise<DispatchHttpRequestResult>(resolve => { |
| 50 | + InspectorFrontendHostInstance.dispatchHttpRequest(request, resolve); |
| 51 | + }); |
| 52 | + |
| 53 | + debugLog({request, response}); |
| 54 | + if ('response' in response && response.statusCode === 200) { |
| 55 | + return JSON.parse(response.response) as R; |
| 56 | + } |
| 57 | + |
| 58 | + return null; |
| 59 | +} |
| 60 | + |
| 61 | +const SERVICE_NAME = 'gdpService'; |
| 62 | +let gdpClientInstance: GdpClient|null = null; |
| 63 | +export class GdpClient { |
| 64 | + #cachedProfilePromise?: Promise<Profile|null>; |
| 65 | + #cachedEligibilityPromise?: Promise<CheckElibigilityResponse|null>; |
| 66 | + |
| 67 | + private constructor() { |
| 68 | + } |
| 69 | + |
| 70 | + static instance({forceNew}: { |
| 71 | + forceNew: boolean, |
| 72 | + } = {forceNew: false}): GdpClient { |
| 73 | + if (!gdpClientInstance || forceNew) { |
| 74 | + gdpClientInstance = new GdpClient(); |
| 75 | + } |
| 76 | + return gdpClientInstance; |
| 77 | + } |
| 78 | + |
| 79 | + async getProfile(): Promise<Profile|null> { |
| 80 | + if (this.#cachedProfilePromise) { |
| 81 | + return await this.#cachedProfilePromise; |
| 82 | + } |
| 83 | + |
| 84 | + this.#cachedProfilePromise = dispatchHttpRequestPromise({ |
| 85 | + service: SERVICE_NAME, |
| 86 | + path: '/v1beta1/profile:get', |
| 87 | + method: 'GET', |
| 88 | + }); |
| 89 | + return await this.#cachedProfilePromise; |
| 90 | + } |
| 91 | + |
| 92 | + async checkEligibility(): Promise<CheckElibigilityResponse|null> { |
| 93 | + if (this.#cachedEligibilityPromise) { |
| 94 | + return await this.#cachedEligibilityPromise; |
| 95 | + } |
| 96 | + |
| 97 | + this.#cachedEligibilityPromise = |
| 98 | + dispatchHttpRequestPromise({service: SERVICE_NAME, path: '/v1beta1/eligibility:check', method: 'GET'}); |
| 99 | + |
| 100 | + return await this.#cachedEligibilityPromise; |
| 101 | + } |
| 102 | + |
| 103 | + createProfile({user, emailPreference}: {user: string, emailPreference: EmailPreference}): Promise<Profile|null> { |
| 104 | + return dispatchHttpRequestPromise({ |
| 105 | + service: SERVICE_NAME, |
| 106 | + path: '/v1beta1/profiles', |
| 107 | + method: 'POST', |
| 108 | + body: JSON.stringify({ |
| 109 | + user, |
| 110 | + newsletter_email: emailPreference, |
| 111 | + }) |
| 112 | + }); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +function isDebugMode(): boolean { |
| 117 | + return Boolean(localStorage.getItem('debugGdpIntegrationEnabled')); |
| 118 | +} |
| 119 | + |
| 120 | +function debugLog(...log: unknown[]): void { |
| 121 | + if (!isDebugMode()) { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + // eslint-disable-next-line no-console |
| 126 | + console.log('debugLog', ...log); |
| 127 | +} |
| 128 | + |
| 129 | +function setDebugGdpIntegrationEnabled(enabled: boolean): void { |
| 130 | + if (enabled) { |
| 131 | + localStorage.setItem('debugGdpIntegrationEnabled', 'true'); |
| 132 | + } else { |
| 133 | + localStorage.removeItem('debugGdpIntegrationEnabled'); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// @ts-expect-error |
| 138 | +globalThis.setDebugGdpIntegrationEnabled = setDebugGdpIntegrationEnabled; |
0 commit comments