Skip to content

Commit d560e0b

Browse files
Juan MojicaChromium LUCI CQ
Juan Mojica
authored and
Chromium LUCI CQ
committed
[CSC] Make Lens a contextual panel if CSC is enabled.
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. Bug: b:301099032, 1444953 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-Commit-Position: refs/heads/main@{#1206041}
1 parent 9e236cf commit d560e0b

8 files changed

+712
-36
lines changed

chrome/browser/ui/side_panel/companion/companion_tab_helper.cc

+29
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "components/translate/core/common/translate_constants.h"
2121
#include "content/public/browser/web_contents.h"
2222
#include "net/base/url_util.h"
23+
#include "ui/base/models/image_model.h"
2324

2425
#if BUILDFLAG(ENABLE_LENS_DESKTOP_GOOGLE_BRANDED_FEATURES)
2526
#include "chrome/browser/lens/region_search/lens_region_search_controller.h"
@@ -233,6 +234,34 @@ void CompanionTabHelper::DidOpenRequestedURL(
233234
}
234235
}
235236

237+
void CompanionTabHelper::CreateAndRegisterLensEntry(
238+
const content::OpenURLParams& params,
239+
std::u16string combobox_label,
240+
const ui::ImageModel favicon) {
241+
delegate_->CreateAndRegisterLensEntry(params, combobox_label, favicon);
242+
}
243+
244+
void CompanionTabHelper::RemoveContextualLensView() {
245+
delegate_->RemoveContextualLensView();
246+
}
247+
248+
void CompanionTabHelper::OpenContextualLensView(
249+
const content::OpenURLParams& params) {
250+
delegate_->OpenContextualLensView(params);
251+
}
252+
253+
content::WebContents* CompanionTabHelper::GetLensViewWebContentsForTesting() {
254+
return delegate_->GetLensViewWebContentsForTesting(); // IN-TEST
255+
}
256+
257+
bool CompanionTabHelper::OpenLensResultsInNewTabForTesting() {
258+
return delegate_->OpenLensResultsInNewTabForTesting(); // IN-TEST
259+
}
260+
261+
bool CompanionTabHelper::IsLensLaunchButtonEnabledForTesting() {
262+
return delegate_->IsLensLaunchButtonEnabledForTesting(); // IN-TEST
263+
}
264+
236265
WEB_CONTENTS_USER_DATA_KEY_IMPL(CompanionTabHelper);
237266

238267
} // namespace companion

chrome/browser/ui/side_panel/companion/companion_tab_helper.h

+33
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ namespace content {
2020
class WebContents;
2121
} // namespace content
2222

23+
namespace ui {
24+
class ImageModel;
25+
} // namespace ui
26+
2327
namespace companion {
2428

2529
class CompanionPageHandler;
@@ -58,6 +62,23 @@ class CompanionTabHelper
5862
// loaded and the onload event was dispatched.
5963
virtual void AddCompanionFinishedLoadingCallback(
6064
base::OnceCallback<void()> callback) = 0;
65+
66+
// Create a contextual Lens entry.
67+
virtual void CreateAndRegisterLensEntry(
68+
const content::OpenURLParams& params,
69+
std::u16string combobox_label,
70+
const ui::ImageModel favicon) = 0;
71+
// Opens a contextual Lens view that was already created.
72+
virtual void OpenContextualLensView(
73+
const content::OpenURLParams& params) = 0;
74+
// Removes and deletes the contextual Lens view.
75+
virtual void RemoveContextualLensView() = 0;
76+
// Testing method to get the Lens view web contents.
77+
virtual content::WebContents* GetLensViewWebContentsForTesting() = 0;
78+
// Testing method to open lens results in a new tab.
79+
virtual bool OpenLensResultsInNewTabForTesting() = 0;
80+
// Testing method to check if the new tab button is enabled for Lens.
81+
virtual bool IsLensLaunchButtonEnabledForTesting() = 0;
6182
};
6283

6384
using CompanionLoadedCallback = base::OnceCallback<void()>;
@@ -143,6 +164,18 @@ class CompanionTabHelper
143164
absl::optional<SidePanelOpenTrigger>
144165
GetAndResetMostRecentSidePanelOpenTrigger();
145166

167+
// Create and register a contextual Lens entry.
168+
void CreateAndRegisterLensEntry(const content::OpenURLParams& params,
169+
std::u16string combobox_label,
170+
const ui::ImageModel favicon);
171+
// Open an already created contextual Lens view with provided params.
172+
void OpenContextualLensView(const content::OpenURLParams& params);
173+
// Delete the contextual lens view associated with this web contents.
174+
void RemoveContextualLensView();
175+
content::WebContents* GetLensViewWebContentsForTesting();
176+
bool OpenLensResultsInNewTabForTesting();
177+
bool IsLensLaunchButtonEnabledForTesting();
178+
146179
private:
147180
explicit CompanionTabHelper(content::WebContents* web_contents);
148181

chrome/browser/ui/views/side_panel/lens/lens_side_panel_coordinator.cc

+98-21
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "chrome/browser/history/history_service_factory.h"
1515
#include "chrome/browser/search/search.h"
1616
#include "chrome/browser/search_engines/template_url_service_factory.h"
17+
#include "chrome/browser/ui/side_panel/companion/companion_tab_helper.h"
18+
#include "chrome/browser/ui/side_panel/companion/companion_utils.h"
1719
#include "chrome/browser/ui/ui_features.h"
1820
#include "chrome/browser/ui/views/frame/browser_view.h"
1921
#include "chrome/browser/ui/views/side_panel/lens/lens_unified_side_panel_view.h"
@@ -30,6 +32,7 @@
3032
#include "components/search_engines/util.h"
3133
#include "components/vector_icons/vector_icons.h"
3234
#include "ui/base/l10n/l10n_util.h"
35+
#include "ui/base/ui_base_features.h"
3336
#include "ui/gfx/image/image.h"
3437
#include "ui/views/vector_icons.h"
3538

@@ -69,8 +72,27 @@ LensSidePanelCoordinator::~LensSidePanelCoordinator() {
6972

7073
void LensSidePanelCoordinator::DeregisterLensFromSidePanel() {
7174
lens_side_panel_view_ = nullptr;
72-
SidePanelCoordinator::GetGlobalSidePanelRegistry(&GetBrowser())
73-
->Deregister(SidePanelEntry::Key(SidePanelEntry::Id::kLens));
75+
auto* active_web_contents = GetBrowserView()->GetActiveWebContents();
76+
const bool is_companion_enabled =
77+
companion::IsSearchInCompanionSidePanelSupported(&GetBrowser());
78+
79+
// Delete contextual lens view if applicable.
80+
if (is_companion_enabled && active_web_contents) {
81+
auto* companion_helper =
82+
companion::CompanionTabHelper::FromWebContents(active_web_contents);
83+
if (companion_helper) {
84+
companion_helper->RemoveContextualLensView();
85+
}
86+
}
87+
88+
// Remove entry from side panel entry if it exists.
89+
auto* registry =
90+
is_companion_enabled
91+
? SidePanelRegistry::Get(active_web_contents)
92+
: SidePanelCoordinator::GetGlobalSidePanelRegistry(&GetBrowser());
93+
if (registry) {
94+
registry->Deregister(SidePanelEntry::Key(SidePanelEntry::Id::kLens));
95+
}
7496
}
7597

7698
void LensSidePanelCoordinator::OnSidePanelDidClose() {
@@ -80,13 +102,16 @@ void LensSidePanelCoordinator::OnSidePanelDidClose() {
80102
}
81103

82104
void LensSidePanelCoordinator::OnFaviconFetched(const gfx::Image& favicon) {
83-
auto* global_registry =
84-
SidePanelCoordinator::GetGlobalSidePanelRegistry(&GetBrowser());
85-
if (global_registry == nullptr)
105+
auto* registry =
106+
companion::IsSearchInCompanionSidePanelSupported(&GetBrowser())
107+
? SidePanelRegistry::Get(GetBrowserView()->GetActiveWebContents())
108+
: SidePanelCoordinator::GetGlobalSidePanelRegistry(&GetBrowser());
109+
if (registry == nullptr) {
86110
return;
111+
}
87112

88-
auto* lens_side_panel_entry = global_registry->GetEntryForKey(
89-
SidePanelEntry::Key(SidePanelEntry::Id::kLens));
113+
auto* lens_side_panel_entry =
114+
registry->GetEntryForKey(SidePanelEntry::Key(SidePanelEntry::Id::kLens));
90115
if (lens_side_panel_entry == nullptr)
91116
return;
92117

@@ -121,6 +146,12 @@ void LensSidePanelCoordinator::OnEntryHidden(SidePanelEntry* entry) {
121146
}
122147

123148
bool LensSidePanelCoordinator::IsLaunchButtonEnabledForTesting() {
149+
if (companion::IsSearchInCompanionSidePanelSupported(&GetBrowser())) {
150+
auto* companion_helper = companion::CompanionTabHelper::FromWebContents(
151+
GetBrowserView()->GetActiveWebContents());
152+
return companion_helper->IsLensLaunchButtonEnabledForTesting(); // IN-TEST
153+
}
154+
124155
DCHECK(lens_side_panel_view_);
125156
return lens_side_panel_view_->IsLaunchButtonEnabledForTesting();
126157
}
@@ -130,6 +161,13 @@ bool LensSidePanelCoordinator::IsDefaultSearchProviderGoogle() {
130161
}
131162

132163
std::u16string LensSidePanelCoordinator::GetComboboxLabel() {
164+
// If this panel was opened while the companion feature is enabled, then we
165+
// want this panel to be labelled like the companion panel.
166+
if (companion::IsSearchInCompanionSidePanelSupportedForProfile(
167+
GetBrowser().profile())) {
168+
return l10n_util::GetStringUTF16(IDS_SIDE_PANEL_COMPANION_TITLE);
169+
}
170+
133171
if (IsDefaultSearchProviderGoogle()) {
134172
return l10n_util::GetStringUTF16(IDS_GOOGLE_LENS_TITLE);
135173
}
@@ -140,6 +178,17 @@ std::u16string LensSidePanelCoordinator::GetComboboxLabel() {
140178
}
141179

142180
const ui::ImageModel LensSidePanelCoordinator::GetFaviconImage() {
181+
// If this panel was opened while the companion feature is enabled, then we
182+
// want this panel to use the companion panel favicon.
183+
if (companion::IsSearchInCompanionSidePanelSupportedForProfile(
184+
GetBrowser().profile())) {
185+
return ui::ImageModel::FromVectorIcon(
186+
features::IsChromeRefresh2023()
187+
? vector_icons::
188+
kGoogleSearchCompanionMonochromeLogoChromeRefreshIcon
189+
: vector_icons::kGoogleSearchCompanionMonochromeLogoIcon);
190+
}
191+
143192
// If google is search engine, return checked-in lens icon.
144193
if (IsDefaultSearchProviderGoogle())
145194
return ui::ImageModel::FromVectorIcon(vector_icons::kGoogleLensLogoIcon);
@@ -172,28 +221,46 @@ const ui::ImageModel LensSidePanelCoordinator::GetFaviconImage() {
172221
void LensSidePanelCoordinator::RegisterEntryAndShow(
173222
const content::OpenURLParams& params) {
174223
base::RecordAction(base::UserMetricsAction("LensUnifiedSidePanel.LensQuery"));
175-
auto* global_registry =
176-
SidePanelCoordinator::GetGlobalSidePanelRegistry(&GetBrowser());
224+
const bool is_companion_enabled =
225+
companion::IsSearchInCompanionSidePanelSupported(&GetBrowser());
226+
auto* companion_helper = companion::CompanionTabHelper::FromWebContents(
227+
GetBrowserView()->GetActiveWebContents());
228+
auto* registry =
229+
is_companion_enabled
230+
? SidePanelRegistry::Get(GetBrowserView()->GetActiveWebContents())
231+
: SidePanelCoordinator::GetGlobalSidePanelRegistry(&GetBrowser());
177232

178233
// check if the view is already registered
179-
if (global_registry->GetEntryForKey(
234+
if (registry->GetEntryForKey(
180235
SidePanelEntry::Key(SidePanelEntry::Id::kLens)) != nullptr &&
181-
lens_side_panel_view_ != nullptr) {
236+
(lens_side_panel_view_ != nullptr || is_companion_enabled)) {
182237
// The user issued a follow-up Lens query.
183238
base::RecordAction(
184239
base::UserMetricsAction("LensUnifiedSidePanel.LensQuery_Followup"));
185-
lens_side_panel_view_->OpenUrl(params);
240+
if (is_companion_enabled) {
241+
companion_helper->OpenContextualLensView(params);
242+
} else {
243+
lens_side_panel_view_->OpenUrl(params);
244+
}
186245
} else {
187246
base::RecordAction(
188247
base::UserMetricsAction("LensUnifiedSidePanel.LensQuery_New"));
189-
auto entry = std::make_unique<SidePanelEntry>(
190-
SidePanelEntry::Id::kLens, GetComboboxLabel(), GetFaviconImage(),
191-
base::BindRepeating(&LensSidePanelCoordinator::CreateLensWebView,
192-
base::Unretained(this), params),
193-
base::BindRepeating(&LensSidePanelCoordinator::GetOpenInNewTabURL,
194-
base::Unretained(this)));
195-
entry->AddObserver(this);
196-
global_registry->Register(std::move(entry));
248+
if (is_companion_enabled) {
249+
// Side panel entry needs to be created and registered
250+
// in the companion side panel controller that exists per web contents in
251+
// order to prevent a dependency on views on CompanionTabHelper.
252+
companion_helper->CreateAndRegisterLensEntry(params, GetComboboxLabel(),
253+
GetFaviconImage());
254+
} else {
255+
auto entry = std::make_unique<SidePanelEntry>(
256+
SidePanelEntry::Id::kLens, GetComboboxLabel(), GetFaviconImage(),
257+
base::BindRepeating(&LensSidePanelCoordinator::CreateLensWebView,
258+
base::Unretained(this), params),
259+
base::BindRepeating(&LensSidePanelCoordinator::GetOpenInNewTabURL,
260+
base::Unretained(this)));
261+
entry->AddObserver(this);
262+
registry->Register(std::move(entry));
263+
}
197264
}
198265

199266
auto* side_panel_coordinator = GetSidePanelCoordinator();
@@ -206,7 +273,6 @@ void LensSidePanelCoordinator::RegisterEntryAndShow(
206273
base::RecordAction(base::UserMetricsAction(
207274
"LensUnifiedSidePanel.LensQuery_SidePanelOpenNonLens"));
208275
}
209-
210276
side_panel_coordinator->Show(SidePanelEntry::Id::kLens,
211277
SidePanelOpenTrigger::kLensContextMenu);
212278
} else {
@@ -216,11 +282,22 @@ void LensSidePanelCoordinator::RegisterEntryAndShow(
216282
}
217283

218284
content::WebContents* LensSidePanelCoordinator::GetViewWebContentsForTesting() {
285+
if (companion::IsSearchInCompanionSidePanelSupported(&GetBrowser())) {
286+
auto* companion_helper = companion::CompanionTabHelper::FromWebContents(
287+
GetBrowserView()->GetActiveWebContents());
288+
return companion_helper->GetLensViewWebContentsForTesting(); // IN-TEST
289+
}
219290
return lens_side_panel_view_ ? lens_side_panel_view_->GetWebContents()
220291
: nullptr;
221292
}
222293

223294
bool LensSidePanelCoordinator::OpenResultsInNewTabForTesting() {
295+
if (companion::IsSearchInCompanionSidePanelSupported(&GetBrowser())) {
296+
auto* companion_helper = companion::CompanionTabHelper::FromWebContents(
297+
GetBrowserView()->GetActiveWebContents());
298+
return companion_helper->OpenLensResultsInNewTabForTesting(); // IN-TEST
299+
}
300+
224301
if (lens_side_panel_view_ == nullptr)
225302
return false;
226303

0 commit comments

Comments
 (0)