From 62c607121a2c0b5bf463cd6da0fd568140523e30 Mon Sep 17 00:00:00 2001 From: "danakj@chromium.org" Date: Wed, 23 Jan 2013 03:51:36 +0000 Subject: ui: Prevent negative sizes. Negative sizes are clamped to be 0, but keeping the setter methods inline, so without DCHECKs. TBR=sky BUG=160158 Relanding https://codereview.chromium.org/11365160/ in pieces. Review URL: https://chromiumcodereview.appspot.com/12042029 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@178237 0039d316-1c4b-4281-b951-d872f2087c98 --- ui/gfx/rect_f.h | 5 +---- ui/gfx/size.cc | 7 ++----- ui/gfx/size_base.h | 22 ++++++++++------------ ui/gfx/size_f.cc | 1 - 4 files changed, 13 insertions(+), 22 deletions(-) diff --git a/ui/gfx/rect_f.h b/ui/gfx/rect_f.h index bbc16cc..78524a0 100644 --- a/ui/gfx/rect_f.h +++ b/ui/gfx/rect_f.h @@ -49,10 +49,7 @@ class UI_EXPORT RectF void Scale(float x_scale, float y_scale) { set_origin(ScalePoint(origin(), x_scale, y_scale)); - - SizeF new_size = gfx::ScaleSize(size(), x_scale, y_scale); - new_size.ClampToNonNegative(); - set_size(new_size); + set_size(ScaleSize(size(), x_scale, y_scale)); } // This method reports if the RectF can be safely converted to an integer diff --git a/ui/gfx/size.cc b/ui/gfx/size.cc index a9a0bdd..b91f70e 100644 --- a/ui/gfx/size.cc +++ b/ui/gfx/size.cc @@ -8,18 +8,15 @@ #include #endif -#include "base/logging.h" #include "base/stringprintf.h" -#include "ui/gfx/size_base.h" namespace gfx { template class SizeBase; #if defined(OS_MACOSX) -Size::Size(const CGSize& s) : SizeBase(0, 0) { - set_width(s.width); - set_height(s.height); +Size::Size(const CGSize& s) + : SizeBase(s.width, s.height) { } Size& Size::operator=(const CGSize& s) { diff --git a/ui/gfx/size_base.h b/ui/gfx/size_base.h index 0ec62e4..57e4700 100644 --- a/ui/gfx/size_base.h +++ b/ui/gfx/size_base.h @@ -28,8 +28,12 @@ class UI_EXPORT SizeBase { set_height(height_ + height); } - void set_width(Type width) { width_ = width; } - void set_height(Type height) { height_ = height; } + void set_width(Type width) { + width_ = width < 0 ? 0 : width; + } + void set_height(Type height) { + height_ = height < 0 ? 0 : height; + } void ClampToMax(const Class& max) { width_ = width_ <= max.width_ ? width_ : max.width_; @@ -42,20 +46,14 @@ class UI_EXPORT SizeBase { } bool IsEmpty() const { - return (width_ <= 0) || (height_ <= 0); - } - - void ClampToNonNegative() { - if (width_ < 0) - width_ = 0; - if (height_ < 0) - height_ = 0; + return (width_ == 0) || (height_ == 0); } protected: SizeBase(Type width, Type height) - : width_(width), - height_(height) {} + : width_(width < 0 ? 0 : width), + height_(height < 0 ? 0 : height) { + } // Destructor is intentionally made non virtual and protected. // Do not make this public. diff --git a/ui/gfx/size_f.cc b/ui/gfx/size_f.cc index 69e0ee9..cbdae03 100644 --- a/ui/gfx/size_f.cc +++ b/ui/gfx/size_f.cc @@ -4,7 +4,6 @@ #include "ui/gfx/size_f.h" -#include "base/logging.h" #include "base/stringprintf.h" namespace gfx { -- cgit v1.1