Commit 70207f3b authored by Becca Hughes's avatar Becca Hughes Committed by Commit Bot

[Media Notification] Fix crash in background

This crash is still appearing and I think |owner_|
might be going away and cause the seg fault.

This removes |owner_| from MediaNotificationBackground.

BUG=994395

Change-Id: I59364ea14e9e7614728dac1e227133bcf76733c2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1772375Reviewed-by: default avatarTommy Steimel <steimel@chromium.org>
Commit-Queue: Becca Hughes <beccahughes@chromium.org>
Cr-Commit-Position: refs/heads/master@{#690768}
parent 08684a50
...@@ -233,16 +233,12 @@ base::Optional<SkColor> GetNotificationForegroundColor( ...@@ -233,16 +233,12 @@ base::Optional<SkColor> GetNotificationForegroundColor(
} // namespace } // namespace
MediaNotificationBackground::MediaNotificationBackground( MediaNotificationBackground::MediaNotificationBackground(
views::View* owner,
int top_radius, int top_radius,
int bottom_radius, int bottom_radius,
double artwork_max_width_pct) double artwork_max_width_pct)
: owner_(owner), : top_radius_(top_radius),
top_radius_(top_radius),
bottom_radius_(bottom_radius), bottom_radius_(bottom_radius),
artwork_max_width_pct_(artwork_max_width_pct) { artwork_max_width_pct_(artwork_max_width_pct) {}
DCHECK(owner);
}
MediaNotificationBackground::~MediaNotificationBackground() = default; MediaNotificationBackground::~MediaNotificationBackground() = default;
...@@ -274,7 +270,7 @@ void MediaNotificationBackground::Paint(gfx::Canvas* canvas, ...@@ -274,7 +270,7 @@ void MediaNotificationBackground::Paint(gfx::Canvas* canvas,
// maintaining the aspect ratio. // maintaining the aspect ratio.
gfx::Rect source_bounds = gfx::Rect source_bounds =
gfx::Rect(0, 0, artwork_.width(), artwork_.height()); gfx::Rect(0, 0, artwork_.width(), artwork_.height());
gfx::Rect artwork_bounds = GetArtworkBounds(bounds); gfx::Rect artwork_bounds = GetArtworkBounds(*view);
canvas->DrawImageInt( canvas->DrawImageInt(
artwork_, source_bounds.x(), source_bounds.y(), source_bounds.width(), artwork_, source_bounds.x(), source_bounds.y(), source_bounds.width(),
...@@ -286,11 +282,11 @@ void MediaNotificationBackground::Paint(gfx::Canvas* canvas, ...@@ -286,11 +282,11 @@ void MediaNotificationBackground::Paint(gfx::Canvas* canvas,
// notification. This may cover up some of the artwork. // notification. This may cover up some of the artwork.
const SkColor background_color = const SkColor background_color =
background_color_.value_or(kMediaNotificationDefaultBackgroundColor); background_color_.value_or(kMediaNotificationDefaultBackgroundColor);
canvas->FillRect(GetFilledBackgroundBounds(bounds), background_color); canvas->FillRect(GetFilledBackgroundBounds(*view), background_color);
{ {
// Draw a gradient to fade the color background and the image together. // Draw a gradient to fade the color background and the image together.
gfx::Rect draw_bounds = GetGradientBounds(bounds); gfx::Rect draw_bounds = GetGradientBounds(*view);
const SkColor colors[2] = { const SkColor colors[2] = {
background_color, SkColorSetA(background_color, SK_AlphaTRANSPARENT)}; background_color, SkColorSetA(background_color, SK_AlphaTRANSPARENT)};
...@@ -315,27 +311,25 @@ void MediaNotificationBackground::UpdateArtwork(const gfx::ImageSkia& image) { ...@@ -315,27 +311,25 @@ void MediaNotificationBackground::UpdateArtwork(const gfx::ImageSkia& image) {
background_color_ = GetNotificationBackgroundColor(artwork_.bitmap()); background_color_ = GetNotificationBackgroundColor(artwork_.bitmap());
foreground_color_ = foreground_color_ =
GetNotificationForegroundColor(background_color_, artwork_.bitmap()); GetNotificationForegroundColor(background_color_, artwork_.bitmap());
owner_->SchedulePaint();
} }
void MediaNotificationBackground::UpdateCornerRadius(int top_radius, bool MediaNotificationBackground::UpdateCornerRadius(int top_radius,
int bottom_radius) { int bottom_radius) {
if (top_radius_ == top_radius && bottom_radius_ == bottom_radius) if (top_radius_ == top_radius && bottom_radius_ == bottom_radius)
return; return false;
top_radius_ = top_radius; top_radius_ = top_radius;
bottom_radius_ = bottom_radius; bottom_radius_ = bottom_radius;
return true;
owner_->SchedulePaint();
} }
void MediaNotificationBackground::UpdateArtworkMaxWidthPct( bool MediaNotificationBackground::UpdateArtworkMaxWidthPct(
double max_width_pct) { double max_width_pct) {
if (artwork_max_width_pct_ == max_width_pct) if (artwork_max_width_pct_ == max_width_pct)
return; return false;
artwork_max_width_pct_ = max_width_pct; artwork_max_width_pct_ = max_width_pct;
owner_->SchedulePaint(); return true;
} }
SkColor MediaNotificationBackground::GetBackgroundColor() const { SkColor MediaNotificationBackground::GetBackgroundColor() const {
...@@ -344,11 +338,12 @@ SkColor MediaNotificationBackground::GetBackgroundColor() const { ...@@ -344,11 +338,12 @@ SkColor MediaNotificationBackground::GetBackgroundColor() const {
return kMediaNotificationDefaultBackgroundColor; return kMediaNotificationDefaultBackgroundColor;
} }
SkColor MediaNotificationBackground::GetForegroundColor() const { SkColor MediaNotificationBackground::GetForegroundColor(
const views::View& owner) const {
const SkColor foreground = const SkColor foreground =
foreground_color_.has_value() foreground_color_.has_value()
? *foreground_color_ ? *foreground_color_
: views::style::GetColor(*owner_, views::style::CONTEXT_LABEL, : views::style::GetColor(owner, views::style::CONTEXT_LABEL,
views::style::STYLE_PRIMARY); views::style::STYLE_PRIMARY);
return color_utils::BlendForMinContrast(foreground, GetBackgroundColor()) return color_utils::BlendForMinContrast(foreground, GetBackgroundColor())
.color; .color;
...@@ -373,31 +368,34 @@ int MediaNotificationBackground::GetArtworkVisibleWidth( ...@@ -373,31 +368,34 @@ int MediaNotificationBackground::GetArtworkVisibleWidth(
} }
gfx::Rect MediaNotificationBackground::GetArtworkBounds( gfx::Rect MediaNotificationBackground::GetArtworkBounds(
const gfx::Rect& view_bounds) const { const views::View& owner) const {
const gfx::Rect& view_bounds = owner.GetContentsBounds();
int width = GetArtworkWidth(view_bounds.size()); int width = GetArtworkWidth(view_bounds.size());
// The artwork should be positioned on the far right hand side of the // The artwork should be positioned on the far right hand side of the
// notification and be the same height. // notification and be the same height.
return owner_->GetMirroredRect( return owner.GetMirroredRect(
gfx::Rect(view_bounds.right() - width, 0, width, view_bounds.height())); gfx::Rect(view_bounds.right() - width, 0, width, view_bounds.height()));
} }
gfx::Rect MediaNotificationBackground::GetFilledBackgroundBounds( gfx::Rect MediaNotificationBackground::GetFilledBackgroundBounds(
const gfx::Rect& view_bounds) const { const views::View& owner) const {
// The filled background should take up the full notification except the area // The filled background should take up the full notification except the area
// taken up by the artwork. // taken up by the artwork.
const gfx::Rect& view_bounds = owner.GetContentsBounds();
gfx::Rect bounds = gfx::Rect(view_bounds); gfx::Rect bounds = gfx::Rect(view_bounds);
bounds.Inset(0, 0, GetArtworkVisibleWidth(view_bounds.size()), 0); bounds.Inset(0, 0, GetArtworkVisibleWidth(view_bounds.size()), 0);
return owner_->GetMirroredRect(bounds); return owner.GetMirroredRect(bounds);
} }
gfx::Rect MediaNotificationBackground::GetGradientBounds( gfx::Rect MediaNotificationBackground::GetGradientBounds(
const gfx::Rect& view_bounds) const { const views::View& owner) const {
if (artwork_.isNull()) if (artwork_.isNull())
return gfx::Rect(0, 0, 0, 0); return gfx::Rect(0, 0, 0, 0);
// The gradient should appear above the artwork on the left. // The gradient should appear above the artwork on the left.
return owner_->GetMirroredRect(gfx::Rect( const gfx::Rect& view_bounds = owner.GetContentsBounds();
return owner.GetMirroredRect(gfx::Rect(
view_bounds.width() - GetArtworkVisibleWidth(view_bounds.size()), view_bounds.width() - GetArtworkVisibleWidth(view_bounds.size()),
view_bounds.y(), kMediaImageGradientWidth, view_bounds.height())); view_bounds.y(), kMediaImageGradientWidth, view_bounds.height()));
} }
......
...@@ -29,8 +29,7 @@ namespace media_message_center { ...@@ -29,8 +29,7 @@ namespace media_message_center {
class COMPONENT_EXPORT(MEDIA_MESSAGE_CENTER) MediaNotificationBackground class COMPONENT_EXPORT(MEDIA_MESSAGE_CENTER) MediaNotificationBackground
: public views::Background { : public views::Background {
public: public:
MediaNotificationBackground(views::View* owner, MediaNotificationBackground(int top_radius,
int top_radius,
int bottom_radius, int bottom_radius,
double artwork_max_width_pct); double artwork_max_width_pct);
~MediaNotificationBackground() override; ~MediaNotificationBackground() override;
...@@ -38,12 +37,12 @@ class COMPONENT_EXPORT(MEDIA_MESSAGE_CENTER) MediaNotificationBackground ...@@ -38,12 +37,12 @@ class COMPONENT_EXPORT(MEDIA_MESSAGE_CENTER) MediaNotificationBackground
// views::Background // views::Background
void Paint(gfx::Canvas* canvas, views::View* view) const override; void Paint(gfx::Canvas* canvas, views::View* view) const override;
void UpdateCornerRadius(int top_radius, int bottom_radius);
void UpdateArtwork(const gfx::ImageSkia& image); void UpdateArtwork(const gfx::ImageSkia& image);
void UpdateArtworkMaxWidthPct(double max_width_pct); bool UpdateCornerRadius(int top_radius, int bottom_radius);
bool UpdateArtworkMaxWidthPct(double max_width_pct);
SkColor GetBackgroundColor() const; SkColor GetBackgroundColor() const;
SkColor GetForegroundColor() const; SkColor GetForegroundColor(const views::View& owner) const;
private: private:
friend class MediaNotificationBackgroundTest; friend class MediaNotificationBackgroundTest;
...@@ -53,15 +52,12 @@ class COMPONENT_EXPORT(MEDIA_MESSAGE_CENTER) MediaNotificationBackground ...@@ -53,15 +52,12 @@ class COMPONENT_EXPORT(MEDIA_MESSAGE_CENTER) MediaNotificationBackground
int GetArtworkWidth(const gfx::Size& view_size) const; int GetArtworkWidth(const gfx::Size& view_size) const;
int GetArtworkVisibleWidth(const gfx::Size& view_size) const; int GetArtworkVisibleWidth(const gfx::Size& view_size) const;
gfx::Rect GetArtworkBounds(const gfx::Rect& view_bounds) const; gfx::Rect GetArtworkBounds(const views::View& owner) const;
gfx::Rect GetFilledBackgroundBounds(const gfx::Rect& view_bounds) const; gfx::Rect GetFilledBackgroundBounds(const views::View& owner) const;
gfx::Rect GetGradientBounds(const gfx::Rect& view_bounds) const; gfx::Rect GetGradientBounds(const views::View& owner) const;
SkPoint GetGradientStartPoint(const gfx::Rect& draw_bounds) const; SkPoint GetGradientStartPoint(const gfx::Rect& draw_bounds) const;
SkPoint GetGradientEndPoint(const gfx::Rect& draw_bounds) const; SkPoint GetGradientEndPoint(const gfx::Rect& draw_bounds) const;
// Reference to the owning view that this is a background for.
views::View* owner_;
int top_radius_; int top_radius_;
int bottom_radius_; int bottom_radius_;
......
...@@ -73,16 +73,13 @@ class MediaNotificationBackgroundTest : public testing::Test { ...@@ -73,16 +73,13 @@ class MediaNotificationBackgroundTest : public testing::Test {
~MediaNotificationBackgroundTest() override = default; ~MediaNotificationBackgroundTest() override = default;
void SetUp() override { void SetUp() override {
owner_ = std::make_unique<views::StaticSizedView>(); background_ = std::make_unique<MediaNotificationBackground>(10, 10, 0.1);
background_ = std::make_unique<MediaNotificationBackground>(owner_.get(),
10, 10, 0.1);
EXPECT_FALSE(GetBackgroundColor().has_value()); EXPECT_FALSE(GetBackgroundColor().has_value());
} }
void TearDown() override { void TearDown() override {
background_.reset(); background_.reset();
owner_.reset();
} }
MediaNotificationBackground* background() const { return background_.get(); } MediaNotificationBackground* background() const { return background_.get(); }
...@@ -96,7 +93,6 @@ class MediaNotificationBackgroundTest : public testing::Test { ...@@ -96,7 +93,6 @@ class MediaNotificationBackgroundTest : public testing::Test {
} }
private: private:
std::unique_ptr<views::StaticSizedView> owner_;
std::unique_ptr<MediaNotificationBackground> background_; std::unique_ptr<MediaNotificationBackground> background_;
DISALLOW_COPY_AND_ASSIGN(MediaNotificationBackgroundTest); DISALLOW_COPY_AND_ASSIGN(MediaNotificationBackgroundTest);
...@@ -322,6 +318,8 @@ class MediaNotificationBackgroundRTLTest ...@@ -322,6 +318,8 @@ class MediaNotificationBackgroundRTLTest
: switches::kForceDirectionLTR); : switches::kForceDirectionLTR);
MediaNotificationBackgroundTest::SetUp(); MediaNotificationBackgroundTest::SetUp();
ASSERT_EQ(IsRTL(), base::i18n::IsRTL());
} }
bool IsRTL() const { return GetParam(); } bool IsRTL() const { return GetParam(); }
...@@ -336,15 +334,19 @@ INSTANTIATE_TEST_SUITE_P(, MediaNotificationBackgroundRTLTest, testing::Bool()); ...@@ -336,15 +334,19 @@ INSTANTIATE_TEST_SUITE_P(, MediaNotificationBackgroundRTLTest, testing::Bool());
TEST_P(MediaNotificationBackgroundRTLTest, BoundsSanityCheck) { TEST_P(MediaNotificationBackgroundRTLTest, BoundsSanityCheck) {
// The test notification will have a width of 200 and a height of 50. // The test notification will have a width of 200 and a height of 50.
gfx::Rect bounds(0, 0, 200, 50); gfx::Rect bounds(0, 0, 200, 50);
auto owner = std::make_unique<views::StaticSizedView>();
owner->SetBoundsRect(bounds);
ASSERT_EQ(bounds, owner->GetContentsBounds());
// Check the artwork is not visible by default. // Check the artwork is not visible by default.
EXPECT_EQ(0, background()->GetArtworkWidth(bounds.size())); EXPECT_EQ(0, background()->GetArtworkWidth(bounds.size()));
EXPECT_EQ(0, background()->GetArtworkVisibleWidth(bounds.size())); EXPECT_EQ(0, background()->GetArtworkVisibleWidth(bounds.size()));
EXPECT_EQ(gfx::Rect(IsRTL() ? -200 : 200, 0, 0, 50), EXPECT_EQ(gfx::Rect(IsRTL() ? 0 : 200, 0, 0, 50),
background()->GetArtworkBounds(bounds)); background()->GetArtworkBounds(*owner.get()));
EXPECT_EQ(gfx::Rect(IsRTL() ? -200 : 0, 0, 200, 50), EXPECT_EQ(gfx::Rect(IsRTL() ? 0 : 0, 0, 200, 50),
background()->GetFilledBackgroundBounds(bounds)); background()->GetFilledBackgroundBounds(*owner.get()));
EXPECT_EQ(gfx::Rect(0, 0, 0, 0), background()->GetGradientBounds(bounds)); EXPECT_EQ(gfx::Rect(0, 0, 0, 0),
background()->GetGradientBounds(*owner.get()));
// The background artwork image will have an aspect ratio of 2:1. // The background artwork image will have an aspect ratio of 2:1.
SkBitmap bitmap; SkBitmap bitmap;
...@@ -362,22 +364,22 @@ TEST_P(MediaNotificationBackgroundRTLTest, BoundsSanityCheck) { ...@@ -362,22 +364,22 @@ TEST_P(MediaNotificationBackgroundRTLTest, BoundsSanityCheck) {
EXPECT_EQ(100, background()->GetArtworkVisibleWidth(bounds.size())); EXPECT_EQ(100, background()->GetArtworkVisibleWidth(bounds.size()));
// Check the artwork is positioned to the right. // Check the artwork is positioned to the right.
EXPECT_EQ(gfx::Rect(IsRTL() ? -200 : 100, 0, 100, 50), EXPECT_EQ(gfx::Rect(IsRTL() ? 0 : 100, 0, 100, 50),
background()->GetArtworkBounds(bounds)); background()->GetArtworkBounds(*owner.get()));
// Check the filled background is to the left of the image. // Check the filled background is to the left of the image.
EXPECT_EQ(gfx::Rect(IsRTL() ? -100 : 0, 0, 100, 50), EXPECT_EQ(gfx::Rect(IsRTL() ? 100 : 0, 0, 100, 50),
background()->GetFilledBackgroundBounds(bounds)); background()->GetFilledBackgroundBounds(*owner.get()));
// Check the gradient is positioned above the artwork. // Check the gradient is positioned above the artwork.
const gfx::Rect gradient_bounds = background()->GetGradientBounds(bounds); const gfx::Rect gradient_bounds =
EXPECT_EQ(gfx::Rect(IsRTL() ? -140 : 100, 0, 40, 50), gradient_bounds); background()->GetGradientBounds(*owner.get());
EXPECT_EQ(gfx::Rect(IsRTL() ? 60 : 100, 0, 40, 50), gradient_bounds);
// Check the gradient point X-values are the start and end of // Check the gradient point X-values are the start and end of
// |gradient_bounds|. // |gradient_bounds|.
EXPECT_EQ(IsRTL() ? -100 : 100, EXPECT_EQ(100, background()->GetGradientStartPoint(gradient_bounds).x());
background()->GetGradientStartPoint(gradient_bounds).x()); EXPECT_EQ(IsRTL() ? 60 : 140,
EXPECT_EQ(IsRTL() ? -140 : 140,
background()->GetGradientEndPoint(gradient_bounds).x()); background()->GetGradientEndPoint(gradient_bounds).x());
// Check both of the gradient point Y-values are half the height. // Check both of the gradient point Y-values are half the height.
......
...@@ -203,7 +203,7 @@ MediaNotificationView::MediaNotificationView( ...@@ -203,7 +203,7 @@ MediaNotificationView::MediaNotificationView(
IDS_MEDIA_MESSAGE_CENTER_MEDIA_NOTIFICATION_ACTION_NEXT_TRACK)); IDS_MEDIA_MESSAGE_CENTER_MEDIA_NOTIFICATION_ACTION_NEXT_TRACK));
SetBackground(std::make_unique<MediaNotificationBackground>( SetBackground(std::make_unique<MediaNotificationBackground>(
this, message_center::kNotificationCornerRadius, message_center::kNotificationCornerRadius,
message_center::kNotificationCornerRadius, kMediaImageMaxWidthPct)); message_center::kNotificationCornerRadius, kMediaImageMaxWidthPct));
UpdateForegroundColor(); UpdateForegroundColor();
...@@ -235,8 +235,10 @@ void MediaNotificationView::SetExpanded(bool expanded) { ...@@ -235,8 +235,10 @@ void MediaNotificationView::SetExpanded(bool expanded) {
void MediaNotificationView::UpdateCornerRadius(int top_radius, void MediaNotificationView::UpdateCornerRadius(int top_radius,
int bottom_radius) { int bottom_radius) {
GetMediaNotificationBackground()->UpdateCornerRadius(top_radius, if (GetMediaNotificationBackground()->UpdateCornerRadius(top_radius,
bottom_radius); bottom_radius)) {
SchedulePaint();
}
} }
void MediaNotificationView::GetAccessibleNodeData(ui::AXNodeData* node_data) { void MediaNotificationView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
...@@ -394,8 +396,10 @@ void MediaNotificationView::UpdateViewForExpandedState() { ...@@ -394,8 +396,10 @@ void MediaNotificationView::UpdateViewForExpandedState() {
main_row_->Layout(); main_row_->Layout();
GetMediaNotificationBackground()->UpdateArtworkMaxWidthPct( if (GetMediaNotificationBackground()->UpdateArtworkMaxWidthPct(
expanded ? kMediaImageMaxWidthExpandedPct : kMediaImageMaxWidthPct); expanded ? kMediaImageMaxWidthExpandedPct : kMediaImageMaxWidthPct)) {
SchedulePaint();
}
header_row_->SetExpanded(expanded); header_row_->SetExpanded(expanded);
...@@ -439,7 +443,7 @@ void MediaNotificationView::UpdateForegroundColor() { ...@@ -439,7 +443,7 @@ void MediaNotificationView::UpdateForegroundColor() {
const SkColor background = const SkColor background =
GetMediaNotificationBackground()->GetBackgroundColor(); GetMediaNotificationBackground()->GetBackgroundColor();
const SkColor foreground = const SkColor foreground =
GetMediaNotificationBackground()->GetForegroundColor(); GetMediaNotificationBackground()->GetForegroundColor(*this);
title_label_->SetEnabledColor(foreground); title_label_->SetEnabledColor(foreground);
artist_label_->SetEnabledColor(foreground); artist_label_->SetEnabledColor(foreground);
......
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