OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/settings/settings_sync_util.h" | 5 #include "chrome/browser/extensions/settings/settings_sync_util.h" |
6 | 6 |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "chrome/browser/sync/protocol/app_setting_specifics.pb.h" |
9 #include "chrome/browser/sync/protocol/extension_setting_specifics.pb.h" | 10 #include "chrome/browser/sync/protocol/extension_setting_specifics.pb.h" |
10 | 11 |
11 namespace extensions { | 12 namespace extensions { |
12 | 13 |
13 namespace settings_sync_util { | 14 namespace settings_sync_util { |
14 | 15 |
| 16 namespace { |
| 17 |
| 18 void PopulateExtensionSettingSpecifics( |
| 19 const std::string& extension_id, |
| 20 const std::string& key, |
| 21 const Value& value, |
| 22 sync_pb::ExtensionSettingSpecifics* specifics) { |
| 23 specifics->set_extension_id(extension_id); |
| 24 specifics->set_key(key); |
| 25 { |
| 26 std::string value_as_json; |
| 27 base::JSONWriter::Write(&value, false, &value_as_json); |
| 28 specifics->set_value(value_as_json); |
| 29 } |
| 30 } |
| 31 |
| 32 void PopulateAppSettingSpecifics( |
| 33 const std::string& extension_id, |
| 34 const std::string& key, |
| 35 const Value& value, |
| 36 sync_pb::AppSettingSpecifics* specifics) { |
| 37 PopulateExtensionSettingSpecifics( |
| 38 extension_id, key, value, specifics->mutable_extension_setting()); |
| 39 } |
| 40 |
| 41 } // namespace |
| 42 |
15 SyncData CreateData( | 43 SyncData CreateData( |
16 const std::string& extension_id, | 44 const std::string& extension_id, |
17 const std::string& key, | 45 const std::string& key, |
18 const Value& value) { | 46 const Value& value, |
| 47 syncable::ModelType type) { |
19 sync_pb::EntitySpecifics specifics; | 48 sync_pb::EntitySpecifics specifics; |
20 sync_pb::ExtensionSettingSpecifics* setting_specifics = | 49 |
21 specifics.MutableExtension(sync_pb::extension_setting); | 50 switch (type) { |
22 setting_specifics->set_extension_id(extension_id); | 51 case syncable::EXTENSION_SETTINGS: |
23 setting_specifics->set_key(key); | 52 PopulateExtensionSettingSpecifics( |
24 std::string value_as_json; | 53 extension_id, |
25 base::JSONWriter::Write(&value, false, &value_as_json); | 54 key, |
26 setting_specifics->set_value(value_as_json); | 55 value, |
| 56 specifics.MutableExtension(sync_pb::extension_setting)); |
| 57 break; |
| 58 |
| 59 case syncable::APP_SETTINGS: |
| 60 PopulateAppSettingSpecifics( |
| 61 extension_id, |
| 62 key, |
| 63 value, |
| 64 specifics.MutableExtension(sync_pb::app_setting)); |
| 65 break; |
| 66 |
| 67 default: |
| 68 NOTREACHED(); |
| 69 } |
| 70 |
27 return SyncData::CreateLocalData(extension_id + "/" + key, key, specifics); | 71 return SyncData::CreateLocalData(extension_id + "/" + key, key, specifics); |
28 } | 72 } |
29 | 73 |
30 SyncChange CreateAdd( | 74 SyncChange CreateAdd( |
31 const std::string& extension_id, | 75 const std::string& extension_id, |
32 const std::string& key, | 76 const std::string& key, |
33 const Value& value) { | 77 const Value& value, |
| 78 syncable::ModelType type) { |
34 return SyncChange( | 79 return SyncChange( |
35 SyncChange::ACTION_ADD, CreateData(extension_id, key, value)); | 80 SyncChange::ACTION_ADD, CreateData(extension_id, key, value, type)); |
36 } | 81 } |
37 | 82 |
38 SyncChange CreateUpdate( | 83 SyncChange CreateUpdate( |
39 const std::string& extension_id, | 84 const std::string& extension_id, |
40 const std::string& key, | 85 const std::string& key, |
41 const Value& value) { | 86 const Value& value, |
| 87 syncable::ModelType type) { |
42 return SyncChange( | 88 return SyncChange( |
43 SyncChange::ACTION_UPDATE, CreateData(extension_id, key, value)); | 89 SyncChange::ACTION_UPDATE, CreateData(extension_id, key, value, type)); |
44 } | 90 } |
45 | 91 |
46 SyncChange CreateDelete( | 92 SyncChange CreateDelete( |
47 const std::string& extension_id, const std::string& key) { | 93 const std::string& extension_id, |
| 94 const std::string& key, |
| 95 syncable::ModelType type) { |
48 DictionaryValue no_value; | 96 DictionaryValue no_value; |
49 return SyncChange( | 97 return SyncChange( |
50 SyncChange::ACTION_DELETE, CreateData(extension_id, key, no_value)); | 98 SyncChange::ACTION_DELETE, CreateData(extension_id, key, no_value, type)); |
51 } | 99 } |
52 | 100 |
53 } // namespace settings_sync_util | 101 } // namespace settings_sync_util |
54 | 102 |
55 } // namespace extensions | 103 } // namespace extensions |
OLD | NEW |