Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide DDG newtab of tor window in qwant region(ger/fr) and qwant is set #657

Merged
merged 2 commits into from
Oct 17, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions browser/ui/webui/brave_new_tab_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
#include "brave/browser/search_engine_provider_util.h"
#include "brave/common/pref_names.h"
#include "brave/common/webui_url_constants.h"
#include "brave/components/search_engines/brave_prepopulated_engines.h"
#include "chrome/browser/profiles/profile.h"
#include "components/grit/brave_components_resources.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
#include "components/search_engines/template_url_data.h"
#include "components/search_engines/template_url_prepopulate_data.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_ui_data_source.h"
Expand Down Expand Up @@ -41,6 +44,17 @@ class NewTabDOMHandler : public content::WebUIMessageHandler {
DISALLOW_COPY_AND_ASSIGN(NewTabDOMHandler);
};

bool IsQwantUsedInQwantRegion(Profile* profile) {
bool region_for_qwant =
TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(
profile->GetPrefs())->prepopulate_id ==
TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_QWANT;
bool qwant_used =
profile->GetPrefs()->GetInteger(kAlternativeSearchEngineProviderInTor) ==
TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_QWANT;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qwant_used only returns true for Tor tabs with this logic; this should return true also for incognito windows

return region_for_qwant && qwant_used;
}

} // namespace

BraveNewTabUI::BraveNewTabUI(content::WebUI* web_ui, const std::string& name)
Expand All @@ -58,6 +72,8 @@ BraveNewTabUI::BraveNewTabUI(content::WebUI* web_ui, const std::string& name)
base::Bind(&BraveNewTabUI::OnPreferenceChanged, base::Unretained(this)));
pref_change_registrar_->Add(kUseAlternativeSearchEngineProvider,
base::Bind(&BraveNewTabUI::OnPreferenceChanged, base::Unretained(this)));
pref_change_registrar_->Add(kAlternativeSearchEngineProviderInTor,
base::Bind(&BraveNewTabUI::OnPreferenceChanged, base::Unretained(this)));

web_ui->AddMessageHandler(std::make_unique<NewTabDOMHandler>());
}
Expand Down Expand Up @@ -90,6 +106,8 @@ void BraveNewTabUI::CustomizeNewTabWebUIProperties(content::RenderViewHost* rend
: "false");
render_view_host->SetWebUIProperty(
"isTor", profile->IsTorProfile() ? "true" : "false");
render_view_host->SetWebUIProperty(
"isQwant", IsQwantUsedInQwantRegion(profile) ? "true" : "false");
}
}

Expand Down
1 change: 1 addition & 0 deletions components/brave_new_tab_ui/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class NewTabPage extends React.Component<Props, {}> {
<NewPrivateTab
useAlternativePrivateSearchEngine={this.useAlternativePrivateSearchEngine}
isTor={this.props.newTabData.isTor}
isQwant={this.props.newTabData.isQwant}
onChangePrivateSearchEngine={this.onChangePrivateSearchEngine}
/>
)
Expand Down
22 changes: 6 additions & 16 deletions components/brave_new_tab_ui/components/privateTab/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,22 @@ import TorTab from './torTab'

interface Props {
isTor: boolean
isQwant: boolean
useAlternativePrivateSearchEngine: boolean
onChangePrivateSearchEngine: (e: React.ChangeEvent<HTMLInputElement>) => void
}

export default class NewPrivateTab extends React.PureComponent<Props, {}> {
get isQwant () {
// This is not technically accurately describing whether
// the browser has been automatically set to a Qwant region.
// Temporarily we are detecting language here, but we should
// use the same setting logic as used during first-run.
// https://github.com/brave/brave-browser/issues/1632
return navigator.language &&
navigator.language.startsWith('de') ||
navigator.language.startsWith('fr')
}

get currentWindow () {
const { isTor, useAlternativePrivateSearchEngine, onChangePrivateSearchEngine } = this.props
const { isTor, isQwant, useAlternativePrivateSearchEngine, onChangePrivateSearchEngine } = this.props
if (isTor) {
if (this.isQwant) {
if (isQwant) {
return <QwantTorTab />
}
return <TorTab />
}

if (this.isQwant) {
if (isQwant) {
return <QwantTab />
}

Expand All @@ -53,9 +43,9 @@ export default class NewPrivateTab extends React.PureComponent<Props, {}> {
}

render () {
const { isTor } = this.props
const { isTor, isQwant } = this.props
return (
<Page isPrivate={!isTor && !this.isQwant}>
<Page isPrivate={!isTor && !isQwant}>
<PageWrapper>
{this.currentWindow}
</PageWrapper>
Expand Down
1 change: 1 addition & 0 deletions components/brave_new_tab_ui/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const defaultState: NewTab.State = {
isIncognito: chrome.extension.inIncognitoContext,
useAlternativePrivateSearchEngine: false,
isTor: chrome.getVariableValue('isTor') === 'true',
isQwant: chrome.getVariableValue('isQwant') === 'true',
bookmarks: {},
stats: {
adsBlockedStat: 0,
Expand Down
1 change: 1 addition & 0 deletions components/definitions/newTab.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ declare namespace NewTab {
isIncognito: boolean,
useAlternativePrivateSearchEngine: boolean,
isTor: boolean,
isQwant: boolean,
bookmarks: Record<string, Bookmark>,
stats: Stats
backgroundImage?: Image
Expand Down