22 * License, v. 2.0. If a copy of the MPL was not distributed with this
33 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
5+ import Platform from "../../platform/index.js" ;
6+ import { Configuration } from "../config.js" ;
57import { GLEAN_VERSION } from "../constants.js" ;
68import { Observer as PingsDatabaseObserver , PingInternalRepresentation } from "../pings/database.js" ;
7- import Glean from "../glean.js" ;
9+ import type PingsDatabase from "../pings/database.js" ;
10+ import PlatformInfo from "../platform_info.js" ;
811import Uploader , { UploadResult , UploadResultStatus } from "./uploader.js" ;
912
1013interface QueuedPing extends PingInternalRepresentation {
@@ -45,10 +48,34 @@ class PingUploader implements PingsDatabaseObserver {
4548 // The object that concretely handles the ping transmission.
4649 private readonly uploader : Uploader ;
4750
48- constructor ( uploader : Uploader ) {
51+ private readonly platformInfo : PlatformInfo ;
52+ private readonly serverEndpoint : string ;
53+ private readonly pingsDatabase : PingsDatabase ;
54+
55+ // Whether or not Glean was initialized, as reported by the
56+ // singleton object.
57+ private initialized = false ;
58+
59+ constructor ( config : Configuration , platform : Platform , pingsDatabase : PingsDatabase ) {
4960 this . queue = [ ] ;
5061 this . status = PingUploaderStatus . Idle ;
51- this . uploader = uploader ;
62+ // Initialize the ping uploader with either the platform defaults or a custom
63+ // provided uploader from the configuration object.
64+ this . uploader = config . httpClient ? config . httpClient : platform . uploader ;
65+ this . platformInfo = platform . info ;
66+ this . serverEndpoint = config . serverEndpoint ;
67+ this . pingsDatabase = pingsDatabase ;
68+ }
69+
70+ /**
71+ * Signals that initialization of Glean was completed.
72+ *
73+ * This is required in order to not depend on the Glean object.
74+ *
75+ * @param state An optional state to set the initialization status to.
76+ */
77+ setInitialized ( state ?: boolean ) : void {
78+ this . initialized = state ?? true ;
5279 }
5380
5481 /**
@@ -105,7 +132,7 @@ class PingUploader implements PingsDatabaseObserver {
105132 "Date" : ( new Date ( ) ) . toISOString ( ) ,
106133 "X-Client-Type" : "Glean.js" ,
107134 "X-Client-Version" : GLEAN_VERSION ,
108- "User-Agent" : `Glean/${ GLEAN_VERSION } (JS on ${ await Glean . platform . info . os ( ) } )`
135+ "User-Agent" : `Glean/${ GLEAN_VERSION } (JS on ${ await this . platformInfo . os ( ) } )`
109136 } ;
110137
111138 return {
@@ -122,7 +149,7 @@ class PingUploader implements PingsDatabaseObserver {
122149 * @returns The status number of the response or `undefined` if unable to attempt upload.
123150 */
124151 private async attemptPingUpload ( ping : QueuedPing ) : Promise < UploadResult > {
125- if ( ! Glean . initialized ) {
152+ if ( ! this . initialized ) {
126153 console . warn ( "Attempted to upload a ping, but Glean is not initialized yet. Ignoring." ) ;
127154 return { result : UploadResultStatus . RecoverableFailure } ;
128155 }
@@ -132,9 +159,7 @@ class PingUploader implements PingsDatabaseObserver {
132159 // We are sure that the applicationId is not `undefined` at this point,
133160 // this function is only called when submitting a ping
134161 // and that function return early when Glean is not initialized.
135- //
136- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
137- `${ Glean . serverEndpoint ! } ${ ping . path } ` ,
162+ `${ this . serverEndpoint } ${ ping . path } ` ,
138163 finalPing . payload ,
139164 finalPing . headers
140165 ) ;
@@ -183,14 +208,14 @@ class PingUploader implements PingsDatabaseObserver {
183208 const { status, result } = response ;
184209 if ( status && status >= 200 && status < 300 ) {
185210 console . info ( `Ping ${ identifier } succesfully sent ${ status } .` ) ;
186- await Glean . pingsDatabase . deletePing ( identifier ) ;
211+ await this . pingsDatabase . deletePing ( identifier ) ;
187212 return false ;
188213 }
189214
190215 if ( result === UploadResultStatus . UnrecoverableFailure || ( status && status >= 400 && status < 500 ) ) {
191216 console . warn (
192217 `Unrecoverable upload failure while attempting to send ping ${ identifier } . Error was ${ status ?? "no status" } .` ) ;
193- await Glean . pingsDatabase . deletePing ( identifier ) ;
218+ await this . pingsDatabase . deletePing ( identifier ) ;
194219 return false ;
195220 }
196221
@@ -273,16 +298,6 @@ class PingUploader implements PingsDatabaseObserver {
273298 this . queue = [ ] ;
274299 }
275300
276- /**
277- * Scans the database for pending pings and enqueues them.
278- */
279- async scanPendingPings ( ) : Promise < void > {
280- const pings = await Glean . pingsDatabase . getAllPings ( ) ;
281- for ( const identifier in pings ) {
282- this . enqueuePing ( { identifier, ...pings [ identifier ] } ) ;
283- }
284- }
285-
286301 /**
287302 * Enqueues a new ping and trigger uploading of enqueued pings.
288303 *
0 commit comments