• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef SYNC_SYNCABLE_SYNCABLE_ID_H_
6 #define SYNC_SYNCABLE_SYNCABLE_ID_H_
7 
8 #include <iosfwd>
9 #include <limits>
10 #include <sstream>
11 #include <string>
12 
13 #include "base/containers/hash_tables.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "sync/base/sync_export.h"
16 
17 class MockConnectionManager;
18 
19 namespace base {
20 class StringValue;
21 }
22 
23 namespace sql {
24 class Statement;
25 }
26 
27 namespace syncer {
28 namespace syncable {
29 struct EntryKernel;
30 class Id;
31 
32 SYNC_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& out, const Id& id);
33 
34 // For historical reasons, 3 concepts got everloaded into the Id:
35 // 1. A unique, opaque identifier for the object.
36 // 2. Flag specifing whether server know about this object.
37 // 3. Flag for root.
38 //
39 // We originally wrapped an integer for this information, but now we use a
40 // string. It will have one of three forms:
41 // 1. c<client only opaque id> for client items that have not been committed.
42 // 2. r for the root item.
43 // 3. s<server provided opaque id> for items that the server knows about.
44 class SYNC_EXPORT Id {
45  public:
46   // This constructor will be handy even when we move away from int64s, just
47   // for unit tests.
Id()48   inline Id() : s_("r") { }
Id(const Id & that)49   inline Id(const Id& that) {
50     Copy(that);
51   }
52   inline Id& operator = (const Id& that) {
53     Copy(that);
54     return *this;
55   }
Copy(const Id & that)56   inline void Copy(const Id& that) {
57     this->s_ = that.s_;
58   }
IsRoot()59   inline bool IsRoot() const {
60     return "r" == s_;
61   }
ServerKnows()62   inline bool ServerKnows() const {
63     return s_[0] == 's' || s_ == "r";
64   }
65 
66   // TODO(sync): We could use null here, but to ease conversion we use "r".
67   // fix this, this is madness :)
IsNull()68   inline bool IsNull() const {
69     return IsRoot();
70   }
Clear()71   inline void Clear() {
72     s_ = "r";
73   }
compare(const Id & that)74   inline int compare(const Id& that) const {
75     return s_.compare(that.s_);
76   }
77   inline bool operator == (const Id& that) const {
78     return s_ == that.s_;
79   }
80   inline bool operator != (const Id& that) const {
81     return s_ != that.s_;
82   }
83   inline bool operator < (const Id& that) const {
84     return s_ < that.s_;
85   }
86   inline bool operator > (const Id& that) const {
87     return s_ > that.s_;
88   }
89 
value()90   const std::string& value() const {
91     return s_;
92   }
93 
94   // Return the next highest ID in the lexicographic ordering.  This is
95   // useful for computing upper bounds on std::sets that are ordered
96   // by operator<.
97   Id GetLexicographicSuccessor() const;
98 
99   // Dumps the ID as a value and returns it.  Transfers ownership of
100   // the StringValue to the caller.
101   base::StringValue* ToValue() const;
102 
103   // Three functions are used to work with our proto buffers.
104   std::string GetServerId() const;
105   static Id CreateFromServerId(const std::string& server_id);
106   // This should only be used if you get back a reference to a local
107   // id from the server. Returns a client only opaque id.
108   static Id CreateFromClientString(const std::string& local_id);
109 
110   // This method returns an ID that will compare less than any valid ID.
111   // The returned ID is not a valid ID itself.  This is useful for
112   // computing lower bounds on std::sets that are ordered by operator<.
113   static Id GetLeastIdForLexicographicComparison();
114 
115  private:
116   friend scoped_ptr<EntryKernel> UnpackEntry(sql::Statement* statement);
117   friend void BindFields(const EntryKernel& entry,
118                          sql::Statement* statement);
119   SYNC_EXPORT_PRIVATE friend std::ostream& operator<<(std::ostream& out,
120                                                       const Id& id);
121   friend class MockConnectionManager;
122   friend class SyncableIdTest;
123 
124   std::string s_;
125 };
126 
127 SYNC_EXPORT_PRIVATE Id GetNullId();
128 
129 }  // namespace syncable
130 }  // namespace syncer
131 
132 #endif  // SYNC_SYNCABLE_SYNCABLE_ID_H_
133