OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sync/engine/build_commit_command.h" | 5 #include "sync/engine/build_commit_command.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "sync/engine/syncer_proto_util.h" | 13 #include "sync/engine/syncer_proto_util.h" |
14 #include "sync/internal_api/public/base/unique_position.h" | 14 #include "sync/internal_api/public/base/unique_position.h" |
15 #include "sync/protocol/bookmark_specifics.pb.h" | 15 #include "sync/protocol/bookmark_specifics.pb.h" |
16 #include "sync/protocol/sync.pb.h" | 16 #include "sync/protocol/sync.pb.h" |
17 #include "sync/sessions/ordered_commit_set.h" | |
18 #include "sync/sessions/sync_session.h" | 17 #include "sync/sessions/sync_session.h" |
19 #include "sync/syncable/directory.h" | 18 #include "sync/syncable/directory.h" |
20 #include "sync/syncable/entry.h" | 19 #include "sync/syncable/entry.h" |
21 #include "sync/syncable/syncable_base_transaction.h" | 20 #include "sync/syncable/syncable_base_transaction.h" |
22 #include "sync/syncable/syncable_changes_version.h" | 21 #include "sync/syncable/syncable_changes_version.h" |
23 #include "sync/syncable/syncable_proto_util.h" | 22 #include "sync/syncable/syncable_proto_util.h" |
24 #include "sync/util/time.h" | 23 #include "sync/util/time.h" |
25 | 24 |
26 using std::set; | 25 using std::set; |
27 using std::string; | 26 using std::string; |
28 using std::vector; | 27 using std::vector; |
29 | 28 |
30 namespace syncer { | 29 namespace syncer { |
31 | 30 |
32 using sessions::SyncSession; | 31 using sessions::SyncSession; |
33 using syncable::Entry; | 32 using syncable::Entry; |
34 using syncable::IS_DEL; | 33 using syncable::IS_DEL; |
35 using syncable::IS_UNAPPLIED_UPDATE; | 34 using syncable::IS_UNAPPLIED_UPDATE; |
36 using syncable::IS_UNSYNCED; | 35 using syncable::IS_UNSYNCED; |
37 using syncable::Id; | 36 using syncable::Id; |
38 using syncable::SPECIFICS; | 37 using syncable::SPECIFICS; |
39 using syncable::UNIQUE_POSITION; | 38 using syncable::UNIQUE_POSITION; |
40 | 39 |
41 BuildCommitCommand::BuildCommitCommand( | 40 void BuildCommitCommand::AddExtensionsActivityToMessage( |
42 syncable::BaseTransaction* trans, | 41 ExtensionsActivity* activity, |
43 const sessions::OrderedCommitSet& batch_commit_set, | 42 ExtensionsActivity::Records* extensions_activity_buffer, |
44 sync_pb::ClientToServerMessage* commit_message, | 43 sync_pb::CommitMessage* message) { |
45 ExtensionsActivity::Records* extensions_activity_buffer) | 44 // This isn't perfect, since the set of extensions activity may not correlate |
46 : trans_(trans), | 45 // exactly with the items being committed. That's OK as long as we're looking |
47 batch_commit_set_(batch_commit_set), | 46 // for a rough estimate of extensions activity, not an precise mapping of |
48 commit_message_(commit_message), | 47 // which commits were triggered by which extension. |
49 extensions_activity_buffer_(extensions_activity_buffer) { | 48 // |
50 } | 49 // We will push this list of extensions activity back into the |
| 50 // ExtensionsActivityMonitor if this commit fails. That's why we must keep a |
| 51 // copy of these records in the session. |
| 52 activity->GetAndClearRecords(extensions_activity_buffer); |
51 | 53 |
52 BuildCommitCommand::~BuildCommitCommand() {} | 54 const ExtensionsActivity::Records& records = *extensions_activity_buffer; |
53 | 55 for (ExtensionsActivity::Records::const_iterator it = |
54 void BuildCommitCommand::AddExtensionsActivityToMessage( | 56 records.begin(); |
55 SyncSession* session, sync_pb::CommitMessage* message) { | 57 it != records.end(); ++it) { |
56 // We only send ExtensionsActivity to the server if bookmarks are being | 58 sync_pb::ChromiumExtensionsActivity* activity_message = |
57 // committed. | 59 message->add_extensions_activity(); |
58 ExtensionsActivity* activity = session->context()->extensions_activity(); | 60 activity_message->set_extension_id(it->second.extension_id); |
59 if (batch_commit_set_.HasBookmarkCommitId()) { | 61 activity_message->set_bookmark_writes_since_last_commit( |
60 // This isn't perfect, since the set of extensions activity may not | 62 it->second.bookmark_write_count); |
61 // correlate exactly with the items being committed. That's OK as | |
62 // long as we're looking for a rough estimate of extensions activity, | |
63 // not an precise mapping of which commits were triggered by which | |
64 // extension. | |
65 // | |
66 // We will push this list of extensions activity back into the | |
67 // ExtensionsActivityMonitor if this commit fails. That's why we must keep | |
68 // a copy of these records in the session. | |
69 activity->GetAndClearRecords(extensions_activity_buffer_); | |
70 | |
71 const ExtensionsActivity::Records& records = | |
72 *extensions_activity_buffer_; | |
73 for (ExtensionsActivity::Records::const_iterator it = | |
74 records.begin(); | |
75 it != records.end(); ++it) { | |
76 sync_pb::ChromiumExtensionsActivity* activity_message = | |
77 message->add_extensions_activity(); | |
78 activity_message->set_extension_id(it->second.extension_id); | |
79 activity_message->set_bookmark_writes_since_last_commit( | |
80 it->second.bookmark_write_count); | |
81 } | |
82 } | 63 } |
83 } | 64 } |
84 | 65 |
85 void BuildCommitCommand::AddClientConfigParamsToMessage( | 66 void BuildCommitCommand::AddClientConfigParamsToMessage( |
86 SyncSession* session, sync_pb::CommitMessage* message) { | 67 ModelTypeSet enabled_types, |
87 const ModelSafeRoutingInfo& routing_info = session->context()->routing_info(); | 68 sync_pb::CommitMessage* message) { |
88 sync_pb::ClientConfigParams* config_params = message->mutable_config_params(); | 69 sync_pb::ClientConfigParams* config_params = message->mutable_config_params(); |
89 for (std::map<ModelType, ModelSafeGroup>::const_iterator iter = | 70 for (ModelTypeSet::Iterator it = enabled_types.First(); it.Good(); it.Inc()) { |
90 routing_info.begin(); iter != routing_info.end(); ++iter) { | 71 if (ProxyTypes().Has(it.Get())) |
91 if (ProxyTypes().Has(iter->first)) | |
92 continue; | 72 continue; |
93 int field_number = GetSpecificsFieldNumberFromModelType(iter->first); | 73 int field_number = GetSpecificsFieldNumberFromModelType(it.Get()); |
94 config_params->mutable_enabled_type_ids()->Add(field_number); | 74 config_params->mutable_enabled_type_ids()->Add(field_number); |
95 } | 75 } |
96 config_params->set_tabs_datatype_enabled( | 76 config_params->set_tabs_datatype_enabled( |
97 routing_info.count(syncer::PROXY_TABS) > 0); | 77 enabled_types.Has(syncer::PROXY_TABS)); |
98 } | 78 } |
99 | 79 |
100 namespace { | 80 namespace { |
101 void SetEntrySpecifics(const Entry& meta_entry, | 81 void SetEntrySpecifics(const Entry& meta_entry, |
102 sync_pb::SyncEntity* sync_entry) { | 82 sync_pb::SyncEntity* sync_entry) { |
103 // Add the new style extension and the folder bit. | 83 // Add the new style extension and the folder bit. |
104 sync_entry->mutable_specifics()->CopyFrom(meta_entry.GetSpecifics()); | 84 sync_entry->mutable_specifics()->CopyFrom(meta_entry.GetSpecifics()); |
105 sync_entry->set_folder(meta_entry.GetIsDir()); | 85 sync_entry->set_folder(meta_entry.GetIsDir()); |
106 | 86 |
107 CHECK(!sync_entry->specifics().password().has_client_only_encrypted_data()); | 87 CHECK(!sync_entry->specifics().password().has_client_only_encrypted_data()); |
108 DCHECK_EQ(meta_entry.GetModelType(), GetModelType(*sync_entry)); | 88 DCHECK_EQ(meta_entry.GetModelType(), GetModelType(*sync_entry)); |
109 } | 89 } |
110 } // namespace | 90 } // namespace |
111 | 91 |
112 SyncerError BuildCommitCommand::ExecuteImpl(SyncSession* session) { | |
113 commit_message_->set_share(session->context()->account_name()); | |
114 commit_message_->set_message_contents(sync_pb::ClientToServerMessage::COMMIT); | |
115 | |
116 sync_pb::CommitMessage* commit_message = commit_message_->mutable_commit(); | |
117 commit_message->set_cache_guid(trans_->directory()->cache_guid()); | |
118 AddExtensionsActivityToMessage(session, commit_message); | |
119 AddClientConfigParamsToMessage(session, commit_message); | |
120 | |
121 for (size_t i = 0; i < batch_commit_set_.Size(); i++) { | |
122 int64 handle = batch_commit_set_.GetCommitHandleAt(i); | |
123 sync_pb::SyncEntity* sync_entry = commit_message->add_entries(); | |
124 | |
125 Entry meta_entry(trans_, syncable::GET_BY_HANDLE, handle); | |
126 CHECK(meta_entry.good()); | |
127 | |
128 DCHECK_NE(0UL, | |
129 session->context()->routing_info().count( | |
130 meta_entry.GetModelType())) | |
131 << "Committing change to datatype that's not actively enabled."; | |
132 | |
133 BuildCommitItem(meta_entry, sync_entry); | |
134 } | |
135 | |
136 | |
137 return SYNCER_OK; | |
138 } | |
139 | |
140 // static. | 92 // static. |
141 void BuildCommitCommand::BuildCommitItem( | 93 void BuildCommitCommand::BuildCommitItem( |
142 const syncable::Entry& meta_entry, | 94 const syncable::Entry& meta_entry, |
143 sync_pb::SyncEntity* sync_entry) { | 95 sync_pb::SyncEntity* sync_entry) { |
144 syncable::Id id = meta_entry.GetId(); | 96 syncable::Id id = meta_entry.GetId(); |
145 sync_entry->set_id_string(SyncableIdToProto(id)); | 97 sync_entry->set_id_string(SyncableIdToProto(id)); |
146 | 98 |
147 string name = meta_entry.GetNonUniqueName(); | 99 string name = meta_entry.GetNonUniqueName(); |
148 CHECK(!name.empty()); // Make sure this isn't an update. | 100 CHECK(!name.empty()); // Make sure this isn't an update. |
149 // Note: Truncation is also performed in WriteNode::SetTitle(..). But this | 101 // Note: Truncation is also performed in WriteNode::SetTitle(..). But this |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 sync_entry->set_position_in_parent( | 168 sync_entry->set_position_in_parent( |
217 meta_entry.GetUniquePosition().ToInt64()); | 169 meta_entry.GetUniquePosition().ToInt64()); |
218 meta_entry.GetUniquePosition().ToProto( | 170 meta_entry.GetUniquePosition().ToProto( |
219 sync_entry->mutable_unique_position()); | 171 sync_entry->mutable_unique_position()); |
220 } | 172 } |
221 SetEntrySpecifics(meta_entry, sync_entry); | 173 SetEntrySpecifics(meta_entry, sync_entry); |
222 } | 174 } |
223 } | 175 } |
224 | 176 |
225 } // namespace syncer | 177 } // namespace syncer |
OLD | NEW |