| 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/sync/glue/extension_util.h" | 5 #include "chrome/browser/sync/glue/extension_util.h" |
| 6 | 6 |
| 7 #include <sstream> | 7 #include <sstream> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/version.h" | 11 #include "base/version.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" | |
| 13 #include "chrome/browser/extensions/extension_sync_data.h" | 12 #include "chrome/browser/extensions/extension_sync_data.h" |
| 14 #include "chrome/browser/sync/protocol/extension_specifics.pb.h" | 13 #include "chrome/browser/sync/protocol/extension_specifics.pb.h" |
| 15 #include "chrome/common/extensions/extension.h" | 14 #include "chrome/common/extensions/extension.h" |
| 16 #include "chrome/common/extensions/extension_constants.h" | 15 #include "chrome/common/extensions/extension_constants.h" |
| 17 | 16 |
| 18 namespace browser_sync { | 17 namespace browser_sync { |
| 19 | 18 |
| 20 bool IsExtensionValid(const Extension& extension) { | 19 bool IsExtensionValid(const Extension& extension) { |
| 21 // TODO(akalin): Figure out if we need to allow some other types. | 20 // TODO(akalin): Figure out if we need to allow some other types. |
| 22 if (extension.location() != Extension::INTERNAL) { | 21 if (extension.location() != Extension::INTERNAL) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 52 ss << "id: " << specifics.id() << ", "; | 51 ss << "id: " << specifics.id() << ", "; |
| 53 ss << "version: " << specifics.version() << ", "; | 52 ss << "version: " << specifics.version() << ", "; |
| 54 ss << "update_url: " << specifics.update_url() << ", "; | 53 ss << "update_url: " << specifics.update_url() << ", "; |
| 55 ss << "enabled: " << specifics.enabled() << ", "; | 54 ss << "enabled: " << specifics.enabled() << ", "; |
| 56 ss << "incognito_enabled: " << specifics.incognito_enabled() << ", "; | 55 ss << "incognito_enabled: " << specifics.incognito_enabled() << ", "; |
| 57 ss << "name: " << specifics.name(); | 56 ss << "name: " << specifics.name(); |
| 58 ss << " }"; | 57 ss << " }"; |
| 59 return ss.str(); | 58 return ss.str(); |
| 60 } | 59 } |
| 61 | 60 |
| 62 bool IsExtensionSpecificsValid( | |
| 63 const sync_pb::ExtensionSpecifics& specifics) { | |
| 64 if (!Extension::IdIsValid(specifics.id())) { | |
| 65 return false; | |
| 66 } | |
| 67 | |
| 68 scoped_ptr<Version> version( | |
| 69 Version::GetVersionFromString(specifics.version())); | |
| 70 if (!version.get()) { | |
| 71 return false; | |
| 72 } | |
| 73 | |
| 74 // The update URL must be either empty or valid. | |
| 75 GURL update_url(specifics.update_url()); | |
| 76 if (!update_url.is_empty() && !update_url.is_valid()) { | |
| 77 return false; | |
| 78 } | |
| 79 | |
| 80 return true; | |
| 81 } | |
| 82 | |
| 83 void DcheckIsExtensionSpecificsValid( | |
| 84 const sync_pb::ExtensionSpecifics& specifics) { | |
| 85 DCHECK(IsExtensionSpecificsValid(specifics)) | |
| 86 << ExtensionSpecificsToString(specifics); | |
| 87 } | |
| 88 | |
| 89 bool AreExtensionSpecificsEqual(const sync_pb::ExtensionSpecifics& a, | |
| 90 const sync_pb::ExtensionSpecifics& b) { | |
| 91 // TODO(akalin): Figure out if we have to worry about version/URL | |
| 92 // strings that are not identical but map to the same object. | |
| 93 return ((a.id() == b.id()) && | |
| 94 (a.version() == b.version()) && | |
| 95 (a.update_url() == b.update_url()) && | |
| 96 (a.enabled() == b.enabled()) && | |
| 97 (a.incognito_enabled() == b.incognito_enabled()) && | |
| 98 (a.name() == b.name())); | |
| 99 } | |
| 100 | |
| 101 bool IsExtensionSpecificsUnset( | |
| 102 const sync_pb::ExtensionSpecifics& specifics) { | |
| 103 return AreExtensionSpecificsEqual(specifics, | |
| 104 sync_pb::ExtensionSpecifics()); | |
| 105 } | |
| 106 | |
| 107 void CopyUserProperties( | |
| 108 const sync_pb::ExtensionSpecifics& specifics, | |
| 109 sync_pb::ExtensionSpecifics* dest_specifics) { | |
| 110 DCHECK(dest_specifics); | |
| 111 dest_specifics->set_enabled(specifics.enabled()); | |
| 112 dest_specifics->set_incognito_enabled(specifics.incognito_enabled()); | |
| 113 } | |
| 114 | |
| 115 void CopyNonUserProperties( | |
| 116 const sync_pb::ExtensionSpecifics& specifics, | |
| 117 sync_pb::ExtensionSpecifics* dest_specifics) { | |
| 118 DCHECK(dest_specifics); | |
| 119 sync_pb::ExtensionSpecifics old_dest_specifics(*dest_specifics); | |
| 120 *dest_specifics = specifics; | |
| 121 CopyUserProperties(old_dest_specifics, dest_specifics); | |
| 122 } | |
| 123 | |
| 124 bool AreExtensionSpecificsUserPropertiesEqual( | |
| 125 const sync_pb::ExtensionSpecifics& a, | |
| 126 const sync_pb::ExtensionSpecifics& b) { | |
| 127 sync_pb::ExtensionSpecifics a_user_properties, b_user_properties; | |
| 128 CopyUserProperties(a, &a_user_properties); | |
| 129 CopyUserProperties(b, &b_user_properties); | |
| 130 return AreExtensionSpecificsEqual(a_user_properties, b_user_properties); | |
| 131 } | |
| 132 | |
| 133 bool AreExtensionSpecificsNonUserPropertiesEqual( | |
| 134 const sync_pb::ExtensionSpecifics& a, | |
| 135 const sync_pb::ExtensionSpecifics& b) { | |
| 136 sync_pb::ExtensionSpecifics a_non_user_properties, b_non_user_properties; | |
| 137 CopyNonUserProperties(a, &a_non_user_properties); | |
| 138 CopyNonUserProperties(b, &b_non_user_properties); | |
| 139 return AreExtensionSpecificsEqual( | |
| 140 a_non_user_properties, b_non_user_properties); | |
| 141 } | |
| 142 | |
| 143 void MergeExtensionSpecifics( | |
| 144 const sync_pb::ExtensionSpecifics& specifics, | |
| 145 bool merge_user_properties, | |
| 146 sync_pb::ExtensionSpecifics* merged_specifics) { | |
| 147 DcheckIsExtensionSpecificsValid(*merged_specifics); | |
| 148 DcheckIsExtensionSpecificsValid(specifics); | |
| 149 DCHECK_EQ(specifics.id(), merged_specifics->id()); | |
| 150 // TODO(akalin): Merge enabled permissions when we sync those. | |
| 151 scoped_ptr<Version> version( | |
| 152 Version::GetVersionFromString(specifics.version())); | |
| 153 CHECK(version.get()); | |
| 154 scoped_ptr<Version> merged_version( | |
| 155 Version::GetVersionFromString(merged_specifics->version())); | |
| 156 CHECK(merged_version.get()); | |
| 157 if (version->CompareTo(*merged_version) >= 0) { | |
| 158 // |specifics| has a more recent or the same version, so merge it | |
| 159 // in. | |
| 160 CopyNonUserProperties(specifics, merged_specifics); | |
| 161 if (merge_user_properties) { | |
| 162 CopyUserProperties(specifics, merged_specifics); | |
| 163 } | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 bool SpecificsToSyncData( | 61 bool SpecificsToSyncData( |
| 168 const sync_pb::ExtensionSpecifics& specifics, | 62 const sync_pb::ExtensionSpecifics& specifics, |
| 169 ExtensionSyncData* sync_data) { | 63 ExtensionSyncData* sync_data) { |
| 170 if (!Extension::IdIsValid(specifics.id())) { | 64 if (!Extension::IdIsValid(specifics.id())) { |
| 171 return false; | 65 return false; |
| 172 } | 66 } |
| 173 | 67 |
| 174 scoped_ptr<Version> version( | 68 scoped_ptr<Version> version( |
| 175 Version::GetVersionFromString(specifics.version())); | 69 Version::GetVersionFromString(specifics.version())); |
| 176 if (!version.get()) { | 70 if (!version.get()) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 199 DCHECK(!sync_data.uninstalled); | 93 DCHECK(!sync_data.uninstalled); |
| 200 specifics->set_id(sync_data.id); | 94 specifics->set_id(sync_data.id); |
| 201 specifics->set_update_url(sync_data.update_url.spec()); | 95 specifics->set_update_url(sync_data.update_url.spec()); |
| 202 specifics->set_version(sync_data.version.GetString()); | 96 specifics->set_version(sync_data.version.GetString()); |
| 203 specifics->set_enabled(sync_data.enabled); | 97 specifics->set_enabled(sync_data.enabled); |
| 204 specifics->set_incognito_enabled(sync_data.incognito_enabled); | 98 specifics->set_incognito_enabled(sync_data.incognito_enabled); |
| 205 specifics->set_name(sync_data.name); | 99 specifics->set_name(sync_data.name); |
| 206 } | 100 } |
| 207 | 101 |
| 208 } // namespace browser_sync | 102 } // namespace browser_sync |
| OLD | NEW |