1 // Copyright 2013 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/pending_enables.h" 6 7 #include "chrome/browser/extensions/extension_service.h" 8 #include "chrome/browser/extensions/sync_bundle.h" 9 #include "chrome/browser/sync/sync_prefs.h" 10 11 namespace extensions { 12 13 PendingEnables::PendingEnables(scoped_ptr<browser_sync::SyncPrefs> sync_prefs, 14 SyncBundle* sync_bundle, 15 syncer::ModelType enable_type) 16 : sync_prefs_(sync_prefs.Pass()), 17 sync_bundle_(sync_bundle), 18 enable_type_(enable_type), 19 is_sync_enabled_for_test_(false) { 20 } 21 22 PendingEnables::~PendingEnables() { 23 } 24 25 void PendingEnables::OnExtensionEnabled(const std::string& extension_id) { 26 if (IsWaitingForSync()) 27 ids_.insert(extension_id); 28 } 29 30 void PendingEnables::OnExtensionDisabled(const std::string& extension_id) { 31 if (IsWaitingForSync()) 32 ids_.erase(extension_id); 33 } 34 35 void PendingEnables::OnSyncStarted(ExtensionService* service) { 36 for (std::set<std::string>::const_iterator it = ids_.begin(); 37 it != ids_.end(); ++it) { 38 const Extension* extension = service->GetExtensionById(*it, true); 39 if (extension) 40 sync_bundle_->SyncChangeIfNeeded(*extension); 41 } 42 ids_.clear(); 43 } 44 45 bool PendingEnables::Contains(const std::string& extension_id) const { 46 return ids_.find(extension_id) != ids_.end(); 47 } 48 49 bool PendingEnables::IsSyncEnabled() { 50 if (is_sync_enabled_for_test_) 51 return true; 52 return sync_prefs_ && 53 sync_prefs_->HasSyncSetupCompleted() && 54 sync_prefs_->GetPreferredDataTypes(syncer::ModelTypeSet(enable_type_)) 55 .Has(enable_type_); 56 } 57 58 bool PendingEnables::IsWaitingForSync() { 59 return IsSyncEnabled() && !sync_bundle_->IsSyncing(); 60 } 61 62 } // namespace extensions 63 64