| 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/extension_sync_data.h" | 5 #include "chrome/browser/extensions/extension_sync_data.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" | |
| 9 #include "chrome/browser/sync/protocol/app_specifics.pb.h" | |
| 10 #include "chrome/browser/sync/protocol/extension_specifics.pb.h" | |
| 11 | 8 |
| 12 ExtensionSyncData::ExtensionSyncData() | 9 ExtensionSyncData::ExtensionSyncData() |
| 13 : uninstalled_(false), | 10 : uninstalled(false), enabled(false), incognito_enabled(false) {} |
| 14 enabled_(false), | |
| 15 incognito_enabled_(false), | |
| 16 type_(Extension::SYNC_TYPE_NONE) { | |
| 17 } | |
| 18 | |
| 19 ExtensionSyncData::ExtensionSyncData(const SyncData& sync_data) | |
| 20 : uninstalled_(false), | |
| 21 enabled_(false), | |
| 22 incognito_enabled_(false), | |
| 23 type_(Extension::SYNC_TYPE_NONE) { | |
| 24 PopulateFromSyncData(sync_data); | |
| 25 } | |
| 26 | |
| 27 ExtensionSyncData::ExtensionSyncData(const SyncChange& sync_change) | |
| 28 : uninstalled_(sync_change.change_type() == SyncChange::ACTION_DELETE) { | |
| 29 PopulateFromSyncData(sync_change.sync_data()); | |
| 30 } | |
| 31 | |
| 32 ExtensionSyncData::ExtensionSyncData(const Extension& extension, | |
| 33 bool enabled, | |
| 34 bool incognito_enabled) | |
| 35 : id_(extension.id()), | |
| 36 uninstalled_(false), | |
| 37 enabled_(enabled), | |
| 38 incognito_enabled_(incognito_enabled), | |
| 39 type_(extension.GetSyncType()), | |
| 40 version_(*extension.version()), | |
| 41 update_url_(extension.update_url()), | |
| 42 name_(extension.name()) { | |
| 43 } | |
| 44 | 11 |
| 45 ExtensionSyncData::~ExtensionSyncData() {} | 12 ExtensionSyncData::~ExtensionSyncData() {} |
| 46 | 13 |
| 47 void ExtensionSyncData::PopulateSyncSpecifics( | 14 void ExtensionSyncData::Merge(const ExtensionSyncData& new_data) { |
| 48 sync_pb::ExtensionSpecifics* specifics) const { | 15 CHECK_EQ(id, new_data.id); |
| 49 DCHECK(Extension::IdIsValid(id_)); | 16 CHECK(!uninstalled); |
| 50 specifics->set_id(id_); | 17 CHECK(!new_data.uninstalled); |
| 51 specifics->set_update_url(update_url_.spec()); | 18 |
| 52 specifics->set_version(version_.GetString()); | 19 // Copy version-independent properties. |
| 53 specifics->set_enabled(enabled_); | 20 enabled = new_data.enabled; |
| 54 specifics->set_incognito_enabled(incognito_enabled_); | 21 incognito_enabled = new_data.incognito_enabled; |
| 55 specifics->set_name(name_); | 22 |
| 23 // Copy version-dependent properties if version <= new_data.version. |
| 24 if (version.CompareTo(new_data.version) <= 0) { |
| 25 version = new_data.version; |
| 26 update_url = new_data.update_url; |
| 27 name = new_data.name; |
| 28 } |
| 56 } | 29 } |
| 57 | |
| 58 SyncData ExtensionSyncData::GetSyncData() const { | |
| 59 sync_pb::EntitySpecifics specifics; | |
| 60 sync_pb::ExtensionSpecifics* ext_specifics; | |
| 61 | |
| 62 switch (type_) { | |
| 63 case Extension::SYNC_TYPE_EXTENSION: | |
| 64 ext_specifics = specifics.MutableExtension(sync_pb::extension); | |
| 65 break; | |
| 66 case Extension::SYNC_TYPE_APP: | |
| 67 ext_specifics = | |
| 68 specifics.MutableExtension(sync_pb::app)->mutable_extension(); | |
| 69 break; | |
| 70 default: | |
| 71 LOG(FATAL) << "Attempt to get non-syncable data."; | |
| 72 } | |
| 73 | |
| 74 PopulateSyncSpecifics(ext_specifics); | |
| 75 | |
| 76 return SyncData::CreateLocalData(id_, name_, specifics); | |
| 77 } | |
| 78 | |
| 79 SyncChange ExtensionSyncData::GetSyncChange( | |
| 80 SyncChange::SyncChangeType change_type) const { | |
| 81 return SyncChange(change_type, GetSyncData()); | |
| 82 } | |
| 83 | |
| 84 void ExtensionSyncData::PopulateFromExtensionSpecifics( | |
| 85 const sync_pb::ExtensionSpecifics& specifics) { | |
| 86 if (!Extension::IdIsValid(specifics.id())) { | |
| 87 LOG(FATAL) << "Attempt to sync bad ExtensionSpecifics."; | |
| 88 } | |
| 89 | |
| 90 scoped_ptr<Version> specifics_version( | |
| 91 Version::GetVersionFromString(specifics.version())); | |
| 92 if (!specifics_version.get()) { | |
| 93 LOG(FATAL) << "Attempt to sync bad ExtensionSpecifics."; | |
| 94 } | |
| 95 | |
| 96 // The update URL must be either empty or valid. | |
| 97 GURL specifics_update_url(specifics.update_url()); | |
| 98 if (!specifics_update_url.is_empty() && !specifics_update_url.is_valid()) { | |
| 99 LOG(FATAL) << "Attempt to sync bad ExtensionSpecifics."; | |
| 100 } | |
| 101 | |
| 102 id_ = specifics.id(); | |
| 103 update_url_ = specifics_update_url; | |
| 104 version_ = *specifics_version; | |
| 105 enabled_ = specifics.enabled(); | |
| 106 incognito_enabled_ = specifics.incognito_enabled(); | |
| 107 name_ = specifics.name(); | |
| 108 } | |
| 109 | |
| 110 void ExtensionSyncData::PopulateFromSyncData(const SyncData& sync_data) { | |
| 111 const sync_pb::EntitySpecifics& entity_specifics = sync_data.GetSpecifics(); | |
| 112 sync_pb::ExtensionSpecifics extension_expecifics; | |
| 113 if (entity_specifics.HasExtension(sync_pb::extension)) { | |
| 114 extension_expecifics = entity_specifics.GetExtension(sync_pb::extension); | |
| 115 type_ = Extension::SYNC_TYPE_EXTENSION; | |
| 116 } else if (entity_specifics.HasExtension(sync_pb::app)) { | |
| 117 extension_expecifics = | |
| 118 entity_specifics.GetExtension(sync_pb::app).extension(); | |
| 119 type_ = Extension::SYNC_TYPE_APP; | |
| 120 } else { | |
| 121 LOG(FATAL) << "Attempt to sync bad EntitySpecifics."; | |
| 122 } | |
| 123 PopulateFromExtensionSpecifics(extension_expecifics); | |
| 124 } | |
| 125 | |
| OLD | NEW |