Bug 1432890 - Refactor the Policies JSON code out of the Policies Manager object in order to support other policies sources. r=Mossop

MozReview-Commit-ID: L6SIgHpp3Yh

--HG--
extra : source : ee868e1ec63606fed44913682e18c8d016ccc5f3
This commit is contained in:
Felipe Gomes 2018-02-06 00:28:59 -02:00
parent fb0b064981
commit 3c92926263

View File

@ -82,30 +82,39 @@ EnterprisePoliciesManager.prototype = {
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 } = ChromeUtils.import("resource:///modules/policies/schema.jsm", {});
let json = this._file.json;
_chooseProvider() {
// TODO: Bug 1433136 - Add GPO provider with higher precendence here
for (let policyName of Object.keys(json.policies)) {
let jsonProvider = new JSONPoliciesProvider();
if (jsonProvider.hasPolicies) {
return jsonProvider;
}
return null;
},
_activatePolicies(unparsedPolicies) {
let { schema } = ChromeUtils.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}`);
@ -275,84 +284,75 @@ EnterprisePoliciesManager.prototype = {
let DisallowedFeatures = {};
function JSONFileReader(file) {
this._file = file;
this._data = {
exists: null,
failed: false,
json: null,
};
}
JSONFileReader.prototype = {
get exists() {
if (this._data.exists === null) {
this.readData();
}
/*
* 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.
*/
return this._data.exists;
},
class JSONPoliciesProvider {
constructor() {
this._policies = null;
this._failed = false;
this._readData();
}
get hasPolicies() {
return this._policies !== null || this._failed;
}
get policies() {
return this._policies;
}
get failed() {
return this._data.failed;
},
return this._failed;
}
get json() {
if (this._data.failed) {
return null;
_getConfigurationFile() {
let configFile = Services.dirsvc.get("XREAppDist", Ci.nsIFile);
configFile.append(POLICIES_FILENAME);
let alternatePath = Services.prefs.getStringPref(PREF_ALTERNATE_PATH, "");
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);
}
if (this._data.json === null) {
this.readData();
}
return configFile;
}
return this._data.json;
},
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);