summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--base/time/time.cc41
-rw-r--r--base/time/time.h40
-rw-r--r--base/time/time_unittest.cc40
-rw-r--r--cc/debug/rasterize_and_record_benchmark.cc3
-rw-r--r--cc/debug/rasterize_and_record_benchmark_impl.cc3
-rw-r--r--cc/resources/picture_pile.cc3
-rw-r--r--cc/resources/picture_pile_impl.cc3
-rw-r--r--content/renderer/media/websourcebuffer_impl.cc2
-rw-r--r--media/base/buffers.h2
-rw-r--r--media/base/media_log.cc15
-rw-r--r--media/base/media_log.h1
-rw-r--r--media/filters/chunk_demuxer.cc1
-rw-r--r--media/filters/ffmpeg_demuxer.cc4
-rw-r--r--net/http/http_response_headers.cc2
-rw-r--r--net/spdy/spdy_session.cc4
-rw-r--r--sync/sessions/nudge_tracker.cc6
16 files changed, 144 insertions, 26 deletions
diff --git a/base/time/time.cc b/base/time/time.cc
index 4605d56..2c63886 100644
--- a/base/time/time.cc
+++ b/base/time/time.cc
@@ -17,40 +17,81 @@ namespace base {
// TimeDelta ------------------------------------------------------------------
+// static
+TimeDelta TimeDelta::Max() {
+ return TimeDelta(std::numeric_limits<int64>::max());
+}
+
int TimeDelta::InDays() const {
+ if (is_max()) {
+ // Preserve max to prevent overflow.
+ return std::numeric_limits<int>::max();
+ }
return static_cast<int>(delta_ / Time::kMicrosecondsPerDay);
}
int TimeDelta::InHours() const {
+ if (is_max()) {
+ // Preserve max to prevent overflow.
+ return std::numeric_limits<int>::max();
+ }
return static_cast<int>(delta_ / Time::kMicrosecondsPerHour);
}
int TimeDelta::InMinutes() const {
+ if (is_max()) {
+ // Preserve max to prevent overflow.
+ return std::numeric_limits<int>::max();
+ }
return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute);
}
double TimeDelta::InSecondsF() const {
+ if (is_max()) {
+ // Preserve max to prevent overflow.
+ return std::numeric_limits<double>::infinity();
+ }
return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond;
}
int64 TimeDelta::InSeconds() const {
+ if (is_max()) {
+ // Preserve max to prevent overflow.
+ return std::numeric_limits<int64>::max();
+ }
return delta_ / Time::kMicrosecondsPerSecond;
}
double TimeDelta::InMillisecondsF() const {
+ if (is_max()) {
+ // Preserve max to prevent overflow.
+ return std::numeric_limits<double>::infinity();
+ }
return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond;
}
int64 TimeDelta::InMilliseconds() const {
+ if (is_max()) {
+ // Preserve max to prevent overflow.
+ return std::numeric_limits<int64>::max();
+ }
return delta_ / Time::kMicrosecondsPerMillisecond;
}
int64 TimeDelta::InMillisecondsRoundedUp() const {
+ if (is_max()) {
+ // Preserve max to prevent overflow.
+ return std::numeric_limits<int64>::max();
+ }
return (delta_ + Time::kMicrosecondsPerMillisecond - 1) /
Time::kMicrosecondsPerMillisecond;
}
int64 TimeDelta::InMicroseconds() const {
+ if (is_max()) {
+ // Preserve max to prevent overflow.
+ return std::numeric_limits<int64>::max();
+ }
return delta_;
}
diff --git a/base/time/time.h b/base/time/time.h
index 40566b9..69a1324 100644
--- a/base/time/time.h
+++ b/base/time/time.h
@@ -61,9 +61,9 @@ class BASE_EXPORT TimeDelta {
}
// Converts units of time to TimeDeltas.
- static TimeDelta FromDays(int64 days);
- static TimeDelta FromHours(int64 hours);
- static TimeDelta FromMinutes(int64 minutes);
+ static TimeDelta FromDays(int days);
+ static TimeDelta FromHours(int hours);
+ static TimeDelta FromMinutes(int minutes);
static TimeDelta FromSeconds(int64 secs);
static TimeDelta FromMilliseconds(int64 ms);
static TimeDelta FromMicroseconds(int64 us);
@@ -79,6 +79,11 @@ class BASE_EXPORT TimeDelta {
return TimeDelta(delta);
}
+ // Returns the maximum time delta, which should be greater than any reasonable
+ // time delta we might compare it to. Adding or subtracting the maximum time
+ // delta to a time or another time delta has an undefined result.
+ static TimeDelta Max();
+
// Returns the internal numeric value of the TimeDelta object. Please don't
// use this and do arithmetic on it, as it is more error prone than using the
// provided operators.
@@ -87,6 +92,11 @@ class BASE_EXPORT TimeDelta {
return delta_;
}
+ // Returns true if the time delta is the maximum time delta.
+ bool is_max() const {
+ return delta_ == std::numeric_limits<int64>::max();
+ }
+
#if defined(OS_POSIX)
struct timespec ToTimeSpec() const;
#endif
@@ -493,32 +503,50 @@ class BASE_EXPORT Time {
// Inline the TimeDelta factory methods, for fast TimeDelta construction.
// static
-inline TimeDelta TimeDelta::FromDays(int64 days) {
+inline TimeDelta TimeDelta::FromDays(int days) {
+ // Preserve max to prevent overflow.
+ if (days == std::numeric_limits<int>::max())
+ return Max();
return TimeDelta(days * Time::kMicrosecondsPerDay);
}
// static
-inline TimeDelta TimeDelta::FromHours(int64 hours) {
+inline TimeDelta TimeDelta::FromHours(int hours) {
+ // Preserve max to prevent overflow.
+ if (hours == std::numeric_limits<int>::max())
+ return Max();
return TimeDelta(hours * Time::kMicrosecondsPerHour);
}
// static
-inline TimeDelta TimeDelta::FromMinutes(int64 minutes) {
+inline TimeDelta TimeDelta::FromMinutes(int minutes) {
+ // Preserve max to prevent overflow.
+ if (minutes == std::numeric_limits<int>::max())
+ return Max();
return TimeDelta(minutes * Time::kMicrosecondsPerMinute);
}
// static
inline TimeDelta TimeDelta::FromSeconds(int64 secs) {
+ // Preserve max to prevent overflow.
+ if (secs == std::numeric_limits<int64>::max())
+ return Max();
return TimeDelta(secs * Time::kMicrosecondsPerSecond);
}
// static
inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) {
+ // Preserve max to prevent overflow.
+ if (ms == std::numeric_limits<int64>::max())
+ return Max();
return TimeDelta(ms * Time::kMicrosecondsPerMillisecond);
}
// static
inline TimeDelta TimeDelta::FromMicroseconds(int64 us) {
+ // Preserve max to prevent overflow.
+ if (us == std::numeric_limits<int64>::max())
+ return Max();
return TimeDelta(us);
}
diff --git a/base/time/time_unittest.cc b/base/time/time_unittest.cc
index b5d2493..40d7bd4 100644
--- a/base/time/time_unittest.cc
+++ b/base/time/time_unittest.cc
@@ -481,6 +481,46 @@ TEST_F(TimeTest, ExplodeBeforeUnixEpoch) {
EXPECT_EQ(1, exploded.millisecond);
}
+TEST_F(TimeTest, TimeDeltaMax) {
+ TimeDelta max = TimeDelta::Max();
+ EXPECT_TRUE(max.is_max());
+ EXPECT_EQ(max, TimeDelta::Max());
+ EXPECT_GT(max, TimeDelta::FromDays(100 * 365));
+ EXPECT_GT(max, TimeDelta());
+}
+
+TEST_F(TimeTest, TimeDeltaMaxConversions) {
+ TimeDelta t = TimeDelta::Max();
+ EXPECT_EQ(std::numeric_limits<int64>::max(), t.ToInternalValue());
+
+ EXPECT_EQ(std::numeric_limits<int>::max(), t.InDays());
+ EXPECT_EQ(std::numeric_limits<int>::max(), t.InHours());
+ EXPECT_EQ(std::numeric_limits<int>::max(), t.InMinutes());
+ EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InSecondsF());
+ EXPECT_EQ(std::numeric_limits<int64>::max(), t.InSeconds());
+ EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InMillisecondsF());
+ EXPECT_EQ(std::numeric_limits<int64>::max(), t.InMilliseconds());
+ EXPECT_EQ(std::numeric_limits<int64>::max(), t.InMillisecondsRoundedUp());
+
+ t = TimeDelta::FromDays(std::numeric_limits<int>::max());
+ EXPECT_TRUE(t.is_max());
+
+ t = TimeDelta::FromHours(std::numeric_limits<int>::max());
+ EXPECT_TRUE(t.is_max());
+
+ t = TimeDelta::FromMinutes(std::numeric_limits<int>::max());
+ EXPECT_TRUE(t.is_max());
+
+ t = TimeDelta::FromSeconds(std::numeric_limits<int64>::max());
+ EXPECT_TRUE(t.is_max());
+
+ t = TimeDelta::FromMilliseconds(std::numeric_limits<int64>::max());
+ EXPECT_TRUE(t.is_max());
+
+ t = TimeDelta::FromMicroseconds(std::numeric_limits<int64>::max());
+ EXPECT_TRUE(t.is_max());
+}
+
TEST_F(TimeTest, Max) {
Time max = Time::Max();
EXPECT_TRUE(max.is_max());
diff --git a/cc/debug/rasterize_and_record_benchmark.cc b/cc/debug/rasterize_and_record_benchmark.cc
index 55f5677..ab8d1cb 100644
--- a/cc/debug/rasterize_and_record_benchmark.cc
+++ b/cc/debug/rasterize_and_record_benchmark.cc
@@ -108,8 +108,7 @@ void RasterizeAndRecordBenchmark::RunOnLayer(PictureLayer* layer) {
return;
- base::TimeDelta min_time =
- base::TimeDelta::FromInternalValue(std::numeric_limits<int64>::max());
+ base::TimeDelta min_time = base::TimeDelta::Max();
for (int i = 0; i < record_repeat_count_; ++i) {
base::TimeTicks start = Now();
scoped_refptr<Picture> picture = Picture::Create(
diff --git a/cc/debug/rasterize_and_record_benchmark_impl.cc b/cc/debug/rasterize_and_record_benchmark_impl.cc
index 5963923..6e0a46a 100644
--- a/cc/debug/rasterize_and_record_benchmark_impl.cc
+++ b/cc/debug/rasterize_and_record_benchmark_impl.cc
@@ -104,8 +104,7 @@ void RasterizeAndRecordBenchmarkImpl::RunOnLayer(PictureLayerImpl* layer) {
int tile_size = content_rect.width() * content_rect.height();
- base::TimeDelta min_time =
- base::TimeDelta::FromInternalValue(std::numeric_limits<int64>::max());
+ base::TimeDelta min_time = base::TimeDelta::Max();
bool is_solid_color = false;
for (int i = 0; i < rasterize_repeat_count_; ++i) {
diff --git a/cc/resources/picture_pile.cc b/cc/resources/picture_pile.cc
index 7e5a822..43b13c5 100644
--- a/cc/resources/picture_pile.cc
+++ b/cc/resources/picture_pile.cc
@@ -228,8 +228,7 @@ bool PicturePile::Update(
bool gather_pixel_refs = num_raster_threads > 1;
{
- base::TimeDelta best_duration = base::TimeDelta::FromInternalValue(
- std::numeric_limits<int64>::max());
+ base::TimeDelta best_duration = base::TimeDelta::Max();
for (int i = 0; i < repeat_count; i++) {
base::TimeTicks start_time = stats_instrumentation->StartRecording();
picture = Picture::Create(record_rect,
diff --git a/cc/resources/picture_pile_impl.cc b/cc/resources/picture_pile_impl.cc
index 7e29dff..2ee6517 100644
--- a/cc/resources/picture_pile_impl.cc
+++ b/cc/resources/picture_pile_impl.cc
@@ -247,8 +247,7 @@ void PicturePileImpl::RasterCommon(
total_clip.Union(positive_clip);
#endif // NDEBUG
- base::TimeDelta best_duration =
- base::TimeDelta::FromInternalValue(std::numeric_limits<int64>::max());
+ base::TimeDelta best_duration = base::TimeDelta::Max();
int repeat_count = std::max(1, slow_down_raster_scale_factor_for_debug_);
int rasterized_pixel_count = 0;
diff --git a/content/renderer/media/websourcebuffer_impl.cc b/content/renderer/media/websourcebuffer_impl.cc
index 6863076..f9c6bdf 100644
--- a/content/renderer/media/websourcebuffer_impl.cc
+++ b/content/renderer/media/websourcebuffer_impl.cc
@@ -15,6 +15,8 @@ static base::TimeDelta DoubleToTimeDelta(double time) {
if (time == std::numeric_limits<double>::infinity())
return media::kInfiniteDuration();
+ // Don't use base::TimeDelta::Max() here, as we want the largest finite time
+ // delta.
base::TimeDelta max_time = base::TimeDelta::FromInternalValue(kint64max - 1);
double max_time_in_seconds = max_time.InSecondsF();
diff --git a/media/base/buffers.h b/media/base/buffers.h
index 6a6c730..5c5c47b 100644
--- a/media/base/buffers.h
+++ b/media/base/buffers.h
@@ -37,7 +37,7 @@ MEDIA_EXPORT extern inline base::TimeDelta kNoTimestamp() {
// Represents an infinite stream duration.
MEDIA_EXPORT extern inline base::TimeDelta kInfiniteDuration() {
- return base::TimeDelta::FromMicroseconds(kint64max);
+ return base::TimeDelta::Max();
}
} // namespace media
diff --git a/media/base/media_log.cc b/media/base/media_log.cc
index 6e21cff..c689d7a 100644
--- a/media/base/media_log.cc
+++ b/media/base/media_log.cc
@@ -141,7 +141,10 @@ scoped_ptr<MediaLogEvent> MediaLog::CreateStringEvent(
scoped_ptr<MediaLogEvent> MediaLog::CreateTimeEvent(
MediaLogEvent::Type type, const char* property, base::TimeDelta value) {
scoped_ptr<MediaLogEvent> event(CreateEvent(type));
- event->params.SetDouble(property, value.InSecondsF());
+ if (value.is_max())
+ event->params.SetString(property, "unknown");
+ else
+ event->params.SetDouble(property, value.InSecondsF());
return event.Pass();
}
@@ -228,4 +231,14 @@ void MediaLog::SetBooleanProperty(
AddEvent(event.Pass());
}
+void MediaLog::SetTimeProperty(
+ const char* key, base::TimeDelta value) {
+ scoped_ptr<MediaLogEvent> event(CreateEvent(MediaLogEvent::PROPERTY_CHANGE));
+ if (value.is_max())
+ event->params.SetString(key, "unknown");
+ else
+ event->params.SetDouble(key, value.InSecondsF());
+ AddEvent(event.Pass());
+}
+
} //namespace media
diff --git a/media/base/media_log.h b/media/base/media_log.h
index 1d25c09..f342ee8 100644
--- a/media/base/media_log.h
+++ b/media/base/media_log.h
@@ -73,6 +73,7 @@ class MEDIA_EXPORT MediaLog : public base::RefCountedThreadSafe<MediaLog> {
void SetIntegerProperty(const char* key, int value);
void SetDoubleProperty(const char* key, double value);
void SetBooleanProperty(const char* key, bool value);
+ void SetTimeProperty(const char* key, base::TimeDelta value);
protected:
friend class base::RefCountedThreadSafe<MediaLog>;
diff --git a/media/filters/chunk_demuxer.cc b/media/filters/chunk_demuxer.cc
index ca16d51..2e22cd8 100644
--- a/media/filters/chunk_demuxer.cc
+++ b/media/filters/chunk_demuxer.cc
@@ -1537,6 +1537,7 @@ void ChunkDemuxer::SetDuration(double duration) {
// This can be different if the value of |duration| doesn't fit the range or
// precision of TimeDelta.
TimeDelta min_duration = TimeDelta::FromInternalValue(1);
+ // Don't use TimeDelta::Max() here, as we want the largest finite time delta.
TimeDelta max_duration = TimeDelta::FromInternalValue(kint64max - 1);
double min_duration_in_seconds = min_duration.InSecondsF();
double max_duration_in_seconds = max_duration.InSecondsF();
diff --git a/media/filters/ffmpeg_demuxer.cc b/media/filters/ffmpeg_demuxer.cc
index 6273b59..34bc0d3 100644
--- a/media/filters/ffmpeg_demuxer.cc
+++ b/media/filters/ffmpeg_demuxer.cc
@@ -733,8 +733,8 @@ void FFmpegDemuxer::OnFindStreamInfoDone(const PipelineStatusCB& status_cb,
}
- media_log_->SetDoubleProperty("max_duration", max_duration.InSecondsF());
- media_log_->SetDoubleProperty("start_time", start_time_.InSecondsF());
+ media_log_->SetTimeProperty("max_duration", max_duration);
+ media_log_->SetTimeProperty("start_time", start_time_);
media_log_->SetIntegerProperty("bitrate", bitrate_);
status_cb.Run(PIPELINE_OK);
diff --git a/net/http/http_response_headers.cc b/net/http/http_response_headers.cc
index 0072976..f26292a 100644
--- a/net/http/http_response_headers.cc
+++ b/net/http/http_response_headers.cc
@@ -1009,7 +1009,7 @@ TimeDelta HttpResponseHeaders::GetFreshnessLifetime(
// These responses are implicitly fresh (unless otherwise overruled):
if (response_code_ == 300 || response_code_ == 301 || response_code_ == 410)
- return TimeDelta::FromMicroseconds(kint64max);
+ return TimeDelta::Max();
return TimeDelta(); // not fresh
}
diff --git a/net/spdy/spdy_session.cc b/net/spdy/spdy_session.cc
index 9c06192..3ba558a 100644
--- a/net/spdy/spdy_session.cc
+++ b/net/spdy/spdy_session.cc
@@ -2725,9 +2725,7 @@ void SpdySession::CheckPingStatus(base::TimeTicks last_check_time) {
if (delay.InMilliseconds() < 0 || last_activity_time_ < last_check_time) {
// Track all failed PING messages in a separate bucket.
- const base::TimeDelta kFailedPing =
- base::TimeDelta::FromInternalValue(INT_MAX);
- RecordPingRTTHistogram(kFailedPing);
+ RecordPingRTTHistogram(base::TimeDelta::Max());
CloseSessionResult result =
DoCloseSession(ERR_SPDY_PING_FAILED, "Failed ping.");
DCHECK_EQ(result, SESSION_CLOSED_AND_REMOVED);
diff --git a/sync/sessions/nudge_tracker.cc b/sync/sessions/nudge_tracker.cc
index 22a9fe0..5b599b8 100644
--- a/sync/sessions/nudge_tracker.cc
+++ b/sync/sessions/nudge_tracker.cc
@@ -174,11 +174,9 @@ bool NudgeTracker::IsTypeThrottled(ModelType type) const {
base::TimeDelta NudgeTracker::GetTimeUntilNextUnthrottle(
base::TimeTicks now) const {
DCHECK(IsAnyTypeThrottled()) << "This function requires a pending unthrottle";
- const base::TimeDelta kMaxTimeDelta =
- base::TimeDelta::FromInternalValue(kint64max);
// Return min of GetTimeUntilUnthrottle() values for all IsThrottled() types.
- base::TimeDelta time_until_next_unthrottle = kMaxTimeDelta;
+ base::TimeDelta time_until_next_unthrottle = base::TimeDelta::Max();
for (TypeTrackerMap::const_iterator it = type_trackers_.begin();
it != type_trackers_.end(); ++it) {
if (it->second.IsThrottled()) {
@@ -187,7 +185,7 @@ base::TimeDelta NudgeTracker::GetTimeUntilNextUnthrottle(
it->second.GetTimeUntilUnthrottle(now));
}
}
- DCHECK(kMaxTimeDelta != time_until_next_unthrottle);
+ DCHECK(!time_until_next_unthrottle.is_max());
return time_until_next_unthrottle;
}