1 // Copyright (c) 2010 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/sync/glue/extension_sync_traits.h" 6 7 #include "base/string_piece.h" 8 #include "base/utf_string_conversions.h" 9 #include "chrome/browser/sync/engine/syncapi.h" 10 #include "chrome/browser/sync/glue/extension_util.h" 11 #include "chrome/browser/sync/protocol/app_specifics.pb.h" 12 #include "chrome/browser/sync/protocol/extension_specifics.pb.h" 13 #include "chrome/common/extensions/extension.h" 14 15 namespace browser_sync { 16 17 ExtensionSyncTraits::ExtensionSyncTraits( 18 syncable::ModelType model_type, 19 IsValidAndSyncablePredicate is_valid_and_syncable, 20 ShouldHandleExtensionUninstallPredicate 21 should_handle_extension_uninstall, 22 const char* root_node_tag, 23 ExtensionSpecificsGetter extension_specifics_getter, 24 ExtensionSpecificsSetter extension_specifics_setter, 25 ExtensionSpecificsEntityGetter extension_specifics_entity_getter) 26 : model_type(model_type), 27 is_valid_and_syncable(is_valid_and_syncable), 28 should_handle_extension_uninstall(should_handle_extension_uninstall), 29 root_node_tag(root_node_tag), 30 extension_specifics_getter(extension_specifics_getter), 31 extension_specifics_setter(extension_specifics_setter), 32 extension_specifics_entity_getter(extension_specifics_entity_getter) {} 33 34 ExtensionSyncTraits::~ExtensionSyncTraits() {} 35 36 namespace { 37 38 const sync_pb::ExtensionSpecifics& GetExtensionSpecifics( 39 const sync_api::BaseNode& node) { 40 return node.GetExtensionSpecifics(); 41 } 42 43 void SetExtensionSpecifics( 44 const sync_pb::ExtensionSpecifics& extension_specifics, 45 sync_api::WriteNode* node) { 46 node->SetTitle(UTF8ToWide(extension_specifics.name())); 47 node->SetExtensionSpecifics(extension_specifics); 48 } 49 50 bool GetExtensionSpecificsFromEntity( 51 const sync_pb::EntitySpecifics& entity_specifics, 52 sync_pb::ExtensionSpecifics* extension_specifics) { 53 if (!entity_specifics.HasExtension(sync_pb::extension)) { 54 return false; 55 } 56 *extension_specifics = entity_specifics.GetExtension(sync_pb::extension); 57 return true; 58 } 59 60 bool IsSyncableExtension(Extension::Type type, const GURL& update_url) { 61 switch (type) { 62 case Extension::TYPE_EXTENSION: 63 return true; 64 case Extension::TYPE_USER_SCRIPT: 65 // We only want to sync user scripts with update URLs. 66 return !update_url.is_empty(); 67 default: 68 return false; 69 } 70 } 71 72 bool IsValidAndSyncableExtension(const Extension& extension) { 73 return 74 IsExtensionValid(extension) && 75 IsSyncableExtension(extension.GetType(), extension.update_url()); 76 } 77 78 bool IsExtensionUninstall( 79 const UninstalledExtensionInfo& uninstalled_extension_info) { 80 return IsSyncableExtension(uninstalled_extension_info.extension_type, 81 uninstalled_extension_info.update_url); 82 } 83 84 } // namespace 85 86 ExtensionSyncTraits GetExtensionSyncTraits() { 87 return ExtensionSyncTraits(syncable::EXTENSIONS, 88 &IsValidAndSyncableExtension, 89 &IsExtensionUninstall, 90 "google_chrome_extensions", 91 &GetExtensionSpecifics, 92 &SetExtensionSpecifics, 93 &GetExtensionSpecificsFromEntity); 94 } 95 96 namespace { 97 98 const sync_pb::ExtensionSpecifics& GetExtensionSpecificsOfApp( 99 const sync_api::BaseNode& node) { 100 return node.GetAppSpecifics().extension(); 101 } 102 103 void SetExtensionSpecificsOfApp( 104 const sync_pb::ExtensionSpecifics& extension_specifics, 105 sync_api::WriteNode* node) { 106 node->SetTitle(UTF8ToWide(extension_specifics.name())); 107 sync_pb::AppSpecifics app_specifics; 108 *app_specifics.mutable_extension() = extension_specifics; 109 node->SetAppSpecifics(app_specifics); 110 } 111 112 bool GetExtensionSpecificsFromEntityOfApp( 113 const sync_pb::EntitySpecifics& entity_specifics, 114 sync_pb::ExtensionSpecifics* extension_specifics) { 115 if (!entity_specifics.HasExtension(sync_pb::app)) { 116 return false; 117 } 118 *extension_specifics = 119 entity_specifics.GetExtension(sync_pb::app).extension(); 120 return true; 121 } 122 123 bool IsSyncableApp(Extension::Type type) { 124 return 125 (type == Extension::TYPE_HOSTED_APP) || 126 (type == Extension::TYPE_PACKAGED_APP); 127 } 128 129 bool IsValidAndSyncableApp( 130 const Extension& extension) { 131 return IsExtensionValid(extension) && IsSyncableApp(extension.GetType()); 132 } 133 134 bool IsAppUninstall( 135 const UninstalledExtensionInfo& uninstalled_extension_info) { 136 return IsSyncableApp(uninstalled_extension_info.extension_type); 137 } 138 139 } // namespace 140 141 ExtensionSyncTraits GetAppSyncTraits() { 142 return ExtensionSyncTraits(syncable::APPS, 143 &IsValidAndSyncableApp, 144 &IsAppUninstall, 145 "google_chrome_apps", 146 &GetExtensionSpecificsOfApp, 147 &SetExtensionSpecificsOfApp, 148 &GetExtensionSpecificsFromEntityOfApp); 149 } 150 151 } // namespace browser_sync 152