Skip to content

Commit

Permalink
Merge pull request #8 from brave/brave-plugin-shields-2
Browse files Browse the repository at this point in the history
Use contentSettings.plugin for shields with resource identifier
  • Loading branch information
bbondy committed Dec 19, 2017
2 parents c3cf030 + 9af4e81 commit 964216a
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 83 deletions.
5 changes: 1 addition & 4 deletions app/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ promisifyAll(chrome.storage, [
])

promisifyAll(chrome.contentSettings, [
'braveAdBlock',
'braveTrackingProtection',
'braveHTTPSEverywhere',
'javascript'
'plugins'
])

require('./background/events')
Expand Down
21 changes: 13 additions & 8 deletions app/background/api/shieldsAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import { Tab } from '../../types/state/shieldsPannelState'
import { BlockOptions } from '../../types/other/blockTypes'
import * as resourceIdentifiers from '../../constants/resourceIdentifiers'

/**
* Obtains the shields panel data for the specified tab data
Expand All @@ -20,10 +21,10 @@ export const getShieldSettingsForTabData = (tabData?: chrome.tabs.Tab) => {
const hostname = url.hostname

return Promise.all([
chrome.contentSettings.braveAdBlock.getAsync({ primaryUrl: origin }),
chrome.contentSettings.braveTrackingProtection.getAsync({ primaryUrl: origin }),
chrome.contentSettings.braveHTTPSEverywhere.getAsync({ primaryUrl: origin }),
chrome.contentSettings.javascript.getAsync({ primaryUrl: origin })
chrome.contentSettings.plugins.getAsync({ primaryUrl: origin, resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_AD_BLOCK } }),
chrome.contentSettings.plugins.getAsync({ primaryUrl: origin, resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_TRACKING_PROTECTION } }),
chrome.contentSettings.plugins.getAsync({ primaryUrl: origin, resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_HTTPS_EVERYWHERE } }),
chrome.contentSettings.plugins.getAsync({ primaryUrl: origin, resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_JAVASCRIPT_BLOCKING } })
]).then((details) => {
return {
url: url.href,
Expand Down Expand Up @@ -76,8 +77,9 @@ export const requestShieldPanelData = (tabId: number) =>
* @return a promise which resolves when the setting is set
*/
export const setAllowAdBlock = (origin: string, setting: string) =>
chrome.contentSettings.braveAdBlock.setAsync({
chrome.contentSettings.plugins.setAsync({
primaryPattern: origin + '/*',
resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_AD_BLOCK },
setting
})

Expand All @@ -88,8 +90,9 @@ export const setAllowAdBlock = (origin: string, setting: string) =>
* @return a promise which resolves with the setting is set
*/
export const setAllowTrackingProtection = (origin: string, setting: string) =>
chrome.contentSettings.braveTrackingProtection.setAsync({
chrome.contentSettings.plugins.setAsync({
primaryPattern: origin + '/*',
resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_TRACKING_PROTECTION },
setting
})

Expand All @@ -100,8 +103,9 @@ export const setAllowTrackingProtection = (origin: string, setting: string) =>
*/
export const setAllowHTTPSEverywhere = (origin: string, setting: BlockOptions) => {
const primaryPattern = origin.replace(/^(http|https):\/\//, '*://') + '/*'
return chrome.contentSettings.braveHTTPSEverywhere.setAsync({
return chrome.contentSettings.plugins.setAsync({
primaryPattern,
resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_HTTPS_EVERYWHERE },
setting
})
}
Expand All @@ -113,8 +117,9 @@ export const setAllowHTTPSEverywhere = (origin: string, setting: BlockOptions) =
* @return a promise which resolves when the setting is set
*/
export const setAllowJavaScript = (origin: string, setting: string) =>
chrome.contentSettings.javascript.setAsync({
chrome.contentSettings.plugins.setAsync({
primaryPattern: origin + '/*',
resourceIdentifier: { id: resourceIdentifiers.RESOURCE_IDENTIFIER_JAVASCRIPT_BLOCKING },
setting
})

Expand Down
8 changes: 8 additions & 0 deletions app/constants/resourceIdentifiers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */

export const RESOURCE_IDENTIFIER_AD_BLOCK = 'ad-block'
export const RESOURCE_IDENTIFIER_TRACKING_PROTECTION = 'tracking-protection'
export const RESOURCE_IDENTIFIER_HTTPS_EVERYWHERE = 'https-everywhere'
export const RESOURCE_IDENTIFIER_JAVASCRIPT_BLOCKING = 'javascript-blocking'
14 changes: 14 additions & 0 deletions app/types/constants/resourceIdentifiers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

import * as types from '../../constants/resourceIdentifiers'

export type RESOURCE_IDENTIFIER_AD_BLOCK = typeof types.RESOURCE_IDENTIFIER_AD_BLOCK
export type RESOURCE_IDENTIFIER_TRACKING_PROTECTION = typeof types.RESOURCE_IDENTIFIER_TRACKING_PROTECTION
export type RESOURCE_IDENTIFIER_HTTPS_EVERYWHERE = typeof types.RESOURCE_IDENTIFIER_HTTPS_EVERYWHERE
export type RESOURCE_IDENTIFIER_JAVASCRIPT_BLOCKING = typeof types.RESOURCE_IDENTIFIER_JAVASCRIPT_BLOCKING
28 changes: 0 additions & 28 deletions app/types/global/chrome.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,6 @@

type BlockTypes = 'adBlock' | 'trackingProtection' | 'httpsEverywhere' | 'javascript'

interface BraveAdBlock {
setAsync: any
getAsync: any
}

interface BraveTrackingProtection {
setAsync: any
getAsync: any
}

interface BraveHTTPSEverywhere {
setAsync: any
getAsync: any
}

interface BraveAdBlock {
setAsync: any
getAsync: any
}

interface BraveTrackingProtection {
getAsync: any
}

interface BlockDetails {
blockType: BlockTypes
tabId: number
Expand All @@ -45,10 +21,6 @@ declare namespace chrome.windows {
}

declare namespace chrome.contentSettings {
const braveAdBlock: BraveAdBlock
const braveTrackingProtection: BraveTrackingProtection
const braveHTTPSEverywhere: BraveHTTPSEverywhere

interface ContentSetting {
setAsync: any
getAsync: any
Expand Down
29 changes: 17 additions & 12 deletions test/app/background/api/shieldsAPITest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import actions from '../../../../app/background/actions/shieldsPanelActions'
import * as shieldsAPI from '../../../../app/background/api/shieldsAPI'
import { activeTabData } from '../../../testData'
import { Tab as TabType } from '../../../../app/types/state/shieldsPannelState'
import * as resourceIdentifiers from '../../../../app/constants/resourceIdentifiers'

describe('Shields API', () => {
describe('getShieldSettingsForTabData', function () {
Expand Down Expand Up @@ -111,20 +112,21 @@ describe('Shields API', () => {

describe('setAllowAdBlock', function () {
before(function () {
this.spy = sinon.spy(chrome.contentSettings.braveAdBlock, 'setAsync')
this.spy = sinon.spy(chrome.contentSettings.plugins, 'setAsync')
this.p = shieldsAPI.setAllowAdBlock('https://www.brave.com', 'block')
})
after(function () {
this.spy.restore()
})
it('calls chrome.contentSettings.braveAdBlock with the correct args', function () {
it('calls chrome.contentSettings.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_AD_BLOCK },
setting: 'block'
})
})
it('passes only 1 arg to chrome.contentSettings.braveAdBlock', function () {
it('passes only 1 arg to chrome.contentSettings.plugins', function () {
assert.equal(this.spy.getCall(0).args.length, 1)
})
it('resolves the returned promise', function (cb) {
Expand All @@ -138,20 +140,21 @@ describe('Shields API', () => {

describe('setAllowTrackingProtection', function () {
before(function () {
this.spy = sinon.spy(chrome.contentSettings.braveTrackingProtection, 'setAsync')
this.spy = sinon.spy(chrome.contentSettings.plugins, 'setAsync')
this.p = shieldsAPI.setAllowTrackingProtection('https://www.brave.com', 'block')
})
after(function () {
this.spy.restore()
})
it('calls chrome.contentSettings.braveTrackingProtection with the correct args', function () {
it('calls chrome.contentSettings.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_TRACKING_PROTECTION },
setting: 'block'
})
})
it('passes only 1 arg to chrome.contentSettings.braveTrackingProtection', function () {
it('passes only 1 arg to chrome.contentSettings.plugins', function () {
assert.equal(this.spy.getCall(0).args.length, 1)
})
it('resolves the returned promise', function (cb) {
Expand All @@ -165,20 +168,21 @@ describe('Shields API', () => {

describe('setAllowHTTPSEverywhere', function () {
before(function () {
this.spy = sinon.spy(chrome.contentSettings.braveHTTPSEverywhere, 'setAsync')
this.spy = sinon.spy(chrome.contentSettings.plugins, 'setAsync')
this.p = shieldsAPI.setAllowHTTPSEverywhere('https://www.brave.com', 'block')
})
after(function () {
this.spy.restore()
})
it('calls chrome.contentSettings.braveHTTPSEverywhere with the correct args', function () {
it('calls chrome.contentSettings.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_HTTPS_EVERYWHERE },
setting: 'block'
})
})
it('passes only 1 arg to chrome.contentSettings.braveHTTPSEverywhere', function () {
it('passes only 1 arg to chrome.contentSettings.plugins', function () {
assert.equal(this.spy.getCall(0).args.length, 1)
})
it('resolves the returned promise', function (cb) {
Expand All @@ -192,23 +196,24 @@ describe('Shields API', () => {

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

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

it('calls chrome.contentSettings.javascript with the correct args', function () {
it('calls chrome.contentSettings.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_JAVASCRIPT_BLOCKING },
setting: 'block'
})
})

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

Expand Down
32 changes: 1 addition & 31 deletions test/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,37 +95,7 @@ export const getMockChrome = () => {
onBlocked: new ChromeEvent()
},
contentSettings: {
braveAdBlock: {
setAsync: function () {
return Promise.resolve()
},
getAsync: function () {
return Promise.resolve({
setting: 'block'
})
}
},
braveTrackingProtection: {
setAsync: function () {
return Promise.resolve()
},
getAsync: function () {
return Promise.resolve({
setting: 'block'
})
}
},
braveHTTPSEverywhere: {
setAsync: function () {
return Promise.resolve()
},
getAsync: function () {
return Promise.resolve({
setting: 'block'
})
}
},
javascript: {
plugins: {
setAsync: function () {
return Promise.resolve()
},
Expand Down

0 comments on commit 964216a

Please sign in to comment.