Bug 1256562 part.1 Implement nsCocoaUtils::GetEventTimeStamp() to convert from timeStamp of NSEvent to TimeStamp r?birtles, r?mstange draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Mon, 26 Dec 2016 12:23:37 +0900
changeset 453854 38ab9e9609808352a0244553cc40745780f064b8
parent 453266 2785aaf276ba29fb2e1f5607d90d441fee42efb4
child 453855 d92549985844c9fc32bc516d3e74db44df71f853
push id39748
push usermasayuki@d-toybox.com
push dateMon, 26 Dec 2016 03:49:32 +0000
reviewersbirtles, mstange
bugs1256562
milestone53.0a1
Bug 1256562 part.1 Implement nsCocoaUtils::GetEventTimeStamp() to convert from timeStamp of NSEvent to TimeStamp r?birtles, r?mstange This patch implements nsCocoaUtils::GetEventTimeStamp() which hides how to get TimeStamp from timeStamp of NSEvent from other developers. Different from Windows and GTK, we don't need to use SystemTimeConverter and implement CurrentTimeGetter class because the internal value of the macOS implementation of TimeStamp is based on mach_absolute_time(), which measures "ticks" since boot. Event timestamps are NSTimeIntervals (seconds) since boot. So the two time representations already have the same base; we only need to convert seconds into ticks. MozReview-Commit-ID: LvioyJOM7S9
widget/cocoa/nsCocoaUtils.h
widget/cocoa/nsCocoaUtils.mm
--- a/widget/cocoa/nsCocoaUtils.h
+++ b/widget/cocoa/nsCocoaUtils.h
@@ -29,16 +29,17 @@
 enum {
   NSEventPhaseMayBegin    = 0x1 << 5
 };
 #endif
 
 class nsIWidget;
 
 namespace mozilla {
+class TimeStamp;
 namespace gfx {
 class SourceSurface;
 } // namespace gfx
 } // namespace mozilla
 
 // Used to retain a Cocoa object for the remainder of a method's execution.
 class nsAutoRetainCocoaObject {
 public:
@@ -372,11 +373,17 @@ public:
   /**
    * Convert string with font attribute to NSMutableAttributedString
    */
   static NSMutableAttributedString* GetNSMutableAttributedString(
            const nsAString& aText,
            const nsTArray<mozilla::FontRange>& aFontRanges,
            const bool aIsVertical,
            const CGFloat aBackingScaleFactor);
+
+  /**
+   * Compute TimeStamp from an event's timestamp.
+   * If aEventTime is 0, this returns current timestamp.
+   */
+  static mozilla::TimeStamp GetEventTimeStamp(NSTimeInterval aEventTime);
 };
 
 #endif // nsCocoaUtils_h_
--- a/widget/cocoa/nsCocoaUtils.mm
+++ b/widget/cocoa/nsCocoaUtils.mm
@@ -1015,8 +1015,27 @@ nsCocoaUtils::GetNSMutableAttributedStri
                     value:[NSNumber numberWithInt: 1]
                     range:NSMakeRange(0, [attrStr length])];
   }
 
   return attrStr;
 
   NS_OBJC_END_TRY_ABORT_BLOCK_NIL
 }
+
+TimeStamp
+nsCocoaUtils::GetEventTimeStamp(NSTimeInterval aEventTime)
+{
+  if (!aEventTime) {
+    // If the event is generated by a 3rd party application, its timestamp
+    // may be 0.  In this case, just return current timestamp.
+    // XXX Should we cache last event time?
+    return TimeStamp::Now();
+  }
+  // The internal value of the macOS implementation of TimeStamp is based on
+  // mach_absolute_time(), which measures "ticks" since boot.
+  // Event timestamps are NSTimeIntervals (seconds) since boot. So the two time
+  // representations already have the same base; we only need to convert
+  // seconds into ticks.
+  int64_t tick =
+    BaseTimeDurationPlatformUtils::TicksFromMilliseconds(aEventTime * 1000.0);
+  return TimeStamp::FromSystemTime(tick);
+}