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

Allow for assigning of default search engine based on country #15154

Merged
merged 5 commits into from
Sep 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
runtime = electron
target_arch = x64
brave_electron_version = 8.0.9
brave_electron_version = 8.0.10
chromedriver_version = 2.38
target = v8.0.9
target = v8.0.10
disturl=https://brave-laptop-binaries.s3.amazonaws.com/atom-shell/dist/
build_from_source = true
32 changes: 26 additions & 6 deletions app/browser/reducers/aboutNewTabReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ const { calculateTopSites } = require('../api/topSites')
const aboutNewTabReducer = (state, action) => {
switch (action.actionType) {
case appConstants.APP_SET_STATE:
// only show private search engine override options if needed
// (ex: not shown if a region specific default is provided)
const useAlternativePrivateSearchEngine = getSetting(settings.USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE, state.get('settings'))
state = aboutNewTabState.mergeDetails(state, {
newTabPageDetail: {
let newTabPageDetail = {}
if (getSetting(settings.SHOW_ALTERNATIVE_PRIVATE_SEARCH_ENGINE, state.get('settings'))) {
newTabPageDetail = {
useAlternativePrivateSearchEngine
}
}
state = aboutNewTabState.mergeDetails(state, {
newTabPageDetail: newTabPageDetail
})
break
case appConstants.APP_TOP_SITE_DATA_AVAILABLE:
Expand All @@ -30,11 +36,25 @@ const aboutNewTabReducer = (state, action) => {
}
break
case appConstants.APP_CHANGE_SETTING:
if (action.key === settings.USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE) {
state = aboutNewTabState.mergeDetails(state, {
newTabPageDetail: {
useAlternativePrivateSearchEngine: action.value
if (action.key === settings.USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE ||
action.key === settings.SHOW_ALTERNATIVE_PRIVATE_SEARCH_ENGINE) {
const showAlternativePrivateSearchEngines = action.key === settings.SHOW_ALTERNATIVE_PRIVATE_SEARCH_ENGINE
? action.value
: getSetting(settings.SHOW_ALTERNATIVE_PRIVATE_SEARCH_ENGINE, state.get('settings'))

const useAlternativePrivateSearchEngine = action.key === settings.USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE
? action.value
: getSetting(settings.USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE, state.get('settings'))

let newTabPageDetail = {}
if (showAlternativePrivateSearchEngines) {
newTabPageDetail = {
useAlternativePrivateSearchEngine
}
}

state = aboutNewTabState.mergeDetails(state, {
newTabPageDetail: newTabPageDetail
})
}
}
Expand Down
6 changes: 5 additions & 1 deletion app/renderer/reducers/urlBarReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ const updateSearchEngineInfoFromInput = (state, frameProps) => {
if (frameProps.get('isPrivate')) {
// handle private tab search with default search provider
const useAlternateDefaultPrivateSearchProvider =
getSetting(isTor(frameProps) ? settings.USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE_TOR : settings.USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE)
getSetting(settings.SHOW_ALTERNATIVE_PRIVATE_SEARCH_ENGINE)
? getSetting(isTor(frameProps)
? settings.USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE_TOR
: settings.USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE)
: false
if (useAlternateDefaultPrivateSearchProvider === true) {
// DuckDuckGo hard-coded as Private Tab default provider
// if asked to use a privacy-centric 'alternative'
Expand Down
43 changes: 34 additions & 9 deletions app/sessionStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -1016,15 +1016,40 @@ module.exports.runImportDefaultSettings = (data) => {
return data
}

module.exports.setDefaultSearchEngine = (immutableData) => {
const defaultLocale = locale.getDefaultLocale(true)
let defaultSearchEngine = config.defaultSearchEngineByLocale.default
const getCanonicalCountryName = () => {
const countryName = (app.getCountryName() || '').replace(/\0/g, '')
switch (countryName) {
case 'US':
case 'Vereinigte Staaten':
case 'en_US.UTF-8':
case 'es_US.UTF-8':
return 'USA'

case 'France':
case 'Frankreich':
case 'FR':
case 'fr_FR.UTF-8':
return 'France'

case 'Germany':
case 'Deutschland':
case 'Allemagne':
case 'DE':
case 'de_DE.UTF-8':
return 'Germany'
}
return countryName
}

for (let entry in config.defaultSearchEngineByLocale) {
if (entry === defaultLocale) {
defaultSearchEngine = config.defaultSearchEngineByLocale[entry]
break
}
module.exports.setDefaultSearchEngine = (immutableData) => {
let defaultSearchEngine = config.defaultSearchEngineByCountry.default
const countryName = getCanonicalCountryName()
const countrySpecificEntry = countryName && config.defaultSearchEngineByCountry[countryName]

if (countrySpecificEntry) {
defaultSearchEngine = countrySpecificEntry
// don't promote private tab engine override if region specific default search engine is set
immutableData = immutableData.setIn(['settings', settings.SHOW_ALTERNATIVE_PRIVATE_SEARCH_ENGINE], false)
}

return defaultSearchEngine
Expand Down Expand Up @@ -1098,13 +1123,13 @@ module.exports.loadAppState = () => {

locale.init(immutableData.getIn(['settings', settings.LANGUAGE])).then((lang) => {
immutableData = setVersionInformation(immutableData)
app.setLocale(lang)

// Set default search engine for locale (if not already set)
if (immutableData.getIn(['settings', settings.DEFAULT_SEARCH_ENGINE]) == null) {
immutableData = module.exports.setDefaultSearchEngine(immutableData)
}

app.setLocale(lang)
resolve(immutableData)
})
})
Expand Down
1 change: 1 addition & 0 deletions js/about/newprivatetab.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class NewPrivateTab extends React.Component {
}
{
isTor &&
this.props.newTabData.hasIn(useAlternativePrivateSearchEngineDataKeys) &&
<div className={css(styles.privateSearch)}>
<div className={css(styles.privateSearch__setting)}>
<p>
Expand Down
17 changes: 12 additions & 5 deletions js/about/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ class SearchTab extends ImmutableComponent {
}

hoverCallback (rows) {
if (!getSetting(settings.SHOW_ALTERNATIVE_PRIVATE_SEARCH_ENGINE, this.props.settings)) {
this.props.onChangeSetting(settings.SHOW_ALTERNATIVE_PRIVATE_SEARCH_ENGINE, true)
}
this.props.onChangeSetting(settings.DEFAULT_SEARCH_ENGINE, rows[1].value)
}

Expand All @@ -289,11 +292,15 @@ class SearchTab extends ImmutableComponent {
onClick={this.hoverCallback.bind(this)}
tableClassNames={css(styles.sortableTable_searchTab)}
/>
<SettingsList>
<DefaultSectionTitle data-l10n-id='privateTabsSearchSettingsTitle' />
<SettingCheckbox dataL10nId='useDuckDuckGoForPrivateSearch' prefKey={settings.USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE} settings={this.props.settings} onChangeSetting={this.props.onChangeSetting} />
<SettingCheckbox dataL10nId='useDuckDuckGoForPrivateSearchTor' prefKey={settings.USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE_TOR} settings={this.props.settings} onChangeSetting={this.props.onChangeSetting} />
</SettingsList>
{
getSetting(settings.SHOW_ALTERNATIVE_PRIVATE_SEARCH_ENGINE, this.props.settings)
? <SettingsList>
<DefaultSectionTitle data-l10n-id='privateTabsSearchSettingsTitle' />
<SettingCheckbox dataL10nId='useDuckDuckGoForPrivateSearch' prefKey={settings.USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE} settings={this.props.settings} onChangeSetting={this.props.onChangeSetting} />
<SettingCheckbox dataL10nId='useDuckDuckGoForPrivateSearchTor' prefKey={settings.USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE_TOR} settings={this.props.settings} onChangeSetting={this.props.onChangeSetting} />
</SettingsList>
: null
}
<DefaultSectionTitle data-l10n-id='locationBarSettings' />
<SettingsList>
<SettingCheckbox dataL10nId='showOpenedTabMatches' prefKey={settings.OPENED_TAB_SUGGESTIONS} settings={this.props.settings} onChangeSetting={this.props.onChangeSetting} />
Expand Down
1 change: 1 addition & 0 deletions js/constants/appConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ module.exports = {
'general.spellcheck-languages': Immutable.fromJS(['en-US']),
'search.default-search-engine': null,
'search.offer-search-suggestions': false, // false by default for privacy reasons
'search.show-alternate-private-search-engine': true,
'search.use-alternate-private-search-engine': false,
'search.use-alternate-private-search-engine-tor': true,
'tabs.switch-to-new-tabs': false,
Expand Down
8 changes: 4 additions & 4 deletions js/constants/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ module.exports = {
defaultSearchSuggestions: false,
maxHistorySites: 10
},
// NOTE: names here correspond to `name` field in:
// NOTE: values here correspond to `name` field in:
// js/data/searchProviders.js
defaultSearchEngineByLocale: {
defaultSearchEngineByCountry: {
// Example entries look like this:
// 'en-US': 'GitHub',
// 'es-MX': 'YouTube',
// 'Germany': 'YouTube',
// 'France': 'GitHub',
'default': 'Google'
},
defaultOpenSearchPath: 'content/search/google.xml',
Expand Down
1 change: 1 addition & 0 deletions js/constants/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const settings = {
SPELLCHECK_LANGUAGES: 'general.spellcheck-languages',
// Search tab
DEFAULT_SEARCH_ENGINE: 'search.default-search-engine',
SHOW_ALTERNATIVE_PRIVATE_SEARCH_ENGINE: 'search.show-alternate-private-search-engine',
USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE: 'search.use-alternate-private-search-engine',
USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE_TOR: 'search.use-alternate-private-search-engine-tor',
OFFER_SEARCH_SUGGESTIONS: 'search.offer-search-suggestions',
Expand Down
7 changes: 5 additions & 2 deletions js/contextMenus.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ const isLinux = platformUtil.isLinux()
*/
const getSearchUrl = (activeFrame, searchTerms) => {
const searchUrl = (
(getSetting(settings.USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE_TOR) && frameStateUtil.isTor(activeFrame)) ||
(getSetting(settings.USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE) && activeFrame.get('isPrivate'))
getSetting(settings.SHOW_ALTERNATIVE_PRIVATE_SEARCH_ENGINE) &&
(
(getSetting(settings.USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE_TOR) && frameStateUtil.isTor(activeFrame)) ||
(getSetting(settings.USE_ALTERNATIVE_PRIVATE_SEARCH_ENGINE) && activeFrame.get('isPrivate'))
)
)
? 'https://duckduckgo.com/?q={searchTerms}'
: appStoreRenderer.state.getIn(['searchDetail', 'searchURL'])
Expand Down
2 changes: 1 addition & 1 deletion js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const getDefaultSetting = (settingKey, settingsCollection) => {
return contributionDefaultAmount(settingKey, settingsCollection)
// >> locale is intialized (which determines default search engine)
case settings.DEFAULT_SEARCH_ENGINE:
return config.defaultSearchEngineByLocale.default
return config.defaultSearchEngineByCountry.default
}
return undefined
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/app/browser/reducers/aboutNewTabsReducerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('aboutNewTabReducerTest', function () {
})

describe('useAlternativePrivateSearchEngine', function () {
const initialState = Immutable.fromJS({
let initialState = Immutable.fromJS({
settings: { },
about: {
newtab: { pinnedTopSites: [] }
Expand Down
24 changes: 12 additions & 12 deletions test/unit/app/sessionStoreTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('sessionStore unit tests', function () {
}
}
const fakeConfig = {
defaultSearchEngineByLocale: {
defaultSearchEngineByCountry: {
default: 'MetaCrawler'
}
}
Expand Down Expand Up @@ -1987,26 +1987,26 @@ describe('sessionStore unit tests', function () {
})

describe('setDefaultSearchEngine', function () {
let getDefaultLocaleSpy
let getCountryNameSpy

beforeEach(function () {
getDefaultLocaleSpy = sinon.spy(fakeLocale, 'getDefaultLocale')
getCountryNameSpy = sinon.spy(fakeElectron.app, 'getCountryName')
})

afterEach(function () {
getDefaultLocaleSpy.restore()
if (fakeConfig.defaultSearchEngineByLocale['en-US']) {
delete fakeConfig.defaultSearchEngineByLocale['en-US']
getCountryNameSpy.restore()
if (fakeConfig.defaultSearchEngineByCountry['USA']) {
delete fakeConfig.defaultSearchEngineByCountry['USA']
}
if (!fakeConfig.defaultSearchEngineByLocale.default) {
fakeConfig.defaultSearchEngineByLocale.default = 'MetaCrawler'
if (!fakeConfig.defaultSearchEngineByCountry.default) {
fakeConfig.defaultSearchEngineByCountry.default = 'MetaCrawler'
}
})

it('calls getDefaultLocale with true', function () {
it('calls app.getCountryName', function () {
const input = Immutable.fromJS({settings: {}})
sessionStore.setDefaultSearchEngine(input)
assert(getDefaultLocaleSpy.calledOnce)
assert(getCountryNameSpy.calledOnce)
})

it('defaults to `default` entry', function () {
Expand All @@ -2016,14 +2016,14 @@ describe('sessionStore unit tests', function () {
})

it('matches a locale specific entry (if present)', function () {
fakeConfig.defaultSearchEngineByLocale['en-US'] = 'Yahoo'
fakeConfig.defaultSearchEngineByCountry['USA'] = 'Yahoo'
const input = Immutable.fromJS({settings: {}})
const output = sessionStore.setDefaultSearchEngine(input)
assert.equal(output.getIn(['settings', settings.DEFAULT_SEARCH_ENGINE]), 'Yahoo')
})

it('does not change input if there is no default in config', function () {
delete fakeConfig.defaultSearchEngineByLocale.default
delete fakeConfig.defaultSearchEngineByCountry.default
const input = Immutable.fromJS({settings: {}})
const output = sessionStore.setDefaultSearchEngine(input)
assert.deepEqual(input, output)
Expand Down
1 change: 1 addition & 0 deletions test/unit/lib/fakeElectron.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const fakeElectron = {
getPath: (param) => `${process.cwd()}/${param}`,
getVersion: () => '0.14.0',
setLocale: (locale) => {},
getCountryName: () => { return 'US' },
quit: () => {},
exit: () => {}
}),
Expand Down
2 changes: 1 addition & 1 deletion tools/cibuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import subprocess
import sys
import os.path
MUON_VERSION = '8.0.9'
MUON_VERSION = '8.0.10'
CHROMEDRIVER_VERSION = '2.38'
SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
TARGET_ARCH= os.environ['TARGET_ARCH'] if os.environ.has_key('TARGET_ARCH') else 'x64'
Expand Down