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(
mojo::PendingReceiver<new_tab_page::mojom::PageHandler>
pending_page_handler,
mojo::PendingRemote<new_tab_page::mojom::Page> pending_page,
Profile* profile)
Profile* profile,
content::WebContents* web_contents)
: chrome_colors_service_(
chrome_colors::ChromeColorsFactory::GetForProfile(profile)),
instant_service_(InstantServiceFactory::GetForProfile(profile)),
ntp_background_service_(
NtpBackgroundServiceFactory::GetForProfile(profile)),
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(ntp_background_service_);
CHECK(web_contents_);
instant_service_->AddObserver(this);
ntp_background_service_->AddObserver(this);
page_->SetTheme(MakeTheme(*instant_service_->GetInitializedNtpTheme()));
......@@ -159,11 +162,11 @@ void NewTabPageHandler::GetChromeThemes(GetChromeThemesCallback callback) {
}
void NewTabPageHandler::ApplyDefaultTheme() {
chrome_colors_service_->ApplyDefaultTheme(web_contents());
chrome_colors_service_->ApplyDefaultTheme(web_contents_);
}
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) {
......@@ -176,8 +179,7 @@ void NewTabPageHandler::ApplyChromeTheme(int32_t id) {
if (result == end) {
return;
}
chrome_colors_service_->ApplyAutogeneratedTheme(result->color,
web_contents());
chrome_colors_service_->ApplyAutogeneratedTheme(result->color, web_contents_);
}
void NewTabPageHandler::ConfirmThemeChanges() {
......
......@@ -26,15 +26,19 @@ namespace chrome_colors {
class ChromeColorsService;
} // namespace chrome_colors
class NewTabPageHandler : public content::WebContentsObserver,
public new_tab_page::mojom::PageHandler,
namespace content {
class WebContents;
} // namespace content
class NewTabPageHandler : public new_tab_page::mojom::PageHandler,
public InstantServiceObserver,
public NtpBackgroundServiceObserver {
public:
NewTabPageHandler(mojo::PendingReceiver<new_tab_page::mojom::PageHandler>
pending_page_handler,
mojo::PendingRemote<new_tab_page::mojom::Page> pending_page,
Profile* profile);
Profile* profile,
content::WebContents* web_contents);
~NewTabPageHandler() override;
// new_tab_page::mojom::PageHandler:
......@@ -82,6 +86,7 @@ class NewTabPageHandler : public content::WebContentsObserver,
GetBackgroundImagesCallback background_images_callback_;
mojo::Remote<new_tab_page::mojom::Page> page_;
mojo::Receiver<new_tab_page::mojom::PageHandler> receiver_;
content::WebContents* web_contents_;
DISALLOW_COPY_AND_ASSIGN(NewTabPageHandler);
};
......
......@@ -129,7 +129,8 @@ NewTabPageUI::NewTabPageUI(content::WebUI* web_ui)
: ui::MojoWebUIController(web_ui, true),
page_factory_receiver_(this),
profile_(Profile::FromWebUI(web_ui)),
instant_service_(InstantServiceFactory::GetForProfile(profile_)) {
instant_service_(InstantServiceFactory::GetForProfile(profile_)),
web_contents_(web_ui->GetWebContents()) {
content::WebUIDataSource::Add(profile_,
CreateNewTabPageUiHtmlSource(profile_));
......@@ -172,7 +173,8 @@ void NewTabPageUI::CreatePageHandler(
pending_page_handler) {
DCHECK(pending_page.is_valid());
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) {
......
......@@ -14,6 +14,7 @@
#include "ui/webui/mojo_web_ui_controller.h"
namespace content {
class WebContents;
class WebUI;
}
class GURL;
......@@ -57,6 +58,7 @@ class NewTabPageUI : public ui::MojoWebUIController,
page_factory_receiver_;
Profile* profile_;
InstantService* instant_service_;
content::WebContents* web_contents_;
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