Skip to content

Commit

Permalink
Convert nested min/max calls to base::ClampToRange() in content/.
Browse files Browse the repository at this point in the history
Bug: 1000055
Change-Id: I4e7fdbe09f31460f27ad88b4bb2f075d42d21a64
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1793289
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Commit-Queue: Matt Falkenhagen <falken@chromium.org>
Reviewed-by: Matt Falkenhagen <falken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#695081}
  • Loading branch information
pkasting authored and Commit Bot committed Sep 10, 2019
1 parent 2b0d21d commit b5b3987
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "base/debug/crash_logging.h"
#import "base/mac/foundation_util.h"
#include "base/mac/mac_util.h"
#include "base/numerics/ranges.h"
#include "base/stl_util.h"
#include "base/strings/sys_string_conversions.h"
#import "content/browser/accessibility/browser_accessibility_cocoa.h"
Expand Down Expand Up @@ -110,13 +111,13 @@ SkColor SkColorFromNSColor(NSColor* color) {
CGFloat r, g, b, a;
[color getRed:&r green:&g blue:&b alpha:&a];

return std::max(0, std::min(static_cast<int>(lroundf(255.0f * a)), 255))
return base::ClampToRange(static_cast<int>(lroundf(255.0f * a)), 0, 255)
<< 24 |
std::max(0, std::min(static_cast<int>(lroundf(255.0f * r)), 255))
base::ClampToRange(static_cast<int>(lroundf(255.0f * r)), 0, 255)
<< 16 |
std::max(0, std::min(static_cast<int>(lroundf(255.0f * g)), 255))
base::ClampToRange(static_cast<int>(lroundf(255.0f * g)), 0, 255)
<< 8 |
std::max(0, std::min(static_cast<int>(lroundf(255.0f * b)), 255));
base::ClampToRange(static_cast<int>(lroundf(255.0f * b)), 0, 255);
}

// Extract underline information from an attributed string. Mostly copied from
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "base/i18n/break_iterator.h"
#include "base/lazy_instance.h"
#include "base/numerics/ranges.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
Expand Down Expand Up @@ -1243,22 +1244,22 @@ bool BrowserAccessibilityAndroid::Scroll(int direction) const {
case UP:
if (y_initial == y_min)
return false;
y = std::min(std::max(y_initial - page_y, y_min), y_max);
y = base::ClampToRange(y_initial - page_y, y_min, y_max);
break;
case DOWN:
if (y_initial == y_max)
return false;
y = std::min(std::max(y_initial + page_y, y_min), y_max);
y = base::ClampToRange(y_initial + page_y, y_min, y_max);
break;
case LEFT:
if (x_initial == x_min)
return false;
x = std::min(std::max(x_initial - page_x, x_min), x_max);
x = base::ClampToRange(x_initial - page_x, x_min, x_max);
break;
case RIGHT:
if (x_initial == x_max)
return false;
x = std::min(std::max(x_initial + page_x, x_min), x_max);
x = base::ClampToRange(x_initial + page_x, x_min, x_max);
break;
default:
NOTREACHED();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "base/feature_list.h"
#include "base/macros.h"
#include "base/metrics/histogram_macros.h"
#include "base/numerics/ranges.h"
#include "content/browser/accessibility/browser_accessibility_android.h"
#include "content/browser/accessibility/browser_accessibility_manager_android.h"
#include "content/browser/accessibility/browser_accessibility_state_impl.h"
Expand Down Expand Up @@ -932,7 +933,7 @@ jboolean WebContentsAccessibilityAndroid::AdjustSlider(
// Slider does not move if the delta value is less than 1.
delta = ((delta < 1) ? 1 : delta);
value += (increment ? delta : -delta);
value = std::max(std::min(value, max), min);
value = base::ClampToRange(value, min, max);
if (value != original_value) {
node->manager()->SetValue(*node, base::NumberToString(value));
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/numerics/ranges.h"
#include "cc/layers/ui_resource_layer.h"
#include "content/public/browser/android/compositor.h"
#include "ui/android/handle_view_resources.h"
Expand Down Expand Up @@ -84,10 +85,9 @@ void CompositedTouchHandleDrawable::SetOrigin(const gfx::PointF& origin) {

void CompositedTouchHandleDrawable::SetAlpha(float alpha) {
DCHECK(layer_->parent());
alpha = std::max(0.f, std::min(1.f, alpha));
bool hidden = alpha <= 0;
alpha = base::ClampToRange(alpha, 0.0f, 1.0f);
layer_->SetOpacity(alpha);
layer_->SetHideLayerAndSubtree(hidden);
layer_->SetHideLayerAndSubtree(!alpha);
}

gfx::RectF CompositedTouchHandleDrawable::GetVisibleBounds() const {
Expand Down
3 changes: 2 additions & 1 deletion content/browser/gpu/gpu_process_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/no_destructor.h"
#include "base/numerics/ranges.h"
#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/post_task.h"
Expand Down Expand Up @@ -746,7 +747,7 @@ GpuProcessHost::~GpuProcessHost() {
// Windows always returns PROCESS_CRASHED on abnormal termination, as it
// doesn't have a way to distinguish the two.
base::UmaHistogramSparse("GPU.GPUProcessExitCode",
std::max(0, std::min(100, info.exit_code)));
base::ClampToRange(info.exit_code, 0, 100));
}

message = "The GPU process ";
Expand Down
16 changes: 6 additions & 10 deletions content/browser/media/capture/lame_capture_overlay_chromeos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cmath>

#include "base/bind.h"
#include "base/numerics/ranges.h"
#include "base/numerics/safe_conversions.h"
#include "content/browser/media/capture/lame_window_capturer_chromeos.h"
#include "media/base/video_frame.h"
Expand Down Expand Up @@ -81,12 +82,9 @@ gfx::Rect ToAbsoluteBoundsForI420(const gfx::RectF& relative,
std::max(0, snapped_bottom - snapped_top));
}

inline int clip_byte(int x) {
return std::max(0, std::min(x, 255));
}

inline int alpha_blend(int alpha, int src, int dst) {
return (src * alpha + dst * (255 - alpha)) / 255;
alpha = (src * alpha + dst * (255 - alpha)) / 255;
return base::ClampToRange(alpha, 0, 255);
}

} // namespace
Expand Down Expand Up @@ -155,7 +153,7 @@ LameCaptureOverlayChromeOS::MakeRenderer(const gfx::Rect& region_in_frame) {
const int color_b = SkColorGetB(color);
const int color_y =
((color_r * 66 + color_g * 129 + color_b * 25 + 128) >> 8) + 16;
yplane[x] = clip_byte(alpha_blend(alpha, color_y, yplane[x]));
yplane[x] = alpha_blend(alpha, color_y, yplane[x]);

// Only sample U and V at even coordinates.
if ((x % 2 == 0) && (y % 2 == 0)) {
Expand All @@ -165,10 +163,8 @@ LameCaptureOverlayChromeOS::MakeRenderer(const gfx::Rect& region_in_frame) {
const int color_v =
((color_r * 112 + color_g * -94 + color_b * -18 + 128) >> 8) +
128;
uplane[x / 2] =
clip_byte(alpha_blend(alpha, color_u, uplane[x / 2]));
vplane[x / 2] =
clip_byte(alpha_blend(alpha, color_v, vplane[x / 2]));
uplane[x / 2] = alpha_blend(alpha, color_u, uplane[x / 2]);
vplane[x / 2] = alpha_blend(alpha, color_v, vplane[x / 2]);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion content/browser/renderer_host/overscroll_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "base/command_line.h"
#include "base/logging.h"
#include "base/numerics/ranges.h"
#include "content/browser/renderer_host/overscroll_controller_delegate.h"
#include "content/public/browser/overscroll_configuration.h"
#include "content/public/common/content_features.h"
Expand Down Expand Up @@ -45,7 +46,7 @@ bool IsGestureScrollUpdateInertialEvent(const blink::WebInputEvent& event) {

float ClampAbsoluteValue(float value, float max_abs) {
DCHECK_LT(0.f, max_abs);
return std::max(-max_abs, std::min(value, max_abs));
return base::ClampToRange(value, -max_abs, max_abs);
}

} // namespace
Expand Down
7 changes: 4 additions & 3 deletions content/browser/speech/speech_recognizer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "base/bind.h"
#include "base/macros.h"
#include "base/numerics/ranges.h"
#include "base/task/post_task.h"
#include "base/time/time.h"
#include "build/build_config.h"
Expand Down Expand Up @@ -847,15 +848,15 @@ void SpeechRecognizerImpl::UpdateSignalAndNoiseLevels(const float& rms,
// Perhaps it might be quite expensive on mobile.
float level = (rms - kAudioMeterMinDb) /
(kAudioMeterDbRange / kAudioMeterRangeMaxUnclipped);
level = std::min(std::max(0.0f, level), kAudioMeterRangeMaxUnclipped);
level = base::ClampToRange(level, 0.0f, kAudioMeterRangeMaxUnclipped);
const float smoothing_factor = (level > audio_level_) ? kUpSmoothingFactor :
kDownSmoothingFactor;
audio_level_ += (level - audio_level_) * smoothing_factor;

float noise_level = (endpointer_.NoiseLevelDb() - kAudioMeterMinDb) /
(kAudioMeterDbRange / kAudioMeterRangeMaxUnclipped);
noise_level = std::min(std::max(0.0f, noise_level),
kAudioMeterRangeMaxUnclipped);
noise_level =
base::ClampToRange(noise_level, 0.0f, kAudioMeterRangeMaxUnclipped);

listener()->OnAudioLevelsChange(
session_id(), clip_detected ? 1.0f : audio_level_, noise_level);
Expand Down
7 changes: 4 additions & 3 deletions content/renderer/pepper/pepper_video_capture_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "content/renderer/pepper/pepper_video_capture_host.h"

#include "base/numerics/ranges.h"
#include "content/renderer/pepper/host_globals.h"
#include "content/renderer/pepper/pepper_media_device_manager.h"
#include "content/renderer/pepper/pepper_platform_video_capture.h"
Expand Down Expand Up @@ -339,11 +340,11 @@ void PepperVideoCaptureHost::SetRequestedInfo(
const PP_VideoCaptureDeviceInfo_Dev& device_info,
uint32_t buffer_count) {
// Clamp the buffer count to between 1 and |kMaxBuffers|.
buffer_count_hint_ = std::min(std::max(buffer_count, 1U), kMaxBuffers);
buffer_count_hint_ = base::ClampToRange(buffer_count, 1U, kMaxBuffers);
// Clamp the frame rate to between 1 and |kMaxFramesPerSecond - 1|.
int frames_per_second =
std::min(std::max(device_info.frames_per_second, 1U),
static_cast<uint32_t>(media::limits::kMaxFramesPerSecond - 1));
base::ClampToRange(device_info.frames_per_second, 1U,
uint32_t{media::limits::kMaxFramesPerSecond - 1});

video_capture_params_.requested_format = media::VideoCaptureFormat(
gfx::Size(device_info.width, device_info.height), frames_per_second,
Expand Down

0 comments on commit b5b3987

Please sign in to comment.