Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CustomizeChrome: Create Customize Chrome Tab Helper and Controller
This CL creates a tab helper and controller for the customize chrome side panel. These classes will help delegate responsibilities for registering and showing the customize chrome side panel on the NTP. The controller class contains the logic for registering/deregistering the customize chrome entry. The actual registering of the side panel when on the NTP and triggering from the "Customize Chrome" button is in this follow-up CL: https://chromium-review.googlesource.com/c/chromium/src/+/3812725 Bug: 1350246 Change-Id: I45c21db07e50e64dddf5d6ebf60145d379c812ae Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3806854 Commit-Queue: Nihar Majmudar <niharm@google.com> Reviewed-by: Thomas Lukaszewicz <tluk@chromium.org> Reviewed-by: Avi Drissman <avi@chromium.org> Reviewed-by: John Lee <johntlee@chromium.org> Cr-Commit-Position: refs/heads/main@{#1034056}
- Loading branch information
Nihar Majmudar
authored and
Chromium LUCI CQ
committed
Aug 11, 2022
1 parent
e41269b
commit b938b59
Showing
17 changed files
with
329 additions
and
1 deletion.
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
1 change: 1 addition & 0 deletions
1
chrome/app/generated_resources_grd/IDS_SIDE_PANEL_CUSTOMIZE_CHROME_TITLE.png.sha1
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 @@ | ||
9b6e2f21db794747e0a7052282b9e9abb972505d |
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
14 changes: 14 additions & 0 deletions
14
chrome/browser/ui/side_panel/customize_chrome/customize_chrome_side_panel_controller_utils.h
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,14 @@ | ||
// 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_SIDE_PANEL_CUSTOMIZE_CHROME_CUSTOMIZE_CHROME_SIDE_PANEL_CONTROLLER_UTILS_H_ | ||
#define CHROME_BROWSER_UI_SIDE_PANEL_CUSTOMIZE_CHROME_CUSTOMIZE_CHROME_SIDE_PANEL_CONTROLLER_UTILS_H_ | ||
|
||
#include "chrome/browser/ui/side_panel/customize_chrome/customize_chrome_tab_helper.h" | ||
|
||
namespace customize_chrome { | ||
std::unique_ptr<CustomizeChromeTabHelper::Delegate> CreateDelegate(); | ||
} // namespace customize_chrome | ||
|
||
#endif // CHROME_BROWSER_UI_SIDE_PANEL_CUSTOMIZE_CHROME_CUSTOMIZE_CHROME_SIDE_PANEL_CONTROLLER_UTLS_H_ |
26 changes: 26 additions & 0 deletions
26
chrome/browser/ui/side_panel/customize_chrome/customize_chrome_tab_helper.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,26 @@ | ||
// 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. | ||
|
||
#include "chrome/browser/ui/side_panel/customize_chrome/customize_chrome_tab_helper.h" | ||
|
||
#include "chrome/browser/ui/side_panel/customize_chrome/customize_chrome_side_panel_controller_utils.h" | ||
|
||
CustomizeChromeTabHelper::~CustomizeChromeTabHelper() = default; | ||
|
||
void CustomizeChromeTabHelper::CreateAndRegisterEntry() { | ||
DCHECK(delegate_); | ||
delegate_->CreateAndRegisterEntry(&WebContentsUserData::GetWebContents()); | ||
} | ||
|
||
void CustomizeChromeTabHelper::DeregisterEntry() { | ||
DCHECK(delegate_); | ||
delegate_->DeregisterEntry(&WebContentsUserData::GetWebContents()); | ||
} | ||
|
||
CustomizeChromeTabHelper::CustomizeChromeTabHelper( | ||
content::WebContents* web_contents) | ||
: content::WebContentsUserData<CustomizeChromeTabHelper>(*web_contents), | ||
delegate_(customize_chrome::CreateDelegate()) {} | ||
|
||
WEB_CONTENTS_USER_DATA_KEY_IMPL(CustomizeChromeTabHelper); |
49 changes: 49 additions & 0 deletions
49
chrome/browser/ui/side_panel/customize_chrome/customize_chrome_tab_helper.h
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,49 @@ | ||
// 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_SIDE_PANEL_CUSTOMIZE_CHROME_CUSTOMIZE_CHROME_TAB_HELPER_H_ | ||
#define CHROME_BROWSER_UI_SIDE_PANEL_CUSTOMIZE_CHROME_CUSTOMIZE_CHROME_TAB_HELPER_H_ | ||
|
||
#include "content/public/browser/web_contents_user_data.h" | ||
|
||
namespace content { | ||
class WebContents; | ||
} // namespace content | ||
|
||
// An observer of WebContents that facilitates the logic for the customize | ||
// chrome side panel. This per-tab class also owns the | ||
// CustomizeChromeSidePanelController. | ||
class CustomizeChromeTabHelper | ||
: public content::WebContentsUserData<CustomizeChromeTabHelper> { | ||
public: | ||
// This class delegates the responsibility for registering and deregistering | ||
// the Customize Chrome side panel. | ||
class Delegate { | ||
public: | ||
virtual void CreateAndRegisterEntry(content::WebContents* web_contents) = 0; | ||
virtual void DeregisterEntry(content::WebContents* web_contents) = 0; | ||
virtual ~Delegate() = default; | ||
}; | ||
|
||
CustomizeChromeTabHelper(const CustomizeChromeTabHelper&) = delete; | ||
CustomizeChromeTabHelper& operator=(const CustomizeChromeTabHelper&) = delete; | ||
~CustomizeChromeTabHelper() override; | ||
|
||
// Creates a WebUISidePanelView for Customize Chrome and registers | ||
// the customize chrome side panel entry. | ||
void CreateAndRegisterEntry(); | ||
|
||
// Deregisters the customize chrome side panel entry. | ||
void DeregisterEntry(); | ||
|
||
private: | ||
friend class content::WebContentsUserData<CustomizeChromeTabHelper>; | ||
explicit CustomizeChromeTabHelper(content::WebContents* web_contents); | ||
|
||
std::unique_ptr<Delegate> delegate_; | ||
|
||
WEB_CONTENTS_USER_DATA_KEY_DECL(); | ||
}; | ||
|
||
#endif // CHROME_BROWSER_UI_SIDE_PANEL_CUSTOMIZE_CHROME_CUSTOMIZE_CHROME_TAB_HELPER_H_ |
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
65 changes: 65 additions & 0 deletions
65
...me/browser/ui/views/side_panel/customize_chrome/customize_chrome_side_panel_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,65 @@ | ||
// 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. | ||
|
||
#include "chrome/browser/ui/views/side_panel/customize_chrome/customize_chrome_side_panel_controller.h" | ||
|
||
#include "base/bind.h" | ||
#include "base/callback.h" | ||
#include "chrome/app/vector_icons/vector_icons.h" | ||
#include "chrome/browser/profiles/profile.h" | ||
#include "chrome/browser/ui/side_panel/customize_chrome/customize_chrome_tab_helper.h" | ||
#include "chrome/browser/ui/views/side_panel/side_panel_coordinator.h" | ||
#include "chrome/browser/ui/views/side_panel/side_panel_registry_observer.h" | ||
#include "chrome/browser/ui/views/side_panel/side_panel_web_ui_view.h" | ||
#include "chrome/browser/ui/webui/side_panel/customize_chrome/customize_chrome_ui.h" | ||
#include "chrome/common/webui_url_constants.h" | ||
#include "chrome/grit/generated_resources.h" | ||
#include "components/omnibox/browser/vector_icons.h" | ||
#include "ui/base/l10n/l10n_util.h" | ||
|
||
CustomizeChromeSidePanelController::CustomizeChromeSidePanelController() = | ||
default; | ||
|
||
CustomizeChromeSidePanelController::~CustomizeChromeSidePanelController() = | ||
default; | ||
|
||
void CustomizeChromeSidePanelController::CreateAndRegisterEntry( | ||
content::WebContents* web_contents) { | ||
auto* registry = SidePanelRegistry::Get(web_contents); | ||
|
||
if (!registry) | ||
return; | ||
|
||
auto entry = std::make_unique<SidePanelEntry>( | ||
SidePanelEntry::Id::kCustomizeChrome, | ||
l10n_util::GetStringUTF16(IDS_SIDE_PANEL_CUSTOMIZE_CHROME_TITLE), | ||
// Icon needs to be changed to customize chrome icon. | ||
ui::ImageModel::FromVectorIcon(omnibox::kStarIcon, ui::kColorIcon), | ||
base::BindRepeating( | ||
&CustomizeChromeSidePanelController::CreateCustomizeChromeWebView, | ||
base::Unretained(this), web_contents)); | ||
registry->Register(std::move(entry)); | ||
} | ||
|
||
void CustomizeChromeSidePanelController::DeregisterEntry( | ||
content::WebContents* web_contents) { | ||
if (auto* registry = SidePanelRegistry::Get(web_contents)) | ||
registry->Deregister(SidePanelEntry::Id::kCustomizeChrome); | ||
} | ||
|
||
std::unique_ptr<views::View> | ||
CustomizeChromeSidePanelController::CreateCustomizeChromeWebView( | ||
content::WebContents* web_contents) { | ||
auto customize_chrome_web_view = | ||
std::make_unique<SidePanelWebUIViewT<CustomizeChromeUI>>( | ||
base::RepeatingClosure(), base::RepeatingClosure(), | ||
std::make_unique<BubbleContentsWrapperT<CustomizeChromeUI>>( | ||
GURL(chrome::kChromeUICustomizeChromeSidePanelURL), | ||
Profile::FromBrowserContext(web_contents->GetBrowserContext()), | ||
IDS_SIDE_PANEL_CUSTOMIZE_CHROME_TITLE, | ||
/*webui_resizes_host=*/false, | ||
/*esc_closes_ui=*/false)); | ||
customize_chrome_web_view->ShowUI(); | ||
return customize_chrome_web_view; | ||
} |
37 changes: 37 additions & 0 deletions
37
chrome/browser/ui/views/side_panel/customize_chrome/customize_chrome_side_panel_controller.h
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,37 @@ | ||
// 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_VIEWS_SIDE_PANEL_CUSTOMIZE_CHROME_CUSTOMIZE_CHROME_SIDE_PANEL_CONTROLLER_H_ | ||
#define CHROME_BROWSER_UI_VIEWS_SIDE_PANEL_CUSTOMIZE_CHROME_CUSTOMIZE_CHROME_SIDE_PANEL_CONTROLLER_H_ | ||
|
||
#include "chrome/browser/ui/side_panel/customize_chrome/customize_chrome_tab_helper.h" | ||
#include "chrome/browser/ui/views/side_panel/side_panel_entry_observer.h" | ||
|
||
namespace content { | ||
class WebContents; | ||
} // namespace content | ||
|
||
// Responsible for implementing logic to create and register/deregister | ||
// the side panel. | ||
class CustomizeChromeSidePanelController | ||
: public CustomizeChromeTabHelper::Delegate { | ||
public: | ||
CustomizeChromeSidePanelController(); | ||
CustomizeChromeSidePanelController( | ||
const CustomizeChromeSidePanelController&) = delete; | ||
CustomizeChromeSidePanelController& operator=( | ||
const CustomizeChromeSidePanelController&) = delete; | ||
~CustomizeChromeSidePanelController() override; | ||
|
||
// CustomizeChromeTabHelper::Delegate | ||
void CreateAndRegisterEntry(content::WebContents* web_contents) override; | ||
void DeregisterEntry(content::WebContents* web_contents) override; | ||
|
||
private: | ||
// Creates view for side panel entry. | ||
std::unique_ptr<views::View> CreateCustomizeChromeWebView( | ||
content::WebContents* web_contents); | ||
}; | ||
|
||
#endif // CHROME_BROWSER_UI_VIEWS_SIDE_PANEL_CUSTOMIZE_CHROME_CUSTOMIZE_CHROME_SIDE_PANEL_CONTROLLER_H_ |
89 changes: 89 additions & 0 deletions
89
...r/ui/views/side_panel/customize_chrome/customize_chrome_side_panel_controller_unittest.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,89 @@ | ||
// Copyright 2021 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. | ||
|
||
#include "chrome/browser/ui/views/side_panel/customize_chrome/customize_chrome_side_panel_controller.h" | ||
|
||
#include "base/test/scoped_feature_list.h" | ||
#include "chrome/browser/ui/ui_features.h" | ||
#include "chrome/browser/ui/views/side_panel/side_panel_registry.h" | ||
#include "chrome/test/base/testing_profile.h" | ||
#include "components/search/ntp_features.h" | ||
#include "content/public/test/browser_task_environment.h" | ||
#include "content/public/test/test_renderer_host.h" | ||
#include "content/public/test/web_contents_tester.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
class CustomizeChromeSidePanelControllerTest : public testing::Test { | ||
public: | ||
// testing::Test: | ||
void SetUp() override { | ||
testing::Test::SetUp(); | ||
scoped_feature_list_.InitWithFeatures( | ||
{ntp_features::kCustomizeChromeSidePanel, features::kUnifiedSidePanel}, | ||
{}); | ||
web_contents_ = | ||
content::WebContentsTester::CreateTestWebContents(&profile_, nullptr); | ||
} | ||
|
||
protected: | ||
content::WebContents* get_web_contents() { return web_contents_.get(); } | ||
|
||
private: | ||
content::BrowserTaskEnvironment task_environment_; | ||
content::RenderViewHostTestEnabler rvh_enabler_; | ||
TestingProfile profile_; | ||
std::unique_ptr<content::WebContents> web_contents_; | ||
base::test::ScopedFeatureList scoped_feature_list_; | ||
}; | ||
|
||
TEST_F(CustomizeChromeSidePanelControllerTest, RegisterCustomizeChromeEntry) { | ||
// When CreateAndRegisterEntry() is called, the current tabs side | ||
// panel registry should contain a kCustomizeChromeEntry. | ||
CustomizeChromeSidePanelController side_panel_controller; | ||
content::WebContents* web_contents = get_web_contents(); | ||
side_panel_controller.CreateAndRegisterEntry(web_contents); | ||
auto* registry = SidePanelRegistry::Get(web_contents); | ||
EXPECT_EQ(registry->GetEntryForId(SidePanelEntry::Id::kCustomizeChrome)->id(), | ||
SidePanelEntry::Id::kCustomizeChrome); | ||
} | ||
|
||
TEST_F(CustomizeChromeSidePanelControllerTest, DeregisterCustomizeChromeEntry) { | ||
// When Deregister() is called, there should be no side panel entry | ||
// in the registry. | ||
CustomizeChromeSidePanelController side_panel_controller; | ||
content::WebContents* web_contents = get_web_contents(); | ||
side_panel_controller.CreateAndRegisterEntry(web_contents); | ||
auto* registry = SidePanelRegistry::Get(web_contents); | ||
EXPECT_EQ(registry->GetEntryForId(SidePanelEntry::Id::kCustomizeChrome)->id(), | ||
SidePanelEntry::Id::kCustomizeChrome); | ||
side_panel_controller.DeregisterEntry(web_contents); | ||
EXPECT_EQ(registry->GetEntryForId(SidePanelEntry::Id::kCustomizeChrome), | ||
nullptr); | ||
} | ||
|
||
TEST_F(CustomizeChromeSidePanelControllerTest, CreateAndRegisterMultipleTimes) { | ||
// When CreateAndRegisterEntry() is called multiple times, only | ||
// one entry should be added to the registry. | ||
CustomizeChromeSidePanelController side_panel_controller; | ||
content::WebContents* web_contents = get_web_contents(); | ||
side_panel_controller.CreateAndRegisterEntry(web_contents); | ||
auto* registry = SidePanelRegistry::Get(web_contents); | ||
EXPECT_EQ(registry->GetEntryForId(SidePanelEntry::Id::kCustomizeChrome)->id(), | ||
SidePanelEntry::Id::kCustomizeChrome); | ||
side_panel_controller.CreateAndRegisterEntry(web_contents); | ||
EXPECT_EQ(registry->GetEntryForId(SidePanelEntry::Id::kCustomizeChrome)->id(), | ||
SidePanelEntry::Id::kCustomizeChrome); | ||
side_panel_controller.DeregisterEntry(web_contents); | ||
EXPECT_EQ(registry->GetEntryForId(SidePanelEntry::Id::kCustomizeChrome), | ||
nullptr); | ||
} | ||
|
||
TEST_F(CustomizeChromeSidePanelControllerTest, | ||
DeregisterEmptyCustomizeChromeEntry) { | ||
// When there is no customize chrome entry, calling deregister should | ||
// not crash. | ||
CustomizeChromeSidePanelController side_panel_controller; | ||
content::WebContents* web_contents = get_web_contents(); | ||
side_panel_controller.DeregisterEntry(web_contents); | ||
} |
15 changes: 15 additions & 0 deletions
15
...wser/ui/views/side_panel/customize_chrome/customize_chrome_side_panel_controller_utils.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,15 @@ | ||
// 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. | ||
|
||
#include "chrome/browser/ui/side_panel/customize_chrome/customize_chrome_side_panel_controller_utils.h" | ||
|
||
#include "chrome/browser/ui/views/side_panel/customize_chrome/customize_chrome_side_panel_controller.h" | ||
|
||
namespace customize_chrome { | ||
|
||
std::unique_ptr<CustomizeChromeTabHelper::Delegate> CreateDelegate() { | ||
return std::make_unique<CustomizeChromeSidePanelController>(); | ||
} | ||
|
||
} // namespace customize_chrome |
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
Oops, something went wrong.