Roll src/third_party/WebKit 5dc6c7a:f688398 (svn 201993:201994)
[chromium-blink-merge.git] / sync / syncable / entry.cc
blobca249e5dc978a1db45d3d0a784d3b4f032a94029
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"
7 #include <iomanip>
9 #include "sync/syncable/directory.h"
10 #include "sync/syncable/syncable_base_transaction.h"
12 namespace syncer {
13 namespace syncable {
15 Entry::Entry(BaseTransaction* trans, GetById, const Id& id)
16 : basetrans_(trans) {
17 kernel_ = trans->directory()->GetEntryById(id);
20 Entry::Entry(BaseTransaction* trans, GetByClientTag, const std::string& tag)
21 : basetrans_(trans) {
22 kernel_ = trans->directory()->GetEntryByClientTag(tag);
25 Entry::Entry(BaseTransaction* trans, GetTypeRoot, ModelType type)
26 : basetrans_(trans) {
27 const std::string& tag = ModelTypeToRootTag(type);
28 kernel_ = trans->directory()->GetEntryByServerTag(tag);
31 Entry::Entry(BaseTransaction* trans, GetByHandle, int64 metahandle)
32 : basetrans_(trans) {
33 kernel_ = trans->directory()->GetEntryByHandle(metahandle);
36 Entry::Entry(BaseTransaction* trans, GetByServerTag, const std::string& tag)
37 : basetrans_(trans) {
38 kernel_ = trans->directory()->GetEntryByServerTag(tag);
41 Directory* Entry::dir() const {
42 return basetrans_->directory();
45 base::DictionaryValue* Entry::ToValue(Cryptographer* cryptographer) const {
46 base::DictionaryValue* entry_info = new base::DictionaryValue();
47 entry_info->SetBoolean("good", good());
48 if (good()) {
49 entry_info->Set("kernel", kernel_->ToValue(cryptographer));
50 entry_info->Set("modelType",
51 ModelTypeToValue(GetModelType()));
52 entry_info->SetBoolean("existsOnClientBecauseNameIsNonEmpty",
53 ExistsOnClientBecauseNameIsNonEmpty());
54 entry_info->SetBoolean("isRoot", IsRoot());
56 return entry_info;
59 bool Entry::GetSyncing() const {
60 DCHECK(kernel_);
61 return kernel_->ref(SYNCING);
64 bool Entry::GetDirtySync() const {
65 DCHECK(kernel_);
66 return kernel_->ref(DIRTY_SYNC);
69 ModelType Entry::GetServerModelType() const {
70 ModelType specifics_type = kernel_->GetServerModelType();
71 if (specifics_type != UNSPECIFIED)
72 return specifics_type;
74 // Otherwise, we don't have a server type yet. That should only happen
75 // if the item is an uncommitted locally created item.
76 // It's possible we'll need to relax these checks in the future; they're
77 // just here for now as a safety measure.
78 DCHECK(GetIsUnsynced());
79 DCHECK_EQ(GetServerVersion(), 0);
80 DCHECK(GetServerIsDel());
81 // Note: can't enforce !GetId().ServerKnows() here because that could
82 // actually happen if we hit AttemptReuniteLostCommitResponses.
83 return UNSPECIFIED;
86 ModelType Entry::GetModelType() const {
87 ModelType specifics_type = GetModelTypeFromSpecifics(GetSpecifics());
88 if (specifics_type != UNSPECIFIED)
89 return specifics_type;
90 if (IsRoot())
91 return TOP_LEVEL_FOLDER;
92 // Loose check for server-created top-level folders that aren't
93 // bound to a particular model type.
94 if (!GetUniqueServerTag().empty() && GetIsDir())
95 return TOP_LEVEL_FOLDER;
97 return UNSPECIFIED;
100 Id Entry::GetPredecessorId() const {
101 return dir()->GetPredecessorId(kernel_);
104 Id Entry::GetSuccessorId() const {
105 return dir()->GetSuccessorId(kernel_);
108 Id Entry::GetFirstChildId() const {
109 return dir()->GetFirstChildId(basetrans_, kernel_);
112 void Entry::GetChildHandles(std::vector<int64>* result) const {
113 dir()->GetChildHandlesById(basetrans_, GetId(), result);
116 int Entry::GetTotalNodeCount() const {
117 return dir()->GetTotalNodeCount(basetrans_, kernel_);
120 int Entry::GetPositionIndex() const {
121 return dir()->GetPositionIndex(basetrans_, kernel_);
124 bool Entry::ShouldMaintainPosition() const {
125 return kernel_->ShouldMaintainPosition();
128 bool Entry::ShouldMaintainHierarchy() const {
129 return kernel_->ShouldMaintainHierarchy();
132 std::ostream& operator<<(std::ostream& os, const Entry& entry) {
133 os << *(entry.kernel_);
134 return os;
137 } // namespace syncable
138 } // namespace syncer