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