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

Add preferences for top sites and site suggestions #5438

Merged
merged 2 commits into from
May 4, 2020
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
6 changes: 6 additions & 0 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
<message name="IDS_SETTINGS_APPEARANCE_SETTINGS_SHOW_AUTOCOMPLETE_IN_ADDRESS_BAR" desc="The label for settings switch controlling the showing of autocomplete in address bar">
Show autocomplete in address bar
</message>
<message name="IDS_SETTINGS_APPEARANCE_SETTINGS_USE_AUTOCOMPLETE_TOP_SITES" desc="The label for settings switch controlling whether or not top sites show up in autocomplete">
Show top sites in autocomplete suggestions
</message>
<message name="IDS_SETTINGS_APPEARANCE_SETTINGS_USE_AUTOCOMPLETE_BRAVE_SUGGESTED_SITES" desc="The label for settings switch controlling whether or not Brave suggested sites show up in autocomplete">
Show Brave suggested sites in autocomplete suggestions
</message>
<!-- New Tab Page -->
<message name="IDS_SETTINGS_NEW_TAB" desc="The text label for the New Tab settings page">
New Tab Page
Expand Down
2 changes: 2 additions & 0 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {

// Autocomplete in address bar
registry->RegisterBooleanPref(kAutocompleteEnabled, true);
registry->RegisterBooleanPref(kTopSiteSuggestionsEnabled, true);
registry->RegisterBooleanPref(kBraveSuggestedSiteSuggestionsEnabled, true);

// Password leak detection should be disabled
registry->SetDefaultPrefValue(
Expand Down
4 changes: 4 additions & 0 deletions browser/extensions/api/settings_private/brave_prefs_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ const PrefsUtil::TypedPrefMap& BravePrefsUtil::GetWhitelistedKeys() {
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_whitelist)[kAutocompleteEnabled] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_whitelist)[kTopSiteSuggestionsEnabled] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_whitelist)[kBraveSuggestedSiteSuggestionsEnabled] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_whitelist)[brave_rewards::prefs::kHideBraveRewardsButton] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_whitelist)[kAskWidevineInstall] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@
pref="{{prefs.brave.autocomplete_enabled}}"
label="$i18n{appearanceSettingsShowAutocompleteInAddressBar}">
</settings-toggle-button>
<settings-toggle-button
pref="{{prefs.brave.top_site_suggestions_enabled}}"
label="$i18n{appearanceSettingsUseTopSiteSuggestions}">
</settings-toggle-button>
<settings-toggle-button
pref="{{prefs.brave.brave_suggested_site_suggestions_enabled}}"
label="$i18n{appearanceSettingsUseBraveSuggestedSiteSuggestions}">
</settings-toggle-button>
<settings-toggle-button pref="{{prefs.brave.hide_brave_rewards_button}}"
label="$i18n{appearanceSettingsHideBraveRewardsButtonLabel}"
sub-label="$i18n{appearanceSettingsHideBraveRewardsButtonDesc}">
Expand Down
11 changes: 11 additions & 0 deletions browser/ui/views/omnibox/omnibox_autocomplete_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,14 @@ IN_PROC_BROWSER_TEST_F(OmniboxAutocompleteTest, AutocompleteDisabledTest) {
EXPECT_TRUE(popup_model()->result().empty());
EXPECT_FALSE(popup_view()->IsOpen());
}

IN_PROC_BROWSER_TEST_F(OmniboxAutocompleteTest, TopSiteSuggestionsEnabledTest) {
EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
kTopSiteSuggestionsEnabled));
}

IN_PROC_BROWSER_TEST_F(OmniboxAutocompleteTest,
BraveSuggestedSiteSuggestionsEnabledTest) {
EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
kBraveSuggestedSiteSuggestionsEnabled));
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ void BraveAddCommonStrings(content::WebUIDataSource* html_source,
IDS_SETTINGS_ALWAYS_SHOW_BOOKMARK_BAR_ON_NTP},
{"appearanceSettingsShowAutocompleteInAddressBar",
IDS_SETTINGS_APPEARANCE_SETTINGS_SHOW_AUTOCOMPLETE_IN_ADDRESS_BAR},
{"appearanceSettingsUseTopSiteSuggestions",
IDS_SETTINGS_APPEARANCE_SETTINGS_USE_AUTOCOMPLETE_TOP_SITES},
{"appearanceSettingsUseBraveSuggestedSiteSuggestions",
IDS_SETTINGS_APPEARANCE_SETTINGS_USE_AUTOCOMPLETE_BRAVE_SUGGESTED_SITES},
{"appearanceSettingsGetMoreThemes",
IDS_SETTINGS_APPEARANCE_SETTINGS_GET_MORE_THEMES},
{"appearanceBraveDefaultImagesOptionLabel",
Expand Down
3 changes: 3 additions & 0 deletions common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ const char kAlwaysShowBookmarkBarOnNTP[] =
"brave.always_show_bookmark_bar_on_ntp";
const char kRemoteDebuggingEnabled[] = "brave.remote_debugging_enabled";
const char kAutocompleteEnabled[] = "brave.autocomplete_enabled";
const char kTopSiteSuggestionsEnabled[] = "brave.top_site_suggestions_enabled";
const char kBraveSuggestedSiteSuggestionsEnabled[] =
"brave.brave_suggested_site_suggestions_enabled";
const char kBraveDarkMode[] = "brave.dark_mode";
const char kOtherBookmarksMigrated[] = "brave.other_bookmarks_migrated";
const char kBraveShieldsSettingsVersion[] = "brave.shields_settings_version";
Expand Down
2 changes: 2 additions & 0 deletions common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ extern const char kBraveWalletWeb3Provider[];
extern const char kAlwaysShowBookmarkBarOnNTP[];
extern const char kRemoteDebuggingEnabled[];
extern const char kAutocompleteEnabled[];
extern const char kTopSiteSuggestionsEnabled[];
extern const char kBraveSuggestedSiteSuggestionsEnabled[];
extern const char kBraveDarkMode[];
extern const char kOtherBookmarksMigrated[];
extern const char kBraveShieldsSettingsVersion[];
Expand Down
19 changes: 19 additions & 0 deletions components/omnibox/browser/fake_autocomplete_provider_client.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* 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/. */

#include "brave/components/omnibox/browser/fake_autocomplete_provider_client.h"

#include "brave/common/pref_names.h"
#include "components/prefs/pref_registry_simple.h"

FakeAutocompleteProviderClient::FakeAutocompleteProviderClient() {
auto* registry = pref_service_.registry();
registry->RegisterBooleanPref(kTopSiteSuggestionsEnabled, true);
registry->RegisterBooleanPref(kBraveSuggestedSiteSuggestionsEnabled, true);
}

PrefService* FakeAutocompleteProviderClient::GetPrefs() {
return &pref_service_;
}
24 changes: 24 additions & 0 deletions components/omnibox/browser/fake_autocomplete_provider_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* 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/. */

#ifndef BRAVE_COMPONENTS_OMNIBOX_BROWSER_FAKE_AUTOCOMPLETE_PROVIDER_CLIENT_H_
#define BRAVE_COMPONENTS_OMNIBOX_BROWSER_FAKE_AUTOCOMPLETE_PROVIDER_CLIENT_H_

#include "brave/components/omnibox/browser/suggested_sites_provider.h"

#include "components/omnibox/browser/mock_autocomplete_provider_client.h"
#include "components/prefs/testing_pref_service.h"

class FakeAutocompleteProviderClient : public MockAutocompleteProviderClient {
public:
FakeAutocompleteProviderClient();
PrefService* GetPrefs() override;

private:
TestingPrefServiceSimple pref_service_;
DISALLOW_COPY_AND_ASSIGN(FakeAutocompleteProviderClient);
};

#endif // BRAVE_COMPONENTS_OMNIBOX_BROWSER_FAKE_AUTOCOMPLETE_PROVIDER_CLIENT_H_
8 changes: 7 additions & 1 deletion components/omnibox/browser/suggested_sites_match.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@
#include "brave/components/omnibox/browser/suggested_sites_match.h"

// This is the provider for Brave Suggested Sites
SuggestedSitesMatch::SuggestedSitesMatch(const GURL& destination_url,
SuggestedSitesMatch::SuggestedSitesMatch(const std::string& match_string,
const GURL& destination_url,
const GURL& stripped_destination_url, const base::string16& display,
const bool allow_default) :
match_string_(match_string),
destination_url_(destination_url),
stripped_destination_url_(stripped_destination_url),
display_(display), allow_default_(allow_default) {
}

SuggestedSitesMatch::SuggestedSitesMatch(const SuggestedSitesMatch& other) {
match_string_ = other.match_string_;
destination_url_ = other.destination_url_;
stripped_destination_url_ = other.stripped_destination_url_;
display_ = other.display_;
allow_default_ = other.allow_default_;
}

SuggestedSitesMatch::~SuggestedSitesMatch() {
}
7 changes: 6 additions & 1 deletion components/omnibox/browser/suggested_sites_match.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@
#ifndef BRAVE_COMPONENTS_OMNIBOX_BROWSER_SUGGESTED_SITES_MATCH_H_
#define BRAVE_COMPONENTS_OMNIBOX_BROWSER_SUGGESTED_SITES_MATCH_H_

#include <string>

#include "base/strings/string16.h"
#include "url/gurl.h"

// This is the provider for Brave Suggested Sites
class SuggestedSitesMatch {
public:
SuggestedSitesMatch(const SuggestedSitesMatch& other);
SuggestedSitesMatch(const GURL& destination_url,
SuggestedSitesMatch(const std::string& match_string,
const GURL& destination_url,
const GURL& stripped_destination_url,
const base::string16& display,
const bool allow_default);
~SuggestedSitesMatch();
std::string match_string_;
GURL destination_url_;
GURL stripped_destination_url_;
base::string16 display_;
Expand Down
29 changes: 17 additions & 12 deletions components/omnibox/browser/suggested_sites_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
#include <utility>

#include "base/strings/utf_string_conversions.h"
#include "brave/common/pref_names.h"
#include "components/omnibox/browser/autocomplete_input.h"
#include "components/omnibox/browser/autocomplete_provider_client.h"
#include "components/prefs/pref_service.h"

// As from autocomplete_provider.h:
// Search Secondary Provider (suggestion) | 100++
Expand All @@ -18,12 +21,17 @@ const int SuggestedSitesProvider::kRelevance = 100;

SuggestedSitesProvider::SuggestedSitesProvider(
AutocompleteProviderClient* client)
: AutocompleteProvider(AutocompleteProvider::TYPE_SEARCH) {
: AutocompleteProvider(AutocompleteProvider::TYPE_SEARCH), client_(client) {
}

void SuggestedSitesProvider::Start(const AutocompleteInput& input,
bool minimal_changes) {
bool minimal_changes) {
matches_.clear();
auto* prefs = client_->GetPrefs();
if (!prefs || !prefs->GetBoolean(kBraveSuggestedSiteSuggestionsEnabled)) {
return;
}

if (input.from_omnibox_focus() ||
(input.type() == metrics::OmniboxInputType::EMPTY) ||
(input.type() == metrics::OmniboxInputType::QUERY)) {
Expand All @@ -33,25 +41,23 @@ void SuggestedSitesProvider::Start(const AutocompleteInput& input,
const std::string input_text =
base::ToLowerASCII(base::UTF16ToUTF8(input.text()));
auto check_add_match =
[&](const std::pair<std::string, SuggestedSitesMatch>& pair) {
const std::string& current_site = pair.first;
const SuggestedSitesMatch& match = pair.second;
[&](const SuggestedSitesMatch& match) {
// Don't bother matching until 4 chars, or less if it's an exact match
if (input_text.length() < 4 &&
current_site.length() != input_text.length()) {
match.match_string_.length() != input_text.length()) {
return;
}
size_t foundPos = current_site.find(input_text);
size_t foundPos = match.match_string_.find(input_text);
// We'd normally check for npos here but we want only people that
// really want these suggestions. Example don't suggest bitcoin and
// litecoin for just a coin search.
if (foundPos == 0) {
ACMatchClassifications styles =
StylesForSingleMatch(input_text,
base::UTF16ToASCII(match.display_));
AddMatch(base::ASCIIToUTF16(current_site), match, styles);
AddMatch(match, styles);
if (match.allow_default_ &&
current_site.length() == input_text.length()) {
match.match_string_.length() == input_text.length()) {
// It's guaranteed that matches_ has at least 1 item
// here because of the previous AddMatch call.
size_t last_index = matches_.size() - 1;
Expand Down Expand Up @@ -96,9 +102,8 @@ ACMatchClassifications SuggestedSitesProvider::StylesForSingleMatch(
return styles;
}

void SuggestedSitesProvider::AddMatch(const base::string16& match_string,
const SuggestedSitesMatch& data,
const ACMatchClassifications& styles) {
void SuggestedSitesProvider::AddMatch(const SuggestedSitesMatch& data,
const ACMatchClassifications& styles) {
AutocompleteMatch match(this, kRelevance + matches_.size(), false,
AutocompleteMatchType::NAVSUGGEST);
match.fill_into_edit = data.display_;
Expand Down
8 changes: 4 additions & 4 deletions components/omnibox/browser/suggested_sites_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#ifndef BRAVE_COMPONENTS_OMNIBOX_BROWSER_SUGGESTED_SITES_PROVIDER_H_
#define BRAVE_COMPONENTS_OMNIBOX_BROWSER_SUGGESTED_SITES_PROVIDER_H_

#include <map>
#include <vector>
#include <string>

#include "base/compiler_specific.h"
Expand All @@ -28,18 +28,18 @@ class SuggestedSitesProvider : public AutocompleteProvider {

private:
~SuggestedSitesProvider() override;
static std::map<std::string, SuggestedSitesMatch> suggested_sites_;
static std::vector<SuggestedSitesMatch> suggested_sites_;

static const int kRelevance;

void AddMatch(const base::string16& match_string,
const SuggestedSitesMatch& match,
void AddMatch(const SuggestedSitesMatch& match,
const ACMatchClassifications& styles);

static ACMatchClassifications StylesForSingleMatch(
const std::string &input_text,
const std::string &site);

AutocompleteProviderClient* client_;
DISALLOW_COPY_AND_ASSIGN(SuggestedSitesProvider);
};

Expand Down
Loading