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

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

Issue 17052007: sync: Expose sync functionality as functions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Minor changes to #includes Created 7 years, 5 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 ConvertConfigureSourceToOrigin(
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 default:
65 NOTREACHED();
66 return sync_pb::SyncEnums::UNKNOWN_ORIGIN;
67 }
68 }
69
70 bool ShouldRequestEncryptionKey(
71 SyncSessionContext* context) {
72 bool need_encryption_key = false;
73 if (context->keystore_encryption_enabled()) {
74 syncable::Directory* dir = context->directory();
75 syncable::ReadTransaction trans(FROM_HERE, dir);
76 syncable::NigoriHandler* nigori_handler = dir->GetNigoriHandler();
77 need_encryption_key = nigori_handler->NeedKeystoreKey(&trans);
78 }
79 return need_encryption_key;
80 }
81
82 SyncerError ExecuteDownloadUpdates(
83 SyncSession* session,
84 sync_pb::ClientToServerMessage* msg) {
85 sync_pb::ClientToServerResponse update_response;
86 StatusController* status = session->mutable_status_controller();
87 bool need_encryption_key = ShouldRequestEncryptionKey(session->context());
88
89 SyncerError result = SyncerProtoUtil::PostClientToServerMessage(
90 msg,
91 &update_response,
92 session);
93
94 DVLOG(2) << SyncerProtoUtil::ClientToServerResponseDebugString(
95 update_response);
96
97 if (result != SYNCER_OK) {
98 status->mutable_updates_response()->Clear();
99 LOG(ERROR) << "PostClientToServerMessage() failed during GetUpdates";
100 } else {
101 status->mutable_updates_response()->CopyFrom(update_response);
102
103 DVLOG(1) << "GetUpdates "
104 << " returned " << update_response.get_updates().entries_size()
105 << " updates and indicated "
106 << update_response.get_updates().changes_remaining()
107 << " updates left on server.";
108
109 if (need_encryption_key ||
110 update_response.get_updates().encryption_keys_size() > 0) {
111 syncable::Directory* dir = session->context()->directory();
112 status->set_last_get_key_result(
113 HandleGetEncryptionKeyResponse(update_response, dir));
114 }
115 }
116
117 ProcessUpdatesCommand process_updates;
118 process_updates.Execute(session);
119
120 StoreTimestampsCommand store_timestamps;
121 store_timestamps.Execute(session);
122
123 return result;
124 }
125
126 void InitDownloadUpdatesRequest(
127 SyncSession* session,
128 bool create_mobile_bookmarks_folder,
129 sync_pb::ClientToServerMessage* message) {
130 message->set_share(session->context()->account_name());
131 message->set_message_contents(sync_pb::ClientToServerMessage::GET_UPDATES);
132
133 sync_pb::GetUpdatesMessage* get_updates = message->mutable_get_updates();
134
135 // We want folders for our associated types, always. If we were to set
136 // this to false, the server would send just the non-container items
137 // (e.g. Bookmark URLs but not their containing folders).
138 get_updates->set_fetch_folders(true);
139
140 DebugInfo* debug_info = message->mutable_debug_info();
141 AppendClientDebugInfoIfNeeded(session, debug_info);
142
143 get_updates->set_create_mobile_bookmarks_folder(
144 create_mobile_bookmarks_folder);
145 bool need_encryption_key = ShouldRequestEncryptionKey(session->context());
146 get_updates->set_need_encryption_key(need_encryption_key);
147 }
148
149 } // namespace
150
151 SyncerError NormalDownloadUpdates(
152 SyncSession* session,
153 bool create_mobile_bookmarks_folder,
154 ModelTypeSet request_types,
155 const sessions::NudgeTracker& nudge_tracker) {
156 sync_pb::ClientToServerMessage client_to_server_message;
157 InitDownloadUpdatesRequest(
158 session,
159 create_mobile_bookmarks_folder,
160 &client_to_server_message);
161 sync_pb::GetUpdatesMessage* get_updates =
162 client_to_server_message.mutable_get_updates();
163
164 // Request updates for all requested types.
165 DVLOG(1) << "Getting updates for types "
166 << ModelTypeSetToString(request_types);
167 DCHECK(!request_types.Empty());
168
169 // Set legacy GetUpdatesMessage.GetUpdatesCallerInfo information.
170 get_updates->mutable_caller_info()->set_source(
171 nudge_tracker.updates_source());
172 get_updates->mutable_caller_info()->set_notifications_enabled(
173 session->context()->notifications_enabled());
174
175 // Set the new and improved version of source, too.
176 get_updates->set_get_updates_origin(sync_pb::SyncEnums::GU_TRIGGER);
177
178 // Set the progress markers and notification hints.
179 syncable::Directory* dir = session->context()->directory();
180 for (ModelTypeSet::Iterator it = request_types.First();
181 it.Good(); it.Inc()) {
182 if (ProxyTypes().Has(it.Get()))
183 continue;
184
185 DCHECK(!nudge_tracker.IsTypeThrottled(it.Get()))
186 << "Throttled types should have been removed from the request_types.";
187
188 sync_pb::DataTypeProgressMarker* progress_marker =
189 get_updates->add_from_progress_marker();
190 dir->GetDownloadProgress(it.Get(), progress_marker);
191 nudge_tracker.SetLegacyNotificationHint(it.Get(), progress_marker);
192 nudge_tracker.FillProtoMessage(
193 it.Get(),
194 progress_marker->mutable_get_update_triggers());
195 }
196
197 StatusController* status = session->mutable_status_controller();
198 status->set_updates_request_types(request_types);
tim (not reviewing) 2013/07/02 21:01:53 Can these two lines (get &status and set_request_t
rlarocque 2013/07/02 22:20:24 Done.
199 return ExecuteDownloadUpdates(session, &client_to_server_message);
200 }
201
202 SyncerError DownloadUpdatesForConfigure(
203 SyncSession* session,
204 bool create_mobile_bookmarks_folder,
205 const syncer::sessions::SyncSourceInfo& source,
206 ModelTypeSet request_types) {
207 sync_pb::ClientToServerMessage client_to_server_message;
208 InitDownloadUpdatesRequest(
209 session,
210 create_mobile_bookmarks_folder,
211 &client_to_server_message);
212 sync_pb::GetUpdatesMessage* get_updates =
213 client_to_server_message.mutable_get_updates();
214
215 // Request updates for all enabled types.
216 DVLOG(1) << "Initial download for types "
217 << ModelTypeSetToString(request_types);
218 DCHECK(!request_types.Empty());
219
220 syncable::Directory* dir = session->context()->directory();
221 for (ModelTypeSet::Iterator it = request_types.First();
222 it.Good(); it.Inc()) {
223 if (ProxyTypes().Has(it.Get()))
224 continue;
225
226 sync_pb::DataTypeProgressMarker* progress_marker =
227 get_updates->add_from_progress_marker();
228 dir->GetDownloadProgress(it.Get(), progress_marker);
229 }
230
231 // Set legacy GetUpdatesMessage.GetUpdatesCallerInfo information.
232 get_updates->mutable_caller_info()->set_source(source.updates_source);
233 get_updates->mutable_caller_info()->set_notifications_enabled(
234 session->context()->notifications_enabled());
235
236 // Set the new and improved version of source, too.
237 sync_pb::SyncEnums::GetUpdatesOrigin origin =
238 ConvertConfigureSourceToOrigin(source.updates_source);
239 get_updates->set_get_updates_origin(origin);
240
241 StatusController* status = session->mutable_status_controller();
242 status->set_updates_request_types(request_types);
243 return ExecuteDownloadUpdates(session, &client_to_server_message);
244 }
245
246 SyncerError DownloadUpdatesForPoll(
247 SyncSession* session,
248 bool create_mobile_bookmarks_folder,
249 ModelTypeSet request_types) {
250 sync_pb::ClientToServerMessage client_to_server_message;
251 InitDownloadUpdatesRequest(
252 session,
253 create_mobile_bookmarks_folder,
254 &client_to_server_message);
255 sync_pb::GetUpdatesMessage* get_updates =
256 client_to_server_message.mutable_get_updates();
257
258 DVLOG(1) << "Polling for types "
259 << ModelTypeSetToString(request_types);
260 DCHECK(!request_types.Empty());
261
262 syncable::Directory* dir = session->context()->directory();
263 for (ModelTypeSet::Iterator it = request_types.First();
264 it.Good(); it.Inc()) {
265 if (ProxyTypes().Has(it.Get()))
266 continue;
267
268 sync_pb::DataTypeProgressMarker* progress_marker =
269 get_updates->add_from_progress_marker();
270 dir->GetDownloadProgress(it.Get(), progress_marker);
tim (not reviewing) 2013/07/02 21:01:53 All Download* variants do the same thing for GetDo
rlarocque 2013/07/02 22:20:24 Done.
271 }
272
273 DCHECK_EQ(sync_pb::GetUpdatesCallerInfo::PERIODIC,
274 session->source().updates_source);
275
276 // Set legacy GetUpdatesMessage.GetUpdatesCallerInfo information.
277 get_updates->mutable_caller_info()->set_source(
278 sync_pb::GetUpdatesCallerInfo::PERIODIC);
279 get_updates->mutable_caller_info()->set_notifications_enabled(
tim (not reviewing) 2013/07/02 21:01:53 Can this (set_notifications_enabled) go in to Init
rlarocque 2013/07/02 22:20:24 Done.
280 session->context()->notifications_enabled());
281
282 // Set the new and improved version of source, too.
283 get_updates->set_get_updates_origin(sync_pb::SyncEnums::PERIODIC);
284
285 StatusController* status = session->mutable_status_controller();
286 status->set_updates_request_types(request_types);
287 return ExecuteDownloadUpdates(session, &client_to_server_message);
288 }
289
290 void AppendClientDebugInfoIfNeeded(
291 SyncSession* session,
292 DebugInfo* debug_info) {
293 // We want to send the debug info only once per sync cycle. Check if it has
294 // already been sent.
295 if (!session->status_controller().debug_info_sent()) {
296 DVLOG(1) << "Sending client debug info ...";
297 // could be null in some unit tests.
298 if (session->context()->debug_info_getter()) {
299 session->context()->debug_info_getter()->GetAndClearDebugInfo(
300 debug_info);
301 }
302 session->mutable_status_controller()->set_debug_info_sent();
303 }
304 }
305
306 } // 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