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.
5 #include "sync/syncable/entry.h"
9 #include "base/json/string_escape.h"
10 #include "base/strings/string_util.h"
11 #include "sync/syncable/directory.h"
12 #include "sync/syncable/syncable_base_transaction.h"
13 #include "sync/syncable/syncable_columns.h"
20 Entry::Entry(BaseTransaction
* trans
, GetById
, const Id
& id
)
22 kernel_
= trans
->directory()->GetEntryById(id
);
25 Entry::Entry(BaseTransaction
* trans
, GetByClientTag
, const string
& tag
)
27 kernel_
= trans
->directory()->GetEntryByClientTag(tag
);
30 Entry::Entry(BaseTransaction
* trans
, GetTypeRoot
, ModelType type
)
32 const std::string
& tag
= ModelTypeToRootTag(type
);
33 kernel_
= trans
->directory()->GetEntryByServerTag(tag
);
36 Entry::Entry(BaseTransaction
* trans
, GetByHandle
, int64 metahandle
)
38 kernel_
= trans
->directory()->GetEntryByHandle(metahandle
);
41 Entry::Entry(BaseTransaction
* trans
, GetByServerTag
, const string
& tag
)
43 kernel_
= trans
->directory()->GetEntryByServerTag(tag
);
46 Directory
* Entry::dir() const {
47 return basetrans_
->directory();
50 base::DictionaryValue
* Entry::ToValue(Cryptographer
* cryptographer
) const {
51 base::DictionaryValue
* entry_info
= new base::DictionaryValue();
52 entry_info
->SetBoolean("good", good());
54 entry_info
->Set("kernel", kernel_
->ToValue(cryptographer
));
55 entry_info
->Set("modelType",
56 ModelTypeToValue(GetModelType()));
57 entry_info
->SetBoolean("existsOnClientBecauseNameIsNonEmpty",
58 ExistsOnClientBecauseNameIsNonEmpty());
59 entry_info
->SetBoolean("isRoot", IsRoot());
64 bool Entry::GetSyncing() const {
66 return kernel_
->ref(SYNCING
);
69 bool Entry::GetDirtySync() const {
71 return kernel_
->ref(DIRTY_SYNC
);
74 ModelType
Entry::GetServerModelType() const {
75 ModelType specifics_type
= kernel_
->GetServerModelType();
76 if (specifics_type
!= UNSPECIFIED
)
77 return specifics_type
;
79 // Otherwise, we don't have a server type yet. That should only happen
80 // if the item is an uncommitted locally created item.
81 // It's possible we'll need to relax these checks in the future; they're
82 // just here for now as a safety measure.
83 DCHECK(GetIsUnsynced());
84 DCHECK_EQ(GetServerVersion(), 0);
85 DCHECK(GetServerIsDel());
86 // Note: can't enforce !GetId().ServerKnows() here because that could
87 // actually happen if we hit AttemptReuniteLostCommitResponses.
91 ModelType
Entry::GetModelType() const {
92 ModelType specifics_type
= GetModelTypeFromSpecifics(GetSpecifics());
93 if (specifics_type
!= UNSPECIFIED
)
94 return specifics_type
;
96 return TOP_LEVEL_FOLDER
;
97 // Loose check for server-created top-level folders that aren't
98 // bound to a particular model type.
99 if (!GetUniqueServerTag().empty() && GetIsDir())
100 return TOP_LEVEL_FOLDER
;
105 Id
Entry::GetPredecessorId() const {
106 return dir()->GetPredecessorId(kernel_
);
109 Id
Entry::GetSuccessorId() const {
110 return dir()->GetSuccessorId(kernel_
);
113 Id
Entry::GetFirstChildId() const {
114 return dir()->GetFirstChildId(basetrans_
, kernel_
);
117 void Entry::GetChildHandles(std::vector
<int64
>* result
) const {
118 dir()->GetChildHandlesById(basetrans_
, GetId(), result
);
121 int Entry::GetTotalNodeCount() const {
122 return dir()->GetTotalNodeCount(basetrans_
, kernel_
);
125 int Entry::GetPositionIndex() const {
126 return dir()->GetPositionIndex(basetrans_
, kernel_
);
129 bool Entry::ShouldMaintainPosition() const {
130 return kernel_
->ShouldMaintainPosition();
133 bool Entry::ShouldMaintainHierarchy() const {
134 return kernel_
->ShouldMaintainHierarchy();
137 std::ostream
& operator<<(std::ostream
& os
, const Entry
& entry
) {
139 EntryKernel
* const kernel
= entry
.kernel_
;
140 for (i
= BEGIN_FIELDS
; i
< INT64_FIELDS_END
; ++i
) {
141 os
<< g_metas_columns
[i
].name
<< ": "
142 << kernel
->ref(static_cast<Int64Field
>(i
)) << ", ";
144 for ( ; i
< TIME_FIELDS_END
; ++i
) {
145 os
<< g_metas_columns
[i
].name
<< ": "
146 << GetTimeDebugString(kernel
->ref(static_cast<TimeField
>(i
))) << ", ";
148 for ( ; i
< ID_FIELDS_END
; ++i
) {
149 os
<< g_metas_columns
[i
].name
<< ": "
150 << kernel
->ref(static_cast<IdField
>(i
)) << ", ";
153 for ( ; i
< BIT_FIELDS_END
; ++i
) {
154 if (kernel
->ref(static_cast<BitField
>(i
)))
155 os
<< g_metas_columns
[i
].name
<< ", ";
157 for ( ; i
< STRING_FIELDS_END
; ++i
) {
158 const std::string
& field
= kernel
->ref(static_cast<StringField
>(i
));
159 os
<< g_metas_columns
[i
].name
<< ": " << field
<< ", ";
161 for ( ; i
< PROTO_FIELDS_END
; ++i
) {
162 std::string escaped_str
= base::EscapeBytesAsInvalidJSONString(
163 kernel
->ref(static_cast<ProtoField
>(i
)).SerializeAsString(),
165 os
<< g_metas_columns
[i
].name
<< ": " << escaped_str
<< ", ";
167 for ( ; i
< UNIQUE_POSITION_FIELDS_END
; ++i
) {
168 os
<< g_metas_columns
[i
].name
<< ": "
169 << kernel
->ref(static_cast<UniquePositionField
>(i
)).ToDebugString()
172 for ( ; i
< ATTACHMENT_METADATA_FIELDS_END
; ++i
) {
173 std::string escaped_str
= base::EscapeBytesAsInvalidJSONString(
174 kernel
->ref(static_cast<AttachmentMetadataField
>(i
))
175 .SerializeAsString(),
177 os
<< g_metas_columns
[i
].name
<< ": " << escaped_str
<< ", ";
180 for ( ; i
< BIT_TEMPS_END
; ++i
) {
181 if (kernel
->ref(static_cast<BitTemp
>(i
)))
182 os
<< "#" << i
- BIT_TEMPS_BEGIN
<< ", ";
187 } // namespace syncable
188 } // namespace syncer