1 // Copyright 2017 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATA_FORMAT_VERSION_H_ 6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATA_FORMAT_VERSION_H_ 7 8 #include <cstdint> 9 #include <limits> 10 11 #include "base/logging.h" 12 #include "content/common/content_export.h" 13 14 namespace content { 15 16 // Contains version data for the wire format used for encoding IndexedDB values. 17 // A version tuple (a, b) is at least as new as (a', b') 18 // iff a >= a' and b >= b'. 19 class IndexedDBDataFormatVersion { 20 public: IndexedDBDataFormatVersion()21 constexpr IndexedDBDataFormatVersion() {} IndexedDBDataFormatVersion(uint32_t v8_version,uint32_t blink_version)22 constexpr IndexedDBDataFormatVersion(uint32_t v8_version, 23 uint32_t blink_version) 24 : v8_version_(v8_version), blink_version_(blink_version) {} 25 GetCurrent()26 static IndexedDBDataFormatVersion GetCurrent() { return current_; } GetMutableCurrentForTesting()27 static IndexedDBDataFormatVersion& GetMutableCurrentForTesting() { 28 return current_; 29 } 30 v8_version()31 uint32_t v8_version() const { return v8_version_; } blink_version()32 uint32_t blink_version() const { return blink_version_; } 33 34 bool operator==(const IndexedDBDataFormatVersion& other) const { 35 return v8_version_ == other.v8_version_ && 36 blink_version_ == other.blink_version_; 37 } 38 bool operator!=(const IndexedDBDataFormatVersion& other) const { 39 return !operator==(other); 40 } 41 IsAtLeast(const IndexedDBDataFormatVersion & other)42 bool IsAtLeast(const IndexedDBDataFormatVersion& other) const { 43 return v8_version_ >= other.v8_version_ && 44 blink_version_ >= other.blink_version_; 45 } 46 47 // Encodes and decodes the tuple from an int64_t. 48 // This scheme is chosen so that earlier versions (before we reported both the 49 // Blink and V8 versions) decode properly, with a V8 version of 0. Encode()50 int64_t Encode() const { 51 // Since negative values are considered invalid, this scheme will only work 52 // as long as the v8 version would not overflow int32_t. We check both 53 // components, to be consistent. 54 DCHECK_GE(static_cast<int32_t>(v8_version_), 0); 55 DCHECK_GE(static_cast<int32_t>(blink_version_), 0); 56 return (static_cast<int64_t>(v8_version_) << 32) | blink_version_; 57 } Decode(int64_t encoded)58 static IndexedDBDataFormatVersion Decode(int64_t encoded) { 59 DCHECK_GE(encoded, 0); 60 return IndexedDBDataFormatVersion(encoded >> 32, encoded); 61 } 62 63 private: 64 uint32_t v8_version_ = 0; 65 uint32_t blink_version_ = 0; 66 67 CONTENT_EXPORT static IndexedDBDataFormatVersion current_; 68 }; 69 70 } // namespace content 71 72 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATA_FORMAT_VERSION_H_ 73