Skip to content

Commit

Permalink
[CSC] Save a pref if user navigated to exps success page
Browse files Browse the repository at this point in the history
This CL adds a pref if the user navigated to the exps registration
success page. This will be used to enable companion.

(cherry picked from commit df8cc7f)

Bug: b/285064611, 1449021
Change-Id: Ifa272a73c33ab49400cd89855b79c7dc37a80bd8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4583554
Reviewed-by: Tarun Bansal <tbansal@chromium.org>
Reviewed-by: Elly Fong-Jones <ellyjones@chromium.org>
Commit-Queue: Shakti Sahu <shaktisahu@chromium.org>
Cr-Original-Commit-Position: refs/heads/main@{#1155070}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4610163
Reviewed-by: Shakti Sahu <shaktisahu@chromium.org>
Commit-Queue: Tarun Bansal <tbansal@chromium.org>
Cr-Commit-Position: refs/branch-heads/5790@{#700}
Cr-Branched-From: 1d71a33-refs/heads/main@{#1148114}
  • Loading branch information
Shakti Sahu authored and Chromium LUCI CQ committed Jun 13, 2023
1 parent a0c15b6 commit 96d8cbe
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 3 deletions.
5 changes: 5 additions & 0 deletions chrome/browser/companion/core/constants.h
Expand Up @@ -20,6 +20,11 @@ const char kExpsPromoShownCountPref[] = "Companion.Promo.Exps.Shown.Count";
const char kExpsOptInStatusGrantedPref[] =
"Companion.Exps.OptIn.Status.Granted";

// Pref name used for tracking whether the user has ever successfully navigated
// to exps registration success page.
const char kHasNavigatedToExpsSuccessPage[] =
"Companion.HasNavigatedToExpsSuccessPage";

} // namespace companion

#endif // CHROME_BROWSER_COMPANION_CORE_CONSTANTS_H_
11 changes: 10 additions & 1 deletion chrome/browser/companion/core/features.cc
Expand Up @@ -17,6 +17,12 @@ namespace features {
BASE_FEATURE(kSidePanelCompanion,
"SidePanelCompanion",
base::FEATURE_DISABLED_BY_DEFAULT);
// Dynamically enables the search companion if the user has experiments
// enabled.
BASE_FEATURE(kCompanionEnabledByObservingExpsNavigations,
"CompanionEnabledByObservingExpsNavigations",
base::FEATURE_DISABLED_BY_DEFAULT);

constexpr base::FeatureParam<std::string> kHomepageURLForCompanion{
&kSidePanelCompanion, "companion-homepage-url",
"https://lens.google.com/companion"};
Expand All @@ -28,10 +34,13 @@ constexpr base::FeatureParam<bool> kEnableOpenCompanionForImageSearch{
&kSidePanelCompanion, "open-companion-for-image-search", true};
constexpr base::FeatureParam<bool> kEnableOpenCompanionForWebSearch{
&kSidePanelCompanion, "open-companion-for-web-search", true};

constexpr base::FeatureParam<bool> kOpenLinksInCurrentTab{
&kSidePanelCompanion, "open-links-in-current-tab", true};

constexpr base::FeatureParam<std::string> kExpsRegistrationSuccessPageURLs{
&kCompanionEnabledByObservingExpsNavigations,
"exps-registration-success-page-urls", ""};

} // namespace features

namespace switches {
Expand Down
2 changes: 2 additions & 0 deletions chrome/browser/companion/core/features.h
Expand Up @@ -14,8 +14,10 @@ namespace companion {
namespace features {

BASE_DECLARE_FEATURE(kSidePanelCompanion);
BASE_DECLARE_FEATURE(kCompanionEnabledByObservingExpsNavigations);
extern const base::FeatureParam<std::string> kHomepageURLForCompanion;
extern const base::FeatureParam<std::string> kImageUploadURLForCompanion;
extern const base::FeatureParam<std::string> kExpsRegistrationSuccessPageURLs;
extern const base::FeatureParam<bool> kEnableOpenCompanionForImageSearch;
extern const base::FeatureParam<bool> kEnableOpenCompanionForWebSearch;
extern const base::FeatureParam<bool> kOpenLinksInCurrentTab;
Expand Down
1 change: 1 addition & 0 deletions chrome/browser/companion/core/promo_handler.cc
Expand Up @@ -26,6 +26,7 @@ void PromoHandler::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterIntegerPref(kExpsPromoShownCountPref, 0);
// TODO(shaktisahu): Move the pref registration to a better location.
registry->RegisterBooleanPref(kExpsOptInStatusGrantedPref, false);
registry->RegisterBooleanPref(kHasNavigatedToExpsSuccessPage, false);
}

void PromoHandler::OnPromoAction(PromoType promo_type,
Expand Down
2 changes: 2 additions & 0 deletions chrome/browser/ui/BUILD.gn
Expand Up @@ -4446,6 +4446,8 @@ static_library("ui") {
"side_panel/companion/companion_tab_helper.h",
"side_panel/companion/companion_utils.cc",
"side_panel/companion/companion_utils.h",
"side_panel/companion/exps_registration_success_observer.cc",
"side_panel/companion/exps_registration_success_observer.h",
"side_panel/customize_chrome/customize_chrome_side_panel_controller_utils.h",
"side_panel/customize_chrome/customize_chrome_tab_helper.cc",
"side_panel/customize_chrome/customize_chrome_tab_helper.h",
Expand Down
@@ -0,0 +1,57 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/side_panel/companion/exps_registration_success_observer.h"

#include "base/strings/string_split.h"
#include "chrome/browser/companion/core/constants.h"
#include "chrome/browser/companion/core/features.h"
#include "chrome/browser/profiles/profile.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/web_contents.h"

namespace companion {

ExpsRegistrationSuccessObserver::ExpsRegistrationSuccessObserver(
content::WebContents* web_contents)
: content::WebContentsObserver(web_contents),
content::WebContentsUserData<ExpsRegistrationSuccessObserver>(
*web_contents) {
const auto& url_strings_to_match = base::SplitString(
companion::features::kExpsRegistrationSuccessPageURLs.Get(), ",",
base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
for (const auto& url_string : url_strings_to_match) {
urls_to_match_against_.emplace_back(url_string);
}
}

ExpsRegistrationSuccessObserver::~ExpsRegistrationSuccessObserver() = default;

void ExpsRegistrationSuccessObserver::PrimaryPageChanged(content::Page& page) {
PrefService* pref_service =
Profile::FromBrowserContext(web_contents()->GetBrowserContext())
->GetPrefs();
if (pref_service->GetBoolean(kHasNavigatedToExpsSuccessPage)) {
return;
}
bool matches_exps_url = false;
const GURL& url = page.GetMainDocument().GetLastCommittedURL();
for (const auto& url_to_match : urls_to_match_against_) {
if (url == url_to_match) {
matches_exps_url = true;
break;
}
}

if (!matches_exps_url) {
return;
}

// Save the status to a pref.
pref_service->SetBoolean(kHasNavigatedToExpsSuccessPage, true);
}

WEB_CONTENTS_USER_DATA_KEY_IMPL(ExpsRegistrationSuccessObserver);

} // namespace companion
@@ -0,0 +1,50 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_SIDE_PANEL_COMPANION_EXPS_REGISTRATION_SUCCESS_OBSERVER_H_
#define CHROME_BROWSER_UI_SIDE_PANEL_COMPANION_EXPS_REGISTRATION_SUCCESS_OBSERVER_H_

#include <vector>

#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
#include "url/gurl.h"

namespace content {
class Page;
class WebContents;
} // namespace content

namespace companion {

// An observer that observes page navigations on a tab and determines if the
// user has laned on the success page of exps registration. .
class ExpsRegistrationSuccessObserver
: public content::WebContentsObserver,
public content::WebContentsUserData<ExpsRegistrationSuccessObserver> {
public:
explicit ExpsRegistrationSuccessObserver(content::WebContents* web_contents);
~ExpsRegistrationSuccessObserver() override;

// Disallow copy/assign.
ExpsRegistrationSuccessObserver(const ExpsRegistrationSuccessObserver&) =
delete;
ExpsRegistrationSuccessObserver& operator=(
const ExpsRegistrationSuccessObserver&) = delete;

private:
friend class content::WebContentsUserData<ExpsRegistrationSuccessObserver>;

// content::WebContentsObserver overrides.
void PrimaryPageChanged(content::Page& page) override;

// The list of URLs to search for a match.
std::vector<GURL> urls_to_match_against_;

WEB_CONTENTS_USER_DATA_KEY_DECL();
};

} // namespace companion

#endif // CHROME_BROWSER_UI_SIDE_PANEL_COMPANION_EXPS_REGISTRATION_SUCCESS_OBSERVER_H_
6 changes: 6 additions & 0 deletions chrome/browser/ui/tab_helpers.cc
Expand Up @@ -177,6 +177,7 @@
#include "chrome/browser/ui/sad_tab_helper.h"
#include "chrome/browser/ui/search/search_tab_helper.h"
#include "chrome/browser/ui/side_panel/companion/companion_tab_helper.h"
#include "chrome/browser/ui/side_panel/companion/exps_registration_success_observer.h"
#include "chrome/browser/ui/side_panel/customize_chrome/customize_chrome_tab_helper.h"
#include "chrome/browser/ui/side_panel/customize_chrome/customize_chrome_utils.h"
#include "chrome/browser/ui/side_panel/history_clusters/history_clusters_tab_helper.h"
Expand Down Expand Up @@ -526,6 +527,11 @@ void TabHelpers::AttachTabHelpers(WebContents* web_contents) {
if (base::FeatureList::IsEnabled(companion::features::kSidePanelCompanion)) {
companion::CompanionTabHelper::CreateForWebContents(web_contents);
}
if (base::FeatureList::IsEnabled(
companion::features::kCompanionEnabledByObservingExpsNavigations)) {
companion::ExpsRegistrationSuccessObserver::CreateForWebContents(
web_contents);
}
#endif

#if BUILDFLAG(IS_MAC)
Expand Down
Expand Up @@ -72,6 +72,7 @@ const char kSearchQueryUrl[] = "https://www.google.com/search?q=xyz";

const char kExpectedExpsPromoUrl[] = "https://foobar.com/";
const char kPhReportingUrl[] = "https://foobar.com/";
const char kExpsRegistrationSuccessUrl[] = "https://foobar.com/experiments";

} // namespace

Expand Down Expand Up @@ -370,8 +371,21 @@ class CompanionPageBrowserTest : public InProcessBrowserTest {
params["companion-image-upload-url"] =
companion_server_.GetURL("/upload").spec();
params["open-links-in-current-tab"] = ShouldOpenLinkInCurrentTab();
feature_list_.InitAndEnableFeatureWithParameters(
companion::features::kSidePanelCompanion, params);
base::FieldTrialParams params2;
params2["exps-registration-success-page-urls"] =
kExpsRegistrationSuccessUrl;

std::vector<base::test::FeatureRefAndParams> enabled_features;
if (enable_feature_side_panel_companion_) {
enabled_features.emplace_back(companion::features::kSidePanelCompanion,
params);
}
enabled_features.emplace_back(
companion::features::kCompanionEnabledByObservingExpsNavigations,
params2);

feature_list_.InitWithFeaturesAndParameters(enabled_features,
/*disabled_features=*/{});
}

virtual std::string ShouldOpenLinkInCurrentTab() { return "false"; }
Expand Down Expand Up @@ -445,6 +459,7 @@ class CompanionPageBrowserTest : public InProcessBrowserTest {
size_t requests_received_on_server_ = 0;
std::string last_sourcelang_;
std::string last_targetlang_;
bool enable_feature_side_panel_companion_ = true;
};

IN_PROC_BROWSER_TEST_F(CompanionPageBrowserTest, InitialNavigationWithoutMsbb) {
Expand Down Expand Up @@ -1228,6 +1243,29 @@ IN_PROC_BROWSER_TEST_F(CompanionPageBrowserTest,
static_cast<int>(SidePanelOpenTrigger::kPinnedEntryToolbarButton));
}

class CompanionPageDisabledBrowserTest : public CompanionPageBrowserTest {
public:
CompanionPageDisabledBrowserTest() : CompanionPageBrowserTest() {
enable_feature_side_panel_companion_ = false;
}
};

IN_PROC_BROWSER_TEST_F(CompanionPageDisabledBrowserTest,
ObservesExpsRegistrationSuccessURL) {
// Navigate to a random page.
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), CreateUrl(kHost, kRelativeUrl1)));
EXPECT_FALSE(browser()->profile()->GetPrefs()->GetBoolean(
companion::kHasNavigatedToExpsSuccessPage));

// Navigate to exps registration success page. It should enable the pref.
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(),
GURL(kExpsRegistrationSuccessUrl)));
// Verify that the pref.
EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
companion::kHasNavigatedToExpsSuccessPage));
}

class CompanionPagePolicyBrowserTest : public CompanionPageBrowserTest {
public:
void EnableCompanionByPolicy(bool enable_companion_by_policy) {
Expand Down

0 comments on commit 96d8cbe

Please sign in to comment.