Skip to content

Commit

Permalink
[ConsentV2] Integrate the consent dialog with the Chrome Cart
Browse files Browse the repository at this point in the history
Demo: https://screencast.googleplex.com/cast/NDYzOTQxNjUwMTkyNzkzNnxkZjRiYWNkNC01NQ

(cherry picked from commit bb4147a)

Change-Id: I30445980da486baae27d75a86d6b13bab3811b3d
Bug: 1317519
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3631228
Reviewed-by: Peter Kasting <pkasting@chromium.org>
Reviewed-by: Wei-Yin Chen <wychen@chromium.org>
Commit-Queue: Mei Liang <meiliang@chromium.org>
Cr-Original-Commit-Position: refs/heads/main@{#1007202}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3679887
Owners-Override: Prudhvikumar Bommana <pbommana@google.com>
Reviewed-by: Prudhvikumar Bommana <pbommana@google.com>
Cr-Commit-Position: refs/branch-heads/5060@{#432}
Cr-Branched-From: b83393d-refs/heads/main@{#1002911}
  • Loading branch information
Mei Liang authored and Chromium LUCI CQ committed May 31, 2022
1 parent 780a34a commit 2813799
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 21 deletions.
10 changes: 7 additions & 3 deletions chrome/browser/cart/cart_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
#include "chrome/browser/cart/cart_db_content.pb.h"
#include "chrome/browser/cart/cart_service.h"
#include "chrome/browser/cart/cart_service_factory.h"
#include "chrome/browser/ui/browser_finder.h"
#include "components/commerce/core/commerce_feature_list.h"
#include "components/search/ntp_features.h"

CartHandler::CartHandler(
mojo::PendingReceiver<chrome_cart::mojom::CartHandler> handler,
Profile* profile)
Profile* profile,
content::WebContents* web_contents)
: handler_(this, std::move(handler)),
cart_service_(CartServiceFactory::GetForProfile(profile)) {}
cart_service_(CartServiceFactory::GetForProfile(profile)),
web_contents_(web_contents) {}

CartHandler::~CartHandler() = default;

Expand Down Expand Up @@ -128,7 +131,8 @@ void CartHandler::OnDiscountConsentContinued() {
void CartHandler::ShowNativeConsentDialog(
ShowNativeConsentDialogCallback callback) {
cart_service_->InterestedInDiscountConsent();
cart_service_->ShowNativeConsentDialog(std::move(callback));
cart_service_->ShowNativeConsentDialog(
chrome::FindBrowserWithWebContents(web_contents_), std::move(callback));
}

void CartHandler::GetDiscountEnabled(GetDiscountEnabledCallback callback) {
Expand Down
8 changes: 7 additions & 1 deletion chrome/browser/cart/cart_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@

class Profile;

namespace content {
class WebContents;
} // namespace content

// Handles requests of chrome cart module sent from JS.
class CartHandler : public chrome_cart::mojom::CartHandler {
public:
CartHandler(mojo::PendingReceiver<chrome_cart::mojom::CartHandler> handler,
Profile* profile);
Profile* profile,
content::WebContents* web_contents);
~CartHandler() override;

// chrome_cart::mojom::CartHandler:
Expand Down Expand Up @@ -52,6 +57,7 @@ class CartHandler : public chrome_cart::mojom::CartHandler {
std::vector<CartDB::KeyAndValue> res);
mojo::Receiver<chrome_cart::mojom::CartHandler> handler_;
raw_ptr<CartService> cart_service_;
raw_ptr<content::WebContents> web_contents_;
base::WeakPtrFactory<CartHandler> weak_factory_{this};
};

Expand Down
8 changes: 6 additions & 2 deletions chrome/browser/cart/cart_handler_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "components/prefs/pref_service.h"
#include "components/search/ntp_features.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_web_contents_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

Expand Down Expand Up @@ -106,10 +107,11 @@ class CartHandlerTest : public testing::Test {
HistoryServiceFactory::GetInstance(),
HistoryServiceFactory::GetDefaultFactory());
profile_ = profile_builder.Build();
web_contents_ = web_contents_factory_.CreateWebContents(profile_.get());

handler_ = std::make_unique<CartHandler>(
mojo::PendingReceiver<chrome_cart::mojom::CartHandler>(),
profile_.get());
profile_.get(), web_contents_);
service_ = CartServiceFactory::GetForProfile(profile_.get());
}

Expand Down Expand Up @@ -159,6 +161,8 @@ class CartHandlerTest : public testing::Test {
// Required to run tests from UI thread.
content::BrowserTaskEnvironment task_environment_;
std::unique_ptr<TestingProfile> profile_;
content::TestWebContentsFactory web_contents_factory_;
raw_ptr<content::WebContents> web_contents_; // Weak. Owned by factory_.
std::unique_ptr<CartHandler> handler_;
raw_ptr<CartService> service_;
base::HistogramTester histogram_tester_;
Expand Down Expand Up @@ -656,7 +660,7 @@ TEST_F(CartHandlerCartURLUTMTest, TestAppendUTMToPartnerMerchant) {
run_loop[0].Run();

// Verifies UTM tags for when discount is enabled.
handler_->SetDiscountEnabled(true);
profile_->GetPrefs()->SetBoolean(prefs::kCartDiscountEnabled, true);
ASSERT_TRUE(service_->IsCartDiscountEnabled());
handler_->GetMerchantCarts(base::BindOnce(
&GetEvaluationMerchantCartWithUtmSource, run_loop[1].QuitClosure(), true,
Expand Down
9 changes: 5 additions & 4 deletions chrome/browser/cart/cart_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/commerce/commerce_prompt.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/browser_resources.h"
#include "chrome/grit/generated_resources.h"
Expand Down Expand Up @@ -1149,10 +1151,9 @@ void CartService::SetCouponServiceForTesting(CouponService* coupon_service) {
}

void CartService::ShowNativeConsentDialog(
Browser* browser,
base::OnceCallback<void(chrome_cart::mojom::ConsentStatus)>
consent_status_callback) {
// TODO(crbug.com/1317519): Show the native dialog and move the
// consent_status_callback to the dialog.
std::move(consent_status_callback)
.Run(chrome_cart::mojom::ConsentStatus::ACCEPTED);
commerce::ShowDiscountConsentPrompt(browser,
std::move(consent_status_callback));
}
3 changes: 3 additions & 0 deletions chrome/browser/cart/cart_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "components/prefs/pref_registry_simple.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

#include "chrome/browser/ui/browser.h"

class DiscountURLLoader;
class FetchDiscountWorker;

Expand Down Expand Up @@ -137,6 +139,7 @@ class CartService : public history::HistoryServiceObserver,
// This is used when the NativeDialog variation is active. It gets called
// when user has clicked the 'Continue' button in the discount consent.
void ShowNativeConsentDialog(
Browser* browser,
base::OnceCallback<void(chrome_cart::mojom::ConsentStatus)>
consent_status_callback);
// Appends UTM tags to the end of |base_url|. It will always append
Expand Down
1 change: 1 addition & 0 deletions chrome/browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,7 @@ static_library("ui") {
"commander/tab_command_source.h",
"commander/window_command_source.cc",
"commander/window_command_source.h",
"commerce/commerce_prompt.h",
"confirm_bubble_model.cc",
"confirm_bubble_model.h",
"content_settings/content_setting_bubble_model.cc",
Expand Down
3 changes: 3 additions & 0 deletions chrome/browser/ui/commerce/OWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
meiliang@chromium.org

file://components/commerce/OWNERS
19 changes: 19 additions & 0 deletions chrome/browser/ui/commerce/commerce_prompt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_COMMERCE_COMMERCE_PROMPT_H_
#define CHROME_BROWSER_UI_COMMERCE_COMMERCE_PROMPT_H_

#include "chrome/browser/cart/chrome_cart.mojom.h"

class Browser;

namespace commerce {
// Factory function to create and show the discount consent prompts for chrome.
void ShowDiscountConsentPrompt(
Browser* browser,
base::OnceCallback<void(chrome_cart::mojom::ConsentStatus)> callback);
} // namespace commerce

#endif // CHROME_BROWSER_UI_COMMERCE_COMMERCE_PROMPT_H_
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "ui/views/widget/widget.h"
#include "ui/views/window/dialog_delegate.h"

#include "chrome/browser/ui/commerce/commerce_prompt.h"
#include "chrome/browser/ui/views/accessibility/theme_tracking_non_accessible_image_view.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/chrome_typography.h"
Expand All @@ -27,14 +28,16 @@ namespace {
constexpr int kChildSpacing = 24;
} // namespace

// static
void NtpDiscountConsentDialogView::Show(Browser* browser,
ActionCallback callback) {
namespace commerce {
void ShowDiscountConsentPrompt(
Browser* browser,
base::OnceCallback<void(chrome_cart::mojom::ConsentStatus)> callback) {
constrained_window::CreateBrowserModalDialogViews(
std::make_unique<NtpDiscountConsentDialogView>(std::move(callback)),
browser->window()->GetNativeWindow())
->Show();
}
} // namespace commerce

NtpDiscountConsentDialogView::NtpDiscountConsentDialogView(
ActionCallback callback)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
#include "chrome/browser/cart/chrome_cart.mojom.h"
#include "ui/views/window/dialog_delegate.h"

class Browser;

class NtpDiscountConsentDialogView : public views::DialogDelegateView {
public:
using ActionCallback =
base::OnceCallback<void(chrome_cart::mojom::ConsentStatus)>;
static void Show(Browser* browser, ActionCallback callback);
explicit NtpDiscountConsentDialogView(ActionCallback callback);
~NtpDiscountConsentDialogView() override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/commerce/ntp_discount_consent_dialog_view.h"

#include "chrome/browser/ui/commerce/commerce_prompt.h"
#include "chrome/browser/ui/test/test_browser_dialog.h"
#include "content/public/test/browser_test.h"

Expand All @@ -19,7 +18,7 @@ class NtpDiscountConsentDialogViewBrowserTest : public DialogBrowserTest {

// DialogBrowserTest:
void ShowUi(const std::string& name) override {
NtpDiscountConsentDialogView::Show(
commerce::ShowDiscountConsentPrompt(
browser(),
base::BindOnce([](chrome_cart::mojom::ConsentStatus status) {}));
}
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/ui/webui/new_tab_page/new_tab_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,8 @@ void NewTabPageUI::BindInterface(
void NewTabPageUI::BindInterface(
mojo::PendingReceiver<chrome_cart::mojom::CartHandler>
pending_page_handler) {
cart_handler_ =
std::make_unique<CartHandler>(std::move(pending_page_handler), profile_);
cart_handler_ = std::make_unique<CartHandler>(std::move(pending_page_handler),
profile_, web_contents_);
}

void NewTabPageUI::CreatePageHandler(
Expand Down

0 comments on commit 2813799

Please sign in to comment.