Skip to content
This repository has been archived by the owner on Jun 6, 2019. It is now read-only.

Commit

Permalink
Use braveShields's contentSetting api
Browse files Browse the repository at this point in the history
Use braveShields's contentSetting method instead of chrome.contentSetting.
This will enable user able to edit shields setting.
So far, user can't edit shields setting because chrome doesn't allow editing
configuration set by extension.
Our new methods stores configuration to user preference store instead of
extension store.
  • Loading branch information
simonhong committed Sep 20, 2018
1 parent 64f47d9 commit b43bcac
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 48 deletions.
2 changes: 1 addition & 1 deletion app/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ promisifyAll(chrome.storage, [
'local'
])

promisifyAll(chrome.contentSettings, [
promisifyAll(chrome.braveShields, [
'javascript',
'plugins'
])
Expand Down
38 changes: 19 additions & 19 deletions app/background/api/shieldsAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ export const getShieldSettingsForTabData = (tabData?: chrome.tabs.Tab) => {
const hostname = url.hostname

return Promise.all([
chrome.contentSettings.plugins.getAsync({ primaryUrl: origin, resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_BRAVE_SHIELDS } }),
chrome.contentSettings.plugins.getAsync({ primaryUrl: origin, resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_ADS } }),
chrome.contentSettings.plugins.getAsync({ primaryUrl: origin, resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_TRACKERS } }),
chrome.contentSettings.plugins.getAsync({ primaryUrl: origin, resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_HTTP_UPGRADABLE_RESOURCES } }),
chrome.contentSettings.javascript.getAsync({ primaryUrl: origin }),
chrome.contentSettings.plugins.getAsync({ primaryUrl: origin, resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_FINGERPRINTING } }),
chrome.contentSettings.plugins.getAsync({ primaryUrl: origin, secondaryUrl: 'https://firstParty/*', resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_FINGERPRINTING } }),
chrome.contentSettings.plugins.getAsync({ primaryUrl: origin, resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_COOKIES } }),
chrome.contentSettings.plugins.getAsync({ primaryUrl: origin, secondaryUrl: 'https://firstParty/', resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_COOKIES } })
chrome.braveShields.plugins.getAsync({ primaryUrl: origin, resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_BRAVE_SHIELDS } }),
chrome.braveShields.plugins.getAsync({ primaryUrl: origin, resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_ADS } }),
chrome.braveShields.plugins.getAsync({ primaryUrl: origin, resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_TRACKERS } }),
chrome.braveShields.plugins.getAsync({ primaryUrl: origin, resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_HTTP_UPGRADABLE_RESOURCES } }),
chrome.braveShields.javascript.getAsync({ primaryUrl: origin }),
chrome.braveShields.plugins.getAsync({ primaryUrl: origin, resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_FINGERPRINTING } }),
chrome.braveShields.plugins.getAsync({ primaryUrl: origin, secondaryUrl: 'https://firstParty/*', resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_FINGERPRINTING } }),
chrome.braveShields.plugins.getAsync({ primaryUrl: origin, resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_COOKIES } }),
chrome.braveShields.plugins.getAsync({ primaryUrl: origin, secondaryUrl: 'https://firstParty/', resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_COOKIES } })
]).then((details) => {
const fingerprinting = details[5].setting !== details[6].setting ? 'block_third_party' : details[5].setting
const cookies = details[7].setting !== details[8].setting ? 'block_third_party' : details[7].setting
Expand Down Expand Up @@ -90,7 +90,7 @@ export const requestShieldPanelData = (tabId: number) =>
* @return a promise which resolves when the setting is set
*/
export const setAllowBraveShields = (origin: string, setting: string) =>
chrome.contentSettings.plugins.setAsync({
chrome.braveShields.plugins.setAsync({
primaryPattern: origin.replace(/^(http|https):\/\//, '*://') + '/*',
resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_BRAVE_SHIELDS },
setting
Expand All @@ -104,7 +104,7 @@ export const setAllowBraveShields = (origin: string, setting: string) =>
* @return a promise which resolves when the setting is set
*/
export const setAllowAds = (origin: string, setting: string) =>
chrome.contentSettings.plugins.setAsync({
chrome.braveShields.plugins.setAsync({
primaryPattern: origin + '/*',
resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_ADS },
setting
Expand All @@ -118,7 +118,7 @@ export const setAllowAds = (origin: string, setting: string) =>
* @return a promise which resolves with the setting is set
*/
export const setAllowTrackers = (origin: string, setting: string) =>
chrome.contentSettings.plugins.setAsync({
chrome.braveShields.plugins.setAsync({
primaryPattern: origin + '/*',
resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_TRACKERS },
setting
Expand All @@ -132,7 +132,7 @@ export const setAllowTrackers = (origin: string, setting: string) =>
*/
export const setAllowHTTPUpgradableResources = (origin: string, setting: BlockOptions) => {
const primaryPattern = origin.replace(/^(http|https):\/\//, '*://') + '/*'
return chrome.contentSettings.plugins.setAsync({
return chrome.braveShields.plugins.setAsync({
primaryPattern,
resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_HTTP_UPGRADABLE_RESOURCES },
setting
Expand All @@ -146,7 +146,7 @@ export const setAllowHTTPUpgradableResources = (origin: string, setting: BlockOp
* @return a promise which resolves when the setting is set
*/
export const setAllowJavaScript = (origin: string, setting: string) =>
chrome.contentSettings.javascript.setAsync({
chrome.braveShields.javascript.setAsync({
primaryPattern: origin + '/*',
setting
})
Expand All @@ -161,13 +161,13 @@ export const setAllowFingerprinting = (origin: string, setting: string) => {
const originSetting = setting === 'allow' ? 'allow' : 'block'
const firstPartySetting = setting === 'block' ? 'block' : 'allow'

const p1 = chrome.contentSettings.plugins.setAsync({
const p1 = chrome.braveShields.plugins.setAsync({
primaryPattern: origin + '/*',
resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_FINGERPRINTING },
setting: originSetting
})

const p2 = chrome.contentSettings.plugins.setAsync({
const p2 = chrome.braveShields.plugins.setAsync({
primaryPattern: origin + '/*',
secondaryPattern: 'https://firstParty/*',
resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_FINGERPRINTING },
Expand All @@ -186,19 +186,19 @@ export const setAllowCookies = (origin: string, setting: string) => {
const originSetting = setting === 'allow' ? 'allow' : 'block'
const firstPartySetting = setting === 'block' ? 'block' : 'allow'

const p1 = chrome.contentSettings.plugins.setAsync({
const p1 = chrome.braveShields.plugins.setAsync({
primaryPattern: origin + '/*',
resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_REFERRERS },
setting: originSetting
})

const p2 = chrome.contentSettings.plugins.setAsync({
const p2 = chrome.braveShields.plugins.setAsync({
primaryPattern: origin + '/*',
resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_COOKIES },
setting: originSetting
})

const p3 = chrome.contentSettings.plugins.setAsync({
const p3 = chrome.braveShields.plugins.setAsync({
primaryPattern: origin + '/*',
secondaryPattern: 'https://firstParty/*',
resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_COOKIES },
Expand Down
9 changes: 2 additions & 7 deletions app/types/global/chrome.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,13 @@ declare namespace chrome.windows {
const getAllAsync: any
}

declare namespace chrome.contentSettings {
interface ContentSetting {
setAsync: any
getAsync: any
}
}

declare namespace chrome.braveShields {
const onBlocked: {
addListener: (callback: (detail: BlockDetails) => void) => void
emit: (detail: BlockDetails) => void
}

const allowScriptsOnce: any
const javascript: any
const plugins: any
}
36 changes: 18 additions & 18 deletions test/app/background/api/shieldsAPITest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,21 +187,21 @@ describe('Shields API', () => {

describe('setAllowAds', function () {
before(function () {
this.spy = sinon.spy(chrome.contentSettings.plugins, 'setAsync')
this.spy = sinon.spy(chrome.braveShields.plugins, 'setAsync')
this.p = shieldsAPI.setAllowAds('https://www.brave.com', 'block')
})
after(function () {
this.spy.restore()
})
it('calls chrome.contentSettings.plugins with the correct args', function () {
it('calls chrome.braveShields.plugins with the correct args', function () {
const arg0 = this.spy.getCall(0).args[0]
assert.deepEqual(arg0, {
primaryPattern: 'https://www.brave.com/*',
resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_ADS },
setting: 'block'
})
})
it('passes only 1 arg to chrome.contentSettings.plugins', function () {
it('passes only 1 arg to chrome.braveShields.plugins', function () {
assert.equal(this.spy.getCall(0).args.length, 1)
})
it('resolves the returned promise', function (cb) {
Expand All @@ -215,21 +215,21 @@ describe('Shields API', () => {

describe('setAllowTrackers', function () {
before(function () {
this.spy = sinon.spy(chrome.contentSettings.plugins, 'setAsync')
this.spy = sinon.spy(chrome.braveShields.plugins, 'setAsync')
this.p = shieldsAPI.setAllowTrackers('https://www.brave.com', 'block')
})
after(function () {
this.spy.restore()
})
it('calls chrome.contentSettings.plugins with the correct args', function () {
it('calls chrome.braveShields.plugins with the correct args', function () {
const arg0 = this.spy.getCall(0).args[0]
assert.deepEqual(arg0, {
primaryPattern: 'https://www.brave.com/*',
resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_TRACKERS },
setting: 'block'
})
})
it('passes only 1 arg to chrome.contentSettings.plugins', function () {
it('passes only 1 arg to chrome.braveShields.plugins', function () {
assert.equal(this.spy.getCall(0).args.length, 1)
})
it('resolves the returned promise', function (cb) {
Expand All @@ -243,21 +243,21 @@ describe('Shields API', () => {

describe('setAllowHTTPUpgradableResource', function () {
before(function () {
this.spy = sinon.spy(chrome.contentSettings.plugins, 'setAsync')
this.spy = sinon.spy(chrome.braveShields.plugins, 'setAsync')
this.p = shieldsAPI.setAllowHTTPUpgradableResources('https://www.brave.com', 'block')
})
after(function () {
this.spy.restore()
})
it('calls chrome.contentSettings.plugins with the correct args', function () {
it('calls chrome.braveShields.plugins with the correct args', function () {
const arg0 = this.spy.getCall(0).args[0]
assert.deepEqual(arg0, {
primaryPattern: '*://www.brave.com/*',
resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_HTTP_UPGRADABLE_RESOURCES },
setting: 'block'
})
})
it('passes only 1 arg to chrome.contentSettings.plugins', function () {
it('passes only 1 arg to chrome.braveShields.plugins', function () {
assert.equal(this.spy.getCall(0).args.length, 1)
})
it('resolves the returned promise', function (cb) {
Expand All @@ -271,23 +271,23 @@ describe('Shields API', () => {

describe('setAllowJavaScript', function () {
before(function () {
this.spy = sinon.spy(chrome.contentSettings.javascript, 'setAsync')
this.spy = sinon.spy(chrome.braveShields.javascript, 'setAsync')
this.p = shieldsAPI.setAllowJavaScript('https://www.brave.com', 'block')
})

after(function () {
this.spy.restore()
})

it('calls chrome.contentSettings.plugins with the correct args', function () {
it('calls chrome.braveShields.plugins with the correct args', function () {
const arg0 = this.spy.getCall(0).args[0]
assert.deepEqual(arg0, {
primaryPattern: 'https://www.brave.com/*',
setting: 'block'
})
})

it('passes only 1 arg to chrome.contentSettings.plugins', function () {
it('passes only 1 arg to chrome.braveShields.plugins', function () {
assert.equal(this.spy.getCall(0).args.length, 1)
})

Expand All @@ -302,13 +302,13 @@ describe('Shields API', () => {

describe('setAllowFingerprinting', function () {
before(function () {
this.spy = sinon.spy(chrome.contentSettings.plugins, 'setAsync')
this.spy = sinon.spy(chrome.braveShields.plugins, 'setAsync')
this.p = shieldsAPI.setAllowFingerprinting('https://www.brave.com', 'block')
})
after(function () {
this.spy.restore()
})
it('calls chrome.contentSettings.plugins with the correct args', function () {
it('calls chrome.braveShields.plugins with the correct args', function () {
const arg0 = this.spy.getCall(0).args[0]
assert.deepEqual(arg0, {
primaryPattern: 'https://www.brave.com/*',
Expand All @@ -323,7 +323,7 @@ describe('Shields API', () => {
setting: 'block'
})
})
it('passes only 1 arg to chrome.contentSettings.plugins', function () {
it('passes only 1 arg to chrome.braveShields.plugins', function () {
assert.equal(this.spy.getCall(0).args.length, 1)
assert.equal(this.spy.getCall(1).args.length, 1)
})
Expand All @@ -340,13 +340,13 @@ describe('Shields API', () => {

describe('setAllowCookies', function () {
before(function () {
this.spy = sinon.spy(chrome.contentSettings.plugins, 'setAsync')
this.spy = sinon.spy(chrome.braveShields.plugins, 'setAsync')
this.p = shieldsAPI.setAllowCookies('https://www.brave.com', 'block')
})
after(function () {
this.spy.restore()
})
it('calls chrome.contentSettings.plugins with the correct args', function () {
it('calls chrome.braveShields.plugins with the correct args', function () {
const arg0 = this.spy.getCall(0).args[0]
assert.deepEqual(arg0, {
primaryPattern: 'https://www.brave.com/*',
Expand All @@ -367,7 +367,7 @@ describe('Shields API', () => {
setting: 'block'
})
})
it('passes only 1 arg to chrome.contentSettings.plugins', function () {
it('passes only 1 arg to chrome.braveShields.plugins', function () {
assert.equal(this.spy.getCall(0).args.length, 1)
assert.equal(this.spy.getCall(1).args.length, 1)
})
Expand Down
4 changes: 1 addition & 3 deletions test/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ export const getMockChrome = () => {
onBlocked: new ChromeEvent(),
allowScriptsOnce: function (origins: Array<string>, tabId: number, cb: () => void) {
setImmediate(cb)
}
},
contentSettings: {
},
plugins: {
setAsync: function () {
return Promise.resolve()
Expand Down

0 comments on commit b43bcac

Please sign in to comment.