Bug 1432890 - Refactor the Policies JSON code out of the Policies Manager object in order to support other policies sources.
MozReview-Commit-ID: 7uuIle0kEUL
--- a/browser/components/enterprisepolicies/EnterprisePolicies.js
+++ b/browser/components/enterprisepolicies/EnterprisePolicies.js
@@ -77,40 +77,49 @@ EnterprisePoliciesManager.prototype = {
_xpcom_factory: EnterprisePoliciesFactory,
_initialize() {
if (!Services.prefs.getBoolPref(PREF_ENABLED, false)) {
this.status = Ci.nsIEnterprisePolicies.INACTIVE;
return;
}
- this._file = new JSONFileReader(getConfigurationFile());
- this._file.readData();
+ let provider = this._chooseProvider();
- if (!this._file.exists) {
+ if (!provider) {
this.status = Ci.nsIEnterprisePolicies.INACTIVE;
return;
}
- if (this._file.failed) {
+ if (provider.failed) {
this.status = Ci.nsIEnterprisePolicies.FAILED;
return;
}
this.status = Ci.nsIEnterprisePolicies.ACTIVE;
- this._activatePolicies();
+ this._activatePolicies(provider.policies);
},
- _activatePolicies() {
- let { schema } = Cu.import("resource:///modules/policies/schema.jsm", {});
- let json = this._file.json;
+ _chooseProvider() {
+ // TODO: Bug 1433136 - Add GPO provider with higher precendence here
+
+ let jsonProvider = new JSONPoliciesProvider();
+ if (jsonProvider.hasPolicies) {
+ return jsonProvider;
+ }
- for (let policyName of Object.keys(json.policies)) {
+ return null;
+ },
+
+ _activatePolicies(unparsedPolicies) {
+ let { schema } = Cu.import("resource:///modules/policies/schema.jsm", {});
+
+ for (let policyName of Object.keys(unparsedPolicies)) {
let policySchema = schema.properties[policyName];
- let policyParameters = json.policies[policyName];
+ let policyParameters = unparsedPolicies[policyName];
if (!policySchema) {
log.error(`Unknown policy: ${policyName}`);
continue;
}
let [parametersAreValid, parsedParameters] =
PoliciesValidator.validateAndParseParameters(policyParameters,
@@ -270,89 +279,80 @@ EnterprisePoliciesManager.prototype = {
isAllowed: function BG_sanitize(feature) {
return !(feature in DisallowedFeatures);
},
};
let DisallowedFeatures = {};
-function JSONFileReader(file) {
- this._file = file;
- this._data = {
- exists: null,
- failed: false,
- json: null,
- };
-}
+
+/*
+ * JSON PROVIDER OF POLICIES
+ *
+ * This is a platform-agnostic provider which looks for
+ * policies specified through a policies.json file stored
+ * in the installation's distribution folder.
+ */
-JSONFileReader.prototype = {
- get exists() {
- if (this._data.exists === null) {
- this.readData();
- }
+class JSONPoliciesProvider {
+ constructor() {
+ this._policies = null;
+ this._failed = false;
+ this._readData();
+ }
- return this._data.exists;
- },
+ get hasPolicies() {
+ return this._policies !== null || this._failed;
+ }
+
+ get policies() {
+ return this._policies;
+ }
get failed() {
- return this._data.failed;
- },
+ return this._failed;
+ }
+
+ _getConfigurationFile() {
+ let configFile = Services.dirsvc.get("XREAppDist", Ci.nsIFile);
+ configFile.append(POLICIES_FILENAME);
+
+ let alternatePath = Services.prefs.getStringPref(PREF_ALTERNATE_PATH, "");
- get json() {
- if (this._data.failed) {
- return null;
- }
-
- if (this._data.json === null) {
- this.readData();
+ if (alternatePath && !configFile.exists()) {
+ // We only want to use the alternate file path if the file on the install
+ // folder doesn't exist. Otherwise it'd be possible for a user to override
+ // the admin-provided policies by changing the user-controlled prefs.
+ // This pref is only meant for tests, so it's fine to use this extra
+ // synchronous configFile.exists() above.
+ configFile = Cc["@mozilla.org/file/local;1"]
+ .createInstance(Ci.nsIFile);
+ configFile.initWithPath(alternatePath);
}
- return this._data.json;
- },
+ return configFile;
+ }
- readData() {
+ _readData() {
try {
- let data = Cu.readUTF8File(this._file);
+ let data = Cu.readUTF8File(this._getConfigurationFile());
if (data) {
- this._data.exists = true;
- this._data.json = JSON.parse(data);
- } else {
- this._data.exists = false;
+ this._policies = JSON.parse(data).policies;
}
} catch (ex) {
if (ex instanceof Components.Exception &&
ex.result == Cr.NS_ERROR_FILE_NOT_FOUND) {
- this._data.exists = false;
+ // Do nothing, _policies will remain null
} else if (ex instanceof SyntaxError) {
log.error("Error parsing JSON file");
- this._data.failed = true;
+ this._failed = true;
} else {
log.error("Error reading file");
- this._data.failed = true;
+ this._failed = true;
}
}
}
-};
-
-function getConfigurationFile() {
- let configFile = Services.dirsvc.get("XREAppDist", Ci.nsIFile);
- configFile.append(POLICIES_FILENAME);
-
- let prefType = Services.prefs.getPrefType(PREF_ALTERNATE_PATH);
+}
- if ((prefType == Services.prefs.PREF_STRING) && !configFile.exists()) {
- // We only want to use the alternate file path if the file on the install
- // folder doesn't exist. Otherwise it'd be possible for a user to override
- // the admin-provided policies by changing the user-controlled prefs.
- // This pref is only meant for tests, so it's fine to use this extra
- // synchronous configFile.exists() above.
- configFile = Cc["@mozilla.org/file/local;1"]
- .createInstance(Ci.nsIFile);
- let alternatePath = Services.prefs.getStringPref(PREF_ALTERNATE_PATH);
- configFile.initWithPath(alternatePath);
- }
-
- return configFile;
-}
var components = [EnterprisePoliciesManager];
this.NSGetFactory = XPCOMUtils.generateNSGetFactory(components);