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

WIP - Search default updates #27758

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,8 @@ public void onUrlFocusChange(boolean hasFocus) {
&& UrlUtilities.isNtpUrl(braveActivity.getActivityTab().getUrl().getSpec())
&& !OnboardingPrefManager.getInstance().hasSearchEngineOnboardingShown()
&& OnboardingPrefManager.getInstance().getUrlFocusCount() == 1
&& !BRAVE_SEARCH_ENGINE_DEFAULT_REGIONS.contains(countryCode)) {
&& !BRAVE_SEARCH_ENGINE_DEFAULT_REGIONS.contains(countryCode)
&& !countryCode.equals("JP")) {
Intent searchActivityIntent = new Intent(context, SearchActivity.class);
searchActivityIntent.setAction(Intent.ACTION_VIEW);
context.startActivity(searchActivityIntent);
Expand Down
13 changes: 13 additions & 0 deletions browser/ui/webui/new_tab_page/brave_new_tab_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "brave/components/brave_news/browser/brave_news_controller.h"
#include "brave/components/brave_news/common/features.h"
#include "brave/components/constants/webui_url_constants.h"
#include "brave/components/l10n/common/country_code_util.h"
#include "brave/components/l10n/common/localization_util.h"
#include "brave/components/misc_metrics/new_tab_metrics.h"
#include "brave/components/ntp_background_images/browser/ntp_custom_images_source.h"
Expand Down Expand Up @@ -54,6 +55,17 @@

using ntp_background_images::NTPCustomImagesSource;

namespace {

std::string GetDefaultSearchWidgetHost(PrefService* local_state) {
if (brave_l10n::GetCountryCode(local_state) == "JP") {
return "search.yahoo.co.jp";
}
return "search.brave.com";
}

} // namespace

BraveNewTabUI::BraveNewTabUI(content::WebUI* web_ui,
const std::string& name,
brave_ads::AdsService* ads_service,
Expand Down Expand Up @@ -121,6 +133,7 @@ BraveNewTabUI::BraveNewTabUI(content::WebUI* web_ui,
source->AddBoolean(
"featureFlagSearchWidget",
base::FeatureList::IsEnabled(features::kBraveNtpSearchWidget));
source->AddString("searchWidgetDefaultHost", GetDefaultSearchWidgetHost(local_state));

source->AddBoolean("vpnWidgetSupported",
#if BUILDFLAG(ENABLE_BRAVE_VPN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

// IMPORTANT! If you make changes to any of the search engine mappings below,
// it's critical to also increment the value `kBraveCurrentDataVersion` in
// `//brave/components/search_engines/brave_prepopulated_engines.cc`.
// `//brave/components/search_engines/brave_prepopulated_engines.h`.

namespace TemplateURLPrepopulateData {

Expand Down Expand Up @@ -97,6 +97,13 @@ constexpr BravePrepopulatedEngineID kBraveEnginesAUIE[] = {
PREPOPULATED_ENGINE_ID_ECOSIA,
};

constexpr BravePrepopulatedEngineID kBraveEnginesJP[] = {
PREPOPULATED_ENGINE_ID_YAHOO_JP, PREPOPULATED_ENGINE_ID_BRAVE,
PREPOPULATED_ENGINE_ID_GOOGLE, PREPOPULATED_ENGINE_ID_DUCKDUCKGO,
PREPOPULATED_ENGINE_ID_QWANT, PREPOPULATED_ENGINE_ID_BING,
PREPOPULATED_ENGINE_ID_STARTPAGE,
};

constexpr BravePrepopulatedEngineID kBraveEnginesKR[] = {
PREPOPULATED_ENGINE_ID_BRAVE,
PREPOPULATED_ENGINE_ID_NAVER,
Expand Down Expand Up @@ -146,6 +153,7 @@ constexpr auto kDefaultEnginesByCountryIdMap =
{country_codes::CountryCharsToCountryID('I', 'E'), kBraveEnginesAUIE},
{country_codes::CountryCharsToCountryID('I', 'T'),
kBraveEnginesWithEcosia},
{country_codes::CountryCharsToCountryID('J', 'P'), kBraveEnginesJP},
{country_codes::CountryCharsToCountryID('K', 'G'),
kBraveEnginesWithYandex},
{country_codes::CountryCharsToCountryID('K', 'R'), kBraveEnginesKR},
Expand Down Expand Up @@ -574,7 +582,68 @@ BravePrepopulatedEngineID GetDefaultSearchEngine(int country_id, int version) {
PREPOPULATED_ENGINE_ID_YANDEX},
});

if (version > 29) {
// Updated default for JP.
static constexpr auto kContentV31 =
base::MakeFixedFlatMap<int, BravePrepopulatedEngineID>({
{country_codes::CountryCharsToCountryID('A', 'M'),
PREPOPULATED_ENGINE_ID_YANDEX},
{country_codes::CountryCharsToCountryID('A', 'R'),
PREPOPULATED_ENGINE_ID_BRAVE},
{country_codes::CountryCharsToCountryID('A', 'T'),
PREPOPULATED_ENGINE_ID_BRAVE},
{country_codes::CountryCharsToCountryID('A', 'U'),
PREPOPULATED_ENGINE_ID_BRAVE},
{country_codes::CountryCharsToCountryID('A', 'Z'),
PREPOPULATED_ENGINE_ID_YANDEX},
{country_codes::CountryCharsToCountryID('B', 'R'),
PREPOPULATED_ENGINE_ID_BRAVE},
{country_codes::CountryCharsToCountryID('B', 'Y'),
PREPOPULATED_ENGINE_ID_YANDEX},
{country_codes::CountryCharsToCountryID('C', 'A'),
PREPOPULATED_ENGINE_ID_BRAVE},
{country_codes::CountryCharsToCountryID('D', 'E'),
PREPOPULATED_ENGINE_ID_BRAVE},
{country_codes::CountryCharsToCountryID('E', 'S'),
PREPOPULATED_ENGINE_ID_BRAVE},
{country_codes::CountryCharsToCountryID('F', 'R'),
PREPOPULATED_ENGINE_ID_BRAVE},
{country_codes::CountryCharsToCountryID('G', 'B'),
PREPOPULATED_ENGINE_ID_BRAVE},
{country_codes::CountryCharsToCountryID('I', 'N'),
PREPOPULATED_ENGINE_ID_BRAVE},
{country_codes::CountryCharsToCountryID('I', 'T'),
PREPOPULATED_ENGINE_ID_BRAVE},
{country_codes::CountryCharsToCountryID('J', 'P'),
PREPOPULATED_ENGINE_ID_YAHOO_JP},
{country_codes::CountryCharsToCountryID('K', 'G'),
PREPOPULATED_ENGINE_ID_YANDEX},
{country_codes::CountryCharsToCountryID('K', 'R'),
PREPOPULATED_ENGINE_ID_NAVER},
{country_codes::CountryCharsToCountryID('K', 'Z'),
PREPOPULATED_ENGINE_ID_YANDEX},
{country_codes::CountryCharsToCountryID('M', 'D'),
PREPOPULATED_ENGINE_ID_YANDEX},
{country_codes::CountryCharsToCountryID('M', 'X'),
PREPOPULATED_ENGINE_ID_BRAVE},
{country_codes::CountryCharsToCountryID('R', 'U'),
PREPOPULATED_ENGINE_ID_YANDEX},
{country_codes::CountryCharsToCountryID('T', 'J'),
PREPOPULATED_ENGINE_ID_YANDEX},
{country_codes::CountryCharsToCountryID('T', 'M'),
PREPOPULATED_ENGINE_ID_YANDEX},
{country_codes::CountryCharsToCountryID('U', 'S'),
PREPOPULATED_ENGINE_ID_BRAVE},
{country_codes::CountryCharsToCountryID('U', 'Z'),
PREPOPULATED_ENGINE_ID_YANDEX},
});

if (version > 30) {
const auto it = kContentV31.find(country_id);
if (it == kContentV31.end()) {
return default_v6;
}
return it->second;
} else if (version > 29) {
const auto it = kContentV30.find(country_id);
if (it == kContentV30.end()) {
return default_v6;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { SearchEngineInfo } from '../../api/background'
import { useNewTabPref } from '../../hooks/usePref'
import * as React from 'react';
import { ENABLED_SEARCH_ENGINES_KEY, LAST_SEARCH_ENGINE_KEY, braveSearchHost } from './config';
import { ENABLED_SEARCH_ENGINES_KEY, LAST_SEARCH_ENGINE_KEY, defaultSearchHost } from './config';

interface Engine {
setLastSearchEngine: (engine: SearchEngineInfo) => void
Expand All @@ -16,7 +16,7 @@ interface Engine {

const EngineContext = React.createContext<Engine>({
setLastSearchEngine: () => { },
lastSearchEngine: braveSearchHost,
lastSearchEngine: defaultSearchHost,
engineConfig: {},
setEngineConfig: () => { },
})
Expand All @@ -28,7 +28,7 @@ const searchEngineConfig = () => {
}
return {
// Default to enabling Brave Search
[braveSearchHost]: true
[defaultSearchHost]: true
};
}

Expand Down Expand Up @@ -56,7 +56,7 @@ export function EngineContextProvider(props: React.PropsWithChildren<{}>) {
// Note: The key for `Google` is the empty string which is falsey so we need
// to check for undefined here.
if (last === undefined || !config[last]) {
return Object.keys(config).find(key => config[key]) ?? braveSearchHost
return Object.keys(config).find(key => config[key]) ?? defaultSearchHost
}

return last
Expand Down
4 changes: 2 additions & 2 deletions components/brave_new_tab_ui/components/search/SearchBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as React from 'react';
import styled from 'styled-components';
import EnginePicker from './EnginePicker';
import { useSearchContext } from './SearchContext';
import { braveSearchHost, searchBoxRadius } from './config';
import { defaultSearchHost, searchBoxRadius } from './config';

const searchBoxClass = 'ntp-search-box'

Expand Down Expand Up @@ -48,7 +48,7 @@ const Container = styled.div`

export default function SearchBox() {
const { searchEngine, query, setQuery } = useSearchContext()
const placeholderText = searchEngine?.host === braveSearchHost
const placeholderText = searchEngine?.host === defaultSearchHost
? getLocale('searchBravePlaceholder')
: getLocale('searchNonBravePlaceholder')
const searchInput = React.useRef<HTMLElement>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import styled from 'styled-components'
import SearchResult from './SearchResult'
import getNTPBrowserAPI, { SearchEngineInfo } from '../../api/background'
import { omniboxController, search, useSearchContext } from './SearchContext'
import { braveSearchHost } from './config'
import { defaultSearchHost } from './config'
import { stringToMojoString16 } from 'gen/ui/webui/resources/tsc/js/mojo_type_util';
import { handleOpenURLClick, validateScheme } from '$web-common/SecureLink';
import MaybePromptEnableSuggestions from './MaybePromptEnableSuggestions';
Expand Down Expand Up @@ -160,7 +160,7 @@ export default function SearchResults() {

const match = matches[selectedMatch!]
if (!match) {
getNTPBrowserAPI().pageHandler.searchWhatYouTyped(searchEngine?.host ?? braveSearchHost, query, e.altKey, e.ctrlKey, e.metaKey, e.shiftKey);
getNTPBrowserAPI().pageHandler.searchWhatYouTyped(searchEngine?.host ?? defaultSearchHost, query, e.altKey, e.ctrlKey, e.metaKey, e.shiftKey);
return;
}

Expand Down
4 changes: 3 additions & 1 deletion components/brave_new_tab_ui/components/search/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// 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 https://mozilla.org/MPL/2.0/.

import { loadTimeData } from '$web-common/loadTimeData'
import { radius } from "@brave/leo/tokens/css/variables"

// You can obtain one at https://mozilla.org/MPL/2.0/.
export const ENABLED_SEARCH_ENGINES_KEY = 'search-engines'
export const LAST_SEARCH_ENGINE_KEY = 'last-search-engine'
export const braveSearchHost = 'search.brave.com'
export const defaultSearchHost = loadTimeData.getString('searchWidgetDefaultHost')
export const searchBoxRadius = radius.xl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { searchEnginesPromise } from '../../../components/search/SearchContext'
import { MediumSearchEngineIcon } from '../../../components/search/SearchEngineIcon'
import { useNewTabPref } from '../../../hooks/usePref'
import { getLocale } from '../../../../common/locale'
import { braveSearchHost } from '../../../components/search/config'
import { defaultSearchHost } from '../../../components/search/config'
import { useEngineContext } from '../../../components/search/EngineContext'

const EnginesContainer = styled(Flex)`
Expand Down Expand Up @@ -89,7 +89,7 @@ export default function SearchSettings() {
// If we've just enabled the searchbox, make sure at least one engine
// is enabled.
if (e.checked && !hasEnabledEngine(engineConfig)) {
setEngineConfig(braveSearchHost, true)
setEngineConfig(defaultSearchHost, true)
}
}} />
</SettingsRow>
Expand Down
4 changes: 2 additions & 2 deletions components/brave_new_tab_ui/storage/new_tab_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Utils
import { debounce } from '../../common/debounce'
import { loadTimeData } from '../../common/loadTimeData'
import { braveSearchHost } from '../components/search/config'
import { defaultSearchHost } from '../components/search/config'

export const keyName = 'new-tab-data'

Expand All @@ -30,7 +30,7 @@ export const defaultState: NewTab.State = {
showBraveTalk: false,
showBraveVPN: false,
showSearchBox: true,
lastUsedNtpSearchEngine: braveSearchHost,
lastUsedNtpSearchEngine: defaultSearchHost,
promptEnableSearchSuggestions: true,
searchSuggestionsEnabled: false,
showBitcoinDotCom: false,
Expand Down
30 changes: 30 additions & 0 deletions components/search_engines/brave_prepopulated_engines.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const std::map<BravePrepopulatedEngineID, const PrepopulatedEngine*>
{PREPOPULATED_ENGINE_ID_STARTPAGE, &startpage},
{PREPOPULATED_ENGINE_ID_ECOSIA, &brave_ecosia},
{PREPOPULATED_ENGINE_ID_BRAVE, &brave_search},
{PREPOPULATED_ENGINE_ID_YAHOO_JP, &brave_yahoo_jp},
};

PrepopulatedEngine ModifyEngineParams(const PrepopulatedEngine& engine,
Expand Down Expand Up @@ -236,6 +237,35 @@ const PrepopulatedEngine brave_bing = ModifyEngineParams(
"https://www.bing.com/images/detail/search?iss=sbiupload#enterInsights",
PREPOPULATED_ENGINE_ID_BING);

const PrepopulatedEngine brave_yahoo_jp = ModifyEngineParams(
yahoo_jp,
u"Yahoo! JAPAN",
nullptr, // keyword
// search url
"https://search.yahoo.co.jp/search?p={searchTerms}&ei={inputEncoding}&fr="
#if BUILDFLAG(IS_ANDROID)
"brave-mobile_ext",
#else
"brave-desktop_ext",
#endif
// suggest url
"https://search.yahooapis.jp/AssistSearchService/V2/"
"webassistSearch?p={searchTerms}&output=fxjson&fr="
#if BUILDFLAG(IS_ANDROID)
"brave-mobile_ext",
#else
"brave-desktop_ext",
#endif
// image url
"https://search.yahoo.co.jp/image/"
"search?p={searchTerms}&ei={inputEncoding}&fr="
#if BUILDFLAG(IS_ANDROID)
"brave-mobile_ext",
#else
"brave-desktop_ext",
#endif
PREPOPULATED_ENGINE_ID_YAHOO_JP);

const std::map<BravePrepopulatedEngineID, const PrepopulatedEngine*>&
GetBraveEnginesMap() {
return brave_engines_map;
Expand Down
4 changes: 3 additions & 1 deletion components/search_engines/brave_prepopulated_engines.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace TemplateURLPrepopulateData {

// IMPORTANT! Make sure to bump this value if you make changes to the
// engines below or add/remove engines.
inline constexpr int kBraveCurrentDataVersion = 30;
inline constexpr int kBraveCurrentDataVersion = 31;

// The version is important to increment because Chromium will cache the list
// of search engines that are shown. When the version is incremented, Chromium
Expand All @@ -41,6 +41,7 @@ enum BravePrepopulatedEngineID : unsigned int {

// These engine IDs are already defined in prepopulated_engines.json
PREPOPULATED_ENGINE_ID_GOOGLE = 1,
PREPOPULATED_ENGINE_ID_YAHOO_JP = 2,
PREPOPULATED_ENGINE_ID_BING = 3,
PREPOPULATED_ENGINE_ID_YANDEX = 15,
PREPOPULATED_ENGINE_ID_NAVER = 67,
Expand Down Expand Up @@ -121,6 +122,7 @@ extern const PrepopulatedEngine brave_yandex;
extern const PrepopulatedEngine brave_search;
extern const PrepopulatedEngine brave_search_tor;
extern const PrepopulatedEngine brave_bing;
extern const PrepopulatedEngine brave_yahoo_jp;

const std::map<BravePrepopulatedEngineID, const PrepopulatedEngine*>&
GetBraveEnginesMap();
Expand Down
Loading