Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(102)

Side by Side Diff: sync/engine/download.cc

Issue 15764010: Experimental functionize patch (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sync: Expose sync functionality as functions Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sync/engine/download.h ('k') | sync/engine/download_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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.h"
6
7 #include <string>
8
9 #include "base/command_line.h"
10 #include "sync/engine/process_updates_command.h"
11 #include "sync/engine/store_timestamps_command.h"
12 #include "sync/engine/syncer.h"
13 #include "sync/engine/syncer_proto_util.h"
14 #include "sync/internal_api/public/base/model_type_invalidation_map.h"
15 #include "sync/sessions/nudge_tracker.h"
16 #include "sync/syncable/directory.h"
17 #include "sync/syncable/nigori_handler.h"
18 #include "sync/syncable/syncable_read_transaction.h"
19
20 using sync_pb::DebugInfo;
21
22 namespace syncer {
23
24 using sessions::StatusController;
25 using sessions::SyncSession;
26 using sessions::SyncSessionContext;
27 using std::string;
28
29 namespace {
30
31 SyncerError HandleGetEncryptionKeyResponse(
32 const sync_pb::ClientToServerResponse& update_response,
33 syncable::Directory* dir) {
34 bool success = false;
35 if (update_response.get_updates().encryption_keys_size() == 0) {
36 LOG(ERROR) << "Failed to receive encryption key from server.";
37 return SERVER_RESPONSE_VALIDATION_FAILED;
38 }
39 syncable::ReadTransaction trans(FROM_HERE, dir);
40 syncable::NigoriHandler* nigori_handler = dir->GetNigoriHandler();
41 success = nigori_handler->SetKeystoreKeys(
42 update_response.get_updates().encryption_keys(),
43 &trans);
44
45 DVLOG(1) << "GetUpdates returned "
46 << update_response.get_updates().encryption_keys_size()
47 << "encryption keys. Nigori keystore key "
48 << (success ? "" : "not ") << "updated.";
49 return (success ? SYNCER_OK : SERVER_RESPONSE_VALIDATION_FAILED);
50 }
51
52 sync_pb::SyncEnums::GetUpdatesOrigin ConvertGetUpdateSourceToOrigin(
53 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source) {
54 switch (source) {
55 // Configurations:
56 case sync_pb::GetUpdatesCallerInfo::NEWLY_SUPPORTED_DATATYPE:
57 return sync_pb::SyncEnums::NEWLY_SUPPORTED_DATATYPE;
58 case sync_pb::GetUpdatesCallerInfo::MIGRATION:
59 return sync_pb::SyncEnums::MIGRATION;
60 case sync_pb::GetUpdatesCallerInfo::RECONFIGURATION:
61 return sync_pb::SyncEnums::RECONFIGURATION;
62 case sync_pb::GetUpdatesCallerInfo::NEW_CLIENT:
63 return sync_pb::SyncEnums::NEW_CLIENT;
64
65 // Poll, which never overlaps with anything else:
66 case sync_pb::GetUpdatesCallerInfo::PERIODIC:
67 return sync_pb::SyncEnums::PERIODIC;
68
69 // Overlapping normal-mode sources (fall-through is intentional):
70 case sync_pb::GetUpdatesCallerInfo::LOCAL:
71 case sync_pb::GetUpdatesCallerInfo::NOTIFICATION:
72 case sync_pb::GetUpdatesCallerInfo::DATATYPE_REFRESH:
73 return sync_pb::SyncEnums::GU_TRIGGER;
74
75 // Deprecated or invalid (fall-through is intentional):
76 case sync_pb::GetUpdatesCallerInfo::UNKNOWN:
77 case sync_pb::GetUpdatesCallerInfo::FIRST_UPDATE:
78 case sync_pb::GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION:
79 NOTREACHED() << "Invalid source: " << source;
80 return sync_pb::SyncEnums::UNKNOWN_ORIGIN;
81 }
82 NOTREACHED();
83 return sync_pb::SyncEnums::UNKNOWN_ORIGIN;
84 }
85
86 void InitDownloadUpdatesRequest(
87 SyncSessionContext* context,
88 sync_pb::ClientToServerMessage* msg) {
89 msg->set_share(context->account_name());
90 msg->set_message_contents(sync_pb::ClientToServerMessage::GET_UPDATES);
91
92 // We want folders for our associated types, always. If we were to set
93 // this to false, the server would send just the non-container items
94 // (e.g. Bookmark URLs but not their containing folders).
95 sync_pb::GetUpdatesMessage* get_updates = msg->mutable_get_updates();
96 get_updates->set_fetch_folders(true);
97 }
98
99 bool ShouldRequestEncryptionKey(
100 SyncSessionContext* context) {
101 bool need_encryption_key = false;
102 if (context->keystore_encryption_enabled()) {
103 syncable::Directory* dir = context->directory();
104 syncable::ReadTransaction trans(FROM_HERE, dir);
105 syncable::NigoriHandler* nigori_handler = dir->GetNigoriHandler();
106 need_encryption_key = nigori_handler->NeedKeystoreKey(&trans);
107 }
108 return need_encryption_key;
109 }
110
111 SyncerError ExecuteDownloadUpdates(
112 bool need_encryption_key,
113 SyncSession* session,
114 sync_pb::ClientToServerMessage* msg) {
115 sync_pb::ClientToServerResponse update_response;
116 StatusController* status = session->mutable_status_controller();
117
118 SyncerError result = SyncerProtoUtil::PostClientToServerMessage(
119 msg,
120 &update_response,
121 session);
122
123 DVLOG(2) << SyncerProtoUtil::ClientToServerResponseDebugString(
124 update_response);
125
126 if (result != SYNCER_OK) {
127 status->mutable_updates_response()->Clear();
128 LOG(ERROR) << "PostClientToServerMessage() failed during GetUpdates";
129 return result;
130 }
131
132 status->mutable_updates_response()->CopyFrom(update_response);
133
134 DVLOG(1) << "GetUpdates "
135 << " returned " << update_response.get_updates().entries_size()
136 << " updates and indicated "
137 << update_response.get_updates().changes_remaining()
138 << " updates left on server.";
139
140 if (need_encryption_key ||
141 update_response.get_updates().encryption_keys_size() > 0) {
142 syncable::Directory* dir = session->context()->directory();
143 status->set_last_get_key_result(
144 HandleGetEncryptionKeyResponse(update_response, dir));
145 }
146
147 ProcessUpdatesCommand process_updates;
148 process_updates.Execute(session);
149
150 StoreTimestampsCommand store_timestamps;
151 store_timestamps.Execute(session);
152
153 return result;
154 }
155
156 } // namespace
157
158 SyncerError NormalDownloadUpdates(
159 SyncSession* session,
160 bool create_mobile_bookmarks_folder,
161 ModelTypeSet request_types,
162 const sessions::NudgeTracker& nudge_tracker) {
163 sync_pb::ClientToServerMessage client_to_server_message;
164 InitDownloadUpdatesRequest(session->context(),
165 &client_to_server_message);
166
167 DebugInfo* debug_info = client_to_server_message.mutable_debug_info();
168 AppendClientDebugInfoIfNeeded(session, debug_info);
169
170 sync_pb::GetUpdatesMessage* get_updates =
171 client_to_server_message.mutable_get_updates();
172 get_updates->set_create_mobile_bookmarks_folder(
173 create_mobile_bookmarks_folder);
174 bool need_encryption_key = ShouldRequestEncryptionKey(session->context());
175 get_updates->set_need_encryption_key(need_encryption_key);
176
177 // Request updates for all requested types.
178 DVLOG(1) << "Getting updates for types "
179 << ModelTypeSetToString(request_types);
180 DCHECK(!request_types.Empty());
181
182 // Set legacy GetUpdatesMessage.GetUpdatesCallerInfo information.
183 get_updates->mutable_caller_info()->set_source(
184 nudge_tracker.updates_source());
185 get_updates->mutable_caller_info()->set_notifications_enabled(
186 session->context()->notifications_enabled());
187
188 // Set the new and improved version of source, too.
189 sync_pb::SyncEnums::GetUpdatesOrigin origin =
190 ConvertGetUpdateSourceToOrigin(nudge_tracker.updates_source());
191 get_updates->set_get_updates_origin(origin);
192 DCHECK_EQ(sync_pb::SyncEnums::GU_TRIGGER, origin);
193
194 // Set the progress markers and notification hints.
195 syncable::Directory* dir = session->context()->directory();
196 for (ModelTypeSet::Iterator it = request_types.First();
197 it.Good(); it.Inc()) {
198 if (ProxyTypes().Has(it.Get()))
199 continue;
200
201 DCHECK(!nudge_tracker.IsTypeThrottled(it.Get()))
202 << "Throttled types should have been removed from the request_types.";
203
204 sync_pb::DataTypeProgressMarker* progress_marker =
205 get_updates->add_from_progress_marker();
206 dir->GetDownloadProgress(it.Get(), progress_marker);
207 nudge_tracker.SetLegacyNotificationHint(it.Get(), progress_marker);
208 nudge_tracker.FillProtoMessage(
209 it.Get(),
210 progress_marker->mutable_get_update_triggers());
211 }
212
213 StatusController* status = session->mutable_status_controller();
214 status->set_updates_request_types(request_types);
215 return ExecuteDownloadUpdates(
216 need_encryption_key,
217 session,
218 &client_to_server_message);
219 }
220
221 SyncerError ConfigureDownloadUpdates(
222 SyncSession* session,
223 bool create_mobile_bookmarks_folder,
224 const syncer::sessions::SyncSourceInfo& source,
225 ModelTypeSet request_types) {
226 sync_pb::ClientToServerMessage client_to_server_message;
227 InitDownloadUpdatesRequest(session->context(),
228 &client_to_server_message);
229
230 DebugInfo* debug_info = client_to_server_message.mutable_debug_info();
231 AppendClientDebugInfoIfNeeded(session, debug_info);
232
233 sync_pb::GetUpdatesMessage* get_updates =
234 client_to_server_message.mutable_get_updates();
235 get_updates->set_create_mobile_bookmarks_folder(
236 create_mobile_bookmarks_folder);
237 bool need_encryption_key = ShouldRequestEncryptionKey(session->context());
238 get_updates->set_need_encryption_key(need_encryption_key);
239
240 // Request updates for all enabled types.
241 DVLOG(1) << "Initial download for types "
242 << ModelTypeSetToString(request_types);
243 DCHECK(!request_types.Empty());
244
245 syncable::Directory* dir = session->context()->directory();
246 for (ModelTypeSet::Iterator it = request_types.First();
247 it.Good(); it.Inc()) {
248 if (ProxyTypes().Has(it.Get()))
249 continue;
250
251 sync_pb::DataTypeProgressMarker* progress_marker =
252 get_updates->add_from_progress_marker();
253 dir->GetDownloadProgress(it.Get(), progress_marker);
254 }
255
256 // Set legacy GetUpdatesMessage.GetUpdatesCallerInfo information.
257 get_updates->mutable_caller_info()->set_source(source.updates_source);
258 get_updates->mutable_caller_info()->set_notifications_enabled(
259 session->context()->notifications_enabled());
260
261 // Set the new and improved version of source, too.
262 sync_pb::SyncEnums::GetUpdatesOrigin origin =
263 ConvertGetUpdateSourceToOrigin(source.updates_source);
264 get_updates->set_get_updates_origin(origin);
265
266 StatusController* status = session->mutable_status_controller();
267 status->set_updates_request_types(request_types);
268 return ExecuteDownloadUpdates(
269 need_encryption_key,
270 session,
271 &client_to_server_message);
272 }
273
274 SyncerError PollDownloadUpdates(
275 SyncSession* session,
276 bool create_mobile_bookmarks_folder,
277 ModelTypeSet request_types) {
278 sync_pb::ClientToServerMessage client_to_server_message;
279 InitDownloadUpdatesRequest(session->context(),
280 &client_to_server_message);
281
282 DebugInfo* debug_info = client_to_server_message.mutable_debug_info();
283 AppendClientDebugInfoIfNeeded(session, debug_info);
284
285 sync_pb::GetUpdatesMessage* get_updates =
286 client_to_server_message.mutable_get_updates();
287 get_updates->set_create_mobile_bookmarks_folder(
288 create_mobile_bookmarks_folder);
289 bool need_encryption_key = ShouldRequestEncryptionKey(session->context());
290 get_updates->set_need_encryption_key(need_encryption_key);
291
292 DVLOG(1) << "Polling for types "
293 << ModelTypeSetToString(request_types);
294 DCHECK(!request_types.Empty());
295
296 syncable::Directory* dir = session->context()->directory();
297 for (ModelTypeSet::Iterator it = request_types.First();
298 it.Good(); it.Inc()) {
299 if (ProxyTypes().Has(it.Get()))
300 continue;
301
302 sync_pb::DataTypeProgressMarker* progress_marker =
303 get_updates->add_from_progress_marker();
304 dir->GetDownloadProgress(it.Get(), progress_marker);
305 }
306
307 DCHECK_EQ(sync_pb::GetUpdatesCallerInfo::PERIODIC,
308 session->source().updates_source);
309
310 // Set legacy GetUpdatesMessage.GetUpdatesCallerInfo information.
311 get_updates->mutable_caller_info()->set_source(
312 sync_pb::GetUpdatesCallerInfo::PERIODIC);
313 get_updates->mutable_caller_info()->set_notifications_enabled(
314 session->context()->notifications_enabled());
315
316 // Set the new and improved version of source, too.
317 get_updates->set_get_updates_origin(sync_pb::SyncEnums::PERIODIC);
318
319 StatusController* status = session->mutable_status_controller();
320 status->set_updates_request_types(request_types);
321 return ExecuteDownloadUpdates(
322 need_encryption_key,
323 session,
324 &client_to_server_message);
325 }
326
327 void AppendClientDebugInfoIfNeeded(
328 SyncSession* session,
329 DebugInfo* debug_info) {
330 // We want to send the debug info only once per sync cycle. Check if it has
331 // already been sent.
332 if (!session->status_controller().debug_info_sent()) {
333 DVLOG(1) << "Sending client debug info ...";
334 // could be null in some unit tests.
335 if (session->context()->debug_info_getter()) {
336 session->context()->debug_info_getter()->GetAndClearDebugInfo(
337 debug_info);
338 }
339 session->mutable_status_controller()->set_debug_info_sent();
340 }
341 }
342
343 } // namespace syncer
OLDNEW
« no previous file with comments | « sync/engine/download.h ('k') | sync/engine/download_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698