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/extension_sync_bundle.h" 6 7 #include "base/location.h" 8 #include "chrome/browser/extensions/extension_sync_service.h" 9 #include "chrome/browser/extensions/extension_util.h" 10 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/common/extensions/sync_helper.h" 12 #include "extensions/common/extension.h" 13 #include "extensions/common/extension_set.h" 14 #include "sync/api/sync_change_processor.h" 15 #include "sync/api/sync_error_factory.h" 16 17 namespace extensions { 18 19 ExtensionSyncBundle::ExtensionSyncBundle( 20 ExtensionSyncService* extension_sync_service) 21 : extension_sync_service_(extension_sync_service) {} 22 23 ExtensionSyncBundle::~ExtensionSyncBundle() {} 24 25 void ExtensionSyncBundle::SetupSync( 26 syncer::SyncChangeProcessor* sync_processor, 27 syncer::SyncErrorFactory* sync_error_factory, 28 const syncer::SyncDataList& initial_sync_data) { 29 sync_processor_.reset(sync_processor); 30 sync_error_factory_.reset(sync_error_factory); 31 32 for (syncer::SyncDataList::const_iterator i = initial_sync_data.begin(); 33 i != initial_sync_data.end(); 34 ++i) { 35 ExtensionSyncData extension_sync_data(*i); 36 AddExtension(extension_sync_data.id()); 37 extension_sync_service_->ProcessExtensionSyncData(extension_sync_data); 38 } 39 } 40 41 void ExtensionSyncBundle::Reset() { 42 sync_processor_.reset(); 43 sync_error_factory_.reset(); 44 synced_extensions_.clear(); 45 pending_sync_data_.clear(); 46 } 47 48 syncer::SyncChange ExtensionSyncBundle::CreateSyncChangeToDelete( 49 const Extension* extension) const { 50 extensions::ExtensionSyncData sync_data = 51 extension_sync_service_->GetExtensionSyncData(*extension); 52 return sync_data.GetSyncChange(syncer::SyncChange::ACTION_DELETE); 53 } 54 55 void ExtensionSyncBundle::ProcessDeletion( 56 std::string extension_id, const syncer::SyncChange& sync_change) { 57 RemoveExtension(extension_id); 58 sync_processor_->ProcessSyncChanges(FROM_HERE, 59 syncer::SyncChangeList(1, sync_change)); 60 } 61 62 syncer::SyncChange ExtensionSyncBundle::CreateSyncChange( 63 const syncer::SyncData& sync_data) { 64 const syncer::SyncDataLocal sync_data_local(sync_data); 65 if (HasExtensionId(sync_data_local.GetTag())) { 66 return syncer::SyncChange(FROM_HERE, 67 syncer::SyncChange::ACTION_UPDATE, 68 sync_data); 69 } else { 70 AddExtension(sync_data_local.GetTag()); 71 return syncer::SyncChange(FROM_HERE, 72 syncer::SyncChange::ACTION_ADD, 73 sync_data); 74 } 75 } 76 77 syncer::SyncDataList ExtensionSyncBundle::GetAllSyncData() const { 78 std::vector<ExtensionSyncData> extension_sync_data = 79 extension_sync_service_->GetExtensionSyncDataList(); 80 syncer::SyncDataList result(extension_sync_data.size()); 81 for (int i = 0; i < static_cast<int>(extension_sync_data.size()); ++i) { 82 result[i] = extension_sync_data[i].GetSyncData(); 83 } 84 return result; 85 } 86 87 void ExtensionSyncBundle::ProcessSyncChange( 88 ExtensionSyncData extension_sync_data) { 89 if (extension_sync_data.uninstalled()) 90 RemoveExtension(extension_sync_data.id()); 91 else 92 AddExtension(extension_sync_data.id()); 93 extension_sync_service_->ProcessExtensionSyncData(extension_sync_data); 94 } 95 96 void ExtensionSyncBundle::ProcessSyncChangeList( 97 syncer::SyncChangeList sync_change_list) { 98 sync_processor_->ProcessSyncChanges(FROM_HERE, sync_change_list); 99 } 100 101 bool ExtensionSyncBundle::HasExtensionId( 102 const std::string& id) const { 103 return synced_extensions_.find(id) != synced_extensions_.end(); 104 } 105 106 bool ExtensionSyncBundle::HasPendingExtensionId( 107 const std::string& id) const { 108 return pending_sync_data_.find(id) != pending_sync_data_.end(); 109 } 110 111 void ExtensionSyncBundle::AddPendingExtension( 112 const std::string& id, 113 const ExtensionSyncData& extension_sync_data) { 114 pending_sync_data_[id] = extension_sync_data; 115 } 116 117 bool ExtensionSyncBundle::IsSyncing() const { 118 return sync_processor_ != NULL; 119 } 120 121 void ExtensionSyncBundle::SyncChangeIfNeeded(const Extension& extension) { 122 ExtensionSyncData extension_sync_data = 123 extension_sync_service_->GetExtensionSyncData(extension); 124 125 syncer::SyncChangeList sync_change_list(1, extension_sync_data.GetSyncChange( 126 HasExtensionId(extension.id()) ? 127 syncer::SyncChange::ACTION_UPDATE : syncer::SyncChange::ACTION_ADD)); 128 sync_processor_->ProcessSyncChanges(FROM_HERE, sync_change_list); 129 MarkPendingExtensionSynced(extension.id()); 130 } 131 132 std::vector<ExtensionSyncData> ExtensionSyncBundle::GetPendingData() const { 133 std::vector<ExtensionSyncData> pending_extensions; 134 for (std::map<std::string, ExtensionSyncData>::const_iterator 135 i = pending_sync_data_.begin(); 136 i != pending_sync_data_.end(); 137 ++i) { 138 pending_extensions.push_back(i->second); 139 } 140 141 return pending_extensions; 142 } 143 144 void ExtensionSyncBundle::GetExtensionSyncDataListHelper( 145 const ExtensionSet& extensions, 146 std::vector<ExtensionSyncData>* sync_data_list) const { 147 Profile* profile = extension_sync_service_->profile(); 148 149 for (ExtensionSet::const_iterator it = extensions.begin(); 150 it != extensions.end(); ++it) { 151 const Extension& extension = *it->get(); 152 // If we have pending extension data for this extension, then this 153 // version is out of date. We'll sync back the version we got from 154 // sync. 155 if (IsSyncing() && util::ShouldSyncExtension(&extension, profile) && 156 !HasPendingExtensionId(extension.id())) { 157 sync_data_list->push_back( 158 extension_sync_service_->GetExtensionSyncData(extension)); 159 } 160 } 161 } 162 163 void ExtensionSyncBundle::AddExtension(const std::string& id) { 164 synced_extensions_.insert(id); 165 } 166 167 void ExtensionSyncBundle::RemoveExtension(const std::string& id) { 168 synced_extensions_.erase(id); 169 } 170 171 void ExtensionSyncBundle::MarkPendingExtensionSynced(const std::string& id) { 172 pending_sync_data_.erase(id); 173 synced_extensions_.insert(id); 174 } 175 176 } // namespace extensions 177