Add GetTimeTicksNow() to PlatformSupport.

And convert some uses of base::TimeTicks (in particular,
base::TimeTicks::Now()).

Still to do: Convert the use in Waiter (which will require some
injection).

R=kulakowski@chromium.org

Review URL: https://codereview.chromium.org/1345583002 .
diff --git a/mojo/edk/embedder/BUILD.gn b/mojo/edk/embedder/BUILD.gn
index b3144f0..08a42fa 100644
--- a/mojo/edk/embedder/BUILD.gn
+++ b/mojo/edk/embedder/BUILD.gn
@@ -52,7 +52,10 @@
   # mojo_system_impl component.
   visibility = [ ":embedder" ]
 
-  mojo_edk_visibility = [ "mojo/edk/system" ]
+  mojo_edk_visibility = [
+    "mojo/edk/system",
+    "mojo/edk/system:test_utils",
+  ]
 
   sources = [
     "platform_channel_pair.cc",
diff --git a/mojo/edk/embedder/platform_support.h b/mojo/edk/embedder/platform_support.h
index 0218616..5b4f0ac 100644
--- a/mojo/edk/embedder/platform_support.h
+++ b/mojo/edk/embedder/platform_support.h
@@ -9,6 +9,7 @@
 
 #include "mojo/edk/embedder/scoped_platform_handle.h"
 #include "mojo/edk/system/system_impl_export.h"
+#include "mojo/public/c/system/types.h"
 #include "mojo/public/cpp/system/macros.h"
 
 namespace mojo {
@@ -23,6 +24,23 @@
  public:
   virtual ~PlatformSupport() {}
 
+  // Gets a "time-ticks" value:
+  //   - The value should be nondecreasing with respect to time/causality.
+  //   - The value should be in microseconds (i.e., if a caller runs
+  //     continuously, getting the value twice, their difference should be
+  //     approximately the real time elapsed between the samples, in
+  //     microseconds).
+  //   - The value should be nonnegative.
+  //   - The behaviour of the value if execution is suspended (i.e., the
+  //     computer "sleeps") is undefined (i.e., this is not a real-time clock),
+  //     except that it must remain monotonic.
+  //   - As observable, monotonicity should hold across threads.
+  // If multiple |PlatformSupport| implementations/instances are used in a
+  // single system, all implementations must agree (i.e., respect the above as
+  // if there were only a single |PlatformSupport|).
+  virtual MojoTimeTicks GetTimeTicksNow() = 0;
+
+  // Gets cryptographically-secure (pseudo)random bytes.
   virtual void GetCryptoRandomBytes(void* bytes, size_t num_bytes) = 0;
 
   virtual PlatformSharedBuffer* CreateSharedBuffer(size_t num_bytes) = 0;
diff --git a/mojo/edk/embedder/simple_platform_support.cc b/mojo/edk/embedder/simple_platform_support.cc
index 4207120..f314323 100644
--- a/mojo/edk/embedder/simple_platform_support.cc
+++ b/mojo/edk/embedder/simple_platform_support.cc
@@ -5,11 +5,16 @@
 #include "mojo/edk/embedder/simple_platform_support.h"
 
 #include "base/rand_util.h"
+#include "base/time/time.h"
 #include "mojo/edk/embedder/simple_platform_shared_buffer.h"
 
 namespace mojo {
 namespace embedder {
 
+MojoTimeTicks SimplePlatformSupport::GetTimeTicksNow() {
+  return base::TimeTicks::Now().ToInternalValue();
+}
+
 void SimplePlatformSupport::GetCryptoRandomBytes(void* bytes,
                                                  size_t num_bytes) {
   base::RandBytes(bytes, num_bytes);
diff --git a/mojo/edk/embedder/simple_platform_support.h b/mojo/edk/embedder/simple_platform_support.h
index f06ae08..0cc888d 100644
--- a/mojo/edk/embedder/simple_platform_support.h
+++ b/mojo/edk/embedder/simple_platform_support.h
@@ -23,6 +23,7 @@
   SimplePlatformSupport() {}
   ~SimplePlatformSupport() override {}
 
+  MojoTimeTicks GetTimeTicksNow() override;
   void GetCryptoRandomBytes(void* bytes, size_t num_bytes) override;
   PlatformSharedBuffer* CreateSharedBuffer(size_t num_bytes) override;
   PlatformSharedBuffer* CreateSharedBufferFromHandle(
diff --git a/mojo/edk/system/BUILD.gn b/mojo/edk/system/BUILD.gn
index 5367be1..7323026 100644
--- a/mojo/edk/system/BUILD.gn
+++ b/mojo/edk/system/BUILD.gn
@@ -163,6 +163,8 @@
     "//base",
     "//base/test:test_support",
   ]
+
+  mojo_edk_deps = [ "mojo/edk/embedder:platform" ]
 }
 
 test("mojo_system_unittests") {
diff --git a/mojo/edk/system/core.cc b/mojo/edk/system/core.cc
index fbb6cf1..ff21f1c 100644
--- a/mojo/edk/system/core.cc
+++ b/mojo/edk/system/core.cc
@@ -7,7 +7,6 @@
 #include <vector>
 
 #include "base/logging.h"
-#include "base/time/time.h"
 #include "mojo/edk/embedder/platform_shared_buffer.h"
 #include "mojo/edk/embedder/platform_support.h"
 #include "mojo/edk/system/async_waiter.h"
@@ -121,7 +120,7 @@
 }
 
 MojoTimeTicks Core::GetTimeTicksNow() {
-  return base::TimeTicks::Now().ToInternalValue();
+  return platform_support_->GetTimeTicksNow();
 }
 
 MojoResult Core::Close(MojoHandle handle) {
diff --git a/mojo/edk/system/test_utils.cc b/mojo/edk/system/test_utils.cc
index 823bcf4..5d78b15 100644
--- a/mojo/edk/system/test_utils.cc
+++ b/mojo/edk/system/test_utils.cc
@@ -79,11 +79,11 @@
 }
 
 void Stopwatch::Start() {
-  start_time_ = base::TimeTicks::Now();
+  start_time_ = platform_support_.GetTimeTicksNow();
 }
 
 MojoDeadline Stopwatch::Elapsed() {
-  int64_t result = (base::TimeTicks::Now() - start_time_).InMicroseconds();
+  int64_t result = platform_support_.GetTimeTicksNow() - start_time_;
   // |DCHECK_GE|, not |CHECK_GE|, since this may be performance-important.
   DCHECK_GE(result, 0);
   return static_cast<MojoDeadline>(result);
diff --git a/mojo/edk/system/test_utils.h b/mojo/edk/system/test_utils.h
index 59f9dd5..8d424cc 100644
--- a/mojo/edk/system/test_utils.h
+++ b/mojo/edk/system/test_utils.h
@@ -5,7 +5,7 @@
 #ifndef MOJO_EDK_SYSTEM_TEST_UTILS_H_
 #define MOJO_EDK_SYSTEM_TEST_UTILS_H_
 
-#include "base/time/time.h"
+#include "mojo/edk/embedder/simple_platform_support.h"
 #include "mojo/public/c/system/types.h"
 #include "mojo/public/cpp/system/macros.h"
 
@@ -55,7 +55,11 @@
   MojoDeadline Elapsed();
 
  private:
-  base::TimeTicks start_time_;
+  // TODO(vtl): We need this for |GetTimeTicksNow()|. Maybe we should have a
+  // singleton for tests instead? Or maybe it should be injected?
+  embedder::SimplePlatformSupport platform_support_;
+
+  MojoTimeTicks start_time_;
 
   MOJO_DISALLOW_COPY_AND_ASSIGN(Stopwatch);
 };