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