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 #include "chrome/browser/extensions/api/storage/settings_sync_util.h" 6 7 #include "base/json/json_writer.h" 8 #include "base/values.h" 9 #include "sync/protocol/app_setting_specifics.pb.h" 10 #include "sync/protocol/extension_setting_specifics.pb.h" 11 #include "sync/protocol/sync.pb.h" 12 13 namespace extensions { 14 15 namespace settings_sync_util { 16 17 namespace { 18 19 void PopulateExtensionSettingSpecifics( 20 const std::string& extension_id, 21 const std::string& key, 22 const Value& value, 23 sync_pb::ExtensionSettingSpecifics* specifics) { 24 specifics->set_extension_id(extension_id); 25 specifics->set_key(key); 26 { 27 std::string value_as_json; 28 base::JSONWriter::Write(&value, &value_as_json); 29 specifics->set_value(value_as_json); 30 } 31 } 32 33 void PopulateAppSettingSpecifics( 34 const std::string& extension_id, 35 const std::string& key, 36 const Value& value, 37 sync_pb::AppSettingSpecifics* specifics) { 38 PopulateExtensionSettingSpecifics( 39 extension_id, key, value, specifics->mutable_extension_setting()); 40 } 41 42 } // namespace 43 44 syncer::SyncData CreateData( 45 const std::string& extension_id, 46 const std::string& key, 47 const Value& value, 48 syncer::ModelType type) { 49 sync_pb::EntitySpecifics specifics; 50 switch (type) { 51 case syncer::EXTENSION_SETTINGS: 52 PopulateExtensionSettingSpecifics( 53 extension_id, 54 key, 55 value, 56 specifics.mutable_extension_setting()); 57 break; 58 59 case syncer::APP_SETTINGS: 60 PopulateAppSettingSpecifics( 61 extension_id, 62 key, 63 value, 64 specifics.mutable_app_setting()); 65 break; 66 67 default: 68 NOTREACHED(); 69 } 70 71 return syncer::SyncData::CreateLocalData( 72 extension_id + "/" + key, key, specifics); 73 } 74 75 syncer::SyncChange CreateAdd( 76 const std::string& extension_id, 77 const std::string& key, 78 const Value& value, 79 syncer::ModelType type) { 80 return syncer::SyncChange( 81 FROM_HERE, 82 syncer::SyncChange::ACTION_ADD, 83 CreateData(extension_id, key, value, type)); 84 } 85 86 syncer::SyncChange CreateUpdate( 87 const std::string& extension_id, 88 const std::string& key, 89 const Value& value, 90 syncer::ModelType type) { 91 return syncer::SyncChange( 92 FROM_HERE, 93 syncer::SyncChange::ACTION_UPDATE, 94 CreateData(extension_id, key, value, type)); 95 } 96 97 syncer::SyncChange CreateDelete( 98 const std::string& extension_id, 99 const std::string& key, 100 syncer::ModelType type) { 101 base::DictionaryValue no_value; 102 return syncer::SyncChange( 103 FROM_HERE, 104 syncer::SyncChange::ACTION_DELETE, 105 CreateData(extension_id, key, no_value, type)); 106 } 107 108 } // namespace settings_sync_util 109 110 } // namespace extensions 111