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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
|
/* 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/. */
import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
WindowsRegistry: "resource://gre/modules/WindowsRegistry.sys.mjs",
WindowsVersionInfo:
"resource://gre/modules/components-utils/WindowsVersionInfo.sys.mjs",
ctypes: "resource://gre/modules/ctypes.sys.mjs",
});
const PER_INSTALLATION_PREFS_PLATFORMS = ["win"];
// The file that stores Application Update configuration settings. The file is
// located in the update directory which makes it a common setting across all
// application profiles and allows the Background Update Agent to read it.
const FILE_UPDATE_CONFIG_JSON = "update-config.json";
const FILE_UPDATE_LOCALE = "update.locale";
const PREF_APP_DISTRIBUTION = "distribution.id";
const PREF_APP_DISTRIBUTION_VERSION = "distribution.version";
export var UpdateUtils = {
_locale: undefined,
_configFilePath: undefined,
/**
* Read the update channel from defaults only. We do this to ensure that
* the channel is tightly coupled with the application and does not apply
* to other instances of the application that may use the same profile.
*
* @param [optional] aIncludePartners
* Whether or not to include the partner bits. Default: true.
*/
getUpdateChannel(aIncludePartners = true) {
let defaults = Services.prefs.getDefaultBranch(null);
let channel = defaults.getCharPref(
"app.update.channel",
AppConstants.MOZ_UPDATE_CHANNEL
);
if (aIncludePartners) {
try {
let partners = Services.prefs.getChildList("app.partner.").sort();
if (partners.length) {
channel += "-cck";
partners.forEach(function (prefName) {
channel += "-" + Services.prefs.getCharPref(prefName);
});
}
} catch (e) {
console.error(e);
}
}
return channel;
},
get UpdateChannel() {
return this.getUpdateChannel();
},
/**
* Formats a URL by replacing %...% values with OS, build and locale specific
* values.
*
* @param url
* The URL to format.
* @return The formatted URL.
*/
async formatUpdateURL(url) {
const locale = await this.getLocale();
return url.replace(/%(\w+)%/g, (match, name) => {
let replacement = match;
switch (name) {
case "PRODUCT":
replacement = Services.appinfo.name;
break;
case "VERSION":
replacement = Services.appinfo.version;
break;
case "BUILD_ID":
replacement = Services.appinfo.appBuildID;
break;
case "BUILD_TARGET":
replacement = Services.appinfo.OS + "_" + this.ABI;
break;
case "OS_VERSION":
replacement = this.OSVersion;
break;
case "LOCALE":
replacement = locale;
break;
case "CHANNEL":
replacement = this.UpdateChannel;
break;
case "PLATFORM_VERSION":
replacement = Services.appinfo.platformVersion;
break;
case "SYSTEM_CAPABILITIES":
replacement = getSystemCapabilities();
break;
case "DISTRIBUTION":
replacement = getDistributionPrefValue(PREF_APP_DISTRIBUTION);
break;
case "DISTRIBUTION_VERSION":
replacement = getDistributionPrefValue(PREF_APP_DISTRIBUTION_VERSION);
break;
}
return encodeURIComponent(replacement);
});
},
/**
* Gets the locale from the update.locale file for replacing %LOCALE% in the
* update url. The update.locale file can be located in the application
* directory or the GRE directory with preference given to it being located in
* the application directory.
*/
async getLocale() {
if (this._locale !== undefined) {
return this._locale;
}
for (let res of ["app", "gre"]) {
const url = "resource://" + res + "/" + FILE_UPDATE_LOCALE;
let data;
try {
data = await fetch(url);
} catch (e) {
continue;
}
const locale = await data.text();
if (locale) {
return (this._locale = locale.trim());
}
}
console.error(
FILE_UPDATE_LOCALE,
" file doesn't exist in either the application or GRE directories"
);
return (this._locale = null);
},
/* Get the path to the config file. */
getConfigFilePath() {
let path = PathUtils.join(
Services.dirsvc.get("UpdRootD", Ci.nsIFile).path,
FILE_UPDATE_CONFIG_JSON
);
return (this._configFilePath = path);
},
get configFilePath() {
if (this._configFilePath !== undefined) {
return this._configFilePath;
}
return this.getConfigFilePath();
},
/**
* Determines whether or not the Application Update Service automatically
* downloads and installs updates. This corresponds to whether or not the user
* has selected "Automatically install updates" in about:preferences.
*
* On Windows, this setting is shared across all profiles for the installation
* and is read asynchronously from the file. On other operating systems, this
* setting is stored in a pref and is thus a per-profile setting.
*
* @return A Promise that resolves with a boolean.
*/
async getAppUpdateAutoEnabled() {
return this.readUpdateConfigSetting("app.update.auto");
},
/**
* Toggles whether the Update Service automatically downloads and installs
* updates. This effectively selects between the "Automatically install
* updates" and "Check for updates but let you choose to install them" options
* in about:preferences.
*
* On Windows, this setting is shared across all profiles for the installation
* and is written asynchronously to the file. On other operating systems, this
* setting is stored in a pref and is thus a per-profile setting.
*
* If this method is called when the setting is locked, the returned promise
* will reject. The lock status can be determined with
* UpdateUtils.appUpdateAutoSettingIsLocked()
*
* @param enabled If set to true, automatic download and installation of
* updates will be enabled. If set to false, this will be
* disabled.
* @return A Promise that, once the setting has been saved, resolves with the
* boolean value that was saved. If the setting could not be
* successfully saved, the Promise will reject.
* On Windows, where this setting is stored in a file, this Promise
* may reject with an I/O error.
* On other operating systems, this promise should not reject as
* this operation simply sets a pref.
*/
async setAppUpdateAutoEnabled(enabledValue) {
return this.writeUpdateConfigSetting("app.update.auto", !!enabledValue);
},
/**
* This function should be used to determine if the automatic application
* update setting is locked by an enterprise policy
*
* @return true if the automatic update setting is currently locked.
* Otherwise, false.
*/
appUpdateAutoSettingIsLocked() {
return this.appUpdateSettingIsLocked("app.update.auto");
},
/**
* Indicates whether or not per-installation prefs are supported on this
* platform.
*/
PER_INSTALLATION_PREFS_SUPPORTED: PER_INSTALLATION_PREFS_PLATFORMS.includes(
AppConstants.platform
),
/**
* Possible per-installation pref types.
*/
PER_INSTALLATION_PREF_TYPE_BOOL: "boolean",
PER_INSTALLATION_PREF_TYPE_ASCII_STRING: "ascii",
PER_INSTALLATION_PREF_TYPE_INT: "integer",
/**
* We want the preference definitions to be part of UpdateUtils for a couple
* of reasons. It's a clean way for consumers to look up things like observer
* topic names. It also allows us to manipulate the supported prefs during
* testing. However, we want to use values out of UpdateUtils (like pref
* types) to construct this object. Therefore, this will initially be a
* placeholder, which we will properly define after the UpdateUtils object
* definition.
*/
PER_INSTALLATION_PREFS: null,
/**
* This function initializes per-installation prefs. Note that it does not
* need to be called manually; it is already called within the file.
*
* This function is called on startup, so it does not read or write to disk.
*/
initPerInstallPrefs() {
// If we don't have per-installation prefs, we store the update config in
// preferences. In that case, the best way to notify observers of this
// setting is just to propagate it from a pref observer. This ensures that
// the expected observers still get notified, even if a user manually
// changes the pref value.
if (!UpdateUtils.PER_INSTALLATION_PREFS_SUPPORTED) {
let initialConfig = {};
for (const [prefName, pref] of Object.entries(
UpdateUtils.PER_INSTALLATION_PREFS
)) {
const prefTypeFns = TYPE_SPECIFIC_PREF_FNS[pref.type];
try {
let initialValue = prefTypeFns.getProfilePref(prefName);
initialConfig[prefName] = initialValue;
} catch (e) {}
Services.prefs.addObserver(prefName, async () => {
let config = { ...gUpdateConfigCache };
config[prefName] = await UpdateUtils.readUpdateConfigSetting(
prefName
);
maybeUpdateConfigChanged(config);
});
}
// On the first call to maybeUpdateConfigChanged, it has nothing to
// compare its input to, so it just populates the cache and doesn't notify
// any observers. This makes sense during normal usage, because the first
// call will be on the first config file read, and we don't want to notify
// observers of changes on the first read. But that means that when
// propagating pref observers, we need to make one initial call to
// simulate that initial read so that the cache will be populated when the
// first pref observer fires.
maybeUpdateConfigChanged(initialConfig);
}
},
/**
* Reads an installation-specific configuration setting from the update config
* JSON file. This function is guaranteed not to throw. If there are problems
* reading the file, the default value will be returned so that update can
* proceed. This is particularly important since the configuration file is
* writable by anyone and we don't want an unprivileged user to be able to
* break update for other users.
*
* If relevant policies are active, this function will read the policy value
* rather than the stored value.
*
* @param prefName
* The preference to read. Must be a key of the
* PER_INSTALLATION_PREFS object.
* @return A Promise that resolves with the pref's value.
*/
readUpdateConfigSetting(prefName) {
if (!(prefName in this.PER_INSTALLATION_PREFS)) {
return Promise.reject(
new Error(
`UpdateUtils.readUpdateConfigSetting: Unknown per-installation ` +
`pref '${prefName}'`
)
);
}
const pref = this.PER_INSTALLATION_PREFS[prefName];
const prefTypeFns = TYPE_SPECIFIC_PREF_FNS[pref.type];
if (Services.policies && "policyFn" in pref) {
let policyValue = pref.policyFn();
if (policyValue !== null) {
return Promise.resolve(policyValue);
}
}
if (!this.PER_INSTALLATION_PREFS_SUPPORTED) {
// If we don't have per-installation prefs, we use regular preferences.
let prefValue = prefTypeFns.getProfilePref(prefName, pref.defaultValue);
return Promise.resolve(prefValue);
}
let readPromise = updateConfigIOPromise
// All promises returned by (read|write)UpdateConfigSetting are part of a
// single promise chain in order to serialize disk operations. But we
// don't want the entire promise chain to reject when one operation fails.
// So we are going to silently clear any rejections the promise chain
// might contain.
//
// We will also pass an empty function for the first then() argument as
// well, just to make sure we are starting fresh rather than potentially
// propagating some stale value.
.then(
() => {},
() => {}
)
.then(readUpdateConfig)
.then(maybeUpdateConfigChanged)
.then(config => {
return readEffectiveValue(config, prefName);
});
updateConfigIOPromise = readPromise;
return readPromise;
},
/**
* Changes an installation-specific configuration setting by writing it to
* the update config JSON file.
*
* If this method is called on a prefName that is locked, the returned promise
* will reject. The lock status can be determined with
* appUpdateSettingIsLocked().
*
* @param prefName
* The preference to change. This must be a key of the
* PER_INSTALLATION_PREFS object.
* @param value
* The value to be written. Its type must match
* PER_INSTALLATION_PREFS[prefName].type
* @param options
* Optional. An object containing any of the following keys:
* setDefaultOnly
* If set to true, the default branch value will be set rather
* than user value. If a user value is set for this pref, this
* will have no effect on the pref's effective value.
* NOTE - The behavior of the default pref branch currently
* differs depending on whether the current platform
* supports per-installation prefs. If they are
* supported, default branch values persist across
* Firefox sessions. If they aren't supported, default
* branch values reset when Firefox shuts down.
* @return A Promise that, once the setting has been saved, resolves with the
* value that was saved.
* @throw If there is an I/O error when attempting to write to the config
* file, the returned Promise will reject with a DOMException.
*/
writeUpdateConfigSetting(prefName, value, options) {
if (!(prefName in this.PER_INSTALLATION_PREFS)) {
return Promise.reject(
new Error(
`UpdateUtils.writeUpdateConfigSetting: Unknown per-installation ` +
`pref '${prefName}'`
)
);
}
if (this.appUpdateSettingIsLocked(prefName)) {
return Promise.reject(
new Error(
`UpdateUtils.writeUpdateConfigSetting: Unable to change value of ` +
`setting '${prefName}' because it is locked by policy`
)
);
}
if (!options) {
options = {};
}
const pref = this.PER_INSTALLATION_PREFS[prefName];
const prefTypeFns = TYPE_SPECIFIC_PREF_FNS[pref.type];
if (!prefTypeFns.isValid(value)) {
return Promise.reject(
new Error(
`UpdateUtils.writeUpdateConfigSetting: Attempted to change pref ` +
`'${prefName} to invalid value: ${JSON.stringify(value)}`
)
);
}
if (!this.PER_INSTALLATION_PREFS_SUPPORTED) {
// If we don't have per-installation prefs, we use regular preferences.
if (options.setDefaultOnly) {
prefTypeFns.setProfileDefaultPref(prefName, value);
} else {
prefTypeFns.setProfilePref(prefName, value);
}
// Rather than call maybeUpdateConfigChanged, a pref observer has
// been connected to the relevant pref. This allows us to catch direct
// changes to prefs (which Firefox shouldn't be doing, but the user
// might do in about:config).
return Promise.resolve(value);
}
let writePromise = updateConfigIOPromise
// All promises returned by (read|write)UpdateConfigSetting are part of a
// single promise chain in order to serialize disk operations. But we
// don't want the entire promise chain to reject when one operation fails.
// So we are going to silently clear any rejections the promise chain
// might contain.
//
// We will also pass an empty function for the first then() argument as
// well, just to make sure we are starting fresh rather than potentially
// propagating some stale value.
.then(
() => {},
() => {}
)
// We always re-read the update config before writing, rather than using a
// cached version. Otherwise, two simultaneous instances may overwrite
// each other's changes.
.then(readUpdateConfig)
.then(async config => {
setConfigValue(config, prefName, value, {
setDefaultOnly: !!options.setDefaultOnly,
});
try {
await writeUpdateConfig(config);
return config;
} catch (e) {
console.error(
"UpdateUtils.writeUpdateConfigSetting: App update configuration " +
"file write failed. Exception: ",
e
);
// Re-throw the error so the caller knows that writing the value in
// the app update config file failed.
throw e;
}
})
.then(maybeUpdateConfigChanged)
.then(() => {
// If this value wasn't written, a previous promise in the chain will
// have thrown, so we can unconditionally return the expected written
// value as the value that was written.
return value;
});
updateConfigIOPromise = writePromise;
return writePromise;
},
/**
* Returns true if the specified pref is controlled by policy and thus should
* not be changeable by the user.
*/
appUpdateSettingIsLocked(prefName) {
if (!(prefName in UpdateUtils.PER_INSTALLATION_PREFS)) {
return Promise.reject(
new Error(
`UpdateUtils.appUpdateSettingIsLocked: Unknown per-installation pref '${prefName}'`
)
);
}
// If we don't have policy support, nothing can be locked.
if (!Services.policies) {
return false;
}
const pref = UpdateUtils.PER_INSTALLATION_PREFS[prefName];
if (!pref.policyFn) {
return false;
}
const policyValue = pref.policyFn();
return policyValue !== null;
},
};
const PER_INSTALLATION_DEFAULTS_BRANCH = "__DEFAULTS__";
/**
* Some prefs are specific to the installation, not the profile. They are
* stored in JSON format in FILE_UPDATE_CONFIG_JSON.
* Not all platforms currently support per-installation prefs, in which case
* we fall back to using profile-specific prefs.
*
* Note: These prefs should always be accessed through UpdateUtils. Do NOT
* attempt to read or write their prefs directly.
*
* Keys in this object should be the name of the pref. The same name will be
* used whether we are writing it to the per-installation or per-profile pref.
* Values in this object should be objects with the following keys:
* type
* Must be one of the Update.PER_INSTALLATION_PREF_TYPE_* values, defined
* above.
* defaultValue
* The default value to use for this pref if no value is set. This must be
* of a type that is compatible with the type value specified.
* migrate
* Optional - defaults to false. A boolean indicating whether an existing
* value in the profile-specific prefs ought to be migrated to an
* installation specific pref. This is useful for prefs like
* app.update.auto that used to be profile-specific prefs.
* Note - Migration currently happens only on the creation of the JSON
* file. If we want to add more prefs that require migration, we
* will probably need to change this.
* observerTopic
* When a config value is changed, an observer will be fired, much like
* the existing preference observers. This specifies the topic of the
* observer that will be fired.
* policyFn
* Optional. If defined, should be a function that returns null or a value
* of the specified type of this pref. If null is returned, this has no
* effect. If another value is returned, it will be used rather than
* reading the pref. This function will only be called if
* Services.policies is defined. Asynchronous functions are not currently
* supported.
*/
UpdateUtils.PER_INSTALLATION_PREFS = {
"app.update.auto": {
type: UpdateUtils.PER_INSTALLATION_PREF_TYPE_BOOL,
defaultValue: true,
migrate: true,
observerTopic: "auto-update-config-change",
policyFn: () => {
if (!Services.policies.isAllowed("app-auto-updates-off")) {
// We aren't allowed to turn off auto-update - it is forced on.
return true;
}
if (!Services.policies.isAllowed("app-auto-updates-on")) {
// We aren't allowed to turn on auto-update - it is forced off.
return false;
}
return null;
},
},
"app.update.background.enabled": {
type: UpdateUtils.PER_INSTALLATION_PREF_TYPE_BOOL,
defaultValue: true,
observerTopic: "background-update-config-change",
policyFn: () => {
if (!Services.policies.isAllowed("app-background-update-off")) {
// We aren't allowed to turn off background update - it is forced on.
return true;
}
if (!Services.policies.isAllowed("app-background-update-on")) {
// We aren't allowed to turn on background update - it is forced off.
return false;
}
return null;
},
},
};
const TYPE_SPECIFIC_PREF_FNS = {
[UpdateUtils.PER_INSTALLATION_PREF_TYPE_BOOL]: {
getProfilePref: Services.prefs.getBoolPref,
setProfilePref: Services.prefs.setBoolPref,
setProfileDefaultPref: (pref, value) => {
let defaults = Services.prefs.getDefaultBranch("");
defaults.setBoolPref(pref, value);
},
isValid: value => typeof value == "boolean",
},
[UpdateUtils.PER_INSTALLATION_PREF_TYPE_ASCII_STRING]: {
getProfilePref: Services.prefs.getCharPref,
setProfilePref: Services.prefs.setCharPref,
setProfileDefaultPref: (pref, value) => {
let defaults = Services.prefs.getDefaultBranch("");
defaults.setCharPref(pref, value);
},
isValid: value => typeof value == "string",
},
[UpdateUtils.PER_INSTALLATION_PREF_TYPE_INT]: {
getProfilePref: Services.prefs.getIntPref,
setProfilePref: Services.prefs.setIntPref,
setProfileDefaultPref: (pref, value) => {
let defaults = Services.prefs.getDefaultBranch("");
defaults.setIntPref(pref, value);
},
isValid: value => Number.isInteger(value),
},
};
/**
* Used for serializing reads and writes of the app update json config file so
* the writes don't happen out of order and the last write is the one that
* the sets the value.
*/
var updateConfigIOPromise = Promise.resolve();
/**
* Returns a pref name that we will use to keep track of if the passed pref has
* been migrated already, so we don't end up migrating it twice.
*/
function getPrefMigratedPref(prefName) {
return prefName + ".migrated";
}
/**
* @return true if prefs need to be migrated from profile-specific prefs to
* installation-specific prefs.
*/
function updateConfigNeedsMigration() {
for (const [prefName, pref] of Object.entries(
UpdateUtils.PER_INSTALLATION_PREFS
)) {
if (pref.migrate) {
let migratedPrefName = getPrefMigratedPref(prefName);
let migrated = Services.prefs.getBoolPref(migratedPrefName, false);
if (!migrated) {
return true;
}
}
}
return false;
}
function setUpdateConfigMigrationDone() {
for (const [prefName, pref] of Object.entries(
UpdateUtils.PER_INSTALLATION_PREFS
)) {
if (pref.migrate) {
let migratedPrefName = getPrefMigratedPref(prefName);
Services.prefs.setBoolPref(migratedPrefName, true);
}
}
}
/**
* Deletes the migrated data.
*/
function onMigrationSuccessful() {
for (const [prefName, pref] of Object.entries(
UpdateUtils.PER_INSTALLATION_PREFS
)) {
if (pref.migrate) {
Services.prefs.clearUserPref(prefName);
}
}
}
function makeMigrationUpdateConfig() {
let config = makeDefaultUpdateConfig();
for (const [prefName, pref] of Object.entries(
UpdateUtils.PER_INSTALLATION_PREFS
)) {
if (!pref.migrate) {
continue;
}
let migratedPrefName = getPrefMigratedPref(prefName);
let alreadyMigrated = Services.prefs.getBoolPref(migratedPrefName, false);
if (alreadyMigrated) {
continue;
}
const prefTypeFns = TYPE_SPECIFIC_PREF_FNS[pref.type];
let prefHasValue = true;
let prefValue;
try {
// Without a second argument, this will throw if the pref has no user
// value or default value.
prefValue = prefTypeFns.getProfilePref(prefName);
} catch (e) {
prefHasValue = false;
}
if (prefHasValue) {
setConfigValue(config, prefName, prefValue);
}
}
return config;
}
function makeDefaultUpdateConfig() {
let config = {};
for (const [prefName, pref] of Object.entries(
UpdateUtils.PER_INSTALLATION_PREFS
)) {
setConfigValue(config, prefName, pref.defaultValue, {
setDefaultOnly: true,
});
}
return config;
}
/**
* Sets the specified value in the config object.
*
* @param config
* The config object for which to set the value
* @param prefName
* The name of the preference to set.
* @param prefValue
* The value to set the preference to.
* @param options
* Optional. An object containing any of the following keys:
* setDefaultOnly
* If set to true, the default value will be set rather than
* user value. If a user value is set for this pref, this will
* have no effect on the pref's effective value.
*/
function setConfigValue(config, prefName, prefValue, options) {
if (!options) {
options = {};
}
if (options.setDefaultOnly) {
if (!(PER_INSTALLATION_DEFAULTS_BRANCH in config)) {
config[PER_INSTALLATION_DEFAULTS_BRANCH] = {};
}
config[PER_INSTALLATION_DEFAULTS_BRANCH][prefName] = prefValue;
} else if (prefValue != readDefaultValue(config, prefName)) {
config[prefName] = prefValue;
} else {
delete config[prefName];
}
}
/**
* Reads the specified pref out of the given configuration object.
* If a user value of the pref is set, that will be returned. If only a default
* branch value is set, that will be returned. Otherwise, the default value from
* PER_INSTALLATION_PREFS will be returned.
*
* Values will be validated before being returned. Invalid values are ignored.
*
* @param config
* The configuration object to read.
* @param prefName
* The name of the preference to read.
* @return The value of the preference.
*/
function readEffectiveValue(config, prefName) {
if (!(prefName in UpdateUtils.PER_INSTALLATION_PREFS)) {
throw new Error(
`readEffectiveValue: Unknown per-installation pref '${prefName}'`
);
}
const pref = UpdateUtils.PER_INSTALLATION_PREFS[prefName];
const prefTypeFns = TYPE_SPECIFIC_PREF_FNS[pref.type];
if (prefName in config) {
if (prefTypeFns.isValid(config[prefName])) {
return config[prefName];
}
console.error(
`readEffectiveValue: Got invalid value for update config's` +
` '${prefName}' value: "${config[prefName]}"`
);
}
return readDefaultValue(config, prefName);
}
/**
* Reads the default branch pref out of the given configuration object. If one
* is not set, the default value from PER_INSTALLATION_PREFS will be returned.
*
* Values will be validated before being returned. Invalid values are ignored.
*
* @param config
* The configuration object to read.
* @param prefName
* The name of the preference to read.
* @return The value of the preference.
*/
function readDefaultValue(config, prefName) {
if (!(prefName in UpdateUtils.PER_INSTALLATION_PREFS)) {
throw new Error(
`readDefaultValue: Unknown per-installation pref '${prefName}'`
);
}
const pref = UpdateUtils.PER_INSTALLATION_PREFS[prefName];
const prefTypeFns = TYPE_SPECIFIC_PREF_FNS[pref.type];
if (PER_INSTALLATION_DEFAULTS_BRANCH in config) {
let defaults = config[PER_INSTALLATION_DEFAULTS_BRANCH];
if (prefName in defaults) {
if (prefTypeFns.isValid(defaults[prefName])) {
return defaults[prefName];
}
console.error(
`readEffectiveValue: Got invalid default value for update` +
` config's '${prefName}' value: "${defaults[prefName]}"`
);
}
}
return pref.defaultValue;
}
/**
* Reads the update config and, if necessary, performs migration of un-migrated
* values. We don't want to completely give up on update if this file is
* unavailable, so default values will be returned on failure rather than
* throwing an error.
*
* @return An Update Config object.
*/
async function readUpdateConfig() {
try {
let config = await IOUtils.readJSON(UpdateUtils.getConfigFilePath());
// We only migrate once. If we read something, the migration has already
// happened so we should make sure it doesn't happen again.
setUpdateConfigMigrationDone();
return config;
} catch (e) {
if (DOMException.isInstance(e) && e.name == "NotFoundError") {
if (updateConfigNeedsMigration()) {
const migrationConfig = makeMigrationUpdateConfig();
setUpdateConfigMigrationDone();
try {
await writeUpdateConfig(migrationConfig);
onMigrationSuccessful();
return migrationConfig;
} catch (e) {
console.error("readUpdateConfig: Migration failed: ", e);
}
}
} else {
// We only migrate once. If we got an error other than the file not
// existing, the migration has already happened so we should make sure
// it doesn't happen again.
setUpdateConfigMigrationDone();
console.error(
"readUpdateConfig: Unable to read app update configuration file. " +
"Exception: ",
e
);
}
return makeDefaultUpdateConfig();
}
}
/**
* Writes the given configuration to the disk.
*
* @param config
* The configuration object to write.
* @return The configuration object written.
* @throw A DOMException will be thrown on I/O error.
*/
async function writeUpdateConfig(config) {
let path = UpdateUtils.getConfigFilePath();
await IOUtils.writeJSON(path, config, { tmpPath: `${path}.tmp` });
return config;
}
var gUpdateConfigCache;
/**
* Notifies observers if any update config prefs have changed.
*
* @param config
* The most up-to-date config object.
* @return The same config object that was passed in.
*/
function maybeUpdateConfigChanged(config) {
if (!gUpdateConfigCache) {
// We don't want to generate a change notification for every pref on the
// first read of the session.
gUpdateConfigCache = config;
return config;
}
for (const [prefName, pref] of Object.entries(
UpdateUtils.PER_INSTALLATION_PREFS
)) {
let newPrefValue = readEffectiveValue(config, prefName);
let oldPrefValue = readEffectiveValue(gUpdateConfigCache, prefName);
if (newPrefValue != oldPrefValue) {
Services.obs.notifyObservers(
null,
pref.observerTopic,
newPrefValue.toString()
);
}
}
gUpdateConfigCache = config;
return config;
}
/**
* Note that this function sets up observers only, it does not do any I/O.
*/
UpdateUtils.initPerInstallPrefs();
/* Get the distribution pref values, from defaults only */
function getDistributionPrefValue(aPrefName) {
let value = Services.prefs
.getDefaultBranch(null)
.getCharPref(aPrefName, "default");
if (!value) {
value = "default";
}
return value;
}
function getSystemCapabilities() {
return "ISET:" + lazy.gInstructionSet + ",MEM:" + getMemoryMB();
}
/**
* Gets the RAM size in megabytes. This will round the value because sysinfo
* doesn't always provide RAM in multiples of 1024.
*/
function getMemoryMB() {
let memoryMB = "unknown";
try {
memoryMB = Services.sysinfo.getProperty("memsize");
if (memoryMB) {
memoryMB = Math.round(memoryMB / 1024 / 1024);
}
} catch (e) {
console.error("Error getting system info memsize property. Exception: ", e);
}
return memoryMB;
}
/**
* Gets the supported CPU instruction set.
*/
ChromeUtils.defineLazyGetter(lazy, "gInstructionSet", function aus_gIS() {
const CPU_EXTENSIONS = [
"hasSSE4_2",
"hasSSE4_1",
"hasSSE4A",
"hasSSSE3",
"hasSSE3",
"hasSSE2",
"hasSSE",
"hasMMX",
"hasNEON",
"hasARMv7",
"hasARMv6",
];
for (let ext of CPU_EXTENSIONS) {
if (Services.sysinfo.getProperty(ext)) {
return ext.substring(3);
}
}
return "unknown";
});
/* Windows only getter that returns the processor architecture. */
ChromeUtils.defineLazyGetter(lazy, "gWinCPUArch", function aus_gWinCPUArch() {
// Get processor architecture
let arch = "unknown";
const WORD = lazy.ctypes.uint16_t;
const DWORD = lazy.ctypes.uint32_t;
// This structure is described at:
// http://msdn.microsoft.com/en-us/library/ms724958%28v=vs.85%29.aspx
const SYSTEM_INFO = new lazy.ctypes.StructType("SYSTEM_INFO", [
{ wProcessorArchitecture: WORD },
{ wReserved: WORD },
{ dwPageSize: DWORD },
{ lpMinimumApplicationAddress: lazy.ctypes.voidptr_t },
{ lpMaximumApplicationAddress: lazy.ctypes.voidptr_t },
{ dwActiveProcessorMask: DWORD.ptr },
{ dwNumberOfProcessors: DWORD },
{ dwProcessorType: DWORD },
{ dwAllocationGranularity: DWORD },
{ wProcessorLevel: WORD },
{ wProcessorRevision: WORD },
]);
let kernel32 = false;
try {
kernel32 = lazy.ctypes.open("Kernel32");
} catch (e) {
console.error("Unable to open kernel32! Exception: ", e);
}
if (kernel32) {
try {
let GetNativeSystemInfo = kernel32.declare(
"GetNativeSystemInfo",
lazy.ctypes.winapi_abi,
lazy.ctypes.void_t,
SYSTEM_INFO.ptr
);
let winSystemInfo = SYSTEM_INFO();
// Default to unknown
winSystemInfo.wProcessorArchitecture = 0xffff;
GetNativeSystemInfo(winSystemInfo.address());
switch (winSystemInfo.wProcessorArchitecture) {
case 12:
arch = "aarch64";
break;
case 9:
arch = "x64";
break;
case 6:
arch = "IA64";
break;
case 0:
arch = "x86";
break;
}
} catch (e) {
console.error("Error getting processor architecture. Exception: ", e);
} finally {
kernel32.close();
}
}
return arch;
});
ChromeUtils.defineLazyGetter(UpdateUtils, "ABI", function () {
let abi = null;
try {
abi = Services.appinfo.XPCOMABI;
} catch (e) {
console.error("XPCOM ABI unknown");
}
if (AppConstants.platform == "win") {
// Windows build should report the CPU architecture that it's running on.
abi += "-" + lazy.gWinCPUArch;
}
if (AppConstants.ASAN) {
// Allow ASan builds to receive their own updates
abi += "-asan";
}
return abi;
});
ChromeUtils.defineLazyGetter(UpdateUtils, "OSVersion", function () {
let osVersion;
try {
osVersion =
Services.sysinfo.getProperty("name") +
" " +
Services.sysinfo.getProperty("version");
} catch (e) {
console.error("OS Version unknown.");
}
if (osVersion) {
if (AppConstants.platform == "win") {
// Add service pack and build number
try {
const { servicePackMajor, servicePackMinor, buildNumber } =
lazy.WindowsVersionInfo.get();
osVersion += `.${servicePackMajor}.${servicePackMinor}.${buildNumber}`;
} catch (err) {
console.error("Unable to retrieve windows version information: ", err);
osVersion += ".unknown";
}
// add UBR if on Windows 10
if (
Services.vc.compare(Services.sysinfo.getProperty("version"), "10") >= 0
) {
const WINDOWS_UBR_KEY_PATH =
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion";
let ubr = lazy.WindowsRegistry.readRegKey(
Ci.nsIWindowsRegKey.ROOT_KEY_LOCAL_MACHINE,
WINDOWS_UBR_KEY_PATH,
"UBR",
Ci.nsIWindowsRegKey.WOW64_64
);
if (ubr !== undefined) {
osVersion += `.${ubr}`;
} else {
osVersion += ".unknown";
}
}
// Add processor architecture
osVersion += " (" + lazy.gWinCPUArch + ")";
}
try {
osVersion +=
" (" + Services.sysinfo.getProperty("secondaryLibrary") + ")";
} catch (e) {
// Not all platforms have a secondary widget library, so an error is nothing to worry about.
}
osVersion = encodeURIComponent(osVersion);
}
return osVersion;
});
|