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 Brave suggested sites #5282

Merged
merged 1 commit into from
Apr 20, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions components/omnibox/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ source_set("browser") {
"brave_omnibox_client.h",
"constants.cc",
"constants.h",
"suggested_sites_match.cc",
"suggested_sites_match.h",
"suggested_sites_provider.cc",
"suggested_sites_provider.h",
"suggested_sites_provider_data.cc",
"topsites_provider_data.cc",
"topsites_provider.cc",
"topsites_provider.h",
Expand Down
31 changes: 15 additions & 16 deletions components/omnibox/browser/brave_autocomplete_controller.cc
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* 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/brave_autocomplete_controller.h"
#include "brave/components/omnibox/browser/topsites_provider.h"

#include <memory>
#include <utility>

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

BraveAutocompleteController::BraveAutocompleteController(
std::unique_ptr<AutocompleteProviderClient> provider_client,
AutocompleteControllerDelegate* delegate,
int provider_types) :
AutocompleteController(
std::move(provider_client),
delegate,
provider_types)
{
if (provider_types & AutocompleteProvider::TYPE_SEARCH) {
providers_.push_back(new TopSitesProvider(provider_client_.get()));
}
}

BraveAutocompleteController::~BraveAutocompleteController() {
int provider_types) : AutocompleteController(std::move(provider_client),
delegate, provider_types) {
if (provider_types & AutocompleteProvider::TYPE_SEARCH) {
providers_.push_back(new TopSitesProvider(provider_client_.get()));
providers_.push_back(new SuggestedSitesProvider(provider_client_.get()));
}
}

}
BraveAutocompleteController::~BraveAutocompleteController() {
}
13 changes: 8 additions & 5 deletions components/omnibox/browser/brave_autocomplete_controller.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* 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 COMPONENTS_OMNIBOX_BROWSER_BRAVE_AUTOCOMPLETE_CONTROLLER_H_
#define COMPONENTS_OMNIBOX_BROWSER_BRAVE_AUTOCOMPLETE_CONTROLLER_H_
#ifndef BRAVE_COMPONENTS_OMNIBOX_BROWSER_BRAVE_AUTOCOMPLETE_CONTROLLER_H_
#define BRAVE_COMPONENTS_OMNIBOX_BROWSER_BRAVE_AUTOCOMPLETE_CONTROLLER_H_

#include <memory>

#include "components/omnibox/browser/autocomplete_controller.h"

class BraveAutocompleteController : public AutocompleteController {
public:
public:
BraveAutocompleteController(
std::unique_ptr<AutocompleteProviderClient> provider_client,
AutocompleteControllerDelegate* delegate,
Expand All @@ -18,4 +21,4 @@ class BraveAutocompleteController : public AutocompleteController {
};


#endif // COMPONENTS_OMNIBOX_BROWSER_BRAVE_AUTOCOMPLETE_CONTROLLER_H_
#endif // BRAVE_COMPONENTS_OMNIBOX_BROWSER_BRAVE_AUTOCOMPLETE_CONTROLLER_H_
22 changes: 22 additions & 0 deletions components/omnibox/browser/suggested_sites_match.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* 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/suggested_sites_match.h"

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

SuggestedSitesMatch::SuggestedSitesMatch(const SuggestedSitesMatch& other) {
destination_url_ = other.destination_url_;
stripped_destination_url_ = other.stripped_destination_url_;
display_ = other.display_;
allow_default_ = other.allow_default_;
}
26 changes: 26 additions & 0 deletions components/omnibox/browser/suggested_sites_match.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* 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_SUGGESTED_SITES_MATCH_H_
#define BRAVE_COMPONENTS_OMNIBOX_BROWSER_SUGGESTED_SITES_MATCH_H_

#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,
const GURL& stripped_destination_url,
const base::string16& display,
const bool allow_default);
GURL destination_url_;
GURL stripped_destination_url_;
base::string16 display_;
bool allow_default_;
};

#endif // BRAVE_COMPONENTS_OMNIBOX_BROWSER_SUGGESTED_SITES_MATCH_H_
110 changes: 110 additions & 0 deletions components/omnibox/browser/suggested_sites_provider.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/* 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/suggested_sites_provider.h"

#include <algorithm>
#include <utility>

#include "base/strings/utf_string_conversions.h"
#include "components/omnibox/browser/autocomplete_input.h"

// As from autocomplete_provider.h:
// Search Secondary Provider (suggestion) | 100++
const int SuggestedSitesProvider::kRelevance = 100;


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

void SuggestedSitesProvider::Start(const AutocompleteInput& input,
bool minimal_changes) {
matches_.clear();
if (input.from_omnibox_focus() ||
(input.type() == metrics::OmniboxInputType::EMPTY) ||
(input.type() == metrics::OmniboxInputType::QUERY)) {
return;
}

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;
// 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()) {
return;
}
size_t foundPos = current_site.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);
if (match.allow_default_ &&
current_site.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;
bbondy marked this conversation as resolved.
Show resolved Hide resolved
matches_[last_index].SetAllowedToBeDefault(input);
// As from autocomplete_provider.h:
// Search Primary Provider (what you typed) | 1300
matches_[last_index].relevance = 1301;
bbondy marked this conversation as resolved.
Show resolved Hide resolved
}
}
};

std::for_each(suggested_sites_.begin(), suggested_sites_.end(),
check_add_match);
}

SuggestedSitesProvider::~SuggestedSitesProvider() {}

// static
ACMatchClassifications SuggestedSitesProvider::StylesForSingleMatch(
const std::string &input_text,
const std::string &site) {
ACMatchClassifications styles;
size_t foundPos = site.find(input_text);
if (std::string::npos == foundPos) {
styles.push_back(ACMatchClassification(0, ACMatchClassification::NONE));
} else if (foundPos == 0) {
styles.push_back(ACMatchClassification(
0, ACMatchClassification::URL | ACMatchClassification::MATCH));
if (site.length() > input_text.length()) {
styles.push_back(ACMatchClassification(input_text.length(),
ACMatchClassification::URL));
}
} else {
styles.push_back(ACMatchClassification(0, ACMatchClassification::URL));
styles.push_back(ACMatchClassification(
foundPos, ACMatchClassification::URL | ACMatchClassification::MATCH));
if (site.length() > foundPos + input_text.length()) {
styles.push_back(
ACMatchClassification(foundPos + input_text.length(), 0));
}
}
return styles;
}

void SuggestedSitesProvider::AddMatch(const base::string16& match_string,
const SuggestedSitesMatch& data,
const ACMatchClassifications& styles) {
AutocompleteMatch match(this, kRelevance + matches_.size(), false,
AutocompleteMatchType::NAVSUGGEST);
match.fill_into_edit = data.display_;
match.destination_url = data.destination_url_;
match.contents = data.display_;
match.contents_class = styles;
match.stripped_destination_url = data.stripped_destination_url_;
matches_.push_back(match);
}
46 changes: 46 additions & 0 deletions components/omnibox/browser/suggested_sites_provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* 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_SUGGESTED_SITES_PROVIDER_H_
#define BRAVE_COMPONENTS_OMNIBOX_BROWSER_SUGGESTED_SITES_PROVIDER_H_

#include <map>
#include <string>

#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/strings/string16.h"
#include "brave/components/omnibox/browser/suggested_sites_match.h"
#include "components/omnibox/browser/autocomplete_match.h"
#include "components/omnibox/browser/autocomplete_provider.h"

class AutocompleteProviderClient;

// This is the provider for Brave Suggested Sites
class SuggestedSitesProvider : public AutocompleteProvider {
public:
explicit SuggestedSitesProvider(AutocompleteProviderClient* client);

// AutocompleteProvider:
void Start(const AutocompleteInput& input, bool minimal_changes) override;

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

static const int kRelevance;

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

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

DISALLOW_COPY_AND_ASSIGN(SuggestedSitesProvider);
};

#endif // BRAVE_COMPONENTS_OMNIBOX_BROWSER_SUGGESTED_SITES_PROVIDER_H_