-
Notifications
You must be signed in to change notification settings - Fork 922
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added search promotion button in omnibox
- Loading branch information
Showing
19 changed files
with
722 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+793 Bytes
.../default_100_percent/brave/brave_search_conversion_button_brave_search_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.72 KB
.../default_200_percent/brave/brave_search_conversion_button_brave_search_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
211 changes: 211 additions & 0 deletions
211
browser/ui/views/location_bar/brave_search_conversion/promotion_button_controller.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
/* Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "brave/browser/ui/views/location_bar/brave_search_conversion/promotion_button_controller.h" | ||
|
||
#include "base/functional/bind.h" | ||
#include "brave/browser/ui/views/location_bar/brave_search_conversion/promotion_button_view.h" | ||
#include "brave/components/ai_chat/core/common/buildflags/buildflags.h" | ||
#include "brave/components/brave_search_conversion/pref_names.h" | ||
#include "brave/components/brave_search_conversion/utils.h" | ||
#include "brave/components/omnibox/browser/leo_provider.h" | ||
#include "brave/components/omnibox/browser/promotion_utils.h" | ||
#include "brave/components/search_engines/brave_prepopulated_engines.h" | ||
#include "chrome/browser/image_fetcher/image_fetcher_service_factory.h" | ||
#include "chrome/browser/profiles/profile.h" | ||
#include "chrome/browser/profiles/profile_key.h" | ||
#include "chrome/browser/search_engines/template_url_service_factory.h" | ||
#include "chrome/browser/ui/browser.h" | ||
#include "chrome/browser/ui/browser_navigator.h" | ||
#include "chrome/browser/ui/browser_navigator_params.h" | ||
#include "chrome/browser/ui/tabs/tab_strip_model.h" | ||
#include "chrome/browser/ui/views/omnibox/omnibox_view_views.h" | ||
#include "components/image_fetcher/core/image_fetcher.h" | ||
#include "components/image_fetcher/core/image_fetcher_service.h" | ||
#include "components/omnibox/browser/autocomplete_match.h" | ||
#include "components/omnibox/browser/omnibox_edit_model.h" | ||
#include "components/omnibox/browser/omnibox_view.h" | ||
#include "components/search_engines/template_url_data_util.h" | ||
#include "components/search_engines/template_url_service.h" | ||
#include "ui/base/page_transition_types.h" | ||
#include "url/gurl.h" | ||
|
||
namespace { | ||
|
||
constexpr char kImageFetcherUmaClientName[] = "SearchPromotionButtonFavicon"; | ||
|
||
constexpr net::NetworkTrafficAnnotationTag | ||
kSearchPromotionButtonTrafficAnnotation = | ||
net::DefineNetworkTrafficAnnotation("search_promotion", R"( | ||
semantics { | ||
sender: "PromotionButtonController" | ||
description: | ||
"Fetches favicon for current search provider" | ||
trigger: | ||
"When current search provider is changed" | ||
data: "URL of the favicon image to be fetched." | ||
destination: WEBSITE | ||
} | ||
policy { | ||
cookies_allowed: NO | ||
setting: "Disabled when the user dismissed." | ||
})"); | ||
|
||
} // namespace | ||
|
||
PromotionButtonController::PromotionButtonController( | ||
PromotionButtonView* button, | ||
OmniboxViewViews* omnibox_view, | ||
Browser* browser) | ||
: button_(button), | ||
omnibox_view_(omnibox_view), | ||
prefs_(*browser->profile()->GetPrefs()), | ||
browser_(*browser) { | ||
CHECK(button_ && omnibox_view); | ||
button_->SetDismissedCallback(base::BindOnce( | ||
&PromotionButtonController::Dismissed, weak_factory_.GetWeakPtr())); | ||
button_->SetMakeDefaultCallback(base::BindOnce( | ||
&PromotionButtonController::SetDefaultAndLoadBraveSearchWithCurrentInput, | ||
weak_factory_.GetWeakPtr())); | ||
template_url_service_ = | ||
TemplateURLServiceFactory::GetForProfile(browser_->profile()); | ||
is_brave_search_default_ = IsBraveSearchDefault(); | ||
view_observation_.AddObservation(button_); | ||
view_observation_.AddObservation(omnibox_view_); | ||
UpdateButtonUI(); | ||
template_url_service_observation_.Observe(template_url_service_); | ||
} | ||
|
||
PromotionButtonController::~PromotionButtonController() { | ||
button_ = nullptr; | ||
omnibox_view_ = nullptr; | ||
} | ||
|
||
void PromotionButtonController::Show(bool show) { | ||
button_->SetVisible(show); | ||
|
||
// For now, animation is triggered only once before chaning search provider. | ||
// Showing animation whenever this button is visible is too much. | ||
// TODO(simonhong): adjust how frequently this animation is used. | ||
if (use_animation_ && show) { | ||
button_->AnimateExpand(); | ||
use_animation_ = false; | ||
} | ||
} | ||
|
||
bool PromotionButtonController::ShouldShowSearchPromotionButton() { | ||
if (!button_ || !omnibox_view_) { | ||
return false; | ||
} | ||
|
||
if (is_brave_search_default_) { | ||
return false; | ||
} | ||
|
||
if (prefs_->GetBoolean(brave_search_conversion::prefs::kDismissed)) { | ||
return false; | ||
} | ||
|
||
if (!omnibox_view_->model()->PopupIsOpen() || | ||
GURL(omnibox_view_->GetText()).is_valid()) { | ||
return false; | ||
} | ||
|
||
const AutocompleteMatch match = omnibox_view_->model()->CurrentMatch(nullptr); | ||
return !IsBraveSearchPromotionMatch(match) && | ||
#if BUILDFLAG(ENABLE_AI_CHAT) | ||
!LeoProvider::IsMatchFromLeoProvider(match) && | ||
#endif | ||
AutocompleteMatch::IsSearchType(match.type); | ||
} | ||
|
||
void PromotionButtonController::OnViewIsDeleting(views::View* observed_view) { | ||
// If any observed view among butto or omnibox is destroying, | ||
// this controller will not do anything after that. | ||
view_observation_.RemoveAllObservations(); | ||
button_ = nullptr; | ||
omnibox_view_ = nullptr; | ||
} | ||
|
||
void PromotionButtonController::OnTemplateURLServiceChanged() { | ||
use_animation_ = true; | ||
is_brave_search_default_ = IsBraveSearchDefault(); | ||
if (is_brave_search_default_) { | ||
return; | ||
} | ||
|
||
UpdateButtonUI(); | ||
} | ||
|
||
void PromotionButtonController::OnTemplateURLServiceShuttingDown() { | ||
template_url_service_observation_.Reset(); | ||
} | ||
|
||
void PromotionButtonController::SetDefaultAndLoadBraveSearchWithCurrentInput() { | ||
CHECK(omnibox_view_); | ||
|
||
// Set brave search as default. | ||
auto provider_data = TemplateURLDataFromPrepopulatedEngine( | ||
TemplateURLPrepopulateData::brave_search); | ||
TemplateURL template_url(*provider_data); | ||
template_url_service_->SetUserSelectedDefaultSearchProvider(&template_url); | ||
|
||
// Load brave search with current input. | ||
const auto url = | ||
template_url_service_->GenerateSearchURLForDefaultSearchProvider( | ||
omnibox_view_->GetText()); | ||
|
||
NavigateParams params(&*browser_, url, ui::PAGE_TRANSITION_TYPED); | ||
params.disposition = WindowOpenDisposition::CURRENT_TAB; | ||
Navigate(¶ms); | ||
} | ||
|
||
void PromotionButtonController::Dismissed() { | ||
button_->SetVisible(false); | ||
brave_search_conversion::SetDismissed(&*prefs_); | ||
|
||
// After dismissed, we don't need to monitor search provider changes. | ||
template_url_service_observation_.Reset(); | ||
} | ||
|
||
void PromotionButtonController::UpdateButtonUI() { | ||
if (!template_url_service_->loaded()) { | ||
return; | ||
} | ||
|
||
const auto* template_url = template_url_service_->GetDefaultSearchProvider(); | ||
auto* service = ImageFetcherServiceFactory::GetForKey( | ||
browser_->profile()->GetProfileKey()); | ||
CHECK(service); | ||
auto* fetcher = service->GetImageFetcher( | ||
image_fetcher::ImageFetcherConfig::kDiskCacheOnly); | ||
image_fetcher::ImageFetcherParams params( | ||
kSearchPromotionButtonTrafficAnnotation, kImageFetcherUmaClientName); | ||
fetcher->FetchImage( | ||
template_url->favicon_url(), | ||
base::BindOnce(&PromotionButtonController::OnGetFaviconImage, | ||
weak_factory_.GetWeakPtr()), | ||
params); | ||
} | ||
|
||
bool PromotionButtonController::IsBraveSearchDefault() { | ||
const auto* template_url = template_url_service_->GetDefaultSearchProvider(); | ||
if (template_url->prepopulate_id() == | ||
TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_BRAVE || | ||
template_url->prepopulate_id() == | ||
TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_BRAVE_TOR) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
void PromotionButtonController::OnGetFaviconImage( | ||
const gfx::Image& image, | ||
const image_fetcher::RequestMetadata& request_metadata) { | ||
if (!image.IsEmpty()) { | ||
button_->UpdateTargetProviderImage(image); | ||
} | ||
} |
Oops, something went wrong.