Skip to content

Commit eebfd59

Browse files
committed
Don't let the platform be changed after Glean was initialized
1 parent eb58d5d commit eebfd59

File tree

6 files changed

+24
-0
lines changed

6 files changed

+24
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[Full changelog](https://github.com/mozilla/glean.js/compare/v0.8.0...main)
44

5+
* [#201](https://github.com/mozilla/glean.js/pull/201): BUGFIX: Do not let the platform be changed after Glean is initialized.
56
# v0.8.0 (2021-04-13)
67

78
[Full changelog](https://github.com/mozilla/glean.js/compare/v0.7.0...v0.8.0)

glean/src/core/glean.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,14 @@ class Glean {
443443
* Please check out the available environments in the platform/ module.
444444
*/
445445
static setPlatform(platform: Platform): void {
446+
if (Context.initialized) {
447+
console.info(
448+
"Attempted to set the platform but Glean has already been initialized. Ignoring.",
449+
`Current platform is: ${Glean.platform.name}}`
450+
);
451+
return;
452+
}
453+
446454
Glean.instance._platform = platform;
447455
}
448456

glean/src/platform/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ interface Platform {
2020
uploader: Uploader,
2121
// The environment specifici implemtation of platform information getters
2222
info: PlatformInfo,
23+
// The name of the platform, useful for logging and debugging purposes.
24+
name: string,
2325
}
2426

2527
export default Platform;

glean/src/platform/test/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const TestPlatform: Platform = {
4343
Storage: MockStorage,
4444
uploader: new MockUploader(),
4545
info: MockPlatformInfo,
46+
name: "test"
4647
};
4748

4849
export default TestPlatform;

glean/src/platform/webext/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const WebExtPlatform: Platform = {
1212
Storage,
1313
uploader,
1414
info,
15+
name: "webext"
1516
};
1617

1718
export default WebExtPlatform;

glean/tests/core/glean.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,4 +514,15 @@ describe("Glean", function() {
514514
[1,2,3,4,5,6,7,8,9,10]
515515
);
516516
});
517+
518+
it("disallow changing the platform after Glean is initialized", async function() {
519+
const MockPlatform = {
520+
...TestPlatform,
521+
name: "mock"
522+
};
523+
524+
Glean.setPlatform(MockPlatform);
525+
526+
assert.strictEqual(TestPlatform.name, Glean.platform.name);
527+
});
517528
});

0 commit comments

Comments
 (0)