Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / sync / protocol / proto_value_conversions.cc
blobe7dddcd54711b052a452784f22d3e56d704c5587
1 // Copyright (c) 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.
5 // Keep this file in sync with the .proto files in this directory.
7 #include "sync/protocol/proto_value_conversions.h"
9 #include <string>
11 #include "base/base64.h"
12 #include "base/basictypes.h"
13 #include "base/i18n/time_formatting.h"
14 #include "base/logging.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/time/time.h"
17 #include "base/values.h"
18 #include "sync/internal_api/public/base/unique_position.h"
19 #include "sync/protocol/app_list_specifics.pb.h"
20 #include "sync/protocol/app_notification_specifics.pb.h"
21 #include "sync/protocol/app_setting_specifics.pb.h"
22 #include "sync/protocol/app_specifics.pb.h"
23 #include "sync/protocol/autofill_specifics.pb.h"
24 #include "sync/protocol/bookmark_specifics.pb.h"
25 #include "sync/protocol/dictionary_specifics.pb.h"
26 #include "sync/protocol/encryption.pb.h"
27 #include "sync/protocol/experiments_specifics.pb.h"
28 #include "sync/protocol/extension_setting_specifics.pb.h"
29 #include "sync/protocol/extension_specifics.pb.h"
30 #include "sync/protocol/favicon_image_specifics.pb.h"
31 #include "sync/protocol/favicon_tracking_specifics.pb.h"
32 #include "sync/protocol/history_delete_directive_specifics.pb.h"
33 #include "sync/protocol/nigori_specifics.pb.h"
34 #include "sync/protocol/password_specifics.pb.h"
35 #include "sync/protocol/preference_specifics.pb.h"
36 #include "sync/protocol/priority_preference_specifics.pb.h"
37 #include "sync/protocol/proto_enum_conversions.h"
38 #include "sync/protocol/search_engine_specifics.pb.h"
39 #include "sync/protocol/session_specifics.pb.h"
40 #include "sync/protocol/sync.pb.h"
41 #include "sync/protocol/theme_specifics.pb.h"
42 #include "sync/protocol/typed_url_specifics.pb.h"
43 #include "sync/protocol/unique_position.pb.h"
44 #include "sync/util/time.h"
46 namespace syncer {
48 namespace {
50 // Basic Type -> Value functions.
52 scoped_ptr<base::StringValue> MakeInt64Value(int64 x) {
53 return make_scoped_ptr(new base::StringValue(base::Int64ToString(x)));
56 // TODO(akalin): Perhaps make JSONWriter support BinaryValue and use
57 // that instead of a StringValue.
58 std::string Base64EncodeString(const std::string& bytes) {
59 std::string bytes_base64;
60 base::Base64Encode(bytes, &bytes_base64);
61 return bytes_base64;
64 scoped_ptr<base::StringValue> MakeStringValue(const std::string& str) {
65 return make_scoped_ptr(new base::StringValue(str));
68 // T is the field type, F is either RepeatedField or RepeatedPtrField,
69 // and V is a subclass of Value.
70 template <class T, class F, class V>
71 scoped_ptr<base::ListValue> MakeRepeatedValue(const F& fields,
72 V (*converter_fn)(T)) {
73 scoped_ptr<base::ListValue> list(new base::ListValue());
74 for (typename F::const_iterator it = fields.begin(); it != fields.end();
75 ++it) {
76 list->Append(converter_fn(*it));
78 return list;
81 base::string16 TimestampToString(int64 tm) {
82 return base::TimeFormatShortDateAndTime(syncer::ProtoTimeToTime(tm));
85 } // namespace
87 // Helper macros to reduce the amount of boilerplate.
89 #define SET_TYPE(field, set_fn, transform) \
90 if (proto.has_##field()) { \
91 value->set_fn(#field, transform(proto.field())); \
93 #define SET(field, fn) SET_TYPE(field, Set, fn)
94 #define SET_REP(field, fn) \
95 value->Set(#field, MakeRepeatedValue(proto.field(), fn))
96 #define SET_ENUM(field, fn) SET_TYPE(field, SetString, fn)
98 #define SET_BOOL(field) SET_TYPE(field, SetBoolean, )
99 #define SET_BYTES(field) SET_TYPE(field, SetString, Base64EncodeString)
100 #define SET_INT32(field) SET_TYPE(field, SetString, base::Int64ToString)
101 #define SET_INT32_REP(field) SET_REP(field, MakeInt64Value)
102 #define SET_INT64(field) SET_TYPE(field, SetString, base::Int64ToString)
103 #define SET_INT64_REP(field) SET_REP(field, MakeInt64Value)
104 #define SET_STR(field) SET_TYPE(field, SetString, )
105 #define SET_TIME_STR(field) SET_TYPE(field, SetString, TimestampToString)
106 #define SET_STR_REP(field) \
107 value->Set( \
108 #field, \
109 MakeRepeatedValue<const std::string&, \
110 google::protobuf::RepeatedPtrField<std::string>, \
111 scoped_ptr<base::StringValue>>(proto.field(), \
112 MakeStringValue))
113 #define SET_EXPERIMENT_ENABLED_FIELD(field) \
114 do { \
115 if (proto.has_##field() && \
116 proto.field().has_enabled()) { \
117 value->Set(#field, \
118 new base::FundamentalValue( \
119 proto.field().enabled())); \
121 } while (0)
123 #define SET_FIELD(field, fn) \
124 do { \
125 if (specifics.has_##field()) { \
126 value->Set(#field, fn(specifics.field())); \
128 } while (0)
130 // If you add another macro, don't forget to add an #undef at the end
131 // of this file, too.
133 scoped_ptr<base::DictionaryValue> EncryptedDataToValue(
134 const sync_pb::EncryptedData& proto) {
135 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
136 SET_STR(key_name);
137 // TODO(akalin): Shouldn't blob be of type bytes instead of string?
138 SET_BYTES(blob);
139 return value;
142 scoped_ptr<base::DictionaryValue> AppSettingsToValue(
143 const sync_pb::AppNotificationSettings& proto) {
144 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
145 SET_BOOL(initial_setup_done);
146 SET_BOOL(disabled);
147 SET_STR(oauth_client_id);
148 return value;
151 scoped_ptr<base::DictionaryValue> SessionHeaderToValue(
152 const sync_pb::SessionHeader& proto) {
153 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
154 SET_REP(window, SessionWindowToValue);
155 SET_STR(client_name);
156 SET_ENUM(device_type, GetDeviceTypeString);
157 return value;
160 scoped_ptr<base::DictionaryValue> SessionTabToValue(
161 const sync_pb::SessionTab& proto) {
162 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
163 SET_INT32(tab_id);
164 SET_INT32(window_id);
165 SET_INT32(tab_visual_index);
166 SET_INT32(current_navigation_index);
167 SET_BOOL(pinned);
168 SET_STR(extension_app_id);
169 SET_REP(navigation, TabNavigationToValue);
170 SET_BYTES(favicon);
171 SET_ENUM(favicon_type, GetFaviconTypeString);
172 SET_STR(favicon_source);
173 SET_REP(variation_id, MakeInt64Value);
174 return value;
177 scoped_ptr<base::DictionaryValue> SessionWindowToValue(
178 const sync_pb::SessionWindow& proto) {
179 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
180 SET_INT32(window_id);
181 SET_INT32(selected_tab_index);
182 SET_INT32_REP(tab);
183 SET_ENUM(browser_type, GetBrowserTypeString);
184 return value;
187 scoped_ptr<base::DictionaryValue> TabNavigationToValue(
188 const sync_pb::TabNavigation& proto) {
189 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
190 SET_STR(virtual_url);
191 SET_STR(referrer);
192 SET_STR(title);
193 SET_STR(state);
194 SET_ENUM(page_transition, GetPageTransitionString);
195 SET_ENUM(redirect_type, GetPageTransitionRedirectTypeString);
196 SET_INT32(unique_id);
197 SET_INT64(timestamp_msec);
198 SET_BOOL(navigation_forward_back);
199 SET_BOOL(navigation_from_address_bar);
200 SET_BOOL(navigation_home_page);
201 SET_BOOL(navigation_chain_start);
202 SET_BOOL(navigation_chain_end);
203 SET_INT64(global_id);
204 SET_STR(search_terms);
205 SET_STR(favicon_url);
206 SET_ENUM(blocked_state, GetBlockedStateString);
207 SET_STR_REP(content_pack_categories);
208 SET_INT32(http_status_code);
209 SET_INT32(obsolete_referrer_policy);
210 SET_BOOL(is_restored);
211 SET_REP(navigation_redirect, NavigationRedirectToValue);
212 SET_STR(last_navigation_redirect_url);
213 SET_INT32(correct_referrer_policy);
214 return value;
217 scoped_ptr<base::DictionaryValue> NavigationRedirectToValue(
218 const sync_pb::NavigationRedirect& proto) {
219 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
220 SET_STR(url);
221 return value;
224 scoped_ptr<base::DictionaryValue> PasswordSpecificsDataToValue(
225 const sync_pb::PasswordSpecificsData& proto) {
226 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
227 SET_INT32(scheme);
228 SET_STR(signon_realm);
229 SET_STR(origin);
230 SET_STR(action);
231 SET_STR(username_element);
232 SET_STR(username_value);
233 SET_STR(password_element);
234 value->SetString("password_value", "<redacted>");
235 SET_BOOL(ssl_valid);
236 SET_BOOL(preferred);
237 SET_INT64(date_created);
238 SET_BOOL(blacklisted);
239 SET_INT32(type);
240 SET_INT32(times_used);
241 SET_STR(display_name);
242 SET_STR(avatar_url);
243 SET_STR(federation_url);
244 return value;
247 scoped_ptr<base::DictionaryValue> GlobalIdDirectiveToValue(
248 const sync_pb::GlobalIdDirective& proto) {
249 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
250 SET_INT64_REP(global_id);
251 SET_INT64(start_time_usec);
252 SET_INT64(end_time_usec);
253 return value;
256 scoped_ptr<base::DictionaryValue> TimeRangeDirectiveToValue(
257 const sync_pb::TimeRangeDirective& proto) {
258 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
259 SET_INT64(start_time_usec);
260 SET_INT64(end_time_usec);
261 return value;
264 scoped_ptr<base::DictionaryValue> AppListSpecificsToValue(
265 const sync_pb::AppListSpecifics& proto) {
266 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
267 SET_STR(item_id);
268 SET_ENUM(item_type, GetAppListItemTypeString);
269 SET_STR(item_name);
270 SET_STR(parent_id);
271 SET_STR(item_ordinal);
273 return value;
276 scoped_ptr<base::DictionaryValue> AppNotificationToValue(
277 const sync_pb::AppNotification& proto) {
278 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
279 SET_STR(guid);
280 SET_STR(app_id);
281 SET_INT64(creation_timestamp_ms);
282 SET_STR(title);
283 SET_STR(body_text);
284 SET_STR(link_url);
285 SET_STR(link_text);
286 return value;
289 scoped_ptr<base::DictionaryValue> AppSettingSpecificsToValue(
290 const sync_pb::AppSettingSpecifics& proto) {
291 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
292 SET(extension_setting, ExtensionSettingSpecificsToValue);
293 return value;
296 scoped_ptr<base::DictionaryValue> LinkedAppIconInfoToValue(
297 const sync_pb::LinkedAppIconInfo& proto) {
298 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
299 SET_STR(url);
300 SET_INT32(size);
301 return value;
304 scoped_ptr<base::DictionaryValue> AppSpecificsToValue(
305 const sync_pb::AppSpecifics& proto) {
306 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
307 SET(extension, ExtensionSpecificsToValue);
308 SET(notification_settings, AppSettingsToValue);
309 SET_STR(app_launch_ordinal);
310 SET_STR(page_ordinal);
311 SET_ENUM(launch_type, GetLaunchTypeString);
312 SET_STR(bookmark_app_url);
313 SET_STR(bookmark_app_description);
314 SET_STR(bookmark_app_icon_color);
315 SET_REP(linked_app_icons, LinkedAppIconInfoToValue);
317 return value;
320 scoped_ptr<base::DictionaryValue> AutofillSpecificsToValue(
321 const sync_pb::AutofillSpecifics& proto) {
322 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
323 SET_STR(name);
324 SET_STR(value);
325 SET_INT64_REP(usage_timestamp);
326 SET(profile, AutofillProfileSpecificsToValue);
327 return value;
330 scoped_ptr<base::DictionaryValue> AutofillProfileSpecificsToValue(
331 const sync_pb::AutofillProfileSpecifics& proto) {
332 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
333 SET_STR(guid);
334 SET_STR(origin);
335 SET_INT64(use_count);
336 SET_INT64(use_date);
338 SET_STR_REP(name_first);
339 SET_STR_REP(name_middle);
340 SET_STR_REP(name_last);
341 SET_STR_REP(name_full);
342 SET_STR_REP(email_address);
343 SET_STR(company_name);
345 SET_STR(address_home_line1);
346 SET_STR(address_home_line2);
347 SET_STR(address_home_city);
348 SET_STR(address_home_state);
349 SET_STR(address_home_zip);
350 SET_STR(address_home_country);
352 SET_STR(address_home_street_address);
353 SET_STR(address_home_sorting_code);
354 SET_STR(address_home_dependent_locality);
355 SET_STR(address_home_language_code);
357 SET_STR_REP(phone_home_whole_number);
358 return value;
361 scoped_ptr<base::DictionaryValue> WalletMetadataSpecificsToValue(
362 const sync_pb::WalletMetadataSpecifics& proto) {
363 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
364 SET_ENUM(type, GetWalletMetadataTypeString);
365 SET_STR(id);
366 SET_INT64(use_count);
367 SET_INT64(use_date);
368 return value;
371 scoped_ptr<base::DictionaryValue> AutofillWalletSpecificsToValue(
372 const sync_pb::AutofillWalletSpecifics& proto) {
373 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
375 SET_ENUM(type, GetWalletInfoTypeString);
376 if (proto.type() == sync_pb::AutofillWalletSpecifics::MASKED_CREDIT_CARD) {
377 value->Set("masked_card",
378 WalletMaskedCreditCardToValue(proto.masked_card()));
379 } else if (proto.type() == sync_pb::AutofillWalletSpecifics::POSTAL_ADDRESS) {
380 value->Set("address",
381 WalletPostalAddressToValue(proto.address()));
383 return value;
386 scoped_ptr<base::DictionaryValue> MetaInfoToValue(
387 const sync_pb::MetaInfo& proto) {
388 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
389 SET_STR(key);
390 SET_STR(value);
391 return value;
394 scoped_ptr<base::DictionaryValue> BookmarkSpecificsToValue(
395 const sync_pb::BookmarkSpecifics& proto) {
396 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
397 SET_STR(url);
398 SET_BYTES(favicon);
399 SET_STR(title);
400 SET_INT64(creation_time_us);
401 SET_STR(icon_url);
402 SET_REP(meta_info, &MetaInfoToValue);
403 return value;
406 scoped_ptr<base::DictionaryValue> DeviceInfoSpecificsToValue(
407 const sync_pb::DeviceInfoSpecifics& proto) {
408 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
409 SET_STR(cache_guid);
410 SET_STR(client_name);
411 SET_ENUM(device_type, GetDeviceTypeString);
412 SET_STR(sync_user_agent);
413 SET_STR(chrome_version);
414 SET_TIME_STR(backup_timestamp);
415 SET_STR(signin_scoped_device_id);
416 return value;
419 scoped_ptr<base::DictionaryValue> DictionarySpecificsToValue(
420 const sync_pb::DictionarySpecifics& proto) {
421 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
422 SET_STR(word);
423 return value;
426 namespace {
428 scoped_ptr<base::DictionaryValue> FaviconSyncFlagsToValue(
429 const sync_pb::FaviconSyncFlags& proto) {
430 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
431 SET_BOOL(enabled);
432 SET_INT32(favicon_sync_limit);
433 return value;
436 } // namespace
438 scoped_ptr<base::DictionaryValue> ExperimentsSpecificsToValue(
439 const sync_pb::ExperimentsSpecifics& proto) {
440 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
441 SET_EXPERIMENT_ENABLED_FIELD(keystore_encryption);
442 SET_EXPERIMENT_ENABLED_FIELD(history_delete_directives);
443 SET_EXPERIMENT_ENABLED_FIELD(autofill_culling);
444 SET_EXPERIMENT_ENABLED_FIELD(pre_commit_update_avoidance);
445 SET(favicon_sync, FaviconSyncFlagsToValue);
446 SET_EXPERIMENT_ENABLED_FIELD(gcm_channel);
447 SET_EXPERIMENT_ENABLED_FIELD(gcm_invalidations);
448 SET_EXPERIMENT_ENABLED_FIELD(wallet_sync);
449 return value;
452 scoped_ptr<base::DictionaryValue> ExtensionSettingSpecificsToValue(
453 const sync_pb::ExtensionSettingSpecifics& proto) {
454 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
455 SET_STR(extension_id);
456 SET_STR(key);
457 SET_STR(value);
458 return value;
461 scoped_ptr<base::DictionaryValue> ExtensionSpecificsToValue(
462 const sync_pb::ExtensionSpecifics& proto) {
463 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
464 SET_STR(id);
465 SET_STR(version);
466 SET_STR(update_url);
467 SET_BOOL(enabled);
468 SET_BOOL(incognito_enabled);
469 SET_STR(name);
470 SET_BOOL(remote_install);
471 SET_BOOL(installed_by_custodian);
472 SET_BOOL(all_urls_enabled);
473 SET_INT32(disable_reasons);
474 return value;
477 namespace {
478 scoped_ptr<base::DictionaryValue> FaviconDataToValue(
479 const sync_pb::FaviconData& proto) {
480 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
481 SET_BYTES(favicon);
482 SET_INT32(width);
483 SET_INT32(height);
484 return value;
486 } // namespace
488 scoped_ptr<base::DictionaryValue> FaviconImageSpecificsToValue(
489 const sync_pb::FaviconImageSpecifics& proto) {
490 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
491 SET_STR(favicon_url);
492 SET(favicon_web, FaviconDataToValue);
493 SET(favicon_web_32, FaviconDataToValue);
494 SET(favicon_touch_64, FaviconDataToValue);
495 SET(favicon_touch_precomposed_64, FaviconDataToValue);
496 return value;
499 scoped_ptr<base::DictionaryValue> FaviconTrackingSpecificsToValue(
500 const sync_pb::FaviconTrackingSpecifics& proto) {
501 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
502 SET_STR(favicon_url);
503 SET_INT64(last_visit_time_ms)
504 SET_BOOL(is_bookmarked);
505 return value;
508 scoped_ptr<base::DictionaryValue> HistoryDeleteDirectiveSpecificsToValue(
509 const sync_pb::HistoryDeleteDirectiveSpecifics& proto) {
510 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
511 SET(global_id_directive, GlobalIdDirectiveToValue);
512 SET(time_range_directive, TimeRangeDirectiveToValue);
513 return value;
516 scoped_ptr<base::DictionaryValue> ManagedUserSettingSpecificsToValue(
517 const sync_pb::ManagedUserSettingSpecifics& proto) {
518 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
519 SET_STR(name);
520 SET_STR(value);
521 return value;
524 scoped_ptr<base::DictionaryValue> ManagedUserSpecificsToValue(
525 const sync_pb::ManagedUserSpecifics& proto) {
526 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
527 SET_STR(id);
528 SET_STR(name);
529 SET_BOOL(acknowledged);
530 SET_STR(master_key);
531 SET_STR(chrome_avatar);
532 SET_STR(chromeos_avatar);
533 return value;
536 scoped_ptr<base::DictionaryValue> ManagedUserSharedSettingSpecificsToValue(
537 const sync_pb::ManagedUserSharedSettingSpecifics& proto) {
538 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
539 SET_STR(mu_id);
540 SET_STR(key);
541 SET_STR(value);
542 SET_BOOL(acknowledged);
543 return value;
546 scoped_ptr<base::DictionaryValue> ManagedUserWhitelistSpecificsToValue(
547 const sync_pb::ManagedUserWhitelistSpecifics& proto) {
548 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
549 SET_STR(id);
550 SET_STR(name);
551 return value;
554 scoped_ptr<base::DictionaryValue> NigoriSpecificsToValue(
555 const sync_pb::NigoriSpecifics& proto) {
556 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
557 SET(encryption_keybag, EncryptedDataToValue);
558 SET_BOOL(keybag_is_frozen);
559 SET_BOOL(encrypt_bookmarks);
560 SET_BOOL(encrypt_preferences);
561 SET_BOOL(encrypt_autofill_profile);
562 SET_BOOL(encrypt_autofill);
563 SET_BOOL(encrypt_themes);
564 SET_BOOL(encrypt_typed_urls);
565 SET_BOOL(encrypt_extension_settings);
566 SET_BOOL(encrypt_extensions);
567 SET_BOOL(encrypt_sessions);
568 SET_BOOL(encrypt_app_settings);
569 SET_BOOL(encrypt_apps);
570 SET_BOOL(encrypt_search_engines);
571 SET_BOOL(encrypt_dictionary);
572 SET_BOOL(encrypt_articles);
573 SET_BOOL(encrypt_app_list);
574 SET_BOOL(encrypt_everything);
575 SET_BOOL(server_only_was_missing_keystore_migration_time);
576 SET_BOOL(sync_tab_favicons);
577 SET_ENUM(passphrase_type, PassphraseTypeString);
578 SET(keystore_decryptor_token, EncryptedDataToValue);
579 SET_INT64(keystore_migration_time);
580 SET_INT64(custom_passphrase_time);
581 return value;
584 scoped_ptr<base::DictionaryValue> ArticlePageToValue(
585 const sync_pb::ArticlePage& proto) {
586 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
587 SET_STR(url);
588 return value;
591 scoped_ptr<base::DictionaryValue> ArticleSpecificsToValue(
592 const sync_pb::ArticleSpecifics& proto) {
593 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
594 SET_STR(entry_id);
595 SET_STR(title);
596 SET_REP(pages, ArticlePageToValue);
597 return value;
600 scoped_ptr<base::DictionaryValue> PasswordSpecificsToValue(
601 const sync_pb::PasswordSpecifics& proto) {
602 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
603 SET(encrypted, EncryptedDataToValue);
604 return value;
607 scoped_ptr<base::DictionaryValue> PreferenceSpecificsToValue(
608 const sync_pb::PreferenceSpecifics& proto) {
609 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
610 SET_STR(name);
611 SET_STR(value);
612 return value;
615 scoped_ptr<base::DictionaryValue> PriorityPreferenceSpecificsToValue(
616 const sync_pb::PriorityPreferenceSpecifics& specifics) {
617 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
618 SET_FIELD(preference, PreferenceSpecificsToValue);
619 return value;
622 scoped_ptr<base::DictionaryValue> SyncedNotificationAppInfoSpecificsToValue(
623 const sync_pb::SyncedNotificationAppInfoSpecifics& proto) {
624 return make_scoped_ptr(new base::DictionaryValue());
627 scoped_ptr<base::DictionaryValue> SyncedNotificationSpecificsToValue(
628 const sync_pb::SyncedNotificationSpecifics& proto) {
629 return make_scoped_ptr(new base::DictionaryValue());
632 scoped_ptr<base::DictionaryValue> SearchEngineSpecificsToValue(
633 const sync_pb::SearchEngineSpecifics& proto) {
634 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
635 SET_STR(short_name);
636 SET_STR(keyword);
637 SET_STR(favicon_url);
638 SET_STR(url);
639 SET_BOOL(safe_for_autoreplace);
640 SET_STR(originating_url);
641 SET_INT64(date_created);
642 SET_STR(input_encodings);
643 SET_BOOL(show_in_default_list);
644 SET_STR(suggestions_url);
645 SET_INT32(prepopulate_id);
646 SET_BOOL(autogenerate_keyword);
647 SET_STR(instant_url);
648 SET_INT64(last_modified);
649 SET_STR(sync_guid);
650 SET_STR_REP(alternate_urls);
651 SET_STR(search_terms_replacement_key);
652 SET_STR(image_url);
653 SET_STR(search_url_post_params);
654 SET_STR(suggestions_url_post_params);
655 SET_STR(instant_url_post_params);
656 SET_STR(image_url_post_params);
657 SET_STR(new_tab_url);
658 return value;
661 scoped_ptr<base::DictionaryValue> SessionSpecificsToValue(
662 const sync_pb::SessionSpecifics& proto) {
663 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
664 SET_STR(session_tag);
665 SET(header, SessionHeaderToValue);
666 SET(tab, SessionTabToValue);
667 SET_INT32(tab_node_id);
668 return value;
671 scoped_ptr<base::DictionaryValue> ThemeSpecificsToValue(
672 const sync_pb::ThemeSpecifics& proto) {
673 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
674 SET_BOOL(use_custom_theme);
675 SET_BOOL(use_system_theme_by_default);
676 SET_STR(custom_theme_name);
677 SET_STR(custom_theme_id);
678 SET_STR(custom_theme_update_url);
679 return value;
682 scoped_ptr<base::DictionaryValue> TypedUrlSpecificsToValue(
683 const sync_pb::TypedUrlSpecifics& proto) {
684 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
685 SET_STR(url);
686 SET_STR(title);
687 SET_BOOL(hidden);
688 SET_INT64_REP(visits);
689 SET_INT32_REP(visit_transitions);
690 return value;
693 scoped_ptr<base::DictionaryValue> WalletMaskedCreditCardToValue(
694 const sync_pb::WalletMaskedCreditCard& proto) {
695 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
696 SET_STR(id);
697 SET_ENUM(status, GetWalletCardStatusString);
698 SET_STR(name_on_card);
699 SET_ENUM(type, GetWalletCardTypeString);
700 SET_STR(last_four);
701 SET_INT32(exp_month);
702 SET_INT32(exp_year);
703 return value;
706 scoped_ptr<base::DictionaryValue> WalletPostalAddressToValue(
707 const sync_pb::WalletPostalAddress& proto) {
708 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
709 SET_STR(recipient_name);
710 SET_STR(company_name);
711 SET_STR_REP(street_address);
712 SET_STR(address_1);
713 SET_STR(address_2);
714 SET_STR(address_3);
715 SET_STR(address_4);
716 SET_STR(postal_code);
717 SET_STR(sorting_code);
718 SET_STR(country_code);
719 SET_STR(phone_number);
720 SET_STR(language_code);
721 return value;
724 scoped_ptr<base::DictionaryValue> WifiCredentialSpecificsToValue(
725 const sync_pb::WifiCredentialSpecifics& proto) {
726 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
727 SET_BYTES(ssid);
728 SET_ENUM(security_class, GetWifiCredentialSecurityClassString);
729 SET_BYTES(passphrase);
730 return value;
733 scoped_ptr<base::DictionaryValue> EntitySpecificsToValue(
734 const sync_pb::EntitySpecifics& specifics) {
735 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
736 SET_FIELD(app, AppSpecificsToValue);
737 SET_FIELD(app_list, AppListSpecificsToValue);
738 SET_FIELD(app_notification, AppNotificationToValue);
739 SET_FIELD(app_setting, AppSettingSpecificsToValue);
740 SET_FIELD(article, ArticleSpecificsToValue);
741 SET_FIELD(autofill, AutofillSpecificsToValue);
742 SET_FIELD(autofill_profile, AutofillProfileSpecificsToValue);
743 SET_FIELD(autofill_wallet, AutofillWalletSpecificsToValue);
744 SET_FIELD(wallet_metadata, WalletMetadataSpecificsToValue);
745 SET_FIELD(bookmark, BookmarkSpecificsToValue);
746 SET_FIELD(device_info, DeviceInfoSpecificsToValue);
747 SET_FIELD(dictionary, DictionarySpecificsToValue);
748 SET_FIELD(experiments, ExperimentsSpecificsToValue);
749 SET_FIELD(extension, ExtensionSpecificsToValue);
750 SET_FIELD(extension_setting, ExtensionSettingSpecificsToValue);
751 SET_FIELD(favicon_image, FaviconImageSpecificsToValue);
752 SET_FIELD(favicon_tracking, FaviconTrackingSpecificsToValue);
753 SET_FIELD(history_delete_directive, HistoryDeleteDirectiveSpecificsToValue);
754 SET_FIELD(managed_user_setting, ManagedUserSettingSpecificsToValue);
755 SET_FIELD(managed_user_shared_setting,
756 ManagedUserSharedSettingSpecificsToValue);
757 SET_FIELD(managed_user, ManagedUserSpecificsToValue);
758 SET_FIELD(managed_user_whitelist, ManagedUserWhitelistSpecificsToValue);
759 SET_FIELD(nigori, NigoriSpecificsToValue);
760 SET_FIELD(password, PasswordSpecificsToValue);
761 SET_FIELD(preference, PreferenceSpecificsToValue);
762 SET_FIELD(priority_preference, PriorityPreferenceSpecificsToValue);
763 SET_FIELD(search_engine, SearchEngineSpecificsToValue);
764 SET_FIELD(session, SessionSpecificsToValue);
765 SET_FIELD(synced_notification, SyncedNotificationSpecificsToValue);
766 SET_FIELD(synced_notification_app_info,
767 SyncedNotificationAppInfoSpecificsToValue);
768 SET_FIELD(theme, ThemeSpecificsToValue);
769 SET_FIELD(typed_url, TypedUrlSpecificsToValue);
770 SET_FIELD(wifi_credential, WifiCredentialSpecificsToValue);
771 return value;
774 namespace {
776 base::StringValue* UniquePositionToStringValue(
777 const sync_pb::UniquePosition& proto) {
778 UniquePosition pos = UniquePosition::FromProto(proto);
779 return new base::StringValue(pos.ToDebugString());
782 } // namespace
784 scoped_ptr<base::DictionaryValue> SyncEntityToValue(
785 const sync_pb::SyncEntity& proto,
786 bool include_specifics) {
787 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
788 SET_STR(id_string);
789 SET_STR(parent_id_string);
790 SET_STR(old_parent_id);
791 SET_INT64(version);
792 SET_INT64(mtime);
793 SET_INT64(ctime);
794 SET_STR(name);
795 SET_STR(non_unique_name);
796 SET_INT64(sync_timestamp);
797 SET_STR(server_defined_unique_tag);
798 SET_INT64(position_in_parent);
799 SET(unique_position, UniquePositionToStringValue);
800 SET_STR(insert_after_item_id);
801 SET_BOOL(deleted);
802 SET_STR(originator_cache_guid);
803 SET_STR(originator_client_item_id);
804 if (include_specifics)
805 SET(specifics, EntitySpecificsToValue);
806 SET_BOOL(folder);
807 SET_STR(client_defined_unique_tag);
808 SET_REP(attachment_id, AttachmentIdProtoToValue);
809 return value;
812 namespace {
814 base::ListValue* SyncEntitiesToValue(
815 const ::google::protobuf::RepeatedPtrField<sync_pb::SyncEntity>& entities,
816 bool include_specifics) {
817 base::ListValue* list = new base::ListValue();
818 ::google::protobuf::RepeatedPtrField<sync_pb::SyncEntity>::const_iterator it;
819 for (it = entities.begin(); it != entities.end(); ++it) {
820 list->Append(SyncEntityToValue(*it, include_specifics));
823 return list;
826 scoped_ptr<base::DictionaryValue> ChromiumExtensionActivityToValue(
827 const sync_pb::ChromiumExtensionsActivity& proto) {
828 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
829 SET_STR(extension_id);
830 SET_INT32(bookmark_writes_since_last_commit);
831 return value;
834 scoped_ptr<base::DictionaryValue> CommitMessageToValue(
835 const sync_pb::CommitMessage& proto,
836 bool include_specifics) {
837 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
838 value->Set("entries",
839 SyncEntitiesToValue(proto.entries(), include_specifics));
840 SET_STR(cache_guid);
841 SET_REP(extensions_activity, ChromiumExtensionActivityToValue);
842 SET(config_params, ClientConfigParamsToValue);
843 return value;
846 scoped_ptr<base::DictionaryValue> GetUpdateTriggersToValue(
847 const sync_pb::GetUpdateTriggers& proto) {
848 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
849 SET_STR_REP(notification_hint);
850 SET_BOOL(client_dropped_hints);
851 SET_BOOL(invalidations_out_of_sync);
852 SET_INT64(local_modification_nudges);
853 SET_INT64(datatype_refresh_nudges);
854 return value;
857 scoped_ptr<base::DictionaryValue> DataTypeProgressMarkerToValue(
858 const sync_pb::DataTypeProgressMarker& proto) {
859 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
860 SET_INT32(data_type_id);
861 SET_BYTES(token);
862 SET_INT64(timestamp_token_for_migration);
863 SET_STR(notification_hint);
864 SET(get_update_triggers, GetUpdateTriggersToValue);
865 return value;
868 scoped_ptr<base::DictionaryValue> DataTypeContextToValue(
869 const sync_pb::DataTypeContext& proto) {
870 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
871 SET_INT32(data_type_id);
872 SET_STR(context);
873 SET_INT64(version);
874 return value;
877 scoped_ptr<base::DictionaryValue> GetUpdatesCallerInfoToValue(
878 const sync_pb::GetUpdatesCallerInfo& proto) {
879 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
880 SET_ENUM(source, GetUpdatesSourceString);
881 SET_BOOL(notifications_enabled);
882 return value;
885 scoped_ptr<base::DictionaryValue> GetUpdatesMessageToValue(
886 const sync_pb::GetUpdatesMessage& proto) {
887 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
888 SET(caller_info, GetUpdatesCallerInfoToValue);
889 SET_BOOL(fetch_folders);
890 SET_INT32(batch_size);
891 SET_REP(from_progress_marker, DataTypeProgressMarkerToValue);
892 SET_BOOL(streaming);
893 SET_BOOL(need_encryption_key);
894 SET_BOOL(create_mobile_bookmarks_folder);
895 SET_ENUM(get_updates_origin, GetUpdatesOriginString);
896 SET_REP(client_contexts, DataTypeContextToValue);
897 return value;
900 scoped_ptr<base::DictionaryValue> ClientStatusToValue(
901 const sync_pb::ClientStatus& proto) {
902 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
903 SET_BOOL(hierarchy_conflict_detected);
904 return value;
907 scoped_ptr<base::DictionaryValue> EntryResponseToValue(
908 const sync_pb::CommitResponse::EntryResponse& proto) {
909 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
910 SET_ENUM(response_type, GetResponseTypeString);
911 SET_STR(id_string);
912 SET_STR(parent_id_string);
913 SET_INT64(position_in_parent);
914 SET_INT64(version);
915 SET_STR(name);
916 SET_STR(error_message);
917 SET_INT64(mtime);
918 return value;
921 scoped_ptr<base::DictionaryValue> CommitResponseToValue(
922 const sync_pb::CommitResponse& proto) {
923 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
924 SET_REP(entryresponse, EntryResponseToValue);
925 return value;
928 scoped_ptr<base::DictionaryValue> GetUpdatesResponseToValue(
929 const sync_pb::GetUpdatesResponse& proto,
930 bool include_specifics) {
931 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
932 value->Set("entries",
933 SyncEntitiesToValue(proto.entries(), include_specifics));
934 SET_INT64(changes_remaining);
935 SET_REP(new_progress_marker, DataTypeProgressMarkerToValue);
936 SET_REP(context_mutations, DataTypeContextToValue);
937 return value;
940 scoped_ptr<base::DictionaryValue> ClientCommandToValue(
941 const sync_pb::ClientCommand& proto) {
942 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
943 SET_INT32(set_sync_poll_interval);
944 SET_INT32(set_sync_long_poll_interval);
945 SET_INT32(max_commit_batch_size);
946 SET_INT32(sessions_commit_delay_seconds);
947 SET_INT32(throttle_delay_seconds);
948 SET_INT32(client_invalidation_hint_buffer_size);
949 return value;
952 scoped_ptr<base::DictionaryValue> ErrorToValue(
953 const sync_pb::ClientToServerResponse::Error& proto) {
954 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
955 SET_ENUM(error_type, GetErrorTypeString);
956 SET_STR(error_description);
957 SET_STR(url);
958 SET_ENUM(action, GetActionString);
959 return value;
962 } // namespace
964 scoped_ptr<base::DictionaryValue> ClientToServerResponseToValue(
965 const sync_pb::ClientToServerResponse& proto,
966 bool include_specifics) {
967 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
968 SET(commit, CommitResponseToValue);
969 if (proto.has_get_updates()) {
970 value->Set("get_updates", GetUpdatesResponseToValue(proto.get_updates(),
971 include_specifics));
974 SET(error, ErrorToValue);
975 SET_ENUM(error_code, GetErrorTypeString);
976 SET_STR(error_message);
977 SET_STR(store_birthday);
978 SET(client_command, ClientCommandToValue);
979 SET_INT32_REP(migrated_data_type_id);
980 return value;
983 scoped_ptr<base::DictionaryValue> ClientToServerMessageToValue(
984 const sync_pb::ClientToServerMessage& proto,
985 bool include_specifics) {
986 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
987 SET_STR(share);
988 SET_INT32(protocol_version);
989 if (proto.has_commit()) {
990 value->Set("commit",
991 CommitMessageToValue(proto.commit(), include_specifics));
994 SET(get_updates, GetUpdatesMessageToValue);
995 SET_STR(store_birthday);
996 SET_BOOL(sync_problem_detected);
997 SET(debug_info, DebugInfoToValue);
998 SET(client_status, ClientStatusToValue);
999 return value;
1002 scoped_ptr<base::DictionaryValue> DatatypeAssociationStatsToValue(
1003 const sync_pb::DatatypeAssociationStats& proto) {
1004 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
1005 SET_INT32(data_type_id);
1006 SET_INT32(num_local_items_before_association);
1007 SET_INT32(num_sync_items_before_association);
1008 SET_INT32(num_local_items_after_association);
1009 SET_INT32(num_sync_items_after_association);
1010 SET_INT32(num_local_items_added);
1011 SET_INT32(num_local_items_deleted);
1012 SET_INT32(num_local_items_modified);
1013 SET_INT32(num_sync_items_added);
1014 SET_INT32(num_sync_items_deleted);
1015 SET_INT32(num_sync_items_modified);
1016 SET_INT64(local_version_pre_association);
1017 SET_INT64(sync_version_pre_association)
1018 SET_BOOL(had_error);
1019 SET_INT64(download_wait_time_us);
1020 SET_INT64(download_time_us);
1021 SET_INT64(association_wait_time_for_high_priority_us);
1022 SET_INT64(association_wait_time_for_same_priority_us);
1023 return value;
1026 scoped_ptr<base::DictionaryValue> DebugEventInfoToValue(
1027 const sync_pb::DebugEventInfo& proto) {
1028 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
1029 SET_ENUM(singleton_event, SingletonDebugEventTypeString);
1030 SET(sync_cycle_completed_event_info, SyncCycleCompletedEventInfoToValue);
1031 SET_INT32(nudging_datatype);
1032 SET_INT32_REP(datatypes_notified_from_server);
1033 SET(datatype_association_stats, DatatypeAssociationStatsToValue);
1034 return value;
1037 scoped_ptr<base::DictionaryValue> DebugInfoToValue(
1038 const sync_pb::DebugInfo& proto) {
1039 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
1040 SET_REP(events, DebugEventInfoToValue);
1041 SET_BOOL(cryptographer_ready);
1042 SET_BOOL(cryptographer_has_pending_keys);
1043 SET_BOOL(events_dropped);
1044 return value;
1047 scoped_ptr<base::DictionaryValue> SyncCycleCompletedEventInfoToValue(
1048 const sync_pb::SyncCycleCompletedEventInfo& proto) {
1049 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
1050 SET_INT32(num_encryption_conflicts);
1051 SET_INT32(num_hierarchy_conflicts);
1052 SET_INT32(num_server_conflicts);
1053 SET_INT32(num_updates_downloaded);
1054 SET_INT32(num_reflected_updates_downloaded);
1055 SET(caller_info, GetUpdatesCallerInfoToValue);
1056 return value;
1059 scoped_ptr<base::DictionaryValue> ClientConfigParamsToValue(
1060 const sync_pb::ClientConfigParams& proto) {
1061 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
1062 SET_INT32_REP(enabled_type_ids);
1063 SET_BOOL(tabs_datatype_enabled);
1064 return value;
1067 scoped_ptr<base::DictionaryValue> AttachmentIdProtoToValue(
1068 const sync_pb::AttachmentIdProto& proto) {
1069 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
1070 SET_STR(unique_id);
1071 return value;
1074 #undef SET_TYPE
1075 #undef SET
1076 #undef SET_REP
1078 #undef SET_BOOL
1079 #undef SET_BYTES
1080 #undef SET_INT32
1081 #undef SET_INT64
1082 #undef SET_INT64_REP
1083 #undef SET_STR
1084 #undef SET_STR_REP
1086 #undef SET_FIELD
1088 } // namespace syncer