Teach cache coders to encode time_points
authorantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 1 Feb 2017 07:55:44 +0000 (07:55 +0000)
committerantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 1 Feb 2017 07:55:44 +0000 (07:55 +0000)
https://bugs.webkit.org/show_bug.cgi?id=167670

Reviewed by Andreas Kling.

Source/WebKit2:

Encode time_point instead of duration.

* NetworkProcess/cache/NetworkCacheStorage.cpp:
(WebKit::NetworkCache::decodeRecordMetaData):
(WebKit::NetworkCache::Storage::readRecord):
(WebKit::NetworkCache::encodeRecordMetaData):
(WebKit::NetworkCache::Storage::encodeRecord):
(WebKit::NetworkCache::Storage::traverse):

Source/WTF:

* wtf/persistence/Coders.h:
(WTF::Persistence::Coder<std::chrono::system_clock::time_point>::encode):
(WTF::Persistence::Coder<std::chrono::system_clock::time_point>::decode):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@211472 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WTF/ChangeLog
Source/WTF/wtf/persistence/Coders.h
Source/WebKit2/ChangeLog
Source/WebKit2/NetworkProcess/cache/NetworkCacheStorage.cpp

index e7bdc19..128b77d 100644 (file)
@@ -1,3 +1,14 @@
+2017-01-31  Antti Koivisto  <antti@apple.com>
+
+        Teach cache coders to encode time_points
+        https://bugs.webkit.org/show_bug.cgi?id=167670
+
+        Reviewed by Andreas Kling.
+
+        * wtf/persistence/Coders.h:
+        (WTF::Persistence::Coder<std::chrono::system_clock::time_point>::encode):
+        (WTF::Persistence::Coder<std::chrono::system_clock::time_point>::decode):
+
 2017-01-31  Joseph Pecoraro  <pecoraro@apple.com>
 
         Removed unused m_nestedCount from RunLoop
index a6692bc..3cde70a 100644 (file)
@@ -261,6 +261,23 @@ template<typename KeyArg, typename HashArg, typename KeyTraitsArg> struct Coder<
     }
 };
 
+template<> struct Coder<std::chrono::system_clock::time_point> {
+    static void encode(Encoder& encoder, const std::chrono::system_clock::time_point& timePoint)
+    {
+        encoder << static_cast<int64_t>(timePoint.time_since_epoch().count());
+    }
+    
+    static bool decode(Decoder& decoder, std::chrono::system_clock::time_point& result)
+    {
+        int64_t time;
+        if (!decoder.decode(time))
+            return false;
+
+        result = std::chrono::system_clock::time_point(std::chrono::system_clock::duration(static_cast<std::chrono::system_clock::rep>(time)));
+        return true;
+    }
+};
+
 template<> struct Coder<AtomicString> {
     WTF_EXPORT_PRIVATE static void encode(Encoder&, const AtomicString&);
     WTF_EXPORT_PRIVATE static bool decode(Decoder&, AtomicString&);
index 95b5fba..99f4467 100644 (file)
@@ -1,3 +1,19 @@
+2017-01-31  Antti Koivisto  <antti@apple.com>
+
+        Teach cache coders to encode time_points
+        https://bugs.webkit.org/show_bug.cgi?id=167670
+
+        Reviewed by Andreas Kling.
+
+        Encode time_point instead of duration.
+
+        * NetworkProcess/cache/NetworkCacheStorage.cpp:
+        (WebKit::NetworkCache::decodeRecordMetaData):
+        (WebKit::NetworkCache::Storage::readRecord):
+        (WebKit::NetworkCache::encodeRecordMetaData):
+        (WebKit::NetworkCache::Storage::encodeRecord):
+        (WebKit::NetworkCache::Storage::traverse):
+
 2017-01-31  Brent Fulgham  <bfulgham@apple.com>
 
         [WebRTC][WebKit2] Support expanding the sandbox to allow microphone access at process launch
index 15cc902..6eab160 100644 (file)
@@ -364,8 +364,7 @@ struct RecordMetaData {
 
     unsigned cacheStorageVersion;
     Key key;
-    // FIXME: Add encoder/decoder for time_point.
-    std::chrono::milliseconds epochRelativeTimeStamp;
+    std::chrono::system_clock::time_point timeStamp;
     SHA1::Digest headerHash;
     uint64_t headerSize;
     SHA1::Digest bodyHash;
@@ -385,7 +384,7 @@ static bool decodeRecordMetaData(RecordMetaData& metaData, const Data& fileData)
             return false;
         if (!decoder.decode(metaData.key))
             return false;
-        if (!decoder.decode(metaData.epochRelativeTimeStamp))
+        if (!decoder.decode(metaData.timeStamp))
             return false;
         if (!decoder.decode(metaData.headerHash))
             return false;
@@ -439,8 +438,7 @@ void Storage::readRecord(ReadOperation& readOperation, const Data& recordData)
         return;
 
     // Sanity check against time stamps in future.
-    auto timeStamp = std::chrono::system_clock::time_point(metaData.epochRelativeTimeStamp);
-    if (timeStamp > std::chrono::system_clock::now())
+    if (metaData.timeStamp > std::chrono::system_clock::now())
         return;
 
     Data bodyData;
@@ -456,7 +454,7 @@ void Storage::readRecord(ReadOperation& readOperation, const Data& recordData)
     readOperation.expectedBodyHash = metaData.bodyHash;
     readOperation.resultRecord = std::make_unique<Storage::Record>(Storage::Record {
         metaData.key,
-        timeStamp,
+        metaData.timeStamp,
         headerData,
         bodyData,
         metaData.bodyHash
@@ -469,7 +467,7 @@ static Data encodeRecordMetaData(const RecordMetaData& metaData)
 
     encoder << metaData.cacheStorageVersion;
     encoder << metaData.key;
-    encoder << metaData.epochRelativeTimeStamp;
+    encoder << metaData.timeStamp;
     encoder << metaData.headerHash;
     encoder << metaData.headerSize;
     encoder << metaData.bodyHash;
@@ -511,7 +509,7 @@ Data Storage::encodeRecord(const Record& record, std::optional<BlobStorage::Blob
     ASSERT(!blob || bytesEqual(blob.value().data, record.body));
 
     RecordMetaData metaData(record.key);
-    metaData.epochRelativeTimeStamp = std::chrono::duration_cast<std::chrono::milliseconds>(record.timeStamp.time_since_epoch());
+    metaData.timeStamp = record.timeStamp;
     metaData.headerHash = computeSHA1(record.header, m_salt);
     metaData.headerSize = record.header.size();
     metaData.bodyHash = blob ? blob.value().hash : computeSHA1(record.body, m_salt);
@@ -836,7 +834,7 @@ void Storage::traverse(const String& type, TraverseFlags flags, TraverseHandler&
                 if (decodeRecordHeader(fileData, metaData, headerData, m_salt)) {
                     Record record {
                         metaData.key,
-                        std::chrono::system_clock::time_point(metaData.epochRelativeTimeStamp),
+                        metaData.timeStamp,
                         headerData,
                         { },
                         metaData.bodyHash