• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 "components/invalidation/invalidation_util.h"
6 
7 #include <ostream>
8 #include <sstream>
9 
10 #include "base/json/json_writer.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/values.h"
13 #include "components/invalidation/invalidation.h"
14 #include "google/cacheinvalidation/include/types.h"
15 #include "google/cacheinvalidation/types.pb.h"
16 
17 namespace syncer {
18 
operator ()(const invalidation::ObjectId & lhs,const invalidation::ObjectId & rhs) const19 bool ObjectIdLessThan::operator()(const invalidation::ObjectId& lhs,
20                                   const invalidation::ObjectId& rhs) const {
21   return (lhs.source() < rhs.source()) ||
22          (lhs.source() == rhs.source() && lhs.name() < rhs.name());
23 }
24 
operator ()(const Invalidation & a,const Invalidation & b) const25 bool InvalidationVersionLessThan::operator()(const Invalidation& a,
26                                              const Invalidation& b) const {
27   DCHECK(a.object_id() == b.object_id())
28       << "a: " << ObjectIdToString(a.object_id()) << ", "
29       << "b: " << ObjectIdToString(a.object_id());
30 
31   if (a.is_unknown_version() && !b.is_unknown_version())
32     return true;
33 
34   if (!a.is_unknown_version() && b.is_unknown_version())
35     return false;
36 
37   if (a.is_unknown_version() && b.is_unknown_version())
38     return false;
39 
40   return a.version() < b.version();
41 }
42 
ObjectIdToValue(const invalidation::ObjectId & object_id)43 scoped_ptr<base::DictionaryValue> ObjectIdToValue(
44     const invalidation::ObjectId& object_id) {
45   scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
46   value->SetInteger("source", object_id.source());
47   value->SetString("name", object_id.name());
48   return value.Pass();
49 }
50 
ObjectIdFromValue(const base::DictionaryValue & value,invalidation::ObjectId * out)51 bool ObjectIdFromValue(const base::DictionaryValue& value,
52                        invalidation::ObjectId* out) {
53   *out = invalidation::ObjectId();
54   std::string name;
55   int source = 0;
56   if (!value.GetInteger("source", &source) || !value.GetString("name", &name)) {
57     return false;
58   }
59   *out = invalidation::ObjectId(source, name);
60   return true;
61 }
62 
ObjectIdToString(const invalidation::ObjectId & object_id)63 std::string ObjectIdToString(const invalidation::ObjectId& object_id) {
64   scoped_ptr<base::DictionaryValue> value(ObjectIdToValue(object_id));
65   std::string str;
66   base::JSONWriter::Write(value.get(), &str);
67   return str;
68 }
69 
70 }  // namespace syncer
71