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/sync/test_profile_sync_service.h" 6 7 #include "chrome/browser/chrome_notification_types.h" 8 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 9 #include "chrome/browser/signin/signin_manager.h" 10 #include "chrome/browser/signin/signin_manager_factory.h" 11 #include "chrome/browser/sync/glue/sync_backend_host.h" 12 #include "chrome/browser/sync/glue/sync_backend_host_core.h" 13 #include "chrome/browser/sync/profile_sync_components_factory.h" 14 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" 15 #include "chrome/browser/sync/test/test_http_bridge_factory.h" 16 #include "sync/internal_api/public/test/sync_manager_factory_for_profile_sync_test.h" 17 #include "sync/internal_api/public/test/test_internal_components_factory.h" 18 #include "sync/internal_api/public/user_share.h" 19 #include "sync/js/js_reply_handler.h" 20 #include "sync/protocol/encryption.pb.h" 21 22 using syncer::InternalComponentsFactory; 23 using syncer::TestInternalComponentsFactory; 24 using syncer::UserShare; 25 26 namespace browser_sync { 27 28 SyncBackendHostForProfileSyncTest::SyncBackendHostForProfileSyncTest( 29 Profile* profile, 30 const base::WeakPtr<SyncPrefs>& sync_prefs, 31 base::Closure& callback) 32 : browser_sync::SyncBackendHostImpl( 33 profile->GetDebugName(), profile, sync_prefs), 34 callback_(callback) {} 35 36 SyncBackendHostForProfileSyncTest::~SyncBackendHostForProfileSyncTest() {} 37 38 void SyncBackendHostForProfileSyncTest::InitCore( 39 scoped_ptr<DoInitializeOptions> options) { 40 options->http_bridge_factory = 41 scoped_ptr<syncer::HttpPostProviderFactory>( 42 new browser_sync::TestHttpBridgeFactory()); 43 options->sync_manager_factory.reset( 44 new syncer::SyncManagerFactoryForProfileSyncTest(callback_)); 45 options->credentials.email = "testuser (at) gmail.com"; 46 options->credentials.sync_token = "token"; 47 options->restored_key_for_bootstrapping = ""; 48 49 // It'd be nice if we avoided creating the InternalComponentsFactory in the 50 // first place, but SyncBackendHost will have created one by now so we must 51 // free it. Grab the switches to pass on first. 52 InternalComponentsFactory::Switches factory_switches = 53 options->internal_components_factory->GetSwitches(); 54 options->internal_components_factory.reset( 55 new TestInternalComponentsFactory(factory_switches, 56 syncer::STORAGE_IN_MEMORY)); 57 58 SyncBackendHostImpl::InitCore(options.Pass()); 59 } 60 61 void SyncBackendHostForProfileSyncTest::RequestConfigureSyncer( 62 syncer::ConfigureReason reason, 63 syncer::ModelTypeSet to_download, 64 syncer::ModelTypeSet to_purge, 65 syncer::ModelTypeSet to_journal, 66 syncer::ModelTypeSet to_unapply, 67 syncer::ModelTypeSet to_ignore, 68 const syncer::ModelSafeRoutingInfo& routing_info, 69 const base::Callback<void(syncer::ModelTypeSet, 70 syncer::ModelTypeSet)>& ready_task, 71 const base::Closure& retry_callback) { 72 syncer::ModelTypeSet failed_configuration_types; 73 74 // The first parameter there should be the set of enabled types. That's not 75 // something we have access to from this strange test harness. We'll just 76 // send back the list of newly configured types instead and hope it doesn't 77 // break anything. 78 FinishConfigureDataTypesOnFrontendLoop( 79 syncer::Difference(to_download, failed_configuration_types), 80 syncer::Difference(to_download, failed_configuration_types), 81 failed_configuration_types, 82 ready_task); 83 } 84 85 } // namespace browser_sync 86 87 syncer::TestIdFactory* TestProfileSyncService::id_factory() { 88 return &id_factory_; 89 } 90 91 syncer::WeakHandle<syncer::JsEventHandler> 92 TestProfileSyncService::GetJsEventHandler() { 93 return syncer::WeakHandle<syncer::JsEventHandler>(); 94 } 95 96 TestProfileSyncService::TestProfileSyncService( 97 ProfileSyncComponentsFactory* factory, 98 Profile* profile, 99 SigninManagerBase* signin, 100 ProfileOAuth2TokenService* oauth2_token_service, 101 ProfileSyncService::StartBehavior behavior) 102 : ProfileSyncService(factory, 103 profile, 104 signin, 105 oauth2_token_service, 106 behavior) { 107 SetSyncSetupCompleted(); 108 } 109 110 TestProfileSyncService::~TestProfileSyncService() { 111 } 112 113 // static 114 BrowserContextKeyedService* TestProfileSyncService::BuildAutoStartAsyncInit( 115 content::BrowserContext* context) { 116 Profile* profile = static_cast<Profile*>(context); 117 SigninManagerBase* signin = 118 SigninManagerFactory::GetForProfile(profile); 119 ProfileOAuth2TokenService* oauth2_token_service = 120 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); 121 ProfileSyncComponentsFactoryMock* factory = 122 new ProfileSyncComponentsFactoryMock(); 123 return new TestProfileSyncService(factory, 124 profile, 125 signin, 126 oauth2_token_service, 127 ProfileSyncService::AUTO_START); 128 } 129 130 ProfileSyncComponentsFactoryMock* 131 TestProfileSyncService::components_factory_mock() { 132 // We always create a mock factory, see Build* routines. 133 return static_cast<ProfileSyncComponentsFactoryMock*>(factory()); 134 } 135 136 void TestProfileSyncService::OnConfigureDone( 137 const browser_sync::DataTypeManager::ConfigureResult& result) { 138 ProfileSyncService::OnConfigureDone(result); 139 base::MessageLoop::current()->Quit(); 140 } 141 142 UserShare* TestProfileSyncService::GetUserShare() const { 143 return backend_->GetUserShare(); 144 } 145 146 void TestProfileSyncService::CreateBackend() { 147 backend_.reset(new browser_sync::SyncBackendHostForProfileSyncTest( 148 profile(), 149 sync_prefs_.AsWeakPtr(), 150 callback_)); 151 } 152 153