blob: bd8ed5ef5b5ec4903913b58509933a3aba5ebf55 [file] [log] [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/omnibox/browser/unscoped_extension_provider.h"
#include <string>
#include "base/check_is_test.h"
#include "base/memory/raw_ptr.h"
#include "components/omnibox/browser/autocomplete_provider.h"
#include "components/omnibox/browser/autocomplete_provider_client.h"
#include "components/omnibox/browser/autocomplete_provider_listener.h"
#include "components/omnibox/browser/unscoped_extension_provider_delegate.h"
#include "components/search_engines/template_url_service.h"
UnscopedExtensionProvider::UnscopedExtensionProvider(
AutocompleteProviderClient* client,
AutocompleteProviderListener* listener)
: AutocompleteProvider(AutocompleteProvider::TYPE_UNSCOPED_EXTENSION),
client_(client),
template_url_service_(client->GetTemplateURLService()),
delegate_(client->GetUnscopedExtensionProviderDelegate(this)) {
AddListener(listener);
}
UnscopedExtensionProvider::~UnscopedExtensionProvider() = default;
void UnscopedExtensionProvider::Start(const AutocompleteInput& input,
bool minimal_changes) {
// If the changes to the input are not minimal, clear the current list of
// matches and suggestion group information and increment the current request
// ID to discard any suggestions that may be incoming later with a stale
// request ID.
Stop(/*clear_cached_results=*/!minimal_changes,
/*due_to_user_inactivity=*/false);
// Extension suggestions are not allowed in keyword mode.
if (input.InKeywordMode()) {
return;
}
// Extension suggestions are not allowed for zero-suggest or empty inputs.
if (input.IsZeroSuggest() ||
input.type() == metrics::OmniboxInputType::EMPTY) {
return;
}
// Extension suggestions are always provided asynchronously.
if (input.omit_asynchronous_matches()) {
return;
}
// Do not forward the input to the extensions delegate if the changes to the
// input are minimal.
if (minimal_changes) {
return;
}
// Do not forward the input to the extensions delegate if there are no
// unscoped extensions.
std::set<std::string> unscoped_extensions =
GetTemplateURLService()->GetUnscopedModeExtensionIds();
if (unscoped_extensions.empty()) {
return;
}
// Forward the input to the extensions delegate.
delegate_->Start(input, minimal_changes, unscoped_extensions);
}
void UnscopedExtensionProvider::Stop(bool clear_cached_results,
bool due_to_user_inactivity) {
AutocompleteProvider::Stop(clear_cached_results, due_to_user_inactivity);
delegate_->Stop(clear_cached_results);
}
TemplateURLService* UnscopedExtensionProvider::GetTemplateURLService() const {
// Make sure the model is loaded. This is cheap and quickly bails out if
// the model is already loaded.
template_url_service_->Load();
return template_url_service_;
}
void UnscopedExtensionProvider::AddToSuggestionGroupsMap(
omnibox::GroupId group_id,
omnibox::GroupConfig group_config) {
suggestion_groups_map_[group_id].MergeFrom(group_config);
}