Commit cfedd433 authored by Tibor Goldschwendt's avatar Tibor Goldschwendt Committed by Commit Bot

[webui][ntp] Properly reference web contents

Previously, NewTabPageHandler was a content::WebContentsObserver.
However, we never associated it with a web contents. As a result, all
usages of web contents in the NTP handler were in fact a nullptr. This
CL fixes this by passing the correct web contents when creating the NTP
handler. Additionally, it removes content::WebContentsObserver as one of
the handler's interfaces as we are not observing any changes.

Change-Id: I4d79b726dbc7377f29b6f0630680bb13082a3c15
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2095785
Auto-Submit: Tibor Goldschwendt <tiborg@chromium.org>
Reviewed-by: default avatarEsmael Elmoslimany <aee@chromium.org>
Commit-Queue: Tibor Goldschwendt <tiborg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#748473}
parent efe94917
...@@ -63,16 +63,19 @@ NewTabPageHandler::NewTabPageHandler( ...@@ -63,16 +63,19 @@ NewTabPageHandler::NewTabPageHandler(
mojo::PendingReceiver<new_tab_page::mojom::PageHandler> mojo::PendingReceiver<new_tab_page::mojom::PageHandler>
pending_page_handler, pending_page_handler,
mojo::PendingRemote<new_tab_page::mojom::Page> pending_page, mojo::PendingRemote<new_tab_page::mojom::Page> pending_page,
Profile* profile) Profile* profile,
content::WebContents* web_contents)
: chrome_colors_service_( : chrome_colors_service_(
chrome_colors::ChromeColorsFactory::GetForProfile(profile)), chrome_colors::ChromeColorsFactory::GetForProfile(profile)),
instant_service_(InstantServiceFactory::GetForProfile(profile)), instant_service_(InstantServiceFactory::GetForProfile(profile)),
ntp_background_service_( ntp_background_service_(
NtpBackgroundServiceFactory::GetForProfile(profile)), NtpBackgroundServiceFactory::GetForProfile(profile)),
page_{std::move(pending_page)}, page_{std::move(pending_page)},
receiver_{this, std::move(pending_page_handler)} { receiver_{this, std::move(pending_page_handler)},
web_contents_(web_contents) {
CHECK(instant_service_); CHECK(instant_service_);
CHECK(ntp_background_service_); CHECK(ntp_background_service_);
CHECK(web_contents_);
instant_service_->AddObserver(this); instant_service_->AddObserver(this);
ntp_background_service_->AddObserver(this); ntp_background_service_->AddObserver(this);
page_->SetTheme(MakeTheme(*instant_service_->GetInitializedNtpTheme())); page_->SetTheme(MakeTheme(*instant_service_->GetInitializedNtpTheme()));
...@@ -159,11 +162,11 @@ void NewTabPageHandler::GetChromeThemes(GetChromeThemesCallback callback) { ...@@ -159,11 +162,11 @@ void NewTabPageHandler::GetChromeThemes(GetChromeThemesCallback callback) {
} }
void NewTabPageHandler::ApplyDefaultTheme() { void NewTabPageHandler::ApplyDefaultTheme() {
chrome_colors_service_->ApplyDefaultTheme(web_contents()); chrome_colors_service_->ApplyDefaultTheme(web_contents_);
} }
void NewTabPageHandler::ApplyAutogeneratedTheme(const SkColor& frame_color) { void NewTabPageHandler::ApplyAutogeneratedTheme(const SkColor& frame_color) {
chrome_colors_service_->ApplyAutogeneratedTheme(frame_color, web_contents()); chrome_colors_service_->ApplyAutogeneratedTheme(frame_color, web_contents_);
} }
void NewTabPageHandler::ApplyChromeTheme(int32_t id) { void NewTabPageHandler::ApplyChromeTheme(int32_t id) {
...@@ -176,8 +179,7 @@ void NewTabPageHandler::ApplyChromeTheme(int32_t id) { ...@@ -176,8 +179,7 @@ void NewTabPageHandler::ApplyChromeTheme(int32_t id) {
if (result == end) { if (result == end) {
return; return;
} }
chrome_colors_service_->ApplyAutogeneratedTheme(result->color, chrome_colors_service_->ApplyAutogeneratedTheme(result->color, web_contents_);
web_contents());
} }
void NewTabPageHandler::ConfirmThemeChanges() { void NewTabPageHandler::ConfirmThemeChanges() {
......
...@@ -26,15 +26,19 @@ namespace chrome_colors { ...@@ -26,15 +26,19 @@ namespace chrome_colors {
class ChromeColorsService; class ChromeColorsService;
} // namespace chrome_colors } // namespace chrome_colors
class NewTabPageHandler : public content::WebContentsObserver, namespace content {
public new_tab_page::mojom::PageHandler, class WebContents;
} // namespace content
class NewTabPageHandler : public new_tab_page::mojom::PageHandler,
public InstantServiceObserver, public InstantServiceObserver,
public NtpBackgroundServiceObserver { public NtpBackgroundServiceObserver {
public: public:
NewTabPageHandler(mojo::PendingReceiver<new_tab_page::mojom::PageHandler> NewTabPageHandler(mojo::PendingReceiver<new_tab_page::mojom::PageHandler>
pending_page_handler, pending_page_handler,
mojo::PendingRemote<new_tab_page::mojom::Page> pending_page, mojo::PendingRemote<new_tab_page::mojom::Page> pending_page,
Profile* profile); Profile* profile,
content::WebContents* web_contents);
~NewTabPageHandler() override; ~NewTabPageHandler() override;
// new_tab_page::mojom::PageHandler: // new_tab_page::mojom::PageHandler:
...@@ -82,6 +86,7 @@ class NewTabPageHandler : public content::WebContentsObserver, ...@@ -82,6 +86,7 @@ class NewTabPageHandler : public content::WebContentsObserver,
GetBackgroundImagesCallback background_images_callback_; GetBackgroundImagesCallback background_images_callback_;
mojo::Remote<new_tab_page::mojom::Page> page_; mojo::Remote<new_tab_page::mojom::Page> page_;
mojo::Receiver<new_tab_page::mojom::PageHandler> receiver_; mojo::Receiver<new_tab_page::mojom::PageHandler> receiver_;
content::WebContents* web_contents_;
DISALLOW_COPY_AND_ASSIGN(NewTabPageHandler); DISALLOW_COPY_AND_ASSIGN(NewTabPageHandler);
}; };
......
...@@ -129,7 +129,8 @@ NewTabPageUI::NewTabPageUI(content::WebUI* web_ui) ...@@ -129,7 +129,8 @@ NewTabPageUI::NewTabPageUI(content::WebUI* web_ui)
: ui::MojoWebUIController(web_ui, true), : ui::MojoWebUIController(web_ui, true),
page_factory_receiver_(this), page_factory_receiver_(this),
profile_(Profile::FromWebUI(web_ui)), profile_(Profile::FromWebUI(web_ui)),
instant_service_(InstantServiceFactory::GetForProfile(profile_)) { instant_service_(InstantServiceFactory::GetForProfile(profile_)),
web_contents_(web_ui->GetWebContents()) {
content::WebUIDataSource::Add(profile_, content::WebUIDataSource::Add(profile_,
CreateNewTabPageUiHtmlSource(profile_)); CreateNewTabPageUiHtmlSource(profile_));
...@@ -172,7 +173,8 @@ void NewTabPageUI::CreatePageHandler( ...@@ -172,7 +173,8 @@ void NewTabPageUI::CreatePageHandler(
pending_page_handler) { pending_page_handler) {
DCHECK(pending_page.is_valid()); DCHECK(pending_page.is_valid());
page_handler_ = std::make_unique<NewTabPageHandler>( page_handler_ = std::make_unique<NewTabPageHandler>(
std::move(pending_page_handler), std::move(pending_page), profile_); std::move(pending_page_handler), std::move(pending_page), profile_,
web_contents_);
} }
void NewTabPageUI::NtpThemeChanged(const NtpTheme& theme) { void NewTabPageUI::NtpThemeChanged(const NtpTheme& theme) {
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "ui/webui/mojo_web_ui_controller.h" #include "ui/webui/mojo_web_ui_controller.h"
namespace content { namespace content {
class WebContents;
class WebUI; class WebUI;
} }
class GURL; class GURL;
...@@ -57,6 +58,7 @@ class NewTabPageUI : public ui::MojoWebUIController, ...@@ -57,6 +58,7 @@ class NewTabPageUI : public ui::MojoWebUIController,
page_factory_receiver_; page_factory_receiver_;
Profile* profile_; Profile* profile_;
InstantService* instant_service_; InstantService* instant_service_;
content::WebContents* web_contents_;
WEB_UI_CONTROLLER_TYPE_DECL(); WEB_UI_CONTROLLER_TYPE_DECL();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment