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

top sites suggestions #8

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ source_set("browser") {

deps = [
"//brave/components/brave_shields/browser:brave_shields",
"//brave/components/omnibox/browser:brave_autocomplete",
"net",
]
}
1 change: 1 addition & 0 deletions browser/net/brave_httpse_network_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "brave/browser/net/url_context.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "brave/components/brave_shields/browser/https_everywhere_service.h"
#include "content/public/browser/browser_thread.h"
#include "net/url_request/url_request.h"

using content::BrowserThread;
Expand Down
15 changes: 15 additions & 0 deletions components/omnibox/browser/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
source_set("brave_autocomplete") {
sources = [
"brave_autocomplete_controller.cc",
"brave_autocomplete_controller.h",
"topsites_provider_data.cc",
"topsites_provider.cc",
"topsites_provider.h",
]

deps = [
]
public_deps = [
"//chrome/common",
]
}
27 changes: 27 additions & 0 deletions components/omnibox/browser/brave_autocomplete_controller.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* 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"



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() {

}
21 changes: 21 additions & 0 deletions components/omnibox/browser/brave_autocomplete_controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* 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_

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

class BraveAutocompleteController : public AutocompleteController {
public:
BraveAutocompleteController(
std::unique_ptr<AutocompleteProviderClient> provider_client,
AutocompleteControllerDelegate* delegate,
int provider_types);
~BraveAutocompleteController() override;
};


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

#include <stddef.h>

#include <algorithm>

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

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


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

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

const std::string input_text = base::ToLowerASCII(base::UTF16ToASCII(input.text()));

for (std::vector<std::string>::const_iterator i = top_sites_.begin();
(i != top_sites_.end()) && (matches_.size() < kMaxMatches); ++i) {

const std::string &current_site = *i;
size_t foundPos = current_site.find(input_text);
if (std::string::npos != foundPos) {
ACMatchClassifications styles = StylesForSingleMatch(input_text, current_site, foundPos);
AddMatch(base::ASCIIToUTF16(current_site), styles);
}
}

for (size_t i = 0; i < matches_.size(); ++i)
matches_[i].relevance = kRelevance + matches_.size() - (i + 1);
if (!HistoryProvider::PreventInlineAutocomplete(input) &&
(matches_.size() == 1) && !matches_[0].inline_autocompletion.empty()) {
// If there's only one possible completion of the user's input and
// allowing completions is okay, give the match a high enough score to
// allow it to beat url-what-you-typed and be inlined.
matches_[0].relevance = 1250;
matches_[0].allowed_to_be_default_match = true;
}
}

TopSitesProvider::~TopSitesProvider() {}

//static
ACMatchClassifications TopSitesProvider::StylesForSingleMatch(
const std::string &input_text,
const std::string &site,
const size_t &foundPos) {
ACMatchClassifications styles;
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 TopSitesProvider::AddMatch(const base::string16& match_string,
const ACMatchClassifications& styles) {
static const base::string16 kScheme = base::ASCIIToUTF16("https://");
AutocompleteMatch match(this, kRelevance, false,
AutocompleteMatchType::NAVSUGGEST);
match.fill_into_edit = match_string;
match.destination_url = GURL(kScheme + match_string);
match.contents = match_string;
match.contents_class = styles;
matches_.push_back(match);
}
44 changes: 44 additions & 0 deletions components/omnibox/browser/topsites_provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* 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_TOPSITES_PROVIDER_H_
#define COMPONENTS_OMNIBOX_BROWSER_TOPSITES_PROVIDER_H_

#include <vector>

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

class AutocompleteProviderClient;

// This is the provider for top Alexa 500 sites URLs
class TopSitesProvider : public AutocompleteProvider {
public:
explicit TopSitesProvider(AutocompleteProviderClient* client);

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

private:
~TopSitesProvider() override;

static const int kRelevance;

static std::vector<std::string> top_sites_;

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

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

DISALLOW_COPY_AND_ASSIGN(TopSitesProvider);
};

#endif // COMPONENTS_OMNIBOX_BROWSER_TOPSITES_PROVIDER_H_
Loading