Skip to content

Commit

Permalink
Merge 18079f2 into 4db9484
Browse files Browse the repository at this point in the history
  • Loading branch information
imurchie committed Aug 9, 2018
2 parents 4db9484 + 18079f2 commit 65583d0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 38 deletions.
81 changes: 47 additions & 34 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,32 @@ async function plistPaths (sim, identifier) {
}

async function updateSettings (sim, plist, updates) {
let paths = await plistPaths(sim, plist);
for (let path of paths) {
await update(path, updates);
let updated = false;
for (let path of await plistPaths(sim, plist)) {
updated = await update(path, updates) || updated;
}
return updated;
}

// update a plist file, located at pathToPlist
// pass in an object, all settings specified in the object will be
// updated on the plist, all others left as-is
async function update (pathToPlist, updates) {
let currentSettings = await read(pathToPlist);
let newSettings = _.merge(currentSettings, updates);
await plist.updatePlistFile(pathToPlist, newSettings, true, false);
const currentSettings = await read(pathToPlist);
const newSettings = Object.assign({}, currentSettings, updates);

if (_.isEqual(currentSettings, newSettings)) {
// no setting changes, so do nothing
return false;
}

return newSettings;
await plist.updatePlistFile(pathToPlist, newSettings, true, false);
return true;
}

async function readSettings (sim, plist) {
let settings = {};
let paths = await plistPaths(sim, plist);
for (let path of paths) {
for (let path of await plistPaths(sim, plist)) {
settings[path] = await read(path);
}
return settings;
Expand All @@ -92,29 +97,29 @@ async function read (pathToPlist) {

async function updateLocationSettings (sim, bundleId, authorized) {
// update location cache
let newCachePrefs = {
const newCachePrefs = {
LastFenceActivityTimestamp: 412122103.232983,
CleanShutdown: true
};
await updateSettings(sim, 'locationCache', {[bundleId]: newCachePrefs});
let updated = await updateSettings(sim, 'locationCache', {[bundleId]: newCachePrefs});

// update location clients
let newClientPrefs = {
const newClientPrefs = {
BundleId: bundleId,
Authorized: !!authorized,
Whitelisted: false,
};
for (let file of await plistPaths(sim, 'locationClients')) {
for (const file of await plistPaths(sim, 'locationClients')) {
log.debug(`Updating location client file: ${file}`);

let updates = {};

// see if the bundle is already there
let plist = await read(file);
const plist = await read(file);

// random data that always seems to be in the clients.plist
let weirdLocKey = 'com.apple.locationd.bundle-/System/Library/' +
'PrivateFrameworks/AOSNotification.framework';
const weirdLocKey = 'com.apple.locationd.bundle-/System/Library/' +
'PrivateFrameworks/AOSNotification.framework';
if (!_.has(plist, weirdLocKey)) {
updates[weirdLocKey] = {
BundlePath: '/System/Library/PrivateFrameworks/AOSNotification.framework',
Expand All @@ -125,13 +130,15 @@ async function updateLocationSettings (sim, bundleId, authorized) {
}

// create the update, and make sure it has sensible values
let baseSetting = _.has(plist, bundleId) ? plist[bundleId] : {};
const baseSetting = _.has(plist, bundleId) ? plist[bundleId] : {};
updates[bundleId] = _.defaults(newClientPrefs, baseSetting);
updates[bundleId].Executable = updates[bundleId].Executable || '';
updates[bundleId].Registered = updates[bundleId].Registered || '';

await update(file, updates);
updated = await update(file, updates) || updated;
}

return updated;
}

async function setReduceMotion (sim, reduceMotion = true) {
Expand All @@ -144,6 +151,8 @@ async function setReduceMotion (sim, reduceMotion = true) {
}

async function updateSafariUserSettings (sim, settingSet) {
log.debug('Updating Safari user settings');

// add extra stuff to UserSettings.plist and EffectiveUserSettings.plist
let newUserSettings = {};
if (_.has(settingSet, 'WebKitJavaScriptEnabled')) {
Expand All @@ -155,25 +164,29 @@ async function updateSafariUserSettings (sim, settingSet) {
if (_.has(settingSet, 'WarnAboutFraudulentWebsites')) {
newUserSettings.safariForceFraudWarning = !settingSet.WarnAboutFraudulentWebsites;
}
if (_.size(newUserSettings) > 0) {
log.debug('Updating Safari user settings');
let curUserSettings = await readSettings(sim, 'userSettings');
for (let [file, userSettingSet] of _.toPairs(curUserSettings)) {
// the user settings plist has two buckets, one for
// boolean settings (`restrictedBool`) and one for
// other value settings (`restrictedValue`). In each, the value
// is in a `value` sub-field.
if (!_.has(userSettingSet, 'restrictedBool')) {
userSettingSet.restrictedBool = {};
}
for (let [key, value] of _.toPairs(newUserSettings)) {
userSettingSet.restrictedBool[key] = {value};
}

// actually do the update
await update(file, userSettingSet);
if (_.size(newUserSettings) === 0) {
return false;
}

let updated = false;
for (const [file, userSettingSet] of _.toPairs(await readSettings(sim, 'userSettings'))) {
// the user settings plist has two buckets, one for
// boolean settings (`restrictedBool`) and one for
// other value settings (`restrictedValue`). In each, the value
// is in a `value` sub-field.
if (!_.has(userSettingSet, 'restrictedBool')) {
userSettingSet.restrictedBool = {};
}
for (let [key, value] of _.toPairs(newUserSettings)) {
userSettingSet.restrictedBool[key] = {value};
}

// actually do the update
updated = await update(file, userSettingSet) || updated;
}

return updated;
}

async function updateLocale (sim, language, locale, calendarFormat) {
Expand Down
5 changes: 3 additions & 2 deletions lib/simulator-xcode-6.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,8 +630,9 @@ class SimulatorXcode6 extends EventEmitter {
* @param {object} updates - The hash of key/value pairs to update for Safari.
*/
async updateSafariSettings (updates) {
await settings.updateSafariUserSettings(this, updates);
await settings.updateSettings(this, 'mobileSafari', updates);
let updated = await settings.updateSafariUserSettings(this, updates);
updated = await settings.updateSettings(this, 'mobileSafari', updates) || updated;
return updated;
}

/**
Expand Down
17 changes: 15 additions & 2 deletions test/unit/settings-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('settings', function () {
});

describe('general plist handling', function () {
const plist = path.resolve('test/assets/sample.plist');
const plist = path.resolve('test', 'assets', 'sample.plist');
const expectedField = 'com.apple.locationd.bundle-/System/Library/PrivateFrameworks/Parsec.framework';
let tmpPlist;

Expand All @@ -51,7 +51,8 @@ describe('settings', function () {
let originalData = await settings.read(tmpPlist);
originalData[expectedField]
.Whitelisted = true;
await settings.update(tmpPlist, originalData);
let updated = await settings.update(tmpPlist, originalData);
updated.should.be.true;
let updatedData = await settings.read(tmpPlist);

updatedData[expectedField]
Expand All @@ -60,6 +61,18 @@ describe('settings', function () {
originalData.should.eql(updatedData);
});

it('should return false when no changes are made', async function () {
let originalData = await settings.read(tmpPlist);
let updated = await settings.update(tmpPlist, originalData);
updated.should.be.false;
let updatedData = await settings.read(tmpPlist);

updatedData[expectedField]
.Whitelisted.should.be.false;

originalData.should.eql(updatedData);
});

it('should read a plist', async function () {
let data = await settings.read(tmpPlist);
data[expectedField]
Expand Down

0 comments on commit 65583d0

Please sign in to comment.