OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "sync/engine/download_updates_command.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/command_line.h" | |
10 #include "sync/engine/syncer.h" | |
11 #include "sync/engine/syncer_proto_util.h" | |
12 #include "sync/internal_api/public/base/model_type_invalidation_map.h" | |
13 #include "sync/sessions/nudge_tracker.h" | |
14 #include "sync/syncable/directory.h" | |
15 #include "sync/syncable/nigori_handler.h" | |
16 #include "sync/syncable/syncable_read_transaction.h" | |
17 | |
18 using sync_pb::DebugInfo; | |
19 | |
20 namespace syncer { | |
21 using sessions::StatusController; | |
22 using sessions::SyncSession; | |
23 using std::string; | |
24 | |
25 DownloadUpdatesCommand::DownloadUpdatesCommand( | |
26 bool create_mobile_bookmarks_folder) | |
27 : create_mobile_bookmarks_folder_(create_mobile_bookmarks_folder) {} | |
28 | |
29 DownloadUpdatesCommand::~DownloadUpdatesCommand() {} | |
30 | |
31 namespace { | |
32 | |
33 SyncerError HandleGetEncryptionKeyResponse( | |
34 const sync_pb::ClientToServerResponse& update_response, | |
35 syncable::Directory* dir) { | |
36 bool success = false; | |
37 if (update_response.get_updates().encryption_keys_size() == 0) { | |
38 LOG(ERROR) << "Failed to receive encryption key from server."; | |
39 return SERVER_RESPONSE_VALIDATION_FAILED; | |
40 } | |
41 syncable::ReadTransaction trans(FROM_HERE, dir); | |
42 syncable::NigoriHandler* nigori_handler = dir->GetNigoriHandler(); | |
43 success = nigori_handler->SetKeystoreKeys( | |
44 update_response.get_updates().encryption_keys(), | |
45 &trans); | |
46 | |
47 DVLOG(1) << "GetUpdates returned " | |
48 << update_response.get_updates().encryption_keys_size() | |
49 << "encryption keys. Nigori keystore key " | |
50 << (success ? "" : "not ") << "updated."; | |
51 return (success ? SYNCER_OK : SERVER_RESPONSE_VALIDATION_FAILED); | |
52 } | |
53 | |
54 sync_pb::SyncEnums::GetUpdatesOrigin ConvertGetUpdateSourceToOrigin( | |
55 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source) { | |
56 switch (source) { | |
57 // Configurations: | |
58 case sync_pb::GetUpdatesCallerInfo::NEWLY_SUPPORTED_DATATYPE: | |
59 return sync_pb::SyncEnums::NEWLY_SUPPORTED_DATATYPE; | |
60 case sync_pb::GetUpdatesCallerInfo::MIGRATION: | |
61 return sync_pb::SyncEnums::MIGRATION; | |
62 case sync_pb::GetUpdatesCallerInfo::RECONFIGURATION: | |
63 return sync_pb::SyncEnums::RECONFIGURATION; | |
64 case sync_pb::GetUpdatesCallerInfo::NEW_CLIENT: | |
65 return sync_pb::SyncEnums::NEW_CLIENT; | |
66 | |
67 // Poll, which never overlaps with anything else: | |
68 case sync_pb::GetUpdatesCallerInfo::PERIODIC: | |
69 return sync_pb::SyncEnums::PERIODIC; | |
70 | |
71 // Overlapping normal-mode sources (fall-through is intentional): | |
72 case sync_pb::GetUpdatesCallerInfo::LOCAL: | |
73 case sync_pb::GetUpdatesCallerInfo::NOTIFICATION: | |
74 case sync_pb::GetUpdatesCallerInfo::DATATYPE_REFRESH: | |
75 return sync_pb::SyncEnums::GU_TRIGGER; | |
76 | |
77 // Deprecated or invalid (fall-through is intentional): | |
78 case sync_pb::GetUpdatesCallerInfo::UNKNOWN: | |
79 case sync_pb::GetUpdatesCallerInfo::FIRST_UPDATE: | |
80 case sync_pb::GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION: | |
81 NOTREACHED() << "Invalid source: " << source; | |
82 return sync_pb::SyncEnums::UNKNOWN_ORIGIN; | |
83 } | |
84 NOTREACHED(); | |
85 return sync_pb::SyncEnums::UNKNOWN_ORIGIN; | |
86 } | |
87 | |
88 } // namespace | |
89 | |
90 SyncerError DownloadUpdatesCommand::ExecuteImpl(SyncSession* session) { | |
91 sync_pb::ClientToServerMessage client_to_server_message; | |
92 sync_pb::ClientToServerResponse update_response; | |
93 | |
94 client_to_server_message.set_share(session->context()->account_name()); | |
95 client_to_server_message.set_message_contents( | |
96 sync_pb::ClientToServerMessage::GET_UPDATES); | |
97 sync_pb::GetUpdatesMessage* get_updates = | |
98 client_to_server_message.mutable_get_updates(); | |
99 get_updates->set_create_mobile_bookmarks_folder( | |
100 create_mobile_bookmarks_folder_); | |
101 | |
102 sync_pb::SyncEnums::GetUpdatesOrigin origin = | |
103 ConvertGetUpdateSourceToOrigin(session->source().updates_source); | |
104 | |
105 syncable::Directory* dir = session->context()->directory(); | |
106 | |
107 // Request updates for all enabled types. | |
108 const ModelTypeSet enabled_types = | |
109 GetRoutingInfoTypes(session->context()->routing_info()); | |
110 DVLOG(1) << "Getting updates for types " | |
111 << ModelTypeSetToString(enabled_types); | |
112 DCHECK(!enabled_types.Empty()); | |
113 | |
114 const ModelTypeInvalidationMap& invalidation_map = | |
115 session->source().types; | |
116 for (ModelTypeSet::Iterator it = enabled_types.First(); | |
117 it.Good(); it.Inc()) { | |
118 if (ProxyTypes().Has(it.Get())) | |
119 continue; | |
120 | |
121 if (origin == sync_pb::SyncEnums::GU_TRIGGER) { | |
122 if (session->nudge_tracker()->IsTypeThrottled(it.Get())) { | |
123 continue; // Skip throttled types. | |
124 } | |
125 } else { | |
126 DCHECK(!session->nudge_tracker()); | |
127 } | |
128 | |
129 sync_pb::DataTypeProgressMarker* progress_marker = | |
130 get_updates->add_from_progress_marker(); | |
131 dir->GetDownloadProgress(it.Get(), progress_marker); | |
132 | |
133 // Set notification hint if present. | |
134 ModelTypeInvalidationMap::const_iterator find_it = | |
135 invalidation_map.find(it.Get()); | |
136 if (find_it != invalidation_map.end()) { | |
137 progress_marker->set_notification_hint(find_it->second.payload); | |
138 } | |
139 | |
140 if (origin == sync_pb::SyncEnums::GU_TRIGGER) { | |
141 session->nudge_tracker()->FillProtoMessage( | |
142 it.Get(), | |
143 progress_marker->mutable_get_update_triggers()); | |
144 } | |
145 } | |
146 | |
147 bool need_encryption_key = false; | |
148 if (session->context()->keystore_encryption_enabled()) { | |
149 syncable::Directory* dir = session->context()->directory(); | |
150 syncable::ReadTransaction trans(FROM_HERE, dir); | |
151 syncable::NigoriHandler* nigori_handler = dir->GetNigoriHandler(); | |
152 need_encryption_key = nigori_handler->NeedKeystoreKey(&trans); | |
153 get_updates->set_need_encryption_key(need_encryption_key); | |
154 | |
155 } | |
156 | |
157 // We want folders for our associated types, always. If we were to set | |
158 // this to false, the server would send just the non-container items | |
159 // (e.g. Bookmark URLs but not their containing folders). | |
160 get_updates->set_fetch_folders(true); | |
161 | |
162 // Set GetUpdatesMessage.GetUpdatesCallerInfo information. | |
163 get_updates->mutable_caller_info()->set_source( | |
164 session->source().updates_source); | |
165 get_updates->mutable_caller_info()->set_notifications_enabled( | |
166 session->context()->notifications_enabled()); | |
167 | |
168 // Set the new and improved version of source, too. | |
169 get_updates->set_get_updates_origin(origin); | |
170 | |
171 DebugInfo* debug_info = client_to_server_message.mutable_debug_info(); | |
172 | |
173 AppendClientDebugInfoIfNeeded(session, debug_info); | |
174 | |
175 SyncerError result = SyncerProtoUtil::PostClientToServerMessage( | |
176 &client_to_server_message, | |
177 &update_response, | |
178 session); | |
179 | |
180 DVLOG(2) << SyncerProtoUtil::ClientToServerResponseDebugString( | |
181 update_response); | |
182 | |
183 StatusController* status = session->mutable_status_controller(); | |
184 status->set_updates_request_types(enabled_types); | |
185 if (result != SYNCER_OK) { | |
186 status->mutable_updates_response()->Clear(); | |
187 LOG(ERROR) << "PostClientToServerMessage() failed during GetUpdates"; | |
188 return result; | |
189 } | |
190 | |
191 status->mutable_updates_response()->CopyFrom(update_response); | |
192 | |
193 DVLOG(1) << "GetUpdates " | |
194 << " returned " << update_response.get_updates().entries_size() | |
195 << " updates and indicated " | |
196 << update_response.get_updates().changes_remaining() | |
197 << " updates left on server."; | |
198 | |
199 if (need_encryption_key || | |
200 update_response.get_updates().encryption_keys_size() > 0) { | |
201 status->set_last_get_key_result( | |
202 HandleGetEncryptionKeyResponse(update_response, dir)); | |
203 } | |
204 | |
205 return result; | |
206 } | |
207 | |
208 void DownloadUpdatesCommand::AppendClientDebugInfoIfNeeded( | |
209 sessions::SyncSession* session, | |
210 DebugInfo* debug_info) { | |
211 // We want to send the debug info only once per sync cycle. Check if it has | |
212 // already been sent. | |
213 if (!session->status_controller().debug_info_sent()) { | |
214 DVLOG(1) << "Sending client debug info ..."; | |
215 // could be null in some unit tests. | |
216 if (session->context()->debug_info_getter()) { | |
217 session->context()->debug_info_getter()->GetAndClearDebugInfo( | |
218 debug_info); | |
219 } | |
220 session->mutable_status_controller()->set_debug_info_sent(); | |
221 } | |
222 } | |
223 | |
224 } // namespace syncer | |
OLD | NEW |