Home | History | Annotate | Download | only in base
      1 // Copyright 2012 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 #include "sync/internal_api/public/base/invalidation.h"
      6 
      7 #include <cstddef>
      8 #include "base/rand_util.h"
      9 #include "base/strings/string_number_conversions.h"
     10 #include "base/values.h"
     11 
     12 namespace syncer {
     13 
     14 namespace {
     15 // Hopefully enough bytes for uniqueness.
     16 const size_t kBytesInHandle = 16;
     17 }  // namespace
     18 
     19 AckHandle AckHandle::CreateUnique() {
     20   // This isn't a valid UUID, so we don't attempt to format it like one.
     21   uint8 random_bytes[kBytesInHandle];
     22   base::RandBytes(random_bytes, sizeof(random_bytes));
     23   return AckHandle(base::HexEncode(random_bytes, sizeof(random_bytes)),
     24                    base::Time::Now());
     25 }
     26 
     27 AckHandle AckHandle::InvalidAckHandle() {
     28   return AckHandle(std::string(), base::Time());
     29 }
     30 
     31 bool AckHandle::Equals(const AckHandle& other) const {
     32   return state_ == other.state_ && timestamp_ == other.timestamp_;
     33 }
     34 
     35 scoped_ptr<base::DictionaryValue> AckHandle::ToValue() const {
     36   scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
     37   value->SetString("state", state_);
     38   value->SetString("timestamp",
     39                    base::Int64ToString(timestamp_.ToInternalValue()));
     40   return value.Pass();
     41 }
     42 
     43 bool AckHandle::ResetFromValue(const base::DictionaryValue& value) {
     44   if (!value.GetString("state", &state_))
     45     return false;
     46   std::string timestamp_as_string;
     47   if (!value.GetString("timestamp", &timestamp_as_string))
     48     return false;
     49   int64 timestamp_value;
     50   if (!base::StringToInt64(timestamp_as_string, &timestamp_value))
     51     return false;
     52   timestamp_ = base::Time::FromInternalValue(timestamp_value);
     53   return true;
     54 }
     55 
     56 bool AckHandle::IsValid() const {
     57   return !state_.empty();
     58 }
     59 
     60 AckHandle::AckHandle(const std::string& state, base::Time timestamp)
     61     : state_(state), timestamp_(timestamp) {
     62 }
     63 
     64 AckHandle::~AckHandle() {
     65 }
     66 
     67 const int64 Invalidation::kUnknownVersion = -1;
     68 
     69 Invalidation::Invalidation()
     70     : version(kUnknownVersion), ack_handle(AckHandle::InvalidAckHandle()) {
     71 }
     72 
     73 Invalidation::~Invalidation() {
     74 }
     75 
     76 bool Invalidation::Equals(const Invalidation& other) const {
     77   return (version == other.version) && (payload == other.payload) &&
     78       ack_handle.Equals(other.ack_handle);
     79 }
     80 
     81 scoped_ptr<base::DictionaryValue> Invalidation::ToValue() const {
     82   scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
     83   value->SetString("version", base::Int64ToString(version));
     84   value->SetString("payload", payload);
     85   value->Set("ackHandle", ack_handle.ToValue().release());
     86   return value.Pass();
     87 }
     88 
     89 bool Invalidation::ResetFromValue(const base::DictionaryValue& value) {
     90   const base::DictionaryValue* ack_handle_value = NULL;
     91   std::string version_as_string;
     92   if (value.GetString("version", &version_as_string)) {
     93     if (!base::StringToInt64(version_as_string, &version))
     94       return false;
     95   } else {
     96     version = kUnknownVersion;
     97   }
     98   return
     99       value.GetString("payload", &payload) &&
    100       value.GetDictionary("ackHandle", &ack_handle_value) &&
    101       ack_handle.ResetFromValue(*ack_handle_value);
    102 }
    103 
    104 }  // namespace syncer
    105