OLD | NEW |
| (Empty) |
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. | |
4 | |
5 // Keep this file in sync with the .proto files in this directory. | |
6 | |
7 #include "chrome/browser/sync/protocol/proto_value_conversions.h" | |
8 | |
9 #include "base/base64.h" | |
10 #include "base/basictypes.h" | |
11 #include "base/logging.h" | |
12 #include "base/string_number_conversions.h" | |
13 #include "base/values.h" | |
14 #include "chrome/browser/sync/protocol/proto_enum_conversions.h" | |
15 #include "sync/protocol/app_notification_specifics.pb.h" | |
16 #include "sync/protocol/app_setting_specifics.pb.h" | |
17 #include "sync/protocol/app_specifics.pb.h" | |
18 #include "sync/protocol/autofill_specifics.pb.h" | |
19 #include "sync/protocol/bookmark_specifics.pb.h" | |
20 #include "sync/protocol/encryption.pb.h" | |
21 #include "sync/protocol/extension_setting_specifics.pb.h" | |
22 #include "sync/protocol/extension_specifics.pb.h" | |
23 #include "sync/protocol/nigori_specifics.pb.h" | |
24 #include "sync/protocol/password_specifics.pb.h" | |
25 #include "sync/protocol/preference_specifics.pb.h" | |
26 #include "sync/protocol/search_engine_specifics.pb.h" | |
27 #include "sync/protocol/session_specifics.pb.h" | |
28 #include "sync/protocol/sync.pb.h" | |
29 #include "sync/protocol/theme_specifics.pb.h" | |
30 #include "sync/protocol/typed_url_specifics.pb.h" | |
31 | |
32 namespace browser_sync { | |
33 | |
34 namespace { | |
35 | |
36 // Basic Type -> Value functions. | |
37 | |
38 StringValue* MakeInt64Value(int64 x) { | |
39 return Value::CreateStringValue(base::Int64ToString(x)); | |
40 } | |
41 | |
42 // TODO(akalin): Perhaps make JSONWriter support BinaryValue and use | |
43 // that instead of a StringValue. | |
44 StringValue* MakeBytesValue(const std::string& bytes) { | |
45 std::string bytes_base64; | |
46 if (!base::Base64Encode(bytes, &bytes_base64)) { | |
47 NOTREACHED(); | |
48 } | |
49 return Value::CreateStringValue(bytes_base64); | |
50 } | |
51 | |
52 // T is the enum type. | |
53 template <class T> | |
54 StringValue* MakeEnumValue(T t, const char* (*converter_fn)(T)) { | |
55 return Value::CreateStringValue(converter_fn(t)); | |
56 } | |
57 | |
58 // T is the field type, F is either RepeatedField or RepeatedPtrField, | |
59 // and V is a subclass of Value. | |
60 template <class T, class F, class V> | |
61 ListValue* MakeRepeatedValue(const F& fields, V* (*converter_fn)(T)) { | |
62 ListValue* list = new ListValue(); | |
63 for (typename F::const_iterator it = fields.begin(); it != fields.end(); | |
64 ++it) { | |
65 list->Append(converter_fn(*it)); | |
66 } | |
67 return list; | |
68 } | |
69 | |
70 } // namespace | |
71 | |
72 // Helper macros to reduce the amount of boilerplate. | |
73 | |
74 #define SET(field, fn) value->Set(#field, fn(proto.field())) | |
75 #define SET_REP(field, fn) \ | |
76 value->Set(#field, MakeRepeatedValue(proto.field(), fn)) | |
77 #define SET_ENUM(field, fn) \ | |
78 value->Set(#field, MakeEnumValue(proto.field(), fn)) | |
79 | |
80 #define SET_BOOL(field) SET(field, Value::CreateBooleanValue) | |
81 #define SET_BYTES(field) SET(field, MakeBytesValue) | |
82 #define SET_INT32(field) SET(field, MakeInt64Value) | |
83 #define SET_INT32_REP(field) SET_REP(field, MakeInt64Value) | |
84 #define SET_INT64(field) SET(field, MakeInt64Value) | |
85 #define SET_INT64_REP(field) SET_REP(field, MakeInt64Value) | |
86 #define SET_STR(field) SET(field, Value::CreateStringValue) | |
87 #define SET_STR_REP(field) \ | |
88 value->Set(#field, \ | |
89 MakeRepeatedValue<const std::string&, \ | |
90 google::protobuf::RepeatedPtrField< \ | |
91 std::string >, \ | |
92 StringValue>(proto.field(), \ | |
93 Value::CreateStringValue)) | |
94 | |
95 #define SET_FIELD(field, fn) \ | |
96 do { \ | |
97 if (specifics.has_##field()) { \ | |
98 value->Set(#field, fn(specifics.field())); \ | |
99 } \ | |
100 } while (0) | |
101 | |
102 // If you add another macro, don't forget to add an #undef at the end | |
103 // of this file, too. | |
104 | |
105 DictionaryValue* EncryptedDataToValue(const sync_pb::EncryptedData& proto) { | |
106 DictionaryValue* value = new DictionaryValue(); | |
107 SET_STR(key_name); | |
108 // TODO(akalin): Shouldn't blob be of type bytes instead of string? | |
109 SET_BYTES(blob); | |
110 return value; | |
111 } | |
112 | |
113 DictionaryValue* AppSettingsToValue( | |
114 const sync_pb::AppNotificationSettings& proto) { | |
115 DictionaryValue* value = new DictionaryValue(); | |
116 SET_BOOL(initial_setup_done); | |
117 SET_BOOL(disabled); | |
118 SET_STR(oauth_client_id); | |
119 return value; | |
120 } | |
121 | |
122 DictionaryValue* SessionHeaderToValue( | |
123 const sync_pb::SessionHeader& proto) { | |
124 DictionaryValue* value = new DictionaryValue(); | |
125 SET_REP(window, SessionWindowToValue); | |
126 SET_STR(client_name); | |
127 SET_ENUM(device_type, GetDeviceTypeString); | |
128 return value; | |
129 } | |
130 | |
131 DictionaryValue* SessionTabToValue( | |
132 const sync_pb::SessionTab& proto) { | |
133 DictionaryValue* value = new DictionaryValue(); | |
134 SET_INT32(tab_id); | |
135 SET_INT32(window_id); | |
136 SET_INT32(tab_visual_index); | |
137 SET_INT32(current_navigation_index); | |
138 SET_BOOL(pinned); | |
139 SET_STR(extension_app_id); | |
140 SET_REP(navigation, TabNavigationToValue); | |
141 return value; | |
142 } | |
143 | |
144 DictionaryValue* SessionWindowToValue( | |
145 const sync_pb::SessionWindow& proto) { | |
146 DictionaryValue* value = new DictionaryValue(); | |
147 SET_INT32(window_id); | |
148 SET_INT32(selected_tab_index); | |
149 SET_INT32_REP(tab); | |
150 SET_ENUM(browser_type, GetBrowserTypeString); | |
151 return value; | |
152 } | |
153 | |
154 DictionaryValue* TabNavigationToValue( | |
155 const sync_pb::TabNavigation& proto) { | |
156 DictionaryValue* value = new DictionaryValue(); | |
157 SET_INT32(index); | |
158 SET_STR(virtual_url); | |
159 SET_STR(referrer); | |
160 SET_STR(title); | |
161 SET_STR(state); | |
162 SET_ENUM(page_transition, GetPageTransitionString); | |
163 SET_ENUM(navigation_qualifier, GetPageTransitionQualifierString); | |
164 return value; | |
165 } | |
166 | |
167 DictionaryValue* PasswordSpecificsDataToValue( | |
168 const sync_pb::PasswordSpecificsData& proto) { | |
169 DictionaryValue* value = new DictionaryValue(); | |
170 SET_INT32(scheme); | |
171 SET_STR(signon_realm); | |
172 SET_STR(origin); | |
173 SET_STR(action); | |
174 SET_STR(username_element); | |
175 SET_STR(username_value); | |
176 SET_STR(password_element); | |
177 value->SetString("password_value", "<redacted>"); | |
178 SET_BOOL(ssl_valid); | |
179 SET_BOOL(preferred); | |
180 SET_INT64(date_created); | |
181 SET_BOOL(blacklisted); | |
182 return value; | |
183 } | |
184 | |
185 DictionaryValue* DeviceInformationToValue( | |
186 const sync_pb::DeviceInformation& proto) { | |
187 DictionaryValue* value = new DictionaryValue(); | |
188 SET_STR(cache_guid); | |
189 SET_STR(name); | |
190 SET_STR(platform); | |
191 SET_STR(chrome_version); | |
192 return value; | |
193 } | |
194 | |
195 DictionaryValue* AppNotificationToValue( | |
196 const sync_pb::AppNotification& proto) { | |
197 DictionaryValue* value = new DictionaryValue(); | |
198 SET_STR(guid); | |
199 SET_STR(app_id); | |
200 SET_INT64(creation_timestamp_ms); | |
201 SET_STR(title); | |
202 SET_STR(body_text); | |
203 SET_STR(link_url); | |
204 SET_STR(link_text); | |
205 return value; | |
206 } | |
207 | |
208 DictionaryValue* AppSettingSpecificsToValue( | |
209 const sync_pb::AppSettingSpecifics& proto) { | |
210 DictionaryValue* value = new DictionaryValue(); | |
211 SET(extension_setting, ExtensionSettingSpecificsToValue); | |
212 return value; | |
213 } | |
214 | |
215 DictionaryValue* AppSpecificsToValue( | |
216 const sync_pb::AppSpecifics& proto) { | |
217 DictionaryValue* value = new DictionaryValue(); | |
218 SET(extension, ExtensionSpecificsToValue); | |
219 SET(notification_settings, AppSettingsToValue); | |
220 SET_STR(app_launch_ordinal); | |
221 SET_STR(page_ordinal); | |
222 | |
223 return value; | |
224 } | |
225 | |
226 DictionaryValue* AutofillSpecificsToValue( | |
227 const sync_pb::AutofillSpecifics& proto) { | |
228 DictionaryValue* value = new DictionaryValue(); | |
229 SET_STR(name); | |
230 SET_STR(value); | |
231 SET_INT64_REP(usage_timestamp); | |
232 SET(profile, AutofillProfileSpecificsToValue); | |
233 return value; | |
234 } | |
235 | |
236 DictionaryValue* AutofillProfileSpecificsToValue( | |
237 const sync_pb::AutofillProfileSpecifics& proto) { | |
238 DictionaryValue* value = new DictionaryValue(); | |
239 SET_STR(label); | |
240 SET_STR(guid); | |
241 | |
242 SET_STR_REP(name_first); | |
243 SET_STR_REP(name_middle); | |
244 SET_STR_REP(name_last); | |
245 SET_STR_REP(email_address); | |
246 SET_STR(company_name); | |
247 | |
248 SET_STR(address_home_line1); | |
249 SET_STR(address_home_line2); | |
250 SET_STR(address_home_city); | |
251 SET_STR(address_home_state); | |
252 SET_STR(address_home_zip); | |
253 SET_STR(address_home_country); | |
254 | |
255 SET_STR_REP(phone_home_whole_number); | |
256 return value; | |
257 } | |
258 | |
259 DictionaryValue* BookmarkSpecificsToValue( | |
260 const sync_pb::BookmarkSpecifics& proto) { | |
261 DictionaryValue* value = new DictionaryValue(); | |
262 SET_STR(url); | |
263 SET_BYTES(favicon); | |
264 SET_STR(title); | |
265 return value; | |
266 } | |
267 | |
268 DictionaryValue* ExtensionSettingSpecificsToValue( | |
269 const sync_pb::ExtensionSettingSpecifics& proto) { | |
270 DictionaryValue* value = new DictionaryValue(); | |
271 SET_STR(extension_id); | |
272 SET_STR(key); | |
273 SET_STR(value); | |
274 return value; | |
275 } | |
276 | |
277 DictionaryValue* ExtensionSpecificsToValue( | |
278 const sync_pb::ExtensionSpecifics& proto) { | |
279 DictionaryValue* value = new DictionaryValue(); | |
280 SET_STR(id); | |
281 SET_STR(version); | |
282 SET_STR(update_url); | |
283 SET_BOOL(enabled); | |
284 SET_BOOL(incognito_enabled); | |
285 SET_STR(name); | |
286 return value; | |
287 } | |
288 | |
289 DictionaryValue* NigoriSpecificsToValue( | |
290 const sync_pb::NigoriSpecifics& proto) { | |
291 DictionaryValue* value = new DictionaryValue(); | |
292 SET(encrypted, EncryptedDataToValue); | |
293 SET_BOOL(using_explicit_passphrase); | |
294 SET_BOOL(encrypt_bookmarks); | |
295 SET_BOOL(encrypt_preferences); | |
296 SET_BOOL(encrypt_autofill_profile); | |
297 SET_BOOL(encrypt_autofill); | |
298 SET_BOOL(encrypt_themes); | |
299 SET_BOOL(encrypt_typed_urls); | |
300 SET_BOOL(encrypt_extension_settings); | |
301 SET_BOOL(encrypt_extensions); | |
302 SET_BOOL(encrypt_sessions); | |
303 SET_BOOL(encrypt_app_settings); | |
304 SET_BOOL(encrypt_apps); | |
305 SET_BOOL(encrypt_search_engines); | |
306 SET_BOOL(sync_tabs); | |
307 SET_BOOL(encrypt_everything); | |
308 SET_REP(device_information, DeviceInformationToValue); | |
309 return value; | |
310 } | |
311 | |
312 DictionaryValue* PasswordSpecificsToValue( | |
313 const sync_pb::PasswordSpecifics& proto) { | |
314 DictionaryValue* value = new DictionaryValue(); | |
315 SET(encrypted, EncryptedDataToValue); | |
316 return value; | |
317 } | |
318 | |
319 DictionaryValue* PreferenceSpecificsToValue( | |
320 const sync_pb::PreferenceSpecifics& proto) { | |
321 DictionaryValue* value = new DictionaryValue(); | |
322 SET_STR(name); | |
323 SET_STR(value); | |
324 return value; | |
325 } | |
326 | |
327 DictionaryValue* SearchEngineSpecificsToValue( | |
328 const sync_pb::SearchEngineSpecifics& proto) { | |
329 DictionaryValue* value = new DictionaryValue(); | |
330 SET_STR(short_name); | |
331 SET_STR(keyword); | |
332 SET_STR(favicon_url); | |
333 SET_STR(url); | |
334 SET_BOOL(safe_for_autoreplace); | |
335 SET_STR(originating_url); | |
336 SET_INT64(date_created); | |
337 SET_STR(input_encodings); | |
338 SET_BOOL(show_in_default_list); | |
339 SET_STR(suggestions_url); | |
340 SET_INT32(prepopulate_id); | |
341 SET_BOOL(autogenerate_keyword); | |
342 SET_STR(instant_url); | |
343 SET_INT64(last_modified); | |
344 SET_STR(sync_guid); | |
345 return value; | |
346 } | |
347 | |
348 DictionaryValue* SessionSpecificsToValue( | |
349 const sync_pb::SessionSpecifics& proto) { | |
350 DictionaryValue* value = new DictionaryValue(); | |
351 SET_STR(session_tag); | |
352 SET(header, SessionHeaderToValue); | |
353 SET(tab, SessionTabToValue); | |
354 return value; | |
355 } | |
356 | |
357 DictionaryValue* ThemeSpecificsToValue( | |
358 const sync_pb::ThemeSpecifics& proto) { | |
359 DictionaryValue* value = new DictionaryValue(); | |
360 SET_BOOL(use_custom_theme); | |
361 SET_BOOL(use_system_theme_by_default); | |
362 SET_STR(custom_theme_name); | |
363 SET_STR(custom_theme_id); | |
364 SET_STR(custom_theme_update_url); | |
365 return value; | |
366 } | |
367 | |
368 DictionaryValue* TypedUrlSpecificsToValue( | |
369 const sync_pb::TypedUrlSpecifics& proto) { | |
370 DictionaryValue* value = new DictionaryValue(); | |
371 SET_STR(url); | |
372 SET_STR(title); | |
373 SET_BOOL(hidden); | |
374 SET_INT64_REP(visits); | |
375 SET_INT32_REP(visit_transitions); | |
376 return value; | |
377 } | |
378 | |
379 DictionaryValue* EntitySpecificsToValue( | |
380 const sync_pb::EntitySpecifics& specifics) { | |
381 DictionaryValue* value = new DictionaryValue(); | |
382 SET_FIELD(app, AppSpecificsToValue); | |
383 SET_FIELD(app_notification, AppNotificationToValue); | |
384 SET_FIELD(app_setting, AppSettingSpecificsToValue); | |
385 SET_FIELD(autofill, AutofillSpecificsToValue); | |
386 SET_FIELD(autofill_profile, AutofillProfileSpecificsToValue); | |
387 SET_FIELD(bookmark, BookmarkSpecificsToValue); | |
388 SET_FIELD(extension, ExtensionSpecificsToValue); | |
389 SET_FIELD(extension_setting, ExtensionSettingSpecificsToValue); | |
390 SET_FIELD(nigori, NigoriSpecificsToValue); | |
391 SET_FIELD(password, PasswordSpecificsToValue); | |
392 SET_FIELD(preference, PreferenceSpecificsToValue); | |
393 SET_FIELD(search_engine, SearchEngineSpecificsToValue); | |
394 SET_FIELD(session, SessionSpecificsToValue); | |
395 SET_FIELD(theme, ThemeSpecificsToValue); | |
396 SET_FIELD(typed_url, TypedUrlSpecificsToValue); | |
397 return value; | |
398 } | |
399 | |
400 #undef SET | |
401 #undef SET_REP | |
402 | |
403 #undef SET_BOOL | |
404 #undef SET_BYTES | |
405 #undef SET_INT32 | |
406 #undef SET_INT64 | |
407 #undef SET_INT64_REP | |
408 #undef SET_STR | |
409 #undef SET_STR_REP | |
410 | |
411 #undef SET_FIELD | |
412 | |
413 } // namespace browser_sync | |
OLD | NEW |