|
OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2006-2009 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 "chrome/browser/sync/engine/build_commit_command.h" | |
6 | |
7 #include <set> | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "chrome/browser/sync/engine/syncer_proto_util.h" | |
12 #include "chrome/browser/sync/engine/syncer_session.h" | |
13 #include "chrome/browser/sync/engine/syncer_util.h" | |
14 #include "chrome/browser/sync/engine/syncproto.h" | |
15 #include "chrome/browser/sync/syncable/syncable.h" | |
16 #include "chrome/browser/sync/syncable/syncable_changes_version.h" | |
17 #include "chrome/browser/sync/util/character_set_converters.h" | |
18 #include "chrome/browser/sync/util/sync_types.h" | |
19 | |
20 using std::set; | |
21 using std::string; | |
22 using std::vector; | |
23 using syncable::ExtendedAttribute; | |
24 using syncable::Id; | |
25 using syncable::MutableEntry; | |
26 using syncable::Name; | |
27 | |
28 namespace browser_sync { | |
29 | |
30 BuildCommitCommand::BuildCommitCommand() {} | |
31 BuildCommitCommand::~BuildCommitCommand() {} | |
32 | |
33 void BuildCommitCommand::ExecuteImpl(SyncerSession *session) { | |
idana
2009/09/10 05:44:37
nit: "SyncerSession *session" -> "SyncerSession* s
| |
34 ClientToServerMessage message; | |
35 message.set_share(ToUTF8(session->account_name()).get_string()); | |
36 message.set_message_contents(ClientToServerMessage::COMMIT); | |
37 | |
38 CommitMessage* commit_message = message.mutable_commit(); | |
39 commit_message->set_cache_guid( | |
40 session->write_transaction()->directory()->cache_guid()); | |
41 | |
42 const vector<Id>& commit_ids = session->commit_ids(); | |
43 for (size_t i = 0; i < commit_ids.size(); i++) { | |
44 Id id = commit_ids[i]; | |
45 SyncEntity* sync_entry = | |
46 static_cast<SyncEntity*>(commit_message->add_entries()); | |
47 sync_entry->set_id(id); | |
48 MutableEntry meta_entry(session->write_transaction(), | |
49 syncable::GET_BY_ID, | |
50 id); | |
51 CHECK(meta_entry.good()); | |
52 // this is the only change we make to the entry in this function. | |
53 meta_entry.Put(syncable::SYNCING, true); | |
54 | |
55 Name name = meta_entry.GetName(); | |
56 CHECK(!name.value().empty()); // Make sure this isn't an update. | |
57 sync_entry->set_name(ToUTF8(name.value()).get_string()); | |
58 // Set the non_unique_name if we have one. If we do, the server ignores | |
59 // the |name| value (using |non_unique_name| instead), and will return | |
60 // in the CommitResponse a unique name if one is generated. Even though | |
61 // we could get away with only sending |name|, we send both because it | |
62 // may aid in logging. | |
63 if (name.value() != name.non_unique_value()) { | |
64 sync_entry->set_non_unique_name( | |
65 ToUTF8(name.non_unique_value()).get_string()); | |
66 } | |
67 // deleted items with negative parent ids can be a problem so we set the | |
idana
2009/09/10 05:44:37
"deleted" -> "Deleted"
| |
68 // parent to 0. (TODO(sync): Still true in protocol? | |
idana
2009/09/10 05:44:37
Missing ")." at the end of this sentence.
| |
69 Id new_parent_id; | |
70 if (meta_entry.Get(syncable::IS_DEL) && | |
71 !meta_entry.Get(syncable::PARENT_ID).ServerKnows()) { | |
72 new_parent_id = session->write_transaction()->root_id(); | |
73 } else { | |
74 new_parent_id = meta_entry.Get(syncable::PARENT_ID); | |
75 } | |
76 sync_entry->set_parent_id(new_parent_id); | |
77 // TODO(sync): Investigate all places that think transactional commits | |
78 // actually exist. | |
79 // | |
80 // This is the only logic we'll need when transactional commits are | |
81 // moved to the server. | |
82 // If our parent has changes, send up the old one so the server can | |
83 // correctly deal with multiple parents. | |
84 if (new_parent_id != meta_entry.Get(syncable::SERVER_PARENT_ID) && | |
85 0 != meta_entry.Get(syncable::BASE_VERSION) && | |
86 syncable::CHANGES_VERSION != meta_entry.Get(syncable::BASE_VERSION)) { | |
87 sync_entry->set_old_parent_id(meta_entry.Get(syncable::SERVER_PARENT_ID)); | |
88 } | |
89 | |
90 int64 version = meta_entry.Get(syncable::BASE_VERSION); | |
91 if (syncable::CHANGES_VERSION == version || 0 == version) { | |
92 // If this CHECK triggers during unit testing, check that we haven't | |
93 // altered an item that's an unapplied update. | |
94 CHECK(!id.ServerKnows()) << meta_entry; | |
95 sync_entry->set_version(0); | |
96 } else { | |
97 CHECK(id.ServerKnows()) << meta_entry; | |
98 sync_entry->set_version(meta_entry.Get(syncable::BASE_VERSION)); | |
99 } | |
100 sync_entry->set_ctime(ClientTimeToServerTime( | |
101 meta_entry.Get(syncable::CTIME))); | |
102 sync_entry->set_mtime(ClientTimeToServerTime( | |
103 meta_entry.Get(syncable::MTIME))); | |
104 | |
105 set<ExtendedAttribute> extended_attributes; | |
106 meta_entry.GetAllExtendedAttributes( | |
107 session->write_transaction(), &extended_attributes); | |
108 set<ExtendedAttribute>::iterator iter; | |
109 sync_pb::ExtendedAttributes* mutable_extended_attributes = | |
110 sync_entry->mutable_extended_attributes(); | |
111 for (iter = extended_attributes.begin(); iter != extended_attributes.end(); | |
112 ++iter) { | |
113 sync_pb::ExtendedAttributes_ExtendedAttribute *extended_attribute = | |
114 mutable_extended_attributes->add_extendedattribute(); | |
115 extended_attribute->set_key(ToUTF8(iter->key()).get_string()); | |
116 SyncerProtoUtil::CopyBlobIntoProtoBytes(iter->value(), | |
117 extended_attribute->mutable_value()); | |
118 } | |
119 | |
120 // Deletion is final on the server, let's move things and then delete them. | |
121 if (meta_entry.Get(syncable::IS_DEL)) { | |
122 sync_entry->set_deleted(true); | |
123 } else if (meta_entry.Get(syncable::IS_BOOKMARK_OBJECT)) { | |
124 sync_pb::SyncEntity_BookmarkData* bookmark = | |
125 sync_entry->mutable_bookmarkdata(); | |
126 bookmark->set_bookmark_folder(meta_entry.Get(syncable::IS_DIR)); | |
127 const Id& prev_id = meta_entry.Get(syncable::PREV_ID); | |
128 string prev_string = prev_id.IsRoot() ? string() : prev_id.GetServerId(); | |
129 sync_entry->set_insert_after_item_id(prev_string); | |
130 | |
131 if (!meta_entry.Get(syncable::IS_DIR)) { | |
132 string bookmark_url = ToUTF8(meta_entry.Get(syncable::BOOKMARK_URL)); | |
133 bookmark->set_bookmark_url(bookmark_url); | |
134 SyncerProtoUtil::CopyBlobIntoProtoBytes( | |
135 meta_entry.Get(syncable::BOOKMARK_FAVICON), | |
136 bookmark->mutable_bookmark_favicon()); | |
137 } | |
138 } | |
139 } | |
140 session->set_commit_message(message); | |
141 } | |
142 | |
143 } // namespace browser_sync | |
OLD | NEW |