OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sync/test_profile_sync_service.h" | 5 #include "chrome/browser/sync/test_profile_sync_service.h" |
6 | 6 |
7 #include "chrome/browser/chrome_notification_types.h" | 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" | 8 #include "chrome/browser/signin/signin_manager.h" |
10 #include "chrome/browser/signin/signin_manager_factory.h" | 9 #include "chrome/browser/signin/signin_manager_factory.h" |
11 #include "chrome/browser/sync/glue/sync_backend_host.h" | 10 #include "chrome/browser/sync/glue/sync_backend_host.h" |
12 #include "chrome/browser/sync/glue/sync_backend_host_core.h" | 11 #include "chrome/browser/sync/glue/sync_backend_host_core.h" |
13 #include "chrome/browser/sync/profile_sync_components_factory.h" | 12 #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" | 13 #include "chrome/browser/sync/test/test_http_bridge_factory.h" |
16 #include "sync/internal_api/public/test/sync_manager_factory_for_profile_sync_te
st.h" | 14 #include "sync/internal_api/public/test/sync_manager_factory_for_profile_sync_te
st.h" |
17 #include "sync/internal_api/public/test/test_internal_components_factory.h" | |
18 #include "sync/internal_api/public/user_share.h" | 15 #include "sync/internal_api/public/user_share.h" |
19 #include "sync/js/js_reply_handler.h" | 16 #include "sync/js/js_reply_handler.h" |
20 #include "sync/protocol/encryption.pb.h" | 17 #include "sync/protocol/encryption.pb.h" |
21 | 18 |
22 using syncer::InternalComponentsFactory; | 19 using syncer::InternalComponentsFactory; |
| 20 using syncer::ModelSafeRoutingInfo; |
23 using syncer::TestInternalComponentsFactory; | 21 using syncer::TestInternalComponentsFactory; |
24 using syncer::UserShare; | 22 using syncer::UserShare; |
25 | 23 |
26 namespace browser_sync { | 24 namespace browser_sync { |
27 | 25 |
28 SyncBackendHostForProfileSyncTest::SyncBackendHostForProfileSyncTest( | 26 SyncBackendHostForProfileSyncTest::SyncBackendHostForProfileSyncTest( |
29 Profile* profile, | 27 Profile* profile, |
30 const base::WeakPtr<SyncPrefs>& sync_prefs, | 28 const base::WeakPtr<SyncPrefs>& sync_prefs, |
31 base::Closure& callback) | 29 base::Closure& callback, |
| 30 bool set_initial_sync_ended_on_init, |
| 31 bool synchronous_init, |
| 32 bool fail_initial_download, |
| 33 syncer::StorageOption storage_option) |
32 : browser_sync::SyncBackendHostImpl( | 34 : browser_sync::SyncBackendHostImpl( |
33 profile->GetDebugName(), profile, sync_prefs), | 35 profile->GetDebugName(), profile, sync_prefs), |
34 callback_(callback) {} | 36 callback_(callback), |
| 37 fail_initial_download_(fail_initial_download), |
| 38 set_initial_sync_ended_on_init_(set_initial_sync_ended_on_init), |
| 39 synchronous_init_(synchronous_init), |
| 40 storage_option_(storage_option), |
| 41 weak_ptr_factory_(this) {} |
35 | 42 |
36 SyncBackendHostForProfileSyncTest::~SyncBackendHostForProfileSyncTest() {} | 43 SyncBackendHostForProfileSyncTest::~SyncBackendHostForProfileSyncTest() {} |
37 | 44 |
| 45 namespace { |
| 46 |
| 47 scoped_ptr<syncer::HttpPostProviderFactory> MakeTestHttpBridgeFactory() { |
| 48 return scoped_ptr<syncer::HttpPostProviderFactory>( |
| 49 new browser_sync::TestHttpBridgeFactory()); |
| 50 } |
| 51 |
| 52 } // namespace |
| 53 |
38 void SyncBackendHostForProfileSyncTest::InitCore( | 54 void SyncBackendHostForProfileSyncTest::InitCore( |
39 scoped_ptr<DoInitializeOptions> options) { | 55 scoped_ptr<DoInitializeOptions> options) { |
40 options->http_bridge_factory = | 56 options->http_bridge_factory = MakeTestHttpBridgeFactory(); |
41 scoped_ptr<syncer::HttpPostProviderFactory>( | |
42 new browser_sync::TestHttpBridgeFactory()); | |
43 options->sync_manager_factory.reset( | 57 options->sync_manager_factory.reset( |
44 new syncer::SyncManagerFactoryForProfileSyncTest(callback_)); | 58 new syncer::SyncManagerFactoryForProfileSyncTest( |
| 59 callback_, |
| 60 set_initial_sync_ended_on_init_)); |
45 options->credentials.email = "testuser@gmail.com"; | 61 options->credentials.email = "testuser@gmail.com"; |
46 options->credentials.sync_token = "token"; | 62 options->credentials.sync_token = "token"; |
47 options->restored_key_for_bootstrapping = ""; | 63 options->restored_key_for_bootstrapping = ""; |
| 64 syncer::StorageOption storage = storage_option_; |
48 | 65 |
49 // It'd be nice if we avoided creating the InternalComponentsFactory in the | 66 // 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 | 67 // first place, but SyncBackendHost will have created one by now so we must |
51 // free it. Grab the switches to pass on first. | 68 // free it. Grab the switches to pass on first. |
52 InternalComponentsFactory::Switches factory_switches = | 69 InternalComponentsFactory::Switches factory_switches = |
53 options->internal_components_factory->GetSwitches(); | 70 options->internal_components_factory->GetSwitches(); |
54 options->internal_components_factory.reset( | 71 options->internal_components_factory.reset( |
55 new TestInternalComponentsFactory(factory_switches, | 72 new TestInternalComponentsFactory(factory_switches, storage)); |
56 syncer::STORAGE_IN_MEMORY)); | |
57 | 73 |
58 SyncBackendHostImpl::InitCore(options.Pass()); | 74 SyncBackendHostImpl::InitCore(options.Pass()); |
| 75 if (synchronous_init_ && !base::MessageLoop::current()->is_running()) { |
| 76 // The SyncBackend posts a task to the current loop when |
| 77 // initialization completes. |
| 78 base::MessageLoop::current()->Run(); |
| 79 } |
| 80 } |
| 81 |
| 82 void SyncBackendHostForProfileSyncTest::UpdateCredentials( |
| 83 const syncer::SyncCredentials& credentials) { |
| 84 // If we had failed the initial download, complete initialization now. |
| 85 if (!initial_download_closure_.is_null()) { |
| 86 initial_download_closure_.Run(); |
| 87 initial_download_closure_.Reset(); |
| 88 } |
59 } | 89 } |
60 | 90 |
61 void SyncBackendHostForProfileSyncTest::RequestConfigureSyncer( | 91 void SyncBackendHostForProfileSyncTest::RequestConfigureSyncer( |
62 syncer::ConfigureReason reason, | 92 syncer::ConfigureReason reason, |
63 syncer::ModelTypeSet to_download, | 93 syncer::ModelTypeSet to_download, |
64 syncer::ModelTypeSet to_purge, | 94 syncer::ModelTypeSet to_purge, |
65 syncer::ModelTypeSet to_journal, | 95 syncer::ModelTypeSet to_journal, |
66 syncer::ModelTypeSet to_unapply, | 96 syncer::ModelTypeSet to_unapply, |
67 syncer::ModelTypeSet to_ignore, | 97 syncer::ModelTypeSet to_ignore, |
68 const syncer::ModelSafeRoutingInfo& routing_info, | 98 const syncer::ModelSafeRoutingInfo& routing_info, |
69 const base::Callback<void(syncer::ModelTypeSet, | 99 const base::Callback<void(syncer::ModelTypeSet, |
70 syncer::ModelTypeSet)>& ready_task, | 100 syncer::ModelTypeSet)>& ready_task, |
71 const base::Closure& retry_callback) { | 101 const base::Closure& retry_callback) { |
72 syncer::ModelTypeSet failed_configuration_types; | 102 syncer::ModelTypeSet failed_configuration_types; |
| 103 if (fail_initial_download_) |
| 104 failed_configuration_types = to_download; |
73 | 105 |
74 // The first parameter there should be the set of enabled types. That's not | 106 // 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 | 107 // 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 | 108 // send back the list of newly configured types instead and hope it doesn't |
77 // break anything. | 109 // break anything. |
78 FinishConfigureDataTypesOnFrontendLoop( | 110 FinishConfigureDataTypesOnFrontendLoop( |
79 syncer::Difference(to_download, failed_configuration_types), | 111 syncer::Difference(to_download, failed_configuration_types), |
80 syncer::Difference(to_download, failed_configuration_types), | 112 syncer::Difference(to_download, failed_configuration_types), |
81 failed_configuration_types, | 113 failed_configuration_types, |
82 ready_task); | 114 ready_task); |
83 } | 115 } |
84 | 116 |
| 117 void |
| 118 SyncBackendHostForProfileSyncTest::HandleInitializationSuccessOnFrontendLoop( |
| 119 const syncer::WeakHandle<syncer::JsBackend> js_backend, |
| 120 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener> |
| 121 debug_info_listener) { |
| 122 if (fail_initial_download_) { |
| 123 // We interrupt this successful init to bring you a simulated failure. |
| 124 initial_download_closure_ = base::Bind( |
| 125 &SyncBackendHostForProfileSyncTest:: |
| 126 HandleInitializationSuccessOnFrontendLoop, |
| 127 weak_ptr_factory_.GetWeakPtr(), |
| 128 js_backend, |
| 129 debug_info_listener); |
| 130 HandleControlTypesDownloadRetry(); |
| 131 if (synchronous_init_) |
| 132 base::MessageLoop::current()->Quit(); |
| 133 } else { |
| 134 SyncBackendHostImpl::HandleInitializationSuccessOnFrontendLoop( |
| 135 js_backend, |
| 136 debug_info_listener); |
| 137 } |
| 138 } |
| 139 |
85 } // namespace browser_sync | 140 } // namespace browser_sync |
86 | 141 |
87 syncer::TestIdFactory* TestProfileSyncService::id_factory() { | 142 syncer::TestIdFactory* TestProfileSyncService::id_factory() { |
88 return &id_factory_; | 143 return &id_factory_; |
89 } | 144 } |
90 | 145 |
| 146 browser_sync::SyncBackendHostForProfileSyncTest* |
| 147 TestProfileSyncService::GetBackendForTest() { |
| 148 return static_cast<browser_sync::SyncBackendHostForProfileSyncTest*>( |
| 149 ProfileSyncService::GetBackendForTest()); |
| 150 } |
| 151 |
91 syncer::WeakHandle<syncer::JsEventHandler> | 152 syncer::WeakHandle<syncer::JsEventHandler> |
92 TestProfileSyncService::GetJsEventHandler() { | 153 TestProfileSyncService::GetJsEventHandler() { |
93 return syncer::WeakHandle<syncer::JsEventHandler>(); | 154 return syncer::WeakHandle<syncer::JsEventHandler>(); |
94 } | 155 } |
95 | 156 |
96 TestProfileSyncService::TestProfileSyncService( | 157 TestProfileSyncService::TestProfileSyncService( |
97 ProfileSyncComponentsFactory* factory, | 158 ProfileSyncComponentsFactory* factory, |
98 Profile* profile, | 159 Profile* profile, |
99 SigninManagerBase* signin, | 160 SigninManagerBase* signin, |
100 ProfileOAuth2TokenService* oauth2_token_service, | 161 ProfileOAuth2TokenService* oauth2_token_service, |
101 ProfileSyncService::StartBehavior behavior) | 162 ProfileSyncService::StartBehavior behavior, |
| 163 bool synchronous_backend_initialization) |
102 : ProfileSyncService(factory, | 164 : ProfileSyncService(factory, |
103 profile, | 165 profile, |
104 signin, | 166 signin, |
105 oauth2_token_service, | 167 oauth2_token_service, |
106 behavior) { | 168 behavior), |
| 169 synchronous_backend_initialization_( |
| 170 synchronous_backend_initialization), |
| 171 synchronous_sync_configuration_(false), |
| 172 set_initial_sync_ended_on_init_(true), |
| 173 fail_initial_download_(false), |
| 174 storage_option_(syncer::STORAGE_IN_MEMORY) { |
107 SetSyncSetupCompleted(); | 175 SetSyncSetupCompleted(); |
108 } | 176 } |
109 | 177 |
110 TestProfileSyncService::~TestProfileSyncService() { | 178 TestProfileSyncService::~TestProfileSyncService() { |
111 } | 179 } |
112 | 180 |
113 // static | 181 // static |
114 BrowserContextKeyedService* TestProfileSyncService::BuildAutoStartAsyncInit( | 182 BrowserContextKeyedService* TestProfileSyncService::BuildAutoStartAsyncInit( |
115 content::BrowserContext* context) { | 183 content::BrowserContext* context) { |
116 Profile* profile = static_cast<Profile*>(context); | 184 Profile* profile = static_cast<Profile*>(context); |
117 SigninManagerBase* signin = | 185 SigninManagerBase* signin = |
118 SigninManagerFactory::GetForProfile(profile); | 186 SigninManagerFactory::GetForProfile(profile); |
119 ProfileOAuth2TokenService* oauth2_token_service = | 187 ProfileOAuth2TokenService* oauth2_token_service = |
120 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | 188 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
121 ProfileSyncComponentsFactoryMock* factory = | 189 ProfileSyncComponentsFactoryMock* factory = |
122 new ProfileSyncComponentsFactoryMock(); | 190 new ProfileSyncComponentsFactoryMock(); |
123 return new TestProfileSyncService(factory, | 191 return new TestProfileSyncService(factory, |
124 profile, | 192 profile, |
125 signin, | 193 signin, |
126 oauth2_token_service, | 194 oauth2_token_service, |
127 ProfileSyncService::AUTO_START); | 195 ProfileSyncService::AUTO_START, |
| 196 false); |
128 } | 197 } |
129 | 198 |
130 ProfileSyncComponentsFactoryMock* | 199 ProfileSyncComponentsFactoryMock* |
131 TestProfileSyncService::components_factory_mock() { | 200 TestProfileSyncService::components_factory_mock() { |
132 // We always create a mock factory, see Build* routines. | 201 // We always create a mock factory, see Build* routines. |
133 return static_cast<ProfileSyncComponentsFactoryMock*>(factory()); | 202 return static_cast<ProfileSyncComponentsFactoryMock*>(factory()); |
134 } | 203 } |
135 | 204 |
| 205 void TestProfileSyncService::RequestAccessToken() { |
| 206 ProfileSyncService::RequestAccessToken(); |
| 207 if (synchronous_backend_initialization_) { |
| 208 base::MessageLoop::current()->Run(); |
| 209 } |
| 210 } |
| 211 |
| 212 void TestProfileSyncService::OnGetTokenSuccess( |
| 213 const OAuth2TokenService::Request* request, |
| 214 const std::string& access_token, |
| 215 const base::Time& expiration_time) { |
| 216 ProfileSyncService::OnGetTokenSuccess(request, access_token, |
| 217 expiration_time); |
| 218 if (synchronous_backend_initialization_) { |
| 219 base::MessageLoop::current()->Quit(); |
| 220 } |
| 221 } |
| 222 |
| 223 void TestProfileSyncService::OnGetTokenFailure( |
| 224 const OAuth2TokenService::Request* request, |
| 225 const GoogleServiceAuthError& error) { |
| 226 ProfileSyncService::OnGetTokenFailure(request, error); |
| 227 if (synchronous_backend_initialization_) { |
| 228 base::MessageLoop::current()->Quit(); |
| 229 } |
| 230 } |
| 231 |
| 232 |
| 233 void TestProfileSyncService::OnBackendInitialized( |
| 234 const syncer::WeakHandle<syncer::JsBackend>& backend, |
| 235 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>& |
| 236 debug_info_listener, |
| 237 bool success) { |
| 238 ProfileSyncService::OnBackendInitialized(backend, |
| 239 debug_info_listener, |
| 240 success); |
| 241 |
| 242 // TODO(akalin): Figure out a better way to do this. |
| 243 if (synchronous_backend_initialization_) { |
| 244 base::MessageLoop::current()->Quit(); |
| 245 } |
| 246 } |
| 247 |
136 void TestProfileSyncService::OnConfigureDone( | 248 void TestProfileSyncService::OnConfigureDone( |
137 const browser_sync::DataTypeManager::ConfigureResult& result) { | 249 const browser_sync::DataTypeManager::ConfigureResult& result) { |
138 ProfileSyncService::OnConfigureDone(result); | 250 ProfileSyncService::OnConfigureDone(result); |
139 base::MessageLoop::current()->Quit(); | 251 if (!synchronous_sync_configuration_) |
| 252 base::MessageLoop::current()->Quit(); |
140 } | 253 } |
141 | 254 |
142 UserShare* TestProfileSyncService::GetUserShare() const { | 255 UserShare* TestProfileSyncService::GetUserShare() const { |
143 return backend_->GetUserShare(); | 256 return backend_->GetUserShare(); |
144 } | 257 } |
145 | 258 |
| 259 void TestProfileSyncService::dont_set_initial_sync_ended_on_init() { |
| 260 set_initial_sync_ended_on_init_ = false; |
| 261 } |
| 262 void TestProfileSyncService::set_synchronous_sync_configuration() { |
| 263 synchronous_sync_configuration_ = true; |
| 264 } |
| 265 void TestProfileSyncService::fail_initial_download() { |
| 266 fail_initial_download_ = true; |
| 267 } |
| 268 void TestProfileSyncService::set_storage_option( |
| 269 syncer::StorageOption storage_option) { |
| 270 storage_option_ = storage_option; |
| 271 } |
| 272 |
146 void TestProfileSyncService::CreateBackend() { | 273 void TestProfileSyncService::CreateBackend() { |
147 backend_.reset(new browser_sync::SyncBackendHostForProfileSyncTest( | 274 backend_.reset(new browser_sync::SyncBackendHostForProfileSyncTest( |
148 profile(), | 275 profile(), |
149 sync_prefs_.AsWeakPtr(), | 276 sync_prefs_.AsWeakPtr(), |
150 callback_)); | 277 callback_, |
| 278 set_initial_sync_ended_on_init_, |
| 279 synchronous_backend_initialization_, |
| 280 fail_initial_download_, |
| 281 storage_option_)); |
151 } | 282 } |
152 | 283 |
OLD | NEW |