Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / sync / syncable / entry.cc
blob5593426a74e7e0d6ced15ce13003a99c5bea9ade
1 // Copyright (c) 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 "base/json/string_escape.h"
10 #include "sync/syncable/base_transaction.h"
11 #include "sync/syncable/blob.h"
12 #include "sync/syncable/directory.h"
13 #include "sync/syncable/syncable_columns.h"
15 using std::string;
17 namespace syncer {
18 namespace syncable {
20 Entry::Entry(BaseTransaction* trans, GetById, const Id& id)
21 : basetrans_(trans) {
22 kernel_ = trans->directory()->GetEntryById(id);
25 Entry::Entry(BaseTransaction* trans, GetByClientTag, const string& tag)
26 : basetrans_(trans) {
27 kernel_ = trans->directory()->GetEntryByClientTag(tag);
30 Entry::Entry(BaseTransaction* trans, GetByServerTag, const string& tag)
31 : basetrans_(trans) {
32 kernel_ = trans->directory()->GetEntryByServerTag(tag);
35 Entry::Entry(BaseTransaction* trans, GetByHandle, int64 metahandle)
36 : basetrans_(trans) {
37 kernel_ = trans->directory()->GetEntryByHandle(metahandle);
40 Directory* Entry::dir() const {
41 return basetrans_->directory();
44 Id Entry::ComputePrevIdFromServerPosition(const Id& parent_id) const {
45 return dir()->ComputePrevIdFromServerPosition(kernel_, parent_id);
48 DictionaryValue* Entry::ToValue() const {
49 DictionaryValue* entry_info = new DictionaryValue();
50 entry_info->SetBoolean("good", good());
51 if (good()) {
52 entry_info->Set("kernel", kernel_->ToValue());
53 entry_info->Set("modelType",
54 ModelTypeToValue(GetModelType()));
55 entry_info->SetBoolean("existsOnClientBecauseNameIsNonEmpty",
56 ExistsOnClientBecauseNameIsNonEmpty());
57 entry_info->SetBoolean("isRoot", IsRoot());
59 return entry_info;
62 const string& Entry::Get(StringField field) const {
63 DCHECK(kernel_);
64 return kernel_->ref(field);
67 ModelType Entry::GetServerModelType() const {
68 ModelType specifics_type = kernel_->GetServerModelType();
69 if (specifics_type != UNSPECIFIED)
70 return specifics_type;
72 // Otherwise, we don't have a server type yet. That should only happen
73 // if the item is an uncommitted locally created item.
74 // It's possible we'll need to relax these checks in the future; they're
75 // just here for now as a safety measure.
76 DCHECK(Get(IS_UNSYNCED));
77 DCHECK_EQ(Get(SERVER_VERSION), 0);
78 DCHECK(Get(SERVER_IS_DEL));
79 // Note: can't enforce !Get(ID).ServerKnows() here because that could
80 // actually happen if we hit AttemptReuniteLostCommitResponses.
81 return UNSPECIFIED;
84 ModelType Entry::GetModelType() const {
85 ModelType specifics_type = GetModelTypeFromSpecifics(Get(SPECIFICS));
86 if (specifics_type != UNSPECIFIED)
87 return specifics_type;
88 if (IsRoot())
89 return TOP_LEVEL_FOLDER;
90 // Loose check for server-created top-level folders that aren't
91 // bound to a particular model type.
92 if (!Get(UNIQUE_SERVER_TAG).empty() && Get(IS_DIR))
93 return TOP_LEVEL_FOLDER;
95 return UNSPECIFIED;
98 std::ostream& operator<<(std::ostream& s, const Blob& blob) {
99 for (Blob::const_iterator i = blob.begin(); i != blob.end(); ++i)
100 s << std::hex << std::setw(2)
101 << std::setfill('0') << static_cast<unsigned int>(*i);
102 return s << std::dec;
105 std::ostream& operator<<(std::ostream& os, const Entry& entry) {
106 int i;
107 EntryKernel* const kernel = entry.kernel_;
108 for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) {
109 os << g_metas_columns[i].name << ": "
110 << kernel->ref(static_cast<Int64Field>(i)) << ", ";
112 for ( ; i < TIME_FIELDS_END; ++i) {
113 os << g_metas_columns[i].name << ": "
114 << GetTimeDebugString(kernel->ref(static_cast<TimeField>(i))) << ", ";
116 for ( ; i < ID_FIELDS_END; ++i) {
117 os << g_metas_columns[i].name << ": "
118 << kernel->ref(static_cast<IdField>(i)) << ", ";
120 os << "Flags: ";
121 for ( ; i < BIT_FIELDS_END; ++i) {
122 if (kernel->ref(static_cast<BitField>(i)))
123 os << g_metas_columns[i].name << ", ";
125 for ( ; i < STRING_FIELDS_END; ++i) {
126 const std::string& field = kernel->ref(static_cast<StringField>(i));
127 os << g_metas_columns[i].name << ": " << field << ", ";
129 for ( ; i < PROTO_FIELDS_END; ++i) {
130 std::string escaped_str;
131 base::JsonDoubleQuote(
132 kernel->ref(static_cast<ProtoField>(i)).SerializeAsString(),
133 false,
134 &escaped_str);
135 os << g_metas_columns[i].name << ": " << escaped_str << ", ";
137 for ( ; i < ORDINAL_FIELDS_END; ++i) {
138 os << g_metas_columns[i].name << ": "
139 << kernel->ref(static_cast<OrdinalField>(i)).ToDebugString()
140 << ", ";
142 os << "TempFlags: ";
143 for ( ; i < BIT_TEMPS_END; ++i) {
144 if (kernel->ref(static_cast<BitTemp>(i)))
145 os << "#" << i - BIT_TEMPS_BEGIN << ", ";
147 return os;
150 } // namespace syncable
151 } // namespace syncer