summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--base/base.gyp12
-rw-r--r--base/base.gypi33
-rw-r--r--base/base_untrusted.gyp2
-rw-r--r--base/time.h652
-rw-r--r--base/time/clock.h6
-rw-r--r--base/time/pr_time_unittest.cc (renamed from base/pr_time_unittest.cc)2
-rw-r--r--base/time/tick_clock.h6
-rw-r--r--base/time/time.cc (renamed from base/time.cc)5
-rw-r--r--base/time/time.h651
-rw-r--r--base/time/time_mac.cc (renamed from base/time_mac.cc)2
-rw-r--r--base/time/time_posix.cc (renamed from base/time_posix.cc)2
-rw-r--r--base/time/time_unittest.cc (renamed from base/time_unittest.cc)2
-rw-r--r--base/time/time_win.cc (renamed from base/time_win.cc)4
-rw-r--r--base/time/time_win_unittest.cc (renamed from base/time_win_unittest.cc)2
-rw-r--r--base/timer.h247
-rw-r--r--base/timer/hi_res_timer_manager.h (renamed from base/hi_res_timer_manager.h)10
-rw-r--r--base/timer/hi_res_timer_manager_posix.cc (renamed from base/hi_res_timer_manager_posix.cc)6
-rw-r--r--base/timer/hi_res_timer_manager_unittest.cc (renamed from base/hi_res_timer_manager_unittest.cc)8
-rw-r--r--base/timer/hi_res_timer_manager_win.cc (renamed from base/hi_res_timer_manager_win.cc)8
-rw-r--r--base/timer/timer.cc (renamed from base/timer.cc)0
-rw-r--r--base/timer/timer.h246
-rw-r--r--base/timer/timer_unittest.cc (renamed from base/timer_unittest.cc)0
-rw-r--r--chrome/nacl/nacl_exe_win_64.cc4
-rw-r--r--chrome/nacl/nacl_main.cc4
-rw-r--r--content/browser/browser_main_loop.cc4
-rw-r--r--content/browser/browser_main_loop.h4
-rw-r--r--content/plugin/plugin_main.cc4
-rw-r--r--content/renderer/renderer_main.cc6
-rw-r--r--content/utility/utility_main.cc4
-rw-r--r--content/worker/worker_main.cc4
30 files changed, 984 insertions, 956 deletions
diff --git a/base/base.gyp b/base/base.gyp
index 85c9751..f1ec2d9 100644
--- a/base/base.gyp
+++ b/base/base.gyp
@@ -486,7 +486,6 @@
'files/scoped_temp_dir_unittest.cc',
'gmock_unittest.cc',
'guid_unittest.cc',
- 'hi_res_timer_manager_unittest.cc',
'id_map_unittest.cc',
'i18n/break_iterator_unittest.cc',
'i18n/char_iterator_unittest.cc',
@@ -550,7 +549,6 @@
'posix/file_descriptor_shuffle_unittest.cc',
'posix/unix_domain_socket_linux_unittest.cc',
'power_monitor/power_monitor_unittest.cc',
- 'pr_time_unittest.cc',
'prefs/default_pref_store_unittest.cc',
'prefs/json_pref_store_unittest.cc',
'prefs/mock_pref_change_callback.h',
@@ -615,9 +613,11 @@
'threading/watchdog_unittest.cc',
'threading/worker_pool_posix_unittest.cc',
'threading/worker_pool_unittest.cc',
- 'time_unittest.cc',
- 'time_win_unittest.cc',
- 'timer_unittest.cc',
+ 'time/pr_time_unittest.cc',
+ 'time/time_unittest.cc',
+ 'time/time_win_unittest.cc',
+ 'timer/hi_res_timer_manager_unittest.cc',
+ 'timer/timer_unittest.cc',
'tools_sanity_unittest.cc',
'tracked_objects_unittest.cc',
'tuple_unittest.cc',
@@ -797,7 +797,7 @@
],
'sources!': [
'debug/trace_event_win_unittest.cc',
- 'time_win_unittest.cc',
+ 'time/time_win_unittest.cc',
'win/win_util_unittest.cc',
],
}],
diff --git a/base/base.gypi b/base/base.gypi
index 7bfd924..1a36f27 100644
--- a/base/base.gypi
+++ b/base/base.gypi
@@ -199,9 +199,6 @@
'guid_win.cc',
'hash.cc',
'hash.h',
- 'hi_res_timer_manager_posix.cc',
- 'hi_res_timer_manager_win.cc',
- 'hi_res_timer_manager.h',
'id_map.h',
'ini_parser.cc',
'ini_parser.h',
@@ -345,6 +342,10 @@
'native_library_mac.mm',
'native_library_posix.cc',
'native_library_win.cc',
+ 'nix/mime_util_xdg.cc',
+ 'nix/mime_util_xdg.h',
+ 'nix/xdg_util.cc',
+ 'nix/xdg_util.h',
'observer_list.h',
'observer_list_threadsafe.h',
'os_compat_android.cc',
@@ -555,14 +556,16 @@
'time/default_tick_clock.h',
'time/tick_clock.cc',
'time/tick_clock.h',
- # TODO(akalin): Move time* into time/.
- 'time.cc',
- 'time.h',
- 'time_mac.cc',
- 'time_posix.cc',
- 'time_win.cc',
- 'timer.cc',
- 'timer.h',
+ 'time/time.cc',
+ 'time/time.h',
+ 'time/time_mac.cc',
+ 'time/time_posix.cc',
+ 'time/time_win.cc',
+ 'timer/hi_res_timer_manager_posix.cc',
+ 'timer/hi_res_timer_manager_win.cc',
+ 'timer/hi_res_timer_manager.h',
+ 'timer/timer.cc',
+ 'timer/timer.h',
'tracked_objects.cc',
'tracked_objects.h',
'tracking_info.cc',
@@ -576,10 +579,6 @@
'version.h',
'vlog.cc',
'vlog.h',
- 'nix/mime_util_xdg.cc',
- 'nix/mime_util_xdg.h',
- 'nix/xdg_util.cc',
- 'nix/xdg_util.h',
'win/enum_variant.cc',
'win/enum_variant.h',
'win/event_trace_consumer.h',
@@ -748,9 +747,9 @@
['include', '^mac/scoped_nsobject\\.'],
['include', '^memory/discardable_memory_mac\\.'],
['include', '^message_loop/message_pump_mac\\.'],
- ['include', '^threading/platform_thread_mac\\.'],
['include', '^strings/sys_string_conversions_mac\\.'],
- ['include', '^time_mac\\.'],
+ ['include', '^threading/platform_thread_mac\\.'],
+ ['include', '^time/time_mac\\.'],
['include', '^worker_pool_mac\\.'],
# Exclude all process_util except the minimal implementation
# needed on iOS (mostly for unit tests).
diff --git a/base/base_untrusted.gyp b/base/base_untrusted.gyp
index d4891dc..fc088f8 100644
--- a/base/base_untrusted.gyp
+++ b/base/base_untrusted.gyp
@@ -27,7 +27,7 @@
'base_switches.h',
'strings/string16.cc',
'sync_socket_nacl.cc',
- 'time_posix.cc',
+ 'time/time_posix.cc',
],
},
'dependencies': [
diff --git a/base/time.h b/base/time.h
index e722122..2a7df7d 100644
--- a/base/time.h
+++ b/base/time.h
@@ -1,651 +1,7 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-// Time represents an absolute point in time, internally represented as
-// microseconds (s/1,000,000) since the Windows epoch (1601-01-01 00:00:00 UTC)
-// (See http://crbug.com/14734). System-dependent clock interface routines are
-// defined in time_PLATFORM.cc.
-//
-// TimeDelta represents a duration of time, internally represented in
-// microseconds.
-//
-// TimeTicks represents an abstract time that is most of the time incrementing
-// for use in measuring time durations. It is internally represented in
-// microseconds. It can not be converted to a human-readable time, but is
-// guaranteed not to decrease (if the user changes the computer clock,
-// Time::Now() may actually decrease or jump). But note that TimeTicks may
-// "stand still", for example if the computer suspended.
-//
-// These classes are represented as only a 64-bit value, so they can be
-// efficiently passed by value.
-
-#ifndef BASE_TIME_H_
-#define BASE_TIME_H_
-
-#include <time.h>
-
-#include "base/atomicops.h"
-#include "base/base_export.h"
-#include "base/basictypes.h"
-
-#if defined(OS_MACOSX)
-#include <CoreFoundation/CoreFoundation.h>
-// Avoid Mac system header macro leak.
-#undef TYPE_BOOL
-#endif
-
-#if defined(OS_POSIX)
-// For struct timeval.
-#include <sys/time.h>
-#endif
-
-#if defined(OS_WIN)
-// For FILETIME in FromFileTime, until it moves to a new converter class.
-// See TODO(iyengar) below.
-#include <windows.h>
-#endif
-
-#include <limits>
-
-namespace base {
-
-class Time;
-class TimeTicks;
-
-// TimeDelta ------------------------------------------------------------------
-
-class BASE_EXPORT TimeDelta {
- public:
- TimeDelta() : delta_(0) {
- }
-
- // Converts units of time to TimeDeltas.
- static TimeDelta FromDays(int64 days);
- static TimeDelta FromHours(int64 hours);
- static TimeDelta FromMinutes(int64 minutes);
- static TimeDelta FromSeconds(int64 secs);
- static TimeDelta FromMilliseconds(int64 ms);
- static TimeDelta FromMicroseconds(int64 us);
-#if defined(OS_WIN)
- static TimeDelta FromQPCValue(LONGLONG qpc_value);
-#endif
-
- // Converts an integer value representing TimeDelta to a class. This is used
- // when deserializing a |TimeDelta| structure, using a value known to be
- // compatible. It is not provided as a constructor because the integer type
- // may be unclear from the perspective of a caller.
- static TimeDelta FromInternalValue(int64 delta) {
- return TimeDelta(delta);
- }
-
- // 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.
- // For serializing, use FromInternalValue to reconstitute.
- int64 ToInternalValue() const {
- return delta_;
- }
-
-#if defined(OS_POSIX)
- struct timespec ToTimeSpec() const;
-#endif
-
- // Returns the time delta in some unit. The F versions return a floating
- // point value, the "regular" versions return a rounded-down value.
- //
- // InMillisecondsRoundedUp() instead returns an integer that is rounded up
- // to the next full millisecond.
- int InDays() const;
- int InHours() const;
- int InMinutes() const;
- double InSecondsF() const;
- int64 InSeconds() const;
- double InMillisecondsF() const;
- int64 InMilliseconds() const;
- int64 InMillisecondsRoundedUp() const;
- int64 InMicroseconds() const;
-
- TimeDelta& operator=(TimeDelta other) {
- delta_ = other.delta_;
- return *this;
- }
-
- // Computations with other deltas.
- TimeDelta operator+(TimeDelta other) const {
- return TimeDelta(delta_ + other.delta_);
- }
- TimeDelta operator-(TimeDelta other) const {
- return TimeDelta(delta_ - other.delta_);
- }
-
- TimeDelta& operator+=(TimeDelta other) {
- delta_ += other.delta_;
- return *this;
- }
- TimeDelta& operator-=(TimeDelta other) {
- delta_ -= other.delta_;
- return *this;
- }
- TimeDelta operator-() const {
- return TimeDelta(-delta_);
- }
-
- // Computations with ints, note that we only allow multiplicative operations
- // with ints, and additive operations with other deltas.
- TimeDelta operator*(int64 a) const {
- return TimeDelta(delta_ * a);
- }
- TimeDelta operator/(int64 a) const {
- return TimeDelta(delta_ / a);
- }
- TimeDelta& operator*=(int64 a) {
- delta_ *= a;
- return *this;
- }
- TimeDelta& operator/=(int64 a) {
- delta_ /= a;
- return *this;
- }
- int64 operator/(TimeDelta a) const {
- return delta_ / a.delta_;
- }
-
- // Defined below because it depends on the definition of the other classes.
- Time operator+(Time t) const;
- TimeTicks operator+(TimeTicks t) const;
-
- // Comparison operators.
- bool operator==(TimeDelta other) const {
- return delta_ == other.delta_;
- }
- bool operator!=(TimeDelta other) const {
- return delta_ != other.delta_;
- }
- bool operator<(TimeDelta other) const {
- return delta_ < other.delta_;
- }
- bool operator<=(TimeDelta other) const {
- return delta_ <= other.delta_;
- }
- bool operator>(TimeDelta other) const {
- return delta_ > other.delta_;
- }
- bool operator>=(TimeDelta other) const {
- return delta_ >= other.delta_;
- }
-
- private:
- friend class Time;
- friend class TimeTicks;
- friend TimeDelta operator*(int64 a, TimeDelta td);
-
- // Constructs a delta given the duration in microseconds. This is private
- // to avoid confusion by callers with an integer constructor. Use
- // FromSeconds, FromMilliseconds, etc. instead.
- explicit TimeDelta(int64 delta_us) : delta_(delta_us) {
- }
-
- // Delta in microseconds.
- int64 delta_;
-};
-
-inline TimeDelta operator*(int64 a, TimeDelta td) {
- return TimeDelta(a * td.delta_);
-}
-
-// Time -----------------------------------------------------------------------
-
-// Represents a wall clock time.
-class BASE_EXPORT Time {
- public:
- static const int64 kMillisecondsPerSecond = 1000;
- static const int64 kMicrosecondsPerMillisecond = 1000;
- static const int64 kMicrosecondsPerSecond = kMicrosecondsPerMillisecond *
- kMillisecondsPerSecond;
- static const int64 kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60;
- static const int64 kMicrosecondsPerHour = kMicrosecondsPerMinute * 60;
- static const int64 kMicrosecondsPerDay = kMicrosecondsPerHour * 24;
- static const int64 kMicrosecondsPerWeek = kMicrosecondsPerDay * 7;
- static const int64 kNanosecondsPerMicrosecond = 1000;
- static const int64 kNanosecondsPerSecond = kNanosecondsPerMicrosecond *
- kMicrosecondsPerSecond;
-
-#if !defined(OS_WIN)
- // On Mac & Linux, this value is the delta from the Windows epoch of 1601 to
- // the Posix delta of 1970. This is used for migrating between the old
- // 1970-based epochs to the new 1601-based ones. It should be removed from
- // this global header and put in the platform-specific ones when we remove the
- // migration code.
- static const int64 kWindowsEpochDeltaMicroseconds;
-#endif
-
- // Represents an exploded time that can be formatted nicely. This is kind of
- // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few
- // additions and changes to prevent errors.
- struct BASE_EXPORT Exploded {
- int year; // Four digit year "2007"
- int month; // 1-based month (values 1 = January, etc.)
- int day_of_week; // 0-based day of week (0 = Sunday, etc.)
- int day_of_month; // 1-based day of month (1-31)
- int hour; // Hour within the current day (0-23)
- int minute; // Minute within the current hour (0-59)
- int second; // Second within the current minute (0-59 plus leap
- // seconds which may take it up to 60).
- int millisecond; // Milliseconds within the current second (0-999)
-
- // A cursory test for whether the data members are within their
- // respective ranges. A 'true' return value does not guarantee the
- // Exploded value can be successfully converted to a Time value.
- bool HasValidValues() const;
- };
-
- // Contains the NULL time. Use Time::Now() to get the current time.
- Time() : us_(0) {
- }
-
- // Returns true if the time object has not been initialized.
- bool is_null() const {
- return us_ == 0;
- }
-
- // Returns true if the time object is the maximum time.
- bool is_max() const {
- return us_ == std::numeric_limits<int64>::max();
- }
-
- // Returns the time for epoch in Unix-like system (Jan 1, 1970).
- static Time UnixEpoch();
-
- // Returns the current time. Watch out, the system might adjust its clock
- // in which case time will actually go backwards. We don't guarantee that
- // times are increasing, or that two calls to Now() won't be the same.
- static Time Now();
-
- // Returns the maximum time, which should be greater than any reasonable time
- // with which we might compare it.
- static Time Max();
-
- // Returns the current time. Same as Now() except that this function always
- // uses system time so that there are no discrepancies between the returned
- // time and system time even on virtual environments including our test bot.
- // For timing sensitive unittests, this function should be used.
- static Time NowFromSystemTime();
-
- // Converts to/from time_t in UTC and a Time class.
- // TODO(brettw) this should be removed once everybody starts using the |Time|
- // class.
- static Time FromTimeT(time_t tt);
- time_t ToTimeT() const;
-
- // Converts time to/from a double which is the number of seconds since epoch
- // (Jan 1, 1970). Webkit uses this format to represent time.
- // Because WebKit initializes double time value to 0 to indicate "not
- // initialized", we map it to empty Time object that also means "not
- // initialized".
- static Time FromDoubleT(double dt);
- double ToDoubleT() const;
-
-#if defined(OS_POSIX)
- // Converts the timespec structure to time. MacOS X 10.8.3 (and tentatively,
- // earlier versions) will have the |ts|'s tv_nsec component zeroed out,
- // having a 1 second resolution, which agrees with
- // https://developer.apple.com/legacy/library/#technotes/tn/tn1150.html#HFSPlusDates.
- static Time FromTimeSpec(const timespec& ts);
-#endif
-
- // Converts to/from the Javascript convention for times, a number of
- // milliseconds since the epoch:
- // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime.
- static Time FromJsTime(double ms_since_epoch);
- double ToJsTime() const;
-
-#if defined(OS_POSIX)
- static Time FromTimeVal(struct timeval t);
- struct timeval ToTimeVal() const;
-#endif
-
-#if defined(OS_MACOSX)
- static Time FromCFAbsoluteTime(CFAbsoluteTime t);
- CFAbsoluteTime ToCFAbsoluteTime() const;
-#endif
-
-#if defined(OS_WIN)
- static Time FromFileTime(FILETIME ft);
- FILETIME ToFileTime() const;
-
- // The minimum time of a low resolution timer. This is basically a windows
- // constant of ~15.6ms. While it does vary on some older OS versions, we'll
- // treat it as static across all windows versions.
- static const int kMinLowResolutionThresholdMs = 16;
-
- // Enable or disable Windows high resolution timer. If the high resolution
- // timer is not enabled, calls to ActivateHighResolutionTimer will fail.
- // When disabling the high resolution timer, this function will not cause
- // the high resolution timer to be deactivated, but will prevent future
- // activations.
- // Must be called from the main thread.
- // For more details see comments in time_win.cc.
- static void EnableHighResolutionTimer(bool enable);
-
- // Activates or deactivates the high resolution timer based on the |activate|
- // flag. If the HighResolutionTimer is not Enabled (see
- // EnableHighResolutionTimer), this function will return false. Otherwise
- // returns true. Each successful activate call must be paired with a
- // subsequent deactivate call.
- // All callers to activate the high resolution timer must eventually call
- // this function to deactivate the high resolution timer.
- static bool ActivateHighResolutionTimer(bool activate);
-
- // Returns true if the high resolution timer is both enabled and activated.
- // This is provided for testing only, and is not tracked in a thread-safe
- // way.
- static bool IsHighResolutionTimerInUse();
-#endif
-
- // Converts an exploded structure representing either the local time or UTC
- // into a Time class.
- static Time FromUTCExploded(const Exploded& exploded) {
- return FromExploded(false, exploded);
- }
- static Time FromLocalExploded(const Exploded& exploded) {
- return FromExploded(true, exploded);
- }
-
- // Converts an integer value representing Time to a class. This is used
- // when deserializing a |Time| structure, using a value known to be
- // compatible. It is not provided as a constructor because the integer type
- // may be unclear from the perspective of a caller.
- static Time FromInternalValue(int64 us) {
- return Time(us);
- }
-
- // Converts a string representation of time to a Time object.
- // An example of a time string which is converted is as below:-
- // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified
- // in the input string, FromString assumes local time and FromUTCString
- // assumes UTC. A timezone that cannot be parsed (e.g. "UTC" which is not
- // specified in RFC822) is treated as if the timezone is not specified.
- // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to
- // a new time converter class.
- static bool FromString(const char* time_string, Time* parsed_time) {
- return FromStringInternal(time_string, true, parsed_time);
- }
- static bool FromUTCString(const char* time_string, Time* parsed_time) {
- return FromStringInternal(time_string, false, parsed_time);
- }
-
- // For serializing, use FromInternalValue to reconstitute. Please don't use
- // this and do arithmetic on it, as it is more error prone than using the
- // provided operators.
- int64 ToInternalValue() const {
- return us_;
- }
-
- // Fills the given exploded structure with either the local time or UTC from
- // this time structure (containing UTC).
- void UTCExplode(Exploded* exploded) const {
- return Explode(false, exploded);
- }
- void LocalExplode(Exploded* exploded) const {
- return Explode(true, exploded);
- }
-
- // Rounds this time down to the nearest day in local time. It will represent
- // midnight on that day.
- Time LocalMidnight() const;
-
- Time& operator=(Time other) {
- us_ = other.us_;
- return *this;
- }
-
- // Compute the difference between two times.
- TimeDelta operator-(Time other) const {
- return TimeDelta(us_ - other.us_);
- }
-
- // Modify by some time delta.
- Time& operator+=(TimeDelta delta) {
- us_ += delta.delta_;
- return *this;
- }
- Time& operator-=(TimeDelta delta) {
- us_ -= delta.delta_;
- return *this;
- }
-
- // Return a new time modified by some delta.
- Time operator+(TimeDelta delta) const {
- return Time(us_ + delta.delta_);
- }
- Time operator-(TimeDelta delta) const {
- return Time(us_ - delta.delta_);
- }
-
- // Comparison operators
- bool operator==(Time other) const {
- return us_ == other.us_;
- }
- bool operator!=(Time other) const {
- return us_ != other.us_;
- }
- bool operator<(Time other) const {
- return us_ < other.us_;
- }
- bool operator<=(Time other) const {
- return us_ <= other.us_;
- }
- bool operator>(Time other) const {
- return us_ > other.us_;
- }
- bool operator>=(Time other) const {
- return us_ >= other.us_;
- }
-
- private:
- friend class TimeDelta;
-
- explicit Time(int64 us) : us_(us) {
- }
-
- // Explodes the given time to either local time |is_local = true| or UTC
- // |is_local = false|.
- void Explode(bool is_local, Exploded* exploded) const;
-
- // Unexplodes a given time assuming the source is either local time
- // |is_local = true| or UTC |is_local = false|.
- static Time FromExploded(bool is_local, const Exploded& exploded);
-
- // Converts a string representation of time to a Time object.
- // An example of a time string which is converted is as below:-
- // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified
- // in the input string, local time |is_local = true| or
- // UTC |is_local = false| is assumed. A timezone that cannot be parsed
- // (e.g. "UTC" which is not specified in RFC822) is treated as if the
- // timezone is not specified.
- static bool FromStringInternal(const char* time_string,
- bool is_local,
- Time* parsed_time);
-
- // The representation of Jan 1, 1970 UTC in microseconds since the
- // platform-dependent epoch.
- static const int64 kTimeTToMicrosecondsOffset;
-
-#if defined(OS_WIN)
- // Indicates whether fast timers are usable right now. For instance,
- // when using battery power, we might elect to prevent high speed timers
- // which would draw more power.
- static bool high_resolution_timer_enabled_;
- // Count of activations on the high resolution timer. Only use in tests
- // which are single threaded.
- static int high_resolution_timer_activated_;
-#endif
-
- // Time in microseconds in UTC.
- int64 us_;
-};
-
-// Inline the TimeDelta factory methods, for fast TimeDelta construction.
-
-// static
-inline TimeDelta TimeDelta::FromDays(int64 days) {
- return TimeDelta(days * Time::kMicrosecondsPerDay);
-}
-
-// static
-inline TimeDelta TimeDelta::FromHours(int64 hours) {
- return TimeDelta(hours * Time::kMicrosecondsPerHour);
-}
-
-// static
-inline TimeDelta TimeDelta::FromMinutes(int64 minutes) {
- return TimeDelta(minutes * Time::kMicrosecondsPerMinute);
-}
-
-// static
-inline TimeDelta TimeDelta::FromSeconds(int64 secs) {
- return TimeDelta(secs * Time::kMicrosecondsPerSecond);
-}
-
-// static
-inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) {
- return TimeDelta(ms * Time::kMicrosecondsPerMillisecond);
-}
-
-// static
-inline TimeDelta TimeDelta::FromMicroseconds(int64 us) {
- return TimeDelta(us);
-}
-
-inline Time TimeDelta::operator+(Time t) const {
- return Time(t.us_ + delta_);
-}
-
-// TimeTicks ------------------------------------------------------------------
-
-class BASE_EXPORT TimeTicks {
- public:
- TimeTicks() : ticks_(0) {
- }
-
- // Platform-dependent tick count representing "right now."
- // The resolution of this clock is ~1-15ms. Resolution varies depending
- // on hardware/operating system configuration.
- static TimeTicks Now();
-
- // Returns a platform-dependent high-resolution tick count. Implementation
- // is hardware dependent and may or may not return sub-millisecond
- // resolution. THIS CALL IS GENERALLY MUCH MORE EXPENSIVE THAN Now() AND
- // SHOULD ONLY BE USED WHEN IT IS REALLY NEEDED.
- static TimeTicks HighResNow();
-
- // Returns the current system trace time or, if none is defined, the current
- // high-res time (i.e. HighResNow()). On systems where a global trace clock
- // is defined, timestamping TraceEvents's with this value guarantees
- // synchronization between events collected inside chrome and events
- // collected outside (e.g. kernel, X server).
- static TimeTicks NowFromSystemTraceTime();
-
-#if defined(OS_WIN)
- // Get the absolute value of QPC time drift. For testing.
- static int64 GetQPCDriftMicroseconds();
-
- static TimeTicks FromQPCValue(LONGLONG qpc_value);
-
- // Returns true if the high resolution clock is working on this system.
- // This is only for testing.
- static bool IsHighResClockWorking();
-#endif
-
- // Returns true if this object has not been initialized.
- bool is_null() const {
- return ticks_ == 0;
- }
-
- // Converts an integer value representing TimeTicks to a class. This is used
- // when deserializing a |TimeTicks| structure, using a value known to be
- // compatible. It is not provided as a constructor because the integer type
- // may be unclear from the perspective of a caller.
- static TimeTicks FromInternalValue(int64 ticks) {
- return TimeTicks(ticks);
- }
-
- // Returns the internal numeric value of the TimeTicks object.
- // For serializing, use FromInternalValue to reconstitute.
- int64 ToInternalValue() const {
- return ticks_;
- }
-
- TimeTicks& operator=(TimeTicks other) {
- ticks_ = other.ticks_;
- return *this;
- }
-
- // Compute the difference between two times.
- TimeDelta operator-(TimeTicks other) const {
- return TimeDelta(ticks_ - other.ticks_);
- }
-
- // Modify by some time delta.
- TimeTicks& operator+=(TimeDelta delta) {
- ticks_ += delta.delta_;
- return *this;
- }
- TimeTicks& operator-=(TimeDelta delta) {
- ticks_ -= delta.delta_;
- return *this;
- }
-
- // Return a new TimeTicks modified by some delta.
- TimeTicks operator+(TimeDelta delta) const {
- return TimeTicks(ticks_ + delta.delta_);
- }
- TimeTicks operator-(TimeDelta delta) const {
- return TimeTicks(ticks_ - delta.delta_);
- }
-
- // Comparison operators
- bool operator==(TimeTicks other) const {
- return ticks_ == other.ticks_;
- }
- bool operator!=(TimeTicks other) const {
- return ticks_ != other.ticks_;
- }
- bool operator<(TimeTicks other) const {
- return ticks_ < other.ticks_;
- }
- bool operator<=(TimeTicks other) const {
- return ticks_ <= other.ticks_;
- }
- bool operator>(TimeTicks other) const {
- return ticks_ > other.ticks_;
- }
- bool operator>=(TimeTicks other) const {
- return ticks_ >= other.ticks_;
- }
-
- protected:
- friend class TimeDelta;
-
- // Please use Now() to create a new object. This is for internal use
- // and testing. Ticks is in microseconds.
- explicit TimeTicks(int64 ticks) : ticks_(ticks) {
- }
-
- // Tick count in microseconds.
- int64 ticks_;
-
-#if defined(OS_WIN)
- typedef DWORD (*TickFunctionType)(void);
- static TickFunctionType SetMockTickFunction(TickFunctionType ticker);
-#endif
-};
-
-inline TimeTicks TimeDelta::operator+(TimeTicks t) const {
- return TimeTicks(t.ticks_ + delta_);
-}
-
-} // namespace base
-
-#endif // BASE_TIME_H_
+// This file has moved, please use the new location.
+// TODO(avi): Remove this file when all users have been updated.
+#include "base/time/time.h"
diff --git a/base/time/clock.h b/base/time/clock.h
index 9fef254..be389be 100644
--- a/base/time/clock.h
+++ b/base/time/clock.h
@@ -6,7 +6,7 @@
#define BASE_CLOCK_H_
#include "base/base_export.h"
-#include "base/time.h"
+#include "base/time/time.h"
namespace base {
@@ -14,7 +14,7 @@ namespace base {
// intended to be able to test the behavior of classes with respect to
// time.
//
-// See DefaultClock (base/default_clock.h) for the default
+// See DefaultClock (base/time/default_clock.h) for the default
// implementation that simply uses Time::Now().
//
// (An implementation that uses Time::SystemTime() should be added as
@@ -23,7 +23,7 @@ namespace base {
// See SimpleTestClock (base/test/simple_test_clock.h) for a simple
// test implementation.
//
-// See TickClock (base/tick_clock.h) for the equivalent interface for
+// See TickClock (base/time/tick_clock.h) for the equivalent interface for
// TimeTicks.
class BASE_EXPORT Clock {
public:
diff --git a/base/pr_time_unittest.cc b/base/time/pr_time_unittest.cc
index 4000afd..9e0c4ed 100644
--- a/base/pr_time_unittest.cc
+++ b/base/time/pr_time_unittest.cc
@@ -6,7 +6,7 @@
#include "base/compiler_specific.h"
#include "base/third_party/nspr/prtime.h"
-#include "base/time.h"
+#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::Time;
diff --git a/base/time/tick_clock.h b/base/time/tick_clock.h
index 396e0c9..3e53d83 100644
--- a/base/time/tick_clock.h
+++ b/base/time/tick_clock.h
@@ -6,7 +6,7 @@
#define BASE_TICK_CLOCK_H_
#include "base/base_export.h"
-#include "base/time.h"
+#include "base/time/time.h"
namespace base {
@@ -14,7 +14,7 @@ namespace base {
// intended to be able to test the behavior of classes with respect to
// non-decreasing time.
//
-// See DefaultTickClock (base/default_tick_clock.h) for the default
+// See DefaultTickClock (base/time/default_tick_clock.h) for the default
// implementation that simply uses TimeTicks::Now().
//
// (Other implementations that use TimeTicks::HighResNow() or
@@ -23,7 +23,7 @@ namespace base {
// See SimpleTestTickClock (base/test/simple_test_tick_clock.h) for a
// simple test implementation.
//
-// See Clock (base/clock.h) for the equivalent interface for Times.
+// See Clock (base/time/clock.h) for the equivalent interface for Times.
class BASE_EXPORT TickClock {
public:
virtual ~TickClock();
diff --git a/base/time.cc b/base/time/time.cc
index 415f707..1a3f6c3 100644
--- a/base/time.cc
+++ b/base/time/time.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/time.h"
+#include "base/time/time.h"
#include <math.h>
#if defined(OS_WIN)
@@ -11,11 +11,10 @@
#include <limits>
+#include "base/logging.h"
#include "base/strings/sys_string_conversions.h"
#include "base/third_party/nspr/prtime.h"
-#include "base/logging.h"
-
namespace base {
namespace {
diff --git a/base/time/time.h b/base/time/time.h
new file mode 100644
index 0000000..1299519
--- /dev/null
+++ b/base/time/time.h
@@ -0,0 +1,651 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Time represents an absolute point in time, internally represented as
+// microseconds (s/1,000,000) since the Windows epoch (1601-01-01 00:00:00 UTC)
+// (See http://crbug.com/14734). System-dependent clock interface routines are
+// defined in time_PLATFORM.cc.
+//
+// TimeDelta represents a duration of time, internally represented in
+// microseconds.
+//
+// TimeTicks represents an abstract time that is most of the time incrementing
+// for use in measuring time durations. It is internally represented in
+// microseconds. It can not be converted to a human-readable time, but is
+// guaranteed not to decrease (if the user changes the computer clock,
+// Time::Now() may actually decrease or jump). But note that TimeTicks may
+// "stand still", for example if the computer suspended.
+//
+// These classes are represented as only a 64-bit value, so they can be
+// efficiently passed by value.
+
+#ifndef BASE_TIME_TIME_H_
+#define BASE_TIME_TIME_H_
+
+#include <time.h>
+
+#include "base/atomicops.h"
+#include "base/base_export.h"
+#include "base/basictypes.h"
+
+#if defined(OS_MACOSX)
+#include <CoreFoundation/CoreFoundation.h>
+// Avoid Mac system header macro leak.
+#undef TYPE_BOOL
+#endif
+
+#if defined(OS_POSIX)
+// For struct timeval.
+#include <sys/time.h>
+#endif
+
+#if defined(OS_WIN)
+// For FILETIME in FromFileTime, until it moves to a new converter class.
+// See TODO(iyengar) below.
+#include <windows.h>
+#endif
+
+#include <limits>
+
+namespace base {
+
+class Time;
+class TimeTicks;
+
+// TimeDelta ------------------------------------------------------------------
+
+class BASE_EXPORT TimeDelta {
+ public:
+ TimeDelta() : delta_(0) {
+ }
+
+ // Converts units of time to TimeDeltas.
+ static TimeDelta FromDays(int64 days);
+ static TimeDelta FromHours(int64 hours);
+ static TimeDelta FromMinutes(int64 minutes);
+ static TimeDelta FromSeconds(int64 secs);
+ static TimeDelta FromMilliseconds(int64 ms);
+ static TimeDelta FromMicroseconds(int64 us);
+#if defined(OS_WIN)
+ static TimeDelta FromQPCValue(LONGLONG qpc_value);
+#endif
+
+ // Converts an integer value representing TimeDelta to a class. This is used
+ // when deserializing a |TimeDelta| structure, using a value known to be
+ // compatible. It is not provided as a constructor because the integer type
+ // may be unclear from the perspective of a caller.
+ static TimeDelta FromInternalValue(int64 delta) {
+ return TimeDelta(delta);
+ }
+
+ // 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.
+ // For serializing, use FromInternalValue to reconstitute.
+ int64 ToInternalValue() const {
+ return delta_;
+ }
+
+#if defined(OS_POSIX)
+ struct timespec ToTimeSpec() const;
+#endif
+
+ // Returns the time delta in some unit. The F versions return a floating
+ // point value, the "regular" versions return a rounded-down value.
+ //
+ // InMillisecondsRoundedUp() instead returns an integer that is rounded up
+ // to the next full millisecond.
+ int InDays() const;
+ int InHours() const;
+ int InMinutes() const;
+ double InSecondsF() const;
+ int64 InSeconds() const;
+ double InMillisecondsF() const;
+ int64 InMilliseconds() const;
+ int64 InMillisecondsRoundedUp() const;
+ int64 InMicroseconds() const;
+
+ TimeDelta& operator=(TimeDelta other) {
+ delta_ = other.delta_;
+ return *this;
+ }
+
+ // Computations with other deltas.
+ TimeDelta operator+(TimeDelta other) const {
+ return TimeDelta(delta_ + other.delta_);
+ }
+ TimeDelta operator-(TimeDelta other) const {
+ return TimeDelta(delta_ - other.delta_);
+ }
+
+ TimeDelta& operator+=(TimeDelta other) {
+ delta_ += other.delta_;
+ return *this;
+ }
+ TimeDelta& operator-=(TimeDelta other) {
+ delta_ -= other.delta_;
+ return *this;
+ }
+ TimeDelta operator-() const {
+ return TimeDelta(-delta_);
+ }
+
+ // Computations with ints, note that we only allow multiplicative operations
+ // with ints, and additive operations with other deltas.
+ TimeDelta operator*(int64 a) const {
+ return TimeDelta(delta_ * a);
+ }
+ TimeDelta operator/(int64 a) const {
+ return TimeDelta(delta_ / a);
+ }
+ TimeDelta& operator*=(int64 a) {
+ delta_ *= a;
+ return *this;
+ }
+ TimeDelta& operator/=(int64 a) {
+ delta_ /= a;
+ return *this;
+ }
+ int64 operator/(TimeDelta a) const {
+ return delta_ / a.delta_;
+ }
+
+ // Defined below because it depends on the definition of the other classes.
+ Time operator+(Time t) const;
+ TimeTicks operator+(TimeTicks t) const;
+
+ // Comparison operators.
+ bool operator==(TimeDelta other) const {
+ return delta_ == other.delta_;
+ }
+ bool operator!=(TimeDelta other) const {
+ return delta_ != other.delta_;
+ }
+ bool operator<(TimeDelta other) const {
+ return delta_ < other.delta_;
+ }
+ bool operator<=(TimeDelta other) const {
+ return delta_ <= other.delta_;
+ }
+ bool operator>(TimeDelta other) const {
+ return delta_ > other.delta_;
+ }
+ bool operator>=(TimeDelta other) const {
+ return delta_ >= other.delta_;
+ }
+
+ private:
+ friend class Time;
+ friend class TimeTicks;
+ friend TimeDelta operator*(int64 a, TimeDelta td);
+
+ // Constructs a delta given the duration in microseconds. This is private
+ // to avoid confusion by callers with an integer constructor. Use
+ // FromSeconds, FromMilliseconds, etc. instead.
+ explicit TimeDelta(int64 delta_us) : delta_(delta_us) {
+ }
+
+ // Delta in microseconds.
+ int64 delta_;
+};
+
+inline TimeDelta operator*(int64 a, TimeDelta td) {
+ return TimeDelta(a * td.delta_);
+}
+
+// Time -----------------------------------------------------------------------
+
+// Represents a wall clock time.
+class BASE_EXPORT Time {
+ public:
+ static const int64 kMillisecondsPerSecond = 1000;
+ static const int64 kMicrosecondsPerMillisecond = 1000;
+ static const int64 kMicrosecondsPerSecond = kMicrosecondsPerMillisecond *
+ kMillisecondsPerSecond;
+ static const int64 kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60;
+ static const int64 kMicrosecondsPerHour = kMicrosecondsPerMinute * 60;
+ static const int64 kMicrosecondsPerDay = kMicrosecondsPerHour * 24;
+ static const int64 kMicrosecondsPerWeek = kMicrosecondsPerDay * 7;
+ static const int64 kNanosecondsPerMicrosecond = 1000;
+ static const int64 kNanosecondsPerSecond = kNanosecondsPerMicrosecond *
+ kMicrosecondsPerSecond;
+
+#if !defined(OS_WIN)
+ // On Mac & Linux, this value is the delta from the Windows epoch of 1601 to
+ // the Posix delta of 1970. This is used for migrating between the old
+ // 1970-based epochs to the new 1601-based ones. It should be removed from
+ // this global header and put in the platform-specific ones when we remove the
+ // migration code.
+ static const int64 kWindowsEpochDeltaMicroseconds;
+#endif
+
+ // Represents an exploded time that can be formatted nicely. This is kind of
+ // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few
+ // additions and changes to prevent errors.
+ struct BASE_EXPORT Exploded {
+ int year; // Four digit year "2007"
+ int month; // 1-based month (values 1 = January, etc.)
+ int day_of_week; // 0-based day of week (0 = Sunday, etc.)
+ int day_of_month; // 1-based day of month (1-31)
+ int hour; // Hour within the current day (0-23)
+ int minute; // Minute within the current hour (0-59)
+ int second; // Second within the current minute (0-59 plus leap
+ // seconds which may take it up to 60).
+ int millisecond; // Milliseconds within the current second (0-999)
+
+ // A cursory test for whether the data members are within their
+ // respective ranges. A 'true' return value does not guarantee the
+ // Exploded value can be successfully converted to a Time value.
+ bool HasValidValues() const;
+ };
+
+ // Contains the NULL time. Use Time::Now() to get the current time.
+ Time() : us_(0) {
+ }
+
+ // Returns true if the time object has not been initialized.
+ bool is_null() const {
+ return us_ == 0;
+ }
+
+ // Returns true if the time object is the maximum time.
+ bool is_max() const {
+ return us_ == std::numeric_limits<int64>::max();
+ }
+
+ // Returns the time for epoch in Unix-like system (Jan 1, 1970).
+ static Time UnixEpoch();
+
+ // Returns the current time. Watch out, the system might adjust its clock
+ // in which case time will actually go backwards. We don't guarantee that
+ // times are increasing, or that two calls to Now() won't be the same.
+ static Time Now();
+
+ // Returns the maximum time, which should be greater than any reasonable time
+ // with which we might compare it.
+ static Time Max();
+
+ // Returns the current time. Same as Now() except that this function always
+ // uses system time so that there are no discrepancies between the returned
+ // time and system time even on virtual environments including our test bot.
+ // For timing sensitive unittests, this function should be used.
+ static Time NowFromSystemTime();
+
+ // Converts to/from time_t in UTC and a Time class.
+ // TODO(brettw) this should be removed once everybody starts using the |Time|
+ // class.
+ static Time FromTimeT(time_t tt);
+ time_t ToTimeT() const;
+
+ // Converts time to/from a double which is the number of seconds since epoch
+ // (Jan 1, 1970). Webkit uses this format to represent time.
+ // Because WebKit initializes double time value to 0 to indicate "not
+ // initialized", we map it to empty Time object that also means "not
+ // initialized".
+ static Time FromDoubleT(double dt);
+ double ToDoubleT() const;
+
+#if defined(OS_POSIX)
+ // Converts the timespec structure to time. MacOS X 10.8.3 (and tentatively,
+ // earlier versions) will have the |ts|'s tv_nsec component zeroed out,
+ // having a 1 second resolution, which agrees with
+ // https://developer.apple.com/legacy/library/#technotes/tn/tn1150.html#HFSPlusDates.
+ static Time FromTimeSpec(const timespec& ts);
+#endif
+
+ // Converts to/from the Javascript convention for times, a number of
+ // milliseconds since the epoch:
+ // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime.
+ static Time FromJsTime(double ms_since_epoch);
+ double ToJsTime() const;
+
+#if defined(OS_POSIX)
+ static Time FromTimeVal(struct timeval t);
+ struct timeval ToTimeVal() const;
+#endif
+
+#if defined(OS_MACOSX)
+ static Time FromCFAbsoluteTime(CFAbsoluteTime t);
+ CFAbsoluteTime ToCFAbsoluteTime() const;
+#endif
+
+#if defined(OS_WIN)
+ static Time FromFileTime(FILETIME ft);
+ FILETIME ToFileTime() const;
+
+ // The minimum time of a low resolution timer. This is basically a windows
+ // constant of ~15.6ms. While it does vary on some older OS versions, we'll
+ // treat it as static across all windows versions.
+ static const int kMinLowResolutionThresholdMs = 16;
+
+ // Enable or disable Windows high resolution timer. If the high resolution
+ // timer is not enabled, calls to ActivateHighResolutionTimer will fail.
+ // When disabling the high resolution timer, this function will not cause
+ // the high resolution timer to be deactivated, but will prevent future
+ // activations.
+ // Must be called from the main thread.
+ // For more details see comments in time_win.cc.
+ static void EnableHighResolutionTimer(bool enable);
+
+ // Activates or deactivates the high resolution timer based on the |activate|
+ // flag. If the HighResolutionTimer is not Enabled (see
+ // EnableHighResolutionTimer), this function will return false. Otherwise
+ // returns true. Each successful activate call must be paired with a
+ // subsequent deactivate call.
+ // All callers to activate the high resolution timer must eventually call
+ // this function to deactivate the high resolution timer.
+ static bool ActivateHighResolutionTimer(bool activate);
+
+ // Returns true if the high resolution timer is both enabled and activated.
+ // This is provided for testing only, and is not tracked in a thread-safe
+ // way.
+ static bool IsHighResolutionTimerInUse();
+#endif
+
+ // Converts an exploded structure representing either the local time or UTC
+ // into a Time class.
+ static Time FromUTCExploded(const Exploded& exploded) {
+ return FromExploded(false, exploded);
+ }
+ static Time FromLocalExploded(const Exploded& exploded) {
+ return FromExploded(true, exploded);
+ }
+
+ // Converts an integer value representing Time to a class. This is used
+ // when deserializing a |Time| structure, using a value known to be
+ // compatible. It is not provided as a constructor because the integer type
+ // may be unclear from the perspective of a caller.
+ static Time FromInternalValue(int64 us) {
+ return Time(us);
+ }
+
+ // Converts a string representation of time to a Time object.
+ // An example of a time string which is converted is as below:-
+ // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified
+ // in the input string, FromString assumes local time and FromUTCString
+ // assumes UTC. A timezone that cannot be parsed (e.g. "UTC" which is not
+ // specified in RFC822) is treated as if the timezone is not specified.
+ // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to
+ // a new time converter class.
+ static bool FromString(const char* time_string, Time* parsed_time) {
+ return FromStringInternal(time_string, true, parsed_time);
+ }
+ static bool FromUTCString(const char* time_string, Time* parsed_time) {
+ return FromStringInternal(time_string, false, parsed_time);
+ }
+
+ // For serializing, use FromInternalValue to reconstitute. Please don't use
+ // this and do arithmetic on it, as it is more error prone than using the
+ // provided operators.
+ int64 ToInternalValue() const {
+ return us_;
+ }
+
+ // Fills the given exploded structure with either the local time or UTC from
+ // this time structure (containing UTC).
+ void UTCExplode(Exploded* exploded) const {
+ return Explode(false, exploded);
+ }
+ void LocalExplode(Exploded* exploded) const {
+ return Explode(true, exploded);
+ }
+
+ // Rounds this time down to the nearest day in local time. It will represent
+ // midnight on that day.
+ Time LocalMidnight() const;
+
+ Time& operator=(Time other) {
+ us_ = other.us_;
+ return *this;
+ }
+
+ // Compute the difference between two times.
+ TimeDelta operator-(Time other) const {
+ return TimeDelta(us_ - other.us_);
+ }
+
+ // Modify by some time delta.
+ Time& operator+=(TimeDelta delta) {
+ us_ += delta.delta_;
+ return *this;
+ }
+ Time& operator-=(TimeDelta delta) {
+ us_ -= delta.delta_;
+ return *this;
+ }
+
+ // Return a new time modified by some delta.
+ Time operator+(TimeDelta delta) const {
+ return Time(us_ + delta.delta_);
+ }
+ Time operator-(TimeDelta delta) const {
+ return Time(us_ - delta.delta_);
+ }
+
+ // Comparison operators
+ bool operator==(Time other) const {
+ return us_ == other.us_;
+ }
+ bool operator!=(Time other) const {
+ return us_ != other.us_;
+ }
+ bool operator<(Time other) const {
+ return us_ < other.us_;
+ }
+ bool operator<=(Time other) const {
+ return us_ <= other.us_;
+ }
+ bool operator>(Time other) const {
+ return us_ > other.us_;
+ }
+ bool operator>=(Time other) const {
+ return us_ >= other.us_;
+ }
+
+ private:
+ friend class TimeDelta;
+
+ explicit Time(int64 us) : us_(us) {
+ }
+
+ // Explodes the given time to either local time |is_local = true| or UTC
+ // |is_local = false|.
+ void Explode(bool is_local, Exploded* exploded) const;
+
+ // Unexplodes a given time assuming the source is either local time
+ // |is_local = true| or UTC |is_local = false|.
+ static Time FromExploded(bool is_local, const Exploded& exploded);
+
+ // Converts a string representation of time to a Time object.
+ // An example of a time string which is converted is as below:-
+ // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified
+ // in the input string, local time |is_local = true| or
+ // UTC |is_local = false| is assumed. A timezone that cannot be parsed
+ // (e.g. "UTC" which is not specified in RFC822) is treated as if the
+ // timezone is not specified.
+ static bool FromStringInternal(const char* time_string,
+ bool is_local,
+ Time* parsed_time);
+
+ // The representation of Jan 1, 1970 UTC in microseconds since the
+ // platform-dependent epoch.
+ static const int64 kTimeTToMicrosecondsOffset;
+
+#if defined(OS_WIN)
+ // Indicates whether fast timers are usable right now. For instance,
+ // when using battery power, we might elect to prevent high speed timers
+ // which would draw more power.
+ static bool high_resolution_timer_enabled_;
+ // Count of activations on the high resolution timer. Only use in tests
+ // which are single threaded.
+ static int high_resolution_timer_activated_;
+#endif
+
+ // Time in microseconds in UTC.
+ int64 us_;
+};
+
+// Inline the TimeDelta factory methods, for fast TimeDelta construction.
+
+// static
+inline TimeDelta TimeDelta::FromDays(int64 days) {
+ return TimeDelta(days * Time::kMicrosecondsPerDay);
+}
+
+// static
+inline TimeDelta TimeDelta::FromHours(int64 hours) {
+ return TimeDelta(hours * Time::kMicrosecondsPerHour);
+}
+
+// static
+inline TimeDelta TimeDelta::FromMinutes(int64 minutes) {
+ return TimeDelta(minutes * Time::kMicrosecondsPerMinute);
+}
+
+// static
+inline TimeDelta TimeDelta::FromSeconds(int64 secs) {
+ return TimeDelta(secs * Time::kMicrosecondsPerSecond);
+}
+
+// static
+inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) {
+ return TimeDelta(ms * Time::kMicrosecondsPerMillisecond);
+}
+
+// static
+inline TimeDelta TimeDelta::FromMicroseconds(int64 us) {
+ return TimeDelta(us);
+}
+
+inline Time TimeDelta::operator+(Time t) const {
+ return Time(t.us_ + delta_);
+}
+
+// TimeTicks ------------------------------------------------------------------
+
+class BASE_EXPORT TimeTicks {
+ public:
+ TimeTicks() : ticks_(0) {
+ }
+
+ // Platform-dependent tick count representing "right now."
+ // The resolution of this clock is ~1-15ms. Resolution varies depending
+ // on hardware/operating system configuration.
+ static TimeTicks Now();
+
+ // Returns a platform-dependent high-resolution tick count. Implementation
+ // is hardware dependent and may or may not return sub-millisecond
+ // resolution. THIS CALL IS GENERALLY MUCH MORE EXPENSIVE THAN Now() AND
+ // SHOULD ONLY BE USED WHEN IT IS REALLY NEEDED.
+ static TimeTicks HighResNow();
+
+ // Returns the current system trace time or, if none is defined, the current
+ // high-res time (i.e. HighResNow()). On systems where a global trace clock
+ // is defined, timestamping TraceEvents's with this value guarantees
+ // synchronization between events collected inside chrome and events
+ // collected outside (e.g. kernel, X server).
+ static TimeTicks NowFromSystemTraceTime();
+
+#if defined(OS_WIN)
+ // Get the absolute value of QPC time drift. For testing.
+ static int64 GetQPCDriftMicroseconds();
+
+ static TimeTicks FromQPCValue(LONGLONG qpc_value);
+
+ // Returns true if the high resolution clock is working on this system.
+ // This is only for testing.
+ static bool IsHighResClockWorking();
+#endif
+
+ // Returns true if this object has not been initialized.
+ bool is_null() const {
+ return ticks_ == 0;
+ }
+
+ // Converts an integer value representing TimeTicks to a class. This is used
+ // when deserializing a |TimeTicks| structure, using a value known to be
+ // compatible. It is not provided as a constructor because the integer type
+ // may be unclear from the perspective of a caller.
+ static TimeTicks FromInternalValue(int64 ticks) {
+ return TimeTicks(ticks);
+ }
+
+ // Returns the internal numeric value of the TimeTicks object.
+ // For serializing, use FromInternalValue to reconstitute.
+ int64 ToInternalValue() const {
+ return ticks_;
+ }
+
+ TimeTicks& operator=(TimeTicks other) {
+ ticks_ = other.ticks_;
+ return *this;
+ }
+
+ // Compute the difference between two times.
+ TimeDelta operator-(TimeTicks other) const {
+ return TimeDelta(ticks_ - other.ticks_);
+ }
+
+ // Modify by some time delta.
+ TimeTicks& operator+=(TimeDelta delta) {
+ ticks_ += delta.delta_;
+ return *this;
+ }
+ TimeTicks& operator-=(TimeDelta delta) {
+ ticks_ -= delta.delta_;
+ return *this;
+ }
+
+ // Return a new TimeTicks modified by some delta.
+ TimeTicks operator+(TimeDelta delta) const {
+ return TimeTicks(ticks_ + delta.delta_);
+ }
+ TimeTicks operator-(TimeDelta delta) const {
+ return TimeTicks(ticks_ - delta.delta_);
+ }
+
+ // Comparison operators
+ bool operator==(TimeTicks other) const {
+ return ticks_ == other.ticks_;
+ }
+ bool operator!=(TimeTicks other) const {
+ return ticks_ != other.ticks_;
+ }
+ bool operator<(TimeTicks other) const {
+ return ticks_ < other.ticks_;
+ }
+ bool operator<=(TimeTicks other) const {
+ return ticks_ <= other.ticks_;
+ }
+ bool operator>(TimeTicks other) const {
+ return ticks_ > other.ticks_;
+ }
+ bool operator>=(TimeTicks other) const {
+ return ticks_ >= other.ticks_;
+ }
+
+ protected:
+ friend class TimeDelta;
+
+ // Please use Now() to create a new object. This is for internal use
+ // and testing. Ticks is in microseconds.
+ explicit TimeTicks(int64 ticks) : ticks_(ticks) {
+ }
+
+ // Tick count in microseconds.
+ int64 ticks_;
+
+#if defined(OS_WIN)
+ typedef DWORD (*TickFunctionType)(void);
+ static TickFunctionType SetMockTickFunction(TickFunctionType ticker);
+#endif
+};
+
+inline TimeTicks TimeDelta::operator+(TimeTicks t) const {
+ return TimeTicks(t.ticks_ + delta_);
+}
+
+} // namespace base
+
+#endif // BASE_TIME_TIME_H_
diff --git a/base/time_mac.cc b/base/time/time_mac.cc
index 91e9864..962da6d 100644
--- a/base/time_mac.cc
+++ b/base/time/time_mac.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/time.h"
+#include "base/time/time.h"
#include <CoreFoundation/CFDate.h>
#include <CoreFoundation/CFTimeZone.h>
diff --git a/base/time_posix.cc b/base/time/time_posix.cc
index 3870201..64a65ea 100644
--- a/base/time_posix.cc
+++ b/base/time/time_posix.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/time.h"
+#include "base/time/time.h"
#include <sys/time.h>
#include <time.h>
diff --git a/base/time_unittest.cc b/base/time/time_unittest.cc
index dd6d26b..d25c441 100644
--- a/base/time_unittest.cc
+++ b/base/time/time_unittest.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/time.h"
+#include "base/time/time.h"
#include <time.h>
diff --git a/base/time_win.cc b/base/time/time_win.cc
index 0f5c401..81b2527 100644
--- a/base/time_win.cc
+++ b/base/time/time_win.cc
@@ -34,15 +34,15 @@
// do. Otherwise, WaitForSingleObject(..., 1) will no less than 15ms when
// there is nothing else to waken the Wait.
-#include "base/time.h"
+#include "base/time/time.h"
#pragma comment(lib, "winmm.lib")
#include <windows.h>
#include <mmsystem.h>
#include "base/basictypes.h"
-#include "base/logging.h"
#include "base/cpu.h"
+#include "base/logging.h"
#include "base/memory/singleton.h"
#include "base/synchronization/lock.h"
diff --git a/base/time_win_unittest.cc b/base/time/time_win_unittest.cc
index 7ac3662..803222f 100644
--- a/base/time_win_unittest.cc
+++ b/base/time/time_win_unittest.cc
@@ -7,7 +7,7 @@
#include <process.h>
#include "base/threading/platform_thread.h"
-#include "base/time.h"
+#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::Time;
diff --git a/base/timer.h b/base/timer.h
index bbcbae2..b594d5d 100644
--- a/base/timer.h
+++ b/base/timer.h
@@ -1,246 +1,7 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-// OneShotTimer and RepeatingTimer provide a simple timer API. As the names
-// suggest, OneShotTimer calls you back once after a time delay expires.
-// RepeatingTimer on the other hand calls you back periodically with the
-// prescribed time interval.
-//
-// OneShotTimer and RepeatingTimer both cancel the timer when they go out of
-// scope, which makes it easy to ensure that you do not get called when your
-// object has gone out of scope. Just instantiate a OneShotTimer or
-// RepeatingTimer as a member variable of the class for which you wish to
-// receive timer events.
-//
-// Sample RepeatingTimer usage:
-//
-// class MyClass {
-// public:
-// void StartDoingStuff() {
-// timer_.Start(FROM_HERE, TimeDelta::FromSeconds(1),
-// this, &MyClass::DoStuff);
-// }
-// void StopDoingStuff() {
-// timer_.Stop();
-// }
-// private:
-// void DoStuff() {
-// // This method is called every second to do stuff.
-// ...
-// }
-// base::RepeatingTimer<MyClass> timer_;
-// };
-//
-// Both OneShotTimer and RepeatingTimer also support a Reset method, which
-// allows you to easily defer the timer event until the timer delay passes once
-// again. So, in the above example, if 0.5 seconds have already passed,
-// calling Reset on timer_ would postpone DoStuff by another 1 second. In
-// other words, Reset is shorthand for calling Stop and then Start again with
-// the same arguments.
-//
-// NOTE: These APIs are not thread safe. Always call from the same thread.
-
-#ifndef BASE_TIMER_H_
-#define BASE_TIMER_H_
-
-// IMPORTANT: If you change timer code, make sure that all tests (including
-// disabled ones) from timer_unittests.cc pass locally. Some are disabled
-// because they're flaky on the buildbot, but when you run them locally you
-// should be able to tell the difference.
-
-#include "base/base_export.h"
-#include "base/bind.h"
-#include "base/bind_helpers.h"
-#include "base/callback.h"
-#include "base/location.h"
-#include "base/time.h"
-
-namespace base {
-
-class BaseTimerTaskInternal;
-class MessageLoop;
-
-//-----------------------------------------------------------------------------
-// This class wraps MessageLoop::PostDelayedTask to manage delayed and repeating
-// tasks. It must be destructed on the same thread that starts tasks. There are
-// DCHECKs in place to verify this.
-//
-class BASE_EXPORT Timer {
- public:
- // Construct a timer in repeating or one-shot mode. Start or SetTaskInfo must
- // be called later to set task info. |retain_user_task| determines whether the
- // user_task is retained or reset when it runs or stops.
- Timer(bool retain_user_task, bool is_repeating);
-
- // Construct a timer with retained task info.
- Timer(const tracked_objects::Location& posted_from,
- TimeDelta delay,
- const base::Closure& user_task,
- bool is_repeating);
-
- virtual ~Timer();
-
- // Returns true if the timer is running (i.e., not stopped).
- bool IsRunning() const {
- return is_running_;
- }
-
- // Returns the current delay for this timer.
- TimeDelta GetCurrentDelay() const {
- return delay_;
- }
-
- // Start the timer to run at the given |delay| from now. If the timer is
- // already running, it will be replaced to call the given |user_task|.
- void Start(const tracked_objects::Location& posted_from,
- TimeDelta delay,
- const base::Closure& user_task);
-
- // Call this method to stop and cancel the timer. It is a no-op if the timer
- // is not running.
- void Stop();
-
- // Call this method to reset the timer delay. The user_task_ must be set. If
- // the timer is not running, this will start it by posting a task.
- void Reset();
-
- const base::Closure& user_task() const { return user_task_; }
- const TimeTicks& desired_run_time() const { return desired_run_time_; }
-
- protected:
- // Used to initiate a new delayed task. This has the side-effect of disabling
- // scheduled_task_ if it is non-null.
- void SetTaskInfo(const tracked_objects::Location& posted_from,
- TimeDelta delay,
- const base::Closure& user_task);
-
- private:
- friend class BaseTimerTaskInternal;
-
- // Allocates a new scheduled_task_ and posts it on the current MessageLoop
- // with the given |delay|. scheduled_task_ must be NULL. scheduled_run_time_
- // and desired_run_time_ are reset to Now() + delay.
- void PostNewScheduledTask(TimeDelta delay);
-
- // Disable scheduled_task_ and abandon it so that it no longer refers back to
- // this object.
- void AbandonScheduledTask();
-
- // Called by BaseTimerTaskInternal when the MessageLoop runs it.
- void RunScheduledTask();
-
- // Stop running task (if any) and abandon scheduled task (if any).
- void StopAndAbandon() {
- Stop();
- AbandonScheduledTask();
- }
-
- // When non-NULL, the scheduled_task_ is waiting in the MessageLoop to call
- // RunScheduledTask() at scheduled_run_time_.
- BaseTimerTaskInternal* scheduled_task_;
-
- // Location in user code.
- tracked_objects::Location posted_from_;
- // Delay requested by user.
- TimeDelta delay_;
- // user_task_ is what the user wants to be run at desired_run_time_.
- base::Closure user_task_;
-
- // The estimated time that the MessageLoop will run the scheduled_task_ that
- // will call RunScheduledTask().
- TimeTicks scheduled_run_time_;
-
- // The desired run time of user_task_. The user may update this at any time,
- // even if their previous request has not run yet. If desired_run_time_ is
- // greater than scheduled_run_time_, a continuation task will be posted to
- // wait for the remaining time. This allows us to reuse the pending task so as
- // not to flood the MessageLoop with orphaned tasks when the user code
- // excessively Stops and Starts the timer.
- TimeTicks desired_run_time_;
-
- // Thread ID of current MessageLoop for verifying single-threaded usage.
- int thread_id_;
-
- // Repeating timers automatically post the task again before calling the task
- // callback.
- const bool is_repeating_;
-
- // If true, hold on to the user_task_ closure object for reuse.
- const bool retain_user_task_;
-
- // If true, user_task_ is scheduled to run sometime in the future.
- bool is_running_;
-
- DISALLOW_COPY_AND_ASSIGN(Timer);
-};
-
-//-----------------------------------------------------------------------------
-// This class is an implementation detail of OneShotTimer and RepeatingTimer.
-// Please do not use this class directly.
-template <class Receiver, bool kIsRepeating>
-class BaseTimerMethodPointer : public Timer {
- public:
- typedef void (Receiver::*ReceiverMethod)();
-
- // This is here to work around the fact that Timer::Start is "hidden" by the
- // Start definition below, rather than being overloaded.
- // TODO(tim): We should remove uses of BaseTimerMethodPointer::Start below
- // and convert callers to use the base::Closure version in Timer::Start,
- // see bug 148832.
- using Timer::Start;
-
- BaseTimerMethodPointer() : Timer(kIsRepeating, kIsRepeating) {}
-
- // Start the timer to run at the given |delay| from now. If the timer is
- // already running, it will be replaced to call a task formed from
- // |reviewer->*method|.
- void Start(const tracked_objects::Location& posted_from,
- TimeDelta delay,
- Receiver* receiver,
- ReceiverMethod method) {
- Timer::Start(posted_from, delay,
- base::Bind(method, base::Unretained(receiver)));
- }
-};
-
-//-----------------------------------------------------------------------------
-// A simple, one-shot timer. See usage notes at the top of the file.
-template <class Receiver>
-class OneShotTimer : public BaseTimerMethodPointer<Receiver, false> {};
-
-//-----------------------------------------------------------------------------
-// A simple, repeating timer. See usage notes at the top of the file.
-template <class Receiver>
-class RepeatingTimer : public BaseTimerMethodPointer<Receiver, true> {};
-
-//-----------------------------------------------------------------------------
-// A Delay timer is like The Button from Lost. Once started, you have to keep
-// calling Reset otherwise it will call the given method in the MessageLoop
-// thread.
-//
-// Once created, it is inactive until Reset is called. Once |delay| seconds have
-// passed since the last call to Reset, the callback is made. Once the callback
-// has been made, it's inactive until Reset is called again.
-//
-// If destroyed, the timeout is canceled and will not occur even if already
-// inflight.
-template <class Receiver>
-class DelayTimer : protected Timer {
- public:
- typedef void (Receiver::*ReceiverMethod)();
-
- DelayTimer(const tracked_objects::Location& posted_from,
- TimeDelta delay,
- Receiver* receiver,
- ReceiverMethod method)
- : Timer(posted_from, delay,
- base::Bind(method, base::Unretained(receiver)),
- false) {}
-
- void Reset() { Timer::Reset(); }
-};
-
-} // namespace base
-
-#endif // BASE_TIMER_H_
+// This file has moved, please use the new location.
+// TODO(avi): Remove this file when all users have been updated.
+#include "base/timer/timer.h"
diff --git a/base/hi_res_timer_manager.h b/base/timer/hi_res_timer_manager.h
index 763aede..7fcdb0d 100644
--- a/base/hi_res_timer_manager.h
+++ b/base/timer/hi_res_timer_manager.h
@@ -2,13 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef BASE_HI_RES_TIMER_MANAGER_H_
-#define BASE_HI_RES_TIMER_MANAGER_H_
+#ifndef BASE_TIMER_HI_RES_TIMER_MANAGER_H_
+#define BASE_TIMER_HI_RES_TIMER_MANAGER_H_
#include "base/base_export.h"
#include "base/basictypes.h"
#include "base/power_monitor/power_observer.h"
+namespace base {
+
// Ensures that the Windows high resolution timer is only used
// when not running on battery power.
class BASE_EXPORT HighResolutionTimerManager : public base::PowerObserver {
@@ -31,4 +33,6 @@ class BASE_EXPORT HighResolutionTimerManager : public base::PowerObserver {
DISALLOW_COPY_AND_ASSIGN(HighResolutionTimerManager);
};
-#endif // BASE_HI_RES_TIMER_MANAGER_H_
+} // namespace base
+
+#endif // BASE_TIMER_HI_RES_TIMER_MANAGER_H_
diff --git a/base/hi_res_timer_manager_posix.cc b/base/timer/hi_res_timer_manager_posix.cc
index 7c16eb7..d2f152c 100644
--- a/base/hi_res_timer_manager_posix.cc
+++ b/base/timer/hi_res_timer_manager_posix.cc
@@ -2,10 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/hi_res_timer_manager.h"
+#include "base/timer/hi_res_timer_manager.h"
// On POSIX we don't need to do anything special with the system timer.
+namespace base {
+
HighResolutionTimerManager::HighResolutionTimerManager()
: hi_res_clock_available_(false) {
}
@@ -18,3 +20,5 @@ void HighResolutionTimerManager::OnPowerStateChange(bool on_battery_power) {
void HighResolutionTimerManager::UseHiResClock(bool use) {
}
+
+} // namespace base
diff --git a/base/hi_res_timer_manager_unittest.cc b/base/timer/hi_res_timer_manager_unittest.cc
index b523284..0017859 100644
--- a/base/hi_res_timer_manager_unittest.cc
+++ b/base/timer/hi_res_timer_manager_unittest.cc
@@ -2,14 +2,16 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/hi_res_timer_manager.h"
+#include "base/timer/hi_res_timer_manager.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "base/power_monitor/power_monitor.h"
-#include "base/time.h"
+#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
+namespace base {
+
#if defined(OS_WIN)
// http://crbug.com/114048
TEST(HiResTimerManagerTest, DISABLED_ToggleOnOff) {
@@ -51,3 +53,5 @@ TEST(HiResTimerManagerTest, DISABLED_ToggleOnOff) {
}
}
#endif // defined(OS_WIN)
+
+} // namespace base
diff --git a/base/hi_res_timer_manager_win.cc b/base/timer/hi_res_timer_manager_win.cc
index 2a297dc..3647714 100644
--- a/base/hi_res_timer_manager_win.cc
+++ b/base/timer/hi_res_timer_manager_win.cc
@@ -2,10 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/hi_res_timer_manager.h"
+#include "base/timer/hi_res_timer_manager.h"
#include "base/power_monitor/power_monitor.h"
-#include "base/time.h"
+#include "base/time/time.h"
+
+namespace base {
HighResolutionTimerManager::HighResolutionTimerManager()
: hi_res_clock_available_(false) {
@@ -29,3 +31,5 @@ void HighResolutionTimerManager::UseHiResClock(bool use) {
hi_res_clock_available_ = use;
base::Time::EnableHighResolutionTimer(use);
}
+
+} // namespace base
diff --git a/base/timer.cc b/base/timer/timer.cc
index c7dcce0..c7dcce0 100644
--- a/base/timer.cc
+++ b/base/timer/timer.cc
diff --git a/base/timer/timer.h b/base/timer/timer.h
new file mode 100644
index 0000000..23b1d3c
--- /dev/null
+++ b/base/timer/timer.h
@@ -0,0 +1,246 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// OneShotTimer and RepeatingTimer provide a simple timer API. As the names
+// suggest, OneShotTimer calls you back once after a time delay expires.
+// RepeatingTimer on the other hand calls you back periodically with the
+// prescribed time interval.
+//
+// OneShotTimer and RepeatingTimer both cancel the timer when they go out of
+// scope, which makes it easy to ensure that you do not get called when your
+// object has gone out of scope. Just instantiate a OneShotTimer or
+// RepeatingTimer as a member variable of the class for which you wish to
+// receive timer events.
+//
+// Sample RepeatingTimer usage:
+//
+// class MyClass {
+// public:
+// void StartDoingStuff() {
+// timer_.Start(FROM_HERE, TimeDelta::FromSeconds(1),
+// this, &MyClass::DoStuff);
+// }
+// void StopDoingStuff() {
+// timer_.Stop();
+// }
+// private:
+// void DoStuff() {
+// // This method is called every second to do stuff.
+// ...
+// }
+// base::RepeatingTimer<MyClass> timer_;
+// };
+//
+// Both OneShotTimer and RepeatingTimer also support a Reset method, which
+// allows you to easily defer the timer event until the timer delay passes once
+// again. So, in the above example, if 0.5 seconds have already passed,
+// calling Reset on timer_ would postpone DoStuff by another 1 second. In
+// other words, Reset is shorthand for calling Stop and then Start again with
+// the same arguments.
+//
+// NOTE: These APIs are not thread safe. Always call from the same thread.
+
+#ifndef BASE_TIMER_TIMER_H_
+#define BASE_TIMER_TIMER_H_
+
+// IMPORTANT: If you change timer code, make sure that all tests (including
+// disabled ones) from timer_unittests.cc pass locally. Some are disabled
+// because they're flaky on the buildbot, but when you run them locally you
+// should be able to tell the difference.
+
+#include "base/base_export.h"
+#include "base/bind.h"
+#include "base/bind_helpers.h"
+#include "base/callback.h"
+#include "base/location.h"
+#include "base/time/time.h"
+
+namespace base {
+
+class BaseTimerTaskInternal;
+class MessageLoop;
+
+//-----------------------------------------------------------------------------
+// This class wraps MessageLoop::PostDelayedTask to manage delayed and repeating
+// tasks. It must be destructed on the same thread that starts tasks. There are
+// DCHECKs in place to verify this.
+//
+class BASE_EXPORT Timer {
+ public:
+ // Construct a timer in repeating or one-shot mode. Start or SetTaskInfo must
+ // be called later to set task info. |retain_user_task| determines whether the
+ // user_task is retained or reset when it runs or stops.
+ Timer(bool retain_user_task, bool is_repeating);
+
+ // Construct a timer with retained task info.
+ Timer(const tracked_objects::Location& posted_from,
+ TimeDelta delay,
+ const base::Closure& user_task,
+ bool is_repeating);
+
+ virtual ~Timer();
+
+ // Returns true if the timer is running (i.e., not stopped).
+ bool IsRunning() const {
+ return is_running_;
+ }
+
+ // Returns the current delay for this timer.
+ TimeDelta GetCurrentDelay() const {
+ return delay_;
+ }
+
+ // Start the timer to run at the given |delay| from now. If the timer is
+ // already running, it will be replaced to call the given |user_task|.
+ void Start(const tracked_objects::Location& posted_from,
+ TimeDelta delay,
+ const base::Closure& user_task);
+
+ // Call this method to stop and cancel the timer. It is a no-op if the timer
+ // is not running.
+ void Stop();
+
+ // Call this method to reset the timer delay. The user_task_ must be set. If
+ // the timer is not running, this will start it by posting a task.
+ void Reset();
+
+ const base::Closure& user_task() const { return user_task_; }
+ const TimeTicks& desired_run_time() const { return desired_run_time_; }
+
+ protected:
+ // Used to initiate a new delayed task. This has the side-effect of disabling
+ // scheduled_task_ if it is non-null.
+ void SetTaskInfo(const tracked_objects::Location& posted_from,
+ TimeDelta delay,
+ const base::Closure& user_task);
+
+ private:
+ friend class BaseTimerTaskInternal;
+
+ // Allocates a new scheduled_task_ and posts it on the current MessageLoop
+ // with the given |delay|. scheduled_task_ must be NULL. scheduled_run_time_
+ // and desired_run_time_ are reset to Now() + delay.
+ void PostNewScheduledTask(TimeDelta delay);
+
+ // Disable scheduled_task_ and abandon it so that it no longer refers back to
+ // this object.
+ void AbandonScheduledTask();
+
+ // Called by BaseTimerTaskInternal when the MessageLoop runs it.
+ void RunScheduledTask();
+
+ // Stop running task (if any) and abandon scheduled task (if any).
+ void StopAndAbandon() {
+ Stop();
+ AbandonScheduledTask();
+ }
+
+ // When non-NULL, the scheduled_task_ is waiting in the MessageLoop to call
+ // RunScheduledTask() at scheduled_run_time_.
+ BaseTimerTaskInternal* scheduled_task_;
+
+ // Location in user code.
+ tracked_objects::Location posted_from_;
+ // Delay requested by user.
+ TimeDelta delay_;
+ // user_task_ is what the user wants to be run at desired_run_time_.
+ base::Closure user_task_;
+
+ // The estimated time that the MessageLoop will run the scheduled_task_ that
+ // will call RunScheduledTask().
+ TimeTicks scheduled_run_time_;
+
+ // The desired run time of user_task_. The user may update this at any time,
+ // even if their previous request has not run yet. If desired_run_time_ is
+ // greater than scheduled_run_time_, a continuation task will be posted to
+ // wait for the remaining time. This allows us to reuse the pending task so as
+ // not to flood the MessageLoop with orphaned tasks when the user code
+ // excessively Stops and Starts the timer.
+ TimeTicks desired_run_time_;
+
+ // Thread ID of current MessageLoop for verifying single-threaded usage.
+ int thread_id_;
+
+ // Repeating timers automatically post the task again before calling the task
+ // callback.
+ const bool is_repeating_;
+
+ // If true, hold on to the user_task_ closure object for reuse.
+ const bool retain_user_task_;
+
+ // If true, user_task_ is scheduled to run sometime in the future.
+ bool is_running_;
+
+ DISALLOW_COPY_AND_ASSIGN(Timer);
+};
+
+//-----------------------------------------------------------------------------
+// This class is an implementation detail of OneShotTimer and RepeatingTimer.
+// Please do not use this class directly.
+template <class Receiver, bool kIsRepeating>
+class BaseTimerMethodPointer : public Timer {
+ public:
+ typedef void (Receiver::*ReceiverMethod)();
+
+ // This is here to work around the fact that Timer::Start is "hidden" by the
+ // Start definition below, rather than being overloaded.
+ // TODO(tim): We should remove uses of BaseTimerMethodPointer::Start below
+ // and convert callers to use the base::Closure version in Timer::Start,
+ // see bug 148832.
+ using Timer::Start;
+
+ BaseTimerMethodPointer() : Timer(kIsRepeating, kIsRepeating) {}
+
+ // Start the timer to run at the given |delay| from now. If the timer is
+ // already running, it will be replaced to call a task formed from
+ // |reviewer->*method|.
+ void Start(const tracked_objects::Location& posted_from,
+ TimeDelta delay,
+ Receiver* receiver,
+ ReceiverMethod method) {
+ Timer::Start(posted_from, delay,
+ base::Bind(method, base::Unretained(receiver)));
+ }
+};
+
+//-----------------------------------------------------------------------------
+// A simple, one-shot timer. See usage notes at the top of the file.
+template <class Receiver>
+class OneShotTimer : public BaseTimerMethodPointer<Receiver, false> {};
+
+//-----------------------------------------------------------------------------
+// A simple, repeating timer. See usage notes at the top of the file.
+template <class Receiver>
+class RepeatingTimer : public BaseTimerMethodPointer<Receiver, true> {};
+
+//-----------------------------------------------------------------------------
+// A Delay timer is like The Button from Lost. Once started, you have to keep
+// calling Reset otherwise it will call the given method in the MessageLoop
+// thread.
+//
+// Once created, it is inactive until Reset is called. Once |delay| seconds have
+// passed since the last call to Reset, the callback is made. Once the callback
+// has been made, it's inactive until Reset is called again.
+//
+// If destroyed, the timeout is canceled and will not occur even if already
+// inflight.
+template <class Receiver>
+class DelayTimer : protected Timer {
+ public:
+ typedef void (Receiver::*ReceiverMethod)();
+
+ DelayTimer(const tracked_objects::Location& posted_from,
+ TimeDelta delay,
+ Receiver* receiver,
+ ReceiverMethod method)
+ : Timer(posted_from, delay,
+ base::Bind(method, base::Unretained(receiver)),
+ false) {}
+
+ void Reset() { Timer::Reset(); }
+};
+
+} // namespace base
+
+#endif // BASE_TIMER_TIMER_H_
diff --git a/base/timer_unittest.cc b/base/timer/timer_unittest.cc
index 9fe21a4..9fe21a4 100644
--- a/base/timer_unittest.cc
+++ b/base/timer/timer_unittest.cc
diff --git a/chrome/nacl/nacl_exe_win_64.cc b/chrome/nacl/nacl_exe_win_64.cc
index 5201315..951d128 100644
--- a/chrome/nacl/nacl_exe_win_64.cc
+++ b/chrome/nacl/nacl_exe_win_64.cc
@@ -4,12 +4,12 @@
#include "base/at_exit.h"
#include "base/command_line.h"
-#include "base/hi_res_timer_manager.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/power_monitor/power_monitor.h"
#include "base/process_util.h"
#include "base/strings/string_util.h"
+#include "base/timer/hi_res_timer_manager.h"
#include "chrome/app/breakpad_win.h"
#include "chrome/common/chrome_result_codes.h"
#include "chrome/common/logging_chrome.h"
@@ -34,7 +34,7 @@ int NaClBrokerMain(const content::MainFunctionParams& parameters) {
base::PlatformThread::SetName("CrNaClBrokerMain");
base::PowerMonitor power_monitor;
- HighResolutionTimerManager hi_res_timer_manager;
+ base::HighResolutionTimerManager hi_res_timer_manager;
NaClBrokerListener listener;
listener.Listen();
diff --git a/chrome/nacl/nacl_main.cc b/chrome/nacl/nacl_main.cc
index 21b4aa3..0deea3e 100644
--- a/chrome/nacl/nacl_main.cc
+++ b/chrome/nacl/nacl_main.cc
@@ -5,9 +5,9 @@
#include "build/build_config.h"
#include "base/command_line.h"
-#include "base/hi_res_timer_manager.h"
#include "base/message_loop.h"
#include "base/power_monitor/power_monitor.h"
+#include "base/timer/hi_res_timer_manager.h"
#include "chrome/common/chrome_result_codes.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/logging_chrome.h"
@@ -25,7 +25,7 @@ int NaClMain(const content::MainFunctionParams& parameters) {
base::PlatformThread::SetName("CrNaClMain");
base::PowerMonitor power_monitor;
- HighResolutionTimerManager hi_res_timer_manager;
+ base::HighResolutionTimerManager hi_res_timer_manager;
#if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)
NaClMainPlatformDelegate platform(parameters);
diff --git a/content/browser/browser_main_loop.cc b/content/browser/browser_main_loop.cc
index 98ef6b2..2c7c4a6 100644
--- a/content/browser/browser_main_loop.cc
+++ b/content/browser/browser_main_loop.cc
@@ -7,7 +7,6 @@
#include "base/bind.h"
#include "base/command_line.h"
#include "base/debug/trace_event.h"
-#include "base/hi_res_timer_manager.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/metrics/field_trial.h"
@@ -18,6 +17,7 @@
#include "base/strings/string_number_conversions.h"
#include "base/system_monitor/system_monitor.h"
#include "base/threading/thread_restrictions.h"
+#include "base/timer/hi_res_timer_manager.h"
#include "content/browser/browser_thread_impl.h"
#include "content/browser/download/save_file_manager.h"
#include "content/browser/gamepad/gamepad_service.h"
@@ -394,7 +394,7 @@ void BrowserMainLoop::MainMessageLoopStart() {
}
{
TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:HighResTimerManager")
- hi_res_timer_manager_.reset(new HighResolutionTimerManager);
+ hi_res_timer_manager_.reset(new base::HighResolutionTimerManager);
}
{
TRACE_EVENT0("startup", "BrowserMainLoop::Subsystem:NetworkChangeNotifier")
diff --git a/content/browser/browser_main_loop.h b/content/browser/browser_main_loop.h
index 80a3983..7184b4c 100644
--- a/content/browser/browser_main_loop.h
+++ b/content/browser/browser_main_loop.h
@@ -10,9 +10,9 @@
#include "content/browser/browser_process_sub_thread.h"
class CommandLine;
-class HighResolutionTimerManager;
namespace base {
+class HighResolutionTimerManager;
class MessageLoop;
class PowerMonitor;
class SystemMonitor;
@@ -106,7 +106,7 @@ class CONTENT_EXPORT BrowserMainLoop {
scoped_ptr<base::MessageLoop> main_message_loop_;
scoped_ptr<base::SystemMonitor> system_monitor_;
scoped_ptr<base::PowerMonitor> power_monitor_;
- scoped_ptr<HighResolutionTimerManager> hi_res_timer_manager_;
+ scoped_ptr<base::HighResolutionTimerManager> hi_res_timer_manager_;
scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;
scoped_ptr<media::AudioManager> audio_manager_;
scoped_ptr<media::MIDIManager> midi_manager_;
diff --git a/content/plugin/plugin_main.cc b/content/plugin/plugin_main.cc
index df47c21..69415e0 100644
--- a/content/plugin/plugin_main.cc
+++ b/content/plugin/plugin_main.cc
@@ -10,11 +10,11 @@
#include "base/bind.h"
#include "base/command_line.h"
-#include "base/hi_res_timer_manager.h"
#include "base/message_loop.h"
#include "base/power_monitor/power_monitor.h"
#include "base/strings/string_util.h"
#include "base/threading/platform_thread.h"
+#include "base/timer/hi_res_timer_manager.h"
#include "content/child/child_process.h"
#include "content/plugin/plugin_thread.h"
#include "content/public/common/content_switches.h"
@@ -56,7 +56,7 @@ int PluginMain(const MainFunctionParams& parameters) {
base::PlatformThread::SetName("CrPluginMain");
base::PowerMonitor power_monitor;
- HighResolutionTimerManager high_resolution_timer_manager;
+ base::HighResolutionTimerManager high_resolution_timer_manager;
const CommandLine& parsed_command_line = parameters.command_line;
diff --git a/content/renderer/renderer_main.cc b/content/renderer/renderer_main.cc
index 22f5816..af0d60f 100644
--- a/content/renderer/renderer_main.cc
+++ b/content/renderer/renderer_main.cc
@@ -7,7 +7,6 @@
#include "base/debug/debugger.h"
#include "base/debug/stack_trace.h"
#include "base/debug/trace_event.h"
-#include "base/hi_res_timer_manager.h"
#include "base/i18n/rtl.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop.h"
@@ -21,7 +20,8 @@
#include "base/process_util.h"
#include "base/strings/string_util.h"
#include "base/threading/platform_thread.h"
-#include "base/time.h"
+#include "base/time/time.h"
+#include "base/timer/hi_res_timer_manager.h"
#include "content/child/child_process.h"
#include "content/common/pepper_plugin_registry.h"
#include "content/public/common/content_switches.h"
@@ -165,7 +165,7 @@ int RendererMain(const MainFunctionParams& parameters) {
base::PlatformThread::SetName("CrRendererMain");
base::PowerMonitor power_monitor;
- HighResolutionTimerManager hi_res_timer_manager;
+ base::HighResolutionTimerManager hi_res_timer_manager;
platform.PlatformInitialize();
diff --git a/content/utility/utility_main.cc b/content/utility/utility_main.cc
index ef4fd1e..21489d4 100644
--- a/content/utility/utility_main.cc
+++ b/content/utility/utility_main.cc
@@ -3,10 +3,10 @@
// found in the LICENSE file.
#include "base/command_line.h"
-#include "base/hi_res_timer_manager.h"
#include "base/message_loop.h"
#include "base/power_monitor/power_monitor.h"
#include "base/threading/platform_thread.h"
+#include "base/timer/hi_res_timer_manager.h"
#include "content/child/child_process.h"
#include "content/common/sandbox_linux.h"
#include "content/public/common/content_switches.h"
@@ -27,7 +27,7 @@ int UtilityMain(const MainFunctionParams& parameters) {
base::PlatformThread::SetName("CrUtilityMain");
base::PowerMonitor power_monitor;
- HighResolutionTimerManager hi_res_timer_manager;
+ base::HighResolutionTimerManager hi_res_timer_manager;
#if defined(OS_LINUX)
// Initialize the sandbox before any thread is created.
diff --git a/content/worker/worker_main.cc b/content/worker/worker_main.cc
index a95bae4..e1dfbc5 100644
--- a/content/worker/worker_main.cc
+++ b/content/worker/worker_main.cc
@@ -4,11 +4,11 @@
#include "base/base_switches.h"
#include "base/command_line.h"
-#include "base/hi_res_timer_manager.h"
#include "base/message_loop.h"
#include "base/power_monitor/power_monitor.h"
#include "base/strings/string_util.h"
#include "base/threading/platform_thread.h"
+#include "base/timer/hi_res_timer_manager.h"
#include "content/child/child_process.h"
#include "content/common/sandbox_linux.h"
#include "content/public/common/main_function_params.h"
@@ -32,7 +32,7 @@ int WorkerMain(const MainFunctionParams& parameters) {
base::PlatformThread::SetName("CrWorkerMain");
base::PowerMonitor power_monitor;
- HighResolutionTimerManager hi_res_timer_manager;
+ base::HighResolutionTimerManager hi_res_timer_manager;
#if defined(OS_WIN)
sandbox::TargetServices* target_services =