Skip to content

Commit

Permalink
Merge pull request greatsuspender#966 from marcospgp/master
Browse files Browse the repository at this point in the history
Allow managed storage settings to be defined and override previous values (Closes greatsuspender#97)
  • Loading branch information
deanoemcke authored Jun 16, 2020
2 parents 394dcfd + d37201e commit 47e2cbe
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 5 deletions.
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,36 @@ The extension in crx format will be inside the build/crx/ directory. You can dra

This extension has a small external api to allow other extensions to request the suspension of a tab. See [this issue](https://github.com/greatsuspender/thegreatsuspender/issues/276) for more information. And please let me know about it so that I can try it out!

### Windows Group Policies

It is possible to force settings by defining group policies on Microsoft
Windows.

The whitelist is stored internally as a string, with one URL per line.

The following settings can be defined:

* `SCREEN_CAPTURE` (string, default: '0')
* `SCREEN_CAPTURE_FORCE` (boolean, default: false)
* `SUSPEND_IN_PLACE_OF_DISCARD` (boolean, default: false)
* `DISCARD_IN_PLACE_OF_SUSPEND` (boolean, default: false)
* `USE_ALT_SCREEN_CAPTURE_LIB` (boolean, default: false)
* `DISCARD_AFTER_SUSPEND` (boolean, default: false)
* `IGNORE_WHEN_OFFLINE` (boolean, default: false)
* `IGNORE_WHEN_CHARGING` (boolean, default: false)
* `UNSUSPEND_ON_FOCUS` (boolean, default: false)
* `IGNORE_PINNED` (boolean, default: true)
* `IGNORE_FORMS` (boolean, default: true)
* `IGNORE_AUDIO` (boolean, default: true)
* `IGNORE_ACTIVE_TABS` (boolean, default: true)
* `IGNORE_CACHE` (boolean, default: false)
* `ADD_CONTEXT` (boolean, default: true)
* `SYNC_SETTINGS` (boolean, default: true)
* `SUSPEND_TIME` (string (minutes), default: '60')
* `NO_NAG` (boolean, default: false)
* `WHITELIST` (string (one URL per line), default: '')
* `THEME` (string, default: 'light')

### Contributing to this extension

Contributions are very welcome. Feel free to submit pull requests for new features and bug fixes. For new features, ideally you would raise an issue for the proposed change first so that we can discuss ideas. This will go a long way to ensuring your pull request is accepted.
Expand All @@ -60,6 +90,6 @@ This work is licensed under a GNU GENERAL PUBLIC LICENSE (v2)

### Shoutouts

This package uses the [html2canvas](https://github.com/niklasvh/html2canvas) library written by Niklas von Hertzen.
It also uses the indexedDb wrapper [db.js](https://github.com/aaronpowell/db.js) written by Aaron Powell.
This package uses the [html2canvas](https://github.com/niklasvh/html2canvas) library written by Niklas von Hertzen.
It also uses the indexedDb wrapper [db.js](https://github.com/aaronpowell/db.js) written by Aaron Powell.
Thank you also to [BrowserStack](https://www.browserstack.com) for providing free chrome testing tools.
3 changes: 2 additions & 1 deletion src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@ var tgs = (function() {
) {
gsUtils.log(
'background',
`Notice target extension version: ${noticeTargetExtensionVersion}
`Notice target extension version: ${noticeTargetExtensionVersion}
does not match actual extension version: ${
chrome.runtime.getManifest().version
}`
Expand Down Expand Up @@ -1886,6 +1886,7 @@ var tgs = (function() {
Promise.resolve()
.then(tgs.backgroundScriptsReadyAsPromised) // wait until all gsLibs have loaded
.then(gsStorage.initSettingsAsPromised) // ensure settings have been loaded and synced
.then(gsStorage.checkManagedStorageAndOverride) // enforce managed settings
.then(() => {
// initialise other gsLibs
return Promise.all([
Expand Down
53 changes: 51 additions & 2 deletions src/js/gsStorage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/*global chrome, gsAnalytics, gsSession, localStorage, gsUtils */
'use strict';

var gsStorage = {
// Used to keep track of which settings were defined in the managed storage
const managedOptions = []; // Example: ["gsTheme, gsWhitelist"]

const gsStorageSettings = {
SCREEN_CAPTURE: 'screenCapture',
SCREEN_CAPTURE_FORCE: 'screenCaptureForce',
SUSPEND_IN_PLACE_OF_DISCARD: 'suspendInPlaceOfDiscard',
Expand All @@ -22,7 +25,11 @@ var gsStorage = {

DISCARD_AFTER_SUSPEND: 'discardAfterSuspend',
DISCARD_IN_PLACE_OF_SUSPEND: 'discardInPlaceOfSuspend',
USE_ALT_SCREEN_CAPTURE_LIB: 'useAlternateScreenCaptureLib',
USE_ALT_SCREEN_CAPTURE_LIB: 'useAlternateScreenCaptureLib'
};

var gsStorage = {
...gsStorageSettings,

APP_VERSION: 'gsVersion',
LAST_NOTICE: 'gsNotice',
Expand Down Expand Up @@ -164,6 +171,40 @@ var gsStorage = {
});
},

/**
* Checks the managed storage for settings and overrides the local storage
* Settings in managed storage are stored by key
* Settings in local storage are stored by name
* Example: in managed storage you will find "SYNC_SETTINGS": true.
* in local storage you will find "gsSyncSettings": true
* I did this because I think the key is easier to interpret for someone
* editing the managed storage manually.
*/
checkManagedStorageAndOverride() {
const settingsList = Object.keys(gsStorageSettings);
chrome.storage.managed.get(settingsList, result => {
const settings = gsStorage.getSettings();

Object.keys(result).forEach(key => {
if (key === "WHITELIST") {
settings[gsStorage[key]] = result[key].replace(/[\s\n]+/g, "\n");
} else {
settings[gsStorage[key]] = result[key];
}

// Mark option as managed
managedOptions.push(gsStorage[key]);
});

gsStorage.saveSettings(settings);
gsUtils.log(
'gsStorage',
'overrode settings with managed storage config:',
settings
);
});
},

// Listen for changes to synced settings
addSettingsSyncListener: function() {
chrome.storage.onChanged.addListener(function(remoteSettings, namespace) {
Expand Down Expand Up @@ -409,4 +450,12 @@ var gsStorage = {
);
}
},

/**
* Used by the options page to tell whether an option is set in managed storage
* and thus should not be changed.
*
* @param option The option name, such as "gsWhitelist" (not "WHITELIST")
*/
isOptionManaged: option => managedOptions.includes(option)
};
8 changes: 8 additions & 0 deletions src/js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
}
}

// Used to prevent options set in managed storage from being changed
function blockOption(element) {
element.setAttribute("disabled", "");
}

//populate settings from synced storage
function initSettings() {
var optionEls = document.getElementsByClassName('option'),
Expand All @@ -50,6 +55,9 @@
element = optionEls[i];
pref = elementPrefMap[element.id];
populateOption(element, gsStorage.getOption(pref));
if (gsStorage.isOptionManaged(pref)) {
blockOption(element);
}
}

setForceScreenCaptureVisibility(
Expand Down
65 changes: 65 additions & 0 deletions src/managed-storage-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"type": "object",
"properties": {
"SCREEN_CAPTURE": {
"type": "string"
},
"SCREEN_CAPTURE_FORCE": {
"type": "boolean"
},
"SUSPEND_IN_PLACE_OF_DISCARD": {
"type": "boolean"
},
"DISCARD_IN_PLACE_OF_SUSPEND": {
"type": "boolean"
},
"USE_ALT_SCREEN_CAPTURE_LIB": {
"type": "boolean"
},
"DISCARD_AFTER_SUSPEND": {
"type": "boolean"
},
"IGNORE_WHEN_OFFLINE": {
"type": "boolean"
},
"IGNORE_WHEN_CHARGING": {
"type": "boolean"
},
"UNSUSPEND_ON_FOCUS": {
"type": "boolean"
},
"IGNORE_PINNED": {
"type": "boolean"
},
"IGNORE_FORMS": {
"type": "boolean"
},
"IGNORE_AUDIO": {
"type": "boolean"
},
"IGNORE_ACTIVE_TABS": {
"type": "boolean"
},
"IGNORE_CACHE": {
"type": "boolean"
},
"ADD_CONTEXT": {
"type": "boolean"
},
"SYNC_SETTINGS": {
"type": "boolean"
},
"SUSPEND_TIME": {
"type": "string"
},
"NO_NAG": {
"type": "boolean"
},
"WHITELIST": {
"type": "string"
},
"THEME": {
"type": "string"
}
}
}
3 changes: 3 additions & 0 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"contextMenus",
"cookies"
],
"storage": {
"managed_schema": "managed-storage-schema.json"
},
"background": {
"scripts": ["js/gsUtils.js", "js/gsChrome.js", "js/gsAnalytics.js", "js/gsStorage.js", "js/db.js", "js/gsIndexedDb.js",
"js/gsMessages.js", "js/gsSession.js", "js/gsTabQueue.js", "js/gsTabCheckManager.js", "js/gsFavicon.js",
Expand Down

0 comments on commit 47e2cbe

Please sign in to comment.