Skip to content

Commit

Permalink
[119][CSC] Make Lens a contextual panel if CSC is enabled.
Browse files Browse the repository at this point in the history
Also, modifies Lens panel to have companion label and icon in side panel
combobox when CSC is enabled with image search feature param disabled.

Lens panel will only open when CSC is enabled if the image search
feature param is disabled. These changes allow the CSC + Lens to coexist in
the side panel without showing up in the combobox as two separate
entries.

(cherry picked from commit d560e0b)

Bug: b:301099032, 1444953, 1449021
Change-Id: I9ac2bd32e5f4517507b758694aacfd04721825c1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4903309
Reviewed-by: Thomas Lukaszewicz <tluk@chromium.org>
Reviewed-by: Anudeep Palanki <apalanki@google.com>
Commit-Queue: Juan Mojica <juanmojica@google.com>
Cr-Original-Commit-Position: refs/heads/main@{#1206041}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4918920
Reviewed-by: Caroline Rising <corising@chromium.org>
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Cr-Commit-Position: refs/branch-heads/6045@{#129}
Cr-Branched-From: 905e8bd-refs/heads/main@{#1204232}
  • Loading branch information
Juan Mojica authored and Chromium LUCI CQ committed Oct 6, 2023
1 parent 6d5a6c9 commit cbdc0d8
Show file tree
Hide file tree
Showing 8 changed files with 712 additions and 36 deletions.
29 changes: 29 additions & 0 deletions chrome/browser/ui/side_panel/companion/companion_tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "components/translate/core/common/translate_constants.h"
#include "content/public/browser/web_contents.h"
#include "net/base/url_util.h"
#include "ui/base/models/image_model.h"

#if BUILDFLAG(ENABLE_LENS_DESKTOP_GOOGLE_BRANDED_FEATURES)
#include "chrome/browser/lens/region_search/lens_region_search_controller.h"
Expand Down Expand Up @@ -233,6 +234,34 @@ void CompanionTabHelper::DidOpenRequestedURL(
}
}

void CompanionTabHelper::CreateAndRegisterLensEntry(
const content::OpenURLParams& params,
std::u16string combobox_label,
const ui::ImageModel favicon) {
delegate_->CreateAndRegisterLensEntry(params, combobox_label, favicon);
}

void CompanionTabHelper::RemoveContextualLensView() {
delegate_->RemoveContextualLensView();
}

void CompanionTabHelper::OpenContextualLensView(
const content::OpenURLParams& params) {
delegate_->OpenContextualLensView(params);
}

content::WebContents* CompanionTabHelper::GetLensViewWebContentsForTesting() {
return delegate_->GetLensViewWebContentsForTesting(); // IN-TEST
}

bool CompanionTabHelper::OpenLensResultsInNewTabForTesting() {
return delegate_->OpenLensResultsInNewTabForTesting(); // IN-TEST
}

bool CompanionTabHelper::IsLensLaunchButtonEnabledForTesting() {
return delegate_->IsLensLaunchButtonEnabledForTesting(); // IN-TEST
}

WEB_CONTENTS_USER_DATA_KEY_IMPL(CompanionTabHelper);

} // namespace companion
33 changes: 33 additions & 0 deletions chrome/browser/ui/side_panel/companion/companion_tab_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ namespace content {
class WebContents;
} // namespace content

namespace ui {
class ImageModel;
} // namespace ui

namespace companion {

class CompanionPageHandler;
Expand Down Expand Up @@ -58,6 +62,23 @@ class CompanionTabHelper
// loaded and the onload event was dispatched.
virtual void AddCompanionFinishedLoadingCallback(
base::OnceCallback<void()> callback) = 0;

// Create a contextual Lens entry.
virtual void CreateAndRegisterLensEntry(
const content::OpenURLParams& params,
std::u16string combobox_label,
const ui::ImageModel favicon) = 0;
// Opens a contextual Lens view that was already created.
virtual void OpenContextualLensView(
const content::OpenURLParams& params) = 0;
// Removes and deletes the contextual Lens view.
virtual void RemoveContextualLensView() = 0;
// Testing method to get the Lens view web contents.
virtual content::WebContents* GetLensViewWebContentsForTesting() = 0;
// Testing method to open lens results in a new tab.
virtual bool OpenLensResultsInNewTabForTesting() = 0;
// Testing method to check if the new tab button is enabled for Lens.
virtual bool IsLensLaunchButtonEnabledForTesting() = 0;
};

using CompanionLoadedCallback = base::OnceCallback<void()>;
Expand Down Expand Up @@ -143,6 +164,18 @@ class CompanionTabHelper
absl::optional<SidePanelOpenTrigger>
GetAndResetMostRecentSidePanelOpenTrigger();

// Create and register a contextual Lens entry.
void CreateAndRegisterLensEntry(const content::OpenURLParams& params,
std::u16string combobox_label,
const ui::ImageModel favicon);
// Open an already created contextual Lens view with provided params.
void OpenContextualLensView(const content::OpenURLParams& params);
// Delete the contextual lens view associated with this web contents.
void RemoveContextualLensView();
content::WebContents* GetLensViewWebContentsForTesting();
bool OpenLensResultsInNewTabForTesting();
bool IsLensLaunchButtonEnabledForTesting();

private:
explicit CompanionTabHelper(content::WebContents* web_contents);

Expand Down
119 changes: 98 additions & 21 deletions chrome/browser/ui/views/side_panel/lens/lens_side_panel_coordinator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/side_panel/companion/companion_tab_helper.h"
#include "chrome/browser/ui/side_panel/companion/companion_utils.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/side_panel/lens/lens_unified_side_panel_view.h"
Expand All @@ -30,6 +32,7 @@
#include "components/search_engines/util.h"
#include "components/vector_icons/vector_icons.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/ui_base_features.h"
#include "ui/gfx/image/image.h"
#include "ui/views/vector_icons.h"

Expand Down Expand Up @@ -69,8 +72,27 @@ LensSidePanelCoordinator::~LensSidePanelCoordinator() {

void LensSidePanelCoordinator::DeregisterLensFromSidePanel() {
lens_side_panel_view_ = nullptr;
SidePanelCoordinator::GetGlobalSidePanelRegistry(&GetBrowser())
->Deregister(SidePanelEntry::Key(SidePanelEntry::Id::kLens));
auto* active_web_contents = GetBrowserView()->GetActiveWebContents();
const bool is_companion_enabled =
companion::IsSearchInCompanionSidePanelSupported(&GetBrowser());

// Delete contextual lens view if applicable.
if (is_companion_enabled && active_web_contents) {
auto* companion_helper =
companion::CompanionTabHelper::FromWebContents(active_web_contents);
if (companion_helper) {
companion_helper->RemoveContextualLensView();
}
}

// Remove entry from side panel entry if it exists.
auto* registry =
is_companion_enabled
? SidePanelRegistry::Get(active_web_contents)
: SidePanelCoordinator::GetGlobalSidePanelRegistry(&GetBrowser());
if (registry) {
registry->Deregister(SidePanelEntry::Key(SidePanelEntry::Id::kLens));
}
}

void LensSidePanelCoordinator::OnSidePanelDidClose() {
Expand All @@ -80,13 +102,16 @@ void LensSidePanelCoordinator::OnSidePanelDidClose() {
}

void LensSidePanelCoordinator::OnFaviconFetched(const gfx::Image& favicon) {
auto* global_registry =
SidePanelCoordinator::GetGlobalSidePanelRegistry(&GetBrowser());
if (global_registry == nullptr)
auto* registry =
companion::IsSearchInCompanionSidePanelSupported(&GetBrowser())
? SidePanelRegistry::Get(GetBrowserView()->GetActiveWebContents())
: SidePanelCoordinator::GetGlobalSidePanelRegistry(&GetBrowser());
if (registry == nullptr) {
return;
}

auto* lens_side_panel_entry = global_registry->GetEntryForKey(
SidePanelEntry::Key(SidePanelEntry::Id::kLens));
auto* lens_side_panel_entry =
registry->GetEntryForKey(SidePanelEntry::Key(SidePanelEntry::Id::kLens));
if (lens_side_panel_entry == nullptr)
return;

Expand Down Expand Up @@ -121,6 +146,12 @@ void LensSidePanelCoordinator::OnEntryHidden(SidePanelEntry* entry) {
}

bool LensSidePanelCoordinator::IsLaunchButtonEnabledForTesting() {
if (companion::IsSearchInCompanionSidePanelSupported(&GetBrowser())) {
auto* companion_helper = companion::CompanionTabHelper::FromWebContents(
GetBrowserView()->GetActiveWebContents());
return companion_helper->IsLensLaunchButtonEnabledForTesting(); // IN-TEST
}

DCHECK(lens_side_panel_view_);
return lens_side_panel_view_->IsLaunchButtonEnabledForTesting();
}
Expand All @@ -130,6 +161,13 @@ bool LensSidePanelCoordinator::IsDefaultSearchProviderGoogle() {
}

std::u16string LensSidePanelCoordinator::GetComboboxLabel() {
// If this panel was opened while the companion feature is enabled, then we
// want this panel to be labelled like the companion panel.
if (companion::IsSearchInCompanionSidePanelSupportedForProfile(
GetBrowser().profile())) {
return l10n_util::GetStringUTF16(IDS_SIDE_PANEL_COMPANION_TITLE);
}

if (IsDefaultSearchProviderGoogle()) {
return l10n_util::GetStringUTF16(IDS_GOOGLE_LENS_TITLE);
}
Expand All @@ -140,6 +178,17 @@ std::u16string LensSidePanelCoordinator::GetComboboxLabel() {
}

const ui::ImageModel LensSidePanelCoordinator::GetFaviconImage() {
// If this panel was opened while the companion feature is enabled, then we
// want this panel to use the companion panel favicon.
if (companion::IsSearchInCompanionSidePanelSupportedForProfile(
GetBrowser().profile())) {
return ui::ImageModel::FromVectorIcon(
features::IsChromeRefresh2023()
? vector_icons::
kGoogleSearchCompanionMonochromeLogoChromeRefreshIcon
: vector_icons::kGoogleSearchCompanionMonochromeLogoIcon);
}

// If google is search engine, return checked-in lens icon.
if (IsDefaultSearchProviderGoogle())
return ui::ImageModel::FromVectorIcon(vector_icons::kGoogleLensLogoIcon);
Expand Down Expand Up @@ -172,28 +221,46 @@ const ui::ImageModel LensSidePanelCoordinator::GetFaviconImage() {
void LensSidePanelCoordinator::RegisterEntryAndShow(
const content::OpenURLParams& params) {
base::RecordAction(base::UserMetricsAction("LensUnifiedSidePanel.LensQuery"));
auto* global_registry =
SidePanelCoordinator::GetGlobalSidePanelRegistry(&GetBrowser());
const bool is_companion_enabled =
companion::IsSearchInCompanionSidePanelSupported(&GetBrowser());
auto* companion_helper = companion::CompanionTabHelper::FromWebContents(
GetBrowserView()->GetActiveWebContents());
auto* registry =
is_companion_enabled
? SidePanelRegistry::Get(GetBrowserView()->GetActiveWebContents())
: SidePanelCoordinator::GetGlobalSidePanelRegistry(&GetBrowser());

// check if the view is already registered
if (global_registry->GetEntryForKey(
if (registry->GetEntryForKey(
SidePanelEntry::Key(SidePanelEntry::Id::kLens)) != nullptr &&
lens_side_panel_view_ != nullptr) {
(lens_side_panel_view_ != nullptr || is_companion_enabled)) {
// The user issued a follow-up Lens query.
base::RecordAction(
base::UserMetricsAction("LensUnifiedSidePanel.LensQuery_Followup"));
lens_side_panel_view_->OpenUrl(params);
if (is_companion_enabled) {
companion_helper->OpenContextualLensView(params);
} else {
lens_side_panel_view_->OpenUrl(params);
}
} else {
base::RecordAction(
base::UserMetricsAction("LensUnifiedSidePanel.LensQuery_New"));
auto entry = std::make_unique<SidePanelEntry>(
SidePanelEntry::Id::kLens, GetComboboxLabel(), GetFaviconImage(),
base::BindRepeating(&LensSidePanelCoordinator::CreateLensWebView,
base::Unretained(this), params),
base::BindRepeating(&LensSidePanelCoordinator::GetOpenInNewTabURL,
base::Unretained(this)));
entry->AddObserver(this);
global_registry->Register(std::move(entry));
if (is_companion_enabled) {
// Side panel entry needs to be created and registered
// in the companion side panel controller that exists per web contents in
// order to prevent a dependency on views on CompanionTabHelper.
companion_helper->CreateAndRegisterLensEntry(params, GetComboboxLabel(),
GetFaviconImage());
} else {
auto entry = std::make_unique<SidePanelEntry>(
SidePanelEntry::Id::kLens, GetComboboxLabel(), GetFaviconImage(),
base::BindRepeating(&LensSidePanelCoordinator::CreateLensWebView,
base::Unretained(this), params),
base::BindRepeating(&LensSidePanelCoordinator::GetOpenInNewTabURL,
base::Unretained(this)));
entry->AddObserver(this);
registry->Register(std::move(entry));
}
}

auto* side_panel_coordinator = GetSidePanelCoordinator();
Expand All @@ -206,7 +273,6 @@ void LensSidePanelCoordinator::RegisterEntryAndShow(
base::RecordAction(base::UserMetricsAction(
"LensUnifiedSidePanel.LensQuery_SidePanelOpenNonLens"));
}

side_panel_coordinator->Show(SidePanelEntry::Id::kLens,
SidePanelOpenTrigger::kLensContextMenu);
} else {
Expand All @@ -216,11 +282,22 @@ void LensSidePanelCoordinator::RegisterEntryAndShow(
}

content::WebContents* LensSidePanelCoordinator::GetViewWebContentsForTesting() {
if (companion::IsSearchInCompanionSidePanelSupported(&GetBrowser())) {
auto* companion_helper = companion::CompanionTabHelper::FromWebContents(
GetBrowserView()->GetActiveWebContents());
return companion_helper->GetLensViewWebContentsForTesting(); // IN-TEST
}
return lens_side_panel_view_ ? lens_side_panel_view_->GetWebContents()
: nullptr;
}

bool LensSidePanelCoordinator::OpenResultsInNewTabForTesting() {
if (companion::IsSearchInCompanionSidePanelSupported(&GetBrowser())) {
auto* companion_helper = companion::CompanionTabHelper::FromWebContents(
GetBrowserView()->GetActiveWebContents());
return companion_helper->OpenLensResultsInNewTabForTesting(); // IN-TEST
}

if (lens_side_panel_view_ == nullptr)
return false;

Expand Down
Loading

0 comments on commit cbdc0d8

Please sign in to comment.