[go: nahoru, domu]

Support base::Token serialization and deserialization

Change-Id: Ib245310f3a43dba9d014ba280e7d67dac3212c58
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1737189
Commit-Queue: Collin Baker <collinbaker@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#684164}
diff --git a/base/token.cc b/base/token.cc
index e7ad8967..ff6acef 100644
--- a/base/token.cc
+++ b/base/token.cc
@@ -6,6 +6,7 @@
 
 #include <inttypes.h>
 
+#include "base/pickle.h"
 #include "base/rand_util.h"
 #include "base/strings/stringprintf.h"
 
@@ -25,4 +26,21 @@
   return base::StringPrintf("%016" PRIX64 "%016" PRIX64, high_, low_);
 }
 
+void WriteTokenToPickle(Pickle* pickle, const Token& token) {
+  pickle->WriteUInt64(token.high());
+  pickle->WriteUInt64(token.low());
+}
+
+Optional<Token> ReadTokenFromPickle(PickleIterator* pickle_iterator) {
+  uint64_t high;
+  if (!pickle_iterator->ReadUInt64(&high))
+    return nullopt;
+
+  uint64_t low;
+  if (!pickle_iterator->ReadUInt64(&low))
+    return nullopt;
+
+  return Token(high, low);
+}
+
 }  // namespace base
diff --git a/base/token.h b/base/token.h
index e2843a6d..c9af068 100644
--- a/base/token.h
+++ b/base/token.h
@@ -12,6 +12,7 @@
 
 #include "base/base_export.h"
 #include "base/hash/hash.h"
+#include "base/optional.h"
 
 namespace base {
 
@@ -67,6 +68,14 @@
   }
 };
 
+class Pickle;
+class PickleIterator;
+
+// For serializing and deserializing Token values.
+BASE_EXPORT void WriteTokenToPickle(Pickle* pickle, const Token& token);
+BASE_EXPORT Optional<Token> ReadTokenFromPickle(
+    PickleIterator* pickle_iterator);
+
 }  // namespace base
 
 #endif  // BASE_TOKEN_H_
diff --git a/base/token_unittest.cc b/base/token_unittest.cc
index 7e3bf2c8..2b5bf50 100644
--- a/base/token_unittest.cc
+++ b/base/token_unittest.cc
@@ -3,6 +3,8 @@
 // found in the LICENSE file.
 
 #include "base/token.h"
+
+#include "base/pickle.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
 namespace base {
@@ -54,4 +56,12 @@
             Token(0xfffffffffffffffdull, 0xfffffffffffffffeull).ToString());
 }
 
+TEST(TokenTest, Pickle) {
+  Pickle pickle;
+  WriteTokenToPickle(&pickle, kTestToken);
+
+  PickleIterator iterator(pickle);
+  EXPECT_EQ(kTestToken, ReadTokenFromPickle(&iterator));
+}
+
 }  // namespace base