| OLD | NEW |
| (Empty) |
| 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 | |
| 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/glue/extension_util.h" | |
| 10 #include "chrome/browser/sync/internal_api/write_node.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 const char* root_node_tag, | |
| 21 ExtensionSpecificsGetter extension_specifics_getter, | |
| 22 ExtensionSpecificsSetter extension_specifics_setter, | |
| 23 ExtensionSpecificsEntityGetter extension_specifics_entity_getter) | |
| 24 : model_type(model_type), | |
| 25 is_valid_and_syncable(is_valid_and_syncable), | |
| 26 root_node_tag(root_node_tag), | |
| 27 extension_specifics_getter(extension_specifics_getter), | |
| 28 extension_specifics_setter(extension_specifics_setter), | |
| 29 extension_specifics_entity_getter(extension_specifics_entity_getter) {} | |
| 30 | |
| 31 ExtensionSyncTraits::~ExtensionSyncTraits() {} | |
| 32 | |
| 33 namespace { | |
| 34 | |
| 35 const sync_pb::ExtensionSpecifics& GetExtensionSpecifics( | |
| 36 const sync_api::BaseNode& node) { | |
| 37 return node.GetExtensionSpecifics(); | |
| 38 } | |
| 39 | |
| 40 void SetExtensionSpecifics( | |
| 41 const sync_pb::ExtensionSpecifics& extension_specifics, | |
| 42 sync_api::WriteNode* node) { | |
| 43 node->SetTitle(UTF8ToWide(extension_specifics.name())); | |
| 44 node->SetExtensionSpecifics(extension_specifics); | |
| 45 } | |
| 46 | |
| 47 bool GetExtensionSpecificsFromEntity( | |
| 48 const sync_pb::EntitySpecifics& entity_specifics, | |
| 49 sync_pb::ExtensionSpecifics* extension_specifics) { | |
| 50 if (!entity_specifics.HasExtension(sync_pb::extension)) { | |
| 51 return false; | |
| 52 } | |
| 53 *extension_specifics = entity_specifics.GetExtension(sync_pb::extension); | |
| 54 return true; | |
| 55 } | |
| 56 | |
| 57 bool IsSyncableExtension(Extension::Type type, const GURL& update_url) { | |
| 58 switch (type) { | |
| 59 case Extension::TYPE_EXTENSION: | |
| 60 return true; | |
| 61 case Extension::TYPE_USER_SCRIPT: | |
| 62 // We only want to sync user scripts with update URLs. | |
| 63 return !update_url.is_empty(); | |
| 64 default: | |
| 65 return false; | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 bool IsValidAndSyncableExtension(const Extension& extension) { | |
| 70 return | |
| 71 IsExtensionValid(extension) && | |
| 72 IsSyncableExtension(extension.GetType(), extension.update_url()); | |
| 73 } | |
| 74 | |
| 75 } // namespace | |
| 76 | |
| 77 ExtensionSyncTraits GetExtensionSyncTraits() { | |
| 78 return ExtensionSyncTraits(syncable::EXTENSIONS, | |
| 79 &IsValidAndSyncableExtension, | |
| 80 "google_chrome_extensions", | |
| 81 &GetExtensionSpecifics, | |
| 82 &SetExtensionSpecifics, | |
| 83 &GetExtensionSpecificsFromEntity); | |
| 84 } | |
| 85 | |
| 86 namespace { | |
| 87 | |
| 88 const sync_pb::ExtensionSpecifics& GetExtensionSpecificsOfApp( | |
| 89 const sync_api::BaseNode& node) { | |
| 90 return node.GetAppSpecifics().extension(); | |
| 91 } | |
| 92 | |
| 93 void SetExtensionSpecificsOfApp( | |
| 94 const sync_pb::ExtensionSpecifics& extension_specifics, | |
| 95 sync_api::WriteNode* node) { | |
| 96 node->SetTitle(UTF8ToWide(extension_specifics.name())); | |
| 97 sync_pb::AppSpecifics app_specifics; | |
| 98 *app_specifics.mutable_extension() = extension_specifics; | |
| 99 node->SetAppSpecifics(app_specifics); | |
| 100 } | |
| 101 | |
| 102 bool GetExtensionSpecificsFromEntityOfApp( | |
| 103 const sync_pb::EntitySpecifics& entity_specifics, | |
| 104 sync_pb::ExtensionSpecifics* extension_specifics) { | |
| 105 if (!entity_specifics.HasExtension(sync_pb::app)) { | |
| 106 return false; | |
| 107 } | |
| 108 *extension_specifics = | |
| 109 entity_specifics.GetExtension(sync_pb::app).extension(); | |
| 110 return true; | |
| 111 } | |
| 112 | |
| 113 bool IsSyncableApp(Extension::Type type) { | |
| 114 return | |
| 115 (type == Extension::TYPE_HOSTED_APP) || | |
| 116 (type == Extension::TYPE_PACKAGED_APP); | |
| 117 } | |
| 118 | |
| 119 bool IsValidAndSyncableApp( | |
| 120 const Extension& extension) { | |
| 121 return IsExtensionValid(extension) && IsSyncableApp(extension.GetType()); | |
| 122 } | |
| 123 | |
| 124 } // namespace | |
| 125 | |
| 126 ExtensionSyncTraits GetAppSyncTraits() { | |
| 127 return ExtensionSyncTraits(syncable::APPS, | |
| 128 &IsValidAndSyncableApp, | |
| 129 "google_chrome_apps", | |
| 130 &GetExtensionSpecificsOfApp, | |
| 131 &SetExtensionSpecificsOfApp, | |
| 132 &GetExtensionSpecificsFromEntityOfApp); | |
| 133 } | |
| 134 | |
| 135 } // namespace browser_sync | |
| OLD | NEW |